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

Matches ' rain', 'Spain', ' main', and'plain'.<br />

Returning Strings Rather than Indices. Here is the same example, this<br />

time specifying the command qualifier 'match'. Inthiscase,regexp returns<br />

the text of the matching strings rather than the starting index:<br />

regexp(str, '..ain', 'match')<br />

ans =<br />

' rain' 'Spain' ' main' 'plain'<br />

Selected Characters — [c1c2c3]<br />

Use [c 1<br />

c 2<br />

c 3<br />

] in an expression to match selected characters r, p, orm followed<br />

by 'ain'. Specify two qualifiers this time, 'match' and 'start', alongwith<br />

an output argument for each, mat and idx. This returns the matching strings<br />

and the starting indices of those strings:<br />

[mat idx] = regexp(str, '[rpm]ain', 'match', 'start')<br />

mat =<br />

'rain' 'pain' 'main'<br />

idx =<br />

5 14 25<br />

Range of Characters — [c1 - c2]<br />

Use [c 1<br />

-c 2<br />

] in an expression to find words that begin with a letter in the<br />

range of A through Z:<br />

[mat idx] = regexp(str, '[A-Z]\w*', 'match', 'start')<br />

mat =<br />

'The' 'Spain'<br />

idx =<br />

1 13<br />

3-35

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

Saved successfully!

Ooh no, something went wrong!