MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Anchor Matches the... Example


expr> End of a word. '\w*e>' matches any words ending
with e.


Lookaround Assertions

Lookaround assertions look for patterns that immediately precede or follow the intended
match, but are not part of the match.

The pointer remains at the current location, and characters that correspond to the test
expression are not captured or discarded. Therefore, lookahead assertions can match
overlapping character groups.

Lookaround
Assertion


Description Example

expr(?=test) Look ahead for characters that match
test.


'\w*(?=ing)' matches terms that are
followed by ing, such as 'Fly' and
'fall' in the input text 'Flying,
not falling.'

expr(?!test) Look ahead for characters that do not
match test.


'i(?!ng)' matches instances of the
letter i that are not followed by ng.

(?<=test)expr Look behind for characters that match
test.


'(?<=re)\w*' matches terms that
follow 're', such as 'new', 'use', and
'cycle' in the input text 'renew,
reuse, recycle'

(?<!test)expr Look behind for characters that do not
match test.


'(?<!\d)(\d)(?!\d)' matches
single-digit numbers (digits that do not
precede or follow other digits).

If you specify a lookahead assertion before an expression, the operation is equivalent to a
logical AND.

Operation Description Example


(?=test)expr Match both test and expr. '(?=[a-z])[^aeiou]' matches
consonants.


(?!test)expr Match expr and do not match test. '(?![aeiou])[a-z]' matches
consonants.


Regular Expressions
Free download pdf