MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

For example, parse different parts of street addresses from several character vectors. A
short name is assigned to each token in the expression:


chr1 = '134 Main Street, Boulder, CO, 14923';
chr2 = '26 Walnut Road, Topeka, KA, 25384';
chr3 = '847 Industrial Drive, Elizabeth, NJ, 73548';


p1 = '(?\d+\s\S+\s(Road|Street|Avenue|Drive))';
p2 = '(?[A-Z][a-z]+)';
p3 = '(?[A-Z]{2})';
p4 = '(?\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(chr1, expr, 'names')


loc1 =


struct with fields:


adrs: '134 Main Street'
city: 'Boulder'
state: 'CO'
zip: '14923'


loc2 = regexp(chr2, expr, 'names')


loc2 =


struct with fields:


adrs: '26 Walnut Road'
city: 'Topeka'
state: 'KA'
zip: '25384'


loc3 = regexp(chr3, expr, 'names')


loc3 =


struct with fields:


Tokens in Regular Expressions
Free download pdf