23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Regular Expressions<br />

This section covers<br />

• “Operators Used with Tokens” on page 3-49<br />

• “Introduction to Using Tokens” on page 3-49<br />

• “Using Tokens — Example 1” on page 3-50<br />

• “Using Tokens — Example 2” on page 3-51<br />

• “TokensThatAreNotMatched”onpage3-52<br />

• “Using Tokens in a Replacement String” on page 3-53<br />

Operators Used with Tokens<br />

Here are the operators you can use with tokens in <strong>MATLAB</strong>.<br />

Operator<br />

(expr)<br />

Usage<br />

Capture in a token all characters matched by the<br />

expression within the parentheses.<br />

\N Match the N th token generated by this command. That is,<br />

use \1 to match the first token, \2 to match the second,<br />

and so on.<br />

$N Insert the match for the N th tokeninthereplacement<br />

string. Used only by the regexprep function. If N<br />

is equal to zero, then insert the entire match in the<br />

replacement string.<br />

(?(N)s1|s2)<br />

If N th token is found, then match s1, elsematchs2<br />

Introduction to Using Tokens<br />

You can turn any pattern being matched into a token by enclosing the<br />

pattern in parentheses within the expression. For example, to create a token<br />

for a dollar amount, you could use ’(\$\d+)’. Each token in the expression<br />

is assigned a number, starting from 1, going from left to right. To make a<br />

reference to a token later in the expression, refer to it using a backslash<br />

followed by the token number. For example, when referencing a token<br />

generated by the third set of parentheses in the expression, use \3.<br />

3-49

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!