/[pcre]/code/trunk/doc/html/pcrepattern.html
ViewVC logotype

Contents of /code/trunk/doc/html/pcrepattern.html

Parent Directory Parent Directory | Revision Log Revision Log


Revision 142 - (show annotations)
Fri Mar 30 15:55:18 2007 UTC (8 years, 5 months ago) by ph10
File MIME type: text/html
File size: 77989 byte(s)
Error occurred while calculating annotation data.
Trailing spaces.
1 <html>
2 <head>
3 <title>pcrepattern specification</title>
4 </head>
5 <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6 <h1>pcrepattern man page</h1>
7 <p>
8 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>
16 <li><a name="TOC1" href="#SEC1">PCRE REGULAR EXPRESSION DETAILS</a>
17 <li><a name="TOC2" href="#SEC2">CHARACTERS AND METACHARACTERS</a>
18 <li><a name="TOC3" href="#SEC3">BACKSLASH</a>
19 <li><a name="TOC4" href="#SEC4">CIRCUMFLEX AND DOLLAR</a>
20 <li><a name="TOC5" href="#SEC5">FULL STOP (PERIOD, DOT)</a>
21 <li><a name="TOC6" href="#SEC6">MATCHING A SINGLE BYTE</a>
22 <li><a name="TOC7" href="#SEC7">SQUARE BRACKETS AND CHARACTER CLASSES</a>
23 <li><a name="TOC8" href="#SEC8">POSIX CHARACTER CLASSES</a>
24 <li><a name="TOC9" href="#SEC9">VERTICAL BAR</a>
25 <li><a name="TOC10" href="#SEC10">INTERNAL OPTION SETTING</a>
26 <li><a name="TOC11" href="#SEC11">SUBPATTERNS</a>
27 <li><a name="TOC12" href="#SEC12">NAMED SUBPATTERNS</a>
28 <li><a name="TOC13" href="#SEC13">REPETITION</a>
29 <li><a name="TOC14" href="#SEC14">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a>
30 <li><a name="TOC15" href="#SEC15">BACK REFERENCES</a>
31 <li><a name="TOC16" href="#SEC16">ASSERTIONS</a>
32 <li><a name="TOC17" href="#SEC17">CONDITIONAL SUBPATTERNS</a>
33 <li><a name="TOC18" href="#SEC18">COMMENTS</a>
34 <li><a name="TOC19" href="#SEC19">RECURSIVE PATTERNS</a>
35 <li><a name="TOC20" href="#SEC20">SUBPATTERNS AS SUBROUTINES</a>
36 <li><a name="TOC21" href="#SEC21">CALLOUTS</a>
37 <li><a name="TOC22" href="#SEC22">SEE ALSO</a>
38 <li><a name="TOC23" href="#SEC23">AUTHOR</a>
39 <li><a name="TOC24" href="#SEC24">REVISION</a>
40 </ul>
41 <br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br>
42 <P>
43 The syntax and semantics of the regular expressions supported by PCRE are
44 described below. Regular expressions are also described in the Perl
45 documentation and in a number of books, some of which have copious examples.
46 Jeffrey Friedl's "Mastering Regular Expressions", published by O'Reilly, covers
47 regular expressions in great detail. This description of PCRE's regular
48 expressions is intended as reference material.
49 </P>
50 <P>
51 The original operation of PCRE was on strings of one-byte characters. However,
52 there is now also support for UTF-8 character strings. To use this, you must
53 build PCRE to include UTF-8 support, and then call <b>pcre_compile()</b> with
54 the PCRE_UTF8 option. How this affects pattern matching is mentioned in several
55 places below. There is also a summary of UTF-8 features in the
56 <a href="pcre.html#utf8support">section on UTF-8 support</a>
57 in the main
58 <a href="pcre.html"><b>pcre</b></a>
59 page.
60 </P>
61 <P>
62 The remainder of this document discusses the patterns that are supported by
63 PCRE when its main matching function, <b>pcre_exec()</b>, is used.
64 From release 6.0, PCRE offers a second matching function,
65 <b>pcre_dfa_exec()</b>, which matches using a different algorithm that is not
66 Perl-compatible. The advantages and disadvantages of the alternative function,
67 and how it differs from the normal function, are discussed in the
68 <a href="pcrematching.html"><b>pcrematching</b></a>
69 page.
70 </P>
71 <br><a name="SEC2" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br>
72 <P>
73 A regular expression is a pattern that is matched against a subject string from
74 left to right. Most characters stand for themselves in a pattern, and match the
75 corresponding characters in the subject. As a trivial example, the pattern
76 <pre>
77 The quick brown fox
78 </pre>
79 matches a portion of a subject string that is identical to itself. When
80 caseless matching is specified (the PCRE_CASELESS option), letters are matched
81 independently of case. In UTF-8 mode, PCRE always understands the concept of
82 case for characters whose values are less than 128, so caseless matching is
83 always possible. For characters with higher values, the concept of case is
84 supported if PCRE is compiled with Unicode property support, but not otherwise.
85 If you want to use caseless matching for characters 128 and above, you must
86 ensure that PCRE is compiled with Unicode property support as well as with
87 UTF-8 support.
88 </P>
89 <P>
90 The power of regular expressions comes from the ability to include alternatives
91 and repetitions in the pattern. These are encoded in the pattern by the use of
92 <i>metacharacters</i>, which do not stand for themselves but instead are
93 interpreted in some special way.
94 </P>
95 <P>
96 There are two different sets of metacharacters: those that are recognized
97 anywhere in the pattern except within square brackets, and those that are
98 recognized within square brackets. Outside square brackets, the metacharacters
99 are as follows:
100 <pre>
101 \ general escape character with several uses
102 ^ assert start of string (or line, in multiline mode)
103 $ assert end of string (or line, in multiline mode)
104 . match any character except newline (by default)
105 [ start character class definition
106 | start of alternative branch
107 ( start subpattern
108 ) end subpattern
109 ? extends the meaning of (
110 also 0 or 1 quantifier
111 also quantifier minimizer
112 * 0 or more quantifier
113 + 1 or more quantifier
114 also "possessive quantifier"
115 { start min/max quantifier
116 </pre>
117 Part of a pattern that is in square brackets is called a "character class". In
118 a character class the only metacharacters are:
119 <pre>
120 \ general escape character
121 ^ negate the class, but only if the first character
122 - indicates character range
123 [ POSIX character class (only if followed by POSIX syntax)
124 ] terminates the character class
125 </pre>
126 The following sections describe the use of each of the metacharacters.
127 </P>
128 <br><a name="SEC3" href="#TOC1">BACKSLASH</a><br>
129 <P>
130 The backslash character has several uses. Firstly, if it is followed by a
131 non-alphanumeric character, it takes away any special meaning that character
132 may have. This use of backslash as an escape character applies both inside and
133 outside character classes.
134 </P>
135 <P>
136 For example, if you want to match a * character, you write \* in the pattern.
137 This escaping action applies whether or not the following character would
138 otherwise be interpreted as a metacharacter, so it is always safe to precede a
139 non-alphanumeric with backslash to specify that it stands for itself. In
140 particular, if you want to match a backslash, you write \\.
141 </P>
142 <P>
143 If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the
144 pattern (other than in a character class) and characters between a # outside
145 a character class and the next newline are ignored. An escaping backslash can
146 be used to include a whitespace or # character as part of the pattern.
147 </P>
148 <P>
149 If you want to remove the special meaning from a sequence of characters, you
150 can do so by putting them between \Q and \E. This is different from Perl in
151 that $ and @ are handled as literals in \Q...\E sequences in PCRE, whereas in
152 Perl, $ and @ cause variable interpolation. Note the following examples:
153 <pre>
154 Pattern PCRE matches Perl matches
155
156 \Qabc$xyz\E abc$xyz abc followed by the contents of $xyz
157 \Qabc\$xyz\E abc\$xyz abc\$xyz
158 \Qabc\E\$\Qxyz\E abc$xyz abc$xyz
159 </pre>
160 The \Q...\E sequence is recognized both inside and outside character classes.
161 <a name="digitsafterbackslash"></a></P>
162 <br><b>
163 Non-printing characters
164 </b><br>
165 <P>
166 A second use of backslash provides a way of encoding non-printing characters
167 in patterns in a visible manner. There is no restriction on the appearance of
168 non-printing characters, apart from the binary zero that terminates a pattern,
169 but when a pattern is being prepared by text editing, it is usually easier to
170 use one of the following escape sequences than the binary character it
171 represents:
172 <pre>
173 \a alarm, that is, the BEL character (hex 07)
174 \cx "control-x", where x is any character
175 \e escape (hex 1B)
176 \f formfeed (hex 0C)
177 \n newline (hex 0A)
178 \r carriage return (hex 0D)
179 \t tab (hex 09)
180 \ddd character with octal code ddd, or backreference
181 \xhh character with hex code hh
182 \x{hhh..} character with hex code hhh..
183 </pre>
184 The precise effect of \cx is as follows: if x is a lower case letter, it
185 is converted to upper case. Then bit 6 of the character (hex 40) is inverted.
186 Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; becomes hex
187 7B.
188 </P>
189 <P>
190 After \x, from zero to two hexadecimal digits are read (letters can be in
191 upper or lower case). Any number of hexadecimal digits may appear between \x{
192 and }, but the value of the character code must be less than 256 in non-UTF-8
193 mode, and less than 2**31 in UTF-8 mode (that is, the maximum hexadecimal value
194 is 7FFFFFFF). If characters other than hexadecimal digits appear between \x{
195 and }, or if there is no terminating }, this form of escape is not recognized.
196 Instead, the initial \x will be interpreted as a basic hexadecimal escape,
197 with no following digits, giving a character whose value is zero.
198 </P>
199 <P>
200 Characters whose value is less than 256 can be defined by either of the two
201 syntaxes for \x. There is no difference in the way they are handled. For
202 example, \xdc is exactly the same as \x{dc}.
203 </P>
204 <P>
205 After \0 up to two further octal digits are read. If there are fewer than two
206 digits, just those that are present are used. Thus the sequence \0\x\07
207 specifies two binary zeros followed by a BEL character (code value 7). Make
208 sure you supply two digits after the initial zero if the pattern character that
209 follows is itself an octal digit.
210 </P>
211 <P>
212 The handling of a backslash followed by a digit other than 0 is complicated.
213 Outside a character class, PCRE reads it and any following digits as a decimal
214 number. If the number is less than 10, or if there have been at least that many
215 previous capturing left parentheses in the expression, the entire sequence is
216 taken as a <i>back reference</i>. A description of how this works is given
217 <a href="#backreferences">later,</a>
218 following the discussion of
219 <a href="#subpattern">parenthesized subpatterns.</a>
220 </P>
221 <P>
222 Inside a character class, or if the decimal number is greater than 9 and there
223 have not been that many capturing subpatterns, PCRE re-reads up to three octal
224 digits following the backslash, and uses them to generate a data character. Any
225 subsequent digits stand for themselves. In non-UTF-8 mode, the value of a
226 character specified in octal must be less than \400. In UTF-8 mode, values up
227 to \777 are permitted. For example:
228 <pre>
229 \040 is another way of writing a space
230 \40 is the same, provided there are fewer than 40 previous capturing subpatterns
231 \7 is always a back reference
232 \11 might be a back reference, or another way of writing a tab
233 \011 is always a tab
234 \0113 is a tab followed by the character "3"
235 \113 might be a back reference, otherwise the character with octal code 113
236 \377 might be a back reference, otherwise the byte consisting entirely of 1 bits
237 \81 is either a back reference, or a binary zero followed by the two characters "8" and "1"
238 </pre>
239 Note that octal values of 100 or greater must not be introduced by a leading
240 zero, because no more than three octal digits are ever read.
241 </P>
242 <P>
243 All the sequences that define a single character value can be used both inside
244 and outside character classes. In addition, inside a character class, the
245 sequence \b is interpreted as the backspace character (hex 08), and the
246 sequences \R and \X are interpreted as the characters "R" and "X",
247 respectively. Outside a character class, these sequences have different
248 meanings
249 <a href="#uniextseq">(see below).</a>
250 </P>
251 <br><b>
252 Absolute and relative back references
253 </b><br>
254 <P>
255 The sequence \g followed by a positive or negative number, optionally enclosed
256 in braces, is an absolute or relative back reference. Back references are
257 discussed
258 <a href="#backreferences">later,</a>
259 following the discussion of
260 <a href="#subpattern">parenthesized subpatterns.</a>
261 </P>
262 <br><b>
263 Generic character types
264 </b><br>
265 <P>
266 Another use of backslash is for specifying generic character types. The
267 following are always recognized:
268 <pre>
269 \d any decimal digit
270 \D any character that is not a decimal digit
271 \s any whitespace character
272 \S any character that is not a whitespace character
273 \w any "word" character
274 \W any "non-word" character
275 </pre>
276 Each pair of escape sequences partitions the complete set of characters into
277 two disjoint sets. Any given character matches one, and only one, of each pair.
278 </P>
279 <P>
280 These character type sequences can appear both inside and outside character
281 classes. They each match one character of the appropriate type. If the current
282 matching point is at the end of the subject string, all of them fail, since
283 there is no character to match.
284 </P>
285 <P>
286 For compatibility with Perl, \s does not match the VT character (code 11).
287 This makes it different from the the POSIX "space" class. The \s characters
288 are HT (9), LF (10), FF (12), CR (13), and space (32). (If "use locale;" is
289 included in a Perl script, \s may match the VT character. In PCRE, it never
290 does.)
291 </P>
292 <P>
293 A "word" character is an underscore or any character less than 256 that is a
294 letter or digit. The definition of letters and digits is controlled by PCRE's
295 low-valued character tables, and may vary if locale-specific matching is taking
296 place (see
297 <a href="pcreapi.html#localesupport">"Locale support"</a>
298 in the
299 <a href="pcreapi.html"><b>pcreapi</b></a>
300 page). For example, in a French locale such as "fr_FR" in Unix-like systems,
301 or "french" in Windows, some character codes greater than 128 are used for
302 accented letters, and these are matched by \w.
303 </P>
304 <P>
305 In UTF-8 mode, characters with values greater than 128 never match \d, \s, or
306 \w, and always match \D, \S, and \W. This is true even when Unicode
307 character property support is available. The use of locales with Unicode is
308 discouraged.
309 </P>
310 <br><b>
311 Newline sequences
312 </b><br>
313 <P>
314 Outside a character class, the escape sequence \R matches any Unicode newline
315 sequence. This is an extension to Perl. In non-UTF-8 mode \R is equivalent to
316 the following:
317 <pre>
318 (?&#62;\r\n|\n|\x0b|\f|\r|\x85)
319 </pre>
320 This is an example of an "atomic group", details of which are given
321 <a href="#atomicgroup">below.</a>
322 This particular group matches either the two-character sequence CR followed by
323 LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,
324 U+000B), FF (formfeed, U+000C), CR (carriage return, U+000D), or NEL (next
325 line, U+0085). The two-character sequence is treated as a single unit that
326 cannot be split.
327 </P>
328 <P>
329 In UTF-8 mode, two additional characters whose codepoints are greater than 255
330 are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).
331 Unicode character property support is not needed for these characters to be
332 recognized.
333 </P>
334 <P>
335 Inside a character class, \R matches the letter "R".
336 <a name="uniextseq"></a></P>
337 <br><b>
338 Unicode character properties
339 </b><br>
340 <P>
341 When PCRE is built with Unicode character property support, three additional
342 escape sequences to match character properties are available when UTF-8 mode
343 is selected. They are:
344 <pre>
345 \p{<i>xx</i>} a character with the <i>xx</i> property
346 \P{<i>xx</i>} a character without the <i>xx</i> property
347 \X an extended Unicode sequence
348 </pre>
349 The property names represented by <i>xx</i> above are limited to the Unicode
350 script names, the general category properties, and "Any", which matches any
351 character (including newline). Other properties such as "InMusicalSymbols" are
352 not currently supported by PCRE. Note that \P{Any} does not match any
353 characters, so always causes a match failure.
354 </P>
355 <P>
356 Sets of Unicode characters are defined as belonging to certain scripts. A
357 character from one of these sets can be matched using a script name. For
358 example:
359 <pre>
360 \p{Greek}
361 \P{Han}
362 </pre>
363 Those that are not part of an identified script are lumped together as
364 "Common". The current list of scripts is:
365 </P>
366 <P>
367 Arabic,
368 Armenian,
369 Balinese,
370 Bengali,
371 Bopomofo,
372 Braille,
373 Buginese,
374 Buhid,
375 Canadian_Aboriginal,
376 Cherokee,
377 Common,
378 Coptic,
379 Cuneiform,
380 Cypriot,
381 Cyrillic,
382 Deseret,
383 Devanagari,
384 Ethiopic,
385 Georgian,
386 Glagolitic,
387 Gothic,
388 Greek,
389 Gujarati,
390 Gurmukhi,
391 Han,
392 Hangul,
393 Hanunoo,
394 Hebrew,
395 Hiragana,
396 Inherited,
397 Kannada,
398 Katakana,
399 Kharoshthi,
400 Khmer,
401 Lao,
402 Latin,
403 Limbu,
404 Linear_B,
405 Malayalam,
406 Mongolian,
407 Myanmar,
408 New_Tai_Lue,
409 Nko,
410 Ogham,
411 Old_Italic,
412 Old_Persian,
413 Oriya,
414 Osmanya,
415 Phags_Pa,
416 Phoenician,
417 Runic,
418 Shavian,
419 Sinhala,
420 Syloti_Nagri,
421 Syriac,
422 Tagalog,
423 Tagbanwa,
424 Tai_Le,
425 Tamil,
426 Telugu,
427 Thaana,
428 Thai,
429 Tibetan,
430 Tifinagh,
431 Ugaritic,
432 Yi.
433 </P>
434 <P>
435 Each character has exactly one general category property, specified by a
436 two-letter abbreviation. For compatibility with Perl, negation can be specified
437 by including a circumflex between the opening brace and the property name. For
438 example, \p{^Lu} is the same as \P{Lu}.
439 </P>
440 <P>
441 If only one letter is specified with \p or \P, it includes all the general
442 category properties that start with that letter. In this case, in the absence
443 of negation, the curly brackets in the escape sequence are optional; these two
444 examples have the same effect:
445 <pre>
446 \p{L}
447 \pL
448 </pre>
449 The following general category property codes are supported:
450 <pre>
451 C Other
452 Cc Control
453 Cf Format
454 Cn Unassigned
455 Co Private use
456 Cs Surrogate
457
458 L Letter
459 Ll Lower case letter
460 Lm Modifier letter
461 Lo Other letter
462 Lt Title case letter
463 Lu Upper case letter
464
465 M Mark
466 Mc Spacing mark
467 Me Enclosing mark
468 Mn Non-spacing mark
469
470 N Number
471 Nd Decimal number
472 Nl Letter number
473 No Other number
474
475 P Punctuation
476 Pc Connector punctuation
477 Pd Dash punctuation
478 Pe Close punctuation
479 Pf Final punctuation
480 Pi Initial punctuation
481 Po Other punctuation
482 Ps Open punctuation
483
484 S Symbol
485 Sc Currency symbol
486 Sk Modifier symbol
487 Sm Mathematical symbol
488 So Other symbol
489
490 Z Separator
491 Zl Line separator
492 Zp Paragraph separator
493 Zs Space separator
494 </pre>
495 The special property L& is also supported: it matches a character that has
496 the Lu, Ll, or Lt property, in other words, a letter that is not classified as
497 a modifier or "other".
498 </P>
499 <P>
500 The long synonyms for these properties that Perl supports (such as \p{Letter})
501 are not supported by PCRE, nor is it permitted to prefix any of these
502 properties with "Is".
503 </P>
504 <P>
505 No character that is in the Unicode table has the Cn (unassigned) property.
506 Instead, this property is assumed for any code point that is not in the
507 Unicode table.
508 </P>
509 <P>
510 Specifying caseless matching does not affect these escape sequences. For
511 example, \p{Lu} always matches only upper case letters.
512 </P>
513 <P>
514 The \X escape matches any number of Unicode characters that form an extended
515 Unicode sequence. \X is equivalent to
516 <pre>
517 (?&#62;\PM\pM*)
518 </pre>
519 That is, it matches a character without the "mark" property, followed by zero
520 or more characters with the "mark" property, and treats the sequence as an
521 atomic group
522 <a href="#atomicgroup">(see below).</a>
523 Characters with the "mark" property are typically accents that affect the
524 preceding character.
525 </P>
526 <P>
527 Matching characters by Unicode property is not fast, because PCRE has to search
528 a structure that contains data for over fifteen thousand characters. That is
529 why the traditional escape sequences such as \d and \w do not use Unicode
530 properties in PCRE.
531 <a name="smallassertions"></a></P>
532 <br><b>
533 Simple assertions
534 </b><br>
535 <P>
536 The final use of backslash is for certain simple assertions. An assertion
537 specifies a condition that has to be met at a particular point in a match,
538 without consuming any characters from the subject string. The use of
539 subpatterns for more complicated assertions is described
540 <a href="#bigassertions">below.</a>
541 The backslashed assertions are:
542 <pre>
543 \b matches at a word boundary
544 \B matches when not at a word boundary
545 \A matches at the start of the subject
546 \Z matches at the end of the subject
547 also matches before a newline at the end of the subject
548 \z matches only at the end of the subject
549 \G matches at the first matching position in the subject
550 </pre>
551 These assertions may not appear in character classes (but note that \b has a
552 different meaning, namely the backspace character, inside a character class).
553 </P>
554 <P>
555 A word boundary is a position in the subject string where the current character
556 and the previous character do not both match \w or \W (i.e. one matches
557 \w and the other matches \W), or the start or end of the string if the
558 first or last character matches \w, respectively.
559 </P>
560 <P>
561 The \A, \Z, and \z assertions differ from the traditional circumflex and
562 dollar (described in the next section) in that they only ever match at the very
563 start and end of the subject string, whatever options are set. Thus, they are
564 independent of multiline mode. These three assertions are not affected by the
565 PCRE_NOTBOL or PCRE_NOTEOL options, which affect only the behaviour of the
566 circumflex and dollar metacharacters. However, if the <i>startoffset</i>
567 argument of <b>pcre_exec()</b> is non-zero, indicating that matching is to start
568 at a point other than the beginning of the subject, \A can never match. The
569 difference between \Z and \z is that \Z matches before a newline at the end
570 of the string as well as at the very end, whereas \z matches only at the end.
571 </P>
572 <P>
573 The \G assertion is true only when the current matching position is at the
574 start point of the match, as specified by the <i>startoffset</i> argument of
575 <b>pcre_exec()</b>. It differs from \A when the value of <i>startoffset</i> is
576 non-zero. By calling <b>pcre_exec()</b> multiple times with appropriate
577 arguments, you can mimic Perl's /g option, and it is in this kind of
578 implementation where \G can be useful.
579 </P>
580 <P>
581 Note, however, that PCRE's interpretation of \G, as the start of the current
582 match, is subtly different from Perl's, which defines it as the end of the
583 previous match. In Perl, these can be different when the previously matched
584 string was empty. Because PCRE does just one match at a time, it cannot
585 reproduce this behaviour.
586 </P>
587 <P>
588 If all the alternatives of a pattern begin with \G, the expression is anchored
589 to the starting match position, and the "anchored" flag is set in the compiled
590 regular expression.
591 </P>
592 <br><a name="SEC4" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>
593 <P>
594 Outside a character class, in the default matching mode, the circumflex
595 character is an assertion that is true only if the current matching point is
596 at the start of the subject string. If the <i>startoffset</i> argument of
597 <b>pcre_exec()</b> is non-zero, circumflex can never match if the PCRE_MULTILINE
598 option is unset. Inside a character class, circumflex has an entirely different
599 meaning
600 <a href="#characterclass">(see below).</a>
601 </P>
602 <P>
603 Circumflex need not be the first character of the pattern if a number of
604 alternatives are involved, but it should be the first thing in each alternative
605 in which it appears if the pattern is ever to match that branch. If all
606 possible alternatives start with a circumflex, that is, if the pattern is
607 constrained to match only at the start of the subject, it is said to be an
608 "anchored" pattern. (There are also other constructs that can cause a pattern
609 to be anchored.)
610 </P>
611 <P>
612 A dollar character is an assertion that is true only if the current matching
613 point is at the end of the subject string, or immediately before a newline
614 at the end of the string (by default). Dollar need not be the last character of
615 the pattern if a number of alternatives are involved, but it should be the last
616 item in any branch in which it appears. Dollar has no special meaning in a
617 character class.
618 </P>
619 <P>
620 The meaning of dollar can be changed so that it matches only at the very end of
621 the string, by setting the PCRE_DOLLAR_ENDONLY option at compile time. This
622 does not affect the \Z assertion.
623 </P>
624 <P>
625 The meanings of the circumflex and dollar characters are changed if the
626 PCRE_MULTILINE option is set. When this is the case, a circumflex matches
627 immediately after internal newlines as well as at the start of the subject
628 string. It does not match after a newline that ends the string. A dollar
629 matches before any newlines in the string, as well as at the very end, when
630 PCRE_MULTILINE is set. When newline is specified as the two-character
631 sequence CRLF, isolated CR and LF characters do not indicate newlines.
632 </P>
633 <P>
634 For example, the pattern /^abc$/ matches the subject string "def\nabc" (where
635 \n represents a newline) in multiline mode, but not otherwise. Consequently,
636 patterns that are anchored in single line mode because all branches start with
637 ^ are not anchored in multiline mode, and a match for circumflex is possible
638 when the <i>startoffset</i> argument of <b>pcre_exec()</b> is non-zero. The
639 PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set.
640 </P>
641 <P>
642 Note that the sequences \A, \Z, and \z can be used to match the start and
643 end of the subject in both modes, and if all branches of a pattern start with
644 \A it is always anchored, whether or not PCRE_MULTILINE is set.
645 </P>
646 <br><a name="SEC5" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br>
647 <P>
648 Outside a character class, a dot in the pattern matches any one character in
649 the subject string except (by default) a character that signifies the end of a
650 line. In UTF-8 mode, the matched character may be more than one byte long.
651 </P>
652 <P>
653 When a line ending is defined as a single character, dot never matches that
654 character; when the two-character sequence CRLF is used, dot does not match CR
655 if it is immediately followed by LF, but otherwise it matches all characters
656 (including isolated CRs and LFs). When any Unicode line endings are being
657 recognized, dot does not match CR or LF or any of the other line ending
658 characters.
659 </P>
660 <P>
661 The behaviour of dot with regard to newlines can be changed. If the PCRE_DOTALL
662 option is set, a dot matches any one character, without exception. If the
663 two-character sequence CRLF is present in the subject string, it takes two dots
664 to match it.
665 </P>
666 <P>
667 The handling of dot is entirely independent of the handling of circumflex and
668 dollar, the only relationship being that they both involve newlines. Dot has no
669 special meaning in a character class.
670 </P>
671 <br><a name="SEC6" href="#TOC1">MATCHING A SINGLE BYTE</a><br>
672 <P>
673 Outside a character class, the escape sequence \C matches any one byte, both
674 in and out of UTF-8 mode. Unlike a dot, it always matches any line-ending
675 characters. The feature is provided in Perl in order to match individual bytes
676 in UTF-8 mode. Because it breaks up UTF-8 characters into individual bytes,
677 what remains in the string may be a malformed UTF-8 string. For this reason,
678 the \C escape sequence is best avoided.
679 </P>
680 <P>
681 PCRE does not allow \C to appear in lookbehind assertions
682 <a href="#lookbehind">(described below),</a>
683 because in UTF-8 mode this would make it impossible to calculate the length of
684 the lookbehind.
685 <a name="characterclass"></a></P>
686 <br><a name="SEC7" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br>
687 <P>
688 An opening square bracket introduces a character class, terminated by a closing
689 square bracket. A closing square bracket on its own is not special. If a
690 closing square bracket is required as a member of the class, it should be the
691 first data character in the class (after an initial circumflex, if present) or
692 escaped with a backslash.
693 </P>
694 <P>
695 A character class matches a single character in the subject. In UTF-8 mode, the
696 character may occupy more than one byte. A matched character must be in the set
697 of characters defined by the class, unless the first character in the class
698 definition is a circumflex, in which case the subject character must not be in
699 the set defined by the class. If a circumflex is actually required as a member
700 of the class, ensure it is not the first character, or escape it with a
701 backslash.
702 </P>
703 <P>
704 For example, the character class [aeiou] matches any lower case vowel, while
705 [^aeiou] matches any character that is not a lower case vowel. Note that a
706 circumflex is just a convenient notation for specifying the characters that
707 are in the class by enumerating those that are not. A class that starts with a
708 circumflex is not an assertion: it still consumes a character from the subject
709 string, and therefore it fails if the current pointer is at the end of the
710 string.
711 </P>
712 <P>
713 In UTF-8 mode, characters with values greater than 255 can be included in a
714 class as a literal string of bytes, or by using the \x{ escaping mechanism.
715 </P>
716 <P>
717 When caseless matching is set, any letters in a class represent both their
718 upper case and lower case versions, so for example, a caseless [aeiou] matches
719 "A" as well as "a", and a caseless [^aeiou] does not match "A", whereas a
720 caseful version would. In UTF-8 mode, PCRE always understands the concept of
721 case for characters whose values are less than 128, so caseless matching is
722 always possible. For characters with higher values, the concept of case is
723 supported if PCRE is compiled with Unicode property support, but not otherwise.
724 If you want to use caseless matching for characters 128 and above, you must
725 ensure that PCRE is compiled with Unicode property support as well as with
726 UTF-8 support.
727 </P>
728 <P>
729 Characters that might indicate line breaks are never treated in any special way
730 when matching character classes, whatever line-ending sequence is in use, and
731 whatever setting of the PCRE_DOTALL and PCRE_MULTILINE options is used. A class
732 such as [^a] always matches one of these characters.
733 </P>
734 <P>
735 The minus (hyphen) character can be used to specify a range of characters in a
736 character class. For example, [d-m] matches any letter between d and m,
737 inclusive. If a minus character is required in a class, it must be escaped with
738 a backslash or appear in a position where it cannot be interpreted as
739 indicating a range, typically as the first or last character in the class.
740 </P>
741 <P>
742 It is not possible to have the literal character "]" as the end character of a
743 range. A pattern such as [W-]46] is interpreted as a class of two characters
744 ("W" and "-") followed by a literal string "46]", so it would match "W46]" or
745 "-46]". However, if the "]" is escaped with a backslash it is interpreted as
746 the end of range, so [W-\]46] is interpreted as a class containing a range
747 followed by two other characters. The octal or hexadecimal representation of
748 "]" can also be used to end a range.
749 </P>
750 <P>
751 Ranges operate in the collating sequence of character values. They can also be
752 used for characters specified numerically, for example [\000-\037]. In UTF-8
753 mode, ranges can include characters whose values are greater than 255, for
754 example [\x{100}-\x{2ff}].
755 </P>
756 <P>
757 If a range that includes letters is used when caseless matching is set, it
758 matches the letters in either case. For example, [W-c] is equivalent to
759 [][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character
760 tables for a French locale are in use, [\xc8-\xcb] matches accented E
761 characters in both cases. In UTF-8 mode, PCRE supports the concept of case for
762 characters with values greater than 128 only when it is compiled with Unicode
763 property support.
764 </P>
765 <P>
766 The character types \d, \D, \p, \P, \s, \S, \w, and \W may also appear
767 in a character class, and add the characters that they match to the class. For
768 example, [\dABCDEF] matches any hexadecimal digit. A circumflex can
769 conveniently be used with the upper case character types to specify a more
770 restricted set of characters than the matching lower case type. For example,
771 the class [^\W_] matches any letter or digit, but not underscore.
772 </P>
773 <P>
774 The only metacharacters that are recognized in character classes are backslash,
775 hyphen (only where it can be interpreted as specifying a range), circumflex
776 (only at the start), opening square bracket (only when it can be interpreted as
777 introducing a POSIX class name - see the next section), and the terminating
778 closing square bracket. However, escaping other non-alphanumeric characters
779 does no harm.
780 </P>
781 <br><a name="SEC8" href="#TOC1">POSIX CHARACTER CLASSES</a><br>
782 <P>
783 Perl supports the POSIX notation for character classes. This uses names
784 enclosed by [: and :] within the enclosing square brackets. PCRE also supports
785 this notation. For example,
786 <pre>
787 [01[:alpha:]%]
788 </pre>
789 matches "0", "1", any alphabetic character, or "%". The supported class names
790 are
791 <pre>
792 alnum letters and digits
793 alpha letters
794 ascii character codes 0 - 127
795 blank space or tab only
796 cntrl control characters
797 digit decimal digits (same as \d)
798 graph printing characters, excluding space
799 lower lower case letters
800 print printing characters, including space
801 punct printing characters, excluding letters and digits
802 space white space (not quite the same as \s)
803 upper upper case letters
804 word "word" characters (same as \w)
805 xdigit hexadecimal digits
806 </pre>
807 The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), and
808 space (32). Notice that this list includes the VT character (code 11). This
809 makes "space" different to \s, which does not include VT (for Perl
810 compatibility).
811 </P>
812 <P>
813 The name "word" is a Perl extension, and "blank" is a GNU extension from Perl
814 5.8. Another Perl extension is negation, which is indicated by a ^ character
815 after the colon. For example,
816 <pre>
817 [12[:^digit:]]
818 </pre>
819 matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the POSIX
820 syntax [.ch.] and [=ch=] where "ch" is a "collating element", but these are not
821 supported, and an error is given if they are encountered.
822 </P>
823 <P>
824 In UTF-8 mode, characters with values greater than 128 do not match any of
825 the POSIX character classes.
826 </P>
827 <br><a name="SEC9" href="#TOC1">VERTICAL BAR</a><br>
828 <P>
829 Vertical bar characters are used to separate alternative patterns. For example,
830 the pattern
831 <pre>
832 gilbert|sullivan
833 </pre>
834 matches either "gilbert" or "sullivan". Any number of alternatives may appear,
835 and an empty alternative is permitted (matching the empty string). The matching
836 process tries each alternative in turn, from left to right, and the first one
837 that succeeds is used. If the alternatives are within a subpattern
838 <a href="#subpattern">(defined below),</a>
839 "succeeds" means matching the rest of the main pattern as well as the
840 alternative in the subpattern.
841 </P>
842 <br><a name="SEC10" href="#TOC1">INTERNAL OPTION SETTING</a><br>
843 <P>
844 The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
845 PCRE_EXTENDED options can be changed from within the pattern by a sequence of
846 Perl option letters enclosed between "(?" and ")". The option letters are
847 <pre>
848 i for PCRE_CASELESS
849 m for PCRE_MULTILINE
850 s for PCRE_DOTALL
851 x for PCRE_EXTENDED
852 </pre>
853 For example, (?im) sets caseless, multiline matching. It is also possible to
854 unset these options by preceding the letter with a hyphen, and a combined
855 setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and
856 PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, is also
857 permitted. If a letter appears both before and after the hyphen, the option is
858 unset.
859 </P>
860 <P>
861 When an option change occurs at top level (that is, not inside subpattern
862 parentheses), the change applies to the remainder of the pattern that follows.
863 If the change is placed right at the start of a pattern, PCRE extracts it into
864 the global options (and it will therefore show up in data extracted by the
865 <b>pcre_fullinfo()</b> function).
866 </P>
867 <P>
868 An option change within a subpattern (see below for a description of
869 subpatterns) affects only that part of the current pattern that follows it, so
870 <pre>
871 (a(?i)b)c
872 </pre>
873 matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used).
874 By this means, options can be made to have different settings in different
875 parts of the pattern. Any changes made in one alternative do carry on
876 into subsequent branches within the same subpattern. For example,
877 <pre>
878 (a(?i)b|c)
879 </pre>
880 matches "ab", "aB", "c", and "C", even though when matching "C" the first
881 branch is abandoned before the option setting. This is because the effects of
882 option settings happen at compile time. There would be some very weird
883 behaviour otherwise.
884 </P>
885 <P>
886 The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be
887 changed in the same way as the Perl-compatible options by using the characters
888 J, U and X respectively.
889 <a name="subpattern"></a></P>
890 <br><a name="SEC11" href="#TOC1">SUBPATTERNS</a><br>
891 <P>
892 Subpatterns are delimited by parentheses (round brackets), which can be nested.
893 Turning part of a pattern into a subpattern does two things:
894 <br>
895 <br>
896 1. It localizes a set of alternatives. For example, the pattern
897 <pre>
898 cat(aract|erpillar|)
899 </pre>
900 matches one of the words "cat", "cataract", or "caterpillar". Without the
901 parentheses, it would match "cataract", "erpillar" or an empty string.
902 <br>
903 <br>
904 2. It sets up the subpattern as a capturing subpattern. This means that, when
905 the whole pattern matches, that portion of the subject string that matched the
906 subpattern is passed back to the caller via the <i>ovector</i> argument of
907 <b>pcre_exec()</b>. Opening parentheses are counted from left to right (starting
908 from 1) to obtain numbers for the capturing subpatterns.
909 </P>
910 <P>
911 For example, if the string "the red king" is matched against the pattern
912 <pre>
913 the ((red|white) (king|queen))
914 </pre>
915 the captured substrings are "red king", "red", and "king", and are numbered 1,
916 2, and 3, respectively.
917 </P>
918 <P>
919 The fact that plain parentheses fulfil two functions is not always helpful.
920 There are often times when a grouping subpattern is required without a
921 capturing requirement. If an opening parenthesis is followed by a question mark
922 and a colon, the subpattern does not do any capturing, and is not counted when
923 computing the number of any subsequent capturing subpatterns. For example, if
924 the string "the white queen" is matched against the pattern
925 <pre>
926 the ((?:red|white) (king|queen))
927 </pre>
928 the captured substrings are "white queen" and "queen", and are numbered 1 and
929 2. The maximum number of capturing subpatterns is 65535.
930 </P>
931 <P>
932 As a convenient shorthand, if any option settings are required at the start of
933 a non-capturing subpattern, the option letters may appear between the "?" and
934 the ":". Thus the two patterns
935 <pre>
936 (?i:saturday|sunday)
937 (?:(?i)saturday|sunday)
938 </pre>
939 match exactly the same set of strings. Because alternative branches are tried
940 from left to right, and options are not reset until the end of the subpattern
941 is reached, an option setting in one branch does affect subsequent branches, so
942 the above patterns match "SUNDAY" as well as "Saturday".
943 </P>
944 <br><a name="SEC12" href="#TOC1">NAMED SUBPATTERNS</a><br>
945 <P>
946 Identifying capturing parentheses by number is simple, but it can be very hard
947 to keep track of the numbers in complicated regular expressions. Furthermore,
948 if an expression is modified, the numbers may change. To help with this
949 difficulty, PCRE supports the naming of subpatterns. This feature was not
950 added to Perl until release 5.10. Python had the feature earlier, and PCRE
951 introduced it at release 4.0, using the Python syntax. PCRE now supports both
952 the Perl and the Python syntax.
953 </P>
954 <P>
955 In PCRE, a subpattern can be named in one of three ways: (?&#60;name&#62;...) or
956 (?'name'...) as in Perl, or (?P&#60;name&#62;...) as in Python. References to capturing
957 parentheses from other parts of the pattern, such as
958 <a href="#backreferences">backreferences,</a>
959 <a href="#recursion">recursion,</a>
960 and
961 <a href="#conditions">conditions,</a>
962 can be made by name as well as by number.
963 </P>
964 <P>
965 Names consist of up to 32 alphanumeric characters and underscores. Named
966 capturing parentheses are still allocated numbers as well as names, exactly as
967 if the names were not present. The PCRE API provides function calls for
968 extracting the name-to-number translation table from a compiled pattern. There
969 is also a convenience function for extracting a captured substring by name.
970 </P>
971 <P>
972 By default, a name must be unique within a pattern, but it is possible to relax
973 this constraint by setting the PCRE_DUPNAMES option at compile time. This can
974 be useful for patterns where only one instance of the named parentheses can
975 match. Suppose you want to match the name of a weekday, either as a 3-letter
976 abbreviation or as the full name, and in both cases you want to extract the
977 abbreviation. This pattern (ignoring the line breaks) does the job:
978 <pre>
979 (?&#60;DN&#62;Mon|Fri|Sun)(?:day)?|
980 (?&#60;DN&#62;Tue)(?:sday)?|
981 (?&#60;DN&#62;Wed)(?:nesday)?|
982 (?&#60;DN&#62;Thu)(?:rsday)?|
983 (?&#60;DN&#62;Sat)(?:urday)?
984 </pre>
985 There are five capturing substrings, but only one is ever set after a match.
986 The convenience function for extracting the data by name returns the substring
987 for the first (and in this example, the only) subpattern of that name that
988 matched. This saves searching to find which numbered subpattern it was. If you
989 make a reference to a non-unique named subpattern from elsewhere in the
990 pattern, the one that corresponds to the lowest number is used. For further
991 details of the interfaces for handling named subpatterns, see the
992 <a href="pcreapi.html"><b>pcreapi</b></a>
993 documentation.
994 </P>
995 <br><a name="SEC13" href="#TOC1">REPETITION</a><br>
996 <P>
997 Repetition is specified by quantifiers, which can follow any of the following
998 items:
999 <pre>
1000 a literal data character
1001 the dot metacharacter
1002 the \C escape sequence
1003 the \X escape sequence (in UTF-8 mode with Unicode properties)
1004 the \R escape sequence
1005 an escape such as \d that matches a single character
1006 a character class
1007 a back reference (see next section)
1008 a parenthesized subpattern (unless it is an assertion)
1009 </pre>
1010 The general repetition quantifier specifies a minimum and maximum number of
1011 permitted matches, by giving the two numbers in curly brackets (braces),
1012 separated by a comma. The numbers must be less than 65536, and the first must
1013 be less than or equal to the second. For example:
1014 <pre>
1015 z{2,4}
1016 </pre>
1017 matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special
1018 character. If the second number is omitted, but the comma is present, there is
1019 no upper limit; if the second number and the comma are both omitted, the
1020 quantifier specifies an exact number of required matches. Thus
1021 <pre>
1022 [aeiou]{3,}
1023 </pre>
1024 matches at least 3 successive vowels, but may match many more, while
1025 <pre>
1026 \d{8}
1027 </pre>
1028 matches exactly 8 digits. An opening curly bracket that appears in a position
1029 where a quantifier is not allowed, or one that does not match the syntax of a
1030 quantifier, is taken as a literal character. For example, {,6} is not a
1031 quantifier, but a literal string of four characters.
1032 </P>
1033 <P>
1034 In UTF-8 mode, quantifiers apply to UTF-8 characters rather than to individual
1035 bytes. Thus, for example, \x{100}{2} matches two UTF-8 characters, each of
1036 which is represented by a two-byte sequence. Similarly, when Unicode property
1037 support is available, \X{3} matches three Unicode extended sequences, each of
1038 which may be several bytes long (and they may be of different lengths).
1039 </P>
1040 <P>
1041 The quantifier {0} is permitted, causing the expression to behave as if the
1042 previous item and the quantifier were not present.
1043 </P>
1044 <P>
1045 For convenience, the three most common quantifiers have single-character
1046 abbreviations:
1047 <pre>
1048 * is equivalent to {0,}
1049 + is equivalent to {1,}
1050 ? is equivalent to {0,1}
1051 </pre>
1052 It is possible to construct infinite loops by following a subpattern that can
1053 match no characters with a quantifier that has no upper limit, for example:
1054 <pre>
1055 (a?)*
1056 </pre>
1057 Earlier versions of Perl and PCRE used to give an error at compile time for
1058 such patterns. However, because there are cases where this can be useful, such
1059 patterns are now accepted, but if any repetition of the subpattern does in fact
1060 match no characters, the loop is forcibly broken.
1061 </P>
1062 <P>
1063 By default, the quantifiers are "greedy", that is, they match as much as
1064 possible (up to the maximum number of permitted times), without causing the
1065 rest of the pattern to fail. The classic example of where this gives problems
1066 is in trying to match comments in C programs. These appear between /* and */
1067 and within the comment, individual * and / characters may appear. An attempt to
1068 match C comments by applying the pattern
1069 <pre>
1070 /\*.*\*/
1071 </pre>
1072 to the string
1073 <pre>
1074 /* first comment */ not comment /* second comment */
1075 </pre>
1076 fails, because it matches the entire string owing to the greediness of the .*
1077 item.
1078 </P>
1079 <P>
1080 However, if a quantifier is followed by a question mark, it ceases to be
1081 greedy, and instead matches the minimum number of times possible, so the
1082 pattern
1083 <pre>
1084 /\*.*?\*/
1085 </pre>
1086 does the right thing with the C comments. The meaning of the various
1087 quantifiers is not otherwise changed, just the preferred number of matches.
1088 Do not confuse this use of question mark with its use as a quantifier in its
1089 own right. Because it has two uses, it can sometimes appear doubled, as in
1090 <pre>
1091 \d??\d
1092 </pre>
1093 which matches one digit by preference, but can match two if that is the only
1094 way the rest of the pattern matches.
1095 </P>
1096 <P>
1097 If the PCRE_UNGREEDY option is set (an option that is not available in Perl),
1098 the quantifiers are not greedy by default, but individual ones can be made
1099 greedy by following them with a question mark. In other words, it inverts the
1100 default behaviour.
1101 </P>
1102 <P>
1103 When a parenthesized subpattern is quantified with a minimum repeat count that
1104 is greater than 1 or with a limited maximum, more memory is required for the
1105 compiled pattern, in proportion to the size of the minimum or maximum.
1106 </P>
1107 <P>
1108 If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equivalent
1109 to Perl's /s) is set, thus allowing the dot to match newlines, the pattern is
1110 implicitly anchored, because whatever follows will be tried against every
1111 character position in the subject string, so there is no point in retrying the
1112 overall match at any position after the first. PCRE normally treats such a
1113 pattern as though it were preceded by \A.
1114 </P>
1115 <P>
1116 In cases where it is known that the subject string contains no newlines, it is
1117 worth setting PCRE_DOTALL in order to obtain this optimization, or
1118 alternatively using ^ to indicate anchoring explicitly.
1119 </P>
1120 <P>
1121 However, there is one situation where the optimization cannot be used. When .*
1122 is inside capturing parentheses that are the subject of a backreference
1123 elsewhere in the pattern, a match at the start may fail where a later one
1124 succeeds. Consider, for example:
1125 <pre>
1126 (.*)abc\1
1127 </pre>
1128 If the subject is "xyz123abc123" the match point is the fourth character. For
1129 this reason, such a pattern is not implicitly anchored.
1130 </P>
1131 <P>
1132 When a capturing subpattern is repeated, the value captured is the substring
1133 that matched the final iteration. For example, after
1134 <pre>
1135 (tweedle[dume]{3}\s*)+
1136 </pre>
1137 has matched "tweedledum tweedledee" the value of the captured substring is
1138 "tweedledee". However, if there are nested capturing subpatterns, the
1139 corresponding captured values may have been set in previous iterations. For
1140 example, after
1141 <pre>
1142 /(a|(b))+/
1143 </pre>
1144 matches "aba" the value of the second captured substring is "b".
1145 <a name="atomicgroup"></a></P>
1146 <br><a name="SEC14" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br>
1147 <P>
1148 With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
1149 repetition, failure of what follows normally causes the repeated item to be
1150 re-evaluated to see if a different number of repeats allows the rest of the
1151 pattern to match. Sometimes it is useful to prevent this, either to change the
1152 nature of the match, or to cause it fail earlier than it otherwise might, when
1153 the author of the pattern knows there is no point in carrying on.
1154 </P>
1155 <P>
1156 Consider, for example, the pattern \d+foo when applied to the subject line
1157 <pre>
1158 123456bar
1159 </pre>
1160 After matching all 6 digits and then failing to match "foo", the normal
1161 action of the matcher is to try again with only 5 digits matching the \d+
1162 item, and then with 4, and so on, before ultimately failing. "Atomic grouping"
1163 (a term taken from Jeffrey Friedl's book) provides the means for specifying
1164 that once a subpattern has matched, it is not to be re-evaluated in this way.
1165 </P>
1166 <P>
1167 If we use atomic grouping for the previous example, the matcher gives up
1168 immediately on failing to match "foo" the first time. The notation is a kind of
1169 special parenthesis, starting with (?&#62; as in this example:
1170 <pre>
1171 (?&#62;\d+)foo
1172 </pre>
1173 This kind of parenthesis "locks up" the part of the pattern it contains once
1174 it has matched, and a failure further into the pattern is prevented from
1175 backtracking into it. Backtracking past it to previous items, however, works as
1176 normal.
1177 </P>
1178 <P>
1179 An alternative description is that a subpattern of this type matches the string
1180 of characters that an identical standalone pattern would match, if anchored at
1181 the current point in the subject string.
1182 </P>
1183 <P>
1184 Atomic grouping subpatterns are not capturing subpatterns. Simple cases such as
1185 the above example can be thought of as a maximizing repeat that must swallow
1186 everything it can. So, while both \d+ and \d+? are prepared to adjust the
1187 number of digits they match in order to make the rest of the pattern match,
1188 (?&#62;\d+) can only match an entire sequence of digits.
1189 </P>
1190 <P>
1191 Atomic groups in general can of course contain arbitrarily complicated
1192 subpatterns, and can be nested. However, when the subpattern for an atomic
1193 group is just a single repeated item, as in the example above, a simpler
1194 notation, called a "possessive quantifier" can be used. This consists of an
1195 additional + character following a quantifier. Using this notation, the
1196 previous example can be rewritten as
1197 <pre>
1198 \d++foo
1199 </pre>
1200 Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY
1201 option is ignored. They are a convenient notation for the simpler forms of
1202 atomic group. However, there is no difference in the meaning of a possessive
1203 quantifier and the equivalent atomic group, though there may be a performance
1204 difference; possessive quantifiers should be slightly faster.
1205 </P>
1206 <P>
1207 The possessive quantifier syntax is an extension to the Perl 5.8 syntax.
1208 Jeffrey Friedl originated the idea (and the name) in the first edition of his
1209 book. Mike McCloskey liked it, so implemented it when he built Sun's Java
1210 package, and PCRE copied it from there. It ultimately found its way into Perl
1211 at release 5.10.
1212 </P>
1213 <P>
1214 PCRE has an optimization that automatically "possessifies" certain simple
1215 pattern constructs. For example, the sequence A+B is treated as A++B because
1216 there is no point in backtracking into a sequence of A's when B must follow.
1217 </P>
1218 <P>
1219 When a pattern contains an unlimited repeat inside a subpattern that can itself
1220 be repeated an unlimited number of times, the use of an atomic group is the
1221 only way to avoid some failing matches taking a very long time indeed. The
1222 pattern
1223 <pre>
1224 (\D+|&#60;\d+&#62;)*[!?]
1225 </pre>
1226 matches an unlimited number of substrings that either consist of non-digits, or
1227 digits enclosed in &#60;&#62;, followed by either ! or ?. When it matches, it runs
1228 quickly. However, if it is applied to
1229 <pre>
1230 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1231 </pre>
1232 it takes a long time before reporting failure. This is because the string can
1233 be divided between the internal \D+ repeat and the external * repeat in a
1234 large number of ways, and all have to be tried. (The example uses [!?] rather
1235 than a single character at the end, because both PCRE and Perl have an
1236 optimization that allows for fast failure when a single character is used. They
1237 remember the last single character that is required for a match, and fail early
1238 if it is not present in the string.) If the pattern is changed so that it uses
1239 an atomic group, like this:
1240 <pre>
1241 ((?&#62;\D+)|&#60;\d+&#62;)*[!?]
1242 </pre>
1243 sequences of non-digits cannot be broken, and failure happens quickly.
1244 <a name="backreferences"></a></P>
1245 <br><a name="SEC15" href="#TOC1">BACK REFERENCES</a><br>
1246 <P>
1247 Outside a character class, a backslash followed by a digit greater than 0 (and
1248 possibly further digits) is a back reference to a capturing subpattern earlier
1249 (that is, to its left) in the pattern, provided there have been that many
1250 previous capturing left parentheses.
1251 </P>
1252 <P>
1253 However, if the decimal number following the backslash is less than 10, it is
1254 always taken as a back reference, and causes an error only if there are not
1255 that many capturing left parentheses in the entire pattern. In other words, the
1256 parentheses that are referenced need not be to the left of the reference for
1257 numbers less than 10. A "forward back reference" of this type can make sense
1258 when a repetition is involved and the subpattern to the right has participated
1259 in an earlier iteration.
1260 </P>
1261 <P>
1262 It is not possible to have a numerical "forward back reference" to a subpattern
1263 whose number is 10 or more using this syntax because a sequence such as \50 is
1264 interpreted as a character defined in octal. See the subsection entitled
1265 "Non-printing characters"
1266 <a href="#digitsafterbackslash">above</a>
1267 for further details of the handling of digits following a backslash. There is
1268 no such problem when named parentheses are used. A back reference to any
1269 subpattern is possible using named parentheses (see below).
1270 </P>
1271 <P>
1272 Another way of avoiding the ambiguity inherent in the use of digits following a
1273 backslash is to use the \g escape sequence, which is a feature introduced in
1274 Perl 5.10. This escape must be followed by a positive or a negative number,
1275 optionally enclosed in braces. These examples are all identical:
1276 <pre>
1277 (ring), \1
1278 (ring), \g1
1279 (ring), \g{1}
1280 </pre>
1281 A positive number specifies an absolute reference without the ambiguity that is
1282 present in the older syntax. It is also useful when literal digits follow the
1283 reference. A negative number is a relative reference. Consider this example:
1284 <pre>
1285 (abc(def)ghi)\g{-1}
1286 </pre>
1287 The sequence \g{-1} is a reference to the most recently started capturing
1288 subpattern before \g, that is, is it equivalent to \2. Similarly, \g{-2}
1289 would be equivalent to \1. The use of relative references can be helpful in
1290 long patterns, and also in patterns that are created by joining together
1291 fragments that contain references within themselves.
1292 </P>
1293 <P>
1294 A back reference matches whatever actually matched the capturing subpattern in
1295 the current subject string, rather than anything matching the subpattern
1296 itself (see
1297 <a href="#subpatternsassubroutines">"Subpatterns as subroutines"</a>
1298 below for a way of doing that). So the pattern
1299 <pre>
1300 (sens|respons)e and \1ibility
1301 </pre>
1302 matches "sense and sensibility" and "response and responsibility", but not
1303 "sense and responsibility". If caseful matching is in force at the time of the
1304 back reference, the case of letters is relevant. For example,
1305 <pre>
1306 ((?i)rah)\s+\1
1307 </pre>
1308 matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original
1309 capturing subpattern is matched caselessly.
1310 </P>
1311 <P>
1312 Back references to named subpatterns use the Perl syntax \k&#60;name&#62; or \k'name'
1313 or the Python syntax (?P=name). We could rewrite the above example in either of
1314 the following ways:
1315 <pre>
1316 (?&#60;p1&#62;(?i)rah)\s+\k&#60;p1&#62;
1317 (?P&#60;p1&#62;(?i)rah)\s+(?P=p1)
1318 </pre>
1319 A subpattern that is referenced by name may appear in the pattern before or
1320 after the reference.
1321 </P>
1322 <P>
1323 There may be more than one back reference to the same subpattern. If a
1324 subpattern has not actually been used in a particular match, any back
1325 references to it always fail. For example, the pattern
1326 <pre>
1327 (a|(bc))\2
1328 </pre>
1329 always fails if it starts to match "a" rather than "bc". Because there may be
1330 many capturing parentheses in a pattern, all digits following the backslash are
1331 taken as part of a potential back reference number. If the pattern continues
1332 with a digit character, some delimiter must be used to terminate the back
1333 reference. If the PCRE_EXTENDED option is set, this can be whitespace.
1334 Otherwise an empty comment (see
1335 <a href="#comments">"Comments"</a>
1336 below) can be used.
1337 </P>
1338 <P>
1339 A back reference that occurs inside the parentheses to which it refers fails
1340 when the subpattern is first used, so, for example, (a\1) never matches.
1341 However, such references can be useful inside repeated subpatterns. For
1342 example, the pattern
1343 <pre>
1344 (a|b\1)+
1345 </pre>
1346 matches any number of "a"s and also "aba", "ababbaa" etc. At each iteration of
1347 the subpattern, the back reference matches the character string corresponding
1348 to the previous iteration. In order for this to work, the pattern must be such
1349 that the first iteration does not need to match the back reference. This can be
1350 done using alternation, as in the example above, or by a quantifier with a
1351 minimum of zero.
1352 <a name="bigassertions"></a></P>
1353 <br><a name="SEC16" href="#TOC1">ASSERTIONS</a><br>
1354 <P>
1355 An assertion is a test on the characters following or preceding the current
1356 matching point that does not actually consume any characters. The simple
1357 assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are described
1358 <a href="#smallassertions">above.</a>
1359 </P>
1360 <P>
1361 More complicated assertions are coded as subpatterns. There are two kinds:
1362 those that look ahead of the current position in the subject string, and those
1363 that look behind it. An assertion subpattern is matched in the normal way,
1364 except that it does not cause the current matching position to be changed.
1365 </P>
1366 <P>
1367 Assertion subpatterns are not capturing subpatterns, and may not be repeated,
1368 because it makes no sense to assert the same thing several times. If any kind
1369 of assertion contains capturing subpatterns within it, these are counted for
1370 the purposes of numbering the capturing subpatterns in the whole pattern.
1371 However, substring capturing is carried out only for positive assertions,
1372 because it does not make sense for negative assertions.
1373 </P>
1374 <br><b>
1375 Lookahead assertions
1376 </b><br>
1377 <P>
1378 Lookahead assertions start with (?= for positive assertions and (?! for
1379 negative assertions. For example,
1380 <pre>
1381 \w+(?=;)
1382 </pre>
1383 matches a word followed by a semicolon, but does not include the semicolon in
1384 the match, and
1385 <pre>
1386 foo(?!bar)
1387 </pre>
1388 matches any occurrence of "foo" that is not followed by "bar". Note that the
1389 apparently similar pattern
1390 <pre>
1391 (?!foo)bar
1392 </pre>
1393 does not find an occurrence of "bar" that is preceded by something other than
1394 "foo"; it finds any occurrence of "bar" whatsoever, because the assertion
1395 (?!foo) is always true when the next three characters are "bar". A
1396 lookbehind assertion is needed to achieve the other effect.
1397 </P>
1398 <P>
1399 If you want to force a matching failure at some point in a pattern, the most
1400 convenient way to do it is with (?!) because an empty string always matches, so
1401 an assertion that requires there not to be an empty string must always fail.
1402 <a name="lookbehind"></a></P>
1403 <br><b>
1404 Lookbehind assertions
1405 </b><br>
1406 <P>
1407 Lookbehind assertions start with (?&#60;= for positive assertions and (?&#60;! for
1408 negative assertions. For example,
1409 <pre>
1410 (?&#60;!foo)bar
1411 </pre>
1412 does find an occurrence of "bar" that is not preceded by "foo". The contents of
1413 a lookbehind assertion are restricted such that all the strings it matches must
1414 have a fixed length. However, if there are several top-level alternatives, they
1415 do not all have to have the same fixed length. Thus
1416 <pre>
1417 (?&#60;=bullock|donkey)
1418 </pre>
1419 is permitted, but
1420 <pre>
1421 (?&#60;!dogs?|cats?)
1422 </pre>
1423 causes an error at compile time. Branches that match different length strings
1424 are permitted only at the top level of a lookbehind assertion. This is an
1425 extension compared with Perl (at least for 5.8), which requires all branches to
1426 match the same length of string. An assertion such as
1427 <pre>
1428 (?&#60;=ab(c|de))
1429 </pre>
1430 is not permitted, because its single top-level branch can match two different
1431 lengths, but it is acceptable if rewritten to use two top-level branches:
1432 <pre>
1433 (?&#60;=abc|abde)
1434 </pre>
1435 The implementation of lookbehind assertions is, for each alternative, to
1436 temporarily move the current position back by the fixed length and then try to
1437 match. If there are insufficient characters before the current position, the
1438 assertion fails.
1439 </P>
1440 <P>
1441 PCRE does not allow the \C escape (which matches a single byte in UTF-8 mode)
1442 to appear in lookbehind assertions, because it makes it impossible to calculate
1443 the length of the lookbehind. The \X and \R escapes, which can match
1444 different numbers of bytes, are also not permitted.
1445 </P>
1446 <P>
1447 Possessive quantifiers can be used in conjunction with lookbehind assertions to
1448 specify efficient matching at the end of the subject string. Consider a simple
1449 pattern such as
1450 <pre>
1451 abcd$
1452 </pre>
1453 when applied to a long string that does not match. Because matching proceeds
1454 from left to right, PCRE will look for each "a" in the subject and then see if
1455 what follows matches the rest of the pattern. If the pattern is specified as
1456 <pre>
1457 ^.*abcd$
1458 </pre>
1459 the initial .* matches the entire string at first, but when this fails (because
1460 there is no following "a"), it backtracks to match all but the last character,
1461 then all but the last two characters, and so on. Once again the search for "a"
1462 covers the entire string, from right to left, so we are no better off. However,
1463 if the pattern is written as
1464 <pre>
1465 ^.*+(?&#60;=abcd)
1466 </pre>
1467 there can be no backtracking for the .*+ item; it can match only the entire
1468 string. The subsequent lookbehind assertion does a single test on the last four
1469 characters. If it fails, the match fails immediately. For long strings, this
1470 approach makes a significant difference to the processing time.
1471 </P>
1472 <br><b>
1473 Using multiple assertions
1474 </b><br>
1475 <P>
1476 Several assertions (of any sort) may occur in succession. For example,
1477 <pre>
1478 (?&#60;=\d{3})(?&#60;!999)foo
1479 </pre>
1480 matches "foo" preceded by three digits that are not "999". Notice that each of
1481 the assertions is applied independently at the same point in the subject
1482 string. First there is a check that the previous three characters are all
1483 digits, and then there is a check that the same three characters are not "999".
1484 This pattern does <i>not</i> match "foo" preceded by six characters, the first
1485 of which are digits and the last three of which are not "999". For example, it
1486 doesn't match "123abcfoo". A pattern to do that is
1487 <pre>
1488 (?&#60;=\d{3}...)(?&#60;!999)foo
1489 </pre>
1490 This time the first assertion looks at the preceding six characters, checking
1491 that the first three are digits, and then the second assertion checks that the
1492 preceding three characters are not "999".
1493 </P>
1494 <P>
1495 Assertions can be nested in any combination. For example,
1496 <pre>
1497 (?&#60;=(?&#60;!foo)bar)baz
1498 </pre>
1499 matches an occurrence of "baz" that is preceded by "bar" which in turn is not
1500 preceded by "foo", while
1501 <pre>
1502 (?&#60;=\d{3}(?!999)...)foo
1503 </pre>
1504 is another pattern that matches "foo" preceded by three digits and any three
1505 characters that are not "999".
1506 <a name="conditions"></a></P>
1507 <br><a name="SEC17" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>
1508 <P>
1509 It is possible to cause the matching process to obey a subpattern
1510 conditionally or to choose between two alternative subpatterns, depending on
1511 the result of an assertion, or whether a previous capturing subpattern matched
1512 or not. The two possible forms of conditional subpattern are
1513 <pre>
1514 (?(condition)yes-pattern)
1515 (?(condition)yes-pattern|no-pattern)
1516 </pre>
1517 If the condition is satisfied, the yes-pattern is used; otherwise the
1518 no-pattern (if present) is used. If there are more than two alternatives in the
1519 subpattern, a compile-time error occurs.
1520 </P>
1521 <P>
1522 There are four kinds of condition: references to subpatterns, references to
1523 recursion, a pseudo-condition called DEFINE, and assertions.
1524 </P>
1525 <br><b>
1526 Checking for a used subpattern by number
1527 </b><br>
1528 <P>
1529 If the text between the parentheses consists of a sequence of digits, the
1530 condition is true if the capturing subpattern of that number has previously
1531 matched.
1532 </P>
1533 <P>
1534 Consider the following pattern, which contains non-significant white space to
1535 make it more readable (assume the PCRE_EXTENDED option) and to divide it into
1536 three parts for ease of discussion:
1537 <pre>
1538 ( \( )? [^()]+ (?(1) \) )
1539 </pre>
1540 The first part matches an optional opening parenthesis, and if that
1541 character is present, sets it as the first captured substring. The second part
1542 matches one or more characters that are not parentheses. The third part is a
1543 conditional subpattern that tests whether the first set of parentheses matched
1544 or not. If they did, that is, if subject started with an opening parenthesis,
1545 the condition is true, and so the yes-pattern is executed and a closing
1546 parenthesis is required. Otherwise, since no-pattern is not present, the
1547 subpattern matches nothing. In other words, this pattern matches a sequence of
1548 non-parentheses, optionally enclosed in parentheses.
1549 </P>
1550 <br><b>
1551 Checking for a used subpattern by name
1552 </b><br>
1553 <P>
1554 Perl uses the syntax (?(&#60;name&#62;)...) or (?('name')...) to test for a used
1555 subpattern by name. For compatibility with earlier versions of PCRE, which had
1556 this facility before Perl, the syntax (?(name)...) is also recognized. However,
1557 there is a possible ambiguity with this syntax, because subpattern names may
1558 consist entirely of digits. PCRE looks first for a named subpattern; if it
1559 cannot find one and the name consists entirely of digits, PCRE looks for a
1560 subpattern of that number, which must be greater than zero. Using subpattern
1561 names that consist entirely of digits is not recommended.
1562 </P>
1563 <P>
1564 Rewriting the above example to use a named subpattern gives this:
1565 <pre>
1566 (?&#60;OPEN&#62; \( )? [^()]+ (?(&#60;OPEN&#62;) \) )
1567
1568 </PRE>
1569 </P>
1570 <br><b>
1571 Checking for pattern recursion
1572 </b><br>
1573 <P>
1574 If the condition is the string (R), and there is no subpattern with the name R,
1575 the condition is true if a recursive call to the whole pattern or any
1576 subpattern has been made. If digits or a name preceded by ampersand follow the
1577 letter R, for example:
1578 <pre>
1579 (?(R3)...) or (?(R&name)...)
1580 </pre>
1581 the condition is true if the most recent recursion is into the subpattern whose
1582 number or name is given. This condition does not check the entire recursion
1583 stack.
1584 </P>
1585 <P>
1586 At "top level", all these recursion test conditions are false. Recursive
1587 patterns are described below.
1588 </P>
1589 <br><b>
1590 Defining subpatterns for use by reference only
1591 </b><br>
1592 <P>
1593 If the condition is the string (DEFINE), and there is no subpattern with the
1594 name DEFINE, the condition is always false. In this case, there may be only one
1595 alternative in the subpattern. It is always skipped if control reaches this
1596 point in the pattern; the idea of DEFINE is that it can be used to define
1597 "subroutines" that can be referenced from elsewhere. (The use of "subroutines"
1598 is described below.) For example, a pattern to match an IPv4 address could be
1599 written like this (ignore whitespace and line breaks):
1600 <pre>
1601 (?(DEFINE) (?&#60;byte&#62; 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
1602 \b (?&byte) (\.(?&byte)){3} \b
1603 </pre>
1604 The first part of the pattern is a DEFINE group inside which a another group
1605 named "byte" is defined. This matches an individual component of an IPv4
1606 address (a number less than 256). When matching takes place, this part of the
1607 pattern is skipped because DEFINE acts like a false condition.
1608 </P>
1609 <P>
1610 The rest of the pattern uses references to the named group to match the four
1611 dot-separated components of an IPv4 address, insisting on a word boundary at
1612 each end.
1613 </P>
1614 <br><b>
1615 Assertion conditions
1616 </b><br>
1617 <P>
1618 If the condition is not in any of the above formats, it must be an assertion.
1619 This may be a positive or negative lookahead or lookbehind assertion. Consider
1620 this pattern, again containing non-significant white space, and with the two
1621 alternatives on the second line:
1622 <pre>
1623 (?(?=[^a-z]*[a-z])
1624 \d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} )
1625 </pre>
1626 The condition is a positive lookahead assertion that matches an optional
1627 sequence of non-letters followed by a letter. In other words, it tests for the
1628 presence of at least one letter in the subject. If a letter is found, the
1629 subject is matched against the first alternative; otherwise it is matched
1630 against the second. This pattern matches strings in one of the two forms
1631 dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.
1632 <a name="comments"></a></P>
1633 <br><a name="SEC18" href="#TOC1">COMMENTS</a><br>
1634 <P>
1635 The sequence (?# marks the start of a comment that continues up to the next
1636 closing parenthesis. Nested parentheses are not permitted. The characters
1637 that make up a comment play no part in the pattern matching at all.
1638 </P>
1639 <P>
1640 If the PCRE_EXTENDED option is set, an unescaped # character outside a
1641 character class introduces a comment that continues to immediately after the
1642 next newline in the pattern.
1643 <a name="recursion"></a></P>
1644 <br><a name="SEC19" href="#TOC1">RECURSIVE PATTERNS</a><br>
1645 <P>
1646 Consider the problem of matching a string in parentheses, allowing for
1647 unlimited nested parentheses. Without the use of recursion, the best that can
1648 be done is to use a pattern that matches up to some fixed depth of nesting. It
1649 is not possible to handle an arbitrary nesting depth.
1650 </P>
1651 <P>
1652 For some time, Perl has provided a facility that allows regular expressions to
1653 recurse (amongst other things). It does this by interpolating Perl code in the
1654 expression at run time, and the code can refer to the expression itself. A Perl
1655 pattern using code interpolation to solve the parentheses problem can be
1656 created like this:
1657 <pre>
1658 $re = qr{\( (?: (?&#62;[^()]+) | (?p{$re}) )* \)}x;
1659 </pre>
1660 The (?p{...}) item interpolates Perl code at run time, and in this case refers
1661 recursively to the pattern in which it appears.
1662 </P>
1663 <P>
1664 Obviously, PCRE cannot support the interpolation of Perl code. Instead, it
1665 supports special syntax for recursion of the entire pattern, and also for
1666 individual subpattern recursion. After its introduction in PCRE and Python,
1667 this kind of recursion was introduced into Perl at release 5.10.
1668 </P>
1669 <P>
1670 A special item that consists of (? followed by a number greater than zero and a
1671 closing parenthesis is a recursive call of the subpattern of the given number,
1672 provided that it occurs inside that subpattern. (If not, it is a "subroutine"
1673 call, which is described in the next section.) The special item (?R) or (?0) is
1674 a recursive call of the entire regular expression.
1675 </P>
1676 <P>
1677 In PCRE (like Python, but unlike Perl), a recursive subpattern call is always
1678 treated as an atomic group. That is, once it has matched some of the subject
1679 string, it is never re-entered, even if it contains untried alternatives and
1680 there is a subsequent matching failure.
1681 </P>
1682 <P>
1683 This PCRE pattern solves the nested parentheses problem (assume the
1684 PCRE_EXTENDED option is set so that white space is ignored):
1685 <pre>
1686 \( ( (?&#62;[^()]+) | (?R) )* \)
1687 </pre>
1688 First it matches an opening parenthesis. Then it matches any number of
1689 substrings which can either be a sequence of non-parentheses, or a recursive
1690 match of the pattern itself (that is, a correctly parenthesized substring).
1691 Finally there is a closing parenthesis.
1692 </P>
1693 <P>
1694 If this were part of a larger pattern, you would not want to recurse the entire
1695 pattern, so instead you could use this:
1696 <pre>
1697 ( \( ( (?&#62;[^()]+) | (?1) )* \) )
1698 </pre>
1699 We have put the pattern into parentheses, and caused the recursion to refer to
1700 them instead of the whole pattern. In a larger pattern, keeping track of
1701 parenthesis numbers can be tricky. It may be more convenient to use named
1702 parentheses instead. The Perl syntax for this is (?&name); PCRE's earlier
1703 syntax (?P&#62;name) is also supported. We could rewrite the above example as
1704 follows:
1705 <pre>
1706 (?&#60;pn&#62; \( ( (?&#62;[^()]+) | (?&pn) )* \) )
1707 </pre>
1708 If there is more than one subpattern with the same name, the earliest one is
1709 used. This particular example pattern contains nested unlimited repeats, and so
1710 the use of atomic grouping for matching strings of non-parentheses is important
1711 when applying the pattern to strings that do not match. For example, when this
1712 pattern is applied to
1713 <pre>
1714 (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
1715 </pre>
1716 it yields "no match" quickly. However, if atomic grouping is not used,
1717 the match runs for a very long time indeed because there are so many different
1718 ways the + and * repeats can carve up the subject, and all have to be tested
1719 before failure can be reported.
1720 </P>
1721 <P>
1722 At the end of a match, the values set for any capturing subpatterns are those
1723 from the outermost level of the recursion at which the subpattern value is set.
1724 If you want to obtain intermediate values, a callout function can be used (see
1725 below and the
1726 <a href="pcrecallout.html"><b>pcrecallout</b></a>
1727 documentation). If the pattern above is matched against
1728 <pre>
1729 (ab(cd)ef)
1730 </pre>
1731 the value for the capturing parentheses is "ef", which is the last value taken
1732 on at the top level. If additional parentheses are added, giving
1733 <pre>
1734 \( ( ( (?&#62;[^()]+) | (?R) )* ) \)
1735 ^ ^
1736 ^ ^
1737 </pre>
1738 the string they capture is "ab(cd)ef", the contents of the top level
1739 parentheses. If there are more than 15 capturing parentheses in a pattern, PCRE
1740 has to obtain extra memory to store data during a recursion, which it does by
1741 using <b>pcre_malloc</b>, freeing it via <b>pcre_free</b> afterwards. If no
1742 memory can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.
1743 </P>
1744 <P>
1745 Do not confuse the (?R) item with the condition (R), which tests for recursion.
1746 Consider this pattern, which matches text in angle brackets, allowing for
1747 arbitrary nesting. Only digits are allowed in nested brackets (that is, when
1748 recursing), whereas any characters are permitted at the outer level.
1749 <pre>
1750 &#60; (?: (?(R) \d++ | [^&#60;&#62;]*+) | (?R)) * &#62;
1751 </pre>
1752 In this pattern, (?(R) is the start of a conditional subpattern, with two
1753 different alternatives for the recursive and non-recursive cases. The (?R) item
1754 is the actual recursive call.
1755 <a name="subpatternsassubroutines"></a></P>
1756 <br><a name="SEC20" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>
1757 <P>
1758 If the syntax for a recursive subpattern reference (either by number or by
1759 name) is used outside the parentheses to which it refers, it operates like a
1760 subroutine in a programming language. The "called" subpattern may be defined
1761 before or after the reference. An earlier example pointed out that the pattern
1762 <pre>
1763 (sens|respons)e and \1ibility
1764 </pre>
1765 matches "sense and sensibility" and "response and responsibility", but not
1766 "sense and responsibility". If instead the pattern
1767 <pre>
1768 (sens|respons)e and (?1)ibility
1769 </pre>
1770 is used, it does match "sense and responsibility" as well as the other two
1771 strings. Another example is given in the discussion of DEFINE above.
1772 </P>
1773 <P>
1774 Like recursive subpatterns, a "subroutine" call is always treated as an atomic
1775 group. That is, once it has matched some of the subject string, it is never
1776 re-entered, even if it contains untried alternatives and there is a subsequent
1777 matching failure.
1778 </P>
1779 <P>
1780 When a subpattern is used as a subroutine, processing options such as
1781 case-independence are fixed when the subpattern is defined. They cannot be
1782 changed for different calls. For example, consider this pattern:
1783 <pre>
1784 (abc)(?i:(?1))
1785 </pre>
1786 It matches "abcabc". It does not match "abcABC" because the change of
1787 processing option does not affect the called subpattern.
1788 </P>
1789 <br><a name="SEC21" href="#TOC1">CALLOUTS</a><br>
1790 <P>
1791 Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl
1792 code to be obeyed in the middle of matching a regular expression. This makes it
1793 possible, amongst other things, to extract different substrings that match the
1794 same pair of parentheses when there is a repetition.
1795 </P>
1796 <P>
1797 PCRE provides a similar feature, but of course it cannot obey arbitrary Perl
1798 code. The feature is called "callout". The caller of PCRE provides an external
1799 function by putting its entry point in the global variable <i>pcre_callout</i>.
1800 By default, this variable contains NULL, which disables all calling out.
1801 </P>
1802 <P>
1803 Within a regular expression, (?C) indicates the points at which the external
1804 function is to be called. If you want to identify different callout points, you
1805 can put a number less than 256 after the letter C. The default value is zero.
1806 For example, this pattern has two callout points:
1807 <pre>
1808 (?C1)\dabc(?C2)def
1809 </pre>
1810 If the PCRE_AUTO_CALLOUT flag is passed to <b>pcre_compile()</b>, callouts are
1811 automatically installed before each item in the pattern. They are all numbered
1812 255.
1813 </P>
1814 <P>
1815 During matching, when PCRE reaches a callout point (and <i>pcre_callout</i> is
1816 set), the external function is called. It is provided with the number of the
1817 callout, the position in the pattern, and, optionally, one item of data
1818 originally supplied by the caller of <b>pcre_exec()</b>. The callout function
1819 may cause matching to proceed, to backtrack, or to fail altogether. A complete
1820 description of the interface to the callout function is given in the
1821 <a href="pcrecallout.html"><b>pcrecallout</b></a>
1822 documentation.
1823 </P>
1824 <br><a name="SEC22" href="#TOC1">SEE ALSO</a><br>
1825 <P>
1826 <b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3), <b>pcre</b>(3).
1827 </P>
1828 <br><a name="SEC23" href="#TOC1">AUTHOR</a><br>
1829 <P>
1830 Philip Hazel
1831 <br>
1832 University Computing Service
1833 <br>
1834 Cambridge CB2 3QH, England.
1835 <br>
1836 </P>
1837 <br><a name="SEC24" href="#TOC1">REVISION</a><br>
1838 <P>
1839 Last updated: 06 March 2007
1840 <br>
1841 Copyright &copy; 1997-2007 University of Cambridge.
1842 <br>
1843 <p>
1844 Return to the <a href="index.html">PCRE index page</a>.
1845 </p>

Properties

Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

  ViewVC Help
Powered by ViewVC 1.1.5