Programming |
Logical operators do not match any specific characters. They are used to specify the context for matching an accompanying regular expression.
Grouping and Capture -- (expr)
You can group elements together using either (expr)
to group and capture or (?:expr)
for grouping alone. For an example of the former, see Using Tokens -- Example 1. For the latter, see the "Grouping-Only" example below.
Grouping Only -- (?:expr)
Use (?:expr)
to group a consonant followed by a vowel in the palindrome pstr
. Specify at least two consecutive occurrences ({2,}
) of this group. Return the starting and ending indices of the matched substrings:
pstr = 'Marge lets Norah see Sharon''s telegram'; expr = '(?:[^aeiou][aeiou]){2,}'; [mat ix1 ix2] = regexp(pstr, expr, 'match', 'start', 'end') mat = 'Nora' 'haro' 'tele' ix1 = 12 23 31 ix2 = 15 26 34
Remove the grouping, and the {2,}
now applies only to [aeiou]
. The command is entirely different now as it looks for a consonant followed by at least two consecutive vowels:
expr = '[^aeiou][aeiou]{2,}'; [mat ix1 ix2] = regexp(pstr, expr, 'match', 'start', 'end') mat = 'see' ix1 = 18 ix2 = 20
Including Comments -- (?#expr)
Use (?#expr)
to add a comment to this expression that matches capitalized words in pstr
. Comments are ignored in the process of finding a match:
Alternative Match -- expr1|expr2
Use p
1|p
2 to pick out words in the string that start with let
or tel
:
Start and End of String Match -- ^expr, expr$
Use ^expr
to match words starting with the letter m
or M
only when it begins the string, and expr$
to match words ending with m
or M
only when it ends the string:
Start and End of Word Match -- \<expr, expr\>
Use \<expr
to match any words starting with n
or N
, or ending with e
or E
:
Exact Word Match -- \<expr\>
Use \<expr\>
to match a word starting with an n or N and ending with an h
or H
:
Character Representation | Lookaround Operators |
© 1994-2005 The MathWorks, Inc.