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

There are three types of explicit grouping operators that you can use when you<br />

need to apply an operation to more than just one element in an expression.<br />

Also in the grouping category is the alternative match (logical OR) operator, |.<br />

This creates two or more groups of elements in the expression and applies an<br />

operation to one of the groups.<br />

Operator<br />

(expr)<br />

(?:expr)<br />

(?>expr)<br />

Usage<br />

Group regular expressions and capture tokens.<br />

Group regular expressions, but do not capture tokens.<br />

Group atomically.<br />

expr 1<br />

|expr 2 Match expression expr 1<br />

or expression expr 2<br />

.<br />

Grouping and Capture — (expr)<br />

When you enclose an expression in parentheses, <strong>MATLAB</strong> not only treats all<br />

oftheenclosedelementsasagroup,butalsocapturesatokenfromthese<br />

elements whenever a match with the input string is found. For an example of<br />

how to use this, see “Using Tokens — Example 1” on page 3-50.<br />

Grouping Only — (?:expr)<br />

Use (?:expr) to group a nonvowel (consonant, numeric, whitespace,<br />

punctuation, etc.) followed by a vowel in the palindrome pstr. Specifyatleast<br />

two consecutive occurrences ({2,}) of this group. Return the starting and<br />

ending indices of the matched substrings:<br />

pstr = 'Marge lets Norah see Sharon''s telegram';<br />

expr = '(?:[^aeiou][aeiou]){2,}';<br />

[mat ix1 ix2] = regexp(pstr, expr, 'match', 'start', 'end')<br />

mat =<br />

'Nora' 'haro' 'tele'<br />

ix1 =<br />

12 23 31<br />

ix2 =<br />

15 26 34<br />

3-38

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

Saved successfully!

Ooh no, something went wrong!