MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

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'} {'t'} {'m'} {'t'}


Columns 15 through 28


{' '} {'t'} {'h'} {' '} {'m'} {'t'} {'r'} {'x'} {' '} {'2'} {'-'} {'n'} {'r'} {'m'}


Columns 29 through 42


{'.'} {'↵'} {' '} {' '} {' '} {' '} {'N'} {'O'} {'R'} {'M'} {'E'} {'S'} {'T'} {'('}


Column 43


{'S'}


Try this again, using a lookahead operator to create the following AND condition:


(lowercase letter) AND (not a vowel)


This time, the result is correct:


c = regexp(chr,'(?=[a-z])[^aeiou]','match')


c =


1×13 cell array


{'s'} {'t'} {'m'} {'t'} {'t'} {'h'} {'m'} {'t'} {'r'} {'x'} {'n'} {'r'} {'m'}


Note that when using a lookahead operator to perform an AND, you need to place the
match expression expr after the test expression test:


Lookahead Assertions in Regular Expressions
Free download pdf