| 21 |
description of PCRE's regular expressions is intended as reference material. |
description of PCRE's regular expressions is intended as reference material. |
| 22 |
.P |
.P |
| 23 |
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, |
| 24 |
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, |
| 25 |
build PCRE to include UTF-8 support, and then call \fBpcre_compile()\fP with |
PCRE must be built to include UTF-8 support, and you must call |
| 26 |
the PCRE_UTF8 option. There is also a special sequence that can be given at the |
\fBpcre_compile()\fP or \fBpcre_compile2()\fP with the PCRE_UTF8 option. There |
| 27 |
start of a pattern: |
is also a special sequence that can be given at the start of a pattern: |
| 28 |
.sp |
.sp |
| 29 |
(*UTF8) |
(*UTF8) |
| 30 |
.sp |
.sp |
| 83 |
(*ANYCRLF) any of the three above |
(*ANYCRLF) any of the three above |
| 84 |
(*ANY) all Unicode newline sequences |
(*ANY) all Unicode newline sequences |
| 85 |
.sp |
.sp |
| 86 |
These override the default and the options given to \fBpcre_compile()\fP. For |
These override the default and the options given to \fBpcre_compile()\fP or |
| 87 |
example, on a Unix system where LF is the default newline sequence, the pattern |
\fBpcre_compile2()\fP. For example, on a Unix system where LF is the default |
| 88 |
|
newline sequence, the pattern |
| 89 |
.sp |
.sp |
| 90 |
(*CR)a.b |
(*CR)a.b |
| 91 |
.sp |
.sp |
| 95 |
they must be in upper case. If more than one of them is present, the last one |
they must be in upper case. If more than one of them is present, the last one |
| 96 |
is used. |
is used. |
| 97 |
.P |
.P |
| 98 |
The newline convention does not affect what the \eR escape sequence matches. By |
The newline convention affects the interpretation of the dot metacharacter when |
| 99 |
default, this is any Unicode newline sequence, for Perl compatibility. However, |
PCRE_DOTALL is not set, and also the behaviour of \eN. However, it does not |
| 100 |
this can be changed; see the description of \eR in the section entitled |
affect what the \eR escape sequence matches. By default, this is any Unicode |
| 101 |
|
newline sequence, for Perl compatibility. However, this can be changed; see the |
| 102 |
|
description of \eR in the section entitled |
| 103 |
.\" HTML <a href="#newlineseq"> |
.\" HTML <a href="#newlineseq"> |
| 104 |
.\" </a> |
.\" </a> |
| 105 |
"Newline sequences" |
"Newline sequences" |
| 209 |
A second use of backslash provides a way of encoding non-printing characters |
A second use of backslash provides a way of encoding non-printing characters |
| 210 |
in patterns in a visible manner. There is no restriction on the appearance of |
in patterns in a visible manner. There is no restriction on the appearance of |
| 211 |
non-printing characters, apart from the binary zero that terminates a pattern, |
non-printing characters, apart from the binary zero that terminates a pattern, |
| 212 |
but when a pattern is being prepared by text editing, it is usually easier to |
but when a pattern is being prepared by text editing, it is often easier to use |
| 213 |
use one of the following escape sequences than the binary character it |
one of the following escape sequences than the binary character it represents: |
|
represents: |
|
| 214 |
.sp |
.sp |
| 215 |
\ea alarm, that is, the BEL character (hex 07) |
\ea alarm, that is, the BEL character (hex 07) |
| 216 |
\ecx "control-x", where x is any character |
\ecx "control-x", where x is any character |
| 219 |
\en linefeed (hex 0A) |
\en linefeed (hex 0A) |
| 220 |
\er carriage return (hex 0D) |
\er carriage return (hex 0D) |
| 221 |
\et tab (hex 09) |
\et tab (hex 09) |
| 222 |
\eddd character with octal code ddd, or backreference |
\eddd character with octal code ddd, or back reference |
| 223 |
\exhh character with hex code hh |
\exhh character with hex code hh |
| 224 |
\ex{hhh..} character with hex code hhh.. |
\ex{hhh..} character with hex code hhh.. |
| 225 |
.sp |
.sp |
| 297 |
.P |
.P |
| 298 |
All the sequences that define a single character value can be used both inside |
All the sequences that define a single character value can be used both inside |
| 299 |
and outside character classes. In addition, inside a character class, the |
and outside character classes. In addition, inside a character class, the |
| 300 |
sequence \eb is interpreted as the backspace character (hex 08), and the |
sequence \eb is interpreted as the backspace character (hex 08). The sequences |
| 301 |
sequences \eR and \eX are interpreted as the characters "R" and "X", |
\eB, \eN, \eR, and \eX are not special inside a character class. Like any other |
| 302 |
respectively. Outside a character class, these sequences have different |
unrecognized escape sequences, they are treated as the literal characters "B", |
| 303 |
meanings |
"N", "R", and "X" by default, but cause an error if the PCRE_EXTRA option is |
| 304 |
.\" HTML <a href="#uniextseq"> |
set. Outside a character class, these sequences have different meanings. |
|
.\" </a> |
|
|
(see below). |
|
|
.\" |
|
| 305 |
. |
. |
| 306 |
. |
. |
| 307 |
.SS "Absolute and relative back references" |
.SS "Absolute and relative back references" |
| 332 |
later. |
later. |
| 333 |
.\" |
.\" |
| 334 |
Note that \eg{...} (Perl syntax) and \eg<...> (Oniguruma syntax) are \fInot\fP |
Note that \eg{...} (Perl syntax) and \eg<...> (Oniguruma syntax) are \fInot\fP |
| 335 |
synonymous. The former is a back reference; the latter is a subroutine call. |
synonymous. The former is a back reference; the latter is a |
| 336 |
|
.\" HTML <a href="#subpatternsassubroutines"> |
| 337 |
|
.\" </a> |
| 338 |
|
subroutine |
| 339 |
|
.\" |
| 340 |
|
call. |
| 341 |
. |
. |
| 342 |
. |
. |
| 343 |
.SS "Generic character types" |
.SS "Generic character types" |
| 344 |
.rs |
.rs |
| 345 |
.sp |
.sp |
| 346 |
Another use of backslash is for specifying generic character types. The |
Another use of backslash is for specifying generic character types: |
|
following are always recognized: |
|
| 347 |
.sp |
.sp |
| 348 |
\ed any decimal digit |
\ed any decimal digit |
| 349 |
\eD any character that is not a decimal digit |
\eD any character that is not a decimal digit |
| 356 |
\ew any "word" character |
\ew any "word" character |
| 357 |
\eW any "non-word" character |
\eW any "non-word" character |
| 358 |
.sp |
.sp |
| 359 |
Each pair of escape sequences partitions the complete set of characters into |
There is also the single sequence \eN, which matches a non-newline character. |
| 360 |
two disjoint sets. Any given character matches one, and only one, of each pair. |
This is the same as |
| 361 |
|
.\" HTML <a href="#fullstopdot"> |
| 362 |
|
.\" </a> |
| 363 |
|
the "." metacharacter |
| 364 |
|
.\" |
| 365 |
|
when PCRE_DOTALL is not set. |
| 366 |
|
.P |
| 367 |
|
Each pair of lower and upper case escape sequences partitions the complete set |
| 368 |
|
of characters into two disjoint sets. Any given character matches one, and only |
| 369 |
|
one, of each pair. |
| 370 |
.P |
.P |
| 371 |
These character type sequences can appear both inside and outside character |
These character type sequences can appear both inside and outside character |
| 372 |
classes. They each match one character of the appropriate type. If the current |
classes. They each match one character of the appropriate type. If the current |
| 475 |
(*BSR_ANYCRLF) CR, LF, or CRLF only |
(*BSR_ANYCRLF) CR, LF, or CRLF only |
| 476 |
(*BSR_UNICODE) any Unicode newline sequence |
(*BSR_UNICODE) any Unicode newline sequence |
| 477 |
.sp |
.sp |
| 478 |
These override the default and the options given to \fBpcre_compile()\fP, but |
These override the default and the options given to \fBpcre_compile()\fP or |
| 479 |
they can be overridden by options given to \fBpcre_exec()\fP. Note that these |
\fBpcre_compile2()\fP, but they can be overridden by options given to |
| 480 |
special settings, which are not Perl-compatible, are recognized only at the |
\fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP. Note that these special settings, |
| 481 |
very start of a pattern, and that they must be in upper case. If more than one |
which are not Perl-compatible, are recognized only at the very start of a |
| 482 |
of them is present, the last one is used. They can be combined with a change of |
pattern, and that they must be in upper case. If more than one of them is |
| 483 |
newline convention, for example, a pattern can start with: |
present, the last one is used. They can be combined with a change of newline |
| 484 |
|
convention, for example, a pattern can start with: |
| 485 |
.sp |
.sp |
| 486 |
(*ANY)(*BSR_ANYCRLF) |
(*ANY)(*BSR_ANYCRLF) |
| 487 |
.sp |
.sp |
| 488 |
Inside a character class, \eR matches the letter "R". |
Inside a character class, \eR is treated as an unrecognized escape sequence, |
| 489 |
|
and so matches the letter "R" by default, but causes an error if PCRE_EXTRA is |
| 490 |
|
set. |
| 491 |
. |
. |
| 492 |
. |
. |
| 493 |
.\" HTML <a name="uniextseq"></a> |
.\" HTML <a name="uniextseq"></a> |
| 505 |
\eX an extended Unicode sequence |
\eX an extended Unicode sequence |
| 506 |
.sp |
.sp |
| 507 |
The property names represented by \fIxx\fP above are limited to the Unicode |
The property names represented by \fIxx\fP above are limited to the Unicode |
| 508 |
script names, the general category properties, and "Any", which matches any |
script names, the general category properties, "Any", which matches any |
| 509 |
character (including newline). Other properties such as "InMusicalSymbols" are |
character (including newline), and some special PCRE properties (described |
| 510 |
not currently supported by PCRE. Note that \eP{Any} does not match any |
in the |
| 511 |
characters, so always causes a match failure. |
.\" HTML <a href="#extraprops"> |
| 512 |
|
.\" </a> |
| 513 |
|
next section). |
| 514 |
|
.\" |
| 515 |
|
Other Perl properties such as "InMusicalSymbols" are not currently supported by |
| 516 |
|
PCRE. Note that \eP{Any} does not match any characters, so always causes a |
| 517 |
|
match failure. |
| 518 |
.P |
.P |
| 519 |
Sets of Unicode characters are defined as belonging to certain scripts. A |
Sets of Unicode characters are defined as belonging to certain scripts. A |
| 520 |
character from one of these sets can be matched using a script name. For |
character from one of these sets can be matched using a script name. For |
| 528 |
.P |
.P |
| 529 |
Arabic, |
Arabic, |
| 530 |
Armenian, |
Armenian, |
| 531 |
|
Avestan, |
| 532 |
Balinese, |
Balinese, |
| 533 |
|
Bamum, |
| 534 |
Bengali, |
Bengali, |
| 535 |
Bopomofo, |
Bopomofo, |
| 536 |
Braille, |
Braille, |
| 537 |
Buginese, |
Buginese, |
| 538 |
Buhid, |
Buhid, |
| 539 |
Canadian_Aboriginal, |
Canadian_Aboriginal, |
| 540 |
|
Carian, |
| 541 |
|
Cham, |
| 542 |
Cherokee, |
Cherokee, |
| 543 |
Common, |
Common, |
| 544 |
Coptic, |
Coptic, |
| 547 |
Cyrillic, |
Cyrillic, |
| 548 |
Deseret, |
Deseret, |
| 549 |
Devanagari, |
Devanagari, |
| 550 |
|
Egyptian_Hieroglyphs, |
| 551 |
Ethiopic, |
Ethiopic, |
| 552 |
Georgian, |
Georgian, |
| 553 |
Glagolitic, |
Glagolitic, |
| 560 |
Hanunoo, |
Hanunoo, |
| 561 |
Hebrew, |
Hebrew, |
| 562 |
Hiragana, |
Hiragana, |
| 563 |
|
Imperial_Aramaic, |
| 564 |
Inherited, |
Inherited, |
| 565 |
|
Inscriptional_Pahlavi, |
| 566 |
|
Inscriptional_Parthian, |
| 567 |
|
Javanese, |
| 568 |
|
Kaithi, |
| 569 |
Kannada, |
Kannada, |
| 570 |
Katakana, |
Katakana, |
| 571 |
|
Kayah_Li, |
| 572 |
Kharoshthi, |
Kharoshthi, |
| 573 |
Khmer, |
Khmer, |
| 574 |
Lao, |
Lao, |
| 575 |
Latin, |
Latin, |
| 576 |
|
Lepcha, |
| 577 |
Limbu, |
Limbu, |
| 578 |
Linear_B, |
Linear_B, |
| 579 |
|
Lisu, |
| 580 |
|
Lycian, |
| 581 |
|
Lydian, |
| 582 |
Malayalam, |
Malayalam, |
| 583 |
|
Meetei_Mayek, |
| 584 |
Mongolian, |
Mongolian, |
| 585 |
Myanmar, |
Myanmar, |
| 586 |
New_Tai_Lue, |
New_Tai_Lue, |
| 588 |
Ogham, |
Ogham, |
| 589 |
Old_Italic, |
Old_Italic, |
| 590 |
Old_Persian, |
Old_Persian, |
| 591 |
|
Old_South_Arabian, |
| 592 |
|
Old_Turkic, |
| 593 |
|
Ol_Chiki, |
| 594 |
Oriya, |
Oriya, |
| 595 |
Osmanya, |
Osmanya, |
| 596 |
Phags_Pa, |
Phags_Pa, |
| 597 |
Phoenician, |
Phoenician, |
| 598 |
|
Rejang, |
| 599 |
Runic, |
Runic, |
| 600 |
|
Samaritan, |
| 601 |
|
Saurashtra, |
| 602 |
Shavian, |
Shavian, |
| 603 |
Sinhala, |
Sinhala, |
| 604 |
|
Sundanese, |
| 605 |
Syloti_Nagri, |
Syloti_Nagri, |
| 606 |
Syriac, |
Syriac, |
| 607 |
Tagalog, |
Tagalog, |
| 608 |
Tagbanwa, |
Tagbanwa, |
| 609 |
Tai_Le, |
Tai_Le, |
| 610 |
|
Tai_Tham, |
| 611 |
|
Tai_Viet, |
| 612 |
Tamil, |
Tamil, |
| 613 |
Telugu, |
Telugu, |
| 614 |
Thaana, |
Thaana, |
| 616 |
Tibetan, |
Tibetan, |
| 617 |
Tifinagh, |
Tifinagh, |
| 618 |
Ugaritic, |
Ugaritic, |
| 619 |
|
Vai, |
| 620 |
Yi. |
Yi. |
| 621 |
.P |
.P |
| 622 |
Each character has exactly one general category property, specified by a |
Each character has exactly one Unicode general category property, specified by |
| 623 |
two-letter abbreviation. For compatibility with Perl, negation can be specified |
a two-letter abbreviation. For compatibility with Perl, negation can be |
| 624 |
by including a circumflex between the opening brace and the property name. For |
specified by including a circumflex between the opening brace and the property |
| 625 |
example, \ep{^Lu} is the same as \eP{Lu}. |
name. For example, \ep{^Lu} is the same as \eP{Lu}. |
| 626 |
.P |
.P |
| 627 |
If only one letter is specified with \ep or \eP, it includes all the general |
If only one letter is specified with \ep or \eP, it includes all the general |
| 628 |
category properties that start with that letter. In this case, in the absence |
category properties that start with that letter. In this case, in the absence |
| 689 |
.\" HREF |
.\" HREF |
| 690 |
\fBpcreapi\fP |
\fBpcreapi\fP |
| 691 |
.\" |
.\" |
| 692 |
page). |
page). Perl does not support the Cs property. |
| 693 |
.P |
.P |
| 694 |
The long synonyms for these properties that Perl supports (such as \ep{Letter}) |
The long synonyms for property names that Perl supports (such as \ep{Letter}) |
| 695 |
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 |
| 696 |
properties with "Is". |
properties with "Is". |
| 697 |
.P |
.P |
| 724 |
properties in PCRE. |
properties in PCRE. |
| 725 |
. |
. |
| 726 |
. |
. |
| 727 |
|
.\" HTML <a name="extraprops"></a> |
| 728 |
|
.SS PCRE's additional properties |
| 729 |
|
.rs |
| 730 |
|
.sp |
| 731 |
|
As well as the standard Unicode properties described in the previous |
| 732 |
|
section, PCRE supports four more that make it possible to convert traditional |
| 733 |
|
escape sequences such as \ew and \es and POSIX character classes to use Unicode |
| 734 |
|
properties. These are: |
| 735 |
|
.sp |
| 736 |
|
Xan Any alphanumeric character |
| 737 |
|
Xps Any POSIX space character |
| 738 |
|
Xsp Any Perl space character |
| 739 |
|
Xwd Any Perl "word" character |
| 740 |
|
.sp |
| 741 |
|
Xan matches characters that have either the L (letter) or the N (number) |
| 742 |
|
property. Xps matches the characters tab, linefeed, vertical tab, formfeed, or |
| 743 |
|
carriage return, and any other character that has the Z (separator) property. |
| 744 |
|
Xsp is the same as Xps, except that vertical tab is excluded. Xwd matches the |
| 745 |
|
same characters as Xan, plus underscore. |
| 746 |
|
. |
| 747 |
|
. |
| 748 |
.\" HTML <a name="resetmatchstart"></a> |
.\" HTML <a name="resetmatchstart"></a> |
| 749 |
.SS "Resetting the match start" |
.SS "Resetting the match start" |
| 750 |
.rs |
.rs |
| 773 |
(foo)\eKbar |
(foo)\eKbar |
| 774 |
.sp |
.sp |
| 775 |
matches "foobar", the first substring is still set to "foo". |
matches "foobar", the first substring is still set to "foo". |
| 776 |
|
.P |
| 777 |
|
Perl documents that the use of \eK within assertions is "not well defined". In |
| 778 |
|
PCRE, \eK is acted upon when it occurs inside positive assertions, but is |
| 779 |
|
ignored in negative assertions. |
| 780 |
. |
. |
| 781 |
. |
. |
| 782 |
.\" HTML <a name="smallassertions"></a> |
.\" HTML <a name="smallassertions"></a> |
| 801 |
\ez matches only at the end of the subject |
\ez matches only at the end of the subject |
| 802 |
\eG matches at the first matching position in the subject |
\eG matches at the first matching position in the subject |
| 803 |
.sp |
.sp |
| 804 |
These assertions may not appear in character classes (but note that \eb has a |
Inside a character class, \eb has a different meaning; it matches the backspace |
| 805 |
different meaning, namely the backspace character, inside a character class). |
character. If any other of these assertions appears in a character class, by |
| 806 |
|
default it matches the corresponding literal character (for example, \eB |
| 807 |
|
matches the letter B). However, if the PCRE_EXTRA option is set, an "invalid |
| 808 |
|
escape sequence" error is generated instead. |
| 809 |
.P |
.P |
| 810 |
A word boundary is a position in the subject string where the current character |
A word boundary is a position in the subject string where the current character |
| 811 |
and the previous character do not both match \ew or \eW (i.e. one matches |
and the previous character do not both match \ew or \eW (i.e. one matches |
| 812 |
\ew and the other matches \eW), or the start or end of the string if the |
\ew and the other matches \eW), or the start or end of the string if the |
| 813 |
first or last character matches \ew, respectively. |
first or last character matches \ew, respectively. Neither PCRE nor Perl has a |
| 814 |
|
separte "start of word" or "end of word" metasequence. However, whatever |
| 815 |
|
follows \eb normally determines which it is. For example, the fragment |
| 816 |
|
\eba matches "a" at the start of a word. |
| 817 |
.P |
.P |
| 818 |
The \eA, \eZ, and \ez assertions differ from the traditional circumflex and |
The \eA, \eZ, and \ez assertions differ from the traditional circumflex and |
| 819 |
dollar (described in the next section) in that they only ever match at the very |
dollar (described in the next section) in that they only ever match at the very |
| 897 |
\eA it is always anchored, whether or not PCRE_MULTILINE is set. |
\eA it is always anchored, whether or not PCRE_MULTILINE is set. |
| 898 |
. |
. |
| 899 |
. |
. |
| 900 |
.SH "FULL STOP (PERIOD, DOT)" |
.\" HTML <a name="fullstopdot"></a> |
| 901 |
|
.SH "FULL STOP (PERIOD, DOT) AND \eN" |
| 902 |
.rs |
.rs |
| 903 |
.sp |
.sp |
| 904 |
Outside a character class, a dot in the pattern matches any one character in |
Outside a character class, a dot in the pattern matches any one character in |
| 920 |
The handling of dot is entirely independent of the handling of circumflex and |
The handling of dot is entirely independent of the handling of circumflex and |
| 921 |
dollar, the only relationship being that they both involve newlines. Dot has no |
dollar, the only relationship being that they both involve newlines. Dot has no |
| 922 |
special meaning in a character class. |
special meaning in a character class. |
| 923 |
|
.P |
| 924 |
|
The escape sequence \eN always behaves as a dot does when PCRE_DOTALL is not |
| 925 |
|
set. In other words, it matches any one character except one that signifies the |
| 926 |
|
end of a line. |
| 927 |
. |
. |
| 928 |
. |
. |
| 929 |
.SH "MATCHING A SINGLE BYTE" |
.SH "MATCHING A SINGLE BYTE" |
| 950 |
.rs |
.rs |
| 951 |
.sp |
.sp |
| 952 |
An opening square bracket introduces a character class, terminated by a closing |
An opening square bracket introduces a character class, terminated by a closing |
| 953 |
square bracket. A closing square bracket on its own is not special. If a |
square bracket. A closing square bracket on its own is not special by default. |
| 954 |
closing square bracket is required as a member of the class, it should be the |
However, if the PCRE_JAVASCRIPT_COMPAT option is set, a lone closing square |
| 955 |
first data character in the class (after an initial circumflex, if present) or |
bracket causes a compile-time error. If a closing square bracket is required as |
| 956 |
escaped with a backslash. |
a member of the class, it should be the first data character in the class |
| 957 |
|
(after an initial circumflex, if present) or escaped with a backslash. |
| 958 |
.P |
.P |
| 959 |
A character class matches a single character in the subject. In UTF-8 mode, the |
A character class matches a single character in the subject. In UTF-8 mode, the |
| 960 |
character may occupy more than one byte. A matched character must be in the set |
character may be more than one byte long. A matched character must be in the |
| 961 |
of characters defined by the class, unless the first character in the class |
set of characters defined by the class, unless the first character in the class |
| 962 |
definition is a circumflex, in which case the subject character must not be in |
definition is a circumflex, in which case the subject character must not be in |
| 963 |
the set defined by the class. If a circumflex is actually required as a member |
the set defined by the class. If a circumflex is actually required as a member |
| 964 |
of the class, ensure it is not the first character, or escape it with a |
of the class, ensure it is not the first character, or escape it with a |
| 968 |
[^aeiou] matches any character that is not a lower case vowel. Note that a |
[^aeiou] matches any character that is not a lower case vowel. Note that a |
| 969 |
circumflex is just a convenient notation for specifying the characters that |
circumflex is just a convenient notation for specifying the characters that |
| 970 |
are in the class by enumerating those that are not. A class that starts with a |
are in the class by enumerating those that are not. A class that starts with a |
| 971 |
circumflex is not an assertion: it still consumes a character from the subject |
circumflex is not an assertion; it still consumes a character from the subject |
| 972 |
string, and therefore it fails if the current pointer is at the end of the |
string, and therefore it fails if the current pointer is at the end of the |
| 973 |
string. |
string. |
| 974 |
.P |
.P |
| 982 |
case for characters whose values are less than 128, so caseless matching is |
case for characters whose values are less than 128, so caseless matching is |
| 983 |
always possible. For characters with higher values, the concept of case is |
always possible. For characters with higher values, the concept of case is |
| 984 |
supported if PCRE is compiled with Unicode property support, but not otherwise. |
supported if PCRE is compiled with Unicode property support, but not otherwise. |
| 985 |
If you want to use caseless matching for characters 128 and above, you must |
If you want to use caseless matching in UTF8-mode for characters 128 and above, |
| 986 |
ensure that PCRE is compiled with Unicode property support as well as with |
you must ensure that PCRE is compiled with Unicode property support as well as |
| 987 |
UTF-8 support. |
with UTF-8 support. |
| 988 |
.P |
.P |
| 989 |
Characters that might indicate line breaks are never treated in any special way |
Characters that might indicate line breaks are never treated in any special way |
| 990 |
when matching character classes, whatever line-ending sequence is in use, and |
when matching character classes, whatever line-ending sequence is in use, and |
| 1211 |
the above patterns match "SUNDAY" as well as "Saturday". |
the above patterns match "SUNDAY" as well as "Saturday". |
| 1212 |
. |
. |
| 1213 |
. |
. |
| 1214 |
|
.\" HTML <a name="dupsubpatternnumber"></a> |
| 1215 |
.SH "DUPLICATE SUBPATTERN NUMBERS" |
.SH "DUPLICATE SUBPATTERN NUMBERS" |
| 1216 |
.rs |
.rs |
| 1217 |
.sp |
.sp |
| 1237 |
/ ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
/ ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
| 1238 |
# 1 2 2 3 2 3 4 |
# 1 2 2 3 2 3 4 |
| 1239 |
.sp |
.sp |
| 1240 |
A backreference or a recursive call to a numbered subpattern always refers to |
A back reference to a numbered subpattern uses the most recent value that is |
| 1241 |
the first one in the pattern with the given number. |
set for that number by any subpattern. The following pattern matches "abcabc" |
| 1242 |
|
or "defdef": |
| 1243 |
|
.sp |
| 1244 |
|
/(?|(abc)|(def))\e1/ |
| 1245 |
|
.sp |
| 1246 |
|
In contrast, a recursive or "subroutine" call to a numbered subpattern always |
| 1247 |
|
refers to the first one in the pattern with the given number. The following |
| 1248 |
|
pattern matches "abcabc" or "defabc": |
| 1249 |
|
.sp |
| 1250 |
|
/(?|(abc)|(def))(?1)/ |
| 1251 |
|
.sp |
| 1252 |
|
If a |
| 1253 |
|
.\" HTML <a href="#conditions"> |
| 1254 |
|
.\" </a> |
| 1255 |
|
condition test |
| 1256 |
|
.\" |
| 1257 |
|
for a subpattern's having matched refers to a non-unique number, the test is |
| 1258 |
|
true if any of the subpatterns of that number have matched. |
| 1259 |
.P |
.P |
| 1260 |
An alternative approach to using this "branch reset" feature is to use |
An alternative approach to using this "branch reset" feature is to use |
| 1261 |
duplicate named subpatterns, as described in the next section. |
duplicate named subpatterns, as described in the next section. |
| 1270 |
difficulty, PCRE supports the naming of subpatterns. This feature was not |
difficulty, PCRE supports the naming of subpatterns. This feature was not |
| 1271 |
added to Perl until release 5.10. Python had the feature earlier, and PCRE |
added to Perl until release 5.10. Python had the feature earlier, and PCRE |
| 1272 |
introduced it at release 4.0, using the Python syntax. PCRE now supports both |
introduced it at release 4.0, using the Python syntax. PCRE now supports both |
| 1273 |
the Perl and the Python syntax. |
the Perl and the Python syntax. Perl allows identically numbered subpatterns to |
| 1274 |
|
have different names, but PCRE does not. |
| 1275 |
.P |
.P |
| 1276 |
In PCRE, a subpattern can be named in one of three ways: (?<name>...) or |
In PCRE, a subpattern can be named in one of three ways: (?<name>...) or |
| 1277 |
(?'name'...) as in Perl, or (?P<name>...) as in Python. References to capturing |
(?'name'...) as in Perl, or (?P<name>...) as in Python. References to capturing |
| 1278 |
parentheses from other parts of the pattern, such as |
parentheses from other parts of the pattern, such as |
| 1279 |
.\" HTML <a href="#backreferences"> |
.\" HTML <a href="#backreferences"> |
| 1280 |
.\" </a> |
.\" </a> |
| 1281 |
backreferences, |
back references, |
| 1282 |
.\" |
.\" |
| 1283 |
.\" HTML <a href="#recursion"> |
.\" HTML <a href="#recursion"> |
| 1284 |
.\" </a> |
.\" </a> |
| 1298 |
is also a convenience function for extracting a captured substring by name. |
is also a convenience function for extracting a captured substring by name. |
| 1299 |
.P |
.P |
| 1300 |
By default, a name must be unique within a pattern, but it is possible to relax |
By default, a name must be unique within a pattern, but it is possible to relax |
| 1301 |
this constraint by setting the PCRE_DUPNAMES option at compile time. This can |
this constraint by setting the PCRE_DUPNAMES option at compile time. (Duplicate |
| 1302 |
be useful for patterns where only one instance of the named parentheses can |
names are also always permitted for subpatterns with the same number, set up as |
| 1303 |
match. Suppose you want to match the name of a weekday, either as a 3-letter |
described in the previous section.) Duplicate names can be useful for patterns |
| 1304 |
abbreviation or as the full name, and in both cases you want to extract the |
where only one instance of the named parentheses can match. Suppose you want to |
| 1305 |
abbreviation. This pattern (ignoring the line breaks) does the job: |
match the name of a weekday, either as a 3-letter abbreviation or as the full |
| 1306 |
|
name, and in both cases you want to extract the abbreviation. This pattern |
| 1307 |
|
(ignoring the line breaks) does the job: |
| 1308 |
.sp |
.sp |
| 1309 |
(?<DN>Mon|Fri|Sun)(?:day)?| |
(?<DN>Mon|Fri|Sun)(?:day)?| |
| 1310 |
(?<DN>Tue)(?:sday)?| |
(?<DN>Tue)(?:sday)?| |
| 1318 |
.P |
.P |
| 1319 |
The convenience function for extracting the data by name returns the substring |
The convenience function for extracting the data by name returns the substring |
| 1320 |
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 |
| 1321 |
matched. This saves searching to find which numbered subpattern it was. If you |
matched. This saves searching to find which numbered subpattern it was. |
| 1322 |
make a reference to a non-unique named subpattern from elsewhere in the |
.P |
| 1323 |
pattern, the one that corresponds to the lowest number is used. For further |
If you make a back reference to a non-unique named subpattern from elsewhere in |
| 1324 |
details of the interfaces for handling named subpatterns, see the |
the pattern, the one that corresponds to the first occurrence of the name is |
| 1325 |
|
used. In the absence of duplicate numbers (see the previous section) this is |
| 1326 |
|
the one with the lowest number. If you use a named reference in a condition |
| 1327 |
|
test (see the |
| 1328 |
|
.\" |
| 1329 |
|
.\" HTML <a href="#conditions"> |
| 1330 |
|
.\" </a> |
| 1331 |
|
section about conditions |
| 1332 |
|
.\" |
| 1333 |
|
below), either to check whether a subpattern has matched, or to check for |
| 1334 |
|
recursion, all subpatterns with the same name are tested. If the condition is |
| 1335 |
|
true for any one of them, the overall condition is true. This is the same |
| 1336 |
|
behaviour as testing by number. For further details of the interfaces for |
| 1337 |
|
handling named subpatterns, see the |
| 1338 |
.\" HREF |
.\" HREF |
| 1339 |
\fBpcreapi\fP |
\fBpcreapi\fP |
| 1340 |
.\" |
.\" |
| 1341 |
documentation. |
documentation. |
| 1342 |
.P |
.P |
| 1343 |
\fBWarning:\fP You cannot use different names to distinguish between two |
\fBWarning:\fP You cannot use different names to distinguish between two |
| 1344 |
subpatterns with the same number (see the previous section) because PCRE uses |
subpatterns with the same number because PCRE uses only the numbers when |
| 1345 |
only the numbers when matching. |
matching. For this reason, an error is given at compile time if different names |
| 1346 |
|
are given to subpatterns with the same number. However, you can give the same |
| 1347 |
|
name to subpatterns with the same number, even when PCRE_DUPNAMES is not set. |
| 1348 |
. |
. |
| 1349 |
. |
. |
| 1350 |
.SH REPETITION |
.SH REPETITION |
| 1362 |
a character class |
a character class |
| 1363 |
a back reference (see next section) |
a back reference (see next section) |
| 1364 |
a parenthesized subpattern (unless it is an assertion) |
a parenthesized subpattern (unless it is an assertion) |
| 1365 |
|
a recursive or "subroutine" call to a subpattern |
| 1366 |
.sp |
.sp |
| 1367 |
The general repetition quantifier specifies a minimum and maximum number of |
The general repetition quantifier specifies a minimum and maximum number of |
| 1368 |
permitted matches, by giving the two numbers in curly brackets (braces), |
permitted matches, by giving the two numbers in curly brackets (braces), |
| 1473 |
alternatively using ^ to indicate anchoring explicitly. |
alternatively using ^ to indicate anchoring explicitly. |
| 1474 |
.P |
.P |
| 1475 |
However, there is one situation where the optimization cannot be used. When .* |
However, there is one situation where the optimization cannot be used. When .* |
| 1476 |
is inside capturing parentheses that are the subject of a backreference |
is inside capturing parentheses that are the subject of a back reference |
| 1477 |
elsewhere in the pattern, a match at the start may fail where a later one |
elsewhere in the pattern, a match at the start may fail where a later one |
| 1478 |
succeeds. Consider, for example: |
succeeds. Consider, for example: |
| 1479 |
.sp |
.sp |
| 1684 |
.P |
.P |
| 1685 |
There may be more than one back reference to the same subpattern. If a |
There may be more than one back reference to the same subpattern. If a |
| 1686 |
subpattern has not actually been used in a particular match, any back |
subpattern has not actually been used in a particular match, any back |
| 1687 |
references to it always fail. For example, the pattern |
references to it always fail by default. For example, the pattern |
| 1688 |
.sp |
.sp |
| 1689 |
(a|(bc))\e2 |
(a|(bc))\e2 |
| 1690 |
.sp |
.sp |
| 1691 |
always fails if it starts to match "a" rather than "bc". Because there may be |
always fails if it starts to match "a" rather than "bc". However, if the |
| 1692 |
many capturing parentheses in a pattern, all digits following the backslash are |
PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back reference to an |
| 1693 |
taken as part of a potential back reference number. If the pattern continues |
unset value matches an empty string. |
| 1694 |
with a digit character, some delimiter must be used to terminate the back |
.P |
| 1695 |
reference. If the PCRE_EXTENDED option is set, this can be whitespace. |
Because there may be many capturing parentheses in a pattern, all digits |
| 1696 |
Otherwise an empty comment (see |
following a backslash are taken as part of a potential back reference number. |
| 1697 |
|
If the pattern continues with a digit character, some delimiter must be used to |
| 1698 |
|
terminate the back reference. If the PCRE_EXTENDED option is set, this can be |
| 1699 |
|
whitespace. Otherwise, the \eg{ syntax or an empty comment (see |
| 1700 |
.\" HTML <a href="#comments"> |
.\" HTML <a href="#comments"> |
| 1701 |
.\" </a> |
.\" </a> |
| 1702 |
"Comments" |
"Comments" |
| 1703 |
.\" |
.\" |
| 1704 |
below) can be used. |
below) can be used. |
| 1705 |
.P |
. |
| 1706 |
|
.SS "Recursive back references" |
| 1707 |
|
.rs |
| 1708 |
|
.sp |
| 1709 |
A back reference that occurs inside the parentheses to which it refers fails |
A back reference that occurs inside the parentheses to which it refers fails |
| 1710 |
when the subpattern is first used, so, for example, (a\e1) never matches. |
when the subpattern is first used, so, for example, (a\e1) never matches. |
| 1711 |
However, such references can be useful inside repeated subpatterns. For |
However, such references can be useful inside repeated subpatterns. For |
| 1719 |
that the first iteration does not need to match the back reference. This can be |
that the first iteration does not need to match the back reference. This can be |
| 1720 |
done using alternation, as in the example above, or by a quantifier with a |
done using alternation, as in the example above, or by a quantifier with a |
| 1721 |
minimum of zero. |
minimum of zero. |
| 1722 |
|
.P |
| 1723 |
|
Back references of this type cause the group that they reference to be treated |
| 1724 |
|
as an |
| 1725 |
|
.\" HTML <a href="#atomicgroup"> |
| 1726 |
|
.\" </a> |
| 1727 |
|
atomic group. |
| 1728 |
|
.\" |
| 1729 |
|
Once the whole group has been matched, a subsequent matching failure cannot |
| 1730 |
|
cause backtracking into the middle of the group. |
| 1731 |
. |
. |
| 1732 |
. |
. |
| 1733 |
.\" HTML <a name="bigassertions"></a> |
.\" HTML <a name="bigassertions"></a> |
| 1781 |
If you want to force a matching failure at some point in a pattern, the most |
If you want to force a matching failure at some point in a pattern, the most |
| 1782 |
convenient way to do it is with (?!) because an empty string always matches, so |
convenient way to do it is with (?!) because an empty string always matches, so |
| 1783 |
an assertion that requires there not to be an empty string must always fail. |
an assertion that requires there not to be an empty string must always fail. |
| 1784 |
|
The Perl 5.10 backtracking control verb (*FAIL) or (*F) is essentially a |
| 1785 |
|
synonym for (?!). |
| 1786 |
. |
. |
| 1787 |
. |
. |
| 1788 |
.\" HTML <a name="lookbehind"></a> |
.\" HTML <a name="lookbehind"></a> |
| 1807 |
.sp |
.sp |
| 1808 |
causes an error at compile time. Branches that match different length strings |
causes an error at compile time. Branches that match different length strings |
| 1809 |
are permitted only at the top level of a lookbehind assertion. This is an |
are permitted only at the top level of a lookbehind assertion. This is an |
| 1810 |
extension compared with Perl (at least for 5.8), which requires all branches to |
extension compared with Perl (5.8 and 5.10), which requires all branches to |
| 1811 |
match the same length of string. An assertion such as |
match the same length of string. An assertion such as |
| 1812 |
.sp |
.sp |
| 1813 |
(?<=ab(c|de)) |
(?<=ab(c|de)) |
| 1814 |
.sp |
.sp |
| 1815 |
is not permitted, because its single top-level branch can match two different |
is not permitted, because its single top-level branch can match two different |
| 1816 |
lengths, but it is acceptable if rewritten to use two top-level branches: |
lengths, but it is acceptable to PCRE if rewritten to use two top-level |
| 1817 |
|
branches: |
| 1818 |
.sp |
.sp |
| 1819 |
(?<=abc|abde) |
(?<=abc|abde) |
| 1820 |
.sp |
.sp |
| 1823 |
.\" </a> |
.\" </a> |
| 1824 |
(see above) |
(see above) |
| 1825 |
.\" |
.\" |
| 1826 |
can be used instead of a lookbehind assertion; this is not restricted to a |
can be used instead of a lookbehind assertion to get round the fixed-length |
| 1827 |
fixed-length. |
restriction. |
| 1828 |
.P |
.P |
| 1829 |
The implementation of lookbehind assertions is, for each alternative, to |
The implementation of lookbehind assertions is, for each alternative, to |
| 1830 |
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 |
| 1836 |
the length of the lookbehind. The \eX and \eR escapes, which can match |
the length of the lookbehind. The \eX and \eR escapes, which can match |
| 1837 |
different numbers of bytes, are also not permitted. |
different numbers of bytes, are also not permitted. |
| 1838 |
.P |
.P |
| 1839 |
|
.\" HTML <a href="#subpatternsassubroutines"> |
| 1840 |
|
.\" </a> |
| 1841 |
|
"Subroutine" |
| 1842 |
|
.\" |
| 1843 |
|
calls (see below) such as (?2) or (?&X) are permitted in lookbehinds, as long |
| 1844 |
|
as the subpattern matches a fixed-length string. |
| 1845 |
|
.\" HTML <a href="#recursion"> |
| 1846 |
|
.\" </a> |
| 1847 |
|
Recursion, |
| 1848 |
|
.\" |
| 1849 |
|
however, is not supported. |
| 1850 |
|
.P |
| 1851 |
Possessive quantifiers can be used in conjunction with lookbehind assertions to |
Possessive quantifiers can be used in conjunction with lookbehind assertions to |
| 1852 |
specify efficient matching at the end of the subject string. Consider a simple |
specify efficient matching of fixed-length strings at the end of subject |
| 1853 |
pattern such as |
strings. Consider a simple pattern such as |
| 1854 |
.sp |
.sp |
| 1855 |
abcd$ |
abcd$ |
| 1856 |
.sp |
.sp |
| 1914 |
.sp |
.sp |
| 1915 |
It is possible to cause the matching process to obey a subpattern |
It is possible to cause the matching process to obey a subpattern |
| 1916 |
conditionally or to choose between two alternative subpatterns, depending on |
conditionally or to choose between two alternative subpatterns, depending on |
| 1917 |
the result of an assertion, or whether a previous capturing subpattern matched |
the result of an assertion, or whether a specific capturing subpattern has |
| 1918 |
or not. The two possible forms of conditional subpattern are |
already been matched. The two possible forms of conditional subpattern are: |
| 1919 |
.sp |
.sp |
| 1920 |
(?(condition)yes-pattern) |
(?(condition)yes-pattern) |
| 1921 |
(?(condition)yes-pattern|no-pattern) |
(?(condition)yes-pattern|no-pattern) |
| 1931 |
.rs |
.rs |
| 1932 |
.sp |
.sp |
| 1933 |
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 |
| 1934 |
condition is true if the capturing subpattern of that number has previously |
condition is true if a capturing subpattern of that number has previously |
| 1935 |
matched. An alternative notation is to precede the digits with a plus or minus |
matched. If there is more than one capturing subpattern with the same number |
| 1936 |
sign. In this case, the subpattern number is relative rather than absolute. |
(see the earlier |
| 1937 |
The most recently opened parentheses can be referenced by (?(-1), the next most |
.\" |
| 1938 |
recent by (?(-2), and so on. In looping constructs it can also make sense to |
.\" HTML <a href="#recursion"> |
| 1939 |
refer to subsequent groups with constructs such as (?(+2). |
.\" </a> |
| 1940 |
|
section about duplicate subpattern numbers), |
| 1941 |
|
.\" |
| 1942 |
|
the condition is true if any of them have been set. An alternative notation is |
| 1943 |
|
to precede the digits with a plus or minus sign. In this case, the subpattern |
| 1944 |
|
number is relative rather than absolute. The most recently opened parentheses |
| 1945 |
|
can be referenced by (?(-1), the next most recent by (?(-2), and so on. In |
| 1946 |
|
looping constructs it can also make sense to refer to subsequent groups with |
| 1947 |
|
constructs such as (?(+2). |
| 1948 |
.P |
.P |
| 1949 |
Consider the following pattern, which contains non-significant white space to |
Consider the following pattern, which contains non-significant white space to |
| 1950 |
make it more readable (assume the PCRE_EXTENDED option) and to divide it into |
make it more readable (assume the PCRE_EXTENDED option) and to divide it into |
| 1985 |
.sp |
.sp |
| 1986 |
(?<OPEN> \e( )? [^()]+ (?(<OPEN>) \e) ) |
(?<OPEN> \e( )? [^()]+ (?(<OPEN>) \e) ) |
| 1987 |
.sp |
.sp |
| 1988 |
|
If the name used in a condition of this kind is a duplicate, the test is |
| 1989 |
|
applied to all subpatterns of the same name, and is true if any one of them has |
| 1990 |
|
matched. |
| 1991 |
. |
. |
| 1992 |
.SS "Checking for pattern recursion" |
.SS "Checking for pattern recursion" |
| 1993 |
.rs |
.rs |
| 1999 |
.sp |
.sp |
| 2000 |
(?(R3)...) or (?(R&name)...) |
(?(R3)...) or (?(R&name)...) |
| 2001 |
.sp |
.sp |
| 2002 |
the condition is true if the most recent recursion is into the subpattern whose |
the condition is true if the most recent recursion is into a subpattern whose |
| 2003 |
number or name is given. This condition does not check the entire recursion |
number or name is given. This condition does not check the entire recursion |
| 2004 |
stack. |
stack. If the name used in a condition of this kind is a duplicate, the test is |
| 2005 |
|
applied to all subpatterns of the same name, and is true if any one of them is |
| 2006 |
|
the most recent recursion. |
| 2007 |
.P |
.P |
| 2008 |
At "top level", all these recursion test conditions are false. Recursive |
At "top level", all these recursion test conditions are false. |
| 2009 |
patterns are described below. |
.\" HTML <a href="#recursion"> |
| 2010 |
|
.\" </a> |
| 2011 |
|
The syntax for recursive patterns |
| 2012 |
|
.\" |
| 2013 |
|
is described below. |
| 2014 |
. |
. |
| 2015 |
.SS "Defining subpatterns for use by reference only" |
.SS "Defining subpatterns for use by reference only" |
| 2016 |
.rs |
.rs |
| 2019 |
name DEFINE, the condition is always false. In this case, there may be only one |
name DEFINE, the condition is always false. In this case, there may be only one |
| 2020 |
alternative in the subpattern. It is always skipped if control reaches this |
alternative in the subpattern. It is always skipped if control reaches this |
| 2021 |
point in the pattern; the idea of DEFINE is that it can be used to define |
point in the pattern; the idea of DEFINE is that it can be used to define |
| 2022 |
"subroutines" that can be referenced from elsewhere. (The use of "subroutines" |
"subroutines" that can be referenced from elsewhere. (The use of |
| 2023 |
|
.\" HTML <a href="#subpatternsassubroutines"> |
| 2024 |
|
.\" </a> |
| 2025 |
|
"subroutines" |
| 2026 |
|
.\" |
| 2027 |
is described below.) For example, a pattern to match an IPv4 address could be |
is described below.) For example, a pattern to match an IPv4 address could be |
| 2028 |
written like this (ignore whitespace and line breaks): |
written like this (ignore whitespace and line breaks): |
| 2029 |
.sp |
.sp |
| 2033 |
The first part of the pattern is a DEFINE group inside which a another group |
The first part of the pattern is a DEFINE group inside which a another group |
| 2034 |
named "byte" is defined. This matches an individual component of an IPv4 |
named "byte" is defined. This matches an individual component of an IPv4 |
| 2035 |
address (a number less than 256). When matching takes place, this part of the |
address (a number less than 256). When matching takes place, this part of the |
| 2036 |
pattern is skipped because DEFINE acts like a false condition. |
pattern is skipped because DEFINE acts like a false condition. The rest of the |
| 2037 |
.P |
pattern uses references to the named group to match the four dot-separated |
| 2038 |
The rest of the pattern uses references to the named group to match the four |
components of an IPv4 address, insisting on a word boundary at each end. |
|
dot-separated components of an IPv4 address, insisting on a word boundary at |
|
|
each end. |
|
| 2039 |
. |
. |
| 2040 |
.SS "Assertion conditions" |
.SS "Assertion conditions" |
| 2041 |
.rs |
.rs |
| 2092 |
Obviously, PCRE cannot support the interpolation of Perl code. Instead, it |
Obviously, PCRE cannot support the interpolation of Perl code. Instead, it |
| 2093 |
supports special syntax for recursion of the entire pattern, and also for |
supports special syntax for recursion of the entire pattern, and also for |
| 2094 |
individual subpattern recursion. After its introduction in PCRE and Python, |
individual subpattern recursion. After its introduction in PCRE and Python, |
| 2095 |
this kind of recursion was introduced into Perl at release 5.10. |
this kind of recursion was subsequently introduced into Perl at release 5.10. |
| 2096 |
.P |
.P |
| 2097 |
A special item that consists of (? followed by a number greater than zero and a |
A special item that consists of (? followed by a number greater than zero and a |
| 2098 |
closing parenthesis is a recursive call of the subpattern of the given number, |
closing parenthesis is a recursive call of the subpattern of the given number, |
| 2099 |
provided that it occurs inside that subpattern. (If not, it is a "subroutine" |
provided that it occurs inside that subpattern. (If not, it is a |
| 2100 |
|
.\" HTML <a href="#subpatternsassubroutines"> |
| 2101 |
|
.\" </a> |
| 2102 |
|
"subroutine" |
| 2103 |
|
.\" |
| 2104 |
call, which is described in the next section.) The special item (?R) or (?0) is |
call, which is described in the next section.) The special item (?R) or (?0) is |
| 2105 |
a recursive call of the entire regular expression. |
a recursive call of the entire regular expression. |
| 2106 |
.P |
.P |
|
In PCRE (like Python, but unlike Perl), a recursive subpattern call is always |
|
|
treated as an atomic group. That is, once it has matched some of the subject |
|
|
string, it is never re-entered, even if it contains untried alternatives and |
|
|
there is a subsequent matching failure. |
|
|
.P |
|
| 2107 |
This PCRE pattern solves the nested parentheses problem (assume the |
This PCRE pattern solves the nested parentheses problem (assume the |
| 2108 |
PCRE_EXTENDED option is set so that white space is ignored): |
PCRE_EXTENDED option is set so that white space is ignored): |
| 2109 |
.sp |
.sp |
| 2110 |
\e( ( (?>[^()]+) | (?R) )* \e) |
\e( ( [^()]++ | (?R) )* \e) |
| 2111 |
.sp |
.sp |
| 2112 |
First it matches an opening parenthesis. Then it matches any number of |
First it matches an opening parenthesis. Then it matches any number of |
| 2113 |
substrings which can either be a sequence of non-parentheses, or a recursive |
substrings which can either be a sequence of non-parentheses, or a recursive |
| 2114 |
match of the pattern itself (that is, a correctly parenthesized substring). |
match of the pattern itself (that is, a correctly parenthesized substring). |
| 2115 |
Finally there is a closing parenthesis. |
Finally there is a closing parenthesis. Note the use of a possessive quantifier |
| 2116 |
|
to avoid backtracking into sequences of non-parentheses. |
| 2117 |
.P |
.P |
| 2118 |
If this were part of a larger pattern, you would not want to recurse the entire |
If this were part of a larger pattern, you would not want to recurse the entire |
| 2119 |
pattern, so instead you could use this: |
pattern, so instead you could use this: |
| 2120 |
.sp |
.sp |
| 2121 |
( \e( ( (?>[^()]+) | (?1) )* \e) ) |
( \e( ( [^()]++ | (?1) )* \e) ) |
| 2122 |
.sp |
.sp |
| 2123 |
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 |
| 2124 |
them instead of the whole pattern. |
them instead of the whole pattern. |
| 2125 |
.P |
.P |
| 2126 |
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 |
| 2127 |
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). |
| 2128 |
Instead of (?1) in the pattern above you can write (?-2) to refer to the second |
Instead of (?1) in the pattern above you can write (?-2) to refer to the second |
| 2129 |
most recently opened parentheses preceding the recursion. In other words, a |
most recently opened parentheses preceding the recursion. In other words, a |
| 2130 |
negative number counts capturing parentheses leftwards from the point at which |
negative number counts capturing parentheses leftwards from the point at which |
| 2133 |
It is also possible to refer to subsequently opened parentheses, by writing |
It is also possible to refer to subsequently opened parentheses, by writing |
| 2134 |
references such as (?+2). However, these cannot be recursive because the |
references such as (?+2). However, these cannot be recursive because the |
| 2135 |
reference is not inside the parentheses that are referenced. They are always |
reference is not inside the parentheses that are referenced. They are always |
| 2136 |
"subroutine" calls, as described in the next section. |
.\" HTML <a href="#subpatternsassubroutines"> |
| 2137 |
|
.\" </a> |
| 2138 |
|
"subroutine" |
| 2139 |
|
.\" |
| 2140 |
|
calls, as described in the next section. |
| 2141 |
.P |
.P |
| 2142 |
An alternative approach is to use named parentheses instead. The Perl syntax |
An alternative approach is to use named parentheses instead. The Perl syntax |
| 2143 |
for this is (?&name); PCRE's earlier syntax (?P>name) is also supported. We |
for this is (?&name); PCRE's earlier syntax (?P>name) is also supported. We |
| 2144 |
could rewrite the above example as follows: |
could rewrite the above example as follows: |
| 2145 |
.sp |
.sp |
| 2146 |
(?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) ) |
(?<pn> \e( ( [^()]++ | (?&pn) )* \e) ) |
| 2147 |
.sp |
.sp |
| 2148 |
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 |
| 2149 |
used. |
used. |
| 2150 |
.P |
.P |
| 2151 |
This particular example pattern that we have been looking at contains nested |
This particular example pattern that we have been looking at contains nested |
| 2152 |
unlimited repeats, and so the use of atomic grouping for matching strings of |
unlimited repeats, and so the use of a possessive quantifier for matching |
| 2153 |
non-parentheses is important when applying the pattern to strings that do not |
strings of non-parentheses is important when applying the pattern to strings |
| 2154 |
match. For example, when this pattern is applied to |
that do not match. For example, when this pattern is applied to |
| 2155 |
.sp |
.sp |
| 2156 |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
| 2157 |
.sp |
.sp |
| 2158 |
it yields "no match" quickly. However, if atomic grouping is not used, |
it yields "no match" quickly. However, if a possessive quantifier is not used, |
| 2159 |
the match runs for a very long time indeed because there are so many different |
the match runs for a very long time indeed because there are so many different |
| 2160 |
ways the + and * repeats can carve up the subject, and all have to be tested |
ways the + and * repeats can carve up the subject, and all have to be tested |
| 2161 |
before failure can be reported. |
before failure can be reported. |
| 2162 |
.P |
.P |
| 2163 |
At the end of a match, the values set for any capturing subpatterns are those |
At the end of a match, the values of capturing parentheses are those from |
| 2164 |
from the outermost level of the recursion at which the subpattern value is set. |
the outermost level. If you want to obtain intermediate values, a callout |
| 2165 |
If you want to obtain intermediate values, a callout function can be used (see |
function can be used (see below and the |
|
below and the |
|
| 2166 |
.\" HREF |
.\" HREF |
| 2167 |
\fBpcrecallout\fP |
\fBpcrecallout\fP |
| 2168 |
.\" |
.\" |
| 2170 |
.sp |
.sp |
| 2171 |
(ab(cd)ef) |
(ab(cd)ef) |
| 2172 |
.sp |
.sp |
| 2173 |
the value for the capturing parentheses is "ef", which is the last value taken |
the value for the inner capturing parentheses (numbered 2) is "ef", which is |
| 2174 |
on at the top level. If additional parentheses are added, giving |
the last value taken on at the top level. If a capturing subpattern is not |
| 2175 |
.sp |
matched at the top level, its final value is unset, even if it is (temporarily) |
| 2176 |
\e( ( ( (?>[^()]+) | (?R) )* ) \e) |
set at a deeper level. |
| 2177 |
^ ^ |
.P |
| 2178 |
^ ^ |
If there are more than 15 capturing parentheses in a pattern, PCRE has to |
| 2179 |
.sp |
obtain extra memory to store data during a recursion, which it does by using |
| 2180 |
the string they capture is "ab(cd)ef", the contents of the top level |
\fBpcre_malloc\fP, freeing it via \fBpcre_free\fP afterwards. If no memory can |
| 2181 |
parentheses. If there are more than 15 capturing parentheses in a pattern, PCRE |
be obtained, the match fails with the PCRE_ERROR_NOMEMORY error. |
|
has to obtain extra memory to store data during a recursion, which it does by |
|
|
using \fBpcre_malloc\fP, freeing it via \fBpcre_free\fP afterwards. If no |
|
|
memory can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error. |
|
| 2182 |
.P |
.P |
| 2183 |
Do not confuse the (?R) item with the condition (R), which tests for recursion. |
Do not confuse the (?R) item with the condition (R), which tests for recursion. |
| 2184 |
Consider this pattern, which matches text in angle brackets, allowing for |
Consider this pattern, which matches text in angle brackets, allowing for |
| 2192 |
is the actual recursive call. |
is the actual recursive call. |
| 2193 |
. |
. |
| 2194 |
. |
. |
| 2195 |
|
.\" HTML <a name="recursiondifference"></a> |
| 2196 |
|
.SS "Recursion difference from Perl" |
| 2197 |
|
.rs |
| 2198 |
|
.sp |
| 2199 |
|
In PCRE (like Python, but unlike Perl), a recursive subpattern call is always |
| 2200 |
|
treated as an atomic group. That is, once it has matched some of the subject |
| 2201 |
|
string, it is never re-entered, even if it contains untried alternatives and |
| 2202 |
|
there is a subsequent matching failure. This can be illustrated by the |
| 2203 |
|
following pattern, which purports to match a palindromic string that contains |
| 2204 |
|
an odd number of characters (for example, "a", "aba", "abcba", "abcdcba"): |
| 2205 |
|
.sp |
| 2206 |
|
^(.|(.)(?1)\e2)$ |
| 2207 |
|
.sp |
| 2208 |
|
The idea is that it either matches a single character, or two identical |
| 2209 |
|
characters surrounding a sub-palindrome. In Perl, this pattern works; in PCRE |
| 2210 |
|
it does not if the pattern is longer than three characters. Consider the |
| 2211 |
|
subject string "abcba": |
| 2212 |
|
.P |
| 2213 |
|
At the top level, the first character is matched, but as it is not at the end |
| 2214 |
|
of the string, the first alternative fails; the second alternative is taken |
| 2215 |
|
and the recursion kicks in. The recursive call to subpattern 1 successfully |
| 2216 |
|
matches the next character ("b"). (Note that the beginning and end of line |
| 2217 |
|
tests are not part of the recursion). |
| 2218 |
|
.P |
| 2219 |
|
Back at the top level, the next character ("c") is compared with what |
| 2220 |
|
subpattern 2 matched, which was "a". This fails. Because the recursion is |
| 2221 |
|
treated as an atomic group, there are now no backtracking points, and so the |
| 2222 |
|
entire match fails. (Perl is able, at this point, to re-enter the recursion and |
| 2223 |
|
try the second alternative.) However, if the pattern is written with the |
| 2224 |
|
alternatives in the other order, things are different: |
| 2225 |
|
.sp |
| 2226 |
|
^((.)(?1)\e2|.)$ |
| 2227 |
|
.sp |
| 2228 |
|
This time, the recursing alternative is tried first, and continues to recurse |
| 2229 |
|
until it runs out of characters, at which point the recursion fails. But this |
| 2230 |
|
time we do have another alternative to try at the higher level. That is the big |
| 2231 |
|
difference: in the previous case the remaining alternative is at a deeper |
| 2232 |
|
recursion level, which PCRE cannot use. |
| 2233 |
|
.P |
| 2234 |
|
To change the pattern so that matches all palindromic strings, not just those |
| 2235 |
|
with an odd number of characters, it is tempting to change the pattern to this: |
| 2236 |
|
.sp |
| 2237 |
|
^((.)(?1)\e2|.?)$ |
| 2238 |
|
.sp |
| 2239 |
|
Again, this works in Perl, but not in PCRE, and for the same reason. When a |
| 2240 |
|
deeper recursion has matched a single character, it cannot be entered again in |
| 2241 |
|
order to match an empty string. The solution is to separate the two cases, and |
| 2242 |
|
write out the odd and even cases as alternatives at the higher level: |
| 2243 |
|
.sp |
| 2244 |
|
^(?:((.)(?1)\e2|)|((.)(?3)\e4|.)) |
| 2245 |
|
.sp |
| 2246 |
|
If you want to match typical palindromic phrases, the pattern has to ignore all |
| 2247 |
|
non-word characters, which can be done like this: |
| 2248 |
|
.sp |
| 2249 |
|
^\eW*+(?:((.)\eW*+(?1)\eW*+\e2|)|((.)\eW*+(?3)\eW*+\e4|\eW*+.\eW*+))\eW*+$ |
| 2250 |
|
.sp |
| 2251 |
|
If run with the PCRE_CASELESS option, this pattern matches phrases such as "A |
| 2252 |
|
man, a plan, a canal: Panama!" and it works well in both PCRE and Perl. Note |
| 2253 |
|
the use of the possessive quantifier *+ to avoid backtracking into sequences of |
| 2254 |
|
non-word characters. Without this, PCRE takes a great deal longer (ten times or |
| 2255 |
|
more) to match typical phrases, and Perl takes so long that you think it has |
| 2256 |
|
gone into a loop. |
| 2257 |
|
.P |
| 2258 |
|
\fBWARNING\fP: The palindrome-matching patterns above work only if the subject |
| 2259 |
|
string does not start with a palindrome that is shorter than the entire string. |
| 2260 |
|
For example, although "abcba" is correctly matched, if the subject is "ababa", |
| 2261 |
|
PCRE finds the palindrome "aba" at the start, then fails at top level because |
| 2262 |
|
the end of the string does not follow. Once again, it cannot jump back into the |
| 2263 |
|
recursion to try other alternatives, so the entire match fails. |
| 2264 |
|
. |
| 2265 |
|
. |
| 2266 |
.\" HTML <a name="subpatternsassubroutines"></a> |
.\" HTML <a name="subpatternsassubroutines"></a> |
| 2267 |
.SH "SUBPATTERNS AS SUBROUTINES" |
.SH "SUBPATTERNS AS SUBROUTINES" |
| 2268 |
.rs |
.rs |
| 2289 |
is used, it does match "sense and responsibility" as well as the other two |
is used, it does match "sense and responsibility" as well as the other two |
| 2290 |
strings. Another example is given in the discussion of DEFINE above. |
strings. Another example is given in the discussion of DEFINE above. |
| 2291 |
.P |
.P |
| 2292 |
Like recursive subpatterns, a "subroutine" call is always treated as an atomic |
Like recursive subpatterns, a subroutine call is always treated as an atomic |
| 2293 |
group. That is, once it has matched some of the subject string, it is never |
group. That is, once it has matched some of the subject string, it is never |
| 2294 |
re-entered, even if it contains untried alternatives and there is a subsequent |
re-entered, even if it contains untried alternatives and there is a subsequent |
| 2295 |
matching failure. |
matching failure. Any capturing parentheses that are set during the subroutine |
| 2296 |
|
call revert to their previous values afterwards. |
| 2297 |
.P |
.P |
| 2298 |
When a subpattern is used as a subroutine, processing options such as |
When a subpattern is used as a subroutine, processing options such as |
| 2299 |
case-independence are fixed when the subpattern is defined. They cannot be |
case-independence are fixed when the subpattern is defined. They cannot be |
| 2362 |
documentation. |
documentation. |
| 2363 |
. |
. |
| 2364 |
. |
. |
| 2365 |
|
.\" HTML <a name="backtrackcontrol"></a> |
| 2366 |
.SH "BACKTRACKING CONTROL" |
.SH "BACKTRACKING CONTROL" |
| 2367 |
.rs |
.rs |
| 2368 |
.sp |
.sp |
| 2378 |
failing negative assertion, they cause an error if encountered by |
failing negative assertion, they cause an error if encountered by |
| 2379 |
\fBpcre_dfa_exec()\fP. |
\fBpcre_dfa_exec()\fP. |
| 2380 |
.P |
.P |
| 2381 |
If any of these verbs are used in an assertion subpattern, their effect is |
If any of these verbs are used in an assertion or subroutine subpattern |
| 2382 |
confined to that subpattern; it does not extend to the surrounding pattern. |
(including recursive subpatterns), their effect is confined to that subpattern; |
| 2383 |
Note that assertion subpatterns are processed as anchored at the point where |
it does not extend to the surrounding pattern. Note that such subpatterns are |
| 2384 |
they are tested. |
processed as anchored at the point where they are tested. |
| 2385 |
.P |
.P |
| 2386 |
The new verbs make use of what was previously invalid syntax: an opening |
The new verbs make use of what was previously invalid syntax: an opening |
| 2387 |
parenthesis followed by an asterisk. In Perl, they are generally of the form |
parenthesis followed by an asterisk. They are generally of the form |
| 2388 |
(*VERB:ARG) but PCRE does not support the use of arguments, so its general |
(*VERB) or (*VERB:NAME). Some may take either form, with differing behaviour, |
| 2389 |
form is just (*VERB). Any number of these verbs may occur in a pattern. There |
depending on whether or not an argument is present. An name is a sequence of |
| 2390 |
are two kinds: |
letters, digits, and underscores. If the name is empty, that is, if the closing |
| 2391 |
|
parenthesis immediately follows the colon, the effect is as if the colon were |
| 2392 |
|
not there. Any number of these verbs may occur in a pattern. |
| 2393 |
|
.P |
| 2394 |
|
PCRE contains some optimizations that are used to speed up matching by running |
| 2395 |
|
some checks at the start of each match attempt. For example, it may know the |
| 2396 |
|
minimum length of matching subject, or that a particular character must be |
| 2397 |
|
present. When one of these optimizations suppresses the running of a match, any |
| 2398 |
|
included backtracking verbs will not, of course, be processed. You can suppress |
| 2399 |
|
the start-of-match optimizations by setting the PCRE_NO_START_OPTIMIZE option |
| 2400 |
|
when calling \fBpcre_exec()\fP. |
| 2401 |
|
. |
| 2402 |
. |
. |
| 2403 |
.SS "Verbs that act immediately" |
.SS "Verbs that act immediately" |
| 2404 |
.rs |
.rs |
| 2405 |
.sp |
.sp |
| 2406 |
The following verbs act as soon as they are encountered: |
The following verbs act as soon as they are encountered. They may not be |
| 2407 |
|
followed by a name. |
| 2408 |
.sp |
.sp |
| 2409 |
(*ACCEPT) |
(*ACCEPT) |
| 2410 |
.sp |
.sp |
| 2411 |
This verb causes the match to end successfully, skipping the remainder of the |
This verb causes the match to end successfully, skipping the remainder of the |
| 2412 |
pattern. When inside a recursion, only the innermost pattern is ended |
pattern. When inside a recursion, only the innermost pattern is ended |
| 2413 |
immediately. PCRE differs from Perl in what happens if the (*ACCEPT) is inside |
immediately. If (*ACCEPT) is inside capturing parentheses, the data so far is |
| 2414 |
capturing parentheses. In Perl, the data so far is captured: in PCRE no data is |
captured. (This feature was added to PCRE at release 8.00.) For example: |
|
captured. For example: |
|
| 2415 |
.sp |
.sp |
| 2416 |
A(A|B(*ACCEPT)|C)D |
A((?:A|B(*ACCEPT)|C)D) |
| 2417 |
.sp |
.sp |
| 2418 |
This matches "AB", "AAD", or "ACD", but when it matches "AB", no data is |
This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is captured by |
| 2419 |
captured. |
the outer parentheses. |
| 2420 |
.sp |
.sp |
| 2421 |
(*FAIL) or (*F) |
(*FAIL) or (*F) |
| 2422 |
.sp |
.sp |
| 2431 |
A match with the string "aaaa" always fails, but the callout is taken before |
A match with the string "aaaa" always fails, but the callout is taken before |
| 2432 |
each backtrack happens (in this example, 10 times). |
each backtrack happens (in this example, 10 times). |
| 2433 |
. |
. |
| 2434 |
|
. |
| 2435 |
|
.SS "Recording which path was taken" |
| 2436 |
|
.rs |
| 2437 |
|
.sp |
| 2438 |
|
There is one verb whose main purpose is to track how a match was arrived at, |
| 2439 |
|
though it also has a secondary use in conjunction with advancing the match |
| 2440 |
|
starting point (see (*SKIP) below). |
| 2441 |
|
.sp |
| 2442 |
|
(*MARK:NAME) or (*:NAME) |
| 2443 |
|
.sp |
| 2444 |
|
A name is always required with this verb. There may be as many instances of |
| 2445 |
|
(*MARK) as you like in a pattern, and their names do not have to be unique. |
| 2446 |
|
.P |
| 2447 |
|
When a match succeeds, the name of the last-encountered (*MARK) is passed back |
| 2448 |
|
to the caller via the \fIpcre_extra\fP data structure, as described in the |
| 2449 |
|
.\" HTML <a href="pcreapi.html#extradata"> |
| 2450 |
|
.\" </a> |
| 2451 |
|
section on \fIpcre_extra\fP |
| 2452 |
|
.\" |
| 2453 |
|
in the |
| 2454 |
|
.\" HREF |
| 2455 |
|
\fBpcreapi\fP |
| 2456 |
|
.\" |
| 2457 |
|
documentation. No data is returned for a partial match. Here is an example of |
| 2458 |
|
\fBpcretest\fP output, where the /K modifier requests the retrieval and |
| 2459 |
|
outputting of (*MARK) data: |
| 2460 |
|
.sp |
| 2461 |
|
/X(*MARK:A)Y|X(*MARK:B)Z/K |
| 2462 |
|
XY |
| 2463 |
|
0: XY |
| 2464 |
|
MK: A |
| 2465 |
|
XZ |
| 2466 |
|
0: XZ |
| 2467 |
|
MK: B |
| 2468 |
|
.sp |
| 2469 |
|
The (*MARK) name is tagged with "MK:" in this output, and in this example it |
| 2470 |
|
indicates which of the two alternatives matched. This is a more efficient way |
| 2471 |
|
of obtaining this information than putting each alternative in its own |
| 2472 |
|
capturing parentheses. |
| 2473 |
|
.P |
| 2474 |
|
A name may also be returned after a failed match if the final path through the |
| 2475 |
|
pattern involves (*MARK). However, unless (*MARK) used in conjunction with |
| 2476 |
|
(*COMMIT), this is unlikely to happen for an unanchored pattern because, as the |
| 2477 |
|
starting point for matching is advanced, the final check is often with an empty |
| 2478 |
|
string, causing a failure before (*MARK) is reached. For example: |
| 2479 |
|
.sp |
| 2480 |
|
/X(*MARK:A)Y|X(*MARK:B)Z/K |
| 2481 |
|
XP |
| 2482 |
|
No match |
| 2483 |
|
.sp |
| 2484 |
|
There are three potential starting points for this match (starting with X, |
| 2485 |
|
starting with P, and with an empty string). If the pattern is anchored, the |
| 2486 |
|
result is different: |
| 2487 |
|
.sp |
| 2488 |
|
/^X(*MARK:A)Y|^X(*MARK:B)Z/K |
| 2489 |
|
XP |
| 2490 |
|
No match, mark = B |
| 2491 |
|
.sp |
| 2492 |
|
PCRE's start-of-match optimizations can also interfere with this. For example, |
| 2493 |
|
if, as a result of a call to \fBpcre_study()\fP, it knows the minimum |
| 2494 |
|
subject length for a match, a shorter subject will not be scanned at all. |
| 2495 |
|
.P |
| 2496 |
|
Note that similar anomalies (though different in detail) exist in Perl, no |
| 2497 |
|
doubt for the same reasons. The use of (*MARK) data after a failed match of an |
| 2498 |
|
unanchored pattern is not recommended, unless (*COMMIT) is involved. |
| 2499 |
|
. |
| 2500 |
|
. |
| 2501 |
.SS "Verbs that act after backtracking" |
.SS "Verbs that act after backtracking" |
| 2502 |
.rs |
.rs |
| 2503 |
.sp |
.sp |
| 2504 |
The following verbs do nothing when they are encountered. Matching continues |
The following verbs do nothing when they are encountered. Matching continues |
| 2505 |
with what follows, but if there is no subsequent match, a failure is forced. |
with what follows, but if there is no subsequent match, causing a backtrack to |
| 2506 |
The verbs differ in exactly what kind of failure occurs. |
the verb, a failure is forced. That is, backtracking cannot pass to the left of |
| 2507 |
|
the verb. However, when one of these verbs appears inside an atomic group, its |
| 2508 |
|
effect is confined to that group, because once the group has been matched, |
| 2509 |
|
there is never any backtracking into it. In this situation, backtracking can |
| 2510 |
|
"jump back" to the left of the entire atomic group. (Remember also, as stated |
| 2511 |
|
above, that this localization also applies in subroutine calls and assertions.) |
| 2512 |
|
.P |
| 2513 |
|
These verbs differ in exactly what kind of failure occurs when backtracking |
| 2514 |
|
reaches them. |
| 2515 |
.sp |
.sp |
| 2516 |
(*COMMIT) |
(*COMMIT) |
| 2517 |
.sp |
.sp |
| 2518 |
This verb causes the whole match to fail outright if the rest of the pattern |
This verb, which may not be followed by a name, causes the whole match to fail |
| 2519 |
does not match. Even if the pattern is unanchored, no further attempts to find |
outright if the rest of the pattern does not match. Even if the pattern is |
| 2520 |
a match by advancing the start point take place. Once (*COMMIT) has been |
unanchored, no further attempts to find a match by advancing the starting point |
| 2521 |
passed, \fBpcre_exec()\fP is committed to finding a match at the current |
take place. Once (*COMMIT) has been passed, \fBpcre_exec()\fP is committed to |
| 2522 |
starting point, or not at all. For example: |
finding a match at the current starting point, or not at all. For example: |
| 2523 |
.sp |
.sp |
| 2524 |
a+(*COMMIT)b |
a+(*COMMIT)b |
| 2525 |
.sp |
.sp |
| 2526 |
This matches "xxaab" but not "aacaab". It can be thought of as a kind of |
This matches "xxaab" but not "aacaab". It can be thought of as a kind of |
| 2527 |
dynamic anchor, or "I've started, so I must finish." |
dynamic anchor, or "I've started, so I must finish." The name of the most |
| 2528 |
.sp |
recently passed (*MARK) in the path is passed back when (*COMMIT) forces a |
| 2529 |
(*PRUNE) |
match failure. |
| 2530 |
.sp |
.P |
| 2531 |
This verb causes the match to fail at the current position if the rest of the |
Note that (*COMMIT) at the start of a pattern is not the same as an anchor, |
| 2532 |
pattern does not match. If the pattern is unanchored, the normal "bumpalong" |
unless PCRE's start-of-match optimizations are turned off, as shown in this |
| 2533 |
advance to the next starting character then happens. Backtracking can occur as |
\fBpcretest\fP example: |
| 2534 |
usual to the left of (*PRUNE), or when matching to the right of (*PRUNE), but |
.sp |
| 2535 |
if there is no match to the right, backtracking cannot cross (*PRUNE). |
/(*COMMIT)abc/ |
| 2536 |
In simple cases, the use of (*PRUNE) is just an alternative to an atomic |
xyzabc |
| 2537 |
group or possessive quantifier, but there are some uses of (*PRUNE) that cannot |
0: abc |
| 2538 |
be expressed in any other way. |
xyzabc\eY |
| 2539 |
|
No match |
| 2540 |
|
.sp |
| 2541 |
|
PCRE knows that any match must start with "a", so the optimization skips along |
| 2542 |
|
the subject to "a" before running the first match attempt, which succeeds. When |
| 2543 |
|
the optimization is disabled by the \eY escape in the second subject, the match |
| 2544 |
|
starts at "x" and so the (*COMMIT) causes it to fail without trying any other |
| 2545 |
|
starting points. |
| 2546 |
|
.sp |
| 2547 |
|
(*PRUNE) or (*PRUNE:NAME) |
| 2548 |
|
.sp |
| 2549 |
|
This verb causes the match to fail at the current starting position in the |
| 2550 |
|
subject if the rest of the pattern does not match. If the pattern is |
| 2551 |
|
unanchored, the normal "bumpalong" advance to the next starting character then |
| 2552 |
|
happens. Backtracking can occur as usual to the left of (*PRUNE), before it is |
| 2553 |
|
reached, or when matching to the right of (*PRUNE), but if there is no match to |
| 2554 |
|
the right, backtracking cannot cross (*PRUNE). In simple cases, the use of |
| 2555 |
|
(*PRUNE) is just an alternative to an atomic group or possessive quantifier, |
| 2556 |
|
but there are some uses of (*PRUNE) that cannot be expressed in any other way. |
| 2557 |
|
The behaviour of (*PRUNE:NAME) is the same as (*MARK:NAME)(*PRUNE) when the |
| 2558 |
|
match fails completely; the name is passed back if this is the final attempt. |
| 2559 |
|
(*PRUNE:NAME) does not pass back a name if the match succeeds. In an anchored |
| 2560 |
|
pattern (*PRUNE) has the same effect as (*COMMIT). |
| 2561 |
.sp |
.sp |
| 2562 |
(*SKIP) |
(*SKIP) |
| 2563 |
.sp |
.sp |
| 2564 |
This verb is like (*PRUNE), except that if the pattern is unanchored, the |
This verb, when given without a name, is like (*PRUNE), except that if the |
| 2565 |
"bumpalong" advance is not to the next character, but to the position in the |
pattern is unanchored, the "bumpalong" advance is not to the next character, |
| 2566 |
subject where (*SKIP) was encountered. (*SKIP) signifies that whatever text |
but to the position in the subject where (*SKIP) was encountered. (*SKIP) |
| 2567 |
was matched leading up to it cannot be part of a successful match. Consider: |
signifies that whatever text was matched leading up to it cannot be part of a |
| 2568 |
|
successful match. Consider: |
| 2569 |
.sp |
.sp |
| 2570 |
a+(*SKIP)b |
a+(*SKIP)b |
| 2571 |
.sp |
.sp |
| 2572 |
If the subject is "aaaac...", after the first match attempt fails (starting at |
If the subject is "aaaac...", after the first match attempt fails (starting at |
| 2573 |
the first character in the string), the starting point skips on to start the |
the first character in the string), the starting point skips on to start the |
| 2574 |
next attempt at "c". Note that a possessive quantifer does not have the same |
next attempt at "c". Note that a possessive quantifer does not have the same |
| 2575 |
effect in this example; although it would suppress backtracking during the |
effect as this example; although it would suppress backtracking during the |
| 2576 |
first match attempt, the second attempt would start at the second character |
first match attempt, the second attempt would start at the second character |
| 2577 |
instead of skipping on to "c". |
instead of skipping on to "c". |
| 2578 |
.sp |
.sp |
| 2579 |
(*THEN) |
(*SKIP:NAME) |
| 2580 |
|
.sp |
| 2581 |
|
When (*SKIP) has an associated name, its behaviour is modified. If the |
| 2582 |
|
following pattern fails to match, the previous path through the pattern is |
| 2583 |
|
searched for the most recent (*MARK) that has the same name. If one is found, |
| 2584 |
|
the "bumpalong" advance is to the subject position that corresponds to that |
| 2585 |
|
(*MARK) instead of to where (*SKIP) was encountered. If no (*MARK) with a |
| 2586 |
|
matching name is found, normal "bumpalong" of one character happens (the |
| 2587 |
|
(*SKIP) is ignored). |
| 2588 |
|
.sp |
| 2589 |
|
(*THEN) or (*THEN:NAME) |
| 2590 |
.sp |
.sp |
| 2591 |
This verb causes a skip to the next alternation if the rest of the pattern does |
This verb causes a skip to the next alternation if the rest of the pattern does |
| 2592 |
not match. That is, it cancels pending backtracking, but only within the |
not match. That is, it cancels pending backtracking, but only within the |
| 2597 |
.sp |
.sp |
| 2598 |
If the COND1 pattern matches, FOO is tried (and possibly further items after |
If the COND1 pattern matches, FOO is tried (and possibly further items after |
| 2599 |
the end of the group if FOO succeeds); on failure the matcher skips to the |
the end of the group if FOO succeeds); on failure the matcher skips to the |
| 2600 |
second alternative and tries COND2, without backtracking into COND1. If (*THEN) |
second alternative and tries COND2, without backtracking into COND1. The |
| 2601 |
is used outside of any alternation, it acts exactly like (*PRUNE). |
behaviour of (*THEN:NAME) is exactly the same as (*MARK:NAME)(*THEN) if the |
| 2602 |
|
overall match fails. If (*THEN) is not directly inside an alternation, it acts |
| 2603 |
|
like (*PRUNE). |
| 2604 |
. |
. |
| 2605 |
. |
. |
| 2606 |
.SH "SEE ALSO" |
.SH "SEE ALSO" |
| 2607 |
.rs |
.rs |
| 2608 |
.sp |
.sp |
| 2609 |
\fBpcreapi\fP(3), \fBpcrecallout\fP(3), \fBpcrematching\fP(3), \fBpcre\fP(3). |
\fBpcreapi\fP(3), \fBpcrecallout\fP(3), \fBpcrematching\fP(3), |
| 2610 |
|
\fBpcresyntax\fP(3), \fBpcre\fP(3). |
| 2611 |
. |
. |
| 2612 |
. |
. |
| 2613 |
.SH AUTHOR |
.SH AUTHOR |
| 2624 |
.rs |
.rs |
| 2625 |
.sp |
.sp |
| 2626 |
.nf |
.nf |
| 2627 |
Last updated: 13 September 2009 |
Last updated: 05 May 2010 |
| 2628 |
Copyright (c) 1997-2009 University of Cambridge. |
Copyright (c) 1997-2010 University of Cambridge. |
| 2629 |
.fi |
.fi |