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.

3 Basic Program Components<br />

As a simple example, if you wanted to search for identical sequential letters<br />

in a string, you could capture the first letter as a token and then search for<br />

a matching character immediately afterwards. In the expression shown<br />

below, the (\S) phrase creates a token whenever regexp matches any<br />

non-whitespace character in the string. The second part of the expression,<br />

'\1', looks for a second instance of the same character immediately following<br />

the first:<br />

poestr = ['While I nodded, nearly napping, ' ...<br />

'suddenly there came a tapping,'];<br />

[mat tok ext] = regexp(poestr, '(\S)\1', 'match', ...<br />

'tokens', 'tokenExtents');<br />

mat<br />

mat =<br />

'dd' 'pp' 'dd' 'pp'<br />

The tokens returned in cell array tok are:<br />

'd', 'p', 'd', 'p'<br />

Starting and ending indices for each token in the input string poestr are:<br />

11 11, 26 26, 35 35, 57 57<br />

UsingTokens—Example1<br />

Here is an example of how tokens are assigned values. Suppose that you<br />

are going to search the following text:<br />

andy ted bob jim andrew andy ted mark<br />

You choose to search the above text with the following search pattern:<br />

and(y|rew)|(t)e(d)<br />

This pattern has three parenthetical expressions that generate tokens. When<br />

you finally perform the search, the following tokens are generated for each<br />

match.<br />

3-50

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

Saved successfully!

Ooh no, something went wrong!