Programming Fundamentals - The MathWorks - #301

/ 840


catalogue search
P. 301
P. 302
P. 303
P. 304
P. 305
P. 306
P. 307
P. 308
P. 309
P. 310
P. 311
P. 312
P. 313
P. 314
P. 315
P. 316
P. 317
P. 318
P. 319
P. 320
P. 321
P. 322
P. 323
P. 324
P. 325
P. 326
P. 327
P. 328
P. 329
P. 330
P. 331
P. 332
P. 333
P. 334
P. 335
P. 336
P. 337
P. 338
P. 339
P. 340
P. 341
P. 342
P. 343
P. 344
P. 345
P. 346
P. 347
P. 348
P. 349
P. 350


See other catalogues for The MathWorks

Text version of the page
Regular Expressions
Labeling Your Output
Named tokens can also be useful in labeling the output from the MATLAB regular expression functions. This is especially true when you are processing numerous strings.
This example parses different pieces ofstreet addresses from several strings. A short name is assigned to each token in the expression string:
str1 = '134 Main Street, Boulder, CO, 14923';
str2 = '26 Walnut Road, Topeka, KA, 25384';
str3 = '847 Industrial Drive, Elizabeth, NJ, 73548';
p1 = '(?<adrs>\d+\s\S+\s(Road|Street|Avenue|Drive))';
p2 = '(?<city>[A-Z][a-z]+)';
p3 = '(?<state>[A-Z]{2})';
p4 = '(?<zip>\d{5})';
expr = [p1 ', ' p2 ', ' p3 ', ' p4];
As the following results demonstrate, you can make your output easier to work with by using named tokens:
loc1 = regexp(str1, expr, 'names')
loc1 =
adrs: '134 Main Street' city: 'Boulder' state: 'CO' zip: '14923'
loc2 = regexp(str2, expr, 'names')
loc2 =
adrs: '26 Walnut Road' city: 'Topeka'
state: 'KA' zip: '25384'
loc3 = regexp(str3, expr, 'names') loc3 =
adrs: '847 Industrial Drive' city: 'Elizabeth'
3-59

pageCatalog pdf di En 2012-06-22-01