MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Quantifier Matches the expression when it
occurs...


Example

expr{m,} At least m times consecutively.


{0,} and {1,} are equivalent to * and
+, respectively.

'<a href="\w{1,}\.html">' matches
an <a> HTML tag when the file name
contains one or more characters.

expr{n} Exactly n times consecutively.


Equivalent to {n,n}.

'\d{4}' matches four consecutive digits.

Quantifiers can appear in three modes, described in the following table. q represents any
of the quantifiers in the previous table.

Mode Description Example


exprq Greedy expression: match as many
characters as possible.


Given the text
'<tr><td><p>text</p></td>', the
expression '</?t.*>' matches all
characters between <tr and /td>:

'<tr><td><p>text</p></td>'

exprq? Lazy expression: match as few
characters as necessary.


Given the
text'<tr><td><p>text</p></td>',
the expression '</?t.*?>' ends each
match at the first occurrence of the
closing angle bracket (>):

'<tr>' '<td>' '</td>'

exprq+ Possessive expression: match as much as
possible, but do not rescan any portions
of the text.


Given the
text'<tr><td><p>text</p></td>',
the expression '</?t.*+>' does not
return any matches, because the closing
angle bracket is captured using .*, and
is not rescanned.

Grouping Operators

Grouping operators allow you to capture tokens, apply one operator to multiple elements,
or disable backtracking in a specific group.

Regular Expressions
Free download pdf