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

Make regexprep more specific to your needs by specifying any of a number<br />

of options with the command. See the regexprep reference page for more<br />

information on these options.<br />

Handling Multiple Strings<br />

You can use any of the <strong>MATLAB</strong> regular expression functions with cell arrays<br />

of strings as well as with single strings. Any or all of the input parameters<br />

(the string, expression, or replacement string) can be a cell array of strings.<br />

The regexp function requires that the string and expression arrays have<br />

the same number of elements. The regexprep function requires that the<br />

expression and replacement arrays have the same number of elements. (The<br />

cell arrays do not have to have the same shape.)<br />

Whenever either input argument in a call to regexp, or the first input<br />

argument in a call to regexprep function is a cell array, all output values are<br />

cell arrays of the same size.<br />

This section covers the following topics:<br />

• “Finding a Single Pattern in Multiple Strings” on page 3-69<br />

• “Finding Multiple Patterns in Multiple Strings” on page 3-70<br />

• “Replacing Multiple Strings” on page 3-71<br />

Finding a Single Pattern in Multiple Strings<br />

The example shown here uses the regexp function on a cell array of strings<br />

cstr. It searches each string of the cell array for consecutive matching letters<br />

(e.g., 'oo'). The function returns a cell array of the same size as the input<br />

array. Each row of the return array contains the indices for which there was a<br />

match against the input cell array.<br />

Here is the input cell array:<br />

cstr = { ...<br />

'Whose woods these are I think I know.' ; ...<br />

'His house is in the village though;' ; ...<br />

'He will not see me stopping here' ; ...<br />

'To watch his woods fill up with snow.'};<br />

3-69

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

Saved successfully!

Ooh no, something went wrong!