| 3 |
<title>pcrepattern specification</title> |
<title>pcrepattern specification</title> |
| 4 |
</head> |
</head> |
| 5 |
<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB"> |
<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB"> |
| 6 |
This HTML document has been generated automatically from the original man page. |
<h1>pcrepattern man page</h1> |
| 7 |
If there is any nonsense in it, please consult the man page, in case the |
<p> |
| 8 |
conversion went wrong.<br> |
Return to the <a href="index.html">PCRE index page</a>. |
| 9 |
|
</p> |
| 10 |
|
<p> |
| 11 |
|
This page is part of the PCRE HTML documentation. It was generated automatically |
| 12 |
|
from the original man page. If there is any nonsense in it, please consult the |
| 13 |
|
man page, in case the conversion went wrong. |
| 14 |
|
<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">BACKSLASH</a> |
| 18 |
<li><a name="TOC3" href="#SEC3">CIRCUMFLEX AND DOLLAR</a> |
<li><a name="TOC3" href="#SEC3">CIRCUMFLEX AND DOLLAR</a> |
| 19 |
<li><a name="TOC4" href="#SEC4">FULL STOP (PERIOD, DOT)</a> |
<li><a name="TOC4" href="#SEC4">FULL STOP (PERIOD, DOT)</a> |
| 20 |
<li><a name="TOC5" href="#SEC5">MATCHING A SINGLE BYTE</a> |
<li><a name="TOC5" href="#SEC5">MATCHING A SINGLE BYTE</a> |
| 21 |
<li><a name="TOC6" href="#SEC6">SQUARE BRACKETS</a> |
<li><a name="TOC6" href="#SEC6">SQUARE BRACKETS AND CHARACTER CLASSES</a> |
| 22 |
<li><a name="TOC7" href="#SEC7">POSIX CHARACTER CLASSES</a> |
<li><a name="TOC7" href="#SEC7">POSIX CHARACTER CLASSES</a> |
| 23 |
<li><a name="TOC8" href="#SEC8">VERTICAL BAR</a> |
<li><a name="TOC8" href="#SEC8">VERTICAL BAR</a> |
| 24 |
<li><a name="TOC9" href="#SEC9">INTERNAL OPTION SETTING</a> |
<li><a name="TOC9" href="#SEC9">INTERNAL OPTION SETTING</a> |
| 38 |
<P> |
<P> |
| 39 |
The syntax and semantics of the regular expressions supported by PCRE are |
The syntax and semantics of the regular expressions supported by PCRE are |
| 40 |
described below. Regular expressions are also described in the Perl |
described below. Regular expressions are also described in the Perl |
| 41 |
documentation and in a number of other books, some of which have copious |
documentation and in a number of books, some of which have copious examples. |
| 42 |
examples. Jeffrey Friedl's "Mastering Regular Expressions", published by |
Jeffrey Friedl's "Mastering Regular Expressions", published by O'Reilly, covers |
| 43 |
O'Reilly, covers them in great detail. The description here is intended as |
regular expressions in great detail. This description of PCRE's regular |
| 44 |
reference documentation. |
expressions is intended as reference material. |
| 45 |
</P> |
</P> |
| 46 |
<P> |
<P> |
| 47 |
The basic operation of PCRE is on strings of bytes. However, there is also |
The original operation of PCRE was on strings of one-byte characters. However, |
| 48 |
support for UTF-8 character strings. To use this support you must build PCRE to |
there is now also support for UTF-8 character strings. To use this, you must |
| 49 |
include UTF-8 support, and then call <b>pcre_compile()</b> with the PCRE_UTF8 |
build PCRE to include UTF-8 support, and then call <b>pcre_compile()</b> with |
| 50 |
option. How this affects the pattern matching is mentioned in several places |
the PCRE_UTF8 option. How this affects pattern matching is mentioned in several |
| 51 |
below. There is also a summary of UTF-8 features in the |
places below. There is also a summary of UTF-8 features in the |
| 52 |
<a href="pcre.html#utf8support">section on UTF-8 support</a> |
<a href="pcre.html#utf8support">section on UTF-8 support</a> |
| 53 |
in the main |
in the main |
| 54 |
<a href="pcre.html"><b>pcre</b></a> |
<a href="pcre.html"><b>pcre</b></a> |
| 58 |
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 |
| 59 |
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 |
| 60 |
corresponding characters in the subject. As a trivial example, the pattern |
corresponding characters in the subject. As a trivial example, the pattern |
|
</P> |
|
|
<P> |
|
| 61 |
<pre> |
<pre> |
| 62 |
The quick brown fox |
The quick brown fox |
| 63 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 64 |
matches a portion of a subject string that is identical to itself. The power of |
matches a portion of a subject string that is identical to itself. The power of |
| 65 |
regular expressions comes from the ability to include alternatives and |
regular expressions comes from the ability to include alternatives and |
| 66 |
repetitions in the pattern. These are encoded in the pattern by the use of |
repetitions in the pattern. These are encoded in the pattern by the use of |
| 67 |
<i>meta-characters</i>, which do not stand for themselves but instead are |
<i>metacharacters</i>, which do not stand for themselves but instead are |
| 68 |
interpreted in some special way. |
interpreted in some special way. |
| 69 |
</P> |
</P> |
| 70 |
<P> |
<P> |
| 71 |
There are two different sets of meta-characters: those that are recognized |
There are two different sets of metacharacters: those that are recognized |
| 72 |
anywhere in the pattern except within square brackets, and those that are |
anywhere in the pattern except within square brackets, and those that are |
| 73 |
recognized in square brackets. Outside square brackets, the meta-characters are |
recognized in square brackets. Outside square brackets, the metacharacters are |
| 74 |
as follows: |
as follows: |
|
</P> |
|
|
<P> |
|
| 75 |
<pre> |
<pre> |
| 76 |
\ general escape character with several uses |
\ general escape character with several uses |
| 77 |
^ assert start of string (or line, in multiline mode) |
^ assert start of string (or line, in multiline mode) |
| 88 |
+ 1 or more quantifier |
+ 1 or more quantifier |
| 89 |
also "possessive quantifier" |
also "possessive quantifier" |
| 90 |
{ start min/max quantifier |
{ start min/max quantifier |
| 91 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 92 |
Part of a pattern that is in square brackets is called a "character class". In |
Part of a pattern that is in square brackets is called a "character class". In |
| 93 |
a character class the only meta-characters are: |
a character class the only metacharacters are: |
|
</P> |
|
|
<P> |
|
| 94 |
<pre> |
<pre> |
| 95 |
\ general escape character |
\ general escape character |
| 96 |
^ negate the class, but only if the first character |
^ negate the class, but only if the first character |
| 97 |
- indicates character range |
- indicates character range |
| 98 |
[ POSIX character class (only if followed by POSIX |
[ POSIX character class (only if followed by POSIX syntax) |
|
syntax) |
|
| 99 |
] terminates the character class |
] terminates the character class |
| 100 |
</PRE> |
</pre> |
| 101 |
</P> |
The following sections describe the use of each of the metacharacters. |
|
<P> |
|
|
The following sections describe the use of each of the meta-characters. |
|
| 102 |
</P> |
</P> |
| 103 |
<br><a name="SEC2" href="#TOC1">BACKSLASH</a><br> |
<br><a name="SEC2" href="#TOC1">BACKSLASH</a><br> |
| 104 |
<P> |
<P> |
| 105 |
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 |
| 106 |
non-alphameric character, it takes away any special meaning that character may |
non-alphanumeric character, it takes away any special meaning that character may |
| 107 |
have. This use of backslash as an escape character applies both inside and |
have. This use of backslash as an escape character applies both inside and |
| 108 |
outside character classes. |
outside character classes. |
| 109 |
</P> |
</P> |
| 110 |
<P> |
<P> |
| 111 |
For example, if you want to match a * character, you write \* in the pattern. |
For example, if you want to match a * character, you write \* in the pattern. |
| 112 |
This escaping action applies whether or not the following character would |
This escaping action applies whether or not the following character would |
| 113 |
otherwise be interpreted as a meta-character, so it is always safe to precede a |
otherwise be interpreted as a metacharacter, so it is always safe to precede a |
| 114 |
non-alphameric with backslash to specify that it stands for itself. In |
non-alphanumeric with backslash to specify that it stands for itself. In |
| 115 |
particular, if you want to match a backslash, you write \\. |
particular, if you want to match a backslash, you write \\. |
| 116 |
</P> |
</P> |
| 117 |
<P> |
<P> |
| 126 |
can do so by putting them between \Q and \E. This is different from Perl in |
can do so by putting them between \Q and \E. This is different from Perl in |
| 127 |
that $ and @ are handled as literals in \Q...\E sequences in PCRE, whereas in |
that $ and @ are handled as literals in \Q...\E sequences in PCRE, whereas in |
| 128 |
Perl, $ and @ cause variable interpolation. Note the following examples: |
Perl, $ and @ cause variable interpolation. Note the following examples: |
|
</P> |
|
|
<P> |
|
| 129 |
<pre> |
<pre> |
| 130 |
Pattern PCRE matches Perl matches |
Pattern PCRE matches Perl matches |
| 131 |
</PRE> |
|
| 132 |
</P> |
\Qabc$xyz\E abc$xyz abc followed by the contents of $xyz |
|
<P> |
|
|
<pre> |
|
|
\Qabc$xyz\E abc$xyz abc followed by the |
|
|
contents of $xyz |
|
| 133 |
\Qabc\$xyz\E abc\$xyz abc\$xyz |
\Qabc\$xyz\E abc\$xyz abc\$xyz |
| 134 |
\Qabc\E\$\Qxyz\E abc$xyz abc$xyz |
\Qabc\E\$\Qxyz\E abc$xyz abc$xyz |
| 135 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 136 |
The \Q...\E sequence is recognized both inside and outside character classes. |
The \Q...\E sequence is recognized both inside and outside character classes. |
| 137 |
</P> |
<a name="digitsafterbackslash"></a></P> |
| 138 |
|
<br><b> |
| 139 |
|
Non-printing characters |
| 140 |
|
</b><br> |
| 141 |
<P> |
<P> |
| 142 |
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 |
| 143 |
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 |
| 145 |
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 usually easier to |
| 146 |
use one of the following escape sequences than the binary character it |
use one of the following escape sequences than the binary character it |
| 147 |
represents: |
represents: |
|
</P> |
|
|
<P> |
|
| 148 |
<pre> |
<pre> |
| 149 |
\a alarm, that is, the BEL character (hex 07) |
\a alarm, that is, the BEL character (hex 07) |
| 150 |
\cx "control-x", where x is any character |
\cx "control-x", where x is any character |
| 156 |
\ddd character with octal code ddd, or backreference |
\ddd character with octal code ddd, or backreference |
| 157 |
\xhh character with hex code hh |
\xhh character with hex code hh |
| 158 |
\x{hhh..} character with hex code hhh... (UTF-8 mode only) |
\x{hhh..} character with hex code hhh... (UTF-8 mode only) |
| 159 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 160 |
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 |
| 161 |
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. |
| 162 |
Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; becomes hex |
Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; becomes hex |
| 170 |
other than hexadecimal digits appear between \x{ and }, or if there is no |
other than hexadecimal digits appear between \x{ and }, or if there is no |
| 171 |
terminating }, this form of escape is not recognized. Instead, the initial |
terminating }, this form of escape is not recognized. Instead, the initial |
| 172 |
\x will be interpreted as a basic hexadecimal escape, with no following |
\x will be interpreted as a basic hexadecimal escape, with no following |
| 173 |
digits, giving a byte whose value is zero. |
digits, giving a character whose value is zero. |
| 174 |
</P> |
</P> |
| 175 |
<P> |
<P> |
| 176 |
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 |
| 182 |
are fewer than two digits, just those that are present are used. Thus the |
are fewer than two digits, just those that are present are used. Thus the |
| 183 |
sequence \0\x\07 specifies two binary zeros followed by a BEL character |
sequence \0\x\07 specifies two binary zeros followed by a BEL character |
| 184 |
(code value 7). Make sure you supply two digits after the initial zero if the |
(code value 7). Make sure you supply two digits after the initial zero if the |
| 185 |
character that follows is itself an octal digit. |
pattern character that follows is itself an octal digit. |
| 186 |
</P> |
</P> |
| 187 |
<P> |
<P> |
| 188 |
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. |
| 190 |
number. If the number is less than 10, or if there have been at least that many |
number. If the number is less than 10, or if there have been at least that many |
| 191 |
previous capturing left parentheses in the expression, the entire sequence is |
previous capturing left parentheses in the expression, the entire sequence is |
| 192 |
taken as a <i>back reference</i>. A description of how this works is given |
taken as a <i>back reference</i>. A description of how this works is given |
| 193 |
later, following the discussion of parenthesized subpatterns. |
<a href="#backreferences">later,</a> |
| 194 |
|
following the discussion of |
| 195 |
|
<a href="#subpattern">parenthesized subpatterns.</a> |
| 196 |
</P> |
</P> |
| 197 |
<P> |
<P> |
| 198 |
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 |
| 200 |
digits following the backslash, and generates a single byte from the least |
digits following the backslash, and generates a single byte from the least |
| 201 |
significant 8 bits of the value. Any subsequent digits stand for themselves. |
significant 8 bits of the value. Any subsequent digits stand for themselves. |
| 202 |
For example: |
For example: |
|
</P> |
|
|
<P> |
|
| 203 |
<pre> |
<pre> |
| 204 |
\040 is another way of writing a space |
\040 is another way of writing a space |
| 205 |
\40 is the same, provided there are fewer than 40 |
\40 is the same, provided there are fewer than 40 previous capturing subpatterns |
|
previous capturing subpatterns |
|
| 206 |
\7 is always a back reference |
\7 is always a back reference |
| 207 |
\11 might be a back reference, or another way of |
\11 might be a back reference, or another way of writing a tab |
|
writing a tab |
|
| 208 |
\011 is always a tab |
\011 is always a tab |
| 209 |
\0113 is a tab followed by the character "3" |
\0113 is a tab followed by the character "3" |
| 210 |
\113 might be a back reference, otherwise the |
\113 might be a back reference, otherwise the character with octal code 113 |
| 211 |
character with octal code 113 |
\377 might be a back reference, otherwise the byte consisting entirely of 1 bits |
| 212 |
\377 might be a back reference, otherwise |
\81 is either a back reference, or a binary zero followed by the two characters "8" and "1" |
| 213 |
the byte consisting entirely of 1 bits |
</pre> |
|
\81 is either a back reference, or a binary zero |
|
|
followed by the two characters "8" and "1" |
|
|
</PRE> |
|
|
</P> |
|
|
<P> |
|
| 214 |
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 |
| 215 |
zero, because no more than three octal digits are ever read. |
zero, because no more than three octal digits are ever read. |
| 216 |
</P> |
</P> |
| 218 |
All the sequences that define a single byte value or a single UTF-8 character |
All the sequences that define a single byte value or a single UTF-8 character |
| 219 |
(in UTF-8 mode) can be used both inside and outside character classes. In |
(in UTF-8 mode) can be used both inside and outside character classes. In |
| 220 |
addition, inside a character class, the sequence \b is interpreted as the |
addition, inside a character class, the sequence \b is interpreted as the |
| 221 |
backspace character (hex 08). Outside a character class it has a different |
backspace character (hex 08), and the sequence \X is interpreted as the |
| 222 |
meaning (see below). |
character "X". Outside a character class, these sequences have different |
| 223 |
</P> |
meanings |
| 224 |
<P> |
<a href="#uniextseq">(see below).</a> |
| 225 |
The third use of backslash is for specifying generic character types: |
</P> |
| 226 |
</P> |
<br><b> |
| 227 |
|
Generic character types |
| 228 |
|
</b><br> |
| 229 |
<P> |
<P> |
| 230 |
|
The third use of backslash is for specifying generic character types. The |
| 231 |
|
following are always recognized: |
| 232 |
<pre> |
<pre> |
| 233 |
\d any decimal digit |
\d any decimal digit |
| 234 |
\D any character that is not a decimal digit |
\D any character that is not a decimal digit |
| 236 |
\S any character that is not a whitespace character |
\S any character that is not a whitespace character |
| 237 |
\w any "word" character |
\w any "word" character |
| 238 |
\W any "non-word" character |
\W any "non-word" character |
| 239 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 240 |
Each pair of escape sequences partitions the complete set of characters into |
Each pair of escape sequences partitions the complete set of characters into |
| 241 |
two disjoint sets. Any given character matches one, and only one, of each pair. |
two disjoint sets. Any given character matches one, and only one, of each pair. |
| 242 |
</P> |
</P> |
| 243 |
<P> |
<P> |
| 244 |
In UTF-8 mode, characters with values greater than 255 never match \d, \s, or |
These character type sequences can appear both inside and outside character |
| 245 |
\w, and always match \D, \S, and \W. |
classes. They each match one character of the appropriate type. If the current |
| 246 |
|
matching point is at the end of the subject string, all of them fail, since |
| 247 |
|
there is no character to match. |
| 248 |
</P> |
</P> |
| 249 |
<P> |
<P> |
| 250 |
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). |
| 252 |
are HT (9), LF (10), FF (12), CR (13), and space (32). |
are HT (9), LF (10), FF (12), CR (13), and space (32). |
| 253 |
</P> |
</P> |
| 254 |
<P> |
<P> |
| 255 |
A "word" character is any letter or digit or the underscore character, that is, |
A "word" character is an underscore or any character less than 256 that is a |
| 256 |
any character which can be part of a Perl "word". The definition of letters and |
letter or digit. The definition of letters and digits is controlled by PCRE's |
| 257 |
digits is controlled by PCRE's character tables, and may vary if locale- |
low-valued character tables, and may vary if locale-specific matching is taking |
| 258 |
specific matching is taking place (see |
place (see |
| 259 |
<a href="pcreapi.html#localesupport">"Locale support"</a> |
<a href="pcreapi.html#localesupport">"Locale support"</a> |
| 260 |
in the |
in the |
| 261 |
<a href="pcreapi.html"><b>pcreapi</b></a> |
<a href="pcreapi.html"><b>pcreapi</b></a> |
| 262 |
page). For example, in the "fr" (French) locale, some character codes greater |
page). For example, in the "fr_FR" (French) locale, some character codes |
| 263 |
than 128 are used for accented letters, and these are matched by \w. |
greater than 128 are used for accented letters, and these are matched by \w. |
| 264 |
</P> |
</P> |
| 265 |
<P> |
<P> |
| 266 |
These character type sequences can appear both inside and outside character |
In UTF-8 mode, characters with values greater than 128 never match \d, \s, or |
| 267 |
classes. They each match one character of the appropriate type. If the current |
\w, and always match \D, \S, and \W. This is true even when Unicode |
| 268 |
matching point is at the end of the subject string, all of them fail, since |
character property support is available. |
| 269 |
there is no character to match. |
<a name="uniextseq"></a></P> |
| 270 |
</P> |
<br><b> |
| 271 |
|
Unicode character properties |
| 272 |
|
</b><br> |
| 273 |
|
<P> |
| 274 |
|
When PCRE is built with Unicode character property support, three additional |
| 275 |
|
escape sequences to match generic character types are available when UTF-8 mode |
| 276 |
|
is selected. They are: |
| 277 |
|
<pre> |
| 278 |
|
\p{<i>xx</i>} a character with the <i>xx</i> property |
| 279 |
|
\P{<i>xx</i>} a character without the <i>xx</i> property |
| 280 |
|
\X an extended Unicode sequence |
| 281 |
|
</pre> |
| 282 |
|
The property names represented by <i>xx</i> above are limited to the |
| 283 |
|
Unicode general category properties. Each character has exactly one such |
| 284 |
|
property, specified by a two-letter abbreviation. For compatibility with Perl, |
| 285 |
|
negation can be specified by including a circumflex between the opening brace |
| 286 |
|
and the property name. For example, \p{^Lu} is the same as \P{Lu}. |
| 287 |
|
</P> |
| 288 |
|
<P> |
| 289 |
|
If only one letter is specified with \p or \P, it includes all the properties |
| 290 |
|
that start with that letter. In this case, in the absence of negation, the |
| 291 |
|
curly brackets in the escape sequence are optional; these two examples have |
| 292 |
|
the same effect: |
| 293 |
|
<pre> |
| 294 |
|
\p{L} |
| 295 |
|
\pL |
| 296 |
|
</pre> |
| 297 |
|
The following property codes are supported: |
| 298 |
|
<pre> |
| 299 |
|
C Other |
| 300 |
|
Cc Control |
| 301 |
|
Cf Format |
| 302 |
|
Cn Unassigned |
| 303 |
|
Co Private use |
| 304 |
|
Cs Surrogate |
| 305 |
|
|
| 306 |
|
L Letter |
| 307 |
|
Ll Lower case letter |
| 308 |
|
Lm Modifier letter |
| 309 |
|
Lo Other letter |
| 310 |
|
Lt Title case letter |
| 311 |
|
Lu Upper case letter |
| 312 |
|
|
| 313 |
|
M Mark |
| 314 |
|
Mc Spacing mark |
| 315 |
|
Me Enclosing mark |
| 316 |
|
Mn Non-spacing mark |
| 317 |
|
|
| 318 |
|
N Number |
| 319 |
|
Nd Decimal number |
| 320 |
|
Nl Letter number |
| 321 |
|
No Other number |
| 322 |
|
|
| 323 |
|
P Punctuation |
| 324 |
|
Pc Connector punctuation |
| 325 |
|
Pd Dash punctuation |
| 326 |
|
Pe Close punctuation |
| 327 |
|
Pf Final punctuation |
| 328 |
|
Pi Initial punctuation |
| 329 |
|
Po Other punctuation |
| 330 |
|
Ps Open punctuation |
| 331 |
|
|
| 332 |
|
S Symbol |
| 333 |
|
Sc Currency symbol |
| 334 |
|
Sk Modifier symbol |
| 335 |
|
Sm Mathematical symbol |
| 336 |
|
So Other symbol |
| 337 |
|
|
| 338 |
|
Z Separator |
| 339 |
|
Zl Line separator |
| 340 |
|
Zp Paragraph separator |
| 341 |
|
Zs Space separator |
| 342 |
|
</pre> |
| 343 |
|
Extended properties such as "Greek" or "InMusicalSymbols" are not supported by |
| 344 |
|
PCRE. |
| 345 |
|
</P> |
| 346 |
|
<P> |
| 347 |
|
Specifying caseless matching does not affect these escape sequences. For |
| 348 |
|
example, \p{Lu} always matches only upper case letters. |
| 349 |
|
</P> |
| 350 |
|
<P> |
| 351 |
|
The \X escape matches any number of Unicode characters that form an extended |
| 352 |
|
Unicode sequence. \X is equivalent to |
| 353 |
|
<pre> |
| 354 |
|
(?>\PM\pM*) |
| 355 |
|
</pre> |
| 356 |
|
That is, it matches a character without the "mark" property, followed by zero |
| 357 |
|
or more characters with the "mark" property, and treats the sequence as an |
| 358 |
|
atomic group |
| 359 |
|
<a href="#atomicgroup">(see below).</a> |
| 360 |
|
Characters with the "mark" property are typically accents that affect the |
| 361 |
|
preceding character. |
| 362 |
|
</P> |
| 363 |
|
<P> |
| 364 |
|
Matching characters by Unicode property is not fast, because PCRE has to search |
| 365 |
|
a structure that contains data for over fifteen thousand characters. That is |
| 366 |
|
why the traditional escape sequences such as \d and \w do not use Unicode |
| 367 |
|
properties in PCRE. |
| 368 |
|
<a name="smallassertions"></a></P> |
| 369 |
|
<br><b> |
| 370 |
|
Simple assertions |
| 371 |
|
</b><br> |
| 372 |
<P> |
<P> |
| 373 |
The fourth use of backslash is for certain simple assertions. An assertion |
The fourth use of backslash is for certain simple assertions. An assertion |
| 374 |
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, |
| 375 |
without consuming any characters from the subject string. The use of |
without consuming any characters from the subject string. The use of |
| 376 |
subpatterns for more complicated assertions is described below. The backslashed |
subpatterns for more complicated assertions is described |
| 377 |
assertions are |
<a href="#bigassertions">below.</a> |
| 378 |
</P> |
The backslashed |
| 379 |
<P> |
assertions are: |
| 380 |
<pre> |
<pre> |
| 381 |
\b matches at a word boundary |
\b matches at a word boundary |
| 382 |
\B matches when not at a word boundary |
\B matches when not at a word boundary |
| 384 |
\Z matches at end of subject or before newline at end |
\Z matches at end of subject or before newline at end |
| 385 |
\z matches at end of subject |
\z matches at end of subject |
| 386 |
\G matches at first matching position in subject |
\G matches at first matching position in subject |
| 387 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 388 |
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 |
| 389 |
different meaning, namely the backspace character, inside a character class). |
different meaning, namely the backspace character, inside a character class). |
| 390 |
</P> |
</P> |
| 396 |
</P> |
</P> |
| 397 |
<P> |
<P> |
| 398 |
The \A, \Z, and \z assertions differ from the traditional circumflex and |
The \A, \Z, and \z assertions differ from the traditional circumflex and |
| 399 |
dollar (described below) in that they only ever match at the very start and end |
dollar (described in the next section) in that they only ever match at the very |
| 400 |
of the subject string, whatever options are set. Thus, they are independent of |
start and end of the subject string, whatever options are set. Thus, they are |
| 401 |
multiline mode. |
independent of multiline mode. These three assertions are not affected by the |
| 402 |
</P> |
PCRE_NOTBOL or PCRE_NOTEOL options, which affect only the behaviour of the |
| 403 |
<P> |
circumflex and dollar metacharacters. However, if the <i>startoffset</i> |
| 404 |
They are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options. If the |
argument of <b>pcre_exec()</b> is non-zero, indicating that matching is to start |
| 405 |
<i>startoffset</i> argument of <b>pcre_exec()</b> is non-zero, indicating that |
at a point other than the beginning of the subject, \A can never match. The |
| 406 |
matching is to start at a point other than the beginning of the subject, \A |
difference between \Z and \z is that \Z matches before a newline that is the |
| 407 |
can never match. The difference between \Z and \z is that \Z matches before |
last character of the string as well as at the end of the string, whereas \z |
| 408 |
a newline that is the last character of the string as well as at the end of the |
matches only at the end. |
|
string, whereas \z matches only at the end. |
|
| 409 |
</P> |
</P> |
| 410 |
<P> |
<P> |
| 411 |
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 |
| 430 |
<br><a name="SEC3" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br> |
<br><a name="SEC3" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br> |
| 431 |
<P> |
<P> |
| 432 |
Outside a character class, in the default matching mode, the circumflex |
Outside a character class, in the default matching mode, the circumflex |
| 433 |
character is an assertion which is true only if the current matching point is |
character is an assertion that is true only if the current matching point is |
| 434 |
at the start of the subject string. If the <i>startoffset</i> argument of |
at the start of the subject string. If the <i>startoffset</i> argument of |
| 435 |
<b>pcre_exec()</b> is non-zero, circumflex can never match if the PCRE_MULTILINE |
<b>pcre_exec()</b> is non-zero, circumflex can never match if the PCRE_MULTILINE |
| 436 |
option is unset. Inside a character class, circumflex has an entirely different |
option is unset. Inside a character class, circumflex has an entirely different |
| 437 |
meaning (see below). |
meaning |
| 438 |
|
<a href="#characterclass">(see below).</a> |
| 439 |
</P> |
</P> |
| 440 |
<P> |
<P> |
| 441 |
Circumflex need not be the first character of the pattern if a number of |
Circumflex need not be the first character of the pattern if a number of |
| 447 |
to be anchored.) |
to be anchored.) |
| 448 |
</P> |
</P> |
| 449 |
<P> |
<P> |
| 450 |
A dollar character is an assertion which is true only if the current matching |
A dollar character is an assertion that is true only if the current matching |
| 451 |
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 |
| 452 |
character that is the last character in the string (by default). Dollar need |
character that is the last character in the string (by default). Dollar need |
| 453 |
not be the last character of the pattern if a number of alternatives are |
not be the last character of the pattern if a number of alternatives are |
| 464 |
PCRE_MULTILINE option is set. When this is the case, they match immediately |
PCRE_MULTILINE option is set. When this is the case, they match immediately |
| 465 |
after and immediately before an internal newline character, respectively, in |
after and immediately before an internal newline character, respectively, in |
| 466 |
addition to matching at the start and end of the subject string. For example, |
addition to matching at the start and end of the subject string. For example, |
| 467 |
the pattern /^abc$/ matches the subject string "def\nabc" in multiline mode, |
the pattern /^abc$/ matches the subject string "def\nabc" (where \n |
| 468 |
but not otherwise. Consequently, patterns that are anchored in single line mode |
represents a newline character) in multiline mode, but not otherwise. |
| 469 |
because all branches start with ^ are not anchored in multiline mode, and a |
Consequently, patterns that are anchored in single line mode because all |
| 470 |
match for circumflex is possible when the <i>startoffset</i> argument of |
branches start with ^ are not anchored in multiline mode, and a match for |
| 471 |
<b>pcre_exec()</b> is non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if |
circumflex is possible when the <i>startoffset</i> argument of <b>pcre_exec()</b> |
| 472 |
PCRE_MULTILINE is set. |
is non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is |
| 473 |
|
set. |
| 474 |
</P> |
</P> |
| 475 |
<P> |
<P> |
| 476 |
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 |
| 482 |
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 |
| 483 |
the subject, including a non-printing character, but not (by default) newline. |
the subject, including a non-printing character, but not (by default) newline. |
| 484 |
In UTF-8 mode, a dot matches any UTF-8 character, which might be more than one |
In UTF-8 mode, a dot matches any UTF-8 character, which might be more than one |
| 485 |
byte long, except (by default) for newline. If the PCRE_DOTALL option is set, |
byte long, except (by default) newline. If the PCRE_DOTALL option is set, |
| 486 |
dots match newlines as well. The handling of dot is entirely independent of the |
dots match newlines as well. The handling of dot is entirely independent of the |
| 487 |
handling of circumflex and dollar, the only relationship being that they both |
handling of circumflex and dollar, the only relationship being that they both |
| 488 |
involve newline characters. Dot has no special meaning in a character class. |
involve newline characters. Dot has no special meaning in a character class. |
| 490 |
<br><a name="SEC5" href="#TOC1">MATCHING A SINGLE BYTE</a><br> |
<br><a name="SEC5" href="#TOC1">MATCHING A SINGLE BYTE</a><br> |
| 491 |
<P> |
<P> |
| 492 |
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 |
| 493 |
in and out of UTF-8 mode. Unlike a dot, it always matches a newline. The |
in and out of UTF-8 mode. Unlike a dot, it can match a newline. The feature is |
| 494 |
feature is provided in Perl in order to match individual bytes in UTF-8 mode. |
provided in Perl in order to match individual bytes in UTF-8 mode. Because it |
| 495 |
Because it breaks up UTF-8 characters into individual bytes, what remains in |
breaks up UTF-8 characters into individual bytes, what remains in the string |
| 496 |
the string may be a malformed UTF-8 string. For this reason it is best avoided. |
may be a malformed UTF-8 string. For this reason, the \C escape sequence is |
| 497 |
|
best avoided. |
| 498 |
</P> |
</P> |
| 499 |
<P> |
<P> |
| 500 |
PCRE does not allow \C to appear in lookbehind assertions (see below), because |
PCRE does not allow \C to appear in lookbehind assertions |
| 501 |
in UTF-8 mode it makes it impossible to calculate the length of the lookbehind. |
<a href="#lookbehind">(described below),</a> |
| 502 |
</P> |
because in UTF-8 mode this would make it impossible to calculate the length of |
| 503 |
<br><a name="SEC6" href="#TOC1">SQUARE BRACKETS</a><br> |
the lookbehind. |
| 504 |
|
<a name="characterclass"></a></P> |
| 505 |
|
<br><a name="SEC6" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br> |
| 506 |
<P> |
<P> |
| 507 |
An opening square bracket introduces a character class, terminated by a closing |
An opening square bracket introduces a character class, terminated by a closing |
| 508 |
square bracket. A closing square bracket on its own is not special. If a |
square bracket. A closing square bracket on its own is not special. If a |
| 522 |
<P> |
<P> |
| 523 |
For example, the character class [aeiou] matches any lower case vowel, while |
For example, the character class [aeiou] matches any lower case vowel, while |
| 524 |
[^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 |
| 525 |
circumflex is just a convenient notation for specifying the characters which |
circumflex is just a convenient notation for specifying the characters that |
| 526 |
are in the class by enumerating those that are not. It is not an assertion: it |
are in the class by enumerating those that are not. A class that starts with a |
| 527 |
still consumes a character from the subject string, and fails if the current |
circumflex is not an assertion: it still consumes a character from the subject |
| 528 |
pointer is at the end of the string. |
string, and therefore it fails if the current pointer is at the end of the |
| 529 |
|
string. |
| 530 |
</P> |
</P> |
| 531 |
<P> |
<P> |
| 532 |
In UTF-8 mode, characters with values greater than 255 can be included in a |
In UTF-8 mode, characters with values greater than 255 can be included in a |
| 536 |
When caseless matching is set, any letters in a class represent both their |
When caseless matching is set, any letters in a class represent both their |
| 537 |
upper case and lower case versions, so for example, a caseless [aeiou] matches |
upper case and lower case versions, so for example, a caseless [aeiou] matches |
| 538 |
"A" as well as "a", and a caseless [^aeiou] does not match "A", whereas a |
"A" as well as "a", and a caseless [^aeiou] does not match "A", whereas a |
| 539 |
caseful version would. PCRE does not support the concept of case for characters |
caseful version would. When running in UTF-8 mode, PCRE supports the concept of |
| 540 |
with values greater than 255. |
case for characters with values greater than 128 only when it is compiled with |
| 541 |
|
Unicode property support. |
| 542 |
</P> |
</P> |
| 543 |
<P> |
<P> |
| 544 |
The newline character is never treated in any special way in character classes, |
The newline character is never treated in any special way in character classes, |
| 557 |
range. A pattern such as [W-]46] is interpreted as a class of two characters |
range. A pattern such as [W-]46] is interpreted as a class of two characters |
| 558 |
("W" and "-") followed by a literal string "46]", so it would match "W46]" or |
("W" and "-") followed by a literal string "46]", so it would match "W46]" or |
| 559 |
"-46]". However, if the "]" is escaped with a backslash it is interpreted as |
"-46]". However, if the "]" is escaped with a backslash it is interpreted as |
| 560 |
the end of range, so [W-\]46] is interpreted as a single class containing a |
the end of range, so [W-\]46] is interpreted as a class containing a range |
| 561 |
range followed by two separate characters. The octal or hexadecimal |
followed by two other characters. The octal or hexadecimal representation of |
| 562 |
representation of "]" can also be used to end a range. |
"]" can also be used to end a range. |
| 563 |
</P> |
</P> |
| 564 |
<P> |
<P> |
| 565 |
Ranges operate in the collating sequence of character values. They can also be |
Ranges operate in the collating sequence of character values. They can also be |
| 570 |
<P> |
<P> |
| 571 |
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 |
| 572 |
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 |
| 573 |
[][\^_`wxyzabc], matched caselessly, and if character tables for the "fr" |
[][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character |
| 574 |
locale are in use, [\xc8-\xcb] matches accented E characters in both cases. |
tables for the "fr_FR" locale are in use, [\xc8-\xcb] matches accented E |
| 575 |
|
characters in both cases. In UTF-8 mode, PCRE supports the concept of case for |
| 576 |
|
characters with values greater than 128 only when it is compiled with Unicode |
| 577 |
|
property support. |
| 578 |
</P> |
</P> |
| 579 |
<P> |
<P> |
| 580 |
The character types \d, \D, \s, \S, \w, and \W may also appear in a |
The character types \d, \D, \p, \P, \s, \S, \w, and \W may also appear |
| 581 |
character class, and add the characters that they match to the class. For |
in a character class, and add the characters that they match to the class. For |
| 582 |
example, [\dABCDEF] matches any hexadecimal digit. A circumflex can |
example, [\dABCDEF] matches any hexadecimal digit. A circumflex can |
| 583 |
conveniently be used with the upper case character types to specify a more |
conveniently be used with the upper case character types to specify a more |
| 584 |
restricted set of characters than the matching lower case type. For example, |
restricted set of characters than the matching lower case type. For example, |
| 585 |
the class [^\W_] matches any letter or digit, but not underscore. |
the class [^\W_] matches any letter or digit, but not underscore. |
| 586 |
</P> |
</P> |
| 587 |
<P> |
<P> |
| 588 |
All non-alphameric characters other than \, -, ^ (at the start) and the |
The only metacharacters that are recognized in character classes are backslash, |
| 589 |
terminating ] are non-special in character classes, but it does no harm if they |
hyphen (only where it can be interpreted as specifying a range), circumflex |
| 590 |
are escaped. |
(only at the start), opening square bracket (only when it can be interpreted as |
| 591 |
|
introducing a POSIX class name - see the next section), and the terminating |
| 592 |
|
closing square bracket. However, escaping other non-alphanumeric characters |
| 593 |
|
does no harm. |
| 594 |
</P> |
</P> |
| 595 |
<br><a name="SEC7" href="#TOC1">POSIX CHARACTER CLASSES</a><br> |
<br><a name="SEC7" href="#TOC1">POSIX CHARACTER CLASSES</a><br> |
| 596 |
<P> |
<P> |
| 597 |
Perl supports the POSIX notation for character classes, which uses names |
Perl supports the POSIX notation for character classes. This uses names |
| 598 |
enclosed by [: and :] within the enclosing square brackets. PCRE also supports |
enclosed by [: and :] within the enclosing square brackets. PCRE also supports |
| 599 |
this notation. For example, |
this notation. For example, |
|
</P> |
|
|
<P> |
|
| 600 |
<pre> |
<pre> |
| 601 |
[01[:alpha:]%] |
[01[:alpha:]%] |
| 602 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 603 |
matches "0", "1", any alphabetic character, or "%". The supported class names |
matches "0", "1", any alphabetic character, or "%". The supported class names |
| 604 |
are |
are |
|
</P> |
|
|
<P> |
|
| 605 |
<pre> |
<pre> |
| 606 |
alnum letters and digits |
alnum letters and digits |
| 607 |
alpha letters |
alpha letters |
| 617 |
upper upper case letters |
upper upper case letters |
| 618 |
word "word" characters (same as \w) |
word "word" characters (same as \w) |
| 619 |
xdigit hexadecimal digits |
xdigit hexadecimal digits |
| 620 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 621 |
The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), and |
The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), and |
| 622 |
space (32). Notice that this list includes the VT character (code 11). This |
space (32). Notice that this list includes the VT character (code 11). This |
| 623 |
makes "space" different to \s, which does not include VT (for Perl |
makes "space" different to \s, which does not include VT (for Perl |
| 627 |
The name "word" is a Perl extension, and "blank" is a GNU extension from Perl |
The name "word" is a Perl extension, and "blank" is a GNU extension from Perl |
| 628 |
5.8. Another Perl extension is negation, which is indicated by a ^ character |
5.8. Another Perl extension is negation, which is indicated by a ^ character |
| 629 |
after the colon. For example, |
after the colon. For example, |
|
</P> |
|
|
<P> |
|
| 630 |
<pre> |
<pre> |
| 631 |
[12[:^digit:]] |
[12[:^digit:]] |
| 632 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 633 |
matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the POSIX |
matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the POSIX |
| 634 |
syntax [.ch.] and [=ch=] where "ch" is a "collating element", but these are not |
syntax [.ch.] and [=ch=] where "ch" is a "collating element", but these are not |
| 635 |
supported, and an error is given if they are encountered. |
supported, and an error is given if they are encountered. |
| 636 |
</P> |
</P> |
| 637 |
<P> |
<P> |
| 638 |
In UTF-8 mode, characters with values greater than 255 do not match any of |
In UTF-8 mode, characters with values greater than 128 do not match any of |
| 639 |
the POSIX character classes. |
the POSIX character classes. |
| 640 |
</P> |
</P> |
| 641 |
<br><a name="SEC8" href="#TOC1">VERTICAL BAR</a><br> |
<br><a name="SEC8" href="#TOC1">VERTICAL BAR</a><br> |
| 642 |
<P> |
<P> |
| 643 |
Vertical bar characters are used to separate alternative patterns. For example, |
Vertical bar characters are used to separate alternative patterns. For example, |
| 644 |
the pattern |
the pattern |
|
</P> |
|
|
<P> |
|
| 645 |
<pre> |
<pre> |
| 646 |
gilbert|sullivan |
gilbert|sullivan |
| 647 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 648 |
matches either "gilbert" or "sullivan". Any number of alternatives may appear, |
matches either "gilbert" or "sullivan". Any number of alternatives may appear, |
| 649 |
and an empty alternative is permitted (matching the empty string). |
and an empty alternative is permitted (matching the empty string). |
| 650 |
The matching process tries each alternative in turn, from left to right, |
The matching process tries each alternative in turn, from left to right, |
| 651 |
and the first one that succeeds is used. If the alternatives are within a |
and the first one that succeeds is used. If the alternatives are within a |
| 652 |
subpattern (defined below), "succeeds" means matching the rest of the main |
subpattern |
| 653 |
pattern as well as the alternative in the subpattern. |
<a href="#subpattern">(defined below),</a> |
| 654 |
|
"succeeds" means matching the rest of the main pattern as well as the |
| 655 |
|
alternative in the subpattern. |
| 656 |
</P> |
</P> |
| 657 |
<br><a name="SEC9" href="#TOC1">INTERNAL OPTION SETTING</a><br> |
<br><a name="SEC9" href="#TOC1">INTERNAL OPTION SETTING</a><br> |
| 658 |
<P> |
<P> |
| 659 |
The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and |
The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and |
| 660 |
PCRE_EXTENDED options can be changed from within the pattern by a sequence of |
PCRE_EXTENDED options can be changed from within the pattern by a sequence of |
| 661 |
Perl option letters enclosed between "(?" and ")". The option letters are |
Perl option letters enclosed between "(?" and ")". The option letters are |
|
</P> |
|
|
<P> |
|
| 662 |
<pre> |
<pre> |
| 663 |
i for PCRE_CASELESS |
i for PCRE_CASELESS |
| 664 |
m for PCRE_MULTILINE |
m for PCRE_MULTILINE |
| 665 |
s for PCRE_DOTALL |
s for PCRE_DOTALL |
| 666 |
x for PCRE_EXTENDED |
x for PCRE_EXTENDED |
| 667 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 668 |
For example, (?im) sets caseless, multiline matching. It is also possible to |
For example, (?im) sets caseless, multiline matching. It is also possible to |
| 669 |
unset these options by preceding the letter with a hyphen, and a combined |
unset these options by preceding the letter with a hyphen, and a combined |
| 670 |
setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and |
setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and |
| 682 |
<P> |
<P> |
| 683 |
An option change within a subpattern affects only that part of the current |
An option change within a subpattern affects only that part of the current |
| 684 |
pattern that follows it, so |
pattern that follows it, so |
|
</P> |
|
|
<P> |
|
| 685 |
<pre> |
<pre> |
| 686 |
(a(?i)b)c |
(a(?i)b)c |
| 687 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 688 |
matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used). |
matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used). |
| 689 |
By this means, options can be made to have different settings in different |
By this means, options can be made to have different settings in different |
| 690 |
parts of the pattern. Any changes made in one alternative do carry on |
parts of the pattern. Any changes made in one alternative do carry on |
| 691 |
into subsequent branches within the same subpattern. For example, |
into subsequent branches within the same subpattern. For example, |
|
</P> |
|
|
<P> |
|
| 692 |
<pre> |
<pre> |
| 693 |
(a(?i)b|c) |
(a(?i)b|c) |
| 694 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 695 |
matches "ab", "aB", "c", and "C", even though when matching "C" the first |
matches "ab", "aB", "c", and "C", even though when matching "C" the first |
| 696 |
branch is abandoned before the option setting. This is because the effects of |
branch is abandoned before the option setting. This is because the effects of |
| 697 |
option settings happen at compile time. There would be some very weird |
option settings happen at compile time. There would be some very weird |
| 702 |
same way as the Perl-compatible options by using the characters U and X |
same way as the Perl-compatible options by using the characters U and X |
| 703 |
respectively. The (?X) flag setting is special in that it must always occur |
respectively. The (?X) flag setting is special in that it must always occur |
| 704 |
earlier in the pattern than any of the additional features it turns on, even |
earlier in the pattern than any of the additional features it turns on, even |
| 705 |
when it is at top level. It is best put at the start. |
when it is at top level. It is best to put it at the start. |
| 706 |
</P> |
<a name="subpattern"></a></P> |
| 707 |
<br><a name="SEC10" href="#TOC1">SUBPATTERNS</a><br> |
<br><a name="SEC10" href="#TOC1">SUBPATTERNS</a><br> |
| 708 |
<P> |
<P> |
| 709 |
Subpatterns are delimited by parentheses (round brackets), which can be nested. |
Subpatterns are delimited by parentheses (round brackets), which can be nested. |
| 710 |
Marking part of a pattern as a subpattern does two things: |
Turning part of a pattern into a subpattern does two things: |
| 711 |
</P> |
<br> |
| 712 |
<P> |
<br> |
| 713 |
1. It localizes a set of alternatives. For example, the pattern |
1. It localizes a set of alternatives. For example, the pattern |
|
</P> |
|
|
<P> |
|
| 714 |
<pre> |
<pre> |
| 715 |
cat(aract|erpillar|) |
cat(aract|erpillar|) |
| 716 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 717 |
matches one of the words "cat", "cataract", or "caterpillar". Without the |
matches one of the words "cat", "cataract", or "caterpillar". Without the |
| 718 |
parentheses, it would match "cataract", "erpillar" or the empty string. |
parentheses, it would match "cataract", "erpillar" or the empty string. |
| 719 |
</P> |
<br> |
| 720 |
<P> |
<br> |
| 721 |
2. It sets up the subpattern as a capturing subpattern (as defined above). |
2. It sets up the subpattern as a capturing subpattern. This means that, when |
| 722 |
When the whole pattern matches, that portion of the subject string that matched |
the whole pattern matches, that portion of the subject string that matched the |
| 723 |
the subpattern is passed back to the caller via the <i>ovector</i> argument of |
subpattern is passed back to the caller via the <i>ovector</i> argument of |
| 724 |
<b>pcre_exec()</b>. Opening parentheses are counted from left to right (starting |
<b>pcre_exec()</b>. Opening parentheses are counted from left to right (starting |
| 725 |
from 1) to obtain the numbers of the capturing subpatterns. |
from 1) to obtain numbers for the capturing subpatterns. |
| 726 |
</P> |
</P> |
| 727 |
<P> |
<P> |
| 728 |
For example, if the string "the red king" is matched against the pattern |
For example, if the string "the red king" is matched against the pattern |
|
</P> |
|
|
<P> |
|
| 729 |
<pre> |
<pre> |
| 730 |
the ((red|white) (king|queen)) |
the ((red|white) (king|queen)) |
| 731 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 732 |
the captured substrings are "red king", "red", and "king", and are numbered 1, |
the captured substrings are "red king", "red", and "king", and are numbered 1, |
| 733 |
2, and 3, respectively. |
2, and 3, respectively. |
| 734 |
</P> |
</P> |
| 739 |
and a colon, the subpattern does not do any capturing, and is not counted when |
and a colon, the subpattern does not do any capturing, and is not counted when |
| 740 |
computing the number of any subsequent capturing subpatterns. For example, if |
computing the number of any subsequent capturing subpatterns. For example, if |
| 741 |
the string "the white queen" is matched against the pattern |
the string "the white queen" is matched against the pattern |
|
</P> |
|
|
<P> |
|
| 742 |
<pre> |
<pre> |
| 743 |
the ((?:red|white) (king|queen)) |
the ((?:red|white) (king|queen)) |
| 744 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 745 |
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 |
| 746 |
2. The maximum number of capturing subpatterns is 65535, and the maximum depth |
2. The maximum number of capturing subpatterns is 65535, and the maximum depth |
| 747 |
of nesting of all subpatterns, both capturing and non-capturing, is 200. |
of nesting of all subpatterns, both capturing and non-capturing, is 200. |
| 750 |
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 |
| 751 |
a non-capturing subpattern, the option letters may appear between the "?" and |
a non-capturing subpattern, the option letters may appear between the "?" and |
| 752 |
the ":". Thus the two patterns |
the ":". Thus the two patterns |
|
</P> |
|
|
<P> |
|
| 753 |
<pre> |
<pre> |
| 754 |
(?i:saturday|sunday) |
(?i:saturday|sunday) |
| 755 |
(?:(?i)saturday|sunday) |
(?:(?i)saturday|sunday) |
| 756 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 757 |
match exactly the same set of strings. Because alternative branches are tried |
match exactly the same set of strings. Because alternative branches are tried |
| 758 |
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 |
| 759 |
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 |
| 763 |
<P> |
<P> |
| 764 |
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 |
| 765 |
to keep track of the numbers in complicated regular expressions. Furthermore, |
to keep track of the numbers in complicated regular expressions. Furthermore, |
| 766 |
if an expression is modified, the numbers may change. To help with the |
if an expression is modified, the numbers may change. To help with this |
| 767 |
difficulty, PCRE supports the naming of subpatterns, something that Perl does |
difficulty, PCRE supports the naming of subpatterns, something that Perl does |
| 768 |
not provide. The Python syntax (?P<name>...) is used. Names consist of |
not provide. The Python syntax (?P<name>...) is used. Names consist of |
| 769 |
alphanumeric characters and underscores, and must be unique within a pattern. |
alphanumeric characters and underscores, and must be unique within a pattern. |
| 771 |
<P> |
<P> |
| 772 |
Named capturing parentheses are still allocated numbers as well as names. The |
Named capturing parentheses are still allocated numbers as well as names. The |
| 773 |
PCRE API provides function calls for extracting the name-to-number translation |
PCRE API provides function calls for extracting the name-to-number translation |
| 774 |
table from a compiled pattern. For further details see the |
table from a compiled pattern. There is also a convenience function for |
| 775 |
|
extracting a captured substring by name. For further details see the |
| 776 |
<a href="pcreapi.html"><b>pcreapi</b></a> |
<a href="pcreapi.html"><b>pcreapi</b></a> |
| 777 |
documentation. |
documentation. |
| 778 |
</P> |
</P> |
| 780 |
<P> |
<P> |
| 781 |
Repetition is specified by quantifiers, which can follow any of the following |
Repetition is specified by quantifiers, which can follow any of the following |
| 782 |
items: |
items: |
|
</P> |
|
|
<P> |
|
| 783 |
<pre> |
<pre> |
| 784 |
a literal data character |
a literal data character |
| 785 |
the . metacharacter |
the . metacharacter |
| 786 |
the \C escape sequence |
the \C escape sequence |
| 787 |
escapes such as \d that match single characters |
the \X escape sequence (in UTF-8 mode with Unicode properties) |
| 788 |
|
an escape such as \d that matches a single character |
| 789 |
a character class |
a character class |
| 790 |
a back reference (see next section) |
a back reference (see next section) |
| 791 |
a parenthesized subpattern (unless it is an assertion) |
a parenthesized subpattern (unless it is an assertion) |
| 792 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 793 |
The general repetition quantifier specifies a minimum and maximum number of |
The general repetition quantifier specifies a minimum and maximum number of |
| 794 |
permitted matches, by giving the two numbers in curly brackets (braces), |
permitted matches, by giving the two numbers in curly brackets (braces), |
| 795 |
separated by a comma. The numbers must be less than 65536, and the first must |
separated by a comma. The numbers must be less than 65536, and the first must |
| 796 |
be less than or equal to the second. For example: |
be less than or equal to the second. For example: |
|
</P> |
|
|
<P> |
|
| 797 |
<pre> |
<pre> |
| 798 |
z{2,4} |
z{2,4} |
| 799 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 800 |
matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special |
matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special |
| 801 |
character. If the second number is omitted, but the comma is present, there is |
character. If the second number is omitted, but the comma is present, there is |
| 802 |
no upper limit; if the second number and the comma are both omitted, the |
no upper limit; if the second number and the comma are both omitted, the |
| 803 |
quantifier specifies an exact number of required matches. Thus |
quantifier specifies an exact number of required matches. Thus |
|
</P> |
|
|
<P> |
|
| 804 |
<pre> |
<pre> |
| 805 |
[aeiou]{3,} |
[aeiou]{3,} |
| 806 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 807 |
matches at least 3 successive vowels, but may match many more, while |
matches at least 3 successive vowels, but may match many more, while |
|
</P> |
|
|
<P> |
|
| 808 |
<pre> |
<pre> |
| 809 |
\d{8} |
\d{8} |
| 810 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 811 |
matches exactly 8 digits. An opening curly bracket that appears in a position |
matches exactly 8 digits. An opening curly bracket that appears in a position |
| 812 |
where a quantifier is not allowed, or one that does not match the syntax of a |
where a quantifier is not allowed, or one that does not match the syntax of a |
| 813 |
quantifier, is taken as a literal character. For example, {,6} is not a |
quantifier, is taken as a literal character. For example, {,6} is not a |
| 816 |
<P> |
<P> |
| 817 |
In UTF-8 mode, quantifiers apply to UTF-8 characters rather than to individual |
In UTF-8 mode, quantifiers apply to UTF-8 characters rather than to individual |
| 818 |
bytes. Thus, for example, \x{100}{2} matches two UTF-8 characters, each of |
bytes. Thus, for example, \x{100}{2} matches two UTF-8 characters, each of |
| 819 |
which is represented by a two-byte sequence. |
which is represented by a two-byte sequence. Similarly, when Unicode property |
| 820 |
|
support is available, \X{3} matches three Unicode extended sequences, each of |
| 821 |
|
which may be several bytes long (and they may be of different lengths). |
| 822 |
</P> |
</P> |
| 823 |
<P> |
<P> |
| 824 |
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 |
| 827 |
<P> |
<P> |
| 828 |
For convenience (and historical compatibility) the three most common |
For convenience (and historical compatibility) the three most common |
| 829 |
quantifiers have single-character abbreviations: |
quantifiers have single-character abbreviations: |
|
</P> |
|
|
<P> |
|
| 830 |
<pre> |
<pre> |
| 831 |
* is equivalent to {0,} |
* is equivalent to {0,} |
| 832 |
+ is equivalent to {1,} |
+ is equivalent to {1,} |
| 833 |
? is equivalent to {0,1} |
? is equivalent to {0,1} |
| 834 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 835 |
It is possible to construct infinite loops by following a subpattern that can |
It is possible to construct infinite loops by following a subpattern that can |
| 836 |
match no characters with a quantifier that has no upper limit, for example: |
match no characters with a quantifier that has no upper limit, for example: |
|
</P> |
|
|
<P> |
|
| 837 |
<pre> |
<pre> |
| 838 |
(a?)* |
(a?)* |
| 839 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 840 |
Earlier versions of Perl and PCRE used to give an error at compile time for |
Earlier versions of Perl and PCRE used to give an error at compile time for |
| 841 |
such patterns. However, because there are cases where this can be useful, such |
such patterns. However, because there are cases where this can be useful, such |
| 842 |
patterns are now accepted, but if any repetition of the subpattern does in fact |
patterns are now accepted, but if any repetition of the subpattern does in fact |
| 846 |
By default, the quantifiers are "greedy", that is, they match as much as |
By default, the quantifiers are "greedy", that is, they match as much as |
| 847 |
possible (up to the maximum number of permitted times), without causing the |
possible (up to the maximum number of permitted times), without causing the |
| 848 |
rest of the pattern to fail. The classic example of where this gives problems |
rest of the pattern to fail. The classic example of where this gives problems |
| 849 |
is in trying to match comments in C programs. These appear between the |
is in trying to match comments in C programs. These appear between /* and */ |
| 850 |
sequences /* and */ and within the sequence, individual * and / characters may |
and within the comment, individual * and / characters may appear. An attempt to |
| 851 |
appear. An attempt to match C comments by applying the pattern |
match C comments by applying the pattern |
|
</P> |
|
|
<P> |
|
| 852 |
<pre> |
<pre> |
| 853 |
/\*.*\*/ |
/\*.*\*/ |
| 854 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 855 |
to the string |
to the string |
|
</P> |
|
|
<P> |
|
| 856 |
<pre> |
<pre> |
| 857 |
/* first command */ not comment /* second comment */ |
/* first comment */ not comment /* second comment */ |
| 858 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 859 |
fails, because it matches the entire string owing to the greediness of the .* |
fails, because it matches the entire string owing to the greediness of the .* |
| 860 |
item. |
item. |
| 861 |
</P> |
</P> |
| 863 |
However, if a quantifier is followed by a question mark, it ceases to be |
However, if a quantifier is followed by a question mark, it ceases to be |
| 864 |
greedy, and instead matches the minimum number of times possible, so the |
greedy, and instead matches the minimum number of times possible, so the |
| 865 |
pattern |
pattern |
|
</P> |
|
|
<P> |
|
| 866 |
<pre> |
<pre> |
| 867 |
/\*.*?\*/ |
/\*.*?\*/ |
| 868 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 869 |
does the right thing with the C comments. The meaning of the various |
does the right thing with the C comments. The meaning of the various |
| 870 |
quantifiers is not otherwise changed, just the preferred number of matches. |
quantifiers is not otherwise changed, just the preferred number of matches. |
| 871 |
Do not confuse this use of question mark with its use as a quantifier in its |
Do not confuse this use of question mark with its use as a quantifier in its |
| 872 |
own right. Because it has two uses, it can sometimes appear doubled, as in |
own right. Because it has two uses, it can sometimes appear doubled, as in |
|
</P> |
|
|
<P> |
|
| 873 |
<pre> |
<pre> |
| 874 |
\d??\d |
\d??\d |
| 875 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 876 |
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 |
| 877 |
way the rest of the pattern matches. |
way the rest of the pattern matches. |
| 878 |
</P> |
</P> |
| 884 |
</P> |
</P> |
| 885 |
<P> |
<P> |
| 886 |
When a parenthesized subpattern is quantified with a minimum repeat count that |
When a parenthesized subpattern is quantified with a minimum repeat count that |
| 887 |
is greater than 1 or with a limited maximum, more store is required for the |
is greater than 1 or with a limited maximum, more memory is required for the |
| 888 |
compiled pattern, in proportion to the size of the minimum or maximum. |
compiled pattern, in proportion to the size of the minimum or maximum. |
| 889 |
</P> |
</P> |
| 890 |
<P> |
<P> |
| 905 |
is inside capturing parentheses that are the subject of a backreference |
is inside capturing parentheses that are the subject of a backreference |
| 906 |
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, and a later one |
| 907 |
succeed. Consider, for example: |
succeed. Consider, for example: |
|
</P> |
|
|
<P> |
|
| 908 |
<pre> |
<pre> |
| 909 |
(.*)abc\1 |
(.*)abc\1 |
| 910 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 911 |
If the subject is "xyz123abc123" the match point is the fourth character. For |
If the subject is "xyz123abc123" the match point is the fourth character. For |
| 912 |
this reason, such a pattern is not implicitly anchored. |
this reason, such a pattern is not implicitly anchored. |
| 913 |
</P> |
</P> |
| 914 |
<P> |
<P> |
| 915 |
When a capturing subpattern is repeated, the value captured is the substring |
When a capturing subpattern is repeated, the value captured is the substring |
| 916 |
that matched the final iteration. For example, after |
that matched the final iteration. For example, after |
|
</P> |
|
|
<P> |
|
| 917 |
<pre> |
<pre> |
| 918 |
(tweedle[dume]{3}\s*)+ |
(tweedle[dume]{3}\s*)+ |
| 919 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 920 |
has matched "tweedledum tweedledee" the value of the captured substring is |
has matched "tweedledum tweedledee" the value of the captured substring is |
| 921 |
"tweedledee". However, if there are nested capturing subpatterns, the |
"tweedledee". However, if there are nested capturing subpatterns, the |
| 922 |
corresponding captured values may have been set in previous iterations. For |
corresponding captured values may have been set in previous iterations. For |
| 923 |
example, after |
example, after |
|
</P> |
|
|
<P> |
|
| 924 |
<pre> |
<pre> |
| 925 |
/(a|(b))+/ |
/(a|(b))+/ |
| 926 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 927 |
matches "aba" the value of the second captured substring is "b". |
matches "aba" the value of the second captured substring is "b". |
| 928 |
</P> |
<a name="atomicgroup"></a></P> |
| 929 |
<br><a name="SEC13" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br> |
<br><a name="SEC13" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br> |
| 930 |
<P> |
<P> |
| 931 |
With both maximizing and minimizing repetition, failure of what follows |
With both maximizing and minimizing repetition, failure of what follows |
| 937 |
</P> |
</P> |
| 938 |
<P> |
<P> |
| 939 |
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 |
|
</P> |
|
|
<P> |
|
| 940 |
<pre> |
<pre> |
| 941 |
123456bar |
123456bar |
| 942 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 943 |
After matching all 6 digits and then failing to match "foo", the normal |
After matching all 6 digits and then failing to match "foo", the normal |
| 944 |
action of the matcher is to try again with only 5 digits matching the \d+ |
action of the matcher is to try again with only 5 digits matching the \d+ |
| 945 |
item, and then with 4, and so on, before ultimately failing. "Atomic grouping" |
item, and then with 4, and so on, before ultimately failing. "Atomic grouping" |
| 950 |
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 would give up |
| 951 |
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 |
| 952 |
special parenthesis, starting with (?> as in this example: |
special parenthesis, starting with (?> as in this example: |
|
</P> |
|
|
<P> |
|
| 953 |
<pre> |
<pre> |
| 954 |
(?>\d+)foo |
(?>\d+)foo |
| 955 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 956 |
This kind of parenthesis "locks up" the part of the pattern it contains once |
This kind of parenthesis "locks up" the part of the pattern it contains once |
| 957 |
it has matched, and a failure further into the pattern is prevented from |
it has matched, and a failure further into the pattern is prevented from |
| 958 |
backtracking into it. Backtracking past it to previous items, however, works as |
backtracking into it. Backtracking past it to previous items, however, works as |
| 977 |
notation, called a "possessive quantifier" can be used. This consists of an |
notation, called a "possessive quantifier" can be used. This consists of an |
| 978 |
additional + character following a quantifier. Using this notation, the |
additional + character following a quantifier. Using this notation, the |
| 979 |
previous example can be rewritten as |
previous example can be rewritten as |
|
</P> |
|
|
<P> |
|
| 980 |
<pre> |
<pre> |
| 981 |
\d++bar |
\d++foo |
| 982 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 983 |
Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY |
Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY |
| 984 |
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 |
| 985 |
atomic group. However, there is no difference in the meaning or processing of a |
atomic group. However, there is no difference in the meaning or processing of a |
| 994 |
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 |
| 995 |
only way to avoid some failing matches taking a very long time indeed. The |
only way to avoid some failing matches taking a very long time indeed. The |
| 996 |
pattern |
pattern |
|
</P> |
|
|
<P> |
|
| 997 |
<pre> |
<pre> |
| 998 |
(\D+|<\d+>)*[!?] |
(\D+|<\d+>)*[!?] |
| 999 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1000 |
matches an unlimited number of substrings that either consist of non-digits, or |
matches an unlimited number of substrings that either consist of non-digits, or |
| 1001 |
digits enclosed in <>, followed by either ! or ?. When it matches, it runs |
digits enclosed in <>, followed by either ! or ?. When it matches, it runs |
| 1002 |
quickly. However, if it is applied to |
quickly. However, if it is applied to |
|
</P> |
|
|
<P> |
|
| 1003 |
<pre> |
<pre> |
| 1004 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| 1005 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1006 |
it takes a long time before reporting failure. This is because the string can |
it takes a long time before reporting failure. This is because the string can |
| 1007 |
be divided between the two repeats in a large number of ways, and all have to |
be divided between the internal \D+ repeat and the external * repeat in a |
| 1008 |
be tried. (The example used [!?] rather than a single character at the end, |
large number of ways, and all have to be tried. (The example uses [!?] rather |
| 1009 |
because both PCRE and Perl have an optimization that allows for fast failure |
than a single character at the end, because both PCRE and Perl have an |
| 1010 |
when a single character is used. They remember the last single character that |
optimization that allows for fast failure when a single character is used. They |
| 1011 |
is required for a match, and fail early if it is not present in the string.) |
remember the last single character that is required for a match, and fail early |
| 1012 |
If the pattern is changed to |
if it is not present in the string.) If the pattern is changed so that it uses |
| 1013 |
</P> |
an atomic group, like this: |
|
<P> |
|
| 1014 |
<pre> |
<pre> |
| 1015 |
((?>\D+)|<\d+>)*[!?] |
((?>\D+)|<\d+>)*[!?] |
| 1016 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1017 |
sequences of non-digits cannot be broken, and failure happens quickly. |
sequences of non-digits cannot be broken, and failure happens quickly. |
| 1018 |
</P> |
<a name="backreferences"></a></P> |
| 1019 |
<br><a name="SEC14" href="#TOC1">BACK REFERENCES</a><br> |
<br><a name="SEC14" href="#TOC1">BACK REFERENCES</a><br> |
| 1020 |
<P> |
<P> |
| 1021 |
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 |
| 1028 |
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 |
| 1029 |
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 |
| 1030 |
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 |
| 1031 |
numbers less than 10. See the section entitled "Backslash" above for further |
numbers less than 10. See the subsection entitled "Non-printing characters" |
| 1032 |
details of the handling of digits following a backslash. |
<a href="#digitsafterbackslash">above</a> |
| 1033 |
|
for further details of the handling of digits following a backslash. |
| 1034 |
</P> |
</P> |
| 1035 |
<P> |
<P> |
| 1036 |
A back reference matches whatever actually matched the capturing subpattern in |
A back reference matches whatever actually matched the capturing subpattern in |
| 1038 |
itself (see |
itself (see |
| 1039 |
<a href="#subpatternsassubroutines">"Subpatterns as subroutines"</a> |
<a href="#subpatternsassubroutines">"Subpatterns as subroutines"</a> |
| 1040 |
below for a way of doing that). So the pattern |
below for a way of doing that). So the pattern |
|
</P> |
|
|
<P> |
|
| 1041 |
<pre> |
<pre> |
| 1042 |
(sens|respons)e and \1ibility |
(sens|respons)e and \1ibility |
| 1043 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1044 |
matches "sense and sensibility" and "response and responsibility", but not |
matches "sense and sensibility" and "response and responsibility", but not |
| 1045 |
"sense and responsibility". If caseful matching is in force at the time of the |
"sense and responsibility". If caseful matching is in force at the time of the |
| 1046 |
back reference, the case of letters is relevant. For example, |
back reference, the case of letters is relevant. For example, |
|
</P> |
|
|
<P> |
|
| 1047 |
<pre> |
<pre> |
| 1048 |
((?i)rah)\s+\1 |
((?i)rah)\s+\1 |
| 1049 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1050 |
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 |
| 1051 |
capturing subpattern is matched caselessly. |
capturing subpattern is matched caselessly. |
| 1052 |
</P> |
</P> |
| 1053 |
<P> |
<P> |
| 1054 |
Back references to named subpatterns use the Python syntax (?P=name). We could |
Back references to named subpatterns use the Python syntax (?P=name). We could |
| 1055 |
rewrite the above example as follows: |
rewrite the above example as follows: |
|
</P> |
|
|
<P> |
|
| 1056 |
<pre> |
<pre> |
| 1057 |
(?<p1>(?i)rah)\s+(?P=p1) |
(?<p1>(?i)rah)\s+(?P=p1) |
| 1058 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1059 |
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 |
| 1060 |
subpattern has not actually been used in a particular match, any back |
subpattern has not actually been used in a particular match, any back |
| 1061 |
references to it always fail. For example, the pattern |
references to it always fail. For example, the pattern |
|
</P> |
|
|
<P> |
|
| 1062 |
<pre> |
<pre> |
| 1063 |
(a|(bc))\2 |
(a|(bc))\2 |
| 1064 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1065 |
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". Because there may be |
| 1066 |
many capturing parentheses in a pattern, all digits following the backslash are |
many capturing parentheses in a pattern, all digits following the backslash are |
| 1067 |
taken as part of a potential back reference number. If the pattern continues |
taken as part of a potential back reference number. If the pattern continues |
| 1068 |
with a digit character, some delimiter must be used to terminate the back |
with a digit character, some delimiter must be used to terminate the back |
| 1069 |
reference. If the PCRE_EXTENDED option is set, this can be whitespace. |
reference. If the PCRE_EXTENDED option is set, this can be whitespace. |
| 1070 |
Otherwise an empty comment can be used. |
Otherwise an empty comment (see |
| 1071 |
|
<a href="#comments">"Comments"</a> |
| 1072 |
|
below) can be used. |
| 1073 |
</P> |
</P> |
| 1074 |
<P> |
<P> |
| 1075 |
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 |
| 1076 |
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. |
| 1077 |
However, such references can be useful inside repeated subpatterns. For |
However, such references can be useful inside repeated subpatterns. For |
| 1078 |
example, the pattern |
example, the pattern |
|
</P> |
|
|
<P> |
|
| 1079 |
<pre> |
<pre> |
| 1080 |
(a|b\1)+ |
(a|b\1)+ |
| 1081 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1082 |
matches any number of "a"s and also "aba", "ababbaa" etc. At each iteration of |
matches any number of "a"s and also "aba", "ababbaa" etc. At each iteration of |
| 1083 |
the subpattern, the back reference matches the character string corresponding |
the subpattern, the back reference matches the character string corresponding |
| 1084 |
to the previous iteration. In order for this to work, the pattern must be such |
to the previous iteration. In order for this to work, the pattern must be such |
| 1085 |
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 |
| 1086 |
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 |
| 1087 |
minimum of zero. |
minimum of zero. |
| 1088 |
</P> |
<a name="bigassertions"></a></P> |
| 1089 |
<br><a name="SEC15" href="#TOC1">ASSERTIONS</a><br> |
<br><a name="SEC15" href="#TOC1">ASSERTIONS</a><br> |
| 1090 |
<P> |
<P> |
| 1091 |
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 |
| 1092 |
matching point that does not actually consume any characters. The simple |
matching point that does not actually consume any characters. The simple |
| 1093 |
assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are described above. |
assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are described |
| 1094 |
|
<a href="#smallassertions">above.</a> |
| 1095 |
|
</P> |
| 1096 |
|
<P> |
| 1097 |
More complicated assertions are coded as subpatterns. There are two kinds: |
More complicated assertions are coded as subpatterns. There are two kinds: |
| 1098 |
those that look ahead of the current position in the subject string, and those |
those that look ahead of the current position in the subject string, and those |
| 1099 |
that look behind it. |
that look behind it. An assertion subpattern is matched in the normal way, |
| 1100 |
|
except that it does not cause the current matching position to be changed. |
| 1101 |
</P> |
</P> |
| 1102 |
<P> |
<P> |
| 1103 |
An assertion subpattern is matched in the normal way, except that it does not |
Assertion subpatterns are not capturing subpatterns, and may not be repeated, |
| 1104 |
cause the current matching position to be changed. Lookahead assertions start |
because it makes no sense to assert the same thing several times. If any kind |
| 1105 |
with (?= for positive assertions and (?! for negative assertions. For example, |
of assertion contains capturing subpatterns within it, these are counted for |
| 1106 |
|
the purposes of numbering the capturing subpatterns in the whole pattern. |
| 1107 |
|
However, substring capturing is carried out only for positive assertions, |
| 1108 |
|
because it does not make sense for negative assertions. |
| 1109 |
</P> |
</P> |
| 1110 |
|
<br><b> |
| 1111 |
|
Lookahead assertions |
| 1112 |
|
</b><br> |
| 1113 |
<P> |
<P> |
| 1114 |
|
Lookahead assertions start |
| 1115 |
|
with (?= for positive assertions and (?! for negative assertions. For example, |
| 1116 |
<pre> |
<pre> |
| 1117 |
\w+(?=;) |
\w+(?=;) |
| 1118 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1119 |
matches a word followed by a semicolon, but does not include the semicolon in |
matches a word followed by a semicolon, but does not include the semicolon in |
| 1120 |
the match, and |
the match, and |
|
</P> |
|
|
<P> |
|
| 1121 |
<pre> |
<pre> |
| 1122 |
foo(?!bar) |
foo(?!bar) |
| 1123 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1124 |
matches any occurrence of "foo" that is not followed by "bar". Note that the |
matches any occurrence of "foo" that is not followed by "bar". Note that the |
| 1125 |
apparently similar pattern |
apparently similar pattern |
|
</P> |
|
|
<P> |
|
| 1126 |
<pre> |
<pre> |
| 1127 |
(?!foo)bar |
(?!foo)bar |
| 1128 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1129 |
does not find an occurrence of "bar" that is preceded by something other than |
does not find an occurrence of "bar" that is preceded by something other than |
| 1130 |
"foo"; it finds any occurrence of "bar" whatsoever, because the assertion |
"foo"; it finds any occurrence of "bar" whatsoever, because the assertion |
| 1131 |
(?!foo) is always true when the next three characters are "bar". A |
(?!foo) is always true when the next three characters are "bar". A |
| 1132 |
lookbehind assertion is needed to achieve this effect. |
lookbehind assertion is needed to achieve the other effect. |
| 1133 |
</P> |
</P> |
| 1134 |
<P> |
<P> |
| 1135 |
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 |
| 1136 |
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 |
| 1137 |
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. |
| 1138 |
</P> |
<a name="lookbehind"></a></P> |
| 1139 |
|
<br><b> |
| 1140 |
|
Lookbehind assertions |
| 1141 |
|
</b><br> |
| 1142 |
<P> |
<P> |
| 1143 |
Lookbehind assertions start with (?<= for positive assertions and (?<! for |
Lookbehind assertions start with (?<= for positive assertions and (?<! for |
| 1144 |
negative assertions. For example, |
negative assertions. For example, |
|
</P> |
|
|
<P> |
|
| 1145 |
<pre> |
<pre> |
| 1146 |
(?<!foo)bar |
(?<!foo)bar |
| 1147 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1148 |
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 |
| 1149 |
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 |
| 1150 |
have a fixed length. However, if there are several alternatives, they do not |
have a fixed length. However, if there are several alternatives, they do not |
| 1151 |
all have to have the same fixed length. Thus |
all have to have the same fixed length. Thus |
|
</P> |
|
|
<P> |
|
| 1152 |
<pre> |
<pre> |
| 1153 |
(?<=bullock|donkey) |
(?<=bullock|donkey) |
| 1154 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1155 |
is permitted, but |
is permitted, but |
|
</P> |
|
|
<P> |
|
| 1156 |
<pre> |
<pre> |
| 1157 |
(?<!dogs?|cats?) |
(?<!dogs?|cats?) |
| 1158 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1159 |
causes an error at compile time. Branches that match different length strings |
causes an error at compile time. Branches that match different length strings |
| 1160 |
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 |
| 1161 |
extension compared with Perl (at least for 5.8), which requires all branches to |
extension compared with Perl (at least for 5.8), which requires all branches to |
| 1162 |
match the same length of string. An assertion such as |
match the same length of string. An assertion such as |
|
</P> |
|
|
<P> |
|
| 1163 |
<pre> |
<pre> |
| 1164 |
(?<=ab(c|de)) |
(?<=ab(c|de)) |
| 1165 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1166 |
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 |
| 1167 |
lengths, but it is acceptable if rewritten to use two top-level branches: |
lengths, but it is acceptable if rewritten to use two top-level branches: |
|
</P> |
|
|
<P> |
|
| 1168 |
<pre> |
<pre> |
| 1169 |
(?<=abc|abde) |
(?<=abc|abde) |
| 1170 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1171 |
The implementation of lookbehind assertions is, for each alternative, to |
The implementation of lookbehind assertions is, for each alternative, to |
| 1172 |
temporarily move the current position back by the fixed width and then try to |
temporarily move the current position back by the fixed width and then try to |
| 1173 |
match. If there are insufficient characters before the current position, the |
match. If there are insufficient characters before the current position, the |
| 1176 |
<P> |
<P> |
| 1177 |
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) |
| 1178 |
to appear in lookbehind assertions, because it makes it impossible to calculate |
to appear in lookbehind assertions, because it makes it impossible to calculate |
| 1179 |
the length of the lookbehind. |
the length of the lookbehind. The \X escape, which can match different numbers |
| 1180 |
|
of bytes, is also not permitted. |
| 1181 |
</P> |
</P> |
| 1182 |
<P> |
<P> |
| 1183 |
Atomic groups can be used in conjunction with lookbehind assertions to specify |
Atomic groups can be used in conjunction with lookbehind assertions to specify |
| 1184 |
efficient matching at the end of the subject string. Consider a simple pattern |
efficient matching at the end of the subject string. Consider a simple pattern |
| 1185 |
such as |
such as |
|
</P> |
|
|
<P> |
|
| 1186 |
<pre> |
<pre> |
| 1187 |
abcd$ |
abcd$ |
| 1188 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1189 |
when applied to a long string that does not match. Because matching proceeds |
when applied to a long string that does not match. Because matching proceeds |
| 1190 |
from left to right, PCRE will look for each "a" in the subject and then see if |
from left to right, PCRE will look for each "a" in the subject and then see if |
| 1191 |
what follows matches the rest of the pattern. If the pattern is specified as |
what follows matches the rest of the pattern. If the pattern is specified as |
|
</P> |
|
|
<P> |
|
| 1192 |
<pre> |
<pre> |
| 1193 |
^.*abcd$ |
^.*abcd$ |
| 1194 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1195 |
the initial .* matches the entire string at first, but when this fails (because |
the initial .* matches the entire string at first, but when this fails (because |
| 1196 |
there is no following "a"), it backtracks to match all but the last character, |
there is no following "a"), it backtracks to match all but the last character, |
| 1197 |
then all but the last two characters, and so on. Once again the search for "a" |
then all but the last two characters, and so on. Once again the search for "a" |
| 1198 |
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, |
| 1199 |
if the pattern is written as |
if the pattern is written as |
|
</P> |
|
|
<P> |
|
| 1200 |
<pre> |
<pre> |
| 1201 |
^(?>.*)(?<=abcd) |
^(?>.*)(?<=abcd) |
| 1202 |
</PRE> |
</pre> |
| 1203 |
</P> |
or, equivalently, using the possessive quantifier syntax, |
|
<P> |
|
|
or, equivalently, |
|
|
</P> |
|
|
<P> |
|
| 1204 |
<pre> |
<pre> |
| 1205 |
^.*+(?<=abcd) |
^.*+(?<=abcd) |
| 1206 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1207 |
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 |
| 1208 |
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 |
| 1209 |
characters. If it fails, the match fails immediately. For long strings, this |
characters. If it fails, the match fails immediately. For long strings, this |
| 1210 |
approach makes a significant difference to the processing time. |
approach makes a significant difference to the processing time. |
| 1211 |
</P> |
</P> |
| 1212 |
|
<br><b> |
| 1213 |
|
Using multiple assertions |
| 1214 |
|
</b><br> |
| 1215 |
<P> |
<P> |
| 1216 |
Several assertions (of any sort) may occur in succession. For example, |
Several assertions (of any sort) may occur in succession. For example, |
|
</P> |
|
|
<P> |
|
| 1217 |
<pre> |
<pre> |
| 1218 |
(?<=\d{3})(?<!999)foo |
(?<=\d{3})(?<!999)foo |
| 1219 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1220 |
matches "foo" preceded by three digits that are not "999". Notice that each of |
matches "foo" preceded by three digits that are not "999". Notice that each of |
| 1221 |
the assertions is applied independently at the same point in the subject |
the assertions is applied independently at the same point in the subject |
| 1222 |
string. First there is a check that the previous three characters are all |
string. First there is a check that the previous three characters are all |
| 1224 |
This pattern does <i>not</i> match "foo" preceded by six characters, the first |
This pattern does <i>not</i> match "foo" preceded by six characters, the first |
| 1225 |
of which are digits and the last three of which are not "999". For example, it |
of which are digits and the last three of which are not "999". For example, it |
| 1226 |
doesn't match "123abcfoo". A pattern to do that is |
doesn't match "123abcfoo". A pattern to do that is |
|
</P> |
|
|
<P> |
|
| 1227 |
<pre> |
<pre> |
| 1228 |
(?<=\d{3}...)(?<!999)foo |
(?<=\d{3}...)(?<!999)foo |
| 1229 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1230 |
This time the first assertion looks at the preceding six characters, checking |
This time the first assertion looks at the preceding six characters, checking |
| 1231 |
that the first three are digits, and then the second assertion checks that the |
that the first three are digits, and then the second assertion checks that the |
| 1232 |
preceding three characters are not "999". |
preceding three characters are not "999". |
| 1233 |
</P> |
</P> |
| 1234 |
<P> |
<P> |
| 1235 |
Assertions can be nested in any combination. For example, |
Assertions can be nested in any combination. For example, |
|
</P> |
|
|
<P> |
|
| 1236 |
<pre> |
<pre> |
| 1237 |
(?<=(?<!foo)bar)baz |
(?<=(?<!foo)bar)baz |
| 1238 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1239 |
matches an occurrence of "baz" that is preceded by "bar" which in turn is not |
matches an occurrence of "baz" that is preceded by "bar" which in turn is not |
| 1240 |
preceded by "foo", while |
preceded by "foo", while |
|
</P> |
|
|
<P> |
|
| 1241 |
<pre> |
<pre> |
| 1242 |
(?<=\d{3}(?!999)...)foo |
(?<=\d{3}(?!999)...)foo |
| 1243 |
</PRE> |
</pre> |
| 1244 |
</P> |
is another pattern that matches "foo" preceded by three digits and any three |
|
<P> |
|
|
is another pattern which matches "foo" preceded by three digits and any three |
|
| 1245 |
characters that are not "999". |
characters that are not "999". |
| 1246 |
</P> |
</P> |
|
<P> |
|
|
Assertion subpatterns are not capturing subpatterns, and may not be repeated, |
|
|
because it makes no sense to assert the same thing several times. If any kind |
|
|
of assertion contains capturing subpatterns within it, these are counted for |
|
|
the purposes of numbering the capturing subpatterns in the whole pattern. |
|
|
However, substring capturing is carried out only for positive assertions, |
|
|
because it does not make sense for negative assertions. |
|
|
</P> |
|
| 1247 |
<br><a name="SEC16" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br> |
<br><a name="SEC16" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br> |
| 1248 |
<P> |
<P> |
| 1249 |
It is possible to cause the matching process to obey a subpattern |
It is possible to cause the matching process to obey a subpattern |
| 1250 |
conditionally or to choose between two alternative subpatterns, depending on |
conditionally or to choose between two alternative subpatterns, depending on |
| 1251 |
the result of an assertion, or whether a previous capturing subpattern matched |
the result of an assertion, or whether a previous capturing subpattern matched |
| 1252 |
or not. The two possible forms of conditional subpattern are |
or not. The two possible forms of conditional subpattern are |
|
</P> |
|
|
<P> |
|
| 1253 |
<pre> |
<pre> |
| 1254 |
(?(condition)yes-pattern) |
(?(condition)yes-pattern) |
| 1255 |
(?(condition)yes-pattern|no-pattern) |
(?(condition)yes-pattern|no-pattern) |
| 1256 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1257 |
If the condition is satisfied, the yes-pattern is used; otherwise the |
If the condition is satisfied, the yes-pattern is used; otherwise the |
| 1258 |
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 |
| 1259 |
subpattern, a compile-time error occurs. |
subpattern, a compile-time error occurs. |
| 1265 |
than zero. Consider the following pattern, which contains non-significant white |
than zero. Consider the following pattern, which contains non-significant white |
| 1266 |
space to make it more readable (assume the PCRE_EXTENDED option) and to divide |
space to make it more readable (assume the PCRE_EXTENDED option) and to divide |
| 1267 |
it into three parts for ease of discussion: |
it into three parts for ease of discussion: |
|
</P> |
|
|
<P> |
|
| 1268 |
<pre> |
<pre> |
| 1269 |
( \( )? [^()]+ (?(1) \) ) |
( \( )? [^()]+ (?(1) \) ) |
| 1270 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1271 |
The first part matches an optional opening parenthesis, and if that |
The first part matches an optional opening parenthesis, and if that |
| 1272 |
character is present, sets it as the first captured substring. The second part |
character is present, sets it as the first captured substring. The second part |
| 1273 |
matches one or more characters that are not parentheses. The third part is a |
matches one or more characters that are not parentheses. The third part is a |
| 1288 |
This may be a positive or negative lookahead or lookbehind assertion. Consider |
This may be a positive or negative lookahead or lookbehind assertion. Consider |
| 1289 |
this pattern, again containing non-significant white space, and with the two |
this pattern, again containing non-significant white space, and with the two |
| 1290 |
alternatives on the second line: |
alternatives on the second line: |
|
</P> |
|
|
<P> |
|
| 1291 |
<pre> |
<pre> |
| 1292 |
(?(?=[^a-z]*[a-z]) |
(?(?=[^a-z]*[a-z]) |
| 1293 |
\d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} ) |
\d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} ) |
| 1294 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1295 |
The condition is a positive lookahead assertion that matches an optional |
The condition is a positive lookahead assertion that matches an optional |
| 1296 |
sequence of non-letters followed by a letter. In other words, it tests for the |
sequence of non-letters followed by a letter. In other words, it tests for the |
| 1297 |
presence of at least one letter in the subject. If a letter is found, the |
presence of at least one letter in the subject. If a letter is found, the |
| 1298 |
subject is matched against the first alternative; otherwise it is matched |
subject is matched against the first alternative; otherwise it is matched |
| 1299 |
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 |
| 1300 |
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. |
| 1301 |
</P> |
<a name="comments"></a></P> |
| 1302 |
<br><a name="SEC17" href="#TOC1">COMMENTS</a><br> |
<br><a name="SEC17" href="#TOC1">COMMENTS</a><br> |
| 1303 |
<P> |
<P> |
| 1304 |
The sequence (?# marks the start of a comment which continues up to the next |
The sequence (?# marks the start of a comment that continues up to the next |
| 1305 |
closing parenthesis. Nested parentheses are not permitted. The characters |
closing parenthesis. Nested parentheses are not permitted. The characters |
| 1306 |
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. |
| 1307 |
</P> |
</P> |
| 1315 |
Consider the problem of matching a string in parentheses, allowing for |
Consider the problem of matching a string in parentheses, allowing for |
| 1316 |
unlimited nested parentheses. Without the use of recursion, the best that can |
unlimited nested parentheses. Without the use of recursion, the best that can |
| 1317 |
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 |
| 1318 |
is not possible to handle an arbitrary nesting depth. Perl has provided an |
is not possible to handle an arbitrary nesting depth. Perl provides a facility |
| 1319 |
experimental facility that allows regular expressions to recurse (amongst other |
that allows regular expressions to recurse (amongst other things). It does this |
| 1320 |
things). It does this by interpolating Perl code in the expression at run time, |
by interpolating Perl code in the expression at run time, and the code can |
| 1321 |
and the code can refer to the expression itself. A Perl pattern to solve the |
refer to the expression itself. A Perl pattern to solve the parentheses problem |
| 1322 |
parentheses problem can be created like this: |
can be created like this: |
|
</P> |
|
|
<P> |
|
| 1323 |
<pre> |
<pre> |
| 1324 |
$re = qr{\( (?: (?>[^()]+) | (?p{$re}) )* \)}x; |
$re = qr{\( (?: (?>[^()]+) | (?p{$re}) )* \)}x; |
| 1325 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1326 |
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 |
| 1327 |
recursively to the pattern in which it appears. Obviously, PCRE cannot support |
recursively to the pattern in which it appears. Obviously, PCRE cannot support |
| 1328 |
the interpolation of Perl code. Instead, it supports some special syntax for |
the interpolation of Perl code. Instead, it supports some special syntax for |
| 1338 |
<P> |
<P> |
| 1339 |
For example, this PCRE pattern solves the nested parentheses problem (assume |
For example, this PCRE pattern solves the nested parentheses problem (assume |
| 1340 |
the PCRE_EXTENDED option is set so that white space is ignored): |
the PCRE_EXTENDED option is set so that white space is ignored): |
|
</P> |
|
|
<P> |
|
| 1341 |
<pre> |
<pre> |
| 1342 |
\( ( (?>[^()]+) | (?R) )* \) |
\( ( (?>[^()]+) | (?R) )* \) |
| 1343 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1344 |
First it matches an opening parenthesis. Then it matches any number of |
First it matches an opening parenthesis. Then it matches any number of |
| 1345 |
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 |
| 1346 |
match of the pattern itself (that is a correctly parenthesized substring). |
match of the pattern itself (that is a correctly parenthesized substring). |
| 1349 |
<P> |
<P> |
| 1350 |
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 |
| 1351 |
pattern, so instead you could use this: |
pattern, so instead you could use this: |
|
</P> |
|
|
<P> |
|
| 1352 |
<pre> |
<pre> |
| 1353 |
( \( ( (?>[^()]+) | (?1) )* \) ) |
( \( ( (?>[^()]+) | (?1) )* \) ) |
| 1354 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1355 |
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 |
| 1356 |
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 |
| 1357 |
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 |
| 1358 |
parentheses instead. For this, PCRE uses (?P>name), which is an extension to |
parentheses instead. For this, PCRE uses (?P>name), which is an extension to |
| 1359 |
the Python syntax that PCRE uses for named parentheses (Perl does not provide |
the Python syntax that PCRE uses for named parentheses (Perl does not provide |
| 1360 |
named parentheses). We could rewrite the above example as follows: |
named parentheses). We could rewrite the above example as follows: |
|
</P> |
|
|
<P> |
|
| 1361 |
<pre> |
<pre> |
| 1362 |
(?P<pn> \( ( (?>[^()]+) | (?P>pn) )* \) ) |
(?P<pn> \( ( (?>[^()]+) | (?P>pn) )* \) ) |
| 1363 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1364 |
This particular example pattern contains nested unlimited repeats, and so the |
This particular example pattern contains nested unlimited repeats, and so the |
| 1365 |
use of atomic grouping for matching strings of non-parentheses is important |
use of atomic grouping for matching strings of non-parentheses is important |
| 1366 |
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 |
| 1367 |
pattern is applied to |
pattern is applied to |
|
</P> |
|
|
<P> |
|
| 1368 |
<pre> |
<pre> |
| 1369 |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
| 1370 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1371 |
it yields "no match" quickly. However, if atomic grouping is not used, |
it yields "no match" quickly. However, if atomic grouping is not used, |
| 1372 |
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 |
| 1373 |
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 |
| 1377 |
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 |
| 1378 |
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. |
| 1379 |
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 |
| 1380 |
below and the |
the next section and the |
| 1381 |
<a href="pcrecallout.html"><b>pcrecallout</b></a> |
<a href="pcrecallout.html"><b>pcrecallout</b></a> |
| 1382 |
documentation). If the pattern above is matched against |
documentation). If the pattern above is matched against |
|
</P> |
|
|
<P> |
|
| 1383 |
<pre> |
<pre> |
| 1384 |
(ab(cd)ef) |
(ab(cd)ef) |
| 1385 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1386 |
the value for the capturing parentheses is "ef", which is the last value taken |
the value for the capturing parentheses is "ef", which is the last value taken |
| 1387 |
on at the top level. If additional parentheses are added, giving |
on at the top level. If additional parentheses are added, giving |
|
</P> |
|
|
<P> |
|
| 1388 |
<pre> |
<pre> |
| 1389 |
\( ( ( (?>[^()]+) | (?R) )* ) \) |
\( ( ( (?>[^()]+) | (?R) )* ) \) |
| 1390 |
^ ^ |
^ ^ |
| 1391 |
^ ^ |
^ ^ |
| 1392 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1393 |
the string they capture is "ab(cd)ef", the contents of the top level |
the string they capture is "ab(cd)ef", the contents of the top level |
| 1394 |
parentheses. If there are more than 15 capturing parentheses in a pattern, PCRE |
parentheses. If there are more than 15 capturing parentheses in a pattern, PCRE |
| 1395 |
has to obtain extra memory to store data during a recursion, which it does by |
has to obtain extra memory to store data during a recursion, which it does by |
| 1401 |
Consider this pattern, which matches text in angle brackets, allowing for |
Consider this pattern, which matches text in angle brackets, allowing for |
| 1402 |
arbitrary nesting. Only digits are allowed in nested brackets (that is, when |
arbitrary nesting. Only digits are allowed in nested brackets (that is, when |
| 1403 |
recursing), whereas any characters are permitted at the outer level. |
recursing), whereas any characters are permitted at the outer level. |
|
</P> |
|
|
<P> |
|
| 1404 |
<pre> |
<pre> |
| 1405 |
< (?: (?(R) \d++ | [^<>]*+) | (?R)) * > |
< (?: (?(R) \d++ | [^<>]*+) | (?R)) * > |
| 1406 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1407 |
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 |
| 1408 |
different alternatives for the recursive and non-recursive cases. The (?R) item |
different alternatives for the recursive and non-recursive cases. The (?R) item |
| 1409 |
is the actual recursive call. |
is the actual recursive call. |
| 1410 |
</P> |
<a name="subpatternsassubroutines"></a></P> |
| 1411 |
<a name="subpatternsassubroutines"></a><br><a name="SEC19" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br> |
<br><a name="SEC19" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br> |
| 1412 |
<P> |
<P> |
| 1413 |
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 |
| 1414 |
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 |
| 1415 |
subroutine in a programming language. An earlier example pointed out that the |
subroutine in a programming language. An earlier example pointed out that the |
| 1416 |
pattern |
pattern |
|
</P> |
|
|
<P> |
|
| 1417 |
<pre> |
<pre> |
| 1418 |
(sens|respons)e and \1ibility |
(sens|respons)e and \1ibility |
| 1419 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1420 |
matches "sense and sensibility" and "response and responsibility", but not |
matches "sense and sensibility" and "response and responsibility", but not |
| 1421 |
"sense and responsibility". If instead the pattern |
"sense and responsibility". If instead the pattern |
|
</P> |
|
|
<P> |
|
| 1422 |
<pre> |
<pre> |
| 1423 |
(sens|respons)e and (?1)ibility |
(sens|respons)e and (?1)ibility |
| 1424 |
</PRE> |
</pre> |
|
</P> |
|
|
<P> |
|
| 1425 |
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 |
| 1426 |
strings. Such references must, however, follow the subpattern to which they |
strings. Such references must, however, follow the subpattern to which they |
| 1427 |
refer. |
refer. |
| 1444 |
function is to be called. If you want to identify different callout points, you |
function is to be called. If you want to identify different callout points, you |
| 1445 |
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. |
| 1446 |
For example, this pattern has two callout points: |
For example, this pattern has two callout points: |
|
</P> |
|
|
<P> |
|
| 1447 |
<pre> |
<pre> |
| 1448 |
(?C1)\dabc(?C2)def |
(?C1)\dabc(?C2)def |
| 1449 |
</PRE> |
</pre> |
| 1450 |
|
If the PCRE_AUTO_CALLOUT flag is passed to <b>pcre_compile()</b>, callouts are |
| 1451 |
|
automatically installed before each item in the pattern. They are all numbered |
| 1452 |
|
255. |
| 1453 |
</P> |
</P> |
| 1454 |
<P> |
<P> |
| 1455 |
During matching, when PCRE reaches a callout point (and <i>pcre_callout</i> is |
During matching, when PCRE reaches a callout point (and <i>pcre_callout</i> is |
| 1456 |
set), the external function is called. It is provided with the number of the |
set), the external function is called. It is provided with the number of the |
| 1457 |
callout, and, optionally, one item of data originally supplied by the caller of |
callout, the position in the pattern, and, optionally, one item of data |
| 1458 |
<b>pcre_exec()</b>. The callout function may cause matching to backtrack, or to |
originally supplied by the caller of <b>pcre_exec()</b>. The callout function |
| 1459 |
fail altogether. A complete description of the interface to the callout |
may cause matching to proceed, to backtrack, or to fail altogether. A complete |
| 1460 |
function is given in the |
description of the interface to the callout function is given in the |
| 1461 |
<a href="pcrecallout.html"><b>pcrecallout</b></a> |
<a href="pcrecallout.html"><b>pcrecallout</b></a> |
| 1462 |
documentation. |
documentation. |
| 1463 |
</P> |
</P> |
| 1464 |
<P> |
<P> |
| 1465 |
Last updated: 03 February 2003 |
Last updated: 09 September 2004 |
| 1466 |
<br> |
<br> |
| 1467 |
Copyright © 1997-2003 University of Cambridge. |
Copyright © 1997-2004 University of Cambridge. |
| 1468 |
|
<p> |
| 1469 |
|
Return to the <a href="index.html">PCRE index page</a>. |
| 1470 |
|
</p> |