23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Regular Expressions<br />

tok{:}<br />

ans =<br />

'C:' 'WINNT' '' 'bpascal' 'LOCALS~1' 'Temp'<br />

The third token extent returned in the variable ext has the starting index<br />

set to 10, which is where the nonmatching substring, Profiles, beginsinthe<br />

string. The ending extent index is set to one less than the starting index, or 9:<br />

ext{:}<br />

ans =<br />

1 2<br />

4 8<br />

10 9<br />

19 25<br />

27 34<br />

36 39<br />

Using Tokens in a Replacement String<br />

When using tokens in a replacement string, reference them using $1, $2, etc.<br />

instead of \1, \2, etc. This example captures two tokens and reverses their<br />

order. The first, $1, is'Norma Jean' and the second, $2, is'Baker'. Note<br />

that regexprep returns the modified string, not a vector of starting indices.<br />

regexprep('Norma Jean Baker', '(\w+\s\w+)\s(\w+)', '$2, $1')<br />

ans =<br />

Baker, Norma Jean<br />

Named Capture<br />

If you use a lot of tokens in your expressions, it may be helpful to assign them<br />

names rather than having to keep track of which token number is assigned<br />

to which token. Use the following operator to assign a name to a token that<br />

finds a match.<br />

3-53

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

Saved successfully!

Ooh no, something went wrong!