Lookaround
Assertion
Description
Example
expr(?=test)
Look ahead for characters that match test. '\w*(?=ing)' matches terms that are
followed by ing, such as 'Fly' and 'fall'
in the input text 'Flying, not falling.'
expr(?!test)
Look ahead for characters that do not
match test.
'i(?!ng)' matches instances of the letter i
that are not followed by ng.
(?<=test)expr
Look behind for characters that match
test.
'(?<=re)\w*' matches terms that follow
're', such as 'new', 'use', and 'cycle'
in the input text 'renew, reuse,
recycle'
(? Look behind for characters that do not
match test.
'(? follow other digits).
If you specify a lookahead assertion before an expression, the operation is equivalent to a logical AND.
Operation
Description
Example
(?=test)expr
Match both test and expr.
'(?=[a-z])[^aeiou]' matches
consonants.
(?!test)expr
Match expr and do not match test.
'(?![aeiou])[a-z]' matches consonants.
For more information, see “Lookahead Assertions in Regular Expressions” on page 2-63.
Logical and Conditional Operators
Logical and conditional operators allow you to test the state of a given condition, and then use the
outcome to determine which pattern, if any, to match next. These operators support logical OR and if
or if/else conditions. (For AND conditions, see “Lookaround Assertions” on page 2-58.)
Conditions can be tokens on page 2-60, lookaround assertions on page 2-58, or dynamic expressions
on page 2-60 of the form (?@cmd). Dynamic expressions must return a logical or numeric value.
Conditional Operator
Description
Example
expr1|expr2
Match expression expr1 or expression
expr2.
If there is a match with expr1, then
expr2 is ignored.
'(let|tel)\w+' matches words that
start with let or tel.
(?(cond)expr)
If condition cond is true, then match
expr.
'(?(?@ispc)[A-Z]:\\)' matches a
drive name, such as C:\, when run on a
Windows system.
(?(cond)expr1|expr2)
If condition cond is true, then match
expr1. Otherwise, match expr2.
'Mr(s?)\..*?(?(1)her|his) \w*'
matches text that includes her when
the text begins with Mrs, or that
includes his when the text begins with
Mr.
Regular Expressions
2-59
Assertion
Description
Example
expr(?=test)
Look ahead for characters that match test. '\w*(?=ing)' matches terms that are
followed by ing, such as 'Fly' and 'fall'
in the input text 'Flying, not falling.'
expr(?!test)
Look ahead for characters that do not
match test.
'i(?!ng)' matches instances of the letter i
that are not followed by ng.
(?<=test)expr
Look behind for characters that match
test.
'(?<=re)\w*' matches terms that follow
're', such as 'new', 'use', and 'cycle'
in the input text 'renew, reuse,
recycle'
(? Look behind for characters that do not
match test.
'(? follow other digits).
If you specify a lookahead assertion before an expression, the operation is equivalent to a logical AND.
Operation
Description
Example
(?=test)expr
Match both test and expr.
'(?=[a-z])[^aeiou]' matches
consonants.
(?!test)expr
Match expr and do not match test.
'(?![aeiou])[a-z]' matches consonants.
For more information, see “Lookahead Assertions in Regular Expressions” on page 2-63.
Logical and Conditional Operators
Logical and conditional operators allow you to test the state of a given condition, and then use the
outcome to determine which pattern, if any, to match next. These operators support logical OR and if
or if/else conditions. (For AND conditions, see “Lookaround Assertions” on page 2-58.)
Conditions can be tokens on page 2-60, lookaround assertions on page 2-58, or dynamic expressions
on page 2-60 of the form (?@cmd). Dynamic expressions must return a logical or numeric value.
Conditional Operator
Description
Example
expr1|expr2
Match expression expr1 or expression
expr2.
If there is a match with expr1, then
expr2 is ignored.
'(let|tel)\w+' matches words that
start with let or tel.
(?(cond)expr)
If condition cond is true, then match
expr.
'(?(?@ispc)[A-Z]:\\)' matches a
drive name, such as C:\, when run on a
Windows system.
(?(cond)expr1|expr2)
If condition cond is true, then match
expr1. Otherwise, match expr2.
'Mr(s?)\..*?(?(1)her|his) \w*'
matches text that includes her when
the text begins with Mrs, or that
includes his when the text begins with
Mr.
Regular Expressions
2-59
