Reputation: 569
I would like to parse a string in MATLAB that has a prespecified format. For instance, I would like to parse the following string,
s = 'a(1,2)+b_c15_d.ext'
so that I can get the substring before '(', i.e., the substring 'a', the numbers that are between the parentheses, i.e, 1 and 2, and so on (the substring 'b' between the '+' and '_', and the substring 'd' before the extension).
I can do it using regexp
and split
, but is there a more convenient way of doing this?
Upvotes: 0
Views: 91
Reputation: 30046
Using regexp
and split
really isn't that inconvenient, you can do it nicely in 2 lines
r = regexp(s,'[()\+_(_c)(.ext)]', 'split');
r = r(~cellfun(@isempty, r))
>> r = {'a' '1,2' 'b' '15' 'd'}
I'm not sure if this is similar to what you'd already tried, since you didn't post any code.
Upvotes: 2
Reputation: 1482
I could do it using regexp and 'split', but I hope there is a more convenient way of doing so.
textscan
offers a simple, perhaps convenient, alternative to regexp
and split
.
a = textscan(s,'%c(%d,%d)+%c_%c%d_%c.ext');
The results are in order as:
>> a =
'a' [1] [2] 'b' 'c' [15] 'd'
If you are going to have more than one character for your substring, %c
to a modified %[]
(character string) depending on your needs. For example:
a = textscan(s,'%[^(](%d,%d)+%[^_]_%[^_]_%[^.].ext')
The %[^(]
above will collect characters from the string until the string ends or a '('
is reached on the string and place the result in the first cell of the output. The '('
is still on the string/stream however, so we remove it directly, by reading it, as shown.
sscanf
is another option, but manipulating the output format is more involved.
Upvotes: 2