MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

ans =


1×1 cell array


{'mississippi'}


To track the exact steps that MATLAB takes in determining the match, the example
inserts a short script (?@disp($1)) in the expression to display the characters that
finally constitute the match. Because the example uses greedy quantifiers, MATLAB
attempts to match as much of the character vector as possible. So, even though MATLAB
finds a match toward the beginning of the string, it continues to look for more matches
until it arrives at the very end of the string. From there, it backs up through the letters i
then p and the next p, stopping at that point because the match is finally satisfied:


regexp('mississippi', '\w(\w)(?@disp($1))\1\w', 'match')


i
p
p


ans =


1×1 cell array


{'mississippi'}


Now try the same example again, this time making the first quantifier lazy (*?). Again,
MATLAB makes the same match:


regexp('mississippi', '\w?(\w)\1\w', 'match')


ans =


1×1 cell array


{'mississippi'}


But by inserting a dynamic script, you can see that this time, MATLAB has matched the
text quite differently. In this case, MATLAB uses the very first match it can find, and does
not even consider the rest of the text:


regexp('mississippi', '\w?(\w)(?@disp($1))\1\w', 'match')


m
i


Dynamic Regular Expressions
Free download pdf