| 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, |
| 75 |
PCRE when its main matching function, <b>pcre_exec()</b>, is used. |
PCRE when its main matching function, <b>pcre_exec()</b>, is used. |
| 76 |
From release 6.0, PCRE offers a second matching function, |
From release 6.0, PCRE offers a second matching function, |
| 77 |
<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 |
| 78 |
Perl-compatible. The advantages and disadvantages of the alternative function, |
Perl-compatible. Some of the features discussed below are not available when |
| 79 |
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 |
| 80 |
|
alternative function, and how it differs from the normal function, are |
| 81 |
|
discussed in the |
| 82 |
<a href="pcrematching.html"><b>pcrematching</b></a> |
<a href="pcrematching.html"><b>pcrematching</b></a> |
| 83 |
page. |
page. |
| 84 |
</P> |
</P> |
| 85 |
<br><a name="SEC2" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br> |
<br><a name="SEC2" href="#TOC1">NEWLINE CONVENTIONS</a><br> |
| 86 |
|
<P> |
| 87 |
|
PCRE supports five different conventions for indicating line breaks in |
| 88 |
|
strings: a single CR (carriage return) character, a single LF (linefeed) |
| 89 |
|
character, the two-character sequence CRLF, any of the three preceding, or any |
| 90 |
|
Unicode newline sequence. The |
| 91 |
|
<a href="pcreapi.html"><b>pcreapi</b></a> |
| 92 |
|
page has |
| 93 |
|
<a href="pcreapi.html#newlines">further discussion</a> |
| 94 |
|
about newlines, and shows how to set the newline convention in the |
| 95 |
|
<i>options</i> arguments for the compiling and matching functions. |
| 96 |
|
</P> |
| 97 |
|
<P> |
| 98 |
|
It is also possible to specify a newline convention by starting a pattern |
| 99 |
|
string with one of the following five sequences: |
| 100 |
|
<pre> |
| 101 |
|
(*CR) carriage return |
| 102 |
|
(*LF) linefeed |
| 103 |
|
(*CRLF) carriage return, followed by linefeed |
| 104 |
|
(*ANYCRLF) any of the three above |
| 105 |
|
(*ANY) all Unicode newline sequences |
| 106 |
|
</pre> |
| 107 |
|
These override the default and the options given to <b>pcre_compile()</b>. For |
| 108 |
|
example, on a Unix system where LF is the default newline sequence, the pattern |
| 109 |
|
<pre> |
| 110 |
|
(*CR)a.b |
| 111 |
|
</pre> |
| 112 |
|
changes the convention to CR. That pattern matches "a\nb" because LF is no |
| 113 |
|
longer a newline. Note that these special settings, which are not |
| 114 |
|
Perl-compatible, are recognized only at the very start of a pattern, and that |
| 115 |
|
they must be in upper case. If more than one of them is present, the last one |
| 116 |
|
is used. |
| 117 |
|
</P> |
| 118 |
|
<P> |
| 119 |
|
The newline convention does not affect what the \R escape sequence matches. By |
| 120 |
|
default, this is any Unicode newline sequence, for Perl compatibility. However, |
| 121 |
|
this can be changed; see the description of \R in the section entitled |
| 122 |
|
<a href="#newlineseq">"Newline sequences"</a> |
| 123 |
|
below. A change of \R setting can be combined with a change of newline |
| 124 |
|
convention. |
| 125 |
|
</P> |
| 126 |
|
<br><a name="SEC3" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br> |
| 127 |
<P> |
<P> |
| 128 |
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 |
| 129 |
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 |
| 180 |
</pre> |
</pre> |
| 181 |
The following sections describe the use of each of the metacharacters. |
The following sections describe the use of each of the metacharacters. |
| 182 |
</P> |
</P> |
| 183 |
<br><a name="SEC3" href="#TOC1">BACKSLASH</a><br> |
<br><a name="SEC4" href="#TOC1">BACKSLASH</a><br> |
| 184 |
<P> |
<P> |
| 185 |
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 |
| 186 |
non-alphanumeric character, it takes away any special meaning that character |
non-alphanumeric character, it takes away any special meaning that character |
| 229 |
\cx "control-x", where x is any character |
\cx "control-x", where x is any character |
| 230 |
\e escape (hex 1B) |
\e escape (hex 1B) |
| 231 |
\f formfeed (hex 0C) |
\f formfeed (hex 0C) |
| 232 |
\n newline (hex 0A) |
\n linefeed (hex 0A) |
| 233 |
\r carriage return (hex 0D) |
\r carriage return (hex 0D) |
| 234 |
\t tab (hex 09) |
\t tab (hex 09) |
| 235 |
\ddd character with octal code ddd, or backreference |
\ddd character with octal code ddd, or backreference |
| 245 |
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 |
| 246 |
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{ |
| 247 |
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 |
| 248 |
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 |
| 249 |
is 7FFFFFFF). If characters other than hexadecimal digits appear between \x{ |
hexadecimal is 7FFFFFFF. Note that this is bigger than the largest Unicode code |
| 250 |
and }, or if there is no terminating }, this form of escape is not recognized. |
point, which is 10FFFF. |
| 251 |
Instead, the initial \x will be interpreted as a basic hexadecimal escape, |
</P> |
| 252 |
with no following digits, giving a character whose value is zero. |
<P> |
| 253 |
|
If characters other than hexadecimal digits appear between \x{ and }, or if |
| 254 |
|
there is no terminating }, this form of escape is not recognized. Instead, the |
| 255 |
|
initial \x will be interpreted as a basic hexadecimal escape, with no |
| 256 |
|
following digits, giving a character whose value is zero. |
| 257 |
</P> |
</P> |
| 258 |
<P> |
<P> |
| 259 |
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 |
| 311 |
Absolute and relative back references |
Absolute and relative back references |
| 312 |
</b><br> |
</b><br> |
| 313 |
<P> |
<P> |
| 314 |
The sequence \g followed by a positive or negative number, optionally enclosed |
The sequence \g followed by an unsigned or a negative number, optionally |
| 315 |
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 |
| 316 |
discussed |
reference can be coded as \g{name}. Back references are discussed |
| 317 |
<a href="#backreferences">later,</a> |
<a href="#backreferences">later,</a> |
| 318 |
following the discussion of |
following the discussion of |
| 319 |
<a href="#subpattern">parenthesized subpatterns.</a> |
<a href="#subpattern">parenthesized subpatterns.</a> |
| 320 |
</P> |
</P> |
| 321 |
<br><b> |
<br><b> |
| 322 |
|
Absolute and relative subroutine calls |
| 323 |
|
</b><br> |
| 324 |
|
<P> |
| 325 |
|
For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or |
| 326 |
|
a number enclosed either in angle brackets or single quotes, is an alternative |
| 327 |
|
syntax for referencing a subpattern as a "subroutine". Details are discussed |
| 328 |
|
<a href="#onigurumasubroutines">later.</a> |
| 329 |
|
Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are <i>not</i> |
| 330 |
|
synonymous. The former is a back reference; the latter is a subroutine call. |
| 331 |
|
</P> |
| 332 |
|
<br><b> |
| 333 |
Generic character types |
Generic character types |
| 334 |
</b><br> |
</b><br> |
| 335 |
<P> |
<P> |
| 338 |
<pre> |
<pre> |
| 339 |
\d any decimal digit |
\d any decimal digit |
| 340 |
\D any character that is not a decimal digit |
\D any character that is not a decimal digit |
| 341 |
|
\h any horizontal whitespace character |
| 342 |
|
\H any character that is not a horizontal whitespace character |
| 343 |
\s any whitespace character |
\s any whitespace character |
| 344 |
\S any character that is not a whitespace character |
\S any character that is not a whitespace character |
| 345 |
|
\v any vertical whitespace character |
| 346 |
|
\V any character that is not a vertical whitespace character |
| 347 |
\w any "word" character |
\w any "word" character |
| 348 |
\W any "non-word" character |
\W any "non-word" character |
| 349 |
</pre> |
</pre> |
| 359 |
<P> |
<P> |
| 360 |
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). |
| 361 |
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 |
| 362 |
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 |
| 363 |
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 |
| 364 |
does.) |
does. |
| 365 |
|
</P> |
| 366 |
|
<P> |
| 367 |
|
In UTF-8 mode, characters with values greater than 128 never match \d, \s, or |
| 368 |
|
\w, and always match \D, \S, and \W. This is true even when Unicode |
| 369 |
|
character property support is available. These sequences retain their original |
| 370 |
|
meanings from before UTF-8 support was available, mainly for efficiency |
| 371 |
|
reasons. Note that this also affects \b, because it is defined in terms of \w |
| 372 |
|
and \W. |
| 373 |
|
</P> |
| 374 |
|
<P> |
| 375 |
|
The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to the |
| 376 |
|
other sequences, these do match certain high-valued codepoints in UTF-8 mode. |
| 377 |
|
The horizontal space characters are: |
| 378 |
|
<pre> |
| 379 |
|
U+0009 Horizontal tab |
| 380 |
|
U+0020 Space |
| 381 |
|
U+00A0 Non-break space |
| 382 |
|
U+1680 Ogham space mark |
| 383 |
|
U+180E Mongolian vowel separator |
| 384 |
|
U+2000 En quad |
| 385 |
|
U+2001 Em quad |
| 386 |
|
U+2002 En space |
| 387 |
|
U+2003 Em space |
| 388 |
|
U+2004 Three-per-em space |
| 389 |
|
U+2005 Four-per-em space |
| 390 |
|
U+2006 Six-per-em space |
| 391 |
|
U+2007 Figure space |
| 392 |
|
U+2008 Punctuation space |
| 393 |
|
U+2009 Thin space |
| 394 |
|
U+200A Hair space |
| 395 |
|
U+202F Narrow no-break space |
| 396 |
|
U+205F Medium mathematical space |
| 397 |
|
U+3000 Ideographic space |
| 398 |
|
</pre> |
| 399 |
|
The vertical space characters are: |
| 400 |
|
<pre> |
| 401 |
|
U+000A Linefeed |
| 402 |
|
U+000B Vertical tab |
| 403 |
|
U+000C Formfeed |
| 404 |
|
U+000D Carriage return |
| 405 |
|
U+0085 Next line |
| 406 |
|
U+2028 Line separator |
| 407 |
|
U+2029 Paragraph separator |
| 408 |
|
</PRE> |
| 409 |
</P> |
</P> |
| 410 |
<P> |
<P> |
| 411 |
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 |
| 417 |
<a href="pcreapi.html"><b>pcreapi</b></a> |
<a href="pcreapi.html"><b>pcreapi</b></a> |
| 418 |
page). For example, in a French locale such as "fr_FR" in Unix-like systems, |
page). For example, in a French locale such as "fr_FR" in Unix-like systems, |
| 419 |
or "french" in Windows, some character codes greater than 128 are used for |
or "french" in Windows, some character codes greater than 128 are used for |
| 420 |
accented letters, and these are matched by \w. |
accented letters, and these are matched by \w. The use of locales with Unicode |
| 421 |
</P> |
is discouraged. |
| 422 |
<P> |
<a name="newlineseq"></a></P> |
|
In UTF-8 mode, characters with values greater than 128 never match \d, \s, or |
|
|
\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> |
|
| 423 |
<br><b> |
<br><b> |
| 424 |
Newline sequences |
Newline sequences |
| 425 |
</b><br> |
</b><br> |
| 426 |
<P> |
<P> |
| 427 |
Outside a character class, the escape sequence \R matches any Unicode newline |
Outside a character class, by default, the escape sequence \R matches any |
| 428 |
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 |
| 429 |
the following: |
equivalent to the following: |
| 430 |
<pre> |
<pre> |
| 431 |
(?>\r\n|\n|\x0b|\f|\r|\x85) |
(?>\r\n|\n|\x0b|\f|\r|\x85) |
| 432 |
</pre> |
</pre> |
| 445 |
recognized. |
recognized. |
| 446 |
</P> |
</P> |
| 447 |
<P> |
<P> |
| 448 |
|
It is possible to restrict \R to match only CR, LF, or CRLF (instead of the |
| 449 |
|
complete set of Unicode line endings) by setting the option PCRE_BSR_ANYCRLF |
| 450 |
|
either at compile time or when the pattern is matched. (BSR is an abbrevation |
| 451 |
|
for "backslash R".) This can be made the default when PCRE is built; if this is |
| 452 |
|
the case, the other behaviour can be requested via the PCRE_BSR_UNICODE option. |
| 453 |
|
It is also possible to specify these settings by starting a pattern string with |
| 454 |
|
one of the following sequences: |
| 455 |
|
<pre> |
| 456 |
|
(*BSR_ANYCRLF) CR, LF, or CRLF only |
| 457 |
|
(*BSR_UNICODE) any Unicode newline sequence |
| 458 |
|
</pre> |
| 459 |
|
These override the default and the options given to <b>pcre_compile()</b>, but |
| 460 |
|
they can be overridden by options given to <b>pcre_exec()</b>. Note that these |
| 461 |
|
special settings, which are not Perl-compatible, are recognized only at the |
| 462 |
|
very start of a pattern, and that they must be in upper case. If more than one |
| 463 |
|
of them is present, the last one is used. They can be combined with a change of |
| 464 |
|
newline convention, for example, a pattern can start with: |
| 465 |
|
<pre> |
| 466 |
|
(*ANY)(*BSR_ANYCRLF) |
| 467 |
|
</pre> |
| 468 |
Inside a character class, \R matches the letter "R". |
Inside a character class, \R matches the letter "R". |
| 469 |
<a name="uniextseq"></a></P> |
<a name="uniextseq"></a></P> |
| 470 |
<br><b> |
<br><b> |
| 472 |
</b><br> |
</b><br> |
| 473 |
<P> |
<P> |
| 474 |
When PCRE is built with Unicode character property support, three additional |
When PCRE is built with Unicode character property support, three additional |
| 475 |
escape sequences to match character properties are available when UTF-8 mode |
escape sequences that match characters with specific properties are available. |
| 476 |
is selected. They are: |
When not in UTF-8 mode, these sequences are of course limited to testing |
| 477 |
|
characters whose codepoints are less than 256, but they do work in this mode. |
| 478 |
|
The extra escape sequences are: |
| 479 |
<pre> |
<pre> |
| 480 |
\p{<i>xx</i>} a character with the <i>xx</i> property |
\p{<i>xx</i>} a character with the <i>xx</i> property |
| 481 |
\P{<i>xx</i>} a character without the <i>xx</i> property |
\P{<i>xx</i>} a character without the <i>xx</i> property |
| 632 |
a modifier or "other". |
a modifier or "other". |
| 633 |
</P> |
</P> |
| 634 |
<P> |
<P> |
| 635 |
|
The Cs (Surrogate) property applies only to characters in the range U+D800 to |
| 636 |
|
U+DFFF. Such characters are not valid in UTF-8 strings (see RFC 3629) and so |
| 637 |
|
cannot be tested by PCRE, unless UTF-8 validity checking has been turned off |
| 638 |
|
(see the discussion of PCRE_NO_UTF8_CHECK in the |
| 639 |
|
<a href="pcreapi.html"><b>pcreapi</b></a> |
| 640 |
|
page). |
| 641 |
|
</P> |
| 642 |
|
<P> |
| 643 |
The long synonyms for these properties that Perl supports (such as \p{Letter}) |
The long synonyms for these properties that Perl supports (such as \p{Letter}) |
| 644 |
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 |
| 645 |
properties with "Is". |
properties with "Is". |
| 664 |
atomic group |
atomic group |
| 665 |
<a href="#atomicgroup">(see below).</a> |
<a href="#atomicgroup">(see below).</a> |
| 666 |
Characters with the "mark" property are typically accents that affect the |
Characters with the "mark" property are typically accents that affect the |
| 667 |
preceding character. |
preceding character. None of them have codepoints less than 256, so in |
| 668 |
|
non-UTF-8 mode \X matches any one character. |
| 669 |
</P> |
</P> |
| 670 |
<P> |
<P> |
| 671 |
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 |
| 672 |
a structure that contains data for over fifteen thousand characters. That is |
a structure that contains data for over fifteen thousand characters. That is |
| 673 |
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 |
| 674 |
properties in PCRE. |
properties in PCRE. |
| 675 |
|
<a name="resetmatchstart"></a></P> |
| 676 |
|
<br><b> |
| 677 |
|
Resetting the match start |
| 678 |
|
</b><br> |
| 679 |
|
<P> |
| 680 |
|
The escape sequence \K, which is a Perl 5.10 feature, causes any previously |
| 681 |
|
matched characters not to be included in the final matched sequence. For |
| 682 |
|
example, the pattern: |
| 683 |
|
<pre> |
| 684 |
|
foo\Kbar |
| 685 |
|
</pre> |
| 686 |
|
matches "foobar", but reports that it has matched "bar". This feature is |
| 687 |
|
similar to a lookbehind assertion |
| 688 |
|
<a href="#lookbehind">(described below).</a> |
| 689 |
|
However, in this case, the part of the subject before the real match does not |
| 690 |
|
have to be of fixed length, as lookbehind assertions do. The use of \K does |
| 691 |
|
not interfere with the setting of |
| 692 |
|
<a href="#subpattern">captured substrings.</a> |
| 693 |
|
For example, when the pattern |
| 694 |
|
<pre> |
| 695 |
|
(foo)\Kbar |
| 696 |
|
</pre> |
| 697 |
|
matches "foobar", the first substring is still set to "foo". |
| 698 |
<a name="smallassertions"></a></P> |
<a name="smallassertions"></a></P> |
| 699 |
<br><b> |
<br><b> |
| 700 |
Simple assertions |
Simple assertions |
| 756 |
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 |
| 757 |
regular expression. |
regular expression. |
| 758 |
</P> |
</P> |
| 759 |
<br><a name="SEC4" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br> |
<br><a name="SEC5" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br> |
| 760 |
<P> |
<P> |
| 761 |
Outside a character class, in the default matching mode, the circumflex |
Outside a character class, in the default matching mode, the circumflex |
| 762 |
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 |
| 810 |
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 |
| 811 |
\A it is always anchored, whether or not PCRE_MULTILINE is set. |
\A it is always anchored, whether or not PCRE_MULTILINE is set. |
| 812 |
</P> |
</P> |
| 813 |
<br><a name="SEC5" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br> |
<br><a name="SEC6" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br> |
| 814 |
<P> |
<P> |
| 815 |
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 |
| 816 |
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 |
| 835 |
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 |
| 836 |
special meaning in a character class. |
special meaning in a character class. |
| 837 |
</P> |
</P> |
| 838 |
<br><a name="SEC6" href="#TOC1">MATCHING A SINGLE BYTE</a><br> |
<br><a name="SEC7" href="#TOC1">MATCHING A SINGLE BYTE</a><br> |
| 839 |
<P> |
<P> |
| 840 |
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 |
| 841 |
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 |
| 850 |
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 |
| 851 |
the lookbehind. |
the lookbehind. |
| 852 |
<a name="characterclass"></a></P> |
<a name="characterclass"></a></P> |
| 853 |
<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> |
| 854 |
<P> |
<P> |
| 855 |
An opening square bracket introduces a character class, terminated by a closing |
An opening square bracket introduces a character class, terminated by a closing |
| 856 |
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. If a |
| 945 |
closing square bracket. However, escaping other non-alphanumeric characters |
closing square bracket. However, escaping other non-alphanumeric characters |
| 946 |
does no harm. |
does no harm. |
| 947 |
</P> |
</P> |
| 948 |
<br><a name="SEC8" href="#TOC1">POSIX CHARACTER CLASSES</a><br> |
<br><a name="SEC9" href="#TOC1">POSIX CHARACTER CLASSES</a><br> |
| 949 |
<P> |
<P> |
| 950 |
Perl supports the POSIX notation for character classes. This uses names |
Perl supports the POSIX notation for character classes. This uses names |
| 951 |
enclosed by [: and :] within the enclosing square brackets. PCRE also supports |
enclosed by [: and :] within the enclosing square brackets. PCRE also supports |
| 991 |
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 |
| 992 |
the POSIX character classes. |
the POSIX character classes. |
| 993 |
</P> |
</P> |
| 994 |
<br><a name="SEC9" href="#TOC1">VERTICAL BAR</a><br> |
<br><a name="SEC10" href="#TOC1">VERTICAL BAR</a><br> |
| 995 |
<P> |
<P> |
| 996 |
Vertical bar characters are used to separate alternative patterns. For example, |
Vertical bar characters are used to separate alternative patterns. For example, |
| 997 |
the pattern |
the pattern |
| 1006 |
"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 |
| 1007 |
alternative in the subpattern. |
alternative in the subpattern. |
| 1008 |
</P> |
</P> |
| 1009 |
<br><a name="SEC10" href="#TOC1">INTERNAL OPTION SETTING</a><br> |
<br><a name="SEC11" href="#TOC1">INTERNAL OPTION SETTING</a><br> |
| 1010 |
<P> |
<P> |
| 1011 |
The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and |
The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and |
| 1012 |
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 |
| 1013 |
Perl option letters enclosed between "(?" and ")". The option letters are |
the pattern by a sequence of Perl option letters enclosed between "(?" and ")". |
| 1014 |
|
The option letters are |
| 1015 |
<pre> |
<pre> |
| 1016 |
i for PCRE_CASELESS |
i for PCRE_CASELESS |
| 1017 |
m for PCRE_MULTILINE |
m for PCRE_MULTILINE |
| 1026 |
unset. |
unset. |
| 1027 |
</P> |
</P> |
| 1028 |
<P> |
<P> |
| 1029 |
|
The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be |
| 1030 |
|
changed in the same way as the Perl-compatible options by using the characters |
| 1031 |
|
J, U and X respectively. |
| 1032 |
|
</P> |
| 1033 |
|
<P> |
| 1034 |
When an option change occurs at top level (that is, not inside subpattern |
When an option change occurs at top level (that is, not inside subpattern |
| 1035 |
parentheses), the change applies to the remainder of the pattern that follows. |
parentheses), the change applies to the remainder of the pattern that follows. |
| 1036 |
If the change is placed right at the start of a pattern, PCRE extracts it into |
If the change is placed right at the start of a pattern, PCRE extracts it into |
| 1056 |
behaviour otherwise. |
behaviour otherwise. |
| 1057 |
</P> |
</P> |
| 1058 |
<P> |
<P> |
| 1059 |
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 |
| 1060 |
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 |
| 1061 |
J, U and X respectively. |
pattern can contain special leading sequences to override what the application |
| 1062 |
|
has set or what has been defaulted. Details are given in the section entitled |
| 1063 |
|
<a href="#newlineseq">"Newline sequences"</a> |
| 1064 |
|
above. |
| 1065 |
<a name="subpattern"></a></P> |
<a name="subpattern"></a></P> |
| 1066 |
<br><a name="SEC11" href="#TOC1">SUBPATTERNS</a><br> |
<br><a name="SEC12" href="#TOC1">SUBPATTERNS</a><br> |
| 1067 |
<P> |
<P> |
| 1068 |
Subpatterns are delimited by parentheses (round brackets), which can be nested. |
Subpatterns are delimited by parentheses (round brackets), which can be nested. |
| 1069 |
Turning part of a pattern into a subpattern does two things: |
Turning part of a pattern into a subpattern does two things: |
| 1117 |
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 |
| 1118 |
the above patterns match "SUNDAY" as well as "Saturday". |
the above patterns match "SUNDAY" as well as "Saturday". |
| 1119 |
</P> |
</P> |
| 1120 |
<br><a name="SEC12" href="#TOC1">NAMED SUBPATTERNS</a><br> |
<br><a name="SEC13" href="#TOC1">DUPLICATE SUBPATTERN NUMBERS</a><br> |
| 1121 |
|
<P> |
| 1122 |
|
Perl 5.10 introduced a feature whereby each alternative in a subpattern uses |
| 1123 |
|
the same numbers for its capturing parentheses. Such a subpattern starts with |
| 1124 |
|
(?| and is itself a non-capturing subpattern. For example, consider this |
| 1125 |
|
pattern: |
| 1126 |
|
<pre> |
| 1127 |
|
(?|(Sat)ur|(Sun))day |
| 1128 |
|
</pre> |
| 1129 |
|
Because the two alternatives are inside a (?| group, both sets of capturing |
| 1130 |
|
parentheses are numbered one. Thus, when the pattern matches, you can look |
| 1131 |
|
at captured substring number one, whichever alternative matched. This construct |
| 1132 |
|
is useful when you want to capture part, but not all, of one of a number of |
| 1133 |
|
alternatives. Inside a (?| group, parentheses are numbered as usual, but the |
| 1134 |
|
number is reset at the start of each branch. The numbers of any capturing |
| 1135 |
|
buffers that follow the subpattern start after the highest number used in any |
| 1136 |
|
branch. The following example is taken from the Perl documentation. |
| 1137 |
|
The numbers underneath show in which buffer the captured content will be |
| 1138 |
|
stored. |
| 1139 |
|
<pre> |
| 1140 |
|
# before ---------------branch-reset----------- after |
| 1141 |
|
/ ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
| 1142 |
|
# 1 2 2 3 2 3 4 |
| 1143 |
|
</pre> |
| 1144 |
|
A backreference or a recursive call to a numbered subpattern always refers to |
| 1145 |
|
the first one in the pattern with the given number. |
| 1146 |
|
</P> |
| 1147 |
|
<P> |
| 1148 |
|
An alternative approach to using this "branch reset" feature is to use |
| 1149 |
|
duplicate named subpatterns, as described in the next section. |
| 1150 |
|
</P> |
| 1151 |
|
<br><a name="SEC14" href="#TOC1">NAMED SUBPATTERNS</a><br> |
| 1152 |
<P> |
<P> |
| 1153 |
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 |
| 1154 |
to keep track of the numbers in complicated regular expressions. Furthermore, |
to keep track of the numbers in complicated regular expressions. Furthermore, |
| 1190 |
(?<DN>Sat)(?:urday)? |
(?<DN>Sat)(?:urday)? |
| 1191 |
</pre> |
</pre> |
| 1192 |
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. |
| 1193 |
|
(An alternative way of solving this problem is to use a "branch reset" |
| 1194 |
|
subpattern, as described in the previous section.) |
| 1195 |
|
</P> |
| 1196 |
|
<P> |
| 1197 |
The convenience function for extracting the data by name returns the substring |
The convenience function for extracting the data by name returns the substring |
| 1198 |
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 |
| 1199 |
matched. This saves searching to find which numbered subpattern it was. If you |
matched. This saves searching to find which numbered subpattern it was. If you |
| 1203 |
<a href="pcreapi.html"><b>pcreapi</b></a> |
<a href="pcreapi.html"><b>pcreapi</b></a> |
| 1204 |
documentation. |
documentation. |
| 1205 |
</P> |
</P> |
| 1206 |
<br><a name="SEC13" href="#TOC1">REPETITION</a><br> |
<P> |
| 1207 |
|
<b>Warning:</b> You cannot use different names to distinguish between two |
| 1208 |
|
subpatterns with the same number (see the previous section) because PCRE uses |
| 1209 |
|
only the numbers when matching. |
| 1210 |
|
</P> |
| 1211 |
|
<br><a name="SEC15" href="#TOC1">REPETITION</a><br> |
| 1212 |
<P> |
<P> |
| 1213 |
Repetition is specified by quantifiers, which can follow any of the following |
Repetition is specified by quantifiers, which can follow any of the following |
| 1214 |
items: |
items: |
| 1255 |
</P> |
</P> |
| 1256 |
<P> |
<P> |
| 1257 |
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 |
| 1258 |
previous item and the quantifier were not present. |
previous item and the quantifier were not present. This may be useful for |
| 1259 |
|
subpatterns that are referenced as |
| 1260 |
|
<a href="#subpatternsassubroutines">subroutines</a> |
| 1261 |
|
from elsewhere in the pattern. Items other than subpatterns that have a {0} |
| 1262 |
|
quantifier are omitted from the compiled pattern. |
| 1263 |
</P> |
</P> |
| 1264 |
<P> |
<P> |
| 1265 |
For convenience, the three most common quantifiers have single-character |
For convenience, the three most common quantifiers have single-character |
| 1363 |
</pre> |
</pre> |
| 1364 |
matches "aba" the value of the second captured substring is "b". |
matches "aba" the value of the second captured substring is "b". |
| 1365 |
<a name="atomicgroup"></a></P> |
<a name="atomicgroup"></a></P> |
| 1366 |
<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> |
| 1367 |
<P> |
<P> |
| 1368 |
With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy") |
With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy") |
| 1369 |
repetition, failure of what follows normally causes the repeated item to be |
repetition, failure of what follows normally causes the repeated item to be |
| 1417 |
<pre> |
<pre> |
| 1418 |
\d++foo |
\d++foo |
| 1419 |
</pre> |
</pre> |
| 1420 |
|
Note that a possessive quantifier can be used with an entire group, for |
| 1421 |
|
example: |
| 1422 |
|
<pre> |
| 1423 |
|
(abc|xyz){2,3}+ |
| 1424 |
|
</pre> |
| 1425 |
Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY |
Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY |
| 1426 |
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 |
| 1427 |
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 |
| 1467 |
</pre> |
</pre> |
| 1468 |
sequences of non-digits cannot be broken, and failure happens quickly. |
sequences of non-digits cannot be broken, and failure happens quickly. |
| 1469 |
<a name="backreferences"></a></P> |
<a name="backreferences"></a></P> |
| 1470 |
<br><a name="SEC15" href="#TOC1">BACK REFERENCES</a><br> |
<br><a name="SEC17" href="#TOC1">BACK REFERENCES</a><br> |
| 1471 |
<P> |
<P> |
| 1472 |
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 |
| 1473 |
possibly further digits) is a back reference to a capturing subpattern earlier |
possibly further digits) is a back reference to a capturing subpattern earlier |
| 1496 |
<P> |
<P> |
| 1497 |
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 |
| 1498 |
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 |
| 1499 |
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 |
| 1500 |
optionally enclosed in braces. These examples are all identical: |
number, optionally enclosed in braces. These examples are all identical: |
| 1501 |
<pre> |
<pre> |
| 1502 |
(ring), \1 |
(ring), \1 |
| 1503 |
(ring), \g1 |
(ring), \g1 |
| 1504 |
(ring), \g{1} |
(ring), \g{1} |
| 1505 |
</pre> |
</pre> |
| 1506 |
A positive number specifies an absolute reference without the ambiguity that is |
An unsigned number specifies an absolute reference without the ambiguity that |
| 1507 |
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 |
| 1508 |
reference. A negative number is a relative reference. Consider this example: |
the reference. A negative number is a relative reference. Consider this |
| 1509 |
|
example: |
| 1510 |
<pre> |
<pre> |
| 1511 |
(abc(def)ghi)\g{-1} |
(abc(def)ghi)\g{-1} |
| 1512 |
</pre> |
</pre> |
| 1535 |
capturing subpattern is matched caselessly. |
capturing subpattern is matched caselessly. |
| 1536 |
</P> |
</P> |
| 1537 |
<P> |
<P> |
| 1538 |
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 |
| 1539 |
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 |
| 1540 |
|
\k'name' are supported, as is the Python syntax (?P=name). Perl 5.10's unified |
| 1541 |
|
back reference syntax, in which \g can be used for both numeric and named |
| 1542 |
|
references, is also supported. We could rewrite the above example in any of |
| 1543 |
the following ways: |
the following ways: |
| 1544 |
<pre> |
<pre> |
| 1545 |
(?<p1>(?i)rah)\s+\k<p1> |
(?<p1>(?i)rah)\s+\k<p1> |
| 1546 |
|
(?'p1'(?i)rah)\s+\k{p1} |
| 1547 |
(?P<p1>(?i)rah)\s+(?P=p1) |
(?P<p1>(?i)rah)\s+(?P=p1) |
| 1548 |
|
(?<p1>(?i)rah)\s+\g{p1} |
| 1549 |
</pre> |
</pre> |
| 1550 |
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 |
| 1551 |
after the reference. |
after the reference. |
| 1581 |
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 |
| 1582 |
minimum of zero. |
minimum of zero. |
| 1583 |
<a name="bigassertions"></a></P> |
<a name="bigassertions"></a></P> |
| 1584 |
<br><a name="SEC16" href="#TOC1">ASSERTIONS</a><br> |
<br><a name="SEC18" href="#TOC1">ASSERTIONS</a><br> |
| 1585 |
<P> |
<P> |
| 1586 |
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 |
| 1587 |
matching point that does not actually consume any characters. The simple |
matching point that does not actually consume any characters. The simple |
| 1663 |
<pre> |
<pre> |
| 1664 |
(?<=abc|abde) |
(?<=abc|abde) |
| 1665 |
</pre> |
</pre> |
| 1666 |
|
In some cases, the Perl 5.10 escape sequence \K |
| 1667 |
|
<a href="#resetmatchstart">(see above)</a> |
| 1668 |
|
can be used instead of a lookbehind assertion; this is not restricted to a |
| 1669 |
|
fixed-length. |
| 1670 |
|
</P> |
| 1671 |
|
<P> |
| 1672 |
The implementation of lookbehind assertions is, for each alternative, to |
The implementation of lookbehind assertions is, for each alternative, to |
| 1673 |
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 |
| 1674 |
match. If there are insufficient characters before the current position, the |
match. If there are insufficient characters before the current position, the |
| 1741 |
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 |
| 1742 |
characters that are not "999". |
characters that are not "999". |
| 1743 |
<a name="conditions"></a></P> |
<a name="conditions"></a></P> |
| 1744 |
<br><a name="SEC17" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br> |
<br><a name="SEC19" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br> |
| 1745 |
<P> |
<P> |
| 1746 |
It is possible to cause the matching process to obey a subpattern |
It is possible to cause the matching process to obey a subpattern |
| 1747 |
conditionally or to choose between two alternative subpatterns, depending on |
conditionally or to choose between two alternative subpatterns, depending on |
| 1765 |
<P> |
<P> |
| 1766 |
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 |
| 1767 |
condition is true if the capturing subpattern of that number has previously |
condition is true if the capturing subpattern of that number has previously |
| 1768 |
matched. |
matched. An alternative notation is to precede the digits with a plus or minus |
| 1769 |
|
sign. In this case, the subpattern number is relative rather than absolute. |
| 1770 |
|
The most recently opened parentheses can be referenced by (?(-1), the next most |
| 1771 |
|
recent by (?(-2), and so on. In looping constructs it can also make sense to |
| 1772 |
|
refer to subsequent groups with constructs such as (?(+2). |
| 1773 |
</P> |
</P> |
| 1774 |
<P> |
<P> |
| 1775 |
Consider the following pattern, which contains non-significant white space to |
Consider the following pattern, which contains non-significant white space to |
| 1788 |
subpattern matches nothing. In other words, this pattern matches a sequence of |
subpattern matches nothing. In other words, this pattern matches a sequence of |
| 1789 |
non-parentheses, optionally enclosed in parentheses. |
non-parentheses, optionally enclosed in parentheses. |
| 1790 |
</P> |
</P> |
| 1791 |
|
<P> |
| 1792 |
|
If you were embedding this pattern in a larger one, you could use a relative |
| 1793 |
|
reference: |
| 1794 |
|
<pre> |
| 1795 |
|
...other stuff... ( \( )? [^()]+ (?(-1) \) ) ... |
| 1796 |
|
</pre> |
| 1797 |
|
This makes the fragment independent of the parentheses in the larger pattern. |
| 1798 |
|
</P> |
| 1799 |
<br><b> |
<br><b> |
| 1800 |
Checking for a used subpattern by name |
Checking for a used subpattern by name |
| 1801 |
</b><br> |
</b><br> |
| 1879 |
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 |
| 1880 |
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. |
| 1881 |
<a name="comments"></a></P> |
<a name="comments"></a></P> |
| 1882 |
<br><a name="SEC18" href="#TOC1">COMMENTS</a><br> |
<br><a name="SEC20" href="#TOC1">COMMENTS</a><br> |
| 1883 |
<P> |
<P> |
| 1884 |
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 |
| 1885 |
closing parenthesis. Nested parentheses are not permitted. The characters |
closing parenthesis. Nested parentheses are not permitted. The characters |
| 1890 |
character class introduces a comment that continues to immediately after the |
character class introduces a comment that continues to immediately after the |
| 1891 |
next newline in the pattern. |
next newline in the pattern. |
| 1892 |
<a name="recursion"></a></P> |
<a name="recursion"></a></P> |
| 1893 |
<br><a name="SEC19" href="#TOC1">RECURSIVE PATTERNS</a><br> |
<br><a name="SEC21" href="#TOC1">RECURSIVE PATTERNS</a><br> |
| 1894 |
<P> |
<P> |
| 1895 |
Consider the problem of matching a string in parentheses, allowing for |
Consider the problem of matching a string in parentheses, allowing for |
| 1896 |
unlimited nested parentheses. Without the use of recursion, the best that can |
unlimited nested parentheses. Without the use of recursion, the best that can |
| 1946 |
( \( ( (?>[^()]+) | (?1) )* \) ) |
( \( ( (?>[^()]+) | (?1) )* \) ) |
| 1947 |
</pre> |
</pre> |
| 1948 |
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 |
| 1949 |
them instead of the whole pattern. In a larger pattern, keeping track of |
them instead of the whole pattern. |
| 1950 |
parenthesis numbers can be tricky. It may be more convenient to use named |
</P> |
| 1951 |
parentheses instead. The Perl syntax for this is (?&name); PCRE's earlier |
<P> |
| 1952 |
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 |
| 1953 |
follows: |
is made easier by the use of relative references. (A Perl 5.10 feature.) |
| 1954 |
|
Instead of (?1) in the pattern above you can write (?-2) to refer to the second |
| 1955 |
|
most recently opened parentheses preceding the recursion. In other words, a |
| 1956 |
|
negative number counts capturing parentheses leftwards from the point at which |
| 1957 |
|
it is encountered. |
| 1958 |
|
</P> |
| 1959 |
|
<P> |
| 1960 |
|
It is also possible to refer to subsequently opened parentheses, by writing |
| 1961 |
|
references such as (?+2). However, these cannot be recursive because the |
| 1962 |
|
reference is not inside the parentheses that are referenced. They are always |
| 1963 |
|
"subroutine" calls, as described in the next section. |
| 1964 |
|
</P> |
| 1965 |
|
<P> |
| 1966 |
|
An alternative approach is to use named parentheses instead. The Perl syntax |
| 1967 |
|
for this is (?&name); PCRE's earlier syntax (?P>name) is also supported. We |
| 1968 |
|
could rewrite the above example as follows: |
| 1969 |
<pre> |
<pre> |
| 1970 |
(?<pn> \( ( (?>[^()]+) | (?&pn) )* \) ) |
(?<pn> \( ( (?>[^()]+) | (?&pn) )* \) ) |
| 1971 |
</pre> |
</pre> |
| 1972 |
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 |
| 1973 |
used. This particular example pattern contains nested unlimited repeats, and so |
used. |
| 1974 |
the use of atomic grouping for matching strings of non-parentheses is important |
</P> |
| 1975 |
when applying the pattern to strings that do not match. For example, when this |
<P> |
| 1976 |
pattern is applied to |
This particular example pattern that we have been looking at contains nested |
| 1977 |
|
unlimited repeats, and so the use of atomic grouping for matching strings of |
| 1978 |
|
non-parentheses is important when applying the pattern to strings that do not |
| 1979 |
|
match. For example, when this pattern is applied to |
| 1980 |
<pre> |
<pre> |
| 1981 |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
| 1982 |
</pre> |
</pre> |
| 2020 |
different alternatives for the recursive and non-recursive cases. The (?R) item |
different alternatives for the recursive and non-recursive cases. The (?R) item |
| 2021 |
is the actual recursive call. |
is the actual recursive call. |
| 2022 |
<a name="subpatternsassubroutines"></a></P> |
<a name="subpatternsassubroutines"></a></P> |
| 2023 |
<br><a name="SEC20" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br> |
<br><a name="SEC22" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br> |
| 2024 |
<P> |
<P> |
| 2025 |
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 |
| 2026 |
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 |
| 2027 |
subroutine in a programming language. The "called" subpattern may be defined |
subroutine in a programming language. The "called" subpattern may be defined |
| 2028 |
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 |
| 2029 |
|
relative, as in these examples: |
| 2030 |
|
<pre> |
| 2031 |
|
(...(absolute)...)...(?2)... |
| 2032 |
|
(...(relative)...)...(?-1)... |
| 2033 |
|
(...(?+1)...(relative)... |
| 2034 |
|
</pre> |
| 2035 |
|
An earlier example pointed out that the pattern |
| 2036 |
<pre> |
<pre> |
| 2037 |
(sens|respons)e and \1ibility |
(sens|respons)e and \1ibility |
| 2038 |
</pre> |
</pre> |
| 2055 |
case-independence are fixed when the subpattern is defined. They cannot be |
case-independence are fixed when the subpattern is defined. They cannot be |
| 2056 |
changed for different calls. For example, consider this pattern: |
changed for different calls. For example, consider this pattern: |
| 2057 |
<pre> |
<pre> |
| 2058 |
(abc)(?i:(?1)) |
(abc)(?i:(?-1)) |
| 2059 |
</pre> |
</pre> |
| 2060 |
It matches "abcabc". It does not match "abcABC" because the change of |
It matches "abcabc". It does not match "abcABC" because the change of |
| 2061 |
processing option does not affect the called subpattern. |
processing option does not affect the called subpattern. |
| 2062 |
|
<a name="onigurumasubroutines"></a></P> |
| 2063 |
|
<br><a name="SEC23" href="#TOC1">ONIGURUMA SUBROUTINE SYNTAX</a><br> |
| 2064 |
|
<P> |
| 2065 |
|
For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or |
| 2066 |
|
a number enclosed either in angle brackets or single quotes, is an alternative |
| 2067 |
|
syntax for referencing a subpattern as a subroutine, possibly recursively. Here |
| 2068 |
|
are two of the examples used above, rewritten using this syntax: |
| 2069 |
|
<pre> |
| 2070 |
|
(?<pn> \( ( (?>[^()]+) | \g<pn> )* \) ) |
| 2071 |
|
(sens|respons)e and \g'1'ibility |
| 2072 |
|
</pre> |
| 2073 |
|
PCRE supports an extension to Oniguruma: if a number is preceded by a |
| 2074 |
|
plus or a minus sign it is taken as a relative reference. For example: |
| 2075 |
|
<pre> |
| 2076 |
|
(abc)(?i:\g<-1>) |
| 2077 |
|
</pre> |
| 2078 |
|
Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are <i>not</i> |
| 2079 |
|
synonymous. The former is a back reference; the latter is a subroutine call. |
| 2080 |
</P> |
</P> |
| 2081 |
<br><a name="SEC21" href="#TOC1">CALLOUTS</a><br> |
<br><a name="SEC24" href="#TOC1">CALLOUTS</a><br> |
| 2082 |
<P> |
<P> |
| 2083 |
Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl |
Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl |
| 2084 |
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 |
| 2113 |
<a href="pcrecallout.html"><b>pcrecallout</b></a> |
<a href="pcrecallout.html"><b>pcrecallout</b></a> |
| 2114 |
documentation. |
documentation. |
| 2115 |
</P> |
</P> |
| 2116 |
<br><a name="SEC22" href="#TOC1">SEE ALSO</a><br> |
<br><a name="SEC25" href="#TOC1">BACKTRACKING CONTROL</a><br> |
| 2117 |
|
<P> |
| 2118 |
|
Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which |
| 2119 |
|
are described in the Perl documentation as "experimental and subject to change |
| 2120 |
|
or removal in a future version of Perl". It goes on to say: "Their usage in |
| 2121 |
|
production code should be noted to avoid problems during upgrades." The same |
| 2122 |
|
remarks apply to the PCRE features described in this section. |
| 2123 |
|
</P> |
| 2124 |
|
<P> |
| 2125 |
|
Since these verbs are specifically related to backtracking, most of them can be |
| 2126 |
|
used only when the pattern is to be matched using <b>pcre_exec()</b>, which uses |
| 2127 |
|
a backtracking algorithm. With the exception of (*FAIL), which behaves like a |
| 2128 |
|
failing negative assertion, they cause an error if encountered by |
| 2129 |
|
<b>pcre_dfa_exec()</b>. |
| 2130 |
|
</P> |
| 2131 |
|
<P> |
| 2132 |
|
The new verbs make use of what was previously invalid syntax: an opening |
| 2133 |
|
parenthesis followed by an asterisk. In Perl, they are generally of the form |
| 2134 |
|
(*VERB:ARG) but PCRE does not support the use of arguments, so its general |
| 2135 |
|
form is just (*VERB). Any number of these verbs may occur in a pattern. There |
| 2136 |
|
are two kinds: |
| 2137 |
|
</P> |
| 2138 |
|
<br><b> |
| 2139 |
|
Verbs that act immediately |
| 2140 |
|
</b><br> |
| 2141 |
|
<P> |
| 2142 |
|
The following verbs act as soon as they are encountered: |
| 2143 |
|
<pre> |
| 2144 |
|
(*ACCEPT) |
| 2145 |
|
</pre> |
| 2146 |
|
This verb causes the match to end successfully, skipping the remainder of the |
| 2147 |
|
pattern. When inside a recursion, only the innermost pattern is ended |
| 2148 |
|
immediately. PCRE differs from Perl in what happens if the (*ACCEPT) is inside |
| 2149 |
|
capturing parentheses. In Perl, the data so far is captured: in PCRE no data is |
| 2150 |
|
captured. For example: |
| 2151 |
|
<pre> |
| 2152 |
|
A(A|B(*ACCEPT)|C)D |
| 2153 |
|
</pre> |
| 2154 |
|
This matches "AB", "AAD", or "ACD", but when it matches "AB", no data is |
| 2155 |
|
captured. |
| 2156 |
|
<pre> |
| 2157 |
|
(*FAIL) or (*F) |
| 2158 |
|
</pre> |
| 2159 |
|
This verb causes the match to fail, forcing backtracking to occur. It is |
| 2160 |
|
equivalent to (?!) but easier to read. The Perl documentation notes that it is |
| 2161 |
|
probably useful only when combined with (?{}) or (??{}). Those are, of course, |
| 2162 |
|
Perl features that are not present in PCRE. The nearest equivalent is the |
| 2163 |
|
callout feature, as for example in this pattern: |
| 2164 |
|
<pre> |
| 2165 |
|
a+(?C)(*FAIL) |
| 2166 |
|
</pre> |
| 2167 |
|
A match with the string "aaaa" always fails, but the callout is taken before |
| 2168 |
|
each backtrack happens (in this example, 10 times). |
| 2169 |
|
</P> |
| 2170 |
|
<br><b> |
| 2171 |
|
Verbs that act after backtracking |
| 2172 |
|
</b><br> |
| 2173 |
|
<P> |
| 2174 |
|
The following verbs do nothing when they are encountered. Matching continues |
| 2175 |
|
with what follows, but if there is no subsequent match, a failure is forced. |
| 2176 |
|
The verbs differ in exactly what kind of failure occurs. |
| 2177 |
|
<pre> |
| 2178 |
|
(*COMMIT) |
| 2179 |
|
</pre> |
| 2180 |
|
This verb causes the whole match to fail outright if the rest of the pattern |
| 2181 |
|
does not match. Even if the pattern is unanchored, no further attempts to find |
| 2182 |
|
a match by advancing the start point take place. Once (*COMMIT) has been |
| 2183 |
|
passed, <b>pcre_exec()</b> is committed to finding a match at the current |
| 2184 |
|
starting point, or not at all. For example: |
| 2185 |
|
<pre> |
| 2186 |
|
a+(*COMMIT)b |
| 2187 |
|
</pre> |
| 2188 |
|
This matches "xxaab" but not "aacaab". It can be thought of as a kind of |
| 2189 |
|
dynamic anchor, or "I've started, so I must finish." |
| 2190 |
|
<pre> |
| 2191 |
|
(*PRUNE) |
| 2192 |
|
</pre> |
| 2193 |
|
This verb causes the match to fail at the current position if the rest of the |
| 2194 |
|
pattern does not match. If the pattern is unanchored, the normal "bumpalong" |
| 2195 |
|
advance to the next starting character then happens. Backtracking can occur as |
| 2196 |
|
usual to the left of (*PRUNE), or when matching to the right of (*PRUNE), but |
| 2197 |
|
if there is no match to the right, backtracking cannot cross (*PRUNE). |
| 2198 |
|
In simple cases, the use of (*PRUNE) is just an alternative to an atomic |
| 2199 |
|
group or possessive quantifier, but there are some uses of (*PRUNE) that cannot |
| 2200 |
|
be expressed in any other way. |
| 2201 |
|
<pre> |
| 2202 |
|
(*SKIP) |
| 2203 |
|
</pre> |
| 2204 |
|
This verb is like (*PRUNE), except that if the pattern is unanchored, the |
| 2205 |
|
"bumpalong" advance is not to the next character, but to the position in the |
| 2206 |
|
subject where (*SKIP) was encountered. (*SKIP) signifies that whatever text |
| 2207 |
|
was matched leading up to it cannot be part of a successful match. Consider: |
| 2208 |
|
<pre> |
| 2209 |
|
a+(*SKIP)b |
| 2210 |
|
</pre> |
| 2211 |
|
If the subject is "aaaac...", after the first match attempt fails (starting at |
| 2212 |
|
the first character in the string), the starting point skips on to start the |
| 2213 |
|
next attempt at "c". Note that a possessive quantifer does not have the same |
| 2214 |
|
effect in this example; although it would suppress backtracking during the |
| 2215 |
|
first match attempt, the second attempt would start at the second character |
| 2216 |
|
instead of skipping on to "c". |
| 2217 |
|
<pre> |
| 2218 |
|
(*THEN) |
| 2219 |
|
</pre> |
| 2220 |
|
This verb causes a skip to the next alternation if the rest of the pattern does |
| 2221 |
|
not match. That is, it cancels pending backtracking, but only within the |
| 2222 |
|
current alternation. Its name comes from the observation that it can be used |
| 2223 |
|
for a pattern-based if-then-else block: |
| 2224 |
|
<pre> |
| 2225 |
|
( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ... |
| 2226 |
|
</pre> |
| 2227 |
|
If the COND1 pattern matches, FOO is tried (and possibly further items after |
| 2228 |
|
the end of the group if FOO succeeds); on failure the matcher skips to the |
| 2229 |
|
second alternative and tries COND2, without backtracking into COND1. If (*THEN) |
| 2230 |
|
is used outside of any alternation, it acts exactly like (*PRUNE). |
| 2231 |
|
</P> |
| 2232 |
|
<br><a name="SEC26" href="#TOC1">SEE ALSO</a><br> |
| 2233 |
<P> |
<P> |
| 2234 |
<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), <b>pcre</b>(3). |
| 2235 |
</P> |
</P> |
| 2236 |
<br><a name="SEC23" href="#TOC1">AUTHOR</a><br> |
<br><a name="SEC27" href="#TOC1">AUTHOR</a><br> |
| 2237 |
<P> |
<P> |
| 2238 |
Philip Hazel |
Philip Hazel |
| 2239 |
<br> |
<br> |
| 2242 |
Cambridge CB2 3QH, England. |
Cambridge CB2 3QH, England. |
| 2243 |
<br> |
<br> |
| 2244 |
</P> |
</P> |
| 2245 |
<br><a name="SEC24" href="#TOC1">REVISION</a><br> |
<br><a name="SEC28" href="#TOC1">REVISION</a><br> |
| 2246 |
<P> |
<P> |
| 2247 |
Last updated: 06 March 2007 |
Last updated: 18 March 2009 |
| 2248 |
<br> |
<br> |
| 2249 |
Copyright © 1997-2007 University of Cambridge. |
Copyright © 1997-2009 University of Cambridge. |
| 2250 |
<br> |
<br> |
| 2251 |
<p> |
<p> |
| 2252 |
Return to the <a href="index.html">PCRE index page</a>. |
Return to the <a href="index.html">PCRE index page</a>. |