Grouping
Operator
Description
Example
(?:expr)
Group, but do not capture tokens.
'(?:[aeiou][^aeiou]){2}' matches two
consecutive patterns of a vowel followed by a
nonvowel, such as 'anon'.
Without grouping, '[aeiou][^aeiou]
{2}'matches a vowel followed by two
nonvowels.
(?>expr)
Group atomically. Do not backtrack within the
group to complete the match, and do not
capture tokens.
'A(?>.*)Z' does not match 'AtoZ',
although 'A(?:.*)Z' does. Using the atomic
group, Z is captured using .* and is not
rescanned.
(expr1|expr2) Match expression expr1 or expression
expr2.
If there is a match with expr1, then expr2 is
ignored.
You can include ?: or ?> after the opening
parenthesis to suppress tokens or group
atomically.
'(let|tel)\w+' matches words that
contain, but do not end, with let or tel.
Anchors
Anchors in the expression match the beginning or end of a character vector or word.
Anchor
Matches the...
Example
^expr
Beginning of the input text.
'^M\w*' matches a word starting with M at
the beginning of the text.
expr$
End of the input text.
'\w*m$' matches words ending with m at the
end of the text.
\
Beginning of a word.
'\
n.
expr\>
End of a word.
'\w*e\>' matches any words ending with e.
Lookaround Assertions
Lookaround assertions look for patterns that immediately precede or follow the intended match, but
are not part of the match.
The pointer remains at the current location, and characters that correspond to the test expression
are not captured or discarded. Therefore, lookahead assertions can match overlapping character
groups.
2 Program Components
2-58
Operator
Description
Example
(?:expr)
Group, but do not capture tokens.
'(?:[aeiou][^aeiou]){2}' matches two
consecutive patterns of a vowel followed by a
nonvowel, such as 'anon'.
Without grouping, '[aeiou][^aeiou]
{2}'matches a vowel followed by two
nonvowels.
(?>expr)
Group atomically. Do not backtrack within the
group to complete the match, and do not
capture tokens.
'A(?>.*)Z' does not match 'AtoZ',
although 'A(?:.*)Z' does. Using the atomic
group, Z is captured using .* and is not
rescanned.
(expr1|expr2) Match expression expr1 or expression
expr2.
If there is a match with expr1, then expr2 is
ignored.
You can include ?: or ?> after the opening
parenthesis to suppress tokens or group
atomically.
'(let|tel)\w+' matches words that
contain, but do not end, with let or tel.
Anchors
Anchors in the expression match the beginning or end of a character vector or word.
Anchor
Matches the...
Example
^expr
Beginning of the input text.
'^M\w*' matches a word starting with M at
the beginning of the text.
expr$
End of the input text.
'\w*m$' matches words ending with m at the
end of the text.
\
'\
expr\>
End of a word.
'\w*e\>' matches any words ending with e.
Lookaround Assertions
Lookaround assertions look for patterns that immediately precede or follow the intended match, but
are not part of the match.
The pointer remains at the current location, and characters that correspond to the test expression
are not captured or discarded. Therefore, lookahead assertions can match overlapping character
groups.
2 Program Components
2-58
