The starting indices correspond to these phrases:
Locate
severa
everal
6-char
-char.
phrase
hrases
Without the lookahead operator, MATLAB parses a character vector from left to right, consuming the
vector as it goes. If matching characters are found, regexp records the location and resumes parsing
the character vector from the location of the most recent match. There is no overlapping of
characters in this process.
chr = 'Locate several 6-char. phrases';
startIndex = regexpi(chr,'\S{6}')
startIndex =
1
8
16
24
The starting indices correspond to these phrases:
Locate
severa
6-char
phrase
Logical AND Conditions
Another way to use a lookahead operation is to perform a logical AND between two conditions. This
example initially attempts to locate all lowercase consonants in a character array consisting of the
first 50 characters of the help for the normest function:
helptext = help('normest');
chr = helptext(1:50)
chr =
' NORMEST Estimate the matrix 2-norm.
NORMEST(S'
Merely searching for non-vowels ([^aeiou]) does not return the expected answer, as the output
includes capital letters, space characters, and punctuation:
c = regexp(chr,'[^aeiou]','match')
c =
1×43 cell array
Columns 1 through 14
{' '}
{'N'}
{'O'}
{'R'}
{'M'}
{'E'}
{'S'}
{'T'}
{' '}
{'E'}
{'s
Columns 15 through 28
{' '}
{'t'}
{'h'}
{' '}
{'m'}
{'t'}
{'r'}
{'x'}
{' '}
{'2'}
{'Columns 29 through 42
{'.'}
{'↵'}
{' '}
{' '}
{' '}
{' '}
{'N'}
{'O'}
{'R'}
{'M'}
{'E
Column 43
{'S'}
2 Program Components
2-64
Précédent

- 114/1450

Suivant