Grouping
Operator
Description Example
(expr) Group elements of the expression and
capture tokens.
'Joh?n\s(\w*)' captures a token that
contains the last name of any person
with the first name John or Jon.
(?: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
start 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 Beginning of a word. '\<n\w*' matches any words starting
with n.
2 Program Components