How do I split a string into two strings using a space + number delimiter and/or search for the delimiter by counting right to left?
4 views (last 30 days)
Show older comments
Hello,
I have data entries that look like this:
Column 1 | Column 2
Default | 8
Default 2 | 9
Default 3 | 3
Default 4 | 5
Card 1 | 6
Card 1 2 | 10
Card 1 3 | 3
Card 1 4 | 2
Card 1 5 | 9
Card 1 6 | 3
Card 2 | 2
Card 2 2 | 2
Card 2 3 | 6
Card 2 4 | 8
Card 2 5 | 6
Card 2 6 | 9
I want to be able to split all the column 1 entries into two columns, the base part and the number. The number I'm interested in is ONLY the last number after the last space. The base part is allowed to have a number in it.
The only common thing between all the entries is that they end with [space][number] and number can be between 1 and 10000.
For the entries similar to the (1,1) entry above, I need to get a 0 at the end of that string as well.
I think I can use 'strsplit' or 'regexp' to do this, but I'm not sure how to tell Matlab that I need it to split at the end of the string effectively (have it count in from the right).
Thanks for any assistance you are able to give,
Matt
0 Comments
Accepted Answer
Guillaume
on 6 Mar 2019
Not sure what exactly the format of your data is (use valid matlab syntax to construct your examples to avoid ambiguity). Is column 2 relevant here? Also, not sure what "For the entries similar to the (1,1) entry above, I need to get a 0 at the end of that string as well." mean.
To split a char vector 'Card 2 6' or similar into {'Card 2', '6'}:
str = 'Card 2 6';
split = regexp(str, '(.*?) (\d+)$', 'tokens')
This basically creates two tokens, the 2nd token must be preceded by a space, must consist of digits and attached to the end of the input (because of the $. The first token is everything else before the space.
4 Comments
Guillaume
on 11 Mar 2019
Ok, now I understand. Case 1 is significantly different from the other cases that it would be hard to cover it with the same regex. Maybe with a dynamic expression but for clarity and simplicity, it's better to either handle separately or simply do the appending and then handle it as case 2.
There's actually no difference between 2 and 3. You just want the last number split. As for case 4, it's simply a matter of considering punctuation the same as space.
%test cases:
tests = {'Default'; 'Internal Air 25'; 'Chassis Body - Chassis 1 16'; 'Shell - Chassis 3 - 14'};
%append number to case 1:
normalised = regexprep(tests, '\D$', '$0 0'); %replace strings ending with a non-digit by that non-digit followed by a space and 0
%splitting
splitted = regexp(normalised, '(.*?)[- ]+(\d+)$', 'tokens', 'once');
splitted = vertcat(splitted{:})
For the splitting, all the punctuation characters that you want to consider go inside the [] . I put the space and - in there. Add whatever other characters you want to consider. Note that - needs to be first or last in the square brackets (otherwise it has special meaning). Some other characters may also have special meaning (\ and ^ for example), read the regexp doc to check.
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!