MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

26 26


ans =


35 35


ans =


57 57


For another example, capture pairs of matching HTML tags (e.g., and ) and the
text between them. The expression used for this example is


expr = '<(\w+).?>.?</\1>';


The first part of the expression, '<(\w+)', matches an opening angle bracket (<)
followed by one or more alphabetic, numeric, or underscore characters. The enclosing
parentheses capture token characters following the opening angle bracket.


The second part of the expression, '.?>.?', matches the remainder of this HTML tag
(characters up to the >), and any characters that may precede the next opening angle
bracket.


The last part, '</\1>', matches all characters in the ending HTML tag. This tag is
composed of the sequence , where tag is whatever characters were captured as a
token.


hstr = '<!comment>Default
';
expr = '<(\w+).?>.?</\1>';


[mat,tok] = regexp(hstr, expr, 'match', 'tokens');
mat{:}


ans =


''


ans =


'Default'


Tokens in Regular Expressions
Free download pdf