| 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">BACKSLASH</a> |
<li><a name="TOC2" href="#SEC2">NEWLINE CONVENTIONS</a> |
| 18 |
<li><a name="TOC3" href="#SEC3">CIRCUMFLEX AND DOLLAR</a> |
<li><a name="TOC3" href="#SEC3">CHARACTERS AND METACHARACTERS</a> |
| 19 |
<li><a name="TOC4" href="#SEC4">FULL STOP (PERIOD, DOT)</a> |
<li><a name="TOC4" href="#SEC4">BACKSLASH</a> |
| 20 |
<li><a name="TOC5" href="#SEC5">MATCHING A SINGLE BYTE</a> |
<li><a name="TOC5" href="#SEC5">CIRCUMFLEX AND DOLLAR</a> |
| 21 |
<li><a name="TOC6" href="#SEC6">SQUARE BRACKETS AND CHARACTER CLASSES</a> |
<li><a name="TOC6" href="#SEC6">FULL STOP (PERIOD, DOT)</a> |
| 22 |
<li><a name="TOC7" href="#SEC7">POSIX CHARACTER CLASSES</a> |
<li><a name="TOC7" href="#SEC7">MATCHING A SINGLE BYTE</a> |
| 23 |
<li><a name="TOC8" href="#SEC8">VERTICAL BAR</a> |
<li><a name="TOC8" href="#SEC8">SQUARE BRACKETS AND CHARACTER CLASSES</a> |
| 24 |
<li><a name="TOC9" href="#SEC9">INTERNAL OPTION SETTING</a> |
<li><a name="TOC9" href="#SEC9">POSIX CHARACTER CLASSES</a> |
| 25 |
<li><a name="TOC10" href="#SEC10">SUBPATTERNS</a> |
<li><a name="TOC10" href="#SEC10">VERTICAL BAR</a> |
| 26 |
<li><a name="TOC11" href="#SEC11">NAMED SUBPATTERNS</a> |
<li><a name="TOC11" href="#SEC11">INTERNAL OPTION SETTING</a> |
| 27 |
<li><a name="TOC12" href="#SEC12">REPETITION</a> |
<li><a name="TOC12" href="#SEC12">SUBPATTERNS</a> |
| 28 |
<li><a name="TOC13" href="#SEC13">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a> |
<li><a name="TOC13" href="#SEC13">DUPLICATE SUBPATTERN NUMBERS</a> |
| 29 |
<li><a name="TOC14" href="#SEC14">BACK REFERENCES</a> |
<li><a name="TOC14" href="#SEC14">NAMED SUBPATTERNS</a> |
| 30 |
<li><a name="TOC15" href="#SEC15">ASSERTIONS</a> |
<li><a name="TOC15" href="#SEC15">REPETITION</a> |
| 31 |
<li><a name="TOC16" href="#SEC16">CONDITIONAL SUBPATTERNS</a> |
<li><a name="TOC16" href="#SEC16">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a> |
| 32 |
<li><a name="TOC17" href="#SEC17">COMMENTS</a> |
<li><a name="TOC17" href="#SEC17">BACK REFERENCES</a> |
| 33 |
<li><a name="TOC18" href="#SEC18">RECURSIVE PATTERNS</a> |
<li><a name="TOC18" href="#SEC18">ASSERTIONS</a> |
| 34 |
<li><a name="TOC19" href="#SEC19">SUBPATTERNS AS SUBROUTINES</a> |
<li><a name="TOC19" href="#SEC19">CONDITIONAL SUBPATTERNS</a> |
| 35 |
<li><a name="TOC20" href="#SEC20">CALLOUTS</a> |
<li><a name="TOC20" href="#SEC20">COMMENTS</a> |
| 36 |
|
<li><a name="TOC21" href="#SEC21">RECURSIVE PATTERNS</a> |
| 37 |
|
<li><a name="TOC22" href="#SEC22">SUBPATTERNS AS SUBROUTINES</a> |
| 38 |
|
<li><a name="TOC23" href="#SEC23">ONIGURUMA SUBROUTINE SYNTAX</a> |
| 39 |
|
<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">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 |
| 158 |
<P> |
<P> |
| 159 |
There are two different sets of metacharacters: those that are recognized |
There are two different sets of metacharacters: those that are recognized |
| 160 |
anywhere in the pattern except within square brackets, and those that are |
anywhere in the pattern except within square brackets, and those that are |
| 161 |
recognized in square brackets. Outside square brackets, the metacharacters are |
recognized within square brackets. Outside square brackets, the metacharacters |
| 162 |
as follows: |
are as follows: |
| 163 |
<pre> |
<pre> |
| 164 |
\ general escape character with several uses |
\ general escape character with several uses |
| 165 |
^ assert start of string (or line, in multiline mode) |
^ assert start of string (or line, in multiline mode) |
| 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="SEC2" 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 may |
non-alphanumeric character, it takes away any special meaning that character |
| 195 |
have. This use of backslash as an escape character applies both inside and |
may have. This use of backslash as an escape character applies both inside and |
| 196 |
outside character classes. |
outside character classes. |
| 197 |
</P> |
</P> |
| 198 |
<P> |
<P> |
| 205 |
<P> |
<P> |
| 206 |
If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the |
If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the |
| 207 |
pattern (other than in a character class) and characters between a # outside |
pattern (other than in a character class) and characters between a # outside |
| 208 |
a character class and the next newline character are ignored. An escaping |
a character class and the next newline are ignored. An escaping backslash can |
| 209 |
backslash can be used to include a whitespace or # character as part of the |
be used to include a whitespace or # character as part of the pattern. |
|
pattern. |
|
| 210 |
</P> |
</P> |
| 211 |
<P> |
<P> |
| 212 |
If you want to remove the special meaning from a sequence of characters, you |
If you want to remove the special meaning from a sequence of characters, you |
| 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... (UTF-8 mode only) |
\x{hhh..} character with hex code hhh.. |
| 245 |
</pre> |
</pre> |
| 246 |
The precise effect of \cx is as follows: if x is a lower case letter, it |
The precise effect of \cx is as follows: if x is a lower case letter, it |
| 247 |
is converted to upper case. Then bit 6 of the character (hex 40) is inverted. |
is converted to upper case. Then bit 6 of the character (hex 40) is inverted. |
| 250 |
</P> |
</P> |
| 251 |
<P> |
<P> |
| 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). In UTF-8 mode, any number of hexadecimal digits may |
upper or lower case). Any number of hexadecimal digits may appear between \x{ |
| 254 |
appear between \x{ and }, but the value of the character code must be less |
and }, but the value of the character code must be less than 256 in non-UTF-8 |
| 255 |
than 2**31 (that is, the maximum hexadecimal value is 7FFFFFFF). If characters |
mode, and less than 2**31 in UTF-8 mode. That is, the maximum value in |
| 256 |
other than hexadecimal digits appear between \x{ and }, or if there is no |
hexadecimal is 7FFFFFFF. Note that this is bigger than the largest Unicode code |
| 257 |
terminating }, this form of escape is not recognized. Instead, the initial |
point, which is 10FFFF. |
| 258 |
\x will be interpreted as a basic hexadecimal escape, with no following |
</P> |
| 259 |
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 |
| 267 |
syntaxes for \x when PCRE is in UTF-8 mode. There is no difference in the |
syntaxes for \x. There is no difference in the way they are handled. For |
| 268 |
way they are handled. For example, \xdc is exactly the same as \x{dc}. |
example, \xdc is exactly the same as \x{dc}. |
| 269 |
</P> |
</P> |
| 270 |
<P> |
<P> |
| 271 |
After \0 up to two further octal digits are read. In both cases, if there |
After \0 up to two further octal digits are read. If there are fewer than two |
| 272 |
are fewer than two digits, just those that are present are used. Thus the |
digits, just those that are present are used. Thus the sequence \0\x\07 |
| 273 |
sequence \0\x\07 specifies two binary zeros followed by a BEL character |
specifies two binary zeros followed by a BEL character (code value 7). Make |
| 274 |
(code value 7). Make sure you supply two digits after the initial zero if the |
sure you supply two digits after the initial zero if the pattern character that |
| 275 |
pattern character that follows is itself an octal digit. |
follows is itself an octal digit. |
| 276 |
</P> |
</P> |
| 277 |
<P> |
<P> |
| 278 |
The handling of a backslash followed by a digit other than 0 is complicated. |
The handling of a backslash followed by a digit other than 0 is complicated. |
| 287 |
<P> |
<P> |
| 288 |
Inside a character class, or if the decimal number is greater than 9 and there |
Inside a character class, or if the decimal number is greater than 9 and there |
| 289 |
have not been that many capturing subpatterns, PCRE re-reads up to three octal |
have not been that many capturing subpatterns, PCRE re-reads up to three octal |
| 290 |
digits following the backslash, and generates a single byte from the least |
digits following the backslash, and uses them to generate a data character. Any |
| 291 |
significant 8 bits of the value. Any subsequent digits stand for themselves. |
subsequent digits stand for themselves. In non-UTF-8 mode, the value of a |
| 292 |
For example: |
character specified in octal must be less than \400. In UTF-8 mode, values up |
| 293 |
|
to \777 are permitted. For example: |
| 294 |
<pre> |
<pre> |
| 295 |
\040 is another way of writing a space |
\040 is another way of writing a space |
| 296 |
\40 is the same, provided there are fewer than 40 previous capturing subpatterns |
\40 is the same, provided there are fewer than 40 previous capturing subpatterns |
| 306 |
zero, because no more than three octal digits are ever read. |
zero, because no more than three octal digits are ever read. |
| 307 |
</P> |
</P> |
| 308 |
<P> |
<P> |
| 309 |
All the sequences that define a single byte value or a single UTF-8 character |
All the sequences that define a single character value can be used both inside |
| 310 |
(in UTF-8 mode) can be used both inside and outside character classes. In |
and outside character classes. In addition, inside a character class, the |
| 311 |
addition, inside a character class, the sequence \b is interpreted as the |
sequence \b is interpreted as the backspace character (hex 08), and the |
| 312 |
backspace character (hex 08), and the sequence \X is interpreted as the |
sequences \R and \X are interpreted as the characters "R" and "X", |
| 313 |
character "X". Outside a character class, these sequences have different |
respectively. Outside a character class, these sequences have different |
| 314 |
meanings |
meanings |
| 315 |
<a href="#uniextseq">(see below).</a> |
<a href="#uniextseq">(see below).</a> |
| 316 |
</P> |
</P> |
| 317 |
<br><b> |
<br><b> |
| 318 |
|
Absolute and relative back references |
| 319 |
|
</b><br> |
| 320 |
|
<P> |
| 321 |
|
The sequence \g followed by an unsigned or a negative number, optionally |
| 322 |
|
enclosed in braces, is an absolute or relative back reference. A named back |
| 323 |
|
reference can be coded as \g{name}. Back references are discussed |
| 324 |
|
<a href="#backreferences">later,</a> |
| 325 |
|
following the discussion of |
| 326 |
|
<a href="#subpattern">parenthesized subpatterns.</a> |
| 327 |
|
</P> |
| 328 |
|
<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> |
| 345 |
The third use of backslash is for specifying generic character types. The |
Another use of backslash is for specifying generic character types. The |
| 346 |
following are always recognized: |
following are always recognized: |
| 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). |
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 |
| 373 |
|
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 |
|
is discouraged. |
| 431 |
|
<a name="newlineseq"></a></P> |
| 432 |
|
<br><b> |
| 433 |
|
Newline sequences |
| 434 |
|
</b><br> |
| 435 |
<P> |
<P> |
| 436 |
In UTF-8 mode, characters with values greater than 128 never match \d, \s, or |
Outside a character class, by default, the escape sequence \R matches any |
| 437 |
\w, and always match \D, \S, and \W. This is true even when Unicode |
Unicode newline sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \R is |
| 438 |
character property support is available. |
equivalent to the following: |
| 439 |
|
<pre> |
| 440 |
|
(?>\r\n|\n|\x0b|\f|\r|\x85) |
| 441 |
|
</pre> |
| 442 |
|
This is an example of an "atomic group", details of which are given |
| 443 |
|
<a href="#atomicgroup">below.</a> |
| 444 |
|
This particular group matches either the two-character sequence CR followed by |
| 445 |
|
LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab, |
| 446 |
|
U+000B), FF (formfeed, U+000C), CR (carriage return, U+000D), or NEL (next |
| 447 |
|
line, U+0085). The two-character sequence is treated as a single unit that |
| 448 |
|
cannot be split. |
| 449 |
|
</P> |
| 450 |
|
<P> |
| 451 |
|
In UTF-8 mode, two additional characters whose codepoints are greater than 255 |
| 452 |
|
are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029). |
| 453 |
|
Unicode character property support is not needed for these characters to be |
| 454 |
|
recognized. |
| 455 |
|
</P> |
| 456 |
|
<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". |
| 479 |
<a name="uniextseq"></a></P> |
<a name="uniextseq"></a></P> |
| 480 |
<br><b> |
<br><b> |
| 481 |
Unicode character properties |
Unicode character properties |
| 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 generic character types 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 |
<pre> |
characters whose codepoints are less than 256, but they do work in this mode. |
| 488 |
\p{<i>xx</i>} a character with the <i>xx</i> property |
The extra escape sequences are: |
| 489 |
\P{<i>xx</i>} a character without the <i>xx</i> property |
<pre> |
| 490 |
\X an extended Unicode sequence |
\p{<i>xx</i>} a character with the <i>xx</i> property |
| 491 |
</pre> |
\P{<i>xx</i>} a character without the <i>xx</i> property |
| 492 |
The property names represented by <i>xx</i> above are limited to the |
\X an extended Unicode sequence |
| 493 |
Unicode general category properties. Each character has exactly one such |
</pre> |
| 494 |
property, specified by a two-letter abbreviation. For compatibility with Perl, |
The property names represented by <i>xx</i> above are limited to the Unicode |
| 495 |
negation can be specified by including a circumflex between the opening brace |
script names, the general category properties, and "Any", which matches any |
| 496 |
and the property name. For example, \p{^Lu} is the same as \P{Lu}. |
character (including newline). Other properties such as "InMusicalSymbols" are |
| 497 |
</P> |
not currently supported by PCRE. Note that \P{Any} does not match any |
| 498 |
<P> |
characters, so always causes a match failure. |
| 499 |
If only one letter is specified with \p or \P, it includes all the properties |
</P> |
| 500 |
that start with that letter. In this case, in the absence of negation, the |
<P> |
| 501 |
curly brackets in the escape sequence are optional; these two examples have |
Sets of Unicode characters are defined as belonging to certain scripts. A |
| 502 |
the same effect: |
character from one of these sets can be matched using a script name. For |
| 503 |
|
example: |
| 504 |
|
<pre> |
| 505 |
|
\p{Greek} |
| 506 |
|
\P{Han} |
| 507 |
|
</pre> |
| 508 |
|
Those that are not part of an identified script are lumped together as |
| 509 |
|
"Common". The current list of scripts is: |
| 510 |
|
</P> |
| 511 |
|
<P> |
| 512 |
|
Arabic, |
| 513 |
|
Armenian, |
| 514 |
|
Avestan, |
| 515 |
|
Balinese, |
| 516 |
|
Bamum, |
| 517 |
|
Bengali, |
| 518 |
|
Bopomofo, |
| 519 |
|
Braille, |
| 520 |
|
Buginese, |
| 521 |
|
Buhid, |
| 522 |
|
Canadian_Aboriginal, |
| 523 |
|
Carian, |
| 524 |
|
Cham, |
| 525 |
|
Cherokee, |
| 526 |
|
Common, |
| 527 |
|
Coptic, |
| 528 |
|
Cuneiform, |
| 529 |
|
Cypriot, |
| 530 |
|
Cyrillic, |
| 531 |
|
Deseret, |
| 532 |
|
Devanagari, |
| 533 |
|
Egyptian_Hieroglyphs, |
| 534 |
|
Ethiopic, |
| 535 |
|
Georgian, |
| 536 |
|
Glagolitic, |
| 537 |
|
Gothic, |
| 538 |
|
Greek, |
| 539 |
|
Gujarati, |
| 540 |
|
Gurmukhi, |
| 541 |
|
Han, |
| 542 |
|
Hangul, |
| 543 |
|
Hanunoo, |
| 544 |
|
Hebrew, |
| 545 |
|
Hiragana, |
| 546 |
|
Imperial_Aramaic, |
| 547 |
|
Inherited, |
| 548 |
|
Inscriptional_Pahlavi, |
| 549 |
|
Inscriptional_Parthian, |
| 550 |
|
Javanese, |
| 551 |
|
Kaithi, |
| 552 |
|
Kannada, |
| 553 |
|
Katakana, |
| 554 |
|
Kayah_Li, |
| 555 |
|
Kharoshthi, |
| 556 |
|
Khmer, |
| 557 |
|
Lao, |
| 558 |
|
Latin, |
| 559 |
|
Lepcha, |
| 560 |
|
Limbu, |
| 561 |
|
Linear_B, |
| 562 |
|
Lisu, |
| 563 |
|
Lycian, |
| 564 |
|
Lydian, |
| 565 |
|
Malayalam, |
| 566 |
|
Meetei_Mayek, |
| 567 |
|
Mongolian, |
| 568 |
|
Myanmar, |
| 569 |
|
New_Tai_Lue, |
| 570 |
|
Nko, |
| 571 |
|
Ogham, |
| 572 |
|
Old_Italic, |
| 573 |
|
Old_Persian, |
| 574 |
|
Old_South_Arabian, |
| 575 |
|
Old_Turkic, |
| 576 |
|
Ol_Chiki, |
| 577 |
|
Oriya, |
| 578 |
|
Osmanya, |
| 579 |
|
Phags_Pa, |
| 580 |
|
Phoenician, |
| 581 |
|
Rejang, |
| 582 |
|
Runic, |
| 583 |
|
Samaritan, |
| 584 |
|
Saurashtra, |
| 585 |
|
Shavian, |
| 586 |
|
Sinhala, |
| 587 |
|
Sundanese, |
| 588 |
|
Syloti_Nagri, |
| 589 |
|
Syriac, |
| 590 |
|
Tagalog, |
| 591 |
|
Tagbanwa, |
| 592 |
|
Tai_Le, |
| 593 |
|
Tai_Tham, |
| 594 |
|
Tai_Viet, |
| 595 |
|
Tamil, |
| 596 |
|
Telugu, |
| 597 |
|
Thaana, |
| 598 |
|
Thai, |
| 599 |
|
Tibetan, |
| 600 |
|
Tifinagh, |
| 601 |
|
Ugaritic, |
| 602 |
|
Vai, |
| 603 |
|
Yi. |
| 604 |
|
</P> |
| 605 |
|
<P> |
| 606 |
|
Each character has exactly one general category property, specified by a |
| 607 |
|
two-letter abbreviation. For compatibility with Perl, negation can be specified |
| 608 |
|
by including a circumflex between the opening brace and the property name. For |
| 609 |
|
example, \p{^Lu} is the same as \P{Lu}. |
| 610 |
|
</P> |
| 611 |
|
<P> |
| 612 |
|
If only one letter is specified with \p or \P, it includes all the general |
| 613 |
|
category properties that start with that letter. In this case, in the absence |
| 614 |
|
of negation, the curly brackets in the escape sequence are optional; these two |
| 615 |
|
examples have the same effect: |
| 616 |
<pre> |
<pre> |
| 617 |
\p{L} |
\p{L} |
| 618 |
\pL |
\pL |
| 619 |
</pre> |
</pre> |
| 620 |
The following property codes are supported: |
The following general category property codes are supported: |
| 621 |
<pre> |
<pre> |
| 622 |
C Other |
C Other |
| 623 |
Cc Control |
Cc Control |
| 663 |
Zp Paragraph separator |
Zp Paragraph separator |
| 664 |
Zs Space separator |
Zs Space separator |
| 665 |
</pre> |
</pre> |
| 666 |
Extended properties such as "Greek" or "InMusicalSymbols" are not supported by |
The special property L& is also supported: it matches a character that has |
| 667 |
PCRE. |
the Lu, Ll, or Lt property, in other words, a letter that is not classified as |
| 668 |
|
a modifier or "other". |
| 669 |
|
</P> |
| 670 |
|
<P> |
| 671 |
|
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 |
| 681 |
|
properties with "Is". |
| 682 |
|
</P> |
| 683 |
|
<P> |
| 684 |
|
No character that is in the Unicode table has the Cn (unassigned) property. |
| 685 |
|
Instead, this property is assumed for any code point that is not in the |
| 686 |
|
Unicode table. |
| 687 |
</P> |
</P> |
| 688 |
<P> |
<P> |
| 689 |
Specifying caseless matching does not affect these escape sequences. For |
Specifying caseless matching does not affect these escape sequences. For |
| 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 |
| 742 |
</b><br> |
</b><br> |
| 743 |
<P> |
<P> |
| 744 |
The fourth use of backslash is for certain simple assertions. An assertion |
The final use of backslash is for certain simple assertions. An assertion |
| 745 |
specifies a condition that has to be met at a particular point in a match, |
specifies a condition that has to be met at a particular point in a match, |
| 746 |
without consuming any characters from the subject string. The use of |
without consuming any characters from the subject string. The use of |
| 747 |
subpatterns for more complicated assertions is described |
subpatterns for more complicated assertions is described |
| 748 |
<a href="#bigassertions">below.</a> |
<a href="#bigassertions">below.</a> |
| 749 |
The backslashed |
The backslashed assertions are: |
|
assertions are: |
|
| 750 |
<pre> |
<pre> |
| 751 |
\b matches at a word boundary |
\b matches at a word boundary |
| 752 |
\B matches when not at a word boundary |
\B matches when not at a word boundary |
| 753 |
\A matches at start of subject |
\A matches at the start of the subject |
| 754 |
\Z matches at end of subject or before newline at end |
\Z matches at the end of the subject |
| 755 |
\z matches at end of subject |
also matches before a newline at the end of the subject |
| 756 |
\G matches at first matching position in subject |
\z matches only at the end of the subject |
| 757 |
|
\G matches at the first matching position in the subject |
| 758 |
</pre> |
</pre> |
| 759 |
These assertions may not appear in character classes (but note that \b has a |
These assertions may not appear in character classes (but note that \b has a |
| 760 |
different meaning, namely the backspace character, inside a character class). |
different meaning, namely the backspace character, inside a character class). |
| 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 |
| 777 |
circumflex and dollar metacharacters. However, if the <i>startoffset</i> |
circumflex and dollar metacharacters. However, if the <i>startoffset</i> |
| 778 |
argument of <b>pcre_exec()</b> is non-zero, indicating that matching is to start |
argument of <b>pcre_exec()</b> is non-zero, indicating that matching is to start |
| 779 |
at a point other than the beginning of the subject, \A can never match. The |
at a point other than the beginning of the subject, \A can never match. The |
| 780 |
difference between \Z and \z is that \Z matches before a newline that is the |
difference between \Z and \z is that \Z matches before a newline at the end |
| 781 |
last character of the string as well as at the end of the string, whereas \z |
of the string as well as at the very end, whereas \z matches only at the end. |
|
matches only at the end. |
|
| 782 |
</P> |
</P> |
| 783 |
<P> |
<P> |
| 784 |
The \G assertion is true only when the current matching position is at the |
The \G assertion is true only when the current matching position is at the |
| 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="SEC3" 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 |
| 822 |
<P> |
<P> |
| 823 |
A dollar character is an assertion that is true only if the current matching |
A dollar character is an assertion that is true only if the current matching |
| 824 |
point is at the end of the subject string, or immediately before a newline |
point is at the end of the subject string, or immediately before a newline |
| 825 |
character that is the last character in the string (by default). Dollar need |
at the end of the string (by default). Dollar need not be the last character of |
| 826 |
not be the last character of the pattern if a number of alternatives are |
the pattern if a number of alternatives are involved, but it should be the last |
| 827 |
involved, but it should be the last item in any branch in which it appears. |
item in any branch in which it appears. Dollar has no special meaning in a |
| 828 |
Dollar has no special meaning in a character class. |
character class. |
| 829 |
</P> |
</P> |
| 830 |
<P> |
<P> |
| 831 |
The meaning of dollar can be changed so that it matches only at the very end of |
The meaning of dollar can be changed so that it matches only at the very end of |
| 834 |
</P> |
</P> |
| 835 |
<P> |
<P> |
| 836 |
The meanings of the circumflex and dollar characters are changed if the |
The meanings of the circumflex and dollar characters are changed if the |
| 837 |
PCRE_MULTILINE option is set. When this is the case, they match immediately |
PCRE_MULTILINE option is set. When this is the case, a circumflex matches |
| 838 |
after and immediately before an internal newline character, respectively, in |
immediately after internal newlines as well as at the start of the subject |
| 839 |
addition to matching at the start and end of the subject string. For example, |
string. It does not match after a newline that ends the string. A dollar |
| 840 |
the pattern /^abc$/ matches the subject string "def\nabc" (where \n |
matches before any newlines in the string, as well as at the very end, when |
| 841 |
represents a newline character) in multiline mode, but not otherwise. |
PCRE_MULTILINE is set. When newline is specified as the two-character |
| 842 |
Consequently, patterns that are anchored in single line mode because all |
sequence CRLF, isolated CR and LF characters do not indicate newlines. |
| 843 |
branches start with ^ are not anchored in multiline mode, and a match for |
</P> |
| 844 |
circumflex is possible when the <i>startoffset</i> argument of <b>pcre_exec()</b> |
<P> |
| 845 |
is non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is |
For example, the pattern /^abc$/ matches the subject string "def\nabc" (where |
| 846 |
set. |
\n represents a newline) in multiline mode, but not otherwise. Consequently, |
| 847 |
|
patterns that are anchored in single line mode because all branches start with |
| 848 |
|
^ are not anchored in multiline mode, and a match for circumflex is possible |
| 849 |
|
when the <i>startoffset</i> argument of <b>pcre_exec()</b> is non-zero. The |
| 850 |
|
PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set. |
| 851 |
</P> |
</P> |
| 852 |
<P> |
<P> |
| 853 |
Note that the sequences \A, \Z, and \z can be used to match the start and |
Note that the sequences \A, \Z, and \z can be used to match the start and |
| 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 PCRE_MULTILINE is set or not. |
\A it is always anchored, whether or not PCRE_MULTILINE is set. |
| 856 |
</P> |
</P> |
| 857 |
<br><a name="SEC4" 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, including a non-printing character, but not (by default) newline. |
the subject string except (by default) a character that signifies the end of a |
| 861 |
In UTF-8 mode, a dot matches any UTF-8 character, which might be more than one |
line. In UTF-8 mode, the matched character may be more than one byte long. |
| 862 |
byte long, except (by default) newline. If the PCRE_DOTALL option is set, |
</P> |
| 863 |
dots match newlines as well. The handling of dot is entirely independent of the |
<P> |
| 864 |
handling of circumflex and dollar, the only relationship being that they both |
When a line ending is defined as a single character, dot never matches that |
| 865 |
involve newline characters. Dot has no special meaning in a character class. |
character; when the two-character sequence CRLF is used, dot does not match CR |
| 866 |
|
if it is immediately followed by LF, but otherwise it matches all characters |
| 867 |
|
(including isolated CRs and LFs). When any Unicode line endings are being |
| 868 |
|
recognized, dot does not match CR or LF or any of the other line ending |
| 869 |
|
characters. |
| 870 |
|
</P> |
| 871 |
|
<P> |
| 872 |
|
The behaviour of dot with regard to newlines can be changed. If the PCRE_DOTALL |
| 873 |
|
option is set, a dot matches any one character, without exception. If the |
| 874 |
|
two-character sequence CRLF is present in the subject string, it takes two dots |
| 875 |
|
to match it. |
| 876 |
|
</P> |
| 877 |
|
<P> |
| 878 |
|
The handling of dot is entirely independent of the handling of circumflex and |
| 879 |
|
dollar, the only relationship being that they both involve newlines. Dot has no |
| 880 |
|
special meaning in a character class. |
| 881 |
</P> |
</P> |
| 882 |
<br><a name="SEC5" 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 can match a newline. The feature is |
in and out of UTF-8 mode. Unlike a dot, it always matches any line-ending |
| 886 |
provided in Perl in order to match individual bytes in UTF-8 mode. Because it |
characters. The feature is provided in Perl in order to match individual bytes |
| 887 |
breaks up UTF-8 characters into individual bytes, what remains in the string |
in UTF-8 mode. Because it breaks up UTF-8 characters into individual bytes, |
| 888 |
may be a malformed UTF-8 string. For this reason, the \C escape sequence is |
what remains in the string may be a malformed UTF-8 string. For this reason, |
| 889 |
best avoided. |
the \C escape sequence is best avoided. |
| 890 |
</P> |
</P> |
| 891 |
<P> |
<P> |
| 892 |
PCRE does not allow \C to appear in lookbehind assertions |
PCRE does not allow \C to appear in lookbehind assertions |
| 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="SEC6" 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 |
The newline character is never treated in any special way in character classes, |
Characters that might indicate line breaks are never treated in any special way |
| 942 |
whatever the setting of the PCRE_DOTALL or PCRE_MULTILINE options is. A class |
when matching character classes, whatever line-ending sequence is in use, and |
| 943 |
such as [^a] will always match a newline. |
whatever setting of the PCRE_DOTALL and PCRE_MULTILINE options is used. A class |
| 944 |
|
such as [^a] always matches one of these characters. |
| 945 |
</P> |
</P> |
| 946 |
<P> |
<P> |
| 947 |
The minus (hyphen) character can be used to specify a range of characters in a |
The minus (hyphen) character can be used to specify a range of characters in a |
| 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="SEC7" 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="SEC8" 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 |
| 1044 |
gilbert|sullivan |
gilbert|sullivan |
| 1045 |
</pre> |
</pre> |
| 1046 |
matches either "gilbert" or "sullivan". Any number of alternatives may appear, |
matches either "gilbert" or "sullivan". Any number of alternatives may appear, |
| 1047 |
and an empty alternative is permitted (matching the empty string). |
and an empty alternative is permitted (matching the empty string). The matching |
| 1048 |
The matching process tries each alternative in turn, from left to right, |
process tries each alternative in turn, from left to right, and the first one |
| 1049 |
and the first one that succeeds is used. If the alternatives are within a |
that succeeds is used. If the alternatives are within a subpattern |
|
subpattern |
|
| 1050 |
<a href="#subpattern">(defined below),</a> |
<a href="#subpattern">(defined below),</a> |
| 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="SEC9" 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. |
|
the global options (and it will therefore show up in data extracted by the |
|
|
<b>pcre_fullinfo()</b> function). |
|
| 1077 |
</P> |
</P> |
| 1078 |
<P> |
<P> |
| 1079 |
An option change within a subpattern affects only that part of the current |
When one of these option changes occurs at top level (that is, not inside |
| 1080 |
pattern that follows it, so |
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> |
| 1085 |
|
<P> |
| 1086 |
|
An option change within a subpattern (see below for a description of |
| 1087 |
|
subpatterns) affects only that part of the current pattern that follows it, so |
| 1088 |
<pre> |
<pre> |
| 1089 |
(a(?i)b)c |
(a(?i)b)c |
| 1090 |
</pre> |
</pre> |
| 1101 |
behaviour otherwise. |
behaviour otherwise. |
| 1102 |
</P> |
</P> |
| 1103 |
<P> |
<P> |
| 1104 |
The PCRE-specific options PCRE_UNGREEDY and PCRE_EXTRA can be changed in the |
<b>Note:</b> There are other PCRE-specific options that can be set by the |
| 1105 |
same way as the Perl-compatible options by using the characters U and X |
application when the compile or match functions are called. In some cases the |
| 1106 |
respectively. The (?X) flag setting is special in that it must always occur |
pattern can contain special leading sequences such as (*CRLF) to override what |
| 1107 |
earlier in the pattern than any of the additional features it turns on, even |
the application has set or what has been defaulted. Details are given in the |
| 1108 |
when it is at top level. It is best to put it at the start. |
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="SEC10" 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: |
| 1121 |
cat(aract|erpillar|) |
cat(aract|erpillar|) |
| 1122 |
</pre> |
</pre> |
| 1123 |
matches one of the words "cat", "cataract", or "caterpillar". Without the |
matches one of the words "cat", "cataract", or "caterpillar". Without the |
| 1124 |
parentheses, it would match "cataract", "erpillar" or the empty string. |
parentheses, it would match "cataract", "erpillar" or an empty string. |
| 1125 |
<br> |
<br> |
| 1126 |
<br> |
<br> |
| 1127 |
2. It sets up the subpattern as a capturing subpattern. This means that, when |
2. It sets up the subpattern as a capturing subpattern. This means that, when |
| 1149 |
the ((?:red|white) (king|queen)) |
the ((?:red|white) (king|queen)) |
| 1150 |
</pre> |
</pre> |
| 1151 |
the captured substrings are "white queen" and "queen", and are numbered 1 and |
the captured substrings are "white queen" and "queen", and are numbered 1 and |
| 1152 |
2. The maximum number of capturing subpatterns is 65535, and the maximum depth |
2. The maximum number of capturing subpatterns is 65535. |
|
of nesting of all subpatterns, both capturing and non-capturing, is 200. |
|
| 1153 |
</P> |
</P> |
| 1154 |
<P> |
<P> |
| 1155 |
As a convenient shorthand, if any option settings are required at the start of |
As a convenient shorthand, if any option settings are required at the start of |
| 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="SEC11" 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, |
| 1216 |
if an expression is modified, the numbers may change. To help with this |
if an expression is modified, the numbers may change. To help with this |
| 1217 |
difficulty, PCRE supports the naming of subpatterns, something that Perl does |
difficulty, PCRE supports the naming of subpatterns. This feature was not |
| 1218 |
not provide. The Python syntax (?P<name>...) is used. Names consist of |
added to Perl until release 5.10. Python had the feature earlier, and PCRE |
| 1219 |
alphanumeric characters and underscores, and must be unique within a pattern. |
introduced it at release 4.0, using the Python syntax. PCRE now supports both |
| 1220 |
</P> |
the Perl and the Python syntax. Perl allows identically numbered subpatterns to |
| 1221 |
<P> |
have different names, but PCRE does not. |
| 1222 |
Named capturing parentheses are still allocated numbers as well as names. The |
</P> |
| 1223 |
PCRE API provides function calls for extracting the name-to-number translation |
<P> |
| 1224 |
table from a compiled pattern. There is also a convenience function for |
In PCRE, a subpattern can be named in one of three ways: (?<name>...) or |
| 1225 |
extracting a captured substring by name. For further details see the |
(?'name'...) as in Perl, or (?P<name>...) as in Python. References to capturing |
| 1226 |
|
parentheses from other parts of the pattern, such as |
| 1227 |
|
<a href="#backreferences">back references,</a> |
| 1228 |
|
<a href="#recursion">recursion,</a> |
| 1229 |
|
and |
| 1230 |
|
<a href="#conditions">conditions,</a> |
| 1231 |
|
can be made by name as well as by number. |
| 1232 |
|
</P> |
| 1233 |
|
<P> |
| 1234 |
|
Names consist of up to 32 alphanumeric characters and underscores. Named |
| 1235 |
|
capturing parentheses are still allocated numbers as well as names, exactly as |
| 1236 |
|
if the names were not present. The PCRE API provides function calls for |
| 1237 |
|
extracting the name-to-number translation table from a compiled pattern. There |
| 1238 |
|
is also a convenience function for extracting a captured substring by name. |
| 1239 |
|
</P> |
| 1240 |
|
<P> |
| 1241 |
|
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. (Duplicate |
| 1243 |
|
names are also always permitted for subpatterns with the same number, set up as |
| 1244 |
|
described in the previous section.) Duplicate names can be useful for patterns |
| 1245 |
|
where only one instance of the named parentheses can match. Suppose you want to |
| 1246 |
|
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> |
| 1250 |
|
(?<DN>Mon|Fri|Sun)(?:day)?| |
| 1251 |
|
(?<DN>Tue)(?:sday)?| |
| 1252 |
|
(?<DN>Wed)(?:nesday)?| |
| 1253 |
|
(?<DN>Thu)(?:rsday)?| |
| 1254 |
|
(?<DN>Sat)(?:urday)? |
| 1255 |
|
</pre> |
| 1256 |
|
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 |
| 1262 |
|
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. |
| 1264 |
|
</P> |
| 1265 |
|
<P> |
| 1266 |
|
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="SEC12" 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: |
| 1291 |
<pre> |
<pre> |
| 1292 |
a literal data character |
a literal data character |
| 1293 |
the . metacharacter |
the dot metacharacter |
| 1294 |
the \C escape sequence |
the \C escape sequence |
| 1295 |
the \X escape sequence (in UTF-8 mode with Unicode properties) |
the \X escape sequence (in UTF-8 mode with Unicode properties) |
| 1296 |
|
the \R escape sequence |
| 1297 |
an escape such as \d that matches a single character |
an escape such as \d that matches a single character |
| 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 (and historical compatibility) the three most common |
For convenience, the three most common quantifiers have single-character |
| 1343 |
quantifiers have single-character abbreviations: |
abbreviations: |
| 1344 |
<pre> |
<pre> |
| 1345 |
* is equivalent to {0,} |
* is equivalent to {0,} |
| 1346 |
+ is equivalent to {1,} |
+ is equivalent to {1,} |
| 1391 |
way the rest of the pattern matches. |
way the rest of the pattern matches. |
| 1392 |
</P> |
</P> |
| 1393 |
<P> |
<P> |
| 1394 |
If the PCRE_UNGREEDY option is set (an option which is not available in Perl), |
If the PCRE_UNGREEDY option is set (an option that is not available in Perl), |
| 1395 |
the quantifiers are not greedy by default, but individual ones can be made |
the quantifiers are not greedy by default, but individual ones can be made |
| 1396 |
greedy by following them with a question mark. In other words, it inverts the |
greedy by following them with a question mark. In other words, it inverts the |
| 1397 |
default behaviour. |
default behaviour. |
| 1403 |
</P> |
</P> |
| 1404 |
<P> |
<P> |
| 1405 |
If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equivalent |
If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equivalent |
| 1406 |
to Perl's /s) is set, thus allowing the . to match newlines, the pattern is |
to Perl's /s) is set, thus allowing the dot to match newlines, the pattern is |
| 1407 |
implicitly anchored, because whatever follows will be tried against every |
implicitly anchored, because whatever follows will be tried against every |
| 1408 |
character position in the subject string, so there is no point in retrying the |
character position in the subject string, so there is no point in retrying the |
| 1409 |
overall match at any position after the first. PCRE normally treats such a |
overall match at any position after the first. PCRE normally treats such a |
| 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, and a later one |
elsewhere in the pattern, a match at the start may fail where a later one |
| 1421 |
succeed. Consider, for example: |
succeeds. Consider, for example: |
| 1422 |
<pre> |
<pre> |
| 1423 |
(.*)abc\1 |
(.*)abc\1 |
| 1424 |
</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="SEC13" 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 and minimizing repetition, failure of what follows |
With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy") |
| 1446 |
normally causes the repeated item to be re-evaluated to see if a different |
repetition, failure of what follows normally causes the repeated item to be |
| 1447 |
number of repeats allows the rest of the pattern to match. Sometimes it is |
re-evaluated to see if a different number of repeats allows the rest of the |
| 1448 |
useful to prevent this, either to change the nature of the match, or to cause |
pattern to match. Sometimes it is useful to prevent this, either to change the |
| 1449 |
it fail earlier than it otherwise might, when the author of the pattern knows |
nature of the match, or to cause it fail earlier than it otherwise might, when |
| 1450 |
there is no point in carrying on. |
the author of the pattern knows there is no point in carrying on. |
| 1451 |
</P> |
</P> |
| 1452 |
<P> |
<P> |
| 1453 |
Consider, for example, the pattern \d+foo when applied to the subject line |
Consider, for example, the pattern \d+foo when applied to the subject line |
| 1461 |
that once a subpattern has matched, it is not to be re-evaluated in this way. |
that once a subpattern has matched, it is not to be re-evaluated in this way. |
| 1462 |
</P> |
</P> |
| 1463 |
<P> |
<P> |
| 1464 |
If we use atomic grouping for the previous example, the matcher would give up |
If we use atomic grouping for the previous example, the matcher gives up |
| 1465 |
immediately on failing to match "foo" the first time. The notation is a kind of |
immediately on failing to match "foo" the first time. The notation is a kind of |
| 1466 |
special parenthesis, starting with (?> as in this example: |
special parenthesis, starting with (?> as in this example: |
| 1467 |
<pre> |
<pre> |
| 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 or processing of a |
atomic group. However, there is no difference in the meaning of a possessive |
| 1505 |
possessive quantifier and the equivalent atomic group. |
quantifier and the equivalent atomic group, though there may be a performance |
| 1506 |
|
difference; possessive quantifiers should be slightly faster. |
| 1507 |
|
</P> |
| 1508 |
|
<P> |
| 1509 |
|
The possessive quantifier syntax is an extension to the Perl 5.8 syntax. |
| 1510 |
|
Jeffrey Friedl originated the idea (and the name) in the first edition of his |
| 1511 |
|
book. Mike McCloskey liked it, so implemented it when he built Sun's Java |
| 1512 |
|
package, and PCRE copied it from there. It ultimately found its way into Perl |
| 1513 |
|
at release 5.10. |
| 1514 |
</P> |
</P> |
| 1515 |
<P> |
<P> |
| 1516 |
The possessive quantifier syntax is an extension to the Perl syntax. It |
PCRE has an optimization that automatically "possessifies" certain simple |
| 1517 |
originates in Sun's Java package. |
pattern constructs. For example, the sequence A+B is treated as A++B because |
| 1518 |
|
there is no point in backtracking into a sequence of A's when B must follow. |
| 1519 |
</P> |
</P> |
| 1520 |
<P> |
<P> |
| 1521 |
When a pattern contains an unlimited repeat inside a subpattern that can itself |
When a pattern contains an unlimited repeat inside a subpattern that can itself |
| 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="SEC14" 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 |
| 1556 |
always taken as a back reference, and causes an error only if there are not |
always taken as a back reference, and causes an error only if there are not |
| 1557 |
that many capturing left parentheses in the entire pattern. In other words, the |
that many capturing left parentheses in the entire pattern. In other words, the |
| 1558 |
parentheses that are referenced need not be to the left of the reference for |
parentheses that are referenced need not be to the left of the reference for |
| 1559 |
numbers less than 10. See the subsection entitled "Non-printing characters" |
numbers less than 10. A "forward back reference" of this type can make sense |
| 1560 |
|
when a repetition is involved and the subpattern to the right has participated |
| 1561 |
|
in an earlier iteration. |
| 1562 |
|
</P> |
| 1563 |
|
<P> |
| 1564 |
|
It is not possible to have a numerical "forward back reference" to a subpattern |
| 1565 |
|
whose number is 10 or more using this syntax because a sequence such as \50 is |
| 1566 |
|
interpreted as a character defined in octal. See the subsection entitled |
| 1567 |
|
"Non-printing characters" |
| 1568 |
<a href="#digitsafterbackslash">above</a> |
<a href="#digitsafterbackslash">above</a> |
| 1569 |
for further details of the handling of digits following a backslash. |
for further details of the handling of digits following a backslash. There is |
| 1570 |
|
no such problem when named parentheses are used. A back reference to any |
| 1571 |
|
subpattern is possible using named parentheses (see below). |
| 1572 |
|
</P> |
| 1573 |
|
<P> |
| 1574 |
|
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 |
| 1576 |
|
Perl 5.10. This escape must be followed by an unsigned number or a negative |
| 1577 |
|
number, optionally enclosed in braces. These examples are all identical: |
| 1578 |
|
<pre> |
| 1579 |
|
(ring), \1 |
| 1580 |
|
(ring), \g1 |
| 1581 |
|
(ring), \g{1} |
| 1582 |
|
</pre> |
| 1583 |
|
An unsigned number specifies an absolute reference without the ambiguity that |
| 1584 |
|
is present in the older syntax. It is also useful when literal digits follow |
| 1585 |
|
the reference. A negative number is a relative reference. Consider this |
| 1586 |
|
example: |
| 1587 |
|
<pre> |
| 1588 |
|
(abc(def)ghi)\g{-1} |
| 1589 |
|
</pre> |
| 1590 |
|
The sequence \g{-1} is a reference to the most recently started capturing |
| 1591 |
|
subpattern before \g, that is, is it equivalent to \2. Similarly, \g{-2} |
| 1592 |
|
would be equivalent to \1. The use of relative references can be helpful in |
| 1593 |
|
long patterns, and also in patterns that are created by joining together |
| 1594 |
|
fragments that contain references within themselves. |
| 1595 |
</P> |
</P> |
| 1596 |
<P> |
<P> |
| 1597 |
A back reference matches whatever actually matched the capturing subpattern in |
A back reference matches whatever actually matched the capturing subpattern in |
| 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 Python syntax (?P=name). We could |
There are several different ways of writing back references to named |
| 1616 |
rewrite the above example as follows: |
subpatterns. The .NET syntax \k{name} and the Perl syntax \k<name> or |
| 1617 |
<pre> |
\k'name' are supported, as is the Python syntax (?P=name). Perl 5.10's unified |
| 1618 |
(?<p1>(?i)rah)\s+(?P=p1) |
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: |
| 1621 |
|
<pre> |
| 1622 |
|
(?<p1>(?i)rah)\s+\k<p1> |
| 1623 |
|
(?'p1'(?i)rah)\s+\k{p1} |
| 1624 |
|
(?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 |
| 1628 |
|
after the reference. |
| 1629 |
|
</P> |
| 1630 |
|
<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="SEC15" 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 |
| 1697 |
Lookahead assertions |
Lookahead assertions |
| 1698 |
</b><br> |
</b><br> |
| 1699 |
<P> |
<P> |
| 1700 |
Lookahead assertions start |
Lookahead assertions start with (?= for positive assertions and (?! for |
| 1701 |
with (?= for positive assertions and (?! for negative assertions. For example, |
negative assertions. For example, |
| 1702 |
<pre> |
<pre> |
| 1703 |
\w+(?=;) |
\w+(?=;) |
| 1704 |
</pre> |
</pre> |
| 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 |
| 1735 |
</pre> |
</pre> |
| 1736 |
does find an occurrence of "bar" that is not preceded by "foo". The contents of |
does find an occurrence of "bar" that is not preceded by "foo". The contents of |
| 1737 |
a lookbehind assertion are restricted such that all the strings it matches must |
a lookbehind assertion are restricted such that all the strings it matches must |
| 1738 |
have a fixed length. However, if there are several alternatives, they do not |
have a fixed length. However, if there are several top-level alternatives, they |
| 1739 |
all have to have the same fixed length. Thus |
do not all have to have the same fixed length. Thus |
| 1740 |
<pre> |
<pre> |
| 1741 |
(?<=bullock|donkey) |
(?<=bullock|donkey) |
| 1742 |
</pre> |
</pre> |
| 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 width 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 |
| 1769 |
match is deemed to fail. |
assertion fails. |
| 1770 |
</P> |
</P> |
| 1771 |
<P> |
<P> |
| 1772 |
PCRE does not allow the \C escape (which matches a single byte in UTF-8 mode) |
PCRE does not allow the \C escape (which matches a single byte in UTF-8 mode) |
| 1773 |
to appear in lookbehind assertions, because it makes it impossible to calculate |
to appear in lookbehind assertions, because it makes it impossible to calculate |
| 1774 |
the length of the lookbehind. The \X escape, which can match different numbers |
the length of the lookbehind. The \X and \R escapes, which can match |
| 1775 |
of bytes, is also not permitted. |
different numbers of bytes, are also not permitted. |
| 1776 |
</P> |
</P> |
| 1777 |
<P> |
<P> |
| 1778 |
Atomic groups can be used in conjunction with lookbehind assertions to specify |
<a href="#subpatternsassubroutines">"Subroutine"</a> |
| 1779 |
efficient matching at the end of the subject string. Consider a simple pattern |
calls (see below) such as (?2) or (?&X) are permitted in lookbehinds, as long |
| 1780 |
such as |
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 |
| 1786 |
|
specify efficient matching of fixed-length strings at the end of subject |
| 1787 |
|
strings. Consider a simple pattern such as |
| 1788 |
<pre> |
<pre> |
| 1789 |
abcd$ |
abcd$ |
| 1790 |
</pre> |
</pre> |
| 1800 |
covers the entire string, from right to left, so we are no better off. However, |
covers the entire string, from right to left, so we are no better off. However, |
| 1801 |
if the pattern is written as |
if the pattern is written as |
| 1802 |
<pre> |
<pre> |
|
^(?>.*)(?<=abcd) |
|
|
</pre> |
|
|
or, equivalently, using the possessive quantifier syntax, |
|
|
<pre> |
|
| 1803 |
^.*+(?<=abcd) |
^.*+(?<=abcd) |
| 1804 |
</pre> |
</pre> |
| 1805 |
there can be no backtracking for the .* item; it can match only the entire |
there can be no backtracking for the .*+ item; it can match only the entire |
| 1806 |
string. The subsequent lookbehind assertion does a single test on the last four |
string. The subsequent lookbehind assertion does a single test on the last four |
| 1807 |
characters. If it fails, the match fails immediately. For long strings, this |
characters. If it fails, the match fails immediately. For long strings, this |
| 1808 |
approach makes a significant difference to the processing time. |
approach makes a significant difference to the processing time. |
| 1841 |
</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 |
</P> |
<a name="conditions"></a></P> |
| 1845 |
<br><a name="SEC16" 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) |
| 1857 |
subpattern, a compile-time error occurs. |
subpattern, a compile-time error occurs. |
| 1858 |
</P> |
</P> |
| 1859 |
<P> |
<P> |
| 1860 |
There are three kinds of condition. If the text between the parentheses |
There are four kinds of condition: references to subpatterns, references to |
| 1861 |
consists of a sequence of digits, the condition is satisfied if the capturing |
recursion, a pseudo-condition called DEFINE, and assertions. |
| 1862 |
subpattern of that number has previously matched. The number must be greater |
</P> |
| 1863 |
than zero. Consider the following pattern, which contains non-significant white |
<br><b> |
| 1864 |
space to make it more readable (assume the PCRE_EXTENDED option) and to divide |
Checking for a used subpattern by number |
| 1865 |
it into three parts for ease of discussion: |
</b><br> |
| 1866 |
|
<P> |
| 1867 |
|
If the text between the parentheses consists of a sequence of digits, the |
| 1868 |
|
condition is true if a capturing subpattern of that number has previously |
| 1869 |
|
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> |
| 1879 |
|
<P> |
| 1880 |
|
Consider the following pattern, which contains non-significant white space to |
| 1881 |
|
make it more readable (assume the PCRE_EXTENDED option) and to divide it into |
| 1882 |
|
three parts for ease of discussion: |
| 1883 |
<pre> |
<pre> |
| 1884 |
( \( )? [^()]+ (?(1) \) ) |
( \( )? [^()]+ (?(1) \) ) |
| 1885 |
</pre> |
</pre> |
| 1894 |
non-parentheses, optionally enclosed in parentheses. |
non-parentheses, optionally enclosed in parentheses. |
| 1895 |
</P> |
</P> |
| 1896 |
<P> |
<P> |
| 1897 |
If the condition is the string (R), it is satisfied if a recursive call to the |
If you were embedding this pattern in a larger one, you could use a relative |
| 1898 |
pattern or subpattern has been made. At "top level", the condition is false. |
reference: |
| 1899 |
This is a PCRE extension. Recursive patterns are described in the next section. |
<pre> |
| 1900 |
|
...other stuff... ( \( )? [^()]+ (?(-1) \) ) ... |
| 1901 |
|
</pre> |
| 1902 |
|
This makes the fragment independent of the parentheses in the larger pattern. |
| 1903 |
</P> |
</P> |
| 1904 |
|
<br><b> |
| 1905 |
|
Checking for a used subpattern by name |
| 1906 |
|
</b><br> |
| 1907 |
<P> |
<P> |
| 1908 |
If the condition is not a sequence of digits or (R), it must be an assertion. |
Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a used |
| 1909 |
|
subpattern by name. For compatibility with earlier versions of PCRE, which had |
| 1910 |
|
this facility before Perl, the syntax (?(name)...) is also recognized. However, |
| 1911 |
|
there is a possible ambiguity with this syntax, because subpattern names may |
| 1912 |
|
consist entirely of digits. PCRE looks first for a named subpattern; if it |
| 1913 |
|
cannot find one and the name consists entirely of digits, PCRE looks for a |
| 1914 |
|
subpattern of that number, which must be greater than zero. Using subpattern |
| 1915 |
|
names that consist entirely of digits is not recommended. |
| 1916 |
|
</P> |
| 1917 |
|
<P> |
| 1918 |
|
Rewriting the above example to use a named subpattern gives this: |
| 1919 |
|
<pre> |
| 1920 |
|
(?<OPEN> \( )? [^()]+ (?(<OPEN>) \) ) |
| 1921 |
|
</pre> |
| 1922 |
|
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> |
| 1926 |
|
<br><b> |
| 1927 |
|
Checking for pattern recursion |
| 1928 |
|
</b><br> |
| 1929 |
|
<P> |
| 1930 |
|
If the condition is the string (R), and there is no subpattern with the name R, |
| 1931 |
|
the condition is true if a recursive call to the whole pattern or any |
| 1932 |
|
subpattern has been made. If digits or a name preceded by ampersand follow the |
| 1933 |
|
letter R, for example: |
| 1934 |
|
<pre> |
| 1935 |
|
(?(R3)...) or (?(R&name)...) |
| 1936 |
|
</pre> |
| 1937 |
|
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 |
| 1939 |
|
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> |
| 1943 |
|
<P> |
| 1944 |
|
At "top level", all these recursion test conditions are false. |
| 1945 |
|
<a href="#recursion">The syntax for recursive patterns</a> |
| 1946 |
|
is described below. |
| 1947 |
|
</P> |
| 1948 |
|
<br><b> |
| 1949 |
|
Defining subpatterns for use by reference only |
| 1950 |
|
</b><br> |
| 1951 |
|
<P> |
| 1952 |
|
If the condition is the string (DEFINE), and there is no subpattern with the |
| 1953 |
|
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 |
| 1955 |
|
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 |
| 1957 |
|
<a href="#subpatternsassubroutines">"subroutines"</a> |
| 1958 |
|
is described below.) For example, a pattern to match an IPv4 address could be |
| 1959 |
|
written like this (ignore whitespace and line breaks): |
| 1960 |
|
<pre> |
| 1961 |
|
(?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) ) |
| 1962 |
|
\b (?&byte) (\.(?&byte)){3} \b |
| 1963 |
|
</pre> |
| 1964 |
|
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 |
| 1966 |
|
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. The rest of the |
| 1968 |
|
pattern uses references to the named group to match the four dot-separated |
| 1969 |
|
components of an IPv4 address, insisting on a word boundary at each end. |
| 1970 |
|
</P> |
| 1971 |
|
<br><b> |
| 1972 |
|
Assertion conditions |
| 1973 |
|
</b><br> |
| 1974 |
|
<P> |
| 1975 |
|
If the condition is not in any of the above formats, it must be an assertion. |
| 1976 |
This may be a positive or negative lookahead or lookbehind assertion. Consider |
This may be a positive or negative lookahead or lookbehind assertion. Consider |
| 1977 |
this pattern, again containing non-significant white space, and with the two |
this pattern, again containing non-significant white space, and with the two |
| 1978 |
alternatives on the second line: |
alternatives on the second line: |
| 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="SEC17" 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 |
| 1995 |
</P> |
</P> |
| 1996 |
<P> |
<P> |
| 1997 |
If the PCRE_EXTENDED option is set, an unescaped # character outside a |
If the PCRE_EXTENDED option is set, an unescaped # character outside a |
| 1998 |
character class introduces a comment that continues up to the next newline |
character class introduces a comment that continues to immediately after the |
| 1999 |
character in the pattern. |
next newline in the pattern. |
| 2000 |
</P> |
<a name="recursion"></a></P> |
| 2001 |
<br><a name="SEC18" 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 |
| 2005 |
be done is to use a pattern that matches up to some fixed depth of nesting. It |
be done is to use a pattern that matches up to some fixed depth of nesting. It |
| 2006 |
is not possible to handle an arbitrary nesting depth. Perl provides a facility |
is not possible to handle an arbitrary nesting depth. |
| 2007 |
that allows regular expressions to recurse (amongst other things). It does this |
</P> |
| 2008 |
by interpolating Perl code in the expression at run time, and the code can |
<P> |
| 2009 |
refer to the expression itself. A Perl pattern to solve the parentheses problem |
For some time, Perl has provided a facility that allows regular expressions to |
| 2010 |
can be created like this: |
recurse (amongst other things). It does this by interpolating Perl code in the |
| 2011 |
|
expression at run time, and the code can refer to the expression itself. A Perl |
| 2012 |
|
pattern using code interpolation to solve the parentheses problem can be |
| 2013 |
|
created like this: |
| 2014 |
<pre> |
<pre> |
| 2015 |
$re = qr{\( (?: (?>[^()]+) | (?p{$re}) )* \)}x; |
$re = qr{\( (?: (?>[^()]+) | (?p{$re}) )* \)}x; |
| 2016 |
</pre> |
</pre> |
| 2017 |
The (?p{...}) item interpolates Perl code at run time, and in this case refers |
The (?p{...}) item interpolates Perl code at run time, and in this case refers |
| 2018 |
recursively to the pattern in which it appears. Obviously, PCRE cannot support |
recursively to the pattern in which it appears. |
| 2019 |
the interpolation of Perl code. Instead, it supports some special syntax for |
</P> |
| 2020 |
recursion of the entire pattern, and also for individual subpattern recursion. |
<P> |
| 2021 |
|
Obviously, PCRE cannot support the interpolation of Perl code. Instead, it |
| 2022 |
|
supports special syntax for recursion of the entire pattern, and also for |
| 2023 |
|
individual subpattern recursion. After its introduction in PCRE and Python, |
| 2024 |
|
this kind of recursion was subsequently introduced into Perl at release 5.10. |
| 2025 |
</P> |
</P> |
| 2026 |
<P> |
<P> |
| 2027 |
The special item that consists of (? followed by a number greater than zero and |
A special item that consists of (? followed by a number greater than zero and a |
| 2028 |
a closing parenthesis is a recursive call of the subpattern of the given |
closing parenthesis is a recursive call of the subpattern of the given number, |
| 2029 |
number, provided that it occurs inside that subpattern. (If not, it is a |
provided that it occurs inside that subpattern. (If not, it is a |
| 2030 |
"subroutine" call, which is described in the next section.) The special item |
<a href="#subpatternsassubroutines">"subroutine"</a> |
| 2031 |
(?R) is a recursive call of the entire regular expression. |
call, which is described in the next section.) The special item (?R) or (?0) is |
| 2032 |
|
a recursive call of the entire regular expression. |
| 2033 |
</P> |
</P> |
| 2034 |
<P> |
<P> |
| 2035 |
For example, this PCRE pattern solves the nested parentheses problem (assume |
This PCRE pattern solves the nested parentheses problem (assume the |
| 2036 |
the 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. For this, PCRE uses (?P>name), which is an extension to |
<P> |
| 2056 |
the Python syntax that PCRE uses for named parentheses (Perl does not provide |
In a larger pattern, keeping track of parenthesis numbers can be tricky. This |
| 2057 |
named parentheses). We could rewrite the above example as follows: |
is made easier by the use of relative references (a Perl 5.10 feature). |
| 2058 |
<pre> |
Instead of (?1) in the pattern above you can write (?-2) to refer to the second |
| 2059 |
(?P<pn> \( ( (?>[^()]+) | (?P>pn) )* \) ) |
most recently opened parentheses preceding the recursion. In other words, a |
| 2060 |
</pre> |
negative number counts capturing parentheses leftwards from the point at which |
| 2061 |
This particular example pattern contains nested unlimited repeats, and so the |
it is encountered. |
| 2062 |
use of atomic grouping for matching strings of non-parentheses is important |
</P> |
| 2063 |
when applying the pattern to strings that do not match. For example, when this |
<P> |
| 2064 |
pattern is applied to |
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> |
| 2075 |
|
(?<pn> \( ( [^()]++ | (?&pn) )* \) ) |
| 2076 |
|
</pre> |
| 2077 |
|
If there is more than one subpattern with the same name, the earliest one is |
| 2078 |
|
used. |
| 2079 |
|
</P> |
| 2080 |
|
<P> |
| 2081 |
|
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 |
|
the next section 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="SEC19" 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. An earlier example pointed out that the |
subroutine in a programming language. The "called" subpattern may be defined |
| 2204 |
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> |
| 2218 |
(sens|respons)e and (?1)ibility |
(sens|respons)e and (?1)ibility |
| 2219 |
</pre> |
</pre> |
| 2220 |
is used, it does match "sense and responsibility" as well as the other two |
is used, it does match "sense and responsibility" as well as the other two |
| 2221 |
strings. Such references must, however, follow the subpattern to which they |
strings. Another example is given in the discussion of DEFINE above. |
| 2222 |
refer. |
</P> |
| 2223 |
|
<P> |
| 2224 |
|
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 |
| 2226 |
|
re-entered, even if it contains untried alternatives and there is a subsequent |
| 2227 |
|
matching failure. Any capturing parentheses that are set during the subroutine |
| 2228 |
|
call revert to their previous values afterwards. |
| 2229 |
</P> |
</P> |
| 2230 |
<br><a name="SEC20" href="#TOC1">CALLOUTS</a><br> |
<P> |
| 2231 |
|
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 |
| 2233 |
|
changed for different calls. For example, consider this pattern: |
| 2234 |
|
<pre> |
| 2235 |
|
(abc)(?i:(?-1)) |
| 2236 |
|
</pre> |
| 2237 |
|
It matches "abcabc". It does not match "abcABC" because the change of |
| 2238 |
|
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> |
| 2258 |
|
<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="SEC25" href="#TOC1">BACKTRACKING CONTROL</a><br> |
| 2294 |
<P> |
<P> |
| 2295 |
Last updated: 28 February 2005 |
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> |
| 2416 |
|
<b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3), |
| 2417 |
|
<b>pcresyntax</b>(3), <b>pcre</b>(3). |
| 2418 |
|
</P> |
| 2419 |
|
<br><a name="SEC27" href="#TOC1">AUTHOR</a><br> |
| 2420 |
|
<P> |
| 2421 |
|
Philip Hazel |
| 2422 |
|
<br> |
| 2423 |
|
University Computing Service |
| 2424 |
|
<br> |
| 2425 |
|
Cambridge CB2 3QH, England. |
| 2426 |
|
<br> |
| 2427 |
|
</P> |
| 2428 |
|
<br><a name="SEC28" href="#TOC1">REVISION</a><br> |
| 2429 |
|
<P> |
| 2430 |
|
Last updated: 06 March 2010 |
| 2431 |
|
<br> |
| 2432 |
|
Copyright © 1997-2010 University of Cambridge. |
| 2433 |
<br> |
<br> |
|
Copyright © 1997-2005 University of Cambridge. |
|
| 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>. |
| 2436 |
</p> |
</p> |