Reputation: 5823
I have the following line where I want to capture several strings via a regular expression:
l = '15 1180 62444 e0e0 049c f3ec 104';
I was augmenting the following regexp to operate on this line:
d = regexpi(l, '([0-9a-f]+)\s?', 'tokens');
When executing this line, I get that length(d) == 7
. Shouldn't the regexp only match the first occurrence, i.e., length(d) == 1
and d{1} == '15'
?
For what it's worth, I used the same regexp in Perl and found that it matches only the first instance of the pattern (which is what I expected):
my $l = ... #defined above already
$l =~ m/([0-9a-f]+)\s?/i;
if (! defined($2)){ # $2, $3, ..., $n will be defined for n matches
print "Didn't match twice!\n"; # this prints when I execute the script
}
Upvotes: 0
Views: 748
Reputation: 22457
Per MatLab's regexpi
page, the first page that you should have looked for, regexpi
returns the starting point of each substring that matches the character patterns specified by the regular expression.
You get an array of starting positions, and the first element d{1}
would be the offset of the first match, not its 'value'.
Upvotes: 0
Reputation: 70732
As stated with using regexpi
...
start = regexpi(str,expr)
returns a row vector, start, containing the indices of the substrings in str that match the regular expression string, expr, regardless of case.
When either str or expr is a cell array of strings, regexpi returns an m-by-n cell array of row vectors of indices, where m is the the number of strings in str and n is the number of regular expression patterns in expr.
[start,finish] = regexpi(str,expr)
returns an additional row vector finish, that contains the indices of the last character of the corresponding substrings in start.
[start,finish,tokens] = regexpi(str,expr)
returns a 1-by-n cell array, tokens, of beginining and ending indices of tokens within the corresponding substrings in start and finish. Tokens are denoted by parentheses in the expression, expr.
[...] = regexpi(str,expr,'once')
finds just the first match. (By default, regexp returns all matches.) If no matches are found, then all return values are empty.
Upvotes: 2