)\s+\1
matches "rah rah" and "RAH RAH", but not "RAH rah", even
though the original capturing subpattern is matched case-
lessly.
Back references to named subpatterns use the Python syntax
(?P=name). We could rewrite the above example as follows:
&nbs
p; (?<p1>(?i)rah)\s+(?P=p1)p; (?<p1>(?i)rah)\s+(?P=p1)
There may be more than one back reference to the same sub-
pattern. If a subpattern has not actually been used in a
particular match, any back references to it always fail. For
example, the pattern
&nbs
p; (a|(bc))\2p; (a|(bc))\2
always fails if it starts to match "a" rather than "bc".
Because there may be many capturing parentheses in a pat-
tern, all digits following the backslash are taken as part
of a potential back reference number. If the pattern contin-
ues pat-
tern
&nbs
p; (a|b\1)+p; (a|b\1)+
matches any number of "a"s and also "aba", "ababbaa" etc. At
each iteration of the subpattern, the back reference matches
the character string corresponding to the previous itera-
tion. com-
plicated,
&nbs
p; \w+(?=;)p; \w+(?=;)
matches a word followed by a semicolon, but does not include
the semicolon in the match, and
&nbs
p; foo(?!bar)p; foo(?!bar)
matches any occurrence of "foo" that is not followed by
"bar". Note that the apparently similar pattern
&nbs
p; (?!foo)barp; (?!foo)bar
does not find an occurrence of "bar" that is preceded by
something other than "foo"; it finds any occurrence of "bar"
whatsoever, because the assertion (?!foo) is always true
when the next three characters are "bar". A lookbehind
assertion is needed to achieve this asser-
tions and (?<! for negative assertions. For example,
&nbs
p; (?<!foo)barp; (?<
&nbs
p; (?<=bullock|donkey)p; (?<=bullock|donkey)
is permitted, but
&nbs
p; (?<!dogs?|cats?)p; (?<!dogs?|cats?)
causes an error at compile time. Branches that match dif-
ferent length strings are permitted only at the top level of
a lookbehind assertion. This is an extension compared with
Perl (at least for 5.8), which requires all branches to
match the same length of string. An assertion such as
&nbs
p; (?<=ab(c|de))p; (?<=ab(c|de))
is not permitted, because its single top-level branch can
match two different lengths, but it is acceptable if rewrit-
ten to use two top-level branches:
&nbs
p; (?<=abc|abde)p; (?<=abc|abde)
The implementation of lookbehind assertions is, for each
alternative, to temporarily move the current position back
by the fixed width and then try to match. If there are
insufficient characters before the current position, the
match is deemed to fail.
PCRE does not allow the \C escape (which matches a single
byte in UTF-8 mode) to appear in lookbehind assertions,
because it makes it impossible to calculate the length of
the lookbehind.
Atomic groups can be used in conjunction with lookbehind
assertions to specify efficient matching at the end of the
subject string. Consider a simple pattern such as
&nbs
p; abcd$p; abcd$
when applied to a long string that does not match. Because
matching proceeds from left to right, PCRE will look for
each "a" in the subject and then see if what follows matches
the rest of the pattern. If the pattern is specified as
&nbs
p; ^.*abcd$p; ^.*abcd$
the initial.* matches the entire string at first, but when
this fails (because there is no following "a"), it back-
tracks to match all but the last character, then all but the
last two characters, and so on. Once again the search for
"a" covers the entire string, from right to left, so we are
no better off. However, if the pattern is written as
&nbs
p; ^(?>.*)(?<=abcd)p; ^(?>.*)(?<=abcd)
or, equivalently,
&nbs
p; ^.*+(?<=abcd,
&nbs
p; (?<=\d{3})(?<!999)foop; (?<=
&nbs
p; (?<=\d{3}...)(?<!999)foop; (?<=\d{3}...)(?<!999)foo
This time the first assertion looks at the preceding six
characters, checking that the first three are digits, and
then the second assertion checks that the preceding three
characters are not "999".
Assertions can be nested in any combination. For example,
&nbs
p; (?<=(?<!foo)bar)bazp; (?<=(?<!foo)bar)baz
matches an occurrence of "baz" that is preceded by "bar"
which in turn is not preceded by "foo", while
&nbs
p; (?<=\d{3}(?!999)...)foop; (?<=.
CONDITIONAL SUBPATTERNS
It is possible to cause the matching process to obey a sub-
pattern conditionally or to choose between two alternative
subpatterns, depending on the result of an assertion, or
whether a previous capturing subpattern matched or not. The
two possible forms of conditional subpattern are
&nbs
p; (?(condition)yes-pattern)p; (?(condition)yes-pattern)
&nbs
p; (?(condition)yes-pattern|no-pap; (?(condition)yes-pattern|no-pa ttern)ttern)
If the condition is satisfied, the yes-pattern is used; oth-
erwise the no-pattern (if present) is used. If there are
more than two alternatives in the subpattern, a compile-time
error occurs.
There are three:
&nbs
p; ( \( )? [^()]+ (?(1) \) )p; ( \( )? [^()]+ (?. This is a PCRE
extension. Recursive patterns are described in the next
section.
If the condition is not a sequence of digits or (R), it must
be an assertion. This may be a positive or negative looka-
head or lookbehind assertion. Consider this pattern, again
containing non-significant white space, and with the two
alternatives on the second line:
&nbs
p; (?(?=[^a-z]*[a-z])p; (?(?=[^a-z]*[a-z])
&nbs
p; \d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} )p;  contin-
ues up to the next closing parenthesis. Nested parentheses
are not permitted. The characters that make up a comment
play no part in the pattern matching at all.
If the PCRE_EXTENDED option is set, an unescaped # character
outside a character class introduces a comment that contin-
ues has pro-
vided an experimental facility that allows regular expres-
sions to recurse (amongst other things). It does this by
interpolating Perl code in the expression at run time, and
the code can refer to the expression itself. A Perl pattern
to solve the parentheses problem can be created like this:
&nbs
p; $re = qr{\( (?: (?>[^()]+) | (?p{$re}) )* \)}x;p; $re = qr{\( (?: (?>[^()]+) | (?p{$re}) )* \)}x;
The (?p{...}) item interpolates Perl code at run time, and
in this case refers recursively to the pattern in which it
appears. Obviously, PCRE cannot support the interpolation of
Perl code. Instead, it supports some special syntax for
recursion of the entire pattern, and also for individual
subpattern recursion.) is a recursive call of the entire regular expres-
sion.
For example, this PCRE pattern solves the nested parentheses
problem (assume the PCRE_EXTENDED option is set so that
white space is ignored):
&nbs
p; \( ( (?>[^()]+) | (?R) )* \)p; \( ( (?>[^