ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

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


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

  ViewVC Help
Powered by ViewVC 1.1.5