MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Ordinal Token Operator Description Example


\N Match the Nth token. '<(\w+).>.</\1>' captures
tokens for HTML tags, such as
'title' from the text
'Some text'.


(?(N)expr1|expr2) If the Nth token is found, then
match expr1. Otherwise, match
expr2.


'Mr(s?)\..*?(?(1)her|his)
\w*' matches text that includes
her when the text begins with Mrs,
or that includes his when the text
begins with Mr.

Named Token Operator Description Example


(?expr) Capture in a named token the
characters that match the enclosed
expression.


'(?<month>\d+)-(?<day>\d+)-
(?<yr>\d+)' creates named
tokens for the month, day, and year
in an input date of the form mm-dd-
yy.

\k Match the token referred to by
name.


'<(?<tag>\w+).*>.*</
\k<tag>>' captures tokens for
HTML tags, such as 'title' from
the text '<title>Some text</
title>'.

(?(name)expr1|
expr2)


If the named token is found, then
match expr1. Otherwise, match
expr2.

'Mr(?<sex>s?)\..*?(?
(sex)her|his) \w*' matches
text that includes her when the text
begins with Mrs, or that includes
his when the text begins with Mr.

NoteIf an expression has nested parentheses, MATLAB captures tokens that correspond
to the outermost set of parentheses. For example, given the search pattern '(and(y|
rew))', MATLAB creates a token for 'andrew' but not for 'y' or 'rew'.

For more information, see “Tokens in Regular Expressions” on page 2-61.

Regular Expressions
Free download pdf