MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 232 and 233: 3 Basic Program Components Evaluati
- Page 234 and 235: 3 Basic Program Components Keywords
- Page 236 and 237: 3 Basic Program Components Hereares
- Page 238 and 239: 3 Basic Program Components Operator
- Page 240 and 241: 3 Basic Program Components To test
- Page 242 and 243: 3 Basic Program Components Function
- Page 244 and 245: 3 Basic Program Components 5 11 10
- Page 246 and 247: 3 Basic Program Components Advantag
- Page 248 and 249: 3 Basic Program Components MATLAB E
- Page 250 and 251: 3 Basic Program Components This M-f
- Page 252 and 253: 3 Basic Program Components • “S
- Page 254 and 255: 3 Basic Program Components Operator
- Page 256 and 257: 3 Basic Program Components Word and
- Page 258 and 259: 3 Basic Program Components There ar
- Page 260 and 261: 3 Basic Program Components regexp(p
- Page 262 and 263: 3 Basic Program Components s = e =
- Page 264 and 265: 3 Basic Program Components ¥ dolla
- Page 266 and 267: 3 Basic Program Components Operator
- Page 268 and 269: 3 Basic Program Components Exact, M
- Page 270 and 271: 3 Basic Program Components As a sim
- Page 272 and 273: 3 Basic Program Components ans = De
- Page 274 and 275: 3 Basic Program Components Operator
- Page 276 and 277: 3 Basic Program Components Operator
- Page 278 and 279: 3 Basic Program Components Conditio
- Page 280 and 281: 3 Basic Program Components ans = g1
- Page 284 and 285: 3 Basic Program Components at each
- Page 286 and 287: 3 Basic Program Components function
- Page 288 and 289: 3 Basic Program Components Operator
- Page 290 and 291: 3 Basic Program Components Find con
- Page 292 and 293: 3 Basic Program Components statemen
- Page 294 and 295: 3 Basic Program Components Characte
- Page 296 and 297: 3 Basic Program Components Quantifi
- Page 298 and 299: 3 Basic Program Components Named To
- Page 300 and 301: 3 Basic Program Components Comma-Se
- Page 302 and 303: 3 Basic Program Components 40 Thisi
- Page 304 and 305: 3 Basic Program Components ans = 10
- Page 306 and 307: 3 Basic Program Components X = -pi:
- Page 308 and 309: 3 Basic Program Components Program
- Page 310 and 311: 3 Basic Program Components if A S1
- Page 312 and 313: 3 Basic Program Components end disp
- Page 314 and 315: 3 Basic Program Components while St
- Page 316 and 317: 3 Basic Program Components Program
- Page 318 and 319: 3 Basic Program Components Asterisk
- Page 320 and 321: 3 Basic Program Components Numeric
- Page 322 and 323: 3 Basic Program Components Cell Arr
- Page 324 and 325: 3 Basic Program Components Dynamic
- Page 326 and 327: 3 Basic Program Components The purp
- Page 328 and 329: 3 Basic Program Components Function
- Page 330 and 331: 3 Basic Program Components MATLAB F
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