Programming Previous page   Next Page

Logical Operators

Logical operators do not match any specific characters. They are used to specify the context for matching an accompanying regular expression.

Group regular expressions and capture tokens.
Group regular expressions, but do not capture tokens.
Group atomically.
Insert a comment into the expression. Comments are ignored in matching.
Match expression expr1 or expression expr2.
Match the expression only at the beginning of the string.
Match the expression only at the end of the string.
Match the characters when they start a word.
Match the characters when they end a word.
Match an exact word.

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:

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:

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 p1|p2 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:

Previous page  Character Representation Lookaround Operators Next page

© 1994-2005 The MathWorks, Inc.