| 36 |
\fBpcrematching\fP |
\fBpcrematching\fP |
| 37 |
.\" |
.\" |
| 38 |
page. |
page. |
| 39 |
.P |
. |
| 40 |
|
. |
| 41 |
|
.SH "CHARACTERS AND METACHARACTERS" |
| 42 |
|
.rs |
| 43 |
|
.sp |
| 44 |
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 |
| 45 |
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 |
| 46 |
corresponding characters in the subject. As a trivial example, the pattern |
corresponding characters in the subject. As a trivial example, the pattern |
| 64 |
.P |
.P |
| 65 |
There are two different sets of metacharacters: those that are recognized |
There are two different sets of metacharacters: those that are recognized |
| 66 |
anywhere in the pattern except within square brackets, and those that are |
anywhere in the pattern except within square brackets, and those that are |
| 67 |
recognized in square brackets. Outside square brackets, the metacharacters are |
recognized within square brackets. Outside square brackets, the metacharacters |
| 68 |
as follows: |
are as follows: |
| 69 |
.sp |
.sp |
| 70 |
\e general escape character with several uses |
\e general escape character with several uses |
| 71 |
^ assert start of string (or line, in multiline mode) |
^ assert start of string (or line, in multiline mode) |
| 96 |
.sp |
.sp |
| 97 |
The following sections describe the use of each of the metacharacters. |
The following sections describe the use of each of the metacharacters. |
| 98 |
. |
. |
| 99 |
|
. |
| 100 |
.SH BACKSLASH |
.SH BACKSLASH |
| 101 |
.rs |
.rs |
| 102 |
.sp |
.sp |
| 103 |
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 |
| 104 |
non-alphanumeric character, it takes away any special meaning that character may |
non-alphanumeric character, it takes away any special meaning that character |
| 105 |
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 |
| 106 |
outside character classes. |
outside character classes. |
| 107 |
.P |
.P |
| 108 |
For example, if you want to match a * character, you write \e* in the pattern. |
For example, if you want to match a * character, you write \e* in the pattern. |
| 113 |
.P |
.P |
| 114 |
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 |
| 115 |
pattern (other than in a character class) and characters between a # outside |
pattern (other than in a character class) and characters between a # outside |
| 116 |
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 |
| 117 |
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. |
|
| 118 |
.P |
.P |
| 119 |
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 |
| 120 |
can do so by putting them between \eQ and \eE. This is different from Perl in |
can do so by putting them between \eQ and \eE. This is different from Perl in |
| 152 |
\et tab (hex 09) |
\et tab (hex 09) |
| 153 |
\eddd character with octal code ddd, or backreference |
\eddd character with octal code ddd, or backreference |
| 154 |
\exhh character with hex code hh |
\exhh character with hex code hh |
| 155 |
\ex{hhh..} character with hex code hhh... (UTF-8 mode only) |
\ex{hhh..} character with hex code hhh.. |
| 156 |
.sp |
.sp |
| 157 |
The precise effect of \ecx is as follows: if x is a lower case letter, it |
The precise effect of \ecx is as follows: if x is a lower case letter, it |
| 158 |
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. |
| 160 |
7B. |
7B. |
| 161 |
.P |
.P |
| 162 |
After \ex, from zero to two hexadecimal digits are read (letters can be in |
After \ex, from zero to two hexadecimal digits are read (letters can be in |
| 163 |
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 \ex{ |
| 164 |
appear between \ex{ 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 |
| 165 |
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 hexadecimal value |
| 166 |
other than hexadecimal digits appear between \ex{ and }, or if there is no |
is 7FFFFFFF). If characters other than hexadecimal digits appear between \ex{ |
| 167 |
terminating }, this form of escape is not recognized. Instead, the initial |
and }, or if there is no terminating }, this form of escape is not recognized. |
| 168 |
\ex will be interpreted as a basic hexadecimal escape, with no following |
Instead, the initial \ex will be interpreted as a basic hexadecimal escape, |
| 169 |
digits, giving a character whose value is zero. |
with no following digits, giving a character whose value is zero. |
| 170 |
.P |
.P |
| 171 |
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 |
| 172 |
syntaxes for \ex when PCRE is in UTF-8 mode. There is no difference in the |
syntaxes for \ex. There is no difference in the way they are handled. For |
| 173 |
way they are handled. For example, \exdc is exactly the same as \ex{dc}. |
example, \exdc is exactly the same as \ex{dc}. |
| 174 |
.P |
.P |
| 175 |
After \e0 up to two further octal digits are read. In both cases, if there |
After \e0 up to two further octal digits are read. If there are fewer than two |
| 176 |
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 \e0\ex\e07 |
| 177 |
sequence \e0\ex\e07 specifies two binary zeros followed by a BEL character |
specifies two binary zeros followed by a BEL character (code value 7). Make |
| 178 |
(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 |
| 179 |
pattern character that follows is itself an octal digit. |
follows is itself an octal digit. |
| 180 |
.P |
.P |
| 181 |
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. |
| 182 |
Outside a character class, PCRE reads it and any following digits as a decimal |
Outside a character class, PCRE reads it and any following digits as a decimal |
| 195 |
.P |
.P |
| 196 |
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 |
| 197 |
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 |
| 198 |
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 |
| 199 |
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 |
| 200 |
For example: |
character specified in octal must be less than \e400. In UTF-8 mode, values up |
| 201 |
|
to \e777 are permitted. For example: |
| 202 |
.sp |
.sp |
| 203 |
\e040 is another way of writing a space |
\e040 is another way of writing a space |
| 204 |
.\" JOIN |
.\" JOIN |
| 223 |
Note that octal values of 100 or greater must not be introduced by a leading |
Note that octal values of 100 or greater must not be introduced by a leading |
| 224 |
zero, because no more than three octal digits are ever read. |
zero, because no more than three octal digits are ever read. |
| 225 |
.P |
.P |
| 226 |
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 |
| 227 |
(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 |
| 228 |
addition, inside a character class, the sequence \eb is interpreted as the |
sequence \eb is interpreted as the backspace character (hex 08), and the |
| 229 |
backspace character (hex 08), and the sequence \eX is interpreted as the |
sequences \eR and \eX are interpreted as the characters "R" and "X", |
| 230 |
character "X". Outside a character class, these sequences have different |
respectively. Outside a character class, these sequences have different |
| 231 |
meanings |
meanings |
| 232 |
.\" HTML <a href="#uniextseq"> |
.\" HTML <a href="#uniextseq"> |
| 233 |
.\" </a> |
.\" </a> |
| 235 |
.\" |
.\" |
| 236 |
. |
. |
| 237 |
. |
. |
| 238 |
|
.SS "Absolute and relative back references" |
| 239 |
|
.rs |
| 240 |
|
.sp |
| 241 |
|
The sequence \eg followed by a positive or negative number, optionally enclosed |
| 242 |
|
in braces, is an absolute or relative back reference. Back references are |
| 243 |
|
discussed |
| 244 |
|
.\" HTML <a href="#backreferences"> |
| 245 |
|
.\" </a> |
| 246 |
|
later, |
| 247 |
|
.\" |
| 248 |
|
following the discussion of |
| 249 |
|
.\" HTML <a href="#subpattern"> |
| 250 |
|
.\" </a> |
| 251 |
|
parenthesized subpatterns. |
| 252 |
|
.\" |
| 253 |
|
. |
| 254 |
|
. |
| 255 |
.SS "Generic character types" |
.SS "Generic character types" |
| 256 |
.rs |
.rs |
| 257 |
.sp |
.sp |
| 258 |
The third use of backslash is for specifying generic character types. The |
Another use of backslash is for specifying generic character types. The |
| 259 |
following are always recognized: |
following are always recognized: |
| 260 |
.sp |
.sp |
| 261 |
\ed any decimal digit |
\ed any decimal digit |
| 275 |
.P |
.P |
| 276 |
For compatibility with Perl, \es does not match the VT character (code 11). |
For compatibility with Perl, \es does not match the VT character (code 11). |
| 277 |
This makes it different from the the POSIX "space" class. The \es characters |
This makes it different from the the POSIX "space" class. The \es characters |
| 278 |
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 |
| 279 |
|
included in a Perl script, \es may match the VT character. In PCRE, it never |
| 280 |
|
does.) |
| 281 |
.P |
.P |
| 282 |
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 |
| 283 |
letter or digit. The definition of letters and digits is controlled by PCRE's |
letter or digit. The definition of letters and digits is controlled by PCRE's |
| 296 |
.P |
.P |
| 297 |
In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or |
In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or |
| 298 |
\ew, and always match \eD, \eS, and \eW. This is true even when Unicode |
\ew, and always match \eD, \eS, and \eW. This is true even when Unicode |
| 299 |
character property support is available. |
character property support is available. The use of locales with Unicode is |
| 300 |
|
discouraged. |
| 301 |
|
. |
| 302 |
|
. |
| 303 |
|
.SS "Newline sequences" |
| 304 |
|
.rs |
| 305 |
|
.sp |
| 306 |
|
Outside a character class, the escape sequence \eR matches any Unicode newline |
| 307 |
|
sequence. This is an extension to Perl. In non-UTF-8 mode \eR is equivalent to |
| 308 |
|
the following: |
| 309 |
|
.sp |
| 310 |
|
(?>\er\en|\en|\ex0b|\ef|\er|\ex85) |
| 311 |
|
.sp |
| 312 |
|
This is an example of an "atomic group", details of which are given |
| 313 |
|
.\" HTML <a href="#atomicgroup"> |
| 314 |
|
.\" </a> |
| 315 |
|
below. |
| 316 |
|
.\" |
| 317 |
|
This particular group matches either the two-character sequence CR followed by |
| 318 |
|
LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab, |
| 319 |
|
U+000B), FF (formfeed, U+000C), CR (carriage return, U+000D), or NEL (next |
| 320 |
|
line, U+0085). The two-character sequence is treated as a single unit that |
| 321 |
|
cannot be split. |
| 322 |
|
.P |
| 323 |
|
In UTF-8 mode, two additional characters whose codepoints are greater than 255 |
| 324 |
|
are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029). |
| 325 |
|
Unicode character property support is not needed for these characters to be |
| 326 |
|
recognized. |
| 327 |
|
.P |
| 328 |
|
Inside a character class, \eR matches the letter "R". |
| 329 |
. |
. |
| 330 |
. |
. |
| 331 |
.\" HTML <a name="uniextseq"></a> |
.\" HTML <a name="uniextseq"></a> |
| 333 |
.rs |
.rs |
| 334 |
.sp |
.sp |
| 335 |
When PCRE is built with Unicode character property support, three additional |
When PCRE is built with Unicode character property support, three additional |
| 336 |
escape sequences to match generic character types are available when UTF-8 mode |
escape sequences to match character properties are available when UTF-8 mode |
| 337 |
is selected. They are: |
is selected. They are: |
| 338 |
.sp |
.sp |
| 339 |
\ep{\fIxx\fP} a character with the \fIxx\fP property |
\ep{\fIxx\fP} a character with the \fIxx\fP property |
| 340 |
\eP{\fIxx\fP} a character without the \fIxx\fP property |
\eP{\fIxx\fP} a character without the \fIxx\fP property |
| 341 |
\eX an extended Unicode sequence |
\eX an extended Unicode sequence |
| 342 |
.sp |
.sp |
| 343 |
The property names represented by \fIxx\fP above are limited to the |
The property names represented by \fIxx\fP above are limited to the Unicode |
| 344 |
Unicode general category properties. Each character has exactly one such |
script names, the general category properties, and "Any", which matches any |
| 345 |
property, specified by a two-letter abbreviation. For compatibility with Perl, |
character (including newline). Other properties such as "InMusicalSymbols" are |
| 346 |
negation can be specified by including a circumflex between the opening brace |
not currently supported by PCRE. Note that \eP{Any} does not match any |
| 347 |
and the property name. For example, \ep{^Lu} is the same as \eP{Lu}. |
characters, so always causes a match failure. |
| 348 |
.P |
.P |
| 349 |
If only one letter is specified with \ep or \eP, it includes all the properties |
Sets of Unicode characters are defined as belonging to certain scripts. A |
| 350 |
that start with that letter. In this case, in the absence of negation, the |
character from one of these sets can be matched using a script name. For |
| 351 |
curly brackets in the escape sequence are optional; these two examples have |
example: |
| 352 |
the same effect: |
.sp |
| 353 |
|
\ep{Greek} |
| 354 |
|
\eP{Han} |
| 355 |
|
.sp |
| 356 |
|
Those that are not part of an identified script are lumped together as |
| 357 |
|
"Common". The current list of scripts is: |
| 358 |
|
.P |
| 359 |
|
Arabic, |
| 360 |
|
Armenian, |
| 361 |
|
Balinese, |
| 362 |
|
Bengali, |
| 363 |
|
Bopomofo, |
| 364 |
|
Braille, |
| 365 |
|
Buginese, |
| 366 |
|
Buhid, |
| 367 |
|
Canadian_Aboriginal, |
| 368 |
|
Cherokee, |
| 369 |
|
Common, |
| 370 |
|
Coptic, |
| 371 |
|
Cuneiform, |
| 372 |
|
Cypriot, |
| 373 |
|
Cyrillic, |
| 374 |
|
Deseret, |
| 375 |
|
Devanagari, |
| 376 |
|
Ethiopic, |
| 377 |
|
Georgian, |
| 378 |
|
Glagolitic, |
| 379 |
|
Gothic, |
| 380 |
|
Greek, |
| 381 |
|
Gujarati, |
| 382 |
|
Gurmukhi, |
| 383 |
|
Han, |
| 384 |
|
Hangul, |
| 385 |
|
Hanunoo, |
| 386 |
|
Hebrew, |
| 387 |
|
Hiragana, |
| 388 |
|
Inherited, |
| 389 |
|
Kannada, |
| 390 |
|
Katakana, |
| 391 |
|
Kharoshthi, |
| 392 |
|
Khmer, |
| 393 |
|
Lao, |
| 394 |
|
Latin, |
| 395 |
|
Limbu, |
| 396 |
|
Linear_B, |
| 397 |
|
Malayalam, |
| 398 |
|
Mongolian, |
| 399 |
|
Myanmar, |
| 400 |
|
New_Tai_Lue, |
| 401 |
|
Nko, |
| 402 |
|
Ogham, |
| 403 |
|
Old_Italic, |
| 404 |
|
Old_Persian, |
| 405 |
|
Oriya, |
| 406 |
|
Osmanya, |
| 407 |
|
Phags_Pa, |
| 408 |
|
Phoenician, |
| 409 |
|
Runic, |
| 410 |
|
Shavian, |
| 411 |
|
Sinhala, |
| 412 |
|
Syloti_Nagri, |
| 413 |
|
Syriac, |
| 414 |
|
Tagalog, |
| 415 |
|
Tagbanwa, |
| 416 |
|
Tai_Le, |
| 417 |
|
Tamil, |
| 418 |
|
Telugu, |
| 419 |
|
Thaana, |
| 420 |
|
Thai, |
| 421 |
|
Tibetan, |
| 422 |
|
Tifinagh, |
| 423 |
|
Ugaritic, |
| 424 |
|
Yi. |
| 425 |
|
.P |
| 426 |
|
Each character has exactly one general category property, specified by a |
| 427 |
|
two-letter abbreviation. For compatibility with Perl, negation can be specified |
| 428 |
|
by including a circumflex between the opening brace and the property name. For |
| 429 |
|
example, \ep{^Lu} is the same as \eP{Lu}. |
| 430 |
|
.P |
| 431 |
|
If only one letter is specified with \ep or \eP, it includes all the general |
| 432 |
|
category properties that start with that letter. In this case, in the absence |
| 433 |
|
of negation, the curly brackets in the escape sequence are optional; these two |
| 434 |
|
examples have the same effect: |
| 435 |
.sp |
.sp |
| 436 |
\ep{L} |
\ep{L} |
| 437 |
\epL |
\epL |
| 438 |
.sp |
.sp |
| 439 |
The following property codes are supported: |
The following general category property codes are supported: |
| 440 |
.sp |
.sp |
| 441 |
C Other |
C Other |
| 442 |
Cc Control |
Cc Control |
| 482 |
Zp Paragraph separator |
Zp Paragraph separator |
| 483 |
Zs Space separator |
Zs Space separator |
| 484 |
.sp |
.sp |
| 485 |
Extended properties such as "Greek" or "InMusicalSymbols" are not supported by |
The special property L& is also supported: it matches a character that has |
| 486 |
PCRE. |
the Lu, Ll, or Lt property, in other words, a letter that is not classified as |
| 487 |
|
a modifier or "other". |
| 488 |
|
.P |
| 489 |
|
The long synonyms for these properties that Perl supports (such as \ep{Letter}) |
| 490 |
|
are not supported by PCRE, nor is it permitted to prefix any of these |
| 491 |
|
properties with "Is". |
| 492 |
|
.P |
| 493 |
|
No character that is in the Unicode table has the Cn (unassigned) property. |
| 494 |
|
Instead, this property is assumed for any code point that is not in the |
| 495 |
|
Unicode table. |
| 496 |
.P |
.P |
| 497 |
Specifying caseless matching does not affect these escape sequences. For |
Specifying caseless matching does not affect these escape sequences. For |
| 498 |
example, \ep{Lu} always matches only upper case letters. |
example, \ep{Lu} always matches only upper case letters. |
| 522 |
.SS "Simple assertions" |
.SS "Simple assertions" |
| 523 |
.rs |
.rs |
| 524 |
.sp |
.sp |
| 525 |
The fourth use of backslash is for certain simple assertions. An assertion |
The final use of backslash is for certain simple assertions. An assertion |
| 526 |
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, |
| 527 |
without consuming any characters from the subject string. The use of |
without consuming any characters from the subject string. The use of |
| 528 |
subpatterns for more complicated assertions is described |
subpatterns for more complicated assertions is described |
| 530 |
.\" </a> |
.\" </a> |
| 531 |
below. |
below. |
| 532 |
.\" |
.\" |
| 533 |
The backslashed |
The backslashed assertions are: |
|
assertions are: |
|
| 534 |
.sp |
.sp |
| 535 |
\eb matches at a word boundary |
\eb matches at a word boundary |
| 536 |
\eB matches when not at a word boundary |
\eB matches when not at a word boundary |
| 537 |
\eA matches at start of subject |
\eA matches at the start of the subject |
| 538 |
\eZ matches at end of subject or before newline at end |
\eZ matches at the end of the subject |
| 539 |
\ez matches at end of subject |
also matches before a newline at the end of the subject |
| 540 |
\eG matches at first matching position in subject |
\ez matches only at the end of the subject |
| 541 |
|
\eG matches at the first matching position in the subject |
| 542 |
.sp |
.sp |
| 543 |
These assertions may not appear in character classes (but note that \eb has a |
These assertions may not appear in character classes (but note that \eb has a |
| 544 |
different meaning, namely the backspace character, inside a character class). |
different meaning, namely the backspace character, inside a character class). |
| 556 |
circumflex and dollar metacharacters. However, if the \fIstartoffset\fP |
circumflex and dollar metacharacters. However, if the \fIstartoffset\fP |
| 557 |
argument of \fBpcre_exec()\fP is non-zero, indicating that matching is to start |
argument of \fBpcre_exec()\fP is non-zero, indicating that matching is to start |
| 558 |
at a point other than the beginning of the subject, \eA can never match. The |
at a point other than the beginning of the subject, \eA can never match. The |
| 559 |
difference between \eZ and \ez is that \eZ matches before a newline that is the |
difference between \eZ and \ez is that \eZ matches before a newline at the end |
| 560 |
last character of the string as well as at the end of the string, whereas \ez |
of the string as well as at the very end, whereas \ez matches only at the end. |
|
matches only at the end. |
|
| 561 |
.P |
.P |
| 562 |
The \eG assertion is true only when the current matching position is at the |
The \eG assertion is true only when the current matching position is at the |
| 563 |
start point of the match, as specified by the \fIstartoffset\fP argument of |
start point of the match, as specified by the \fIstartoffset\fP argument of |
| 601 |
.P |
.P |
| 602 |
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 |
| 603 |
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 |
| 604 |
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 |
| 605 |
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 |
| 606 |
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 |
| 607 |
Dollar has no special meaning in a character class. |
character class. |
| 608 |
.P |
.P |
| 609 |
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 |
| 610 |
the string, by setting the PCRE_DOLLAR_ENDONLY option at compile time. This |
the string, by setting the PCRE_DOLLAR_ENDONLY option at compile time. This |
| 611 |
does not affect the \eZ assertion. |
does not affect the \eZ assertion. |
| 612 |
.P |
.P |
| 613 |
The meanings of the circumflex and dollar characters are changed if the |
The meanings of the circumflex and dollar characters are changed if the |
| 614 |
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 |
| 615 |
after and immediately before an internal newline character, respectively, in |
immediately after internal newlines as well as at the start of the subject |
| 616 |
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 |
| 617 |
the pattern /^abc$/ matches the subject string "def\enabc" (where \en |
matches before any newlines in the string, as well as at the very end, when |
| 618 |
represents a newline character) in multiline mode, but not otherwise. |
PCRE_MULTILINE is set. When newline is specified as the two-character |
| 619 |
Consequently, patterns that are anchored in single line mode because all |
sequence CRLF, isolated CR and LF characters do not indicate newlines. |
| 620 |
branches start with ^ are not anchored in multiline mode, and a match for |
.P |
| 621 |
circumflex is possible when the \fIstartoffset\fP argument of \fBpcre_exec()\fP |
For example, the pattern /^abc$/ matches the subject string "def\enabc" (where |
| 622 |
is non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is |
\en represents a newline) in multiline mode, but not otherwise. Consequently, |
| 623 |
set. |
patterns that are anchored in single line mode because all branches start with |
| 624 |
|
^ are not anchored in multiline mode, and a match for circumflex is possible |
| 625 |
|
when the \fIstartoffset\fP argument of \fBpcre_exec()\fP is non-zero. The |
| 626 |
|
PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set. |
| 627 |
.P |
.P |
| 628 |
Note that the sequences \eA, \eZ, and \ez can be used to match the start and |
Note that the sequences \eA, \eZ, and \ez can be used to match the start and |
| 629 |
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 |
| 630 |
\eA it is always anchored, whether PCRE_MULTILINE is set or not. |
\eA it is always anchored, whether or not PCRE_MULTILINE is set. |
| 631 |
. |
. |
| 632 |
. |
. |
| 633 |
.SH "FULL STOP (PERIOD, DOT)" |
.SH "FULL STOP (PERIOD, DOT)" |
| 634 |
.rs |
.rs |
| 635 |
.sp |
.sp |
| 636 |
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 |
| 637 |
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 |
| 638 |
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. |
| 639 |
byte long, except (by default) newline. If the PCRE_DOTALL option is set, |
.P |
| 640 |
dots match newlines as well. The handling of dot is entirely independent of the |
When a line ending is defined as a single character, dot never matches that |
| 641 |
handling of circumflex and dollar, the only relationship being that they both |
character; when the two-character sequence CRLF is used, dot does not match CR |
| 642 |
involve newline characters. Dot has no special meaning in a character class. |
if it is immediately followed by LF, but otherwise it matches all characters |
| 643 |
|
(including isolated CRs and LFs). When any Unicode line endings are being |
| 644 |
|
recognized, dot does not match CR or LF or any of the other line ending |
| 645 |
|
characters. |
| 646 |
|
.P |
| 647 |
|
The behaviour of dot with regard to newlines can be changed. If the PCRE_DOTALL |
| 648 |
|
option is set, a dot matches any one character, without exception. If the |
| 649 |
|
two-character sequence CRLF is present in the subject string, it takes two dots |
| 650 |
|
to match it. |
| 651 |
|
.P |
| 652 |
|
The handling of dot is entirely independent of the handling of circumflex and |
| 653 |
|
dollar, the only relationship being that they both involve newlines. Dot has no |
| 654 |
|
special meaning in a character class. |
| 655 |
. |
. |
| 656 |
. |
. |
| 657 |
.SH "MATCHING A SINGLE BYTE" |
.SH "MATCHING A SINGLE BYTE" |
| 658 |
.rs |
.rs |
| 659 |
.sp |
.sp |
| 660 |
Outside a character class, the escape sequence \eC matches any one byte, both |
Outside a character class, the escape sequence \eC matches any one byte, both |
| 661 |
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 |
| 662 |
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 |
| 663 |
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, |
| 664 |
may be a malformed UTF-8 string. For this reason, the \eC escape sequence is |
what remains in the string may be a malformed UTF-8 string. For this reason, |
| 665 |
best avoided. |
the \eC escape sequence is best avoided. |
| 666 |
.P |
.P |
| 667 |
PCRE does not allow \eC to appear in lookbehind assertions |
PCRE does not allow \eC to appear in lookbehind assertions |
| 668 |
.\" HTML <a href="#lookbehind"> |
.\" HTML <a href="#lookbehind"> |
| 713 |
ensure that PCRE is compiled with Unicode property support as well as with |
ensure that PCRE is compiled with Unicode property support as well as with |
| 714 |
UTF-8 support. |
UTF-8 support. |
| 715 |
.P |
.P |
| 716 |
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 |
| 717 |
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 |
| 718 |
such as [^a] will always match a newline. |
whatever setting of the PCRE_DOTALL and PCRE_MULTILINE options is used. A class |
| 719 |
|
such as [^a] always matches one of these characters. |
| 720 |
.P |
.P |
| 721 |
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 |
| 722 |
character class. For example, [d-m] matches any letter between d and m, |
character class. For example, [d-m] matches any letter between d and m, |
| 815 |
gilbert|sullivan |
gilbert|sullivan |
| 816 |
.sp |
.sp |
| 817 |
matches either "gilbert" or "sullivan". Any number of alternatives may appear, |
matches either "gilbert" or "sullivan". Any number of alternatives may appear, |
| 818 |
and an empty alternative is permitted (matching the empty string). |
and an empty alternative is permitted (matching the empty string). The matching |
| 819 |
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 |
| 820 |
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 |
|
| 821 |
.\" HTML <a href="#subpattern"> |
.\" HTML <a href="#subpattern"> |
| 822 |
.\" </a> |
.\" </a> |
| 823 |
(defined below), |
(defined below), |
| 851 |
the global options (and it will therefore show up in data extracted by the |
the global options (and it will therefore show up in data extracted by the |
| 852 |
\fBpcre_fullinfo()\fP function). |
\fBpcre_fullinfo()\fP function). |
| 853 |
.P |
.P |
| 854 |
An option change within a subpattern affects only that part of the current |
An option change within a subpattern (see below for a description of |
| 855 |
pattern that follows it, so |
subpatterns) affects only that part of the current pattern that follows it, so |
| 856 |
.sp |
.sp |
| 857 |
(a(?i)b)c |
(a(?i)b)c |
| 858 |
.sp |
.sp |
| 868 |
option settings happen at compile time. There would be some very weird |
option settings happen at compile time. There would be some very weird |
| 869 |
behaviour otherwise. |
behaviour otherwise. |
| 870 |
.P |
.P |
| 871 |
The PCRE-specific options PCRE_UNGREEDY and PCRE_EXTRA can be changed in the |
The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be |
| 872 |
same way as the Perl-compatible options by using the characters U and X |
changed in the same way as the Perl-compatible options by using the characters |
| 873 |
respectively. The (?X) flag setting is special in that it must always occur |
J, U and X respectively. |
|
earlier in the pattern than any of the additional features it turns on, even |
|
|
when it is at top level. It is best to put it at the start. |
|
| 874 |
. |
. |
| 875 |
. |
. |
| 876 |
.\" HTML <a name="subpattern"></a> |
.\" HTML <a name="subpattern"></a> |
| 885 |
cat(aract|erpillar|) |
cat(aract|erpillar|) |
| 886 |
.sp |
.sp |
| 887 |
matches one of the words "cat", "cataract", or "caterpillar". Without the |
matches one of the words "cat", "cataract", or "caterpillar". Without the |
| 888 |
parentheses, it would match "cataract", "erpillar" or the empty string. |
parentheses, it would match "cataract", "erpillar" or an empty string. |
| 889 |
.sp |
.sp |
| 890 |
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 |
| 891 |
the whole pattern matches, that portion of the subject string that matched the |
the whole pattern matches, that portion of the subject string that matched the |
| 910 |
the ((?:red|white) (king|queen)) |
the ((?:red|white) (king|queen)) |
| 911 |
.sp |
.sp |
| 912 |
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 |
| 913 |
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. |
|
| 914 |
.P |
.P |
| 915 |
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 |
| 916 |
a non-capturing subpattern, the option letters may appear between the "?" and |
a non-capturing subpattern, the option letters may appear between the "?" and |
| 931 |
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 |
| 932 |
to keep track of the numbers in complicated regular expressions. Furthermore, |
to keep track of the numbers in complicated regular expressions. Furthermore, |
| 933 |
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 |
| 934 |
difficulty, PCRE supports the naming of subpatterns, something that Perl does |
difficulty, PCRE supports the naming of subpatterns. This feature was not |
| 935 |
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 |
| 936 |
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 |
| 937 |
.P |
the Perl and the Python syntax. |
| 938 |
Named capturing parentheses are still allocated numbers as well as names. The |
.P |
| 939 |
PCRE API provides function calls for extracting the name-to-number translation |
In PCRE, a subpattern can be named in one of three ways: (?<name>...) or |
| 940 |
table from a compiled pattern. There is also a convenience function for |
(?'name'...) as in Perl, or (?P<name>...) as in Python. References to capturing |
| 941 |
extracting a captured substring by name. For further details see the |
parentheses from other parts of the pattern, such as |
| 942 |
|
.\" HTML <a href="#backreferences"> |
| 943 |
|
.\" </a> |
| 944 |
|
backreferences, |
| 945 |
|
.\" |
| 946 |
|
.\" HTML <a href="#recursion"> |
| 947 |
|
.\" </a> |
| 948 |
|
recursion, |
| 949 |
|
.\" |
| 950 |
|
and |
| 951 |
|
.\" HTML <a href="#conditions"> |
| 952 |
|
.\" </a> |
| 953 |
|
conditions, |
| 954 |
|
.\" |
| 955 |
|
can be made by name as well as by number. |
| 956 |
|
.P |
| 957 |
|
Names consist of up to 32 alphanumeric characters and underscores. Named |
| 958 |
|
capturing parentheses are still allocated numbers as well as names, exactly as |
| 959 |
|
if the names were not present. The PCRE API provides function calls for |
| 960 |
|
extracting the name-to-number translation table from a compiled pattern. There |
| 961 |
|
is also a convenience function for extracting a captured substring by name. |
| 962 |
|
.P |
| 963 |
|
By default, a name must be unique within a pattern, but it is possible to relax |
| 964 |
|
this constraint by setting the PCRE_DUPNAMES option at compile time. This can |
| 965 |
|
be useful for patterns where only one instance of the named parentheses can |
| 966 |
|
match. Suppose you want to match the name of a weekday, either as a 3-letter |
| 967 |
|
abbreviation or as the full name, and in both cases you want to extract the |
| 968 |
|
abbreviation. This pattern (ignoring the line breaks) does the job: |
| 969 |
|
.sp |
| 970 |
|
(?<DN>Mon|Fri|Sun)(?:day)?| |
| 971 |
|
(?<DN>Tue)(?:sday)?| |
| 972 |
|
(?<DN>Wed)(?:nesday)?| |
| 973 |
|
(?<DN>Thu)(?:rsday)?| |
| 974 |
|
(?<DN>Sat)(?:urday)? |
| 975 |
|
.sp |
| 976 |
|
There are five capturing substrings, but only one is ever set after a match. |
| 977 |
|
The convenience function for extracting the data by name returns the substring |
| 978 |
|
for the first (and in this example, the only) subpattern of that name that |
| 979 |
|
matched. This saves searching to find which numbered subpattern it was. If you |
| 980 |
|
make a reference to a non-unique named subpattern from elsewhere in the |
| 981 |
|
pattern, the one that corresponds to the lowest number is used. For further |
| 982 |
|
details of the interfaces for handling named subpatterns, see the |
| 983 |
.\" HREF |
.\" HREF |
| 984 |
\fBpcreapi\fP |
\fBpcreapi\fP |
| 985 |
.\" |
.\" |
| 993 |
items: |
items: |
| 994 |
.sp |
.sp |
| 995 |
a literal data character |
a literal data character |
| 996 |
the . metacharacter |
the dot metacharacter |
| 997 |
the \eC escape sequence |
the \eC escape sequence |
| 998 |
the \eX escape sequence (in UTF-8 mode with Unicode properties) |
the \eX escape sequence (in UTF-8 mode with Unicode properties) |
| 999 |
|
the \eR escape sequence |
| 1000 |
an escape such as \ed that matches a single character |
an escape such as \ed that matches a single character |
| 1001 |
a character class |
a character class |
| 1002 |
a back reference (see next section) |
a back reference (see next section) |
| 1034 |
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 |
| 1035 |
previous item and the quantifier were not present. |
previous item and the quantifier were not present. |
| 1036 |
.P |
.P |
| 1037 |
For convenience (and historical compatibility) the three most common |
For convenience, the three most common quantifiers have single-character |
| 1038 |
quantifiers have single-character abbreviations: |
abbreviations: |
| 1039 |
.sp |
.sp |
| 1040 |
* is equivalent to {0,} |
* is equivalent to {0,} |
| 1041 |
+ is equivalent to {1,} |
+ is equivalent to {1,} |
| 1083 |
which matches one digit by preference, but can match two if that is the only |
which matches one digit by preference, but can match two if that is the only |
| 1084 |
way the rest of the pattern matches. |
way the rest of the pattern matches. |
| 1085 |
.P |
.P |
| 1086 |
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), |
| 1087 |
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 |
| 1088 |
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 |
| 1089 |
default behaviour. |
default behaviour. |
| 1093 |
compiled pattern, in proportion to the size of the minimum or maximum. |
compiled pattern, in proportion to the size of the minimum or maximum. |
| 1094 |
.P |
.P |
| 1095 |
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 |
| 1096 |
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 |
| 1097 |
implicitly anchored, because whatever follows will be tried against every |
implicitly anchored, because whatever follows will be tried against every |
| 1098 |
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 |
| 1099 |
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 |
| 1105 |
.P |
.P |
| 1106 |
However, there is one situation where the optimization cannot be used. When .* |
However, there is one situation where the optimization cannot be used. When .* |
| 1107 |
is inside capturing parentheses that are the subject of a backreference |
is inside capturing parentheses that are the subject of a backreference |
| 1108 |
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 |
| 1109 |
succeed. Consider, for example: |
succeeds. Consider, for example: |
| 1110 |
.sp |
.sp |
| 1111 |
(.*)abc\e1 |
(.*)abc\e1 |
| 1112 |
.sp |
.sp |
| 1132 |
.SH "ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS" |
.SH "ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS" |
| 1133 |
.rs |
.rs |
| 1134 |
.sp |
.sp |
| 1135 |
With both maximizing and minimizing repetition, failure of what follows |
With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy") |
| 1136 |
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 |
| 1137 |
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 |
| 1138 |
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 |
| 1139 |
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 |
| 1140 |
there is no point in carrying on. |
the author of the pattern knows there is no point in carrying on. |
| 1141 |
.P |
.P |
| 1142 |
Consider, for example, the pattern \ed+foo when applied to the subject line |
Consider, for example, the pattern \ed+foo when applied to the subject line |
| 1143 |
.sp |
.sp |
| 1149 |
(a term taken from Jeffrey Friedl's book) provides the means for specifying |
(a term taken from Jeffrey Friedl's book) provides the means for specifying |
| 1150 |
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. |
| 1151 |
.P |
.P |
| 1152 |
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 |
| 1153 |
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 |
| 1154 |
special parenthesis, starting with (?> as in this example: |
special parenthesis, starting with (?> as in this example: |
| 1155 |
.sp |
.sp |
| 1181 |
.sp |
.sp |
| 1182 |
Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY |
Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY |
| 1183 |
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 |
| 1184 |
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 |
| 1185 |
possessive quantifier and the equivalent atomic group. |
quantifier and the equivalent atomic group, though there may be a performance |
| 1186 |
.P |
difference; possessive quantifiers should be slightly faster. |
| 1187 |
The possessive quantifier syntax is an extension to the Perl syntax. It |
.P |
| 1188 |
originates in Sun's Java package. |
The possessive quantifier syntax is an extension to the Perl 5.8 syntax. |
| 1189 |
|
Jeffrey Friedl originated the idea (and the name) in the first edition of his |
| 1190 |
|
book. Mike McCloskey liked it, so implemented it when he built Sun's Java |
| 1191 |
|
package, and PCRE copied it from there. It ultimately found its way into Perl |
| 1192 |
|
at release 5.10. |
| 1193 |
|
.P |
| 1194 |
|
PCRE has an optimization that automatically "possessifies" certain simple |
| 1195 |
|
pattern constructs. For example, the sequence A+B is treated as A++B because |
| 1196 |
|
there is no point in backtracking into a sequence of A's when B must follow. |
| 1197 |
.P |
.P |
| 1198 |
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 |
| 1199 |
be repeated an unlimited number of times, the use of an atomic group is the |
be repeated an unlimited number of times, the use of an atomic group is the |
| 1235 |
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 |
| 1236 |
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 |
| 1237 |
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 |
| 1238 |
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 |
| 1239 |
|
when a repetition is involved and the subpattern to the right has participated |
| 1240 |
|
in an earlier iteration. |
| 1241 |
|
.P |
| 1242 |
|
It is not possible to have a numerical "forward back reference" to a subpattern |
| 1243 |
|
whose number is 10 or more using this syntax because a sequence such as \e50 is |
| 1244 |
|
interpreted as a character defined in octal. See the subsection entitled |
| 1245 |
|
"Non-printing characters" |
| 1246 |
.\" HTML <a href="#digitsafterbackslash"> |
.\" HTML <a href="#digitsafterbackslash"> |
| 1247 |
.\" </a> |
.\" </a> |
| 1248 |
above |
above |
| 1249 |
.\" |
.\" |
| 1250 |
for further details of the handling of digits following a backslash. |
for further details of the handling of digits following a backslash. There is |
| 1251 |
|
no such problem when named parentheses are used. A back reference to any |
| 1252 |
|
subpattern is possible using named parentheses (see below). |
| 1253 |
|
.P |
| 1254 |
|
Another way of avoiding the ambiguity inherent in the use of digits following a |
| 1255 |
|
backslash is to use the \eg escape sequence, which is a feature introduced in |
| 1256 |
|
Perl 5.10. This escape must be followed by a positive or a negative number, |
| 1257 |
|
optionally enclosed in braces. These examples are all identical: |
| 1258 |
|
.sp |
| 1259 |
|
(ring), \e1 |
| 1260 |
|
(ring), \eg1 |
| 1261 |
|
(ring), \eg{1} |
| 1262 |
|
.sp |
| 1263 |
|
A positive number specifies an absolute reference without the ambiguity that is |
| 1264 |
|
present in the older syntax. It is also useful when literal digits follow the |
| 1265 |
|
reference. A negative number is a relative reference. Consider this example: |
| 1266 |
|
.sp |
| 1267 |
|
(abc(def)ghi)\eg{-1} |
| 1268 |
|
.sp |
| 1269 |
|
The sequence \eg{-1} is a reference to the most recently started capturing |
| 1270 |
|
subpattern before \eg, that is, is it equivalent to \e2. Similarly, \eg{-2} |
| 1271 |
|
would be equivalent to \e1. The use of relative references can be helpful in |
| 1272 |
|
long patterns, and also in patterns that are created by joining together |
| 1273 |
|
fragments that contain references within themselves. |
| 1274 |
.P |
.P |
| 1275 |
A back reference matches whatever actually matched the capturing subpattern in |
A back reference matches whatever actually matched the capturing subpattern in |
| 1276 |
the current subject string, rather than anything matching the subpattern |
the current subject string, rather than anything matching the subpattern |
| 1292 |
matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original |
matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original |
| 1293 |
capturing subpattern is matched caselessly. |
capturing subpattern is matched caselessly. |
| 1294 |
.P |
.P |
| 1295 |
Back references to named subpatterns use the Python syntax (?P=name). We could |
Back references to named subpatterns use the Perl syntax \ek<name> or \ek'name' |
| 1296 |
rewrite the above example as follows: |
or the Python syntax (?P=name). We could rewrite the above example in either of |
| 1297 |
|
the following ways: |
| 1298 |
.sp |
.sp |
| 1299 |
(?<p1>(?i)rah)\es+(?P=p1) |
(?<p1>(?i)rah)\es+\ek<p1> |
| 1300 |
|
(?P<p1>(?i)rah)\es+(?P=p1) |
| 1301 |
.sp |
.sp |
| 1302 |
|
A subpattern that is referenced by name may appear in the pattern before or |
| 1303 |
|
after the reference. |
| 1304 |
|
.P |
| 1305 |
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 |
| 1306 |
subpattern has not actually been used in a particular match, any back |
subpattern has not actually been used in a particular match, any back |
| 1307 |
references to it always fail. For example, the pattern |
references to it always fail. For example, the pattern |
| 1363 |
.SS "Lookahead assertions" |
.SS "Lookahead assertions" |
| 1364 |
.rs |
.rs |
| 1365 |
.sp |
.sp |
| 1366 |
Lookahead assertions start |
Lookahead assertions start with (?= for positive assertions and (?! for |
| 1367 |
with (?= for positive assertions and (?! for negative assertions. For example, |
negative assertions. For example, |
| 1368 |
.sp |
.sp |
| 1369 |
\ew+(?=;) |
\ew+(?=;) |
| 1370 |
.sp |
.sp |
| 1399 |
.sp |
.sp |
| 1400 |
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 |
| 1401 |
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 |
| 1402 |
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 |
| 1403 |
all have to have the same fixed length. Thus |
do not all have to have the same fixed length. Thus |
| 1404 |
.sp |
.sp |
| 1405 |
(?<=bullock|donkey) |
(?<=bullock|donkey) |
| 1406 |
.sp |
.sp |
| 1421 |
(?<=abc|abde) |
(?<=abc|abde) |
| 1422 |
.sp |
.sp |
| 1423 |
The implementation of lookbehind assertions is, for each alternative, to |
The implementation of lookbehind assertions is, for each alternative, to |
| 1424 |
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 |
| 1425 |
match. If there are insufficient characters before the current position, the |
match. If there are insufficient characters before the current position, the |
| 1426 |
match is deemed to fail. |
assertion fails. |
| 1427 |
.P |
.P |
| 1428 |
PCRE does not allow the \eC escape (which matches a single byte in UTF-8 mode) |
PCRE does not allow the \eC escape (which matches a single byte in UTF-8 mode) |
| 1429 |
to appear in lookbehind assertions, because it makes it impossible to calculate |
to appear in lookbehind assertions, because it makes it impossible to calculate |
| 1430 |
the length of the lookbehind. The \eX escape, which can match different numbers |
the length of the lookbehind. The \eX and \eR escapes, which can match |
| 1431 |
of bytes, is also not permitted. |
different numbers of bytes, are also not permitted. |
| 1432 |
.P |
.P |
| 1433 |
Atomic groups can be used in conjunction with lookbehind assertions to specify |
Possessive quantifiers can be used in conjunction with lookbehind assertions to |
| 1434 |
efficient matching at the end of the subject string. Consider a simple pattern |
specify efficient matching at the end of the subject string. Consider a simple |
| 1435 |
such as |
pattern such as |
| 1436 |
.sp |
.sp |
| 1437 |
abcd$ |
abcd$ |
| 1438 |
.sp |
.sp |
| 1448 |
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, |
| 1449 |
if the pattern is written as |
if the pattern is written as |
| 1450 |
.sp |
.sp |
|
^(?>.*)(?<=abcd) |
|
|
.sp |
|
|
or, equivalently, using the possessive quantifier syntax, |
|
|
.sp |
|
| 1451 |
^.*+(?<=abcd) |
^.*+(?<=abcd) |
| 1452 |
.sp |
.sp |
| 1453 |
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 |
| 1454 |
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 |
| 1455 |
characters. If it fails, the match fails immediately. For long strings, this |
characters. If it fails, the match fails immediately. For long strings, this |
| 1456 |
approach makes a significant difference to the processing time. |
approach makes a significant difference to the processing time. |
| 1490 |
characters that are not "999". |
characters that are not "999". |
| 1491 |
. |
. |
| 1492 |
. |
. |
| 1493 |
|
.\" HTML <a name="conditions"></a> |
| 1494 |
.SH "CONDITIONAL SUBPATTERNS" |
.SH "CONDITIONAL SUBPATTERNS" |
| 1495 |
.rs |
.rs |
| 1496 |
.sp |
.sp |
| 1506 |
no-pattern (if present) is used. If there are more than two alternatives in the |
no-pattern (if present) is used. If there are more than two alternatives in the |
| 1507 |
subpattern, a compile-time error occurs. |
subpattern, a compile-time error occurs. |
| 1508 |
.P |
.P |
| 1509 |
There are three kinds of condition. If the text between the parentheses |
There are four kinds of condition: references to subpatterns, references to |
| 1510 |
consists of a sequence of digits, the condition is satisfied if the capturing |
recursion, a pseudo-condition called DEFINE, and assertions. |
| 1511 |
subpattern of that number has previously matched. The number must be greater |
. |
| 1512 |
than zero. Consider the following pattern, which contains non-significant white |
.SS "Checking for a used subpattern by number" |
| 1513 |
space to make it more readable (assume the PCRE_EXTENDED option) and to divide |
.rs |
| 1514 |
it into three parts for ease of discussion: |
.sp |
| 1515 |
|
If the text between the parentheses consists of a sequence of digits, the |
| 1516 |
|
condition is true if the capturing subpattern of that number has previously |
| 1517 |
|
matched. |
| 1518 |
|
.P |
| 1519 |
|
Consider the following pattern, which contains non-significant white space to |
| 1520 |
|
make it more readable (assume the PCRE_EXTENDED option) and to divide it into |
| 1521 |
|
three parts for ease of discussion: |
| 1522 |
.sp |
.sp |
| 1523 |
( \e( )? [^()]+ (?(1) \e) ) |
( \e( )? [^()]+ (?(1) \e) ) |
| 1524 |
.sp |
.sp |
| 1531 |
parenthesis is required. Otherwise, since no-pattern is not present, the |
parenthesis is required. Otherwise, since no-pattern is not present, the |
| 1532 |
subpattern matches nothing. In other words, this pattern matches a sequence of |
subpattern matches nothing. In other words, this pattern matches a sequence of |
| 1533 |
non-parentheses, optionally enclosed in parentheses. |
non-parentheses, optionally enclosed in parentheses. |
| 1534 |
|
. |
| 1535 |
|
.SS "Checking for a used subpattern by name" |
| 1536 |
|
.rs |
| 1537 |
|
.sp |
| 1538 |
|
Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a used |
| 1539 |
|
subpattern by name. For compatibility with earlier versions of PCRE, which had |
| 1540 |
|
this facility before Perl, the syntax (?(name)...) is also recognized. However, |
| 1541 |
|
there is a possible ambiguity with this syntax, because subpattern names may |
| 1542 |
|
consist entirely of digits. PCRE looks first for a named subpattern; if it |
| 1543 |
|
cannot find one and the name consists entirely of digits, PCRE looks for a |
| 1544 |
|
subpattern of that number, which must be greater than zero. Using subpattern |
| 1545 |
|
names that consist entirely of digits is not recommended. |
| 1546 |
|
.P |
| 1547 |
|
Rewriting the above example to use a named subpattern gives this: |
| 1548 |
|
.sp |
| 1549 |
|
(?<OPEN> \e( )? [^()]+ (?(<OPEN>) \e) ) |
| 1550 |
|
.sp |
| 1551 |
|
. |
| 1552 |
|
.SS "Checking for pattern recursion" |
| 1553 |
|
.rs |
| 1554 |
|
.sp |
| 1555 |
|
If the condition is the string (R), and there is no subpattern with the name R, |
| 1556 |
|
the condition is true if a recursive call to the whole pattern or any |
| 1557 |
|
subpattern has been made. If digits or a name preceded by ampersand follow the |
| 1558 |
|
letter R, for example: |
| 1559 |
|
.sp |
| 1560 |
|
(?(R3)...) or (?(R&name)...) |
| 1561 |
|
.sp |
| 1562 |
|
the condition is true if the most recent recursion is into the subpattern whose |
| 1563 |
|
number or name is given. This condition does not check the entire recursion |
| 1564 |
|
stack. |
| 1565 |
.P |
.P |
| 1566 |
If the condition is the string (R), it is satisfied if a recursive call to the |
At "top level", all these recursion test conditions are false. Recursive |
| 1567 |
pattern or subpattern has been made. At "top level", the condition is false. |
patterns are described below. |
| 1568 |
This is a PCRE extension. Recursive patterns are described in the next section. |
. |
| 1569 |
|
.SS "Defining subpatterns for use by reference only" |
| 1570 |
|
.rs |
| 1571 |
|
.sp |
| 1572 |
|
If the condition is the string (DEFINE), and there is no subpattern with the |
| 1573 |
|
name DEFINE, the condition is always false. In this case, there may be only one |
| 1574 |
|
alternative in the subpattern. It is always skipped if control reaches this |
| 1575 |
|
point in the pattern; the idea of DEFINE is that it can be used to define |
| 1576 |
|
"subroutines" that can be referenced from elsewhere. (The use of "subroutines" |
| 1577 |
|
is described below.) For example, a pattern to match an IPv4 address could be |
| 1578 |
|
written like this (ignore whitespace and line breaks): |
| 1579 |
|
.sp |
| 1580 |
|
(?(DEFINE) (?<byte> 2[0-4]\ed | 25[0-5] | 1\ed\ed | [1-9]?\ed) ) |
| 1581 |
|
\eb (?&byte) (\e.(?&byte)){3} \eb |
| 1582 |
|
.sp |
| 1583 |
|
The first part of the pattern is a DEFINE group inside which a another group |
| 1584 |
|
named "byte" is defined. This matches an individual component of an IPv4 |
| 1585 |
|
address (a number less than 256). When matching takes place, this part of the |
| 1586 |
|
pattern is skipped because DEFINE acts like a false condition. |
| 1587 |
.P |
.P |
| 1588 |
If the condition is not a sequence of digits or (R), it must be an assertion. |
The rest of the pattern uses references to the named group to match the four |
| 1589 |
|
dot-separated components of an IPv4 address, insisting on a word boundary at |
| 1590 |
|
each end. |
| 1591 |
|
. |
| 1592 |
|
.SS "Assertion conditions" |
| 1593 |
|
.rs |
| 1594 |
|
.sp |
| 1595 |
|
If the condition is not in any of the above formats, it must be an assertion. |
| 1596 |
This may be a positive or negative lookahead or lookbehind assertion. Consider |
This may be a positive or negative lookahead or lookbehind assertion. Consider |
| 1597 |
this pattern, again containing non-significant white space, and with the two |
this pattern, again containing non-significant white space, and with the two |
| 1598 |
alternatives on the second line: |
alternatives on the second line: |
| 1617 |
that make up a comment play no part in the pattern matching at all. |
that make up a comment play no part in the pattern matching at all. |
| 1618 |
.P |
.P |
| 1619 |
If the PCRE_EXTENDED option is set, an unescaped # character outside a |
If the PCRE_EXTENDED option is set, an unescaped # character outside a |
| 1620 |
character class introduces a comment that continues up to the next newline |
character class introduces a comment that continues to immediately after the |
| 1621 |
character in the pattern. |
next newline in the pattern. |
| 1622 |
. |
. |
| 1623 |
. |
. |
| 1624 |
|
.\" HTML <a name="recursion"></a> |
| 1625 |
.SH "RECURSIVE PATTERNS" |
.SH "RECURSIVE PATTERNS" |
| 1626 |
.rs |
.rs |
| 1627 |
.sp |
.sp |
| 1628 |
Consider the problem of matching a string in parentheses, allowing for |
Consider the problem of matching a string in parentheses, allowing for |
| 1629 |
unlimited nested parentheses. Without the use of recursion, the best that can |
unlimited nested parentheses. Without the use of recursion, the best that can |
| 1630 |
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 |
| 1631 |
is not possible to handle an arbitrary nesting depth. Perl provides a facility |
is not possible to handle an arbitrary nesting depth. |
| 1632 |
that allows regular expressions to recurse (amongst other things). It does this |
.P |
| 1633 |
by interpolating Perl code in the expression at run time, and the code can |
For some time, Perl has provided a facility that allows regular expressions to |
| 1634 |
refer to the expression itself. A Perl pattern to solve the parentheses problem |
recurse (amongst other things). It does this by interpolating Perl code in the |
| 1635 |
can be created like this: |
expression at run time, and the code can refer to the expression itself. A Perl |
| 1636 |
|
pattern using code interpolation to solve the parentheses problem can be |
| 1637 |
|
created like this: |
| 1638 |
.sp |
.sp |
| 1639 |
$re = qr{\e( (?: (?>[^()]+) | (?p{$re}) )* \e)}x; |
$re = qr{\e( (?: (?>[^()]+) | (?p{$re}) )* \e)}x; |
| 1640 |
.sp |
.sp |
| 1641 |
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 |
| 1642 |
recursively to the pattern in which it appears. Obviously, PCRE cannot support |
recursively to the pattern in which it appears. |
|
the interpolation of Perl code. Instead, it supports some special syntax for |
|
|
recursion of the entire pattern, and also for individual subpattern recursion. |
|
|
.P |
|
|
The special item that consists of (? followed by a number greater than zero and |
|
|
a closing parenthesis is a recursive call of the subpattern of the given |
|
|
number, provided that it occurs inside that subpattern. (If not, it is a |
|
|
"subroutine" call, which is described in the next section.) The special item |
|
|
(?R) is a recursive call of the entire regular expression. |
|
| 1643 |
.P |
.P |
| 1644 |
For example, this PCRE pattern solves the nested parentheses problem (assume |
Obviously, PCRE cannot support the interpolation of Perl code. Instead, it |
| 1645 |
the PCRE_EXTENDED option is set so that white space is ignored): |
supports special syntax for recursion of the entire pattern, and also for |
| 1646 |
|
individual subpattern recursion. After its introduction in PCRE and Python, |
| 1647 |
|
this kind of recursion was introduced into Perl at release 5.10. |
| 1648 |
|
.P |
| 1649 |
|
A special item that consists of (? followed by a number greater than zero and a |
| 1650 |
|
closing parenthesis is a recursive call of the subpattern of the given number, |
| 1651 |
|
provided that it occurs inside that subpattern. (If not, it is a "subroutine" |
| 1652 |
|
call, which is described in the next section.) The special item (?R) or (?0) is |
| 1653 |
|
a recursive call of the entire regular expression. |
| 1654 |
|
.P |
| 1655 |
|
In PCRE (like Python, but unlike Perl), a recursive subpattern call is always |
| 1656 |
|
treated as an atomic group. That is, once it has matched some of the subject |
| 1657 |
|
string, it is never re-entered, even if it contains untried alternatives and |
| 1658 |
|
there is a subsequent matching failure. |
| 1659 |
|
.P |
| 1660 |
|
This PCRE pattern solves the nested parentheses problem (assume the |
| 1661 |
|
PCRE_EXTENDED option is set so that white space is ignored): |
| 1662 |
.sp |
.sp |
| 1663 |
\e( ( (?>[^()]+) | (?R) )* \e) |
\e( ( (?>[^()]+) | (?R) )* \e) |
| 1664 |
.sp |
.sp |
| 1665 |
First it matches an opening parenthesis. Then it matches any number of |
First it matches an opening parenthesis. Then it matches any number of |
| 1666 |
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 |
| 1667 |
match of the pattern itself (that is a correctly parenthesized substring). |
match of the pattern itself (that is, a correctly parenthesized substring). |
| 1668 |
Finally there is a closing parenthesis. |
Finally there is a closing parenthesis. |
| 1669 |
.P |
.P |
| 1670 |
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 |
| 1675 |
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 |
| 1676 |
them instead of the whole pattern. In a larger pattern, keeping track of |
them instead of the whole pattern. In a larger pattern, keeping track of |
| 1677 |
parenthesis numbers can be tricky. It may be more convenient to use named |
parenthesis numbers can be tricky. It may be more convenient to use named |
| 1678 |
parentheses instead. For this, PCRE uses (?P>name), which is an extension to |
parentheses instead. The Perl syntax for this is (?&name); PCRE's earlier |
| 1679 |
the Python syntax that PCRE uses for named parentheses (Perl does not provide |
syntax (?P>name) is also supported. We could rewrite the above example as |
| 1680 |
named parentheses). We could rewrite the above example as follows: |
follows: |
| 1681 |
.sp |
.sp |
| 1682 |
(?P<pn> \e( ( (?>[^()]+) | (?P>pn) )* \e) ) |
(?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) ) |
| 1683 |
.sp |
.sp |
| 1684 |
This particular example pattern contains nested unlimited repeats, and so the |
If there is more than one subpattern with the same name, the earliest one is |
| 1685 |
use of atomic grouping for matching strings of non-parentheses is important |
used. This particular example pattern contains nested unlimited repeats, and so |
| 1686 |
|
the use of atomic grouping for matching strings of non-parentheses is important |
| 1687 |
when applying the pattern to strings that do not match. For example, when this |
when applying the pattern to strings that do not match. For example, when this |
| 1688 |
pattern is applied to |
pattern is applied to |
| 1689 |
.sp |
.sp |
| 1697 |
At the end of a match, the values set for any capturing subpatterns are those |
At the end of a match, the values set for any capturing subpatterns are those |
| 1698 |
from the outermost level of the recursion at which the subpattern value is set. |
from the outermost level of the recursion at which the subpattern value is set. |
| 1699 |
If you want to obtain intermediate values, a callout function can be used (see |
If you want to obtain intermediate values, a callout function can be used (see |
| 1700 |
the next section and the |
below and the |
| 1701 |
.\" HREF |
.\" HREF |
| 1702 |
\fBpcrecallout\fP |
\fBpcrecallout\fP |
| 1703 |
.\" |
.\" |
| 1736 |
.sp |
.sp |
| 1737 |
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 |
| 1738 |
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 |
| 1739 |
subroutine in a programming language. An earlier example pointed out that the |
subroutine in a programming language. The "called" subpattern may be defined |
| 1740 |
pattern |
before or after the reference. An earlier example pointed out that the pattern |
| 1741 |
.sp |
.sp |
| 1742 |
(sens|respons)e and \e1ibility |
(sens|respons)e and \e1ibility |
| 1743 |
.sp |
.sp |
| 1747 |
(sens|respons)e and (?1)ibility |
(sens|respons)e and (?1)ibility |
| 1748 |
.sp |
.sp |
| 1749 |
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 |
| 1750 |
strings. Such references must, however, follow the subpattern to which they |
strings. Another example is given in the discussion of DEFINE above. |
| 1751 |
refer. |
.P |
| 1752 |
|
Like recursive subpatterns, a "subroutine" call is always treated as an atomic |
| 1753 |
|
group. That is, once it has matched some of the subject string, it is never |
| 1754 |
|
re-entered, even if it contains untried alternatives and there is a subsequent |
| 1755 |
|
matching failure. |
| 1756 |
|
.P |
| 1757 |
|
When a subpattern is used as a subroutine, processing options such as |
| 1758 |
|
case-independence are fixed when the subpattern is defined. They cannot be |
| 1759 |
|
changed for different calls. For example, consider this pattern: |
| 1760 |
|
.sp |
| 1761 |
|
(abc)(?i:(?1)) |
| 1762 |
|
.sp |
| 1763 |
|
It matches "abcabc". It does not match "abcABC" because the change of |
| 1764 |
|
processing option does not affect the called subpattern. |
| 1765 |
. |
. |
| 1766 |
. |
. |
| 1767 |
.SH CALLOUTS |
.SH CALLOUTS |
| 1798 |
\fBpcrecallout\fP |
\fBpcrecallout\fP |
| 1799 |
.\" |
.\" |
| 1800 |
documentation. |
documentation. |
| 1801 |
|
. |
| 1802 |
|
. |
| 1803 |
|
.SH "SEE ALSO" |
| 1804 |
|
.rs |
| 1805 |
|
.sp |
| 1806 |
|
\fBpcreapi\fP(3), \fBpcrecallout\fP(3), \fBpcrematching\fP(3), \fBpcre\fP(3). |
| 1807 |
.P |
.P |
| 1808 |
.in 0 |
.in 0 |
| 1809 |
Last updated: 28 February 2005 |
Last updated: 06 December 2006 |
| 1810 |
.br |
.br |
| 1811 |
Copyright (c) 1997-2005 University of Cambridge. |
Copyright (c) 1997-2006 University of Cambridge. |