Lookahead Assertions in Regular Expressions
In this section...
“Lookahead Assertions” on page 2-57
“Overlapping Matches” on page 2-58
“Logical AND Conditions” on page 2-58
Lookahead Assertions
There are two types of lookaround assertions for regular expressions: lookahead and
lookbehind. In both cases, the assertion is a condition that must be satisfied to return a
match to the expression.
A lookahead assertion has the form (?=test) and can appear anywhere in a regular
expression. MATLAB looks ahead of the current location in the text for the test condition.
If MATLAB matches the test condition, it continues processing the rest of the expression
to find a match.
For example, look ahead in a character vector specifying a path to find the name of the
folder that contains a program file (in this case, fileread.m).
chr = which('fileread')
chr =
'matlabroot\toolbox\matlab\iofun\fileread.m'
regexp(chr,'\w+(?=\\\w+\.[mp])','match')
ans =
1×1 cell array
{'iofun'}
The match expression, \w+, searches for one or more alphanumeric or underscore
characters. Each time regexp finds a term that matches this condition, it looks ahead for
a backslash (specified with two backslashes, \\), followed by a file name (\w+) with an .m
or .p extension (\.[mp]). The regexp function returns the match that satisfies the
lookahead condition, which is the folder name iofun.
Lookahead Assertions in Regular Expressions