/[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 93 - (hide annotations) (download) (as text)
Sat Feb 24 21:41:42 2007 UTC (7 years, 6 months ago) by nigel
File MIME type: text/html
File size: 77657 byte(s)
Load pcre-7.0 into code/trunk.

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12