|
A conditional expression is a form of if-then-else statement that allows
one to choose which patterns are to be matched, based on some condition. There are two types of
conditional expression: (?(condition)yes-regexp) and (?(condition)yes-regexp|no-regexp).
(?(condition)yes-regexp) is like an 'if () {}'
statement in Perl. If the condition is true, the yes-regexp will be
matched. If the condition is false, the yes-regexp will be skipped and
perl will move onto the next regexp element. The second form is like an 'if () {} else {}'
statement in Perl. If the condition is true, the yes-regexp will be
matched, otherwise the no-regexp will be matched.
The condition can have two forms. The first form is simply an integer in
parentheses (integer). It is true if the corresponding backreference \integer
matched earlier in the regexp. The second form is a bare zero width assertion (?...),
either a lookahead, a lookbehind, or a code assertion (discussed in the next section).
The integer form of the condition allows us to choose, with more flexibility,
what to match based on what matched earlier in the regexp. This searches for words of the form "$x$x"
or "$x$y$y$x":
% simple_grep '^(\w+)(\w+)?(?(2)\2\1|\1)$' /usr/dict/words
beriberi
coco
couscous
deed
...
toot
toto
tutu
|
|
The lookbehind condition allows, along with backreferences, an earlier part of
the match to influence a later part of the match. For instance,
matches a DNA sequence such that it either ends in AAG, or some other base pair
combination and C. Note that the form is (?(?<=AA)G|C) and not (?((?<=AA))G|C);
for the lookahead, lookbehind or code assertions, the parentheses around the conditional are not
needed.
|
|