MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

3 Basic Program Components The dynamic expression reverses the order of the letters that make up the string, and then attempts to match as much of the reversed-order string as possible. This requires a dynamic expression because the value for $1 relies on the value of the token (.{3,}): % Put the string in lowercase. str = lower(... 'Find the palindrome Never Odd or Even in this string'); % Remove all nonword characters. str = regexprep(str, '\W*', '') str = findthepalindromeneveroddoreveninthisstring % Now locate the palindrome within the string. palstr = regexp(str, '(.{3,}).?(??@fliplr($1))', 'match') str = 'neveroddoreven' Dynamic expressions in MATLAB have access to the currently active workspace. This means that you can change any of the functions or variables used in a dynamic expression just by changing variables in the workspace. Repeat the last command of the example above, but this time define the function to be called within the expression using a function handle stored in the base workspace: fun = @fliplr; palstr = regexp(str, '(.{3,}).?(??@fun($1))', 'match') palstr = 'neveroddoreven' Dynamic Commands that Serve a Functional Purpose — (?@cmd). The (?@cmd) operator specifies a MATLAB command that regexp or regexprep is to run while parsing the overall match expression. Unlike the other dynamic expressions in MATLAB, this operator does not alter the contents of the expression it is used in. Instead, you can use this functionality to get MATLABtoreportjustwhatstepsit’stakingasitparsesthecontentsofone of your regular expressions. 3-62

Regular Expressions The following example parses a word for zero or more characters followed by two identical characters followed again by zero or more characters: regexp('mississippi', '\w*(\w)\1\w*', 'match') ans = 'mississippi' To track the exact steps that MATLAB takes in determining the match, the example inserts a short script (?@disp($1)) in the expression to display the characters that finally constitute the match. Because the example uses greedy quantifiers, MATLAB attempts to match as much of the string as possible. So, even though MATLAB finds a match toward the beginning of the string, it continues to look for more matches until it arrives at the very end of the string. From there, it backs up through the letters i then p and the next p, stopping at that point because the match is finally satisfied: regexp('mississippi', '\w*(\w)(?@disp($1))\1\w*'); i p p Now try the same example again, this time making the first quantifier lazy (*?). Again, MATLAB makes the same match: regexp('mississippi', '\w*?(\w)\1\w*', 'match') ans = 'mississippi' But by inserting a dynamic script, you can see that this time, MATLAB has matched the string quite differently. In this case, MATLAB uses the very first match it can find, and does not even consider the rest of the string: regexp('mississippi', '\w*?(\w)(?@disp($1))\1\w*';) m i s Todemonstratehowversatilethistypeof dynamic expression can be, consider the next example that progressively assembles a cell array as MATLAB iteratively parses the input string. The (?!) metacharacter found at the end of the expression is actually an empty lookahead operator, and forces a failure 3-63

Regular Expressions<br />

The following example parses a word for zero or more characters followed by<br />

two identical characters followed again by zero or more characters:<br />

regexp('mississippi', '\w*(\w)\1\w*', 'match')<br />

ans =<br />

'mississippi'<br />

To track the exact steps that <strong>MATLAB</strong> takes in determining the match, the<br />

example inserts a short script (?@disp($1)) in the expression to display the<br />

characters that finally constitute the match. Because the example uses greedy<br />

quantifiers, <strong>MATLAB</strong> attempts to match as much of the string as possible.<br />

So, even though <strong>MATLAB</strong> finds a match toward the beginning of the string,<br />

it continues to look for more matches until it arrives at the very end of the<br />

string. From there, it backs up through the letters i then p and the next p,<br />

stopping at that point because the match is finally satisfied:<br />

regexp('mississippi', '\w*(\w)(?@disp($1))\1\w*');<br />

i<br />

p<br />

p<br />

Now try the same example again, this time making the first quantifier lazy<br />

(*?). Again, <strong>MATLAB</strong> makes the same match:<br />

regexp('mississippi', '\w*?(\w)\1\w*', 'match')<br />

ans =<br />

'mississippi'<br />

But by inserting a dynamic script, you can see that this time, <strong>MATLAB</strong> has<br />

matched the string quite differently. In this case, <strong>MATLAB</strong> uses the very first<br />

match it can find, and does not even consider the rest of the string:<br />

regexp('mississippi', '\w*?(\w)(?@disp($1))\1\w*';)<br />

m<br />

i<br />

s<br />

Todemonstratehowversatilethistypeof dynamic expression can be, consider<br />

the next example that progressively assembles a cell array as <strong>MATLAB</strong><br />

iteratively parses the input string. The (?!) metacharacter found at the end<br />

of the expression is actually an empty lookahead operator, and forces a failure<br />

3-63

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

Saved successfully!

Ooh no, something went wrong!