Token Operators
Tokens are portions of the matched text that you define by enclosing part of the regular expression in
parentheses. You can refer to a token by its sequence in the text (an ordinal token), or assign names
to tokens for easier code maintenance and readable output.
Ordinal Token Operator
Description
Example
(expr)
Capture in a token the characters that
match the enclosed expression.
'Joh?n\s(\w*)' captures a token that
contains the last name of any person
with the first name John or Jon.
\N
Match the Nth token.
'<(\w+).*>.*' captures tokens
for HTML tags, such as 'title' from
the text 'Some text</<br /> title>'.<br /> (?(N)expr1|expr2)<br /> If the Nth token is found, then match<br /> expr1. Otherwise, match expr2.<br /> 'Mr(s?)\..*?(?(1)her|his) \w*'<br /> matches text that includes her when<br /> the text begins with Mrs, or that<br /> includes his when the text begins with<br /> Mr.<br /> Named Token Operator<br /> Description<br /> Example<br /> (?<name>expr)<br /> Capture in a named token the<br /> characters that match the enclosed<br /> expression.<br /> '(?<month>\d+)-(?<day>\d+)-(?<br /> <yr>\d+)' creates named tokens for<br /> the month, day, and year in an input<br /> date of the form mm-dd-yy.<br /> \k<name><br /> Match the token referred to by name. '<(?<tag>\w+).*>.*</\k<tag>>'<br /> captures tokens for HTML tags, such as<br /> 'title' from the text '<title>Some<br /> text'.
(?(name)expr1|expr2)
If the named token is found, then
match expr1. Otherwise, match
expr2.
'Mr(?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.
Note If 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-66.
Dynamic Expressions
Dynamic expressions allow you to execute a MATLAB command or a regular expression to determine
the text to match.
The parentheses that enclose dynamic expressions do not create a capturing group.
2 Program Components
2-60
Précédent

- 110/1450

Suivant