I have a string that either has a number or the letter a
, possibly followed byr
or l
.
In MATLAB the following regexp returns as
>> regexp('10r', '([0-9]*|a)(l|r)*', 'match')
ans =
'10r'
I would expect 10
and r
separately, because I have two capture groups. Is there a way to get a cell array with both returned independently? I can't see it in the documentation.
You want 'tokens' instead of 'match'
>> toks = regexp('10r', '([0-9]*|a)(l|r)*', 'tokens');
>> toks{1}
ans =
'10' 'r'
Or if you want to get fancy, name the tokens and get a struct array:
>> toks = regexp('10r', '(?<number>[0-9]*|a)(?<letter>l|r)*', 'names');
>> toks
toks =
number: '10'
letter: 'r'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With