| 14 |
<br> |
<br> |
| 15 |
<ul> |
<ul> |
| 16 |
<li><a name="TOC1" href="#SEC1">PCRE REGULAR EXPRESSION DETAILS</a> |
<li><a name="TOC1" href="#SEC1">PCRE REGULAR EXPRESSION DETAILS</a> |
| 17 |
<li><a name="TOC2" href="#SEC2">CHARACTERS AND METACHARACTERS</a> |
<li><a name="TOC2" href="#SEC2">NEWLINE CONVENTIONS</a> |
| 18 |
<li><a name="TOC3" href="#SEC3">BACKSLASH</a> |
<li><a name="TOC3" href="#SEC3">CHARACTERS AND METACHARACTERS</a> |
| 19 |
<li><a name="TOC4" href="#SEC4">CIRCUMFLEX AND DOLLAR</a> |
<li><a name="TOC4" href="#SEC4">BACKSLASH</a> |
| 20 |
<li><a name="TOC5" href="#SEC5">FULL STOP (PERIOD, DOT)</a> |
<li><a name="TOC5" href="#SEC5">CIRCUMFLEX AND DOLLAR</a> |
| 21 |
<li><a name="TOC6" href="#SEC6">MATCHING A SINGLE BYTE</a> |
<li><a name="TOC6" href="#SEC6">FULL STOP (PERIOD, DOT)</a> |
| 22 |
<li><a name="TOC7" href="#SEC7">SQUARE BRACKETS AND CHARACTER CLASSES</a> |
<li><a name="TOC7" href="#SEC7">MATCHING A SINGLE BYTE</a> |
| 23 |
<li><a name="TOC8" href="#SEC8">POSIX CHARACTER CLASSES</a> |
<li><a name="TOC8" href="#SEC8">SQUARE BRACKETS AND CHARACTER CLASSES</a> |
| 24 |
<li><a name="TOC9" href="#SEC9">VERTICAL BAR</a> |
<li><a name="TOC9" href="#SEC9">POSIX CHARACTER CLASSES</a> |
| 25 |
<li><a name="TOC10" href="#SEC10">INTERNAL OPTION SETTING</a> |
<li><a name="TOC10" href="#SEC10">VERTICAL BAR</a> |
| 26 |
<li><a name="TOC11" href="#SEC11">SUBPATTERNS</a> |
<li><a name="TOC11" href="#SEC11">INTERNAL OPTION SETTING</a> |
| 27 |
<li><a name="TOC12" href="#SEC12">NAMED SUBPATTERNS</a> |
<li><a name="TOC12" href="#SEC12">SUBPATTERNS</a> |
| 28 |
<li><a name="TOC13" href="#SEC13">REPETITION</a> |
<li><a name="TOC13" href="#SEC13">DUPLICATE SUBPATTERN NUMBERS</a> |
| 29 |
<li><a name="TOC14" href="#SEC14">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a> |
<li><a name="TOC14" href="#SEC14">NAMED SUBPATTERNS</a> |
| 30 |
<li><a name="TOC15" href="#SEC15">BACK REFERENCES</a> |
<li><a name="TOC15" href="#SEC15">REPETITION</a> |
| 31 |
<li><a name="TOC16" href="#SEC16">ASSERTIONS</a> |
<li><a name="TOC16" href="#SEC16">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a> |
| 32 |
<li><a name="TOC17" href="#SEC17">CONDITIONAL SUBPATTERNS</a> |
<li><a name="TOC17" href="#SEC17">BACK REFERENCES</a> |
| 33 |
<li><a name="TOC18" href="#SEC18">COMMENTS</a> |
<li><a name="TOC18" href="#SEC18">ASSERTIONS</a> |
| 34 |
<li><a name="TOC19" href="#SEC19">RECURSIVE PATTERNS</a> |
<li><a name="TOC19" href="#SEC19">CONDITIONAL SUBPATTERNS</a> |
| 35 |
<li><a name="TOC20" href="#SEC20">SUBPATTERNS AS SUBROUTINES</a> |
<li><a name="TOC20" href="#SEC20">COMMENTS</a> |
| 36 |
<li><a name="TOC21" href="#SEC21">CALLOUTS</a> |
<li><a name="TOC21" href="#SEC21">RECURSIVE PATTERNS</a> |
| 37 |
<li><a name="TOC22" href="#SEC22">SEE ALSO</a> |
<li><a name="TOC22" href="#SEC22">SUBPATTERNS AS SUBROUTINES</a> |
| 38 |
<li><a name="TOC23" href="#SEC23">AUTHOR</a> |
<li><a name="TOC23" href="#SEC23">ONIGURUMA SUBROUTINE SYNTAX</a> |
| 39 |
<li><a name="TOC24" href="#SEC24">REVISION</a> |
<li><a name="TOC24" href="#SEC24">CALLOUTS</a> |
| 40 |
|
<li><a name="TOC25" href="#SEC25">BACKTRACKING CONTROL</a> |
| 41 |
|
<li><a name="TOC26" href="#SEC26">SEE ALSO</a> |
| 42 |
|
<li><a name="TOC27" href="#SEC27">AUTHOR</a> |
| 43 |
|
<li><a name="TOC28" href="#SEC28">REVISION</a> |
| 44 |
</ul> |
</ul> |
| 45 |
<br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br> |
<br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br> |
| 46 |
<P> |
<P> |
| 47 |
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 |
| 48 |
described below. Regular expressions are also described in the Perl |
are described in detail below. There is a quick-reference syntax summary in the |
| 49 |
documentation and in a number of books, some of which have copious examples. |
<a href="pcresyntax.html"><b>pcresyntax</b></a> |
| 50 |
Jeffrey Friedl's "Mastering Regular Expressions", published by O'Reilly, covers |
page. PCRE tries to match Perl syntax and semantics as closely as it can. PCRE |
| 51 |
regular expressions in great detail. This description of PCRE's regular |
also supports some alternative regular expression syntax (which does not |
| 52 |
expressions is intended as reference material. |
conflict with the Perl syntax) in order to provide some compatibility with |
| 53 |
|
regular expressions in Python, .NET, and Oniguruma. |
| 54 |
|
</P> |
| 55 |
|
<P> |
| 56 |
|
Perl's regular expressions are described in its own documentation, and |
| 57 |
|
regular expressions in general are covered in a number of books, some of which |
| 58 |
|
have copious examples. Jeffrey Friedl's "Mastering Regular Expressions", |
| 59 |
|
published by O'Reilly, covers regular expressions in great detail. This |
| 60 |
|
description of PCRE's regular expressions is intended as reference material. |
| 61 |
</P> |
</P> |
| 62 |
<P> |
<P> |
| 63 |
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, |
| 64 |
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, |
| 65 |
build PCRE to include UTF-8 support, and then call <b>pcre_compile()</b> with |
PCRE must be built to include UTF-8 support, and you must call |
| 66 |
the PCRE_UTF8 option. How this affects pattern matching is mentioned in several |
<b>pcre_compile()</b> or <b>pcre_compile2()</b> with the PCRE_UTF8 option. There |
| 67 |
places below. There is also a summary of UTF-8 features in the |
is also a special sequence that can be given at the start of a pattern: |
| 68 |
|
<pre> |
| 69 |
|
(*UTF8) |
| 70 |
|
</pre> |
| 71 |
|
Starting a pattern with this sequence is equivalent to setting the PCRE_UTF8 |
| 72 |
|
option. This feature is not Perl-compatible. How setting UTF-8 mode affects |
| 73 |
|
pattern matching is mentioned in several places below. There is also a summary |
| 74 |
|
of UTF-8 features in the |
| 75 |
<a href="pcre.html#utf8support">section on UTF-8 support</a> |
<a href="pcre.html#utf8support">section on UTF-8 support</a> |
| 76 |
in the main |
in the main |
| 77 |
<a href="pcre.html"><b>pcre</b></a> |
<a href="pcre.html"><b>pcre</b></a> |
| 82 |
PCRE when its main matching function, <b>pcre_exec()</b>, is used. |
PCRE when its main matching function, <b>pcre_exec()</b>, is used. |
| 83 |
From release 6.0, PCRE offers a second matching function, |
From release 6.0, PCRE offers a second matching function, |
| 84 |
<b>pcre_dfa_exec()</b>, which matches using a different algorithm that is not |
<b>pcre_dfa_exec()</b>, which matches using a different algorithm that is not |
| 85 |
Perl-compatible. The advantages and disadvantages of the alternative function, |
Perl-compatible. Some of the features discussed below are not available when |
| 86 |
and how it differs from the normal function, are discussed in the |
<b>pcre_dfa_exec()</b> is used. The advantages and disadvantages of the |
| 87 |
|
alternative function, and how it differs from the normal function, are |
| 88 |
|
discussed in the |
| 89 |
<a href="pcrematching.html"><b>pcrematching</b></a> |
<a href="pcrematching.html"><b>pcrematching</b></a> |
| 90 |
page. |
page. |
| 91 |
</P> |
</P> |
| 92 |
<br><a name="SEC2" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br> |
<br><a name="SEC2" href="#TOC1">NEWLINE CONVENTIONS</a><br> |
| 93 |
|
<P> |
| 94 |
|
PCRE supports five different conventions for indicating line breaks in |
| 95 |
|
strings: a single CR (carriage return) character, a single LF (linefeed) |
| 96 |
|
character, the two-character sequence CRLF, any of the three preceding, or any |
| 97 |
|
Unicode newline sequence. The |
| 98 |
|
<a href="pcreapi.html"><b>pcreapi</b></a> |
| 99 |
|
page has |
| 100 |
|
<a href="pcreapi.html#newlines">further discussion</a> |
| 101 |
|
about newlines, and shows how to set the newline convention in the |
| 102 |
|
<i>options</i> arguments for the compiling and matching functions. |
| 103 |
|
</P> |
| 104 |
|
<P> |
| 105 |
|
It is also possible to specify a newline convention by starting a pattern |
| 106 |
|
string with one of the following five sequences: |
| 107 |
|
<pre> |
| 108 |
|
(*CR) carriage return |
| 109 |
|
(*LF) linefeed |
| 110 |
|
(*CRLF) carriage return, followed by linefeed |
| 111 |
|
(*ANYCRLF) any of the three above |
| 112 |
|
(*ANY) all Unicode newline sequences |
| 113 |
|
</pre> |
| 114 |
|
These override the default and the options given to <b>pcre_compile()</b> or |
| 115 |
|
<b>pcre_compile2()</b>. For example, on a Unix system where LF is the default |
| 116 |
|
newline sequence, the pattern |
| 117 |
|
<pre> |
| 118 |
|
(*CR)a.b |
| 119 |
|
</pre> |
| 120 |
|
changes the convention to CR. That pattern matches "a\nb" because LF is no |
| 121 |
|
longer a newline. Note that these special settings, which are not |
| 122 |
|
Perl-compatible, are recognized only at the very start of a pattern, and that |
| 123 |
|
they must be in upper case. If more than one of them is present, the last one |
| 124 |
|
is used. |
| 125 |
|
</P> |
| 126 |
|
<P> |
| 127 |
|
The newline convention does not affect what the \R escape sequence matches. By |
| 128 |
|
default, this is any Unicode newline sequence, for Perl compatibility. However, |
| 129 |
|
this can be changed; see the description of \R in the section entitled |
| 130 |
|
<a href="#newlineseq">"Newline sequences"</a> |
| 131 |
|
below. A change of \R setting can be combined with a change of newline |
| 132 |
|
convention. |
| 133 |
|
</P> |
| 134 |
|
<br><a name="SEC3" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br> |
| 135 |
<P> |
<P> |
| 136 |
A regular expression is a pattern that is matched against a subject string from |
A regular expression is a pattern that is matched against a subject string from |
| 137 |
left to right. Most characters stand for themselves in a pattern, and match the |
left to right. Most characters stand for themselves in a pattern, and match the |
| 188 |
</pre> |
</pre> |
| 189 |
The following sections describe the use of each of the metacharacters. |
The following sections describe the use of each of the metacharacters. |
| 190 |
</P> |
</P> |
| 191 |
<br><a name="SEC3" href="#TOC1">BACKSLASH</a><br> |
<br><a name="SEC4" href="#TOC1">BACKSLASH</a><br> |
| 192 |
<P> |
<P> |
| 193 |
The backslash character has several uses. Firstly, if it is followed by a |
The backslash character has several uses. Firstly, if it is followed by a |
| 194 |
non-alphanumeric character, it takes away any special meaning that character |
non-alphanumeric character, it takes away any special meaning that character |
| 229 |
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 |
| 230 |
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 |
| 231 |
non-printing characters, apart from the binary zero that terminates a pattern, |
non-printing characters, apart from the binary zero that terminates a pattern, |
| 232 |
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 |
| 233 |
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: |
|
| 234 |
<pre> |
<pre> |
| 235 |
\a alarm, that is, the BEL character (hex 07) |
\a alarm, that is, the BEL character (hex 07) |
| 236 |
\cx "control-x", where x is any character |
\cx "control-x", where x is any character |
| 237 |
\e escape (hex 1B) |
\e escape (hex 1B) |
| 238 |
\f formfeed (hex 0C) |
\f formfeed (hex 0C) |
| 239 |
\n newline (hex 0A) |
\n linefeed (hex 0A) |
| 240 |
\r carriage return (hex 0D) |
\r carriage return (hex 0D) |
| 241 |
\t tab (hex 09) |
\t tab (hex 09) |
| 242 |
\ddd character with octal code ddd, or backreference |
\ddd character with octal code ddd, or back reference |
| 243 |
\xhh character with hex code hh |
\xhh character with hex code hh |
| 244 |
\x{hhh..} character with hex code hhh.. |
\x{hhh..} character with hex code hhh.. |
| 245 |
</pre> |
</pre> |
| 252 |
After \x, from zero to two hexadecimal digits are read (letters can be in |
After \x, from zero to two hexadecimal digits are read (letters can be in |
| 253 |
upper or lower case). Any number of hexadecimal digits may appear between \x{ |
upper or lower case). Any number of hexadecimal digits may appear between \x{ |
| 254 |
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 |
| 255 |
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 |
| 256 |
is 7FFFFFFF). If characters other than hexadecimal digits appear between \x{ |
hexadecimal is 7FFFFFFF. Note that this is bigger than the largest Unicode code |
| 257 |
and }, or if there is no terminating }, this form of escape is not recognized. |
point, which is 10FFFF. |
| 258 |
Instead, the initial \x will be interpreted as a basic hexadecimal escape, |
</P> |
| 259 |
with no following digits, giving a character whose value is zero. |
<P> |
| 260 |
|
If characters other than hexadecimal digits appear between \x{ and }, or if |
| 261 |
|
there is no terminating }, this form of escape is not recognized. Instead, the |
| 262 |
|
initial \x will be interpreted as a basic hexadecimal escape, with no |
| 263 |
|
following digits, giving a character whose value is zero. |
| 264 |
</P> |
</P> |
| 265 |
<P> |
<P> |
| 266 |
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 |
| 318 |
Absolute and relative back references |
Absolute and relative back references |
| 319 |
</b><br> |
</b><br> |
| 320 |
<P> |
<P> |
| 321 |
The sequence \g followed by a positive or negative number, optionally enclosed |
The sequence \g followed by an unsigned or a negative number, optionally |
| 322 |
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 |
| 323 |
discussed |
reference can be coded as \g{name}. Back references are discussed |
| 324 |
<a href="#backreferences">later,</a> |
<a href="#backreferences">later,</a> |
| 325 |
following the discussion of |
following the discussion of |
| 326 |
<a href="#subpattern">parenthesized subpatterns.</a> |
<a href="#subpattern">parenthesized subpatterns.</a> |
| 327 |
</P> |
</P> |
| 328 |
<br><b> |
<br><b> |
| 329 |
|
Absolute and relative subroutine calls |
| 330 |
|
</b><br> |
| 331 |
|
<P> |
| 332 |
|
For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or |
| 333 |
|
a number enclosed either in angle brackets or single quotes, is an alternative |
| 334 |
|
syntax for referencing a subpattern as a "subroutine". Details are discussed |
| 335 |
|
<a href="#onigurumasubroutines">later.</a> |
| 336 |
|
Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are <i>not</i> |
| 337 |
|
synonymous. The former is a back reference; the latter is a |
| 338 |
|
<a href="#subpatternsassubroutines">subroutine</a> |
| 339 |
|
call. |
| 340 |
|
</P> |
| 341 |
|
<br><b> |
| 342 |
Generic character types |
Generic character types |
| 343 |
</b><br> |
</b><br> |
| 344 |
<P> |
<P> |
| 347 |
<pre> |
<pre> |
| 348 |
\d any decimal digit |
\d any decimal digit |
| 349 |
\D any character that is not a decimal digit |
\D any character that is not a decimal digit |
| 350 |
|
\h any horizontal whitespace character |
| 351 |
|
\H any character that is not a horizontal whitespace character |
| 352 |
\s any whitespace character |
\s any whitespace character |
| 353 |
\S any character that is not a whitespace character |
\S any character that is not a whitespace character |
| 354 |
|
\v any vertical whitespace character |
| 355 |
|
\V any character that is not a vertical whitespace character |
| 356 |
\w any "word" character |
\w any "word" character |
| 357 |
\W any "non-word" character |
\W any "non-word" character |
| 358 |
</pre> |
</pre> |
| 368 |
<P> |
<P> |
| 369 |
For compatibility with Perl, \s does not match the VT character (code 11). |
For compatibility with Perl, \s does not match the VT character (code 11). |
| 370 |
This makes it different from the the POSIX "space" class. The \s characters |
This makes it different from the the POSIX "space" class. The \s characters |
| 371 |
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 |
| 372 |
included in a Perl script, \s may match the VT character. In PCRE, it never |
included in a Perl script, \s may match the VT character. In PCRE, it never |
| 373 |
does.) |
does. |
| 374 |
|
</P> |
| 375 |
|
<P> |
| 376 |
|
In UTF-8 mode, characters with values greater than 128 never match \d, \s, or |
| 377 |
|
\w, and always match \D, \S, and \W. This is true even when Unicode |
| 378 |
|
character property support is available. These sequences retain their original |
| 379 |
|
meanings from before UTF-8 support was available, mainly for efficiency |
| 380 |
|
reasons. Note that this also affects \b, because it is defined in terms of \w |
| 381 |
|
and \W. |
| 382 |
|
</P> |
| 383 |
|
<P> |
| 384 |
|
The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to the |
| 385 |
|
other sequences, these do match certain high-valued codepoints in UTF-8 mode. |
| 386 |
|
The horizontal space characters are: |
| 387 |
|
<pre> |
| 388 |
|
U+0009 Horizontal tab |
| 389 |
|
U+0020 Space |
| 390 |
|
U+00A0 Non-break space |
| 391 |
|
U+1680 Ogham space mark |
| 392 |
|
U+180E Mongolian vowel separator |
| 393 |
|
U+2000 En quad |
| 394 |
|
U+2001 Em quad |
| 395 |
|
U+2002 En space |
| 396 |
|
U+2003 Em space |
| 397 |
|
U+2004 Three-per-em space |
| 398 |
|
U+2005 Four-per-em space |
| 399 |
|
U+2006 Six-per-em space |
| 400 |
|
U+2007 Figure space |
| 401 |
|
U+2008 Punctuation space |
| 402 |
|
U+2009 Thin space |
| 403 |
|
U+200A Hair space |
| 404 |
|
U+202F Narrow no-break space |
| 405 |
|
U+205F Medium mathematical space |
| 406 |
|
U+3000 Ideographic space |
| 407 |
|
</pre> |
| 408 |
|
The vertical space characters are: |
| 409 |
|
<pre> |
| 410 |
|
U+000A Linefeed |
| 411 |
|
U+000B Vertical tab |
| 412 |
|
U+000C Formfeed |
| 413 |
|
U+000D Carriage return |
| 414 |
|
U+0085 Next line |
| 415 |
|
U+2028 Line separator |
| 416 |
|
U+2029 Paragraph separator |
| 417 |
|
</PRE> |
| 418 |
</P> |
</P> |
| 419 |
<P> |
<P> |
| 420 |
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 |
| 424 |
<a href="pcreapi.html#localesupport">"Locale support"</a> |
<a href="pcreapi.html#localesupport">"Locale support"</a> |
| 425 |
in the |
in the |
| 426 |
<a href="pcreapi.html"><b>pcreapi</b></a> |
<a href="pcreapi.html"><b>pcreapi</b></a> |
| 427 |
page). For example, in the "fr_FR" (French) locale, some character codes |
page). For example, in a French locale such as "fr_FR" in Unix-like systems, |
| 428 |
greater than 128 are used for accented letters, and these are matched by \w. |
or "french" in Windows, some character codes greater than 128 are used for |
| 429 |
</P> |
accented letters, and these are matched by \w. The use of locales with Unicode |
| 430 |
<P> |
is discouraged. |
| 431 |
In UTF-8 mode, characters with values greater than 128 never match \d, \s, or |
<a name="newlineseq"></a></P> |
|
\w, and always match \D, \S, and \W. This is true even when Unicode |
|
|
character property support is available. The use of locales with Unicode is |
|
|
discouraged. |
|
|
</P> |
|
| 432 |
<br><b> |
<br><b> |
| 433 |
Newline sequences |
Newline sequences |
| 434 |
</b><br> |
</b><br> |
| 435 |
<P> |
<P> |
| 436 |
Outside a character class, the escape sequence \R matches any Unicode newline |
Outside a character class, by default, the escape sequence \R matches any |
| 437 |
sequence. This is an extension to Perl. In non-UTF-8 mode \R is equivalent to |
Unicode newline sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \R is |
| 438 |
the following: |
equivalent to the following: |
| 439 |
<pre> |
<pre> |
| 440 |
(?>\r\n|\n|\x0b|\f|\r|\x85) |
(?>\r\n|\n|\x0b|\f|\r|\x85) |
| 441 |
</pre> |
</pre> |
| 454 |
recognized. |
recognized. |
| 455 |
</P> |
</P> |
| 456 |
<P> |
<P> |
| 457 |
|
It is possible to restrict \R to match only CR, LF, or CRLF (instead of the |
| 458 |
|
complete set of Unicode line endings) by setting the option PCRE_BSR_ANYCRLF |
| 459 |
|
either at compile time or when the pattern is matched. (BSR is an abbrevation |
| 460 |
|
for "backslash R".) This can be made the default when PCRE is built; if this is |
| 461 |
|
the case, the other behaviour can be requested via the PCRE_BSR_UNICODE option. |
| 462 |
|
It is also possible to specify these settings by starting a pattern string with |
| 463 |
|
one of the following sequences: |
| 464 |
|
<pre> |
| 465 |
|
(*BSR_ANYCRLF) CR, LF, or CRLF only |
| 466 |
|
(*BSR_UNICODE) any Unicode newline sequence |
| 467 |
|
</pre> |
| 468 |
|
These override the default and the options given to <b>pcre_compile()</b> or |
| 469 |
|
<b>pcre_compile2()</b>, but they can be overridden by options given to |
| 470 |
|
<b>pcre_exec()</b> or <b>pcre_dfa_exec()</b>. Note that these special settings, |
| 471 |
|
which are not Perl-compatible, are recognized only at the very start of a |
| 472 |
|
pattern, and that they must be in upper case. If more than one of them is |
| 473 |
|
present, the last one is used. They can be combined with a change of newline |
| 474 |
|
convention, for example, a pattern can start with: |
| 475 |
|
<pre> |
| 476 |
|
(*ANY)(*BSR_ANYCRLF) |
| 477 |
|
</pre> |
| 478 |
Inside a character class, \R matches the letter "R". |
Inside a character class, \R matches the letter "R". |
| 479 |
<a name="uniextseq"></a></P> |
<a name="uniextseq"></a></P> |
| 480 |
<br><b> |
<br><b> |
| 482 |
</b><br> |
</b><br> |
| 483 |
<P> |
<P> |
| 484 |
When PCRE is built with Unicode character property support, three additional |
When PCRE is built with Unicode character property support, three additional |
| 485 |
escape sequences to match character properties are available when UTF-8 mode |
escape sequences that match characters with specific properties are available. |
| 486 |
is selected. They are: |
When not in UTF-8 mode, these sequences are of course limited to testing |
| 487 |
|
characters whose codepoints are less than 256, but they do work in this mode. |
| 488 |
|
The extra escape sequences are: |
| 489 |
<pre> |
<pre> |
| 490 |
\p{<i>xx</i>} a character with the <i>xx</i> property |
\p{<i>xx</i>} a character with the <i>xx</i> property |
| 491 |
\P{<i>xx</i>} a character without the <i>xx</i> property |
\P{<i>xx</i>} a character without the <i>xx</i> property |
| 511 |
<P> |
<P> |
| 512 |
Arabic, |
Arabic, |
| 513 |
Armenian, |
Armenian, |
| 514 |
|
Avestan, |
| 515 |
Balinese, |
Balinese, |
| 516 |
|
Bamum, |
| 517 |
Bengali, |
Bengali, |
| 518 |
Bopomofo, |
Bopomofo, |
| 519 |
Braille, |
Braille, |
| 520 |
Buginese, |
Buginese, |
| 521 |
Buhid, |
Buhid, |
| 522 |
Canadian_Aboriginal, |
Canadian_Aboriginal, |
| 523 |
|
Carian, |
| 524 |
|
Cham, |
| 525 |
Cherokee, |
Cherokee, |
| 526 |
Common, |
Common, |
| 527 |
Coptic, |
Coptic, |
| 530 |
Cyrillic, |
Cyrillic, |
| 531 |
Deseret, |
Deseret, |
| 532 |
Devanagari, |
Devanagari, |
| 533 |
|
Egyptian_Hieroglyphs, |
| 534 |
Ethiopic, |
Ethiopic, |
| 535 |
Georgian, |
Georgian, |
| 536 |
Glagolitic, |
Glagolitic, |
| 543 |
Hanunoo, |
Hanunoo, |
| 544 |
Hebrew, |
Hebrew, |
| 545 |
Hiragana, |
Hiragana, |
| 546 |
|
Imperial_Aramaic, |
| 547 |
Inherited, |
Inherited, |
| 548 |
|
Inscriptional_Pahlavi, |
| 549 |
|
Inscriptional_Parthian, |
| 550 |
|
Javanese, |
| 551 |
|
Kaithi, |
| 552 |
Kannada, |
Kannada, |
| 553 |
Katakana, |
Katakana, |
| 554 |
|
Kayah_Li, |
| 555 |
Kharoshthi, |
Kharoshthi, |
| 556 |
Khmer, |
Khmer, |
| 557 |
Lao, |
Lao, |
| 558 |
Latin, |
Latin, |
| 559 |
|
Lepcha, |
| 560 |
Limbu, |
Limbu, |
| 561 |
Linear_B, |
Linear_B, |
| 562 |
|
Lisu, |
| 563 |
|
Lycian, |
| 564 |
|
Lydian, |
| 565 |
Malayalam, |
Malayalam, |
| 566 |
|
Meetei_Mayek, |
| 567 |
Mongolian, |
Mongolian, |
| 568 |
Myanmar, |
Myanmar, |
| 569 |
New_Tai_Lue, |
New_Tai_Lue, |
| 571 |
Ogham, |
Ogham, |
| 572 |
Old_Italic, |
Old_Italic, |
| 573 |
Old_Persian, |
Old_Persian, |
| 574 |
|
Old_South_Arabian, |
| 575 |
|
Old_Turkic, |
| 576 |
|
Ol_Chiki, |
| 577 |
Oriya, |
Oriya, |
| 578 |
Osmanya, |
Osmanya, |
| 579 |
Phags_Pa, |
Phags_Pa, |
| 580 |
Phoenician, |
Phoenician, |
| 581 |
|
Rejang, |
| 582 |
Runic, |
Runic, |
| 583 |
|
Samaritan, |
| 584 |
|
Saurashtra, |
| 585 |
Shavian, |
Shavian, |
| 586 |
Sinhala, |
Sinhala, |
| 587 |
|
Sundanese, |
| 588 |
Syloti_Nagri, |
Syloti_Nagri, |
| 589 |
Syriac, |
Syriac, |
| 590 |
Tagalog, |
Tagalog, |
| 591 |
Tagbanwa, |
Tagbanwa, |
| 592 |
Tai_Le, |
Tai_Le, |
| 593 |
|
Tai_Tham, |
| 594 |
|
Tai_Viet, |
| 595 |
Tamil, |
Tamil, |
| 596 |
Telugu, |
Telugu, |
| 597 |
Thaana, |
Thaana, |
| 599 |
Tibetan, |
Tibetan, |
| 600 |
Tifinagh, |
Tifinagh, |
| 601 |
Ugaritic, |
Ugaritic, |
| 602 |
|
Vai, |
| 603 |
Yi. |
Yi. |
| 604 |
</P> |
</P> |
| 605 |
<P> |
<P> |
| 668 |
a modifier or "other". |
a modifier or "other". |
| 669 |
</P> |
</P> |
| 670 |
<P> |
<P> |
| 671 |
The long synonyms for these properties that Perl supports (such as \p{Letter}) |
The Cs (Surrogate) property applies only to characters in the range U+D800 to |
| 672 |
|
U+DFFF. Such characters are not valid in UTF-8 strings (see RFC 3629) and so |
| 673 |
|
cannot be tested by PCRE, unless UTF-8 validity checking has been turned off |
| 674 |
|
(see the discussion of PCRE_NO_UTF8_CHECK in the |
| 675 |
|
<a href="pcreapi.html"><b>pcreapi</b></a> |
| 676 |
|
page). Perl does not support the Cs property. |
| 677 |
|
</P> |
| 678 |
|
<P> |
| 679 |
|
The long synonyms for property names that Perl supports (such as \p{Letter}) |
| 680 |
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 |
| 681 |
properties with "Is". |
properties with "Is". |
| 682 |
</P> |
</P> |
| 700 |
atomic group |
atomic group |
| 701 |
<a href="#atomicgroup">(see below).</a> |
<a href="#atomicgroup">(see below).</a> |
| 702 |
Characters with the "mark" property are typically accents that affect the |
Characters with the "mark" property are typically accents that affect the |
| 703 |
preceding character. |
preceding character. None of them have codepoints less than 256, so in |
| 704 |
|
non-UTF-8 mode \X matches any one character. |
| 705 |
</P> |
</P> |
| 706 |
<P> |
<P> |
| 707 |
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 |
| 708 |
a structure that contains data for over fifteen thousand characters. That is |
a structure that contains data for over fifteen thousand characters. That is |
| 709 |
why the traditional escape sequences such as \d and \w do not use Unicode |
why the traditional escape sequences such as \d and \w do not use Unicode |
| 710 |
properties in PCRE. |
properties in PCRE. |
| 711 |
|
<a name="resetmatchstart"></a></P> |
| 712 |
|
<br><b> |
| 713 |
|
Resetting the match start |
| 714 |
|
</b><br> |
| 715 |
|
<P> |
| 716 |
|
The escape sequence \K, which is a Perl 5.10 feature, causes any previously |
| 717 |
|
matched characters not to be included in the final matched sequence. For |
| 718 |
|
example, the pattern: |
| 719 |
|
<pre> |
| 720 |
|
foo\Kbar |
| 721 |
|
</pre> |
| 722 |
|
matches "foobar", but reports that it has matched "bar". This feature is |
| 723 |
|
similar to a lookbehind assertion |
| 724 |
|
<a href="#lookbehind">(described below).</a> |
| 725 |
|
However, in this case, the part of the subject before the real match does not |
| 726 |
|
have to be of fixed length, as lookbehind assertions do. The use of \K does |
| 727 |
|
not interfere with the setting of |
| 728 |
|
<a href="#subpattern">captured substrings.</a> |
| 729 |
|
For example, when the pattern |
| 730 |
|
<pre> |
| 731 |
|
(foo)\Kbar |
| 732 |
|
</pre> |
| 733 |
|
matches "foobar", the first substring is still set to "foo". |
| 734 |
|
</P> |
| 735 |
|
<P> |
| 736 |
|
Perl documents that the use of \K within assertions is "not well defined". In |
| 737 |
|
PCRE, \K is acted upon when it occurs inside positive assertions, but is |
| 738 |
|
ignored in negative assertions. |
| 739 |
<a name="smallassertions"></a></P> |
<a name="smallassertions"></a></P> |
| 740 |
<br><b> |
<br><b> |
| 741 |
Simple assertions |
Simple assertions |
| 763 |
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 |
| 764 |
and the previous character do not both match \w or \W (i.e. one matches |
and the previous character do not both match \w or \W (i.e. one matches |
| 765 |
\w and the other matches \W), or the start or end of the string if the |
\w and the other matches \W), or the start or end of the string if the |
| 766 |
first or last character matches \w, respectively. |
first or last character matches \w, respectively. Neither PCRE nor Perl has a |
| 767 |
|
separte "start of word" or "end of word" metasequence. However, whatever |
| 768 |
|
follows \b normally determines which it is. For example, the fragment |
| 769 |
|
\ba matches "a" at the start of a word. |
| 770 |
</P> |
</P> |
| 771 |
<P> |
<P> |
| 772 |
The \A, \Z, and \z assertions differ from the traditional circumflex and |
The \A, \Z, and \z assertions differ from the traditional circumflex and |
| 800 |
to the starting match position, and the "anchored" flag is set in the compiled |
to the starting match position, and the "anchored" flag is set in the compiled |
| 801 |
regular expression. |
regular expression. |
| 802 |
</P> |
</P> |
| 803 |
<br><a name="SEC4" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br> |
<br><a name="SEC5" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br> |
| 804 |
<P> |
<P> |
| 805 |
Outside a character class, in the default matching mode, the circumflex |
Outside a character class, in the default matching mode, the circumflex |
| 806 |
character is an assertion that is true only if the current matching point is |
character is an assertion that is true only if the current matching point is |
| 854 |
end of the subject in both modes, and if all branches of a pattern start with |
end of the subject in both modes, and if all branches of a pattern start with |
| 855 |
\A it is always anchored, whether or not PCRE_MULTILINE is set. |
\A it is always anchored, whether or not PCRE_MULTILINE is set. |
| 856 |
</P> |
</P> |
| 857 |
<br><a name="SEC5" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br> |
<br><a name="SEC6" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br> |
| 858 |
<P> |
<P> |
| 859 |
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 |
| 860 |
the subject string except (by default) a character that signifies the end of a |
the subject string except (by default) a character that signifies the end of a |
| 879 |
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 |
| 880 |
special meaning in a character class. |
special meaning in a character class. |
| 881 |
</P> |
</P> |
| 882 |
<br><a name="SEC6" href="#TOC1">MATCHING A SINGLE BYTE</a><br> |
<br><a name="SEC7" href="#TOC1">MATCHING A SINGLE BYTE</a><br> |
| 883 |
<P> |
<P> |
| 884 |
Outside a character class, the escape sequence \C matches any one byte, both |
Outside a character class, the escape sequence \C matches any one byte, both |
| 885 |
in and out of UTF-8 mode. Unlike a dot, it always matches any line-ending |
in and out of UTF-8 mode. Unlike a dot, it always matches any line-ending |
| 894 |
because in UTF-8 mode this would make it impossible to calculate the length of |
because in UTF-8 mode this would make it impossible to calculate the length of |
| 895 |
the lookbehind. |
the lookbehind. |
| 896 |
<a name="characterclass"></a></P> |
<a name="characterclass"></a></P> |
| 897 |
<br><a name="SEC7" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br> |
<br><a name="SEC8" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br> |
| 898 |
<P> |
<P> |
| 899 |
An opening square bracket introduces a character class, terminated by a closing |
An opening square bracket introduces a character class, terminated by a closing |
| 900 |
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. |
| 901 |
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 |
| 902 |
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 |
| 903 |
escaped with a backslash. |
a member of the class, it should be the first data character in the class |
| 904 |
|
(after an initial circumflex, if present) or escaped with a backslash. |
| 905 |
</P> |
</P> |
| 906 |
<P> |
<P> |
| 907 |
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 |
| 908 |
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 |
| 909 |
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 |
| 910 |
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 |
| 911 |
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 |
| 912 |
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 |
| 917 |
[^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 |
| 918 |
circumflex is just a convenient notation for specifying the characters that |
circumflex is just a convenient notation for specifying the characters that |
| 919 |
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 |
| 920 |
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 |
| 921 |
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 |
| 922 |
string. |
string. |
| 923 |
</P> |
</P> |
| 933 |
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 |
| 934 |
always possible. For characters with higher values, the concept of case is |
always possible. For characters with higher values, the concept of case is |
| 935 |
supported if PCRE is compiled with Unicode property support, but not otherwise. |
supported if PCRE is compiled with Unicode property support, but not otherwise. |
| 936 |
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, |
| 937 |
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 |
| 938 |
UTF-8 support. |
with UTF-8 support. |
| 939 |
</P> |
</P> |
| 940 |
<P> |
<P> |
| 941 |
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 |
| 969 |
If a range that includes letters is used when caseless matching is set, it |
If a range that includes letters is used when caseless matching is set, it |
| 970 |
matches the letters in either case. For example, [W-c] is equivalent to |
matches the letters in either case. For example, [W-c] is equivalent to |
| 971 |
[][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character |
[][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character |
| 972 |
tables for the "fr_FR" locale are in use, [\xc8-\xcb] matches accented E |
tables for a French locale are in use, [\xc8-\xcb] matches accented E |
| 973 |
characters in both cases. In UTF-8 mode, PCRE supports the concept of case for |
characters in both cases. In UTF-8 mode, PCRE supports the concept of case for |
| 974 |
characters with values greater than 128 only when it is compiled with Unicode |
characters with values greater than 128 only when it is compiled with Unicode |
| 975 |
property support. |
property support. |
| 990 |
closing square bracket. However, escaping other non-alphanumeric characters |
closing square bracket. However, escaping other non-alphanumeric characters |
| 991 |
does no harm. |
does no harm. |
| 992 |
</P> |
</P> |
| 993 |
<br><a name="SEC8" href="#TOC1">POSIX CHARACTER CLASSES</a><br> |
<br><a name="SEC9" href="#TOC1">POSIX CHARACTER CLASSES</a><br> |
| 994 |
<P> |
<P> |
| 995 |
Perl supports the POSIX notation for character classes. This uses names |
Perl supports the POSIX notation for character classes. This uses names |
| 996 |
enclosed by [: and :] within the enclosing square brackets. PCRE also supports |
enclosed by [: and :] within the enclosing square brackets. PCRE also supports |
| 1036 |
In UTF-8 mode, characters with values greater than 128 do not match any of |
In UTF-8 mode, characters with values greater than 128 do not match any of |
| 1037 |
the POSIX character classes. |
the POSIX character classes. |
| 1038 |
</P> |
</P> |
| 1039 |
<br><a name="SEC9" href="#TOC1">VERTICAL BAR</a><br> |
<br><a name="SEC10" href="#TOC1">VERTICAL BAR</a><br> |
| 1040 |
<P> |
<P> |
| 1041 |
Vertical bar characters are used to separate alternative patterns. For example, |
Vertical bar characters are used to separate alternative patterns. For example, |
| 1042 |
the pattern |
the pattern |
| 1051 |
"succeeds" means matching the rest of the main pattern as well as the |
"succeeds" means matching the rest of the main pattern as well as the |
| 1052 |
alternative in the subpattern. |
alternative in the subpattern. |
| 1053 |
</P> |
</P> |
| 1054 |
<br><a name="SEC10" href="#TOC1">INTERNAL OPTION SETTING</a><br> |
<br><a name="SEC11" href="#TOC1">INTERNAL OPTION SETTING</a><br> |
| 1055 |
<P> |
<P> |
| 1056 |
The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and |
The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and |
| 1057 |
PCRE_EXTENDED options can be changed from within the pattern by a sequence of |
PCRE_EXTENDED options (which are Perl-compatible) can be changed from within |
| 1058 |
Perl option letters enclosed between "(?" and ")". The option letters are |
the pattern by a sequence of Perl option letters enclosed between "(?" and ")". |
| 1059 |
|
The option letters are |
| 1060 |
<pre> |
<pre> |
| 1061 |
i for PCRE_CASELESS |
i for PCRE_CASELESS |
| 1062 |
m for PCRE_MULTILINE |
m for PCRE_MULTILINE |
| 1071 |
unset. |
unset. |
| 1072 |
</P> |
</P> |
| 1073 |
<P> |
<P> |
| 1074 |
When an option change occurs at top level (that is, not inside subpattern |
The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be |
| 1075 |
parentheses), the change applies to the remainder of the pattern that follows. |
changed in the same way as the Perl-compatible options by using the characters |
| 1076 |
If the change is placed right at the start of a pattern, PCRE extracts it into |
J, U and X respectively. |
| 1077 |
the global options (and it will therefore show up in data extracted by the |
</P> |
| 1078 |
<b>pcre_fullinfo()</b> function). |
<P> |
| 1079 |
|
When one of these option changes occurs at top level (that is, not inside |
| 1080 |
|
subpattern parentheses), the change applies to the remainder of the pattern |
| 1081 |
|
that follows. If the change is placed right at the start of a pattern, PCRE |
| 1082 |
|
extracts it into the global options (and it will therefore show up in data |
| 1083 |
|
extracted by the <b>pcre_fullinfo()</b> function). |
| 1084 |
</P> |
</P> |
| 1085 |
<P> |
<P> |
| 1086 |
An option change within a subpattern (see below for a description of |
An option change within a subpattern (see below for a description of |
| 1101 |
behaviour otherwise. |
behaviour otherwise. |
| 1102 |
</P> |
</P> |
| 1103 |
<P> |
<P> |
| 1104 |
The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be |
<b>Note:</b> There are other PCRE-specific options that can be set by the |
| 1105 |
changed in the same way as the Perl-compatible options by using the characters |
application when the compile or match functions are called. In some cases the |
| 1106 |
J, U and X respectively. |
pattern can contain special leading sequences such as (*CRLF) to override what |
| 1107 |
|
the application has set or what has been defaulted. Details are given in the |
| 1108 |
|
section entitled |
| 1109 |
|
<a href="#newlineseq">"Newline sequences"</a> |
| 1110 |
|
above. There is also the (*UTF8) leading sequence that can be used to set UTF-8 |
| 1111 |
|
mode; this is equivalent to setting the PCRE_UTF8 option. |
| 1112 |
<a name="subpattern"></a></P> |
<a name="subpattern"></a></P> |
| 1113 |
<br><a name="SEC11" href="#TOC1">SUBPATTERNS</a><br> |
<br><a name="SEC12" href="#TOC1">SUBPATTERNS</a><br> |
| 1114 |
<P> |
<P> |
| 1115 |
Subpatterns are delimited by parentheses (round brackets), which can be nested. |
Subpatterns are delimited by parentheses (round brackets), which can be nested. |
| 1116 |
Turning part of a pattern into a subpattern does two things: |
Turning part of a pattern into a subpattern does two things: |
| 1163 |
from left to right, and options are not reset until the end of the subpattern |
from left to right, and options are not reset until the end of the subpattern |
| 1164 |
is reached, an option setting in one branch does affect subsequent branches, so |
is reached, an option setting in one branch does affect subsequent branches, so |
| 1165 |
the above patterns match "SUNDAY" as well as "Saturday". |
the above patterns match "SUNDAY" as well as "Saturday". |
| 1166 |
|
<a name="dupsubpatternnumber"></a></P> |
| 1167 |
|
<br><a name="SEC13" href="#TOC1">DUPLICATE SUBPATTERN NUMBERS</a><br> |
| 1168 |
|
<P> |
| 1169 |
|
Perl 5.10 introduced a feature whereby each alternative in a subpattern uses |
| 1170 |
|
the same numbers for its capturing parentheses. Such a subpattern starts with |
| 1171 |
|
(?| and is itself a non-capturing subpattern. For example, consider this |
| 1172 |
|
pattern: |
| 1173 |
|
<pre> |
| 1174 |
|
(?|(Sat)ur|(Sun))day |
| 1175 |
|
</pre> |
| 1176 |
|
Because the two alternatives are inside a (?| group, both sets of capturing |
| 1177 |
|
parentheses are numbered one. Thus, when the pattern matches, you can look |
| 1178 |
|
at captured substring number one, whichever alternative matched. This construct |
| 1179 |
|
is useful when you want to capture part, but not all, of one of a number of |
| 1180 |
|
alternatives. Inside a (?| group, parentheses are numbered as usual, but the |
| 1181 |
|
number is reset at the start of each branch. The numbers of any capturing |
| 1182 |
|
buffers that follow the subpattern start after the highest number used in any |
| 1183 |
|
branch. The following example is taken from the Perl documentation. |
| 1184 |
|
The numbers underneath show in which buffer the captured content will be |
| 1185 |
|
stored. |
| 1186 |
|
<pre> |
| 1187 |
|
# before ---------------branch-reset----------- after |
| 1188 |
|
/ ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
| 1189 |
|
# 1 2 2 3 2 3 4 |
| 1190 |
|
</pre> |
| 1191 |
|
A back reference to a numbered subpattern uses the most recent value that is |
| 1192 |
|
set for that number by any subpattern. The following pattern matches "abcabc" |
| 1193 |
|
or "defdef": |
| 1194 |
|
<pre> |
| 1195 |
|
/(?|(abc)|(def))\1/ |
| 1196 |
|
</pre> |
| 1197 |
|
In contrast, a recursive or "subroutine" call to a numbered subpattern always |
| 1198 |
|
refers to the first one in the pattern with the given number. The following |
| 1199 |
|
pattern matches "abcabc" or "defabc": |
| 1200 |
|
<pre> |
| 1201 |
|
/(?|(abc)|(def))(?1)/ |
| 1202 |
|
</pre> |
| 1203 |
|
If a |
| 1204 |
|
<a href="#conditions">condition test</a> |
| 1205 |
|
for a subpattern's having matched refers to a non-unique number, the test is |
| 1206 |
|
true if any of the subpatterns of that number have matched. |
| 1207 |
</P> |
</P> |
| 1208 |
<br><a name="SEC12" href="#TOC1">NAMED SUBPATTERNS</a><br> |
<P> |
| 1209 |
|
An alternative approach to using this "branch reset" feature is to use |
| 1210 |
|
duplicate named subpatterns, as described in the next section. |
| 1211 |
|
</P> |
| 1212 |
|
<br><a name="SEC14" href="#TOC1">NAMED SUBPATTERNS</a><br> |
| 1213 |
<P> |
<P> |
| 1214 |
Identifying capturing parentheses by number is simple, but it can be very hard |
Identifying capturing parentheses by number is simple, but it can be very hard |
| 1215 |
to keep track of the numbers in complicated regular expressions. Furthermore, |
to keep track of the numbers in complicated regular expressions. Furthermore, |
| 1217 |
difficulty, PCRE supports the naming of subpatterns. This feature was not |
difficulty, PCRE supports the naming of subpatterns. This feature was not |
| 1218 |
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 |
| 1219 |
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 |
| 1220 |
the Perl and the Python syntax. |
the Perl and the Python syntax. Perl allows identically numbered subpatterns to |
| 1221 |
|
have different names, but PCRE does not. |
| 1222 |
</P> |
</P> |
| 1223 |
<P> |
<P> |
| 1224 |
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 |
| 1225 |
(?'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 |
| 1226 |
parentheses from other parts of the pattern, such as |
parentheses from other parts of the pattern, such as |
| 1227 |
<a href="#backreferences">backreferences,</a> |
<a href="#backreferences">back references,</a> |
| 1228 |
<a href="#recursion">recursion,</a> |
<a href="#recursion">recursion,</a> |
| 1229 |
and |
and |
| 1230 |
<a href="#conditions">conditions,</a> |
<a href="#conditions">conditions,</a> |
| 1239 |
</P> |
</P> |
| 1240 |
<P> |
<P> |
| 1241 |
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 |
| 1242 |
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 |
| 1243 |
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 |
| 1244 |
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 |
| 1245 |
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 |
| 1246 |
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 |
| 1247 |
|
name, and in both cases you want to extract the abbreviation. This pattern |
| 1248 |
|
(ignoring the line breaks) does the job: |
| 1249 |
<pre> |
<pre> |
| 1250 |
(?<DN>Mon|Fri|Sun)(?:day)?| |
(?<DN>Mon|Fri|Sun)(?:day)?| |
| 1251 |
(?<DN>Tue)(?:sday)?| |
(?<DN>Tue)(?:sday)?| |
| 1254 |
(?<DN>Sat)(?:urday)? |
(?<DN>Sat)(?:urday)? |
| 1255 |
</pre> |
</pre> |
| 1256 |
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. |
| 1257 |
|
(An alternative way of solving this problem is to use a "branch reset" |
| 1258 |
|
subpattern, as described in the previous section.) |
| 1259 |
|
</P> |
| 1260 |
|
<P> |
| 1261 |
The convenience function for extracting the data by name returns the substring |
The convenience function for extracting the data by name returns the substring |
| 1262 |
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 |
| 1263 |
matched. This saves searching to find which numbered subpattern it was. If you |
matched. This saves searching to find which numbered subpattern it was. |
| 1264 |
make a reference to a non-unique named subpattern from elsewhere in the |
</P> |
| 1265 |
pattern, the one that corresponds to the lowest number is used. For further |
<P> |
| 1266 |
details of the interfaces for handling named subpatterns, see the |
If you make a back reference to a non-unique named subpattern from elsewhere in |
| 1267 |
|
the pattern, the one that corresponds to the first occurrence of the name is |
| 1268 |
|
used. In the absence of duplicate numbers (see the previous section) this is |
| 1269 |
|
the one with the lowest number. If you use a named reference in a condition |
| 1270 |
|
test (see the |
| 1271 |
|
<a href="#conditions">section about conditions</a> |
| 1272 |
|
below), either to check whether a subpattern has matched, or to check for |
| 1273 |
|
recursion, all subpatterns with the same name are tested. If the condition is |
| 1274 |
|
true for any one of them, the overall condition is true. This is the same |
| 1275 |
|
behaviour as testing by number. For further details of the interfaces for |
| 1276 |
|
handling named subpatterns, see the |
| 1277 |
<a href="pcreapi.html"><b>pcreapi</b></a> |
<a href="pcreapi.html"><b>pcreapi</b></a> |
| 1278 |
documentation. |
documentation. |
| 1279 |
</P> |
</P> |
| 1280 |
<br><a name="SEC13" href="#TOC1">REPETITION</a><br> |
<P> |
| 1281 |
|
<b>Warning:</b> You cannot use different names to distinguish between two |
| 1282 |
|
subpatterns with the same number because PCRE uses only the numbers when |
| 1283 |
|
matching. For this reason, an error is given at compile time if different names |
| 1284 |
|
are given to subpatterns with the same number. However, you can give the same |
| 1285 |
|
name to subpatterns with the same number, even when PCRE_DUPNAMES is not set. |
| 1286 |
|
</P> |
| 1287 |
|
<br><a name="SEC15" href="#TOC1">REPETITION</a><br> |
| 1288 |
<P> |
<P> |
| 1289 |
Repetition is specified by quantifiers, which can follow any of the following |
Repetition is specified by quantifiers, which can follow any of the following |
| 1290 |
items: |
items: |
| 1298 |
a character class |
a character class |
| 1299 |
a back reference (see next section) |
a back reference (see next section) |
| 1300 |
a parenthesized subpattern (unless it is an assertion) |
a parenthesized subpattern (unless it is an assertion) |
| 1301 |
|
a recursive or "subroutine" call to a subpattern |
| 1302 |
</pre> |
</pre> |
| 1303 |
The general repetition quantifier specifies a minimum and maximum number of |
The general repetition quantifier specifies a minimum and maximum number of |
| 1304 |
permitted matches, by giving the two numbers in curly brackets (braces), |
permitted matches, by giving the two numbers in curly brackets (braces), |
| 1332 |
</P> |
</P> |
| 1333 |
<P> |
<P> |
| 1334 |
The quantifier {0} is permitted, causing the expression to behave as if the |
The quantifier {0} is permitted, causing the expression to behave as if the |
| 1335 |
previous item and the quantifier were not present. |
previous item and the quantifier were not present. This may be useful for |
| 1336 |
|
subpatterns that are referenced as |
| 1337 |
|
<a href="#subpatternsassubroutines">subroutines</a> |
| 1338 |
|
from elsewhere in the pattern. Items other than subpatterns that have a {0} |
| 1339 |
|
quantifier are omitted from the compiled pattern. |
| 1340 |
</P> |
</P> |
| 1341 |
<P> |
<P> |
| 1342 |
For convenience, the three most common quantifiers have single-character |
For convenience, the three most common quantifiers have single-character |
| 1416 |
</P> |
</P> |
| 1417 |
<P> |
<P> |
| 1418 |
However, there is one situation where the optimization cannot be used. When .* |
However, there is one situation where the optimization cannot be used. When .* |
| 1419 |
is inside capturing parentheses that are the subject of a backreference |
is inside capturing parentheses that are the subject of a back reference |
| 1420 |
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 |
| 1421 |
succeeds. Consider, for example: |
succeeds. Consider, for example: |
| 1422 |
<pre> |
<pre> |
| 1440 |
</pre> |
</pre> |
| 1441 |
matches "aba" the value of the second captured substring is "b". |
matches "aba" the value of the second captured substring is "b". |
| 1442 |
<a name="atomicgroup"></a></P> |
<a name="atomicgroup"></a></P> |
| 1443 |
<br><a name="SEC14" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br> |
<br><a name="SEC16" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br> |
| 1444 |
<P> |
<P> |
| 1445 |
With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy") |
With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy") |
| 1446 |
repetition, failure of what follows normally causes the repeated item to be |
repetition, failure of what follows normally causes the repeated item to be |
| 1494 |
<pre> |
<pre> |
| 1495 |
\d++foo |
\d++foo |
| 1496 |
</pre> |
</pre> |
| 1497 |
|
Note that a possessive quantifier can be used with an entire group, for |
| 1498 |
|
example: |
| 1499 |
|
<pre> |
| 1500 |
|
(abc|xyz){2,3}+ |
| 1501 |
|
</pre> |
| 1502 |
Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY |
Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY |
| 1503 |
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 |
| 1504 |
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 |
| 1544 |
</pre> |
</pre> |
| 1545 |
sequences of non-digits cannot be broken, and failure happens quickly. |
sequences of non-digits cannot be broken, and failure happens quickly. |
| 1546 |
<a name="backreferences"></a></P> |
<a name="backreferences"></a></P> |
| 1547 |
<br><a name="SEC15" href="#TOC1">BACK REFERENCES</a><br> |
<br><a name="SEC17" href="#TOC1">BACK REFERENCES</a><br> |
| 1548 |
<P> |
<P> |
| 1549 |
Outside a character class, a backslash followed by a digit greater than 0 (and |
Outside a character class, a backslash followed by a digit greater than 0 (and |
| 1550 |
possibly further digits) is a back reference to a capturing subpattern earlier |
possibly further digits) is a back reference to a capturing subpattern earlier |
| 1573 |
<P> |
<P> |
| 1574 |
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 |
| 1575 |
backslash is to use the \g escape sequence, which is a feature introduced in |
backslash is to use the \g escape sequence, which is a feature introduced in |
| 1576 |
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 |
| 1577 |
optionally enclosed in braces. These examples are all identical: |
number, optionally enclosed in braces. These examples are all identical: |
| 1578 |
<pre> |
<pre> |
| 1579 |
(ring), \1 |
(ring), \1 |
| 1580 |
(ring), \g1 |
(ring), \g1 |
| 1581 |
(ring), \g{1} |
(ring), \g{1} |
| 1582 |
</pre> |
</pre> |
| 1583 |
A positive number specifies an absolute reference without the ambiguity that is |
An unsigned number specifies an absolute reference without the ambiguity that |
| 1584 |
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 |
| 1585 |
reference. A negative number is a relative reference. Consider this example: |
the reference. A negative number is a relative reference. Consider this |
| 1586 |
|
example: |
| 1587 |
<pre> |
<pre> |
| 1588 |
(abc(def)ghi)\g{-1} |
(abc(def)ghi)\g{-1} |
| 1589 |
</pre> |
</pre> |
| 1612 |
capturing subpattern is matched caselessly. |
capturing subpattern is matched caselessly. |
| 1613 |
</P> |
</P> |
| 1614 |
<P> |
<P> |
| 1615 |
Back references to named subpatterns use the Perl syntax \k<name> or \k'name' |
There are several different ways of writing back references to named |
| 1616 |
or the Python syntax (?P=name). We could rewrite the above example in either of |
subpatterns. The .NET syntax \k{name} and the Perl syntax \k<name> or |
| 1617 |
|
\k'name' are supported, as is the Python syntax (?P=name). Perl 5.10's unified |
| 1618 |
|
back reference syntax, in which \g can be used for both numeric and named |
| 1619 |
|
references, is also supported. We could rewrite the above example in any of |
| 1620 |
the following ways: |
the following ways: |
| 1621 |
<pre> |
<pre> |
| 1622 |
(?<p1>(?i)rah)\s+\k<p1> |
(?<p1>(?i)rah)\s+\k<p1> |
| 1623 |
|
(?'p1'(?i)rah)\s+\k{p1} |
| 1624 |
(?P<p1>(?i)rah)\s+(?P=p1) |
(?P<p1>(?i)rah)\s+(?P=p1) |
| 1625 |
|
(?<p1>(?i)rah)\s+\g{p1} |
| 1626 |
</pre> |
</pre> |
| 1627 |
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 |
| 1628 |
after the reference. |
after the reference. |
| 1630 |
<P> |
<P> |
| 1631 |
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 |
| 1632 |
subpattern has not actually been used in a particular match, any back |
subpattern has not actually been used in a particular match, any back |
| 1633 |
references to it always fail. For example, the pattern |
references to it always fail by default. For example, the pattern |
| 1634 |
<pre> |
<pre> |
| 1635 |
(a|(bc))\2 |
(a|(bc))\2 |
| 1636 |
</pre> |
</pre> |
| 1637 |
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 |
| 1638 |
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 |
| 1639 |
taken as part of a potential back reference number. If the pattern continues |
unset value matches an empty string. |
| 1640 |
with a digit character, some delimiter must be used to terminate the back |
</P> |
| 1641 |
reference. If the PCRE_EXTENDED option is set, this can be whitespace. |
<P> |
| 1642 |
Otherwise an empty comment (see |
Because there may be many capturing parentheses in a pattern, all digits |
| 1643 |
|
following a backslash are taken as part of a potential back reference number. |
| 1644 |
|
If the pattern continues with a digit character, some delimiter must be used to |
| 1645 |
|
terminate the back reference. If the PCRE_EXTENDED option is set, this can be |
| 1646 |
|
whitespace. Otherwise, the \g{ syntax or an empty comment (see |
| 1647 |
<a href="#comments">"Comments"</a> |
<a href="#comments">"Comments"</a> |
| 1648 |
below) can be used. |
below) can be used. |
| 1649 |
</P> |
</P> |
| 1650 |
|
<br><b> |
| 1651 |
|
Recursive back references |
| 1652 |
|
</b><br> |
| 1653 |
<P> |
<P> |
| 1654 |
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 |
| 1655 |
when the subpattern is first used, so, for example, (a\1) never matches. |
when the subpattern is first used, so, for example, (a\1) never matches. |
| 1664 |
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 |
| 1665 |
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 |
| 1666 |
minimum of zero. |
minimum of zero. |
| 1667 |
|
</P> |
| 1668 |
|
<P> |
| 1669 |
|
Back references of this type cause the group that they reference to be treated |
| 1670 |
|
as an |
| 1671 |
|
<a href="#atomicgroup">atomic group.</a> |
| 1672 |
|
Once the whole group has been matched, a subsequent matching failure cannot |
| 1673 |
|
cause backtracking into the middle of the group. |
| 1674 |
<a name="bigassertions"></a></P> |
<a name="bigassertions"></a></P> |
| 1675 |
<br><a name="SEC16" href="#TOC1">ASSERTIONS</a><br> |
<br><a name="SEC18" href="#TOC1">ASSERTIONS</a><br> |
| 1676 |
<P> |
<P> |
| 1677 |
An assertion is a test on the characters following or preceding the current |
An assertion is a test on the characters following or preceding the current |
| 1678 |
matching point that does not actually consume any characters. The simple |
matching point that does not actually consume any characters. The simple |
| 1721 |
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 |
| 1722 |
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 |
| 1723 |
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. |
| 1724 |
|
The Perl 5.10 backtracking control verb (*FAIL) or (*F) is essentially a |
| 1725 |
|
synonym for (?!). |
| 1726 |
<a name="lookbehind"></a></P> |
<a name="lookbehind"></a></P> |
| 1727 |
<br><b> |
<br><b> |
| 1728 |
Lookbehind assertions |
Lookbehind assertions |
| 1746 |
</pre> |
</pre> |
| 1747 |
causes an error at compile time. Branches that match different length strings |
causes an error at compile time. Branches that match different length strings |
| 1748 |
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 |
| 1749 |
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 |
| 1750 |
match the same length of string. An assertion such as |
match the same length of string. An assertion such as |
| 1751 |
<pre> |
<pre> |
| 1752 |
(?<=ab(c|de)) |
(?<=ab(c|de)) |
| 1753 |
</pre> |
</pre> |
| 1754 |
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 |
| 1755 |
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 |
| 1756 |
|
branches: |
| 1757 |
<pre> |
<pre> |
| 1758 |
(?<=abc|abde) |
(?<=abc|abde) |
| 1759 |
</pre> |
</pre> |
| 1760 |
|
In some cases, the Perl 5.10 escape sequence \K |
| 1761 |
|
<a href="#resetmatchstart">(see above)</a> |
| 1762 |
|
can be used instead of a lookbehind assertion to get round the fixed-length |
| 1763 |
|
restriction. |
| 1764 |
|
</P> |
| 1765 |
|
<P> |
| 1766 |
The implementation of lookbehind assertions is, for each alternative, to |
The implementation of lookbehind assertions is, for each alternative, to |
| 1767 |
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 |
| 1768 |
match. If there are insufficient characters before the current position, the |
match. If there are insufficient characters before the current position, the |
| 1775 |
different numbers of bytes, are also not permitted. |
different numbers of bytes, are also not permitted. |
| 1776 |
</P> |
</P> |
| 1777 |
<P> |
<P> |
| 1778 |
|
<a href="#subpatternsassubroutines">"Subroutine"</a> |
| 1779 |
|
calls (see below) such as (?2) or (?&X) are permitted in lookbehinds, as long |
| 1780 |
|
as the subpattern matches a fixed-length string. |
| 1781 |
|
<a href="#recursion">Recursion,</a> |
| 1782 |
|
however, is not supported. |
| 1783 |
|
</P> |
| 1784 |
|
<P> |
| 1785 |
Possessive quantifiers can be used in conjunction with lookbehind assertions to |
Possessive quantifiers can be used in conjunction with lookbehind assertions to |
| 1786 |
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 |
| 1787 |
pattern such as |
strings. Consider a simple pattern such as |
| 1788 |
<pre> |
<pre> |
| 1789 |
abcd$ |
abcd$ |
| 1790 |
</pre> |
</pre> |
| 1842 |
is another pattern that matches "foo" preceded by three digits and any three |
is another pattern that matches "foo" preceded by three digits and any three |
| 1843 |
characters that are not "999". |
characters that are not "999". |
| 1844 |
<a name="conditions"></a></P> |
<a name="conditions"></a></P> |
| 1845 |
<br><a name="SEC17" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br> |
<br><a name="SEC19" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br> |
| 1846 |
<P> |
<P> |
| 1847 |
It is possible to cause the matching process to obey a subpattern |
It is possible to cause the matching process to obey a subpattern |
| 1848 |
conditionally or to choose between two alternative subpatterns, depending on |
conditionally or to choose between two alternative subpatterns, depending on |
| 1849 |
the result of an assertion, or whether a previous capturing subpattern matched |
the result of an assertion, or whether a specific capturing subpattern has |
| 1850 |
or not. The two possible forms of conditional subpattern are |
already been matched. The two possible forms of conditional subpattern are: |
| 1851 |
<pre> |
<pre> |
| 1852 |
(?(condition)yes-pattern) |
(?(condition)yes-pattern) |
| 1853 |
(?(condition)yes-pattern|no-pattern) |
(?(condition)yes-pattern|no-pattern) |
| 1865 |
</b><br> |
</b><br> |
| 1866 |
<P> |
<P> |
| 1867 |
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 |
| 1868 |
condition is true if the capturing subpattern of that number has previously |
condition is true if a capturing subpattern of that number has previously |
| 1869 |
matched. |
matched. If there is more than one capturing subpattern with the same number |
| 1870 |
|
(see the earlier |
| 1871 |
|
<a href="#recursion">section about duplicate subpattern numbers),</a> |
| 1872 |
|
the condition is true if any of them have been set. An alternative notation is |
| 1873 |
|
to precede the digits with a plus or minus sign. In this case, the subpattern |
| 1874 |
|
number is relative rather than absolute. The most recently opened parentheses |
| 1875 |
|
can be referenced by (?(-1), the next most recent by (?(-2), and so on. In |
| 1876 |
|
looping constructs it can also make sense to refer to subsequent groups with |
| 1877 |
|
constructs such as (?(+2). |
| 1878 |
</P> |
</P> |
| 1879 |
<P> |
<P> |
| 1880 |
Consider the following pattern, which contains non-significant white space to |
Consider the following pattern, which contains non-significant white space to |
| 1893 |
subpattern matches nothing. In other words, this pattern matches a sequence of |
subpattern matches nothing. In other words, this pattern matches a sequence of |
| 1894 |
non-parentheses, optionally enclosed in parentheses. |
non-parentheses, optionally enclosed in parentheses. |
| 1895 |
</P> |
</P> |
| 1896 |
|
<P> |
| 1897 |
|
If you were embedding this pattern in a larger one, you could use a relative |
| 1898 |
|
reference: |
| 1899 |
|
<pre> |
| 1900 |
|
...other stuff... ( \( )? [^()]+ (?(-1) \) ) ... |
| 1901 |
|
</pre> |
| 1902 |
|
This makes the fragment independent of the parentheses in the larger pattern. |
| 1903 |
|
</P> |
| 1904 |
<br><b> |
<br><b> |
| 1905 |
Checking for a used subpattern by name |
Checking for a used subpattern by name |
| 1906 |
</b><br> |
</b><br> |
| 1918 |
Rewriting the above example to use a named subpattern gives this: |
Rewriting the above example to use a named subpattern gives this: |
| 1919 |
<pre> |
<pre> |
| 1920 |
(?<OPEN> \( )? [^()]+ (?(<OPEN>) \) ) |
(?<OPEN> \( )? [^()]+ (?(<OPEN>) \) ) |
| 1921 |
|
</pre> |
| 1922 |
</PRE> |
If the name used in a condition of this kind is a duplicate, the test is |
| 1923 |
|
applied to all subpatterns of the same name, and is true if any one of them has |
| 1924 |
|
matched. |
| 1925 |
</P> |
</P> |
| 1926 |
<br><b> |
<br><b> |
| 1927 |
Checking for pattern recursion |
Checking for pattern recursion |
| 1934 |
<pre> |
<pre> |
| 1935 |
(?(R3)...) or (?(R&name)...) |
(?(R3)...) or (?(R&name)...) |
| 1936 |
</pre> |
</pre> |
| 1937 |
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 |
| 1938 |
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 |
| 1939 |
stack. |
stack. If the name used in a condition of this kind is a duplicate, the test is |
| 1940 |
|
applied to all subpatterns of the same name, and is true if any one of them is |
| 1941 |
|
the most recent recursion. |
| 1942 |
</P> |
</P> |
| 1943 |
<P> |
<P> |
| 1944 |
At "top level", all these recursion test conditions are false. Recursive |
At "top level", all these recursion test conditions are false. |
| 1945 |
patterns are described below. |
<a href="#recursion">The syntax for recursive patterns</a> |
| 1946 |
|
is described below. |
| 1947 |
</P> |
</P> |
| 1948 |
<br><b> |
<br><b> |
| 1949 |
Defining subpatterns for use by reference only |
Defining subpatterns for use by reference only |
| 1953 |
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 |
| 1954 |
alternative in the subpattern. It is always skipped if control reaches this |
alternative in the subpattern. It is always skipped if control reaches this |
| 1955 |
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 |
| 1956 |
"subroutines" that can be referenced from elsewhere. (The use of "subroutines" |
"subroutines" that can be referenced from elsewhere. (The use of |
| 1957 |
|
<a href="#subpatternsassubroutines">"subroutines"</a> |
| 1958 |
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 |
| 1959 |
written like this (ignore whitespace and line breaks): |
written like this (ignore whitespace and line breaks): |
| 1960 |
<pre> |
<pre> |
| 1964 |
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 |
| 1965 |
named "byte" is defined. This matches an individual component of an IPv4 |
named "byte" is defined. This matches an individual component of an IPv4 |
| 1966 |
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 |
| 1967 |
pattern is skipped because DEFINE acts like a false condition. |
pattern is skipped because DEFINE acts like a false condition. The rest of the |
| 1968 |
</P> |
pattern uses references to the named group to match the four dot-separated |
| 1969 |
<P> |
components of an IPv4 address, insisting on a word boundary at each end. |
|
The rest of the pattern uses references to the named group to match the four |
|
|
dot-separated components of an IPv4 address, insisting on a word boundary at |
|
|
each end. |
|
| 1970 |
</P> |
</P> |
| 1971 |
<br><b> |
<br><b> |
| 1972 |
Assertion conditions |
Assertion conditions |
| 1987 |
against the second. This pattern matches strings in one of the two forms |
against the second. This pattern matches strings in one of the two forms |
| 1988 |
dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits. |
dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits. |
| 1989 |
<a name="comments"></a></P> |
<a name="comments"></a></P> |
| 1990 |
<br><a name="SEC18" href="#TOC1">COMMENTS</a><br> |
<br><a name="SEC20" href="#TOC1">COMMENTS</a><br> |
| 1991 |
<P> |
<P> |
| 1992 |
The sequence (?# marks the start of a comment that continues up to the next |
The sequence (?# marks the start of a comment that continues up to the next |
| 1993 |
closing parenthesis. Nested parentheses are not permitted. The characters |
closing parenthesis. Nested parentheses are not permitted. The characters |
| 1998 |
character class introduces a comment that continues to immediately after the |
character class introduces a comment that continues to immediately after the |
| 1999 |
next newline in the pattern. |
next newline in the pattern. |
| 2000 |
<a name="recursion"></a></P> |
<a name="recursion"></a></P> |
| 2001 |
<br><a name="SEC19" href="#TOC1">RECURSIVE PATTERNS</a><br> |
<br><a name="SEC21" href="#TOC1">RECURSIVE PATTERNS</a><br> |
| 2002 |
<P> |
<P> |
| 2003 |
Consider the problem of matching a string in parentheses, allowing for |
Consider the problem of matching a string in parentheses, allowing for |
| 2004 |
unlimited nested parentheses. Without the use of recursion, the best that can |
unlimited nested parentheses. Without the use of recursion, the best that can |
| 2021 |
Obviously, PCRE cannot support the interpolation of Perl code. Instead, it |
Obviously, PCRE cannot support the interpolation of Perl code. Instead, it |
| 2022 |
supports special syntax for recursion of the entire pattern, and also for |
supports special syntax for recursion of the entire pattern, and also for |
| 2023 |
individual subpattern recursion. After its introduction in PCRE and Python, |
individual subpattern recursion. After its introduction in PCRE and Python, |
| 2024 |
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. |
| 2025 |
</P> |
</P> |
| 2026 |
<P> |
<P> |
| 2027 |
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 |
| 2028 |
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, |
| 2029 |
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 |
| 2030 |
|
<a href="#subpatternsassubroutines">"subroutine"</a> |
| 2031 |
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 |
| 2032 |
a recursive call of the entire regular expression. |
a recursive call of the entire regular expression. |
| 2033 |
</P> |
</P> |
| 2034 |
<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> |
|
|
<P> |
|
| 2035 |
This PCRE pattern solves the nested parentheses problem (assume the |
This PCRE pattern solves the nested parentheses problem (assume the |
| 2036 |
PCRE_EXTENDED option is set so that white space is ignored): |
PCRE_EXTENDED option is set so that white space is ignored): |
| 2037 |
<pre> |
<pre> |
| 2038 |
\( ( (?>[^()]+) | (?R) )* \) |
\( ( [^()]++ | (?R) )* \) |
| 2039 |
</pre> |
</pre> |
| 2040 |
First it matches an opening parenthesis. Then it matches any number of |
First it matches an opening parenthesis. Then it matches any number of |
| 2041 |
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 |
| 2042 |
match of the pattern itself (that is, a correctly parenthesized substring). |
match of the pattern itself (that is, a correctly parenthesized substring). |
| 2043 |
Finally there is a closing parenthesis. |
Finally there is a closing parenthesis. Note the use of a possessive quantifier |
| 2044 |
|
to avoid backtracking into sequences of non-parentheses. |
| 2045 |
</P> |
</P> |
| 2046 |
<P> |
<P> |
| 2047 |
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 |
| 2048 |
pattern, so instead you could use this: |
pattern, so instead you could use this: |
| 2049 |
<pre> |
<pre> |
| 2050 |
( \( ( (?>[^()]+) | (?1) )* \) ) |
( \( ( [^()]++ | (?1) )* \) ) |
| 2051 |
</pre> |
</pre> |
| 2052 |
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 |
| 2053 |
them instead of the whole pattern. In a larger pattern, keeping track of |
them instead of the whole pattern. |
| 2054 |
parenthesis numbers can be tricky. It may be more convenient to use named |
</P> |
| 2055 |
parentheses instead. The Perl syntax for this is (?&name); PCRE's earlier |
<P> |
| 2056 |
syntax (?P>name) is also supported. We could rewrite the above example as |
In a larger pattern, keeping track of parenthesis numbers can be tricky. This |
| 2057 |
follows: |
is made easier by the use of relative references (a Perl 5.10 feature). |
| 2058 |
|
Instead of (?1) in the pattern above you can write (?-2) to refer to the second |
| 2059 |
|
most recently opened parentheses preceding the recursion. In other words, a |
| 2060 |
|
negative number counts capturing parentheses leftwards from the point at which |
| 2061 |
|
it is encountered. |
| 2062 |
|
</P> |
| 2063 |
|
<P> |
| 2064 |
|
It is also possible to refer to subsequently opened parentheses, by writing |
| 2065 |
|
references such as (?+2). However, these cannot be recursive because the |
| 2066 |
|
reference is not inside the parentheses that are referenced. They are always |
| 2067 |
|
<a href="#subpatternsassubroutines">"subroutine"</a> |
| 2068 |
|
calls, as described in the next section. |
| 2069 |
|
</P> |
| 2070 |
|
<P> |
| 2071 |
|
An alternative approach is to use named parentheses instead. The Perl syntax |
| 2072 |
|
for this is (?&name); PCRE's earlier syntax (?P>name) is also supported. We |
| 2073 |
|
could rewrite the above example as follows: |
| 2074 |
<pre> |
<pre> |
| 2075 |
(?<pn> \( ( (?>[^()]+) | (?&pn) )* \) ) |
(?<pn> \( ( [^()]++ | (?&pn) )* \) ) |
| 2076 |
</pre> |
</pre> |
| 2077 |
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 |
| 2078 |
used. This particular example pattern contains nested unlimited repeats, and so |
used. |
| 2079 |
the use of atomic grouping for matching strings of non-parentheses is important |
</P> |
| 2080 |
when applying the pattern to strings that do not match. For example, when this |
<P> |
| 2081 |
pattern is applied to |
This particular example pattern that we have been looking at contains nested |
| 2082 |
|
unlimited repeats, and so the use of a possessive quantifier for matching |
| 2083 |
|
strings of non-parentheses is important when applying the pattern to strings |
| 2084 |
|
that do not match. For example, when this pattern is applied to |
| 2085 |
<pre> |
<pre> |
| 2086 |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
| 2087 |
</pre> |
</pre> |
| 2088 |
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, |
| 2089 |
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 |
| 2090 |
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 |
| 2091 |
before failure can be reported. |
before failure can be reported. |
| 2092 |
</P> |
</P> |
| 2093 |
<P> |
<P> |
| 2094 |
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 |
| 2095 |
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 |
| 2096 |
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 |
|
| 2097 |
<a href="pcrecallout.html"><b>pcrecallout</b></a> |
<a href="pcrecallout.html"><b>pcrecallout</b></a> |
| 2098 |
documentation). If the pattern above is matched against |
documentation). If the pattern above is matched against |
| 2099 |
<pre> |
<pre> |
| 2100 |
(ab(cd)ef) |
(ab(cd)ef) |
| 2101 |
</pre> |
</pre> |
| 2102 |
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 |
| 2103 |
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 |
| 2104 |
<pre> |
matched at the top level, its final value is unset, even if it is (temporarily) |
| 2105 |
\( ( ( (?>[^()]+) | (?R) )* ) \) |
set at a deeper level. |
| 2106 |
^ ^ |
</P> |
| 2107 |
^ ^ |
<P> |
| 2108 |
</pre> |
If there are more than 15 capturing parentheses in a pattern, PCRE has to |
| 2109 |
the string they capture is "ab(cd)ef", the contents of the top level |
obtain extra memory to store data during a recursion, which it does by using |
| 2110 |
parentheses. If there are more than 15 capturing parentheses in a pattern, PCRE |
<b>pcre_malloc</b>, freeing it via <b>pcre_free</b> afterwards. If no memory can |
| 2111 |
has to obtain extra memory to store data during a recursion, which it does by |
be obtained, the match fails with the PCRE_ERROR_NOMEMORY error. |
|
using <b>pcre_malloc</b>, freeing it via <b>pcre_free</b> afterwards. If no |
|
|
memory can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error. |
|
| 2112 |
</P> |
</P> |
| 2113 |
<P> |
<P> |
| 2114 |
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. |
| 2121 |
In this pattern, (?(R) is the start of a conditional subpattern, with two |
In this pattern, (?(R) is the start of a conditional subpattern, with two |
| 2122 |
different alternatives for the recursive and non-recursive cases. The (?R) item |
different alternatives for the recursive and non-recursive cases. The (?R) item |
| 2123 |
is the actual recursive call. |
is the actual recursive call. |
| 2124 |
|
<a name="recursiondifference"></a></P> |
| 2125 |
|
<br><b> |
| 2126 |
|
Recursion difference from Perl |
| 2127 |
|
</b><br> |
| 2128 |
|
<P> |
| 2129 |
|
In PCRE (like Python, but unlike Perl), a recursive subpattern call is always |
| 2130 |
|
treated as an atomic group. That is, once it has matched some of the subject |
| 2131 |
|
string, it is never re-entered, even if it contains untried alternatives and |
| 2132 |
|
there is a subsequent matching failure. This can be illustrated by the |
| 2133 |
|
following pattern, which purports to match a palindromic string that contains |
| 2134 |
|
an odd number of characters (for example, "a", "aba", "abcba", "abcdcba"): |
| 2135 |
|
<pre> |
| 2136 |
|
^(.|(.)(?1)\2)$ |
| 2137 |
|
</pre> |
| 2138 |
|
The idea is that it either matches a single character, or two identical |
| 2139 |
|
characters surrounding a sub-palindrome. In Perl, this pattern works; in PCRE |
| 2140 |
|
it does not if the pattern is longer than three characters. Consider the |
| 2141 |
|
subject string "abcba": |
| 2142 |
|
</P> |
| 2143 |
|
<P> |
| 2144 |
|
At the top level, the first character is matched, but as it is not at the end |
| 2145 |
|
of the string, the first alternative fails; the second alternative is taken |
| 2146 |
|
and the recursion kicks in. The recursive call to subpattern 1 successfully |
| 2147 |
|
matches the next character ("b"). (Note that the beginning and end of line |
| 2148 |
|
tests are not part of the recursion). |
| 2149 |
|
</P> |
| 2150 |
|
<P> |
| 2151 |
|
Back at the top level, the next character ("c") is compared with what |
| 2152 |
|
subpattern 2 matched, which was "a". This fails. Because the recursion is |
| 2153 |
|
treated as an atomic group, there are now no backtracking points, and so the |
| 2154 |
|
entire match fails. (Perl is able, at this point, to re-enter the recursion and |
| 2155 |
|
try the second alternative.) However, if the pattern is written with the |
| 2156 |
|
alternatives in the other order, things are different: |
| 2157 |
|
<pre> |
| 2158 |
|
^((.)(?1)\2|.)$ |
| 2159 |
|
</pre> |
| 2160 |
|
This time, the recursing alternative is tried first, and continues to recurse |
| 2161 |
|
until it runs out of characters, at which point the recursion fails. But this |
| 2162 |
|
time we do have another alternative to try at the higher level. That is the big |
| 2163 |
|
difference: in the previous case the remaining alternative is at a deeper |
| 2164 |
|
recursion level, which PCRE cannot use. |
| 2165 |
|
</P> |
| 2166 |
|
<P> |
| 2167 |
|
To change the pattern so that matches all palindromic strings, not just those |
| 2168 |
|
with an odd number of characters, it is tempting to change the pattern to this: |
| 2169 |
|
<pre> |
| 2170 |
|
^((.)(?1)\2|.?)$ |
| 2171 |
|
</pre> |
| 2172 |
|
Again, this works in Perl, but not in PCRE, and for the same reason. When a |
| 2173 |
|
deeper recursion has matched a single character, it cannot be entered again in |
| 2174 |
|
order to match an empty string. The solution is to separate the two cases, and |
| 2175 |
|
write out the odd and even cases as alternatives at the higher level: |
| 2176 |
|
<pre> |
| 2177 |
|
^(?:((.)(?1)\2|)|((.)(?3)\4|.)) |
| 2178 |
|
</pre> |
| 2179 |
|
If you want to match typical palindromic phrases, the pattern has to ignore all |
| 2180 |
|
non-word characters, which can be done like this: |
| 2181 |
|
<pre> |
| 2182 |
|
^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$ |
| 2183 |
|
</pre> |
| 2184 |
|
If run with the PCRE_CASELESS option, this pattern matches phrases such as "A |
| 2185 |
|
man, a plan, a canal: Panama!" and it works well in both PCRE and Perl. Note |
| 2186 |
|
the use of the possessive quantifier *+ to avoid backtracking into sequences of |
| 2187 |
|
non-word characters. Without this, PCRE takes a great deal longer (ten times or |
| 2188 |
|
more) to match typical phrases, and Perl takes so long that you think it has |
| 2189 |
|
gone into a loop. |
| 2190 |
|
</P> |
| 2191 |
|
<P> |
| 2192 |
|
<b>WARNING</b>: The palindrome-matching patterns above work only if the subject |
| 2193 |
|
string does not start with a palindrome that is shorter than the entire string. |
| 2194 |
|
For example, although "abcba" is correctly matched, if the subject is "ababa", |
| 2195 |
|
PCRE finds the palindrome "aba" at the start, then fails at top level because |
| 2196 |
|
the end of the string does not follow. Once again, it cannot jump back into the |
| 2197 |
|
recursion to try other alternatives, so the entire match fails. |
| 2198 |
<a name="subpatternsassubroutines"></a></P> |
<a name="subpatternsassubroutines"></a></P> |
| 2199 |
<br><a name="SEC20" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br> |
<br><a name="SEC22" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br> |
| 2200 |
<P> |
<P> |
| 2201 |
If the syntax for a recursive subpattern reference (either by number or by |
If the syntax for a recursive subpattern reference (either by number or by |
| 2202 |
name) is used outside the parentheses to which it refers, it operates like a |
name) is used outside the parentheses to which it refers, it operates like a |
| 2203 |
subroutine in a programming language. The "called" subpattern may be defined |
subroutine in a programming language. The "called" subpattern may be defined |
| 2204 |
before or after the reference. An earlier example pointed out that the pattern |
before or after the reference. A numbered reference can be absolute or |
| 2205 |
|
relative, as in these examples: |
| 2206 |
|
<pre> |
| 2207 |
|
(...(absolute)...)...(?2)... |
| 2208 |
|
(...(relative)...)...(?-1)... |
| 2209 |
|
(...(?+1)...(relative)... |
| 2210 |
|
</pre> |
| 2211 |
|
An earlier example pointed out that the pattern |
| 2212 |
<pre> |
<pre> |
| 2213 |
(sens|respons)e and \1ibility |
(sens|respons)e and \1ibility |
| 2214 |
</pre> |
</pre> |
| 2221 |
strings. Another example is given in the discussion of DEFINE above. |
strings. Another example is given in the discussion of DEFINE above. |
| 2222 |
</P> |
</P> |
| 2223 |
<P> |
<P> |
| 2224 |
Like recursive subpatterns, a "subroutine" call is always treated as an atomic |
Like recursive subpatterns, a subroutine call is always treated as an atomic |
| 2225 |
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 |
| 2226 |
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 |
| 2227 |
matching failure. |
matching failure. Any capturing parentheses that are set during the subroutine |
| 2228 |
|
call revert to their previous values afterwards. |
| 2229 |
</P> |
</P> |
| 2230 |
<P> |
<P> |
| 2231 |
When a subpattern is used as a subroutine, processing options such as |
When a subpattern is used as a subroutine, processing options such as |
| 2232 |
case-independence are fixed when the subpattern is defined. They cannot be |
case-independence are fixed when the subpattern is defined. They cannot be |
| 2233 |
changed for different calls. For example, consider this pattern: |
changed for different calls. For example, consider this pattern: |
| 2234 |
<pre> |
<pre> |
| 2235 |
(abc)(?i:(?1)) |
(abc)(?i:(?-1)) |
| 2236 |
</pre> |
</pre> |
| 2237 |
It matches "abcabc". It does not match "abcABC" because the change of |
It matches "abcabc". It does not match "abcABC" because the change of |
| 2238 |
processing option does not affect the called subpattern. |
processing option does not affect the called subpattern. |
| 2239 |
|
<a name="onigurumasubroutines"></a></P> |
| 2240 |
|
<br><a name="SEC23" href="#TOC1">ONIGURUMA SUBROUTINE SYNTAX</a><br> |
| 2241 |
|
<P> |
| 2242 |
|
For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or |
| 2243 |
|
a number enclosed either in angle brackets or single quotes, is an alternative |
| 2244 |
|
syntax for referencing a subpattern as a subroutine, possibly recursively. Here |
| 2245 |
|
are two of the examples used above, rewritten using this syntax: |
| 2246 |
|
<pre> |
| 2247 |
|
(?<pn> \( ( (?>[^()]+) | \g<pn> )* \) ) |
| 2248 |
|
(sens|respons)e and \g'1'ibility |
| 2249 |
|
</pre> |
| 2250 |
|
PCRE supports an extension to Oniguruma: if a number is preceded by a |
| 2251 |
|
plus or a minus sign it is taken as a relative reference. For example: |
| 2252 |
|
<pre> |
| 2253 |
|
(abc)(?i:\g<-1>) |
| 2254 |
|
</pre> |
| 2255 |
|
Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are <i>not</i> |
| 2256 |
|
synonymous. The former is a back reference; the latter is a subroutine call. |
| 2257 |
</P> |
</P> |
| 2258 |
<br><a name="SEC21" href="#TOC1">CALLOUTS</a><br> |
<br><a name="SEC24" href="#TOC1">CALLOUTS</a><br> |
| 2259 |
<P> |
<P> |
| 2260 |
Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl |
Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl |
| 2261 |
code to be obeyed in the middle of matching a regular expression. This makes it |
code to be obeyed in the middle of matching a regular expression. This makes it |
| 2274 |
can put a number less than 256 after the letter C. The default value is zero. |
can put a number less than 256 after the letter C. The default value is zero. |
| 2275 |
For example, this pattern has two callout points: |
For example, this pattern has two callout points: |
| 2276 |
<pre> |
<pre> |
| 2277 |
(?C1)\dabc(?C2)def |
(?C1)abc(?C2)def |
| 2278 |
</pre> |
</pre> |
| 2279 |
If the PCRE_AUTO_CALLOUT flag is passed to <b>pcre_compile()</b>, callouts are |
If the PCRE_AUTO_CALLOUT flag is passed to <b>pcre_compile()</b>, callouts are |
| 2280 |
automatically installed before each item in the pattern. They are all numbered |
automatically installed before each item in the pattern. They are all numbered |
| 2290 |
<a href="pcrecallout.html"><b>pcrecallout</b></a> |
<a href="pcrecallout.html"><b>pcrecallout</b></a> |
| 2291 |
documentation. |
documentation. |
| 2292 |
</P> |
</P> |
| 2293 |
<br><a name="SEC22" href="#TOC1">SEE ALSO</a><br> |
<br><a name="SEC25" href="#TOC1">BACKTRACKING CONTROL</a><br> |
| 2294 |
|
<P> |
| 2295 |
|
Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which |
| 2296 |
|
are described in the Perl documentation as "experimental and subject to change |
| 2297 |
|
or removal in a future version of Perl". It goes on to say: "Their usage in |
| 2298 |
|
production code should be noted to avoid problems during upgrades." The same |
| 2299 |
|
remarks apply to the PCRE features described in this section. |
| 2300 |
|
</P> |
| 2301 |
|
<P> |
| 2302 |
|
Since these verbs are specifically related to backtracking, most of them can be |
| 2303 |
|
used only when the pattern is to be matched using <b>pcre_exec()</b>, which uses |
| 2304 |
|
a backtracking algorithm. With the exception of (*FAIL), which behaves like a |
| 2305 |
|
failing negative assertion, they cause an error if encountered by |
| 2306 |
|
<b>pcre_dfa_exec()</b>. |
| 2307 |
|
</P> |
| 2308 |
|
<P> |
| 2309 |
|
If any of these verbs are used in an assertion or subroutine subpattern |
| 2310 |
|
(including recursive subpatterns), their effect is confined to that subpattern; |
| 2311 |
|
it does not extend to the surrounding pattern. Note that such subpatterns are |
| 2312 |
|
processed as anchored at the point where they are tested. |
| 2313 |
|
</P> |
| 2314 |
|
<P> |
| 2315 |
|
The new verbs make use of what was previously invalid syntax: an opening |
| 2316 |
|
parenthesis followed by an asterisk. In Perl, they are generally of the form |
| 2317 |
|
(*VERB:ARG) but PCRE does not support the use of arguments, so its general |
| 2318 |
|
form is just (*VERB). Any number of these verbs may occur in a pattern. There |
| 2319 |
|
are two kinds: |
| 2320 |
|
</P> |
| 2321 |
|
<br><b> |
| 2322 |
|
Verbs that act immediately |
| 2323 |
|
</b><br> |
| 2324 |
|
<P> |
| 2325 |
|
The following verbs act as soon as they are encountered: |
| 2326 |
|
<pre> |
| 2327 |
|
(*ACCEPT) |
| 2328 |
|
</pre> |
| 2329 |
|
This verb causes the match to end successfully, skipping the remainder of the |
| 2330 |
|
pattern. When inside a recursion, only the innermost pattern is ended |
| 2331 |
|
immediately. If (*ACCEPT) is inside capturing parentheses, the data so far is |
| 2332 |
|
captured. (This feature was added to PCRE at release 8.00.) For example: |
| 2333 |
|
<pre> |
| 2334 |
|
A((?:A|B(*ACCEPT)|C)D) |
| 2335 |
|
</pre> |
| 2336 |
|
This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is captured by |
| 2337 |
|
the outer parentheses. |
| 2338 |
|
<pre> |
| 2339 |
|
(*FAIL) or (*F) |
| 2340 |
|
</pre> |
| 2341 |
|
This verb causes the match to fail, forcing backtracking to occur. It is |
| 2342 |
|
equivalent to (?!) but easier to read. The Perl documentation notes that it is |
| 2343 |
|
probably useful only when combined with (?{}) or (??{}). Those are, of course, |
| 2344 |
|
Perl features that are not present in PCRE. The nearest equivalent is the |
| 2345 |
|
callout feature, as for example in this pattern: |
| 2346 |
|
<pre> |
| 2347 |
|
a+(?C)(*FAIL) |
| 2348 |
|
</pre> |
| 2349 |
|
A match with the string "aaaa" always fails, but the callout is taken before |
| 2350 |
|
each backtrack happens (in this example, 10 times). |
| 2351 |
|
</P> |
| 2352 |
|
<br><b> |
| 2353 |
|
Verbs that act after backtracking |
| 2354 |
|
</b><br> |
| 2355 |
|
<P> |
| 2356 |
|
The following verbs do nothing when they are encountered. Matching continues |
| 2357 |
|
with what follows, but if there is no subsequent match, a failure is forced. |
| 2358 |
|
The verbs differ in exactly what kind of failure occurs. |
| 2359 |
|
<pre> |
| 2360 |
|
(*COMMIT) |
| 2361 |
|
</pre> |
| 2362 |
|
This verb causes the whole match to fail outright if the rest of the pattern |
| 2363 |
|
does not match. Even if the pattern is unanchored, no further attempts to find |
| 2364 |
|
a match by advancing the starting point take place. Once (*COMMIT) has been |
| 2365 |
|
passed, <b>pcre_exec()</b> is committed to finding a match at the current |
| 2366 |
|
starting point, or not at all. For example: |
| 2367 |
|
<pre> |
| 2368 |
|
a+(*COMMIT)b |
| 2369 |
|
</pre> |
| 2370 |
|
This matches "xxaab" but not "aacaab". It can be thought of as a kind of |
| 2371 |
|
dynamic anchor, or "I've started, so I must finish." |
| 2372 |
|
<pre> |
| 2373 |
|
(*PRUNE) |
| 2374 |
|
</pre> |
| 2375 |
|
This verb causes the match to fail at the current position if the rest of the |
| 2376 |
|
pattern does not match. If the pattern is unanchored, the normal "bumpalong" |
| 2377 |
|
advance to the next starting character then happens. Backtracking can occur as |
| 2378 |
|
usual to the left of (*PRUNE), or when matching to the right of (*PRUNE), but |
| 2379 |
|
if there is no match to the right, backtracking cannot cross (*PRUNE). |
| 2380 |
|
In simple cases, the use of (*PRUNE) is just an alternative to an atomic |
| 2381 |
|
group or possessive quantifier, but there are some uses of (*PRUNE) that cannot |
| 2382 |
|
be expressed in any other way. |
| 2383 |
|
<pre> |
| 2384 |
|
(*SKIP) |
| 2385 |
|
</pre> |
| 2386 |
|
This verb is like (*PRUNE), except that if the pattern is unanchored, the |
| 2387 |
|
"bumpalong" advance is not to the next character, but to the position in the |
| 2388 |
|
subject where (*SKIP) was encountered. (*SKIP) signifies that whatever text |
| 2389 |
|
was matched leading up to it cannot be part of a successful match. Consider: |
| 2390 |
|
<pre> |
| 2391 |
|
a+(*SKIP)b |
| 2392 |
|
</pre> |
| 2393 |
|
If the subject is "aaaac...", after the first match attempt fails (starting at |
| 2394 |
|
the first character in the string), the starting point skips on to start the |
| 2395 |
|
next attempt at "c". Note that a possessive quantifer does not have the same |
| 2396 |
|
effect as this example; although it would suppress backtracking during the |
| 2397 |
|
first match attempt, the second attempt would start at the second character |
| 2398 |
|
instead of skipping on to "c". |
| 2399 |
|
<pre> |
| 2400 |
|
(*THEN) |
| 2401 |
|
</pre> |
| 2402 |
|
This verb causes a skip to the next alternation if the rest of the pattern does |
| 2403 |
|
not match. That is, it cancels pending backtracking, but only within the |
| 2404 |
|
current alternation. Its name comes from the observation that it can be used |
| 2405 |
|
for a pattern-based if-then-else block: |
| 2406 |
|
<pre> |
| 2407 |
|
( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ... |
| 2408 |
|
</pre> |
| 2409 |
|
If the COND1 pattern matches, FOO is tried (and possibly further items after |
| 2410 |
|
the end of the group if FOO succeeds); on failure the matcher skips to the |
| 2411 |
|
second alternative and tries COND2, without backtracking into COND1. If (*THEN) |
| 2412 |
|
is used outside of any alternation, it acts exactly like (*PRUNE). |
| 2413 |
|
</P> |
| 2414 |
|
<br><a name="SEC26" href="#TOC1">SEE ALSO</a><br> |
| 2415 |
<P> |
<P> |
| 2416 |
<b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3), <b>pcre</b>(3). |
<b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3), |
| 2417 |
|
<b>pcresyntax</b>(3), <b>pcre</b>(3). |
| 2418 |
</P> |
</P> |
| 2419 |
<br><a name="SEC23" href="#TOC1">AUTHOR</a><br> |
<br><a name="SEC27" href="#TOC1">AUTHOR</a><br> |
| 2420 |
<P> |
<P> |
| 2421 |
Philip Hazel |
Philip Hazel |
| 2422 |
<br> |
<br> |
| 2425 |
Cambridge CB2 3QH, England. |
Cambridge CB2 3QH, England. |
| 2426 |
<br> |
<br> |
| 2427 |
</P> |
</P> |
| 2428 |
<br><a name="SEC24" href="#TOC1">REVISION</a><br> |
<br><a name="SEC28" href="#TOC1">REVISION</a><br> |
| 2429 |
<P> |
<P> |
| 2430 |
Last updated: 06 March 2007 |
Last updated: 06 March 2010 |
| 2431 |
<br> |
<br> |
| 2432 |
Copyright © 1997-2007 University of Cambridge. |
Copyright © 1997-2010 University of Cambridge. |
| 2433 |
<br> |
<br> |
| 2434 |
<p> |
<p> |
| 2435 |
Return to the <a href="index.html">PCRE index page</a>. |
Return to the <a href="index.html">PCRE index page</a>. |