MATLAB Programming
MATLAB Programming MATLAB Programming
3 Basic Program Components at each iteration. This forced failure is necessary if you want to trace the steps that MATLAB is taking to resolve the expression. MATLAB makes a number of passes through the input string, each time trying another combination of letters to see if a fit better than last match can be found. On any passes in which no matches are found, the test results in an empty string. The dynamic script (?@if(~isempty($&))) serves to omit these strings from the matches cell array: matches = {}; expr = ['(Euler\s)?(Cauchy\s)?(Boole)?(?@if(~isempty($&)),' ... 'matches{end+1}=$&;end)(?!)']; regexp('Euler Cauchy Boole', expr); matches matches = 'Euler Cauchy Boole' 'Euler Cauchy ' 'Euler ' 'Cauchy Boole' 'Cauchy ' 'Boole' The metacharacters $& (or the equivalent $0), $`, and$' refer to that part of the input string that is currently a match, all characters that precede the current match, and all characters to follow the current match, respectively. These metacharacters are sometimes useful when working with dynamic expressions, particularly those that employ the (?@cmd) operator. This example parses the input string looking for the letter g. At each iteration through the string, regexp compares the current character with g, andnot finding it, advances to the next character. The example tracks the progress of scan through the string by marking the current location being parsed with a ^ character. (The $` and $· metacharacters capture that part of the string that precedes and follows the current parsing location. You need two single-quotation marks ($'') toexpressthesequence$· when it appears within a string.) str = 'abcdefghij'; expr = '(?@disp(sprintf(''starting match: [%s^%s]'',$`,$'')))g'; regexp(str, expr, 'once'); 3-64
Regular Expressions starting match: [^abcdefghij] starting match: [a^bcdefghij] starting match: [ab^cdefghij] starting match: [abc^defghij] starting match: [abcd^efghij] starting match: [abcde^fghij] starting match: [abcdef^ghij] Dynamic Operators for the Replacement Expression The three types of dynamic expressions discussed above can be used only in the match expression (second input) argument of the regular expression functions. MATLAB provides one more type of dynamic expression; this one is for use in a replacement string (third input) argument of the regexprep function. Dynamic Commands that Modify the Replacement Expression — ${cmd}. The ${cmd} operator modifies the contents of a regular expression replacement string, making this string adaptable to parameters in the input string that might vary from one use to the next. As with the other dynamic expressions used in MATLAB, you can include any number of these expressions within the overall replacement expression. In the regexprep call shown here, the replacement string is '${convert($1,$2)}'. In this case, the entire replacement string is a dynamic expression: regexprep('This highway is 125 miles long', ... '(\d+\.?\d*)\W(\w+)', '${convert($1,$2)}') The dynamic expression tells MATLAB to execute an M-file function named convert using the two tokens (\d+\.?\d*) and (\w+), derived from the string being matched, as input arguments in the call to convert. The replacement string requires a dynamic expression because the values of $1 and $2 are generated at runtime. The following example defines the M-file named convert that converts measurements from imperial units to metric. To convert values from the string being parsed, regexprep calls the convert function, passing in values for the quantity to be converted and name of the imperial unit: 3-65
- 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 282 and 283: 3 Basic Program Components The dyna
- 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
- Page 332 and 333: 3 Basic Program Components Forcing
3 Basic Program Components<br />
at each iteration. This forced failure is necessary if you want to trace the steps<br />
that <strong>MATLAB</strong> is taking to resolve the expression.<br />
<strong>MATLAB</strong> makes a number of passes through the input string, each time<br />
trying another combination of letters to see if a fit better than last match can<br />
be found. On any passes in which no matches are found, the test results in<br />
an empty string. The dynamic script (?@if(~isempty($&))) serves to omit<br />
these strings from the matches cell array:<br />
matches = {};<br />
expr = ['(Euler\s)?(Cauchy\s)?(Boole)?(?@if(~isempty($&)),' ...<br />
'matches{end+1}=$&;end)(?!)'];<br />
regexp('Euler Cauchy Boole', expr);<br />
matches<br />
matches =<br />
'Euler Cauchy Boole' 'Euler Cauchy ' 'Euler '<br />
'Cauchy Boole' 'Cauchy ' 'Boole'<br />
The metacharacters $& (or the equivalent $0), $`, and$' refer to that part<br />
of the input string that is currently a match, all characters that precede the<br />
current match, and all characters to follow the current match, respectively.<br />
These metacharacters are sometimes useful when working with dynamic<br />
expressions, particularly those that employ the (?@cmd) operator.<br />
This example parses the input string looking for the letter g. At each iteration<br />
through the string, regexp compares the current character with g, andnot<br />
finding it, advances to the next character. The example tracks the progress of<br />
scan through the string by marking the current location being parsed with a<br />
^ character.<br />
(The $` and $· metacharacters capture that part of the string that precedes<br />
and follows the current parsing location. You need two single-quotation marks<br />
($'') toexpressthesequence$· when it appears within a string.)<br />
str = 'abcdefghij';<br />
expr = '(?@disp(sprintf(''starting match: [%s^%s]'',$`,$'')))g';<br />
regexp(str, expr, 'once');<br />
3-64