| 4 |
.SH "PCRE REGULAR EXPRESSION DETAILS" |
.SH "PCRE REGULAR EXPRESSION DETAILS" |
| 5 |
.rs |
.rs |
| 6 |
.sp |
.sp |
| 7 |
The syntax and semantics of the regular expressions supported by PCRE are |
The syntax and semantics of the regular expressions that are supported by PCRE |
| 8 |
described below. Regular expressions are also described in the Perl |
are described in detail below. There is a quick-reference syntax summary in the |
| 9 |
documentation and in a number of books, some of which have copious examples. |
.\" HREF |
| 10 |
Jeffrey Friedl's "Mastering Regular Expressions", published by O'Reilly, covers |
\fBpcresyntax\fP |
| 11 |
regular expressions in great detail. This description of PCRE's regular |
.\" |
| 12 |
expressions is intended as reference material. |
page. Perl's regular expressions are described in its own documentation, and |
| 13 |
|
regular expressions in general are covered in a number of books, some of which |
| 14 |
|
have copious examples. Jeffrey Friedl's "Mastering Regular Expressions", |
| 15 |
|
published by O'Reilly, covers regular expressions in great detail. This |
| 16 |
|
description of PCRE's regular expressions is intended as reference material. |
| 17 |
.P |
.P |
| 18 |
The original operation of PCRE was on strings of one-byte characters. However, |
The original operation of PCRE was on strings of one-byte characters. However, |
| 19 |
there is now also support for UTF-8 character strings. To use this, you must |
there is now also support for UTF-8 character strings. To use this, you must |
| 34 |
PCRE when its main matching function, \fBpcre_exec()\fP, is used. |
PCRE when its main matching function, \fBpcre_exec()\fP, is used. |
| 35 |
From release 6.0, PCRE offers a second matching function, |
From release 6.0, PCRE offers a second matching function, |
| 36 |
\fBpcre_dfa_exec()\fP, which matches using a different algorithm that is not |
\fBpcre_dfa_exec()\fP, which matches using a different algorithm that is not |
| 37 |
Perl-compatible. The advantages and disadvantages of the alternative function, |
Perl-compatible. Some of the features discussed below are not available when |
| 38 |
and how it differs from the normal function, are discussed in the |
\fBpcre_dfa_exec()\fP is used. The advantages and disadvantages of the |
| 39 |
|
alternative function, and how it differs from the normal function, are |
| 40 |
|
discussed in the |
| 41 |
.\" HREF |
.\" HREF |
| 42 |
\fBpcrematching\fP |
\fBpcrematching\fP |
| 43 |
.\" |
.\" |
| 168 |
After \ex, from zero to two hexadecimal digits are read (letters can be in |
After \ex, from zero to two hexadecimal digits are read (letters can be in |
| 169 |
upper or lower case). Any number of hexadecimal digits may appear between \ex{ |
upper or lower case). Any number of hexadecimal digits may appear between \ex{ |
| 170 |
and }, but the value of the character code must be less than 256 in non-UTF-8 |
and }, but the value of the character code must be less than 256 in non-UTF-8 |
| 171 |
mode, and less than 2**31 in UTF-8 mode (that is, the maximum hexadecimal value |
mode, and less than 2**31 in UTF-8 mode. That is, the maximum value in |
| 172 |
is 7FFFFFFF). If characters other than hexadecimal digits appear between \ex{ |
hexadecimal is 7FFFFFFF. Note that this is bigger than the largest Unicode code |
| 173 |
and }, or if there is no terminating }, this form of escape is not recognized. |
point, which is 10FFFF. |
| 174 |
Instead, the initial \ex will be interpreted as a basic hexadecimal escape, |
.P |
| 175 |
with no following digits, giving a character whose value is zero. |
If characters other than hexadecimal digits appear between \ex{ and }, or if |
| 176 |
|
there is no terminating }, this form of escape is not recognized. Instead, the |
| 177 |
|
initial \ex will be interpreted as a basic hexadecimal escape, with no |
| 178 |
|
following digits, giving a character whose value is zero. |
| 179 |
.P |
.P |
| 180 |
Characters whose value is less than 256 can be defined by either of the two |
Characters whose value is less than 256 can be defined by either of the two |
| 181 |
syntaxes for \ex. There is no difference in the way they are handled. For |
syntaxes for \ex. There is no difference in the way they are handled. For |
| 247 |
.SS "Absolute and relative back references" |
.SS "Absolute and relative back references" |
| 248 |
.rs |
.rs |
| 249 |
.sp |
.sp |
| 250 |
The sequence \eg followed by a positive or negative number, optionally enclosed |
The sequence \eg followed by an unsigned or a negative number, optionally |
| 251 |
in braces, is an absolute or relative back reference. Back references are |
enclosed in braces, is an absolute or relative back reference. A named back |
| 252 |
discussed |
reference can be coded as \eg{name}. Back references are discussed |
| 253 |
.\" HTML <a href="#backreferences"> |
.\" HTML <a href="#backreferences"> |
| 254 |
.\" </a> |
.\" </a> |
| 255 |
later, |
later, |
| 269 |
.sp |
.sp |
| 270 |
\ed any decimal digit |
\ed any decimal digit |
| 271 |
\eD any character that is not a decimal digit |
\eD any character that is not a decimal digit |
| 272 |
|
\eh any horizontal whitespace character |
| 273 |
|
\eH any character that is not a horizontal whitespace character |
| 274 |
\es any whitespace character |
\es any whitespace character |
| 275 |
\eS any character that is not a whitespace character |
\eS any character that is not a whitespace character |
| 276 |
|
\ev any vertical whitespace character |
| 277 |
|
\eV any character that is not a vertical whitespace character |
| 278 |
\ew any "word" character |
\ew any "word" character |
| 279 |
\eW any "non-word" character |
\eW any "non-word" character |
| 280 |
.sp |
.sp |
| 288 |
.P |
.P |
| 289 |
For compatibility with Perl, \es does not match the VT character (code 11). |
For compatibility with Perl, \es does not match the VT character (code 11). |
| 290 |
This makes it different from the the POSIX "space" class. The \es characters |
This makes it different from the the POSIX "space" class. The \es characters |
| 291 |
are HT (9), LF (10), FF (12), CR (13), and space (32). (If "use locale;" is |
are HT (9), LF (10), FF (12), CR (13), and space (32). If "use locale;" is |
| 292 |
included in a Perl script, \es may match the VT character. In PCRE, it never |
included in a Perl script, \es may match the VT character. In PCRE, it never |
| 293 |
does.) |
does. |
| 294 |
|
.P |
| 295 |
|
In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or |
| 296 |
|
\ew, and always match \eD, \eS, and \eW. This is true even when Unicode |
| 297 |
|
character property support is available. These sequences retain their original |
| 298 |
|
meanings from before UTF-8 support was available, mainly for efficiency |
| 299 |
|
reasons. |
| 300 |
|
.P |
| 301 |
|
The sequences \eh, \eH, \ev, and \eV are Perl 5.10 features. In contrast to the |
| 302 |
|
other sequences, these do match certain high-valued codepoints in UTF-8 mode. |
| 303 |
|
The horizontal space characters are: |
| 304 |
|
.sp |
| 305 |
|
U+0009 Horizontal tab |
| 306 |
|
U+0020 Space |
| 307 |
|
U+00A0 Non-break space |
| 308 |
|
U+1680 Ogham space mark |
| 309 |
|
U+180E Mongolian vowel separator |
| 310 |
|
U+2000 En quad |
| 311 |
|
U+2001 Em quad |
| 312 |
|
U+2002 En space |
| 313 |
|
U+2003 Em space |
| 314 |
|
U+2004 Three-per-em space |
| 315 |
|
U+2005 Four-per-em space |
| 316 |
|
U+2006 Six-per-em space |
| 317 |
|
U+2007 Figure space |
| 318 |
|
U+2008 Punctuation space |
| 319 |
|
U+2009 Thin space |
| 320 |
|
U+200A Hair space |
| 321 |
|
U+202F Narrow no-break space |
| 322 |
|
U+205F Medium mathematical space |
| 323 |
|
U+3000 Ideographic space |
| 324 |
|
.sp |
| 325 |
|
The vertical space characters are: |
| 326 |
|
.sp |
| 327 |
|
U+000A Linefeed |
| 328 |
|
U+000B Vertical tab |
| 329 |
|
U+000C Formfeed |
| 330 |
|
U+000D Carriage return |
| 331 |
|
U+0085 Next line |
| 332 |
|
U+2028 Line separator |
| 333 |
|
U+2029 Paragraph separator |
| 334 |
.P |
.P |
| 335 |
A "word" character is an underscore or any character less than 256 that is a |
A "word" character is an underscore or any character less than 256 that is a |
| 336 |
letter or digit. The definition of letters and digits is controlled by PCRE's |
letter or digit. The definition of letters and digits is controlled by PCRE's |
| 346 |
.\" |
.\" |
| 347 |
page). For example, in a French locale such as "fr_FR" in Unix-like systems, |
page). For example, in a French locale such as "fr_FR" in Unix-like systems, |
| 348 |
or "french" in Windows, some character codes greater than 128 are used for |
or "french" in Windows, some character codes greater than 128 are used for |
| 349 |
accented letters, and these are matched by \ew. |
accented letters, and these are matched by \ew. The use of locales with Unicode |
| 350 |
.P |
is discouraged. |
|
In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or |
|
|
\ew, and always match \eD, \eS, and \eW. This is true even when Unicode |
|
|
character property support is available. The use of locales with Unicode is |
|
|
discouraged. |
|
| 351 |
. |
. |
| 352 |
. |
. |
| 353 |
.SS "Newline sequences" |
.SS "Newline sequences" |
| 354 |
.rs |
.rs |
| 355 |
.sp |
.sp |
| 356 |
Outside a character class, the escape sequence \eR matches any Unicode newline |
Outside a character class, the escape sequence \eR matches any Unicode newline |
| 357 |
sequence. This is an extension to Perl. In non-UTF-8 mode \eR is equivalent to |
sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \eR is equivalent to |
| 358 |
the following: |
the following: |
| 359 |
.sp |
.sp |
| 360 |
(?>\er\en|\en|\ex0b|\ef|\er|\ex85) |
(?>\er\en|\en|\ex0b|\ef|\er|\ex85) |
| 383 |
.rs |
.rs |
| 384 |
.sp |
.sp |
| 385 |
When PCRE is built with Unicode character property support, three additional |
When PCRE is built with Unicode character property support, three additional |
| 386 |
escape sequences to match character properties are available when UTF-8 mode |
escape sequences that match characters with specific properties are available. |
| 387 |
is selected. They are: |
When not in UTF-8 mode, these sequences are of course limited to testing |
| 388 |
|
characters whose codepoints are less than 256, but they do work in this mode. |
| 389 |
|
The extra escape sequences are: |
| 390 |
.sp |
.sp |
| 391 |
\ep{\fIxx\fP} a character with the \fIxx\fP property |
\ep{\fIxx\fP} a character with the \fIxx\fP property |
| 392 |
\eP{\fIxx\fP} a character without the \fIxx\fP property |
\eP{\fIxx\fP} a character without the \fIxx\fP property |
| 538 |
the Lu, Ll, or Lt property, in other words, a letter that is not classified as |
the Lu, Ll, or Lt property, in other words, a letter that is not classified as |
| 539 |
a modifier or "other". |
a modifier or "other". |
| 540 |
.P |
.P |
| 541 |
|
The Cs (Surrogate) property applies only to characters in the range U+D800 to |
| 542 |
|
U+DFFF. Such characters are not valid in UTF-8 strings (see RFC 3629) and so |
| 543 |
|
cannot be tested by PCRE, unless UTF-8 validity checking has been turned off |
| 544 |
|
(see the discussion of PCRE_NO_UTF8_CHECK in the |
| 545 |
|
.\" HREF |
| 546 |
|
\fBpcreapi\fP |
| 547 |
|
.\" |
| 548 |
|
page). |
| 549 |
|
.P |
| 550 |
The long synonyms for these properties that Perl supports (such as \ep{Letter}) |
The long synonyms for these properties that Perl supports (such as \ep{Letter}) |
| 551 |
are not supported by PCRE, nor is it permitted to prefix any of these |
are not supported by PCRE, nor is it permitted to prefix any of these |
| 552 |
properties with "Is". |
properties with "Is". |
| 571 |
(see below). |
(see below). |
| 572 |
.\" |
.\" |
| 573 |
Characters with the "mark" property are typically accents that affect the |
Characters with the "mark" property are typically accents that affect the |
| 574 |
preceding character. |
preceding character. None of them have codepoints less than 256, so in |
| 575 |
|
non-UTF-8 mode \eX matches any one character. |
| 576 |
.P |
.P |
| 577 |
Matching characters by Unicode property is not fast, because PCRE has to search |
Matching characters by Unicode property is not fast, because PCRE has to search |
| 578 |
a structure that contains data for over fifteen thousand characters. That is |
a structure that contains data for over fifteen thousand characters. That is |
| 580 |
properties in PCRE. |
properties in PCRE. |
| 581 |
. |
. |
| 582 |
. |
. |
| 583 |
|
.\" HTML <a name="resetmatchstart"></a> |
| 584 |
|
.SS "Resetting the match start" |
| 585 |
|
.rs |
| 586 |
|
.sp |
| 587 |
|
The escape sequence \eK, which is a Perl 5.10 feature, causes any previously |
| 588 |
|
matched characters not to be included in the final matched sequence. For |
| 589 |
|
example, the pattern: |
| 590 |
|
.sp |
| 591 |
|
foo\eKbar |
| 592 |
|
.sp |
| 593 |
|
matches "foobar", but reports that it has matched "bar". This feature is |
| 594 |
|
similar to a lookbehind assertion |
| 595 |
|
.\" HTML <a href="#lookbehind"> |
| 596 |
|
.\" </a> |
| 597 |
|
(described below). |
| 598 |
|
.\" |
| 599 |
|
However, in this case, the part of the subject before the real match does not |
| 600 |
|
have to be of fixed length, as lookbehind assertions do. The use of \eK does |
| 601 |
|
not interfere with the setting of |
| 602 |
|
.\" HTML <a href="#subpattern"> |
| 603 |
|
.\" </a> |
| 604 |
|
captured substrings. |
| 605 |
|
.\" |
| 606 |
|
For example, when the pattern |
| 607 |
|
.sp |
| 608 |
|
(foo)\eKbar |
| 609 |
|
.sp |
| 610 |
|
matches "foobar", the first substring is still set to "foo". |
| 611 |
|
. |
| 612 |
|
. |
| 613 |
.\" HTML <a name="smallassertions"></a> |
.\" HTML <a name="smallassertions"></a> |
| 614 |
.SS "Simple assertions" |
.SS "Simple assertions" |
| 615 |
.rs |
.rs |
| 1017 |
the above patterns match "SUNDAY" as well as "Saturday". |
the above patterns match "SUNDAY" as well as "Saturday". |
| 1018 |
. |
. |
| 1019 |
. |
. |
| 1020 |
|
.SH "DUPLICATE SUBPATTERN NUMBERS" |
| 1021 |
|
.rs |
| 1022 |
|
.sp |
| 1023 |
|
Perl 5.10 introduced a feature whereby each alternative in a subpattern uses |
| 1024 |
|
the same numbers for its capturing parentheses. Such a subpattern starts with |
| 1025 |
|
(?| and is itself a non-capturing subpattern. For example, consider this |
| 1026 |
|
pattern: |
| 1027 |
|
.sp |
| 1028 |
|
(?|(Sat)ur|(Sun))day |
| 1029 |
|
.sp |
| 1030 |
|
Because the two alternatives are inside a (?| group, both sets of capturing |
| 1031 |
|
parentheses are numbered one. Thus, when the pattern matches, you can look |
| 1032 |
|
at captured substring number one, whichever alternative matched. This construct |
| 1033 |
|
is useful when you want to capture part, but not all, of one of a number of |
| 1034 |
|
alternatives. Inside a (?| group, parentheses are numbered as usual, but the |
| 1035 |
|
number is reset at the start of each branch. The numbers of any capturing |
| 1036 |
|
buffers that follow the subpattern start after the highest number used in any |
| 1037 |
|
branch. The following example is taken from the Perl documentation. |
| 1038 |
|
The numbers underneath show in which buffer the captured content will be |
| 1039 |
|
stored. |
| 1040 |
|
.sp |
| 1041 |
|
# before ---------------branch-reset----------- after |
| 1042 |
|
/ ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
| 1043 |
|
# 1 2 2 3 2 3 4 |
| 1044 |
|
.sp |
| 1045 |
|
A backreference or a recursive call to a numbered subpattern always refers to |
| 1046 |
|
the first one in the pattern with the given number. |
| 1047 |
|
.P |
| 1048 |
|
An alternative approach to using this "branch reset" feature is to use |
| 1049 |
|
duplicate named subpatterns, as described in the next section. |
| 1050 |
|
. |
| 1051 |
|
. |
| 1052 |
.SH "NAMED SUBPATTERNS" |
.SH "NAMED SUBPATTERNS" |
| 1053 |
.rs |
.rs |
| 1054 |
.sp |
.sp |
| 1098 |
(?<DN>Sat)(?:urday)? |
(?<DN>Sat)(?:urday)? |
| 1099 |
.sp |
.sp |
| 1100 |
There are five capturing substrings, but only one is ever set after a match. |
There are five capturing substrings, but only one is ever set after a match. |
| 1101 |
|
(An alternative way of solving this problem is to use a "branch reset" |
| 1102 |
|
subpattern, as described in the previous section.) |
| 1103 |
|
.P |
| 1104 |
The convenience function for extracting the data by name returns the substring |
The convenience function for extracting the data by name returns the substring |
| 1105 |
for the first (and in this example, the only) subpattern of that name that |
for the first (and in this example, the only) subpattern of that name that |
| 1106 |
matched. This saves searching to find which numbered subpattern it was. If you |
matched. This saves searching to find which numbered subpattern it was. If you |
| 1306 |
.sp |
.sp |
| 1307 |
\ed++foo |
\ed++foo |
| 1308 |
.sp |
.sp |
| 1309 |
|
Note that a possessive quantifier can be used with an entire group, for |
| 1310 |
|
example: |
| 1311 |
|
.sp |
| 1312 |
|
(abc|xyz){2,3}+ |
| 1313 |
|
.sp |
| 1314 |
Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY |
Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY |
| 1315 |
option is ignored. They are a convenient notation for the simpler forms of |
option is ignored. They are a convenient notation for the simpler forms of |
| 1316 |
atomic group. However, there is no difference in the meaning of a possessive |
atomic group. However, there is no difference in the meaning of a possessive |
| 1385 |
.P |
.P |
| 1386 |
Another way of avoiding the ambiguity inherent in the use of digits following a |
Another way of avoiding the ambiguity inherent in the use of digits following a |
| 1387 |
backslash is to use the \eg escape sequence, which is a feature introduced in |
backslash is to use the \eg escape sequence, which is a feature introduced in |
| 1388 |
Perl 5.10. This escape must be followed by a positive or a negative number, |
Perl 5.10. This escape must be followed by an unsigned number or a negative |
| 1389 |
optionally enclosed in braces. These examples are all identical: |
number, optionally enclosed in braces. These examples are all identical: |
| 1390 |
.sp |
.sp |
| 1391 |
(ring), \e1 |
(ring), \e1 |
| 1392 |
(ring), \eg1 |
(ring), \eg1 |
| 1393 |
(ring), \eg{1} |
(ring), \eg{1} |
| 1394 |
.sp |
.sp |
| 1395 |
A positive number specifies an absolute reference without the ambiguity that is |
An unsigned number specifies an absolute reference without the ambiguity that |
| 1396 |
present in the older syntax. It is also useful when literal digits follow the |
is present in the older syntax. It is also useful when literal digits follow |
| 1397 |
reference. A negative number is a relative reference. Consider this example: |
the reference. A negative number is a relative reference. Consider this |
| 1398 |
|
example: |
| 1399 |
.sp |
.sp |
| 1400 |
(abc(def)ghi)\eg{-1} |
(abc(def)ghi)\eg{-1} |
| 1401 |
.sp |
.sp |
| 1425 |
matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original |
matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original |
| 1426 |
capturing subpattern is matched caselessly. |
capturing subpattern is matched caselessly. |
| 1427 |
.P |
.P |
| 1428 |
Back references to named subpatterns use the Perl syntax \ek<name> or \ek'name' |
There are several different ways of writing back references to named |
| 1429 |
or the Python syntax (?P=name). We could rewrite the above example in either of |
subpatterns. The .NET syntax \ek{name} and the Perl syntax \ek<name> or |
| 1430 |
|
\ek'name' are supported, as is the Python syntax (?P=name). Perl 5.10's unified |
| 1431 |
|
back reference syntax, in which \eg can be used for both numeric and named |
| 1432 |
|
references, is also supported. We could rewrite the above example in any of |
| 1433 |
the following ways: |
the following ways: |
| 1434 |
.sp |
.sp |
| 1435 |
(?<p1>(?i)rah)\es+\ek<p1> |
(?<p1>(?i)rah)\es+\ek<p1> |
| 1436 |
|
(?'p1'(?i)rah)\es+\ek{p1} |
| 1437 |
(?P<p1>(?i)rah)\es+(?P=p1) |
(?P<p1>(?i)rah)\es+(?P=p1) |
| 1438 |
|
(?<p1>(?i)rah)\es+\eg{p1} |
| 1439 |
.sp |
.sp |
| 1440 |
A subpattern that is referenced by name may appear in the pattern before or |
A subpattern that is referenced by name may appear in the pattern before or |
| 1441 |
after the reference. |
after the reference. |
| 1558 |
.sp |
.sp |
| 1559 |
(?<=abc|abde) |
(?<=abc|abde) |
| 1560 |
.sp |
.sp |
| 1561 |
|
In some cases, the Perl 5.10 escape sequence \eK |
| 1562 |
|
.\" HTML <a href="#resetmatchstart"> |
| 1563 |
|
.\" </a> |
| 1564 |
|
(see above) |
| 1565 |
|
.\" |
| 1566 |
|
can be used instead of a lookbehind assertion; this is not restricted to a |
| 1567 |
|
fixed-length. |
| 1568 |
|
.P |
| 1569 |
The implementation of lookbehind assertions is, for each alternative, to |
The implementation of lookbehind assertions is, for each alternative, to |
| 1570 |
temporarily move the current position back by the fixed length and then try to |
temporarily move the current position back by the fixed length and then try to |
| 1571 |
match. If there are insufficient characters before the current position, the |
match. If there are insufficient characters before the current position, the |
| 1660 |
.sp |
.sp |
| 1661 |
If the text between the parentheses consists of a sequence of digits, the |
If the text between the parentheses consists of a sequence of digits, the |
| 1662 |
condition is true if the capturing subpattern of that number has previously |
condition is true if the capturing subpattern of that number has previously |
| 1663 |
matched. An alternative notation is to precede the digits with a plus or minus |
matched. An alternative notation is to precede the digits with a plus or minus |
| 1664 |
sign. In this case, the subpattern number is relative rather than absolute. |
sign. In this case, the subpattern number is relative rather than absolute. |
| 1665 |
The most recently opened parentheses can be referenced by (?(-1), the next most |
The most recently opened parentheses can be referenced by (?(-1), the next most |
| 1666 |
recent by (?(-2), and so on. In looping constructs it can also make sense to |
recent by (?(-2), and so on. In looping constructs it can also make sense to |
| 1667 |
refer to subsequent groups with constructs such as (?(+2). |
refer to subsequent groups with constructs such as (?(+2). |
| 1668 |
.P |
.P |
| 1682 |
subpattern matches nothing. In other words, this pattern matches a sequence of |
subpattern matches nothing. In other words, this pattern matches a sequence of |
| 1683 |
non-parentheses, optionally enclosed in parentheses. |
non-parentheses, optionally enclosed in parentheses. |
| 1684 |
.P |
.P |
| 1685 |
If you were embedding this pattern in a larger one, you could use a relative |
If you were embedding this pattern in a larger one, you could use a relative |
| 1686 |
reference: |
reference: |
| 1687 |
.sp |
.sp |
| 1688 |
...other stuff... ( \e( )? [^()]+ (?(-1) \e) ) ... |
...other stuff... ( \e( )? [^()]+ (?(-1) \e) ) ... |
| 1830 |
( \e( ( (?>[^()]+) | (?1) )* \e) ) |
( \e( ( (?>[^()]+) | (?1) )* \e) ) |
| 1831 |
.sp |
.sp |
| 1832 |
We have put the pattern into parentheses, and caused the recursion to refer to |
We have put the pattern into parentheses, and caused the recursion to refer to |
| 1833 |
them instead of the whole pattern. |
them instead of the whole pattern. |
| 1834 |
.P |
.P |
| 1835 |
In a larger pattern, keeping track of parenthesis numbers can be tricky. This |
In a larger pattern, keeping track of parenthesis numbers can be tricky. This |
| 1836 |
is made easier by the use of relative references. (A Perl 5.10 feature.) |
is made easier by the use of relative references. (A Perl 5.10 feature.) |
| 1851 |
(?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) ) |
(?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) ) |
| 1852 |
.sp |
.sp |
| 1853 |
If there is more than one subpattern with the same name, the earliest one is |
If there is more than one subpattern with the same name, the earliest one is |
| 1854 |
used. |
used. |
| 1855 |
.P |
.P |
| 1856 |
This particular example pattern that we have been looking at contains nested |
This particular example pattern that we have been looking at contains nested |
| 1857 |
unlimited repeats, and so the use of atomic grouping for matching strings of |
unlimited repeats, and so the use of atomic grouping for matching strings of |
| 1913 |
.sp |
.sp |
| 1914 |
(...(absolute)...)...(?2)... |
(...(absolute)...)...(?2)... |
| 1915 |
(...(relative)...)...(?-1)... |
(...(relative)...)...(?-1)... |
| 1916 |
(...(?+1)...(relative)... |
(...(?+1)...(relative)... |
| 1917 |
.sp |
.sp |
| 1918 |
An earlier example pointed out that the pattern |
An earlier example pointed out that the pattern |
| 1919 |
.sp |
.sp |
| 1978 |
documentation. |
documentation. |
| 1979 |
. |
. |
| 1980 |
. |
. |
| 1981 |
|
.SH "BACTRACKING CONTROL" |
| 1982 |
|
.rs |
| 1983 |
|
.sp |
| 1984 |
|
Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which |
| 1985 |
|
are described in the Perl documentation as "experimental and subject to change |
| 1986 |
|
or removal in a future version of Perl". It goes on to say: "Their usage in |
| 1987 |
|
production code should be noted to avoid problems during upgrades." The same |
| 1988 |
|
remarks apply to the PCRE features described in this section. |
| 1989 |
|
.P |
| 1990 |
|
Since these verbs are specifically related to backtracking, they can be used |
| 1991 |
|
only when the pattern is to be matched using \fBpcre_exec()\fP, which uses a |
| 1992 |
|
backtracking algorithm. They cause an error if encountered by |
| 1993 |
|
\fBpcre_dfa_exec()\fP. |
| 1994 |
|
.P |
| 1995 |
|
The new verbs make use of what was previously invalid syntax: an opening |
| 1996 |
|
parenthesis followed by an asterisk. In Perl, they are generally of the form |
| 1997 |
|
(*VERB:ARG) but PCRE does not support the use of arguments, so its general |
| 1998 |
|
form is just (*VERB). Any number of these verbs may occur in a pattern. There |
| 1999 |
|
are two kinds: |
| 2000 |
|
. |
| 2001 |
|
.SS "Verbs that act immediately" |
| 2002 |
|
.rs |
| 2003 |
|
.sp |
| 2004 |
|
The following verbs act as soon as they are encountered: |
| 2005 |
|
.sp |
| 2006 |
|
(*ACCEPT) |
| 2007 |
|
.sp |
| 2008 |
|
This verb causes the match to end successfully, skipping the remainder of the |
| 2009 |
|
pattern. When inside a recursion, only the innermost pattern is ended |
| 2010 |
|
immediately. PCRE differs from Perl in what happens if the (*ACCEPT) is inside |
| 2011 |
|
capturing parentheses. In Perl, the data so far is captured: in PCRE no data is |
| 2012 |
|
captured. For example: |
| 2013 |
|
.sp |
| 2014 |
|
A(A|B(*ACCEPT)|C)D |
| 2015 |
|
.sp |
| 2016 |
|
This matches "AB", "AAD", or "ACD", but when it matches "AB", no data is |
| 2017 |
|
captured. |
| 2018 |
|
.sp |
| 2019 |
|
(*FAIL) or (*F) |
| 2020 |
|
.sp |
| 2021 |
|
This verb causes the match to fail, forcing backtracking to occur. It is |
| 2022 |
|
equivalent to (?!) but easier to read. The Perl documentation notes that it is |
| 2023 |
|
probably useful only when combined with (?{}) or (??{}). Those are, of course, |
| 2024 |
|
Perl features that are not present in PCRE. The nearest equivalent is the |
| 2025 |
|
callout feature, as for example in this pattern: |
| 2026 |
|
.sp |
| 2027 |
|
a+(?C)(*FAIL) |
| 2028 |
|
.sp |
| 2029 |
|
A match with the string "aaaa" always fails, but the callout is taken before |
| 2030 |
|
each backtrack happens (in this example, 10 times). |
| 2031 |
|
. |
| 2032 |
|
.SS "Verbs that act after backtracking" |
| 2033 |
|
.rs |
| 2034 |
|
.sp |
| 2035 |
|
The following verbs do nothing when they are encountered. Matching continues |
| 2036 |
|
with what follows, but if there is no subsequent match, a failure is forced. |
| 2037 |
|
The verbs differ in exactly what kind of failure occurs. |
| 2038 |
|
.sp |
| 2039 |
|
(*COMMIT) |
| 2040 |
|
.sp |
| 2041 |
|
This verb causes the whole match to fail outright if the rest of the pattern |
| 2042 |
|
does not match. Even if the pattern is unanchored, no further attempts to find |
| 2043 |
|
a match by advancing the start point take place. Once (*COMMIT) has been |
| 2044 |
|
passed, \fBpcre_exec()\fP is committed to finding a match at the current |
| 2045 |
|
starting point, or not at all. For example: |
| 2046 |
|
.sp |
| 2047 |
|
a+(*COMMIT)b |
| 2048 |
|
.sp |
| 2049 |
|
This matches "xxaab" but not "aacaab". It can be thought of as a kind of |
| 2050 |
|
dynamic anchor, or "I've started, so I must finish." |
| 2051 |
|
.sp |
| 2052 |
|
(*PRUNE) |
| 2053 |
|
.sp |
| 2054 |
|
This verb causes the match to fail at the current position if the rest of the |
| 2055 |
|
pattern does not match. If the pattern is unanchored, the normal "bumpalong" |
| 2056 |
|
advance to the next starting character then happens. Backtracking can occur as |
| 2057 |
|
usual to the left of (*PRUNE), or when matching to the right of (*PRUNE), but |
| 2058 |
|
if there is no match to the right, backtracking cannot cross (*PRUNE). |
| 2059 |
|
In simple cases, the use of (*PRUNE) is just an alternative to an atomic |
| 2060 |
|
group or possessive quantifier, but there are some uses of (*PRUNE) that cannot |
| 2061 |
|
be expressed in any other way. |
| 2062 |
|
.sp |
| 2063 |
|
(*SKIP) |
| 2064 |
|
.sp |
| 2065 |
|
This verb is like (*PRUNE), except that if the pattern is unanchored, the |
| 2066 |
|
"bumpalong" advance is not to the next character, but to the position in the |
| 2067 |
|
subject where (*SKIP) was encountered. (*SKIP) signifies that whatever text |
| 2068 |
|
was matched leading up to it cannot be part of a successful match. Consider: |
| 2069 |
|
.sp |
| 2070 |
|
a+(*SKIP)b |
| 2071 |
|
.sp |
| 2072 |
|
If the subject is "aaaac...", after the first match attempt fails (starting at |
| 2073 |
|
the first character in the string), the starting point skips on to start the |
| 2074 |
|
next attempt at "c". Note that a possessive quantifer does not have the same |
| 2075 |
|
effect in this example; although it would suppress backtracking during the |
| 2076 |
|
first match attempt, the second attempt would start at the second character |
| 2077 |
|
instead of skipping on to "c". |
| 2078 |
|
.sp |
| 2079 |
|
(*THEN) |
| 2080 |
|
.sp |
| 2081 |
|
This verb causes a skip to the next alternation if the rest of the pattern does |
| 2082 |
|
not match. That is, it cancels pending backtracking, but only within the |
| 2083 |
|
current alternation. Its name comes from the observation that it can be used |
| 2084 |
|
for a pattern-based if-then-else block: |
| 2085 |
|
.sp |
| 2086 |
|
( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ... |
| 2087 |
|
.sp |
| 2088 |
|
If the COND1 pattern matches, FOO is tried (and possibly further items after |
| 2089 |
|
the end of the group if FOO succeeds); on failure the matcher skips to the |
| 2090 |
|
second alternative and tries COND2, without backtracking into COND1. If (*THEN) |
| 2091 |
|
is used outside of any alternation, it acts exactly like (*PRUNE). |
| 2092 |
|
. |
| 2093 |
|
. |
| 2094 |
.SH "SEE ALSO" |
.SH "SEE ALSO" |
| 2095 |
.rs |
.rs |
| 2096 |
.sp |
.sp |
| 2111 |
.rs |
.rs |
| 2112 |
.sp |
.sp |
| 2113 |
.nf |
.nf |
| 2114 |
Last updated: 09 May 2007 |
Last updated: 09 August 2007 |
| 2115 |
Copyright (c) 1997-2007 University of Cambridge. |
Copyright (c) 1997-2007 University of Cambridge. |
| 2116 |
.fi |
.fi |