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

Operator<br />

Usage<br />

\r Carriage return<br />

\t Horizontal tab<br />

\v Vertical tab<br />

\oN or \o{N}<br />

\xN or \x{N}<br />

\char<br />

Character of octal value N<br />

Character of hexadecimal value N<br />

If a character has special meaning in a regular expression,<br />

precede it with backslash (\) to match it literally.<br />

Octal and Hexadecimal — \o, \x<br />

Use \x and \o in an expression to find a comma (hex 2C) followedbyaspace<br />

(octal 40) followed by the character 2:<br />

numstr = 'Easy as 1, 2, 3';<br />

[mat idx] = regexp(numstr, '\x2C\o{40}2', 'match', 'start')<br />

mat =<br />

', 2'<br />

idx =<br />

10<br />

Grouping Operators<br />

When you need to use one of the regular expression operators on a number of<br />

consecutive elements in an expression, group these elements together with<br />

one of the grouping operators and apply the operation to the entire group. For<br />

example, this command matches a capital letter followed by a numeral and<br />

then an optional space character. These elements have to occur at least two<br />

times in succession for there to be a match. To apply the {2,} multiplier to<br />

all three consecutive characters, you can first make a group of the characters<br />

and then apply the (?:) quantifier to this group:<br />

regexp('B5 A2 6F 63 R6 P4 B2 BC', '(?:[A-Z]\d\s?){2,}', 'match')<br />

ans =<br />

'B5 A2 ' 'R6 P4 B2 '<br />

3-37

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

Saved successfully!

Ooh no, something went wrong!