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 />

specify three tokens in an expression (that is, if you enclose three parts of<br />

the expression in parentheses), then you would refer to these tokens in a<br />

condition statement as 1, 2, and3.<br />

The following example uses the conditional statement (?(1)her|his) to<br />

match the string regardless of the gender used. You could translate this into<br />

the phrase, “if token 1 is found (i.e., Mr is followed by the letter s), then<br />

match her, else match his.”<br />

expr = 'Mr(s?)\..*?(?(1)her|his) son';<br />

[mat tok] = regexp('Mr. Clark went to see his son', ...<br />

expr, 'match', 'tokens')<br />

mat =<br />

'Mr. Clark went to see his son'<br />

tok =<br />

{1x2 cell}<br />

tok{:}<br />

ans =<br />

''<br />

'his'<br />

In the second part of the example, the token s is found and <strong>MATLAB</strong> matches<br />

the word her:<br />

[mat tok] = regexp('Mrs. Clark went to see her son', ...<br />

expr, 'match', 'tokens')<br />

mat =<br />

'Mrs. Clark went to see her son'<br />

tok =<br />

{1x2 cell}<br />

tok{:}<br />

ans =<br />

's'<br />

'her'<br />

Note When referring to a token within a condition, use just the number of<br />

the token. For example, refer to token 2 by using the number 2 alone, and<br />

not \2 or $2.<br />

3-57

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

Saved successfully!

Ooh no, something went wrong!