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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 211 by ph10, Thu Aug 9 09:52:43 2007 UTC revision 567 by ph10, Sat Nov 6 17:10:00 2010 UTC
# Line 14  man page, in case the conversion went wr Line 14  man page, in case the conversion went wr
14  <br>  <br>
15  <ul>  <ul>
16  <li><a name="TOC1" href="#SEC1">PCRE REGULAR EXPRESSION DETAILS</a>  <li><a name="TOC1" href="#SEC1">PCRE REGULAR EXPRESSION DETAILS</a>
17  <li><a name="TOC2" href="#SEC2">CHARACTERS AND METACHARACTERS</a>  <li><a name="TOC2" href="#SEC2">NEWLINE CONVENTIONS</a>
18  <li><a name="TOC3" href="#SEC3">BACKSLASH</a>  <li><a name="TOC3" href="#SEC3">CHARACTERS AND METACHARACTERS</a>
19  <li><a name="TOC4" href="#SEC4">CIRCUMFLEX AND DOLLAR</a>  <li><a name="TOC4" href="#SEC4">BACKSLASH</a>
20  <li><a name="TOC5" href="#SEC5">FULL STOP (PERIOD, DOT)</a>  <li><a name="TOC5" href="#SEC5">CIRCUMFLEX AND DOLLAR</a>
21  <li><a name="TOC6" href="#SEC6">MATCHING A SINGLE BYTE</a>  <li><a name="TOC6" href="#SEC6">FULL STOP (PERIOD, DOT) AND \N</a>
22  <li><a name="TOC7" href="#SEC7">SQUARE BRACKETS AND CHARACTER CLASSES</a>  <li><a name="TOC7" href="#SEC7">MATCHING A SINGLE BYTE</a>
23  <li><a name="TOC8" href="#SEC8">POSIX CHARACTER CLASSES</a>  <li><a name="TOC8" href="#SEC8">SQUARE BRACKETS AND CHARACTER CLASSES</a>
24  <li><a name="TOC9" href="#SEC9">VERTICAL BAR</a>  <li><a name="TOC9" href="#SEC9">POSIX CHARACTER CLASSES</a>
25  <li><a name="TOC10" href="#SEC10">INTERNAL OPTION SETTING</a>  <li><a name="TOC10" href="#SEC10">VERTICAL BAR</a>
26  <li><a name="TOC11" href="#SEC11">SUBPATTERNS</a>  <li><a name="TOC11" href="#SEC11">INTERNAL OPTION SETTING</a>
27  <li><a name="TOC12" href="#SEC12">DUPLICATE SUBPATTERN NUMBERS</a>  <li><a name="TOC12" href="#SEC12">SUBPATTERNS</a>
28  <li><a name="TOC13" href="#SEC13">NAMED SUBPATTERNS</a>  <li><a name="TOC13" href="#SEC13">DUPLICATE SUBPATTERN NUMBERS</a>
29  <li><a name="TOC14" href="#SEC14">REPETITION</a>  <li><a name="TOC14" href="#SEC14">NAMED SUBPATTERNS</a>
30  <li><a name="TOC15" href="#SEC15">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a>  <li><a name="TOC15" href="#SEC15">REPETITION</a>
31  <li><a name="TOC16" href="#SEC16">BACK REFERENCES</a>  <li><a name="TOC16" href="#SEC16">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a>
32  <li><a name="TOC17" href="#SEC17">ASSERTIONS</a>  <li><a name="TOC17" href="#SEC17">BACK REFERENCES</a>
33  <li><a name="TOC18" href="#SEC18">CONDITIONAL SUBPATTERNS</a>  <li><a name="TOC18" href="#SEC18">ASSERTIONS</a>
34  <li><a name="TOC19" href="#SEC19">COMMENTS</a>  <li><a name="TOC19" href="#SEC19">CONDITIONAL SUBPATTERNS</a>
35  <li><a name="TOC20" href="#SEC20">RECURSIVE PATTERNS</a>  <li><a name="TOC20" href="#SEC20">COMMENTS</a>
36  <li><a name="TOC21" href="#SEC21">SUBPATTERNS AS SUBROUTINES</a>  <li><a name="TOC21" href="#SEC21">RECURSIVE PATTERNS</a>
37  <li><a name="TOC22" href="#SEC22">CALLOUTS</a>  <li><a name="TOC22" href="#SEC22">SUBPATTERNS AS SUBROUTINES</a>
38  <li><a name="TOC23" href="#SEC23">BACTRACKING CONTROL</a>  <li><a name="TOC23" href="#SEC23">ONIGURUMA SUBROUTINE SYNTAX</a>
39  <li><a name="TOC24" href="#SEC24">SEE ALSO</a>  <li><a name="TOC24" href="#SEC24">CALLOUTS</a>
40  <li><a name="TOC25" href="#SEC25">AUTHOR</a>  <li><a name="TOC25" href="#SEC25">BACKTRACKING CONTROL</a>
41  <li><a name="TOC26" href="#SEC26">REVISION</a>  <li><a name="TOC26" href="#SEC26">SEE ALSO</a>
42    <li><a name="TOC27" href="#SEC27">AUTHOR</a>
43    <li><a name="TOC28" href="#SEC28">REVISION</a>
44  </ul>  </ul>
45  <br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br>  <br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br>
46  <P>  <P>
47  The syntax and semantics of the regular expressions that are supported by PCRE  The syntax and semantics of the regular expressions that are supported by PCRE
48  are described in detail below. There is a quick-reference syntax summary in the  are described in detail below. There is a quick-reference syntax summary in the
49  <a href="pcresyntax.html"><b>pcresyntax</b></a>  <a href="pcresyntax.html"><b>pcresyntax</b></a>
50  page. Perl's regular expressions are described in its own documentation, and  page. PCRE tries to match Perl syntax and semantics as closely as it can. PCRE
51    also supports some alternative regular expression syntax (which does not
52    conflict with the Perl syntax) in order to provide some compatibility with
53    regular expressions in Python, .NET, and Oniguruma.
54    </P>
55    <P>
56    Perl's regular expressions are described in its own documentation, and
57  regular expressions in general are covered in a number of books, some of which  regular expressions in general are covered in a number of books, some of which
58  have copious examples. Jeffrey Friedl's "Mastering Regular Expressions",  have copious examples. Jeffrey Friedl's "Mastering Regular Expressions",
59  published by O'Reilly, covers regular expressions in great detail. This  published by O'Reilly, covers regular expressions in great detail. This
# Line 53  description of PCRE's regular expression Line 61  description of PCRE's regular expression
61  </P>  </P>
62  <P>  <P>
63  The original operation of PCRE was on strings of one-byte characters. However,  The original operation of PCRE was on strings of one-byte characters. However,
64  there is now also support for UTF-8 character strings. To use this, you must  there is now also support for UTF-8 character strings. To use this,
65  build PCRE to include UTF-8 support, and then call <b>pcre_compile()</b> with  PCRE must be built to include UTF-8 support, and you must call
66  the PCRE_UTF8 option. How this affects pattern matching is mentioned in several  <b>pcre_compile()</b> or <b>pcre_compile2()</b> with the PCRE_UTF8 option. There
67  places below. There is also a summary of UTF-8 features in the  is also a special sequence that can be given at the start of a pattern:
68    <pre>
69      (*UTF8)
70    </pre>
71    Starting a pattern with this sequence is equivalent to setting the PCRE_UTF8
72    option. This feature is not Perl-compatible. How setting UTF-8 mode affects
73    pattern matching is mentioned in several places below. There is also a summary
74    of UTF-8 features in the
75  <a href="pcre.html#utf8support">section on UTF-8 support</a>  <a href="pcre.html#utf8support">section on UTF-8 support</a>
76  in the main  in the main
77  <a href="pcre.html"><b>pcre</b></a>  <a href="pcre.html"><b>pcre</b></a>
78  page.  page.
79  </P>  </P>
80  <P>  <P>
81    Another special sequence that may appear at the start of a pattern or in
82    combination with (*UTF8) is:
83    <pre>
84      (*UCP)
85    </pre>
86    This has the same effect as setting the PCRE_UCP option: it causes sequences
87    such as \d and \w to use Unicode properties to determine character types,
88    instead of recognizing only characters with codes less than 128 via a lookup
89    table.
90    </P>
91    <P>
92  The remainder of this document discusses the patterns that are supported by  The remainder of this document discusses the patterns that are supported by
93  PCRE when its main matching function, <b>pcre_exec()</b>, is used.  PCRE when its main matching function, <b>pcre_exec()</b>, is used.
94  From release 6.0, PCRE offers a second matching function,  From release 6.0, PCRE offers a second matching function,
# Line 73  alternative function, and how it differs Line 99  alternative function, and how it differs
99  discussed in the  discussed in the
100  <a href="pcrematching.html"><b>pcrematching</b></a>  <a href="pcrematching.html"><b>pcrematching</b></a>
101  page.  page.
102    <a name="newlines"></a></P>
103    <br><a name="SEC2" href="#TOC1">NEWLINE CONVENTIONS</a><br>
104    <P>
105    PCRE supports five different conventions for indicating line breaks in
106    strings: a single CR (carriage return) character, a single LF (linefeed)
107    character, the two-character sequence CRLF, any of the three preceding, or any
108    Unicode newline sequence. The
109    <a href="pcreapi.html"><b>pcreapi</b></a>
110    page has
111    <a href="pcreapi.html#newlines">further discussion</a>
112    about newlines, and shows how to set the newline convention in the
113    <i>options</i> arguments for the compiling and matching functions.
114    </P>
115    <P>
116    It is also possible to specify a newline convention by starting a pattern
117    string with one of the following five sequences:
118    <pre>
119      (*CR)        carriage return
120      (*LF)        linefeed
121      (*CRLF)      carriage return, followed by linefeed
122      (*ANYCRLF)   any of the three above
123      (*ANY)       all Unicode newline sequences
124    </pre>
125    These override the default and the options given to <b>pcre_compile()</b> or
126    <b>pcre_compile2()</b>. For example, on a Unix system where LF is the default
127    newline sequence, the pattern
128    <pre>
129      (*CR)a.b
130    </pre>
131    changes the convention to CR. That pattern matches "a\nb" because LF is no
132    longer a newline. Note that these special settings, which are not
133    Perl-compatible, are recognized only at the very start of a pattern, and that
134    they must be in upper case. If more than one of them is present, the last one
135    is used.
136    </P>
137    <P>
138    The newline convention affects the interpretation of the dot metacharacter when
139    PCRE_DOTALL is not set, and also the behaviour of \N. However, it does not
140    affect what the \R escape sequence matches. By default, this is any Unicode
141    newline sequence, for Perl compatibility. However, this can be changed; see the
142    description of \R in the section entitled
143    <a href="#newlineseq">"Newline sequences"</a>
144    below. A change of \R setting can be combined with a change of newline
145    convention.
146  </P>  </P>
147  <br><a name="SEC2" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br>  <br><a name="SEC3" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br>
148  <P>  <P>
149  A regular expression is a pattern that is matched against a subject string from  A regular expression is a pattern that is matched against a subject string from
150  left to right. Most characters stand for themselves in a pattern, and match the  left to right. Most characters stand for themselves in a pattern, and match the
# Line 131  a character class the only metacharacter Line 201  a character class the only metacharacter
201  </pre>  </pre>
202  The following sections describe the use of each of the metacharacters.  The following sections describe the use of each of the metacharacters.
203  </P>  </P>
204  <br><a name="SEC3" href="#TOC1">BACKSLASH</a><br>  <br><a name="SEC4" href="#TOC1">BACKSLASH</a><br>
205  <P>  <P>
206  The backslash character has several uses. Firstly, if it is followed by a  The backslash character has several uses. Firstly, if it is followed by a
207  non-alphanumeric character, it takes away any special meaning that character  non-alphanumeric character, it takes away any special meaning that character
# Line 164  Perl, $ and @ cause variable interpolati Line 234  Perl, $ and @ cause variable interpolati
234    \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz    \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz
235  </pre>  </pre>
236  The \Q...\E sequence is recognized both inside and outside character classes.  The \Q...\E sequence is recognized both inside and outside character classes.
237    An isolated \E that is not preceded by \Q is ignored.
238  <a name="digitsafterbackslash"></a></P>  <a name="digitsafterbackslash"></a></P>
239  <br><b>  <br><b>
240  Non-printing characters  Non-printing characters
# Line 172  Non-printing characters Line 243  Non-printing characters
243  A second use of backslash provides a way of encoding non-printing characters  A second use of backslash provides a way of encoding non-printing characters
244  in patterns in a visible manner. There is no restriction on the appearance of  in patterns in a visible manner. There is no restriction on the appearance of
245  non-printing characters, apart from the binary zero that terminates a pattern,  non-printing characters, apart from the binary zero that terminates a pattern,
246  but when a pattern is being prepared by text editing, it is usually easier to  but when a pattern is being prepared by text editing, it is often easier to use
247  use one of the following escape sequences than the binary character it  one of the following escape sequences than the binary character it represents:
 represents:  
248  <pre>  <pre>
249    \a        alarm, that is, the BEL character (hex 07)    \a        alarm, that is, the BEL character (hex 07)
250    \cx       "control-x", where x is any character    \cx       "control-x", where x is any character
251    \e        escape (hex 1B)    \e        escape (hex 1B)
252    \f        formfeed (hex 0C)    \f        formfeed (hex 0C)
253    \n        newline (hex 0A)    \n        linefeed (hex 0A)
254    \r        carriage return (hex 0D)    \r        carriage return (hex 0D)
255    \t        tab (hex 09)    \t        tab (hex 09)
256    \ddd      character with octal code ddd, or backreference    \ddd      character with octal code ddd, or back reference
257    \xhh      character with hex code hh    \xhh      character with hex code hh
258    \x{hhh..} character with hex code hhh..    \x{hhh..} character with hex code hhh..
259  </pre>  </pre>
# Line 252  zero, because no more than three octal d Line 322  zero, because no more than three octal d
322  <P>  <P>
323  All the sequences that define a single character value can be used both inside  All the sequences that define a single character value can be used both inside
324  and outside character classes. In addition, inside a character class, the  and outside character classes. In addition, inside a character class, the
325  sequence \b is interpreted as the backspace character (hex 08), and the  sequence \b is interpreted as the backspace character (hex 08). The sequences
326  sequences \R and \X are interpreted as the characters "R" and "X",  \B, \N, \R, and \X are not special inside a character class. Like any other
327  respectively. Outside a character class, these sequences have different  unrecognized escape sequences, they are treated as the literal characters "B",
328  meanings  "N", "R", and "X" by default, but cause an error if the PCRE_EXTRA option is
329  <a href="#uniextseq">(see below).</a>  set. Outside a character class, these sequences have different meanings.
330  </P>  </P>
331  <br><b>  <br><b>
332  Absolute and relative back references  Absolute and relative back references
# Line 270  following the discussion of Line 340  following the discussion of
340  <a href="#subpattern">parenthesized subpatterns.</a>  <a href="#subpattern">parenthesized subpatterns.</a>
341  </P>  </P>
342  <br><b>  <br><b>
343    Absolute and relative subroutine calls
344    </b><br>
345    <P>
346    For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
347    a number enclosed either in angle brackets or single quotes, is an alternative
348    syntax for referencing a subpattern as a "subroutine". Details are discussed
349    <a href="#onigurumasubroutines">later.</a>
350    Note that \g{...} (Perl syntax) and \g&#60;...&#62; (Oniguruma syntax) are <i>not</i>
351    synonymous. The former is a back reference; the latter is a
352    <a href="#subpatternsassubroutines">subroutine</a>
353    call.
354    <a name="genericchartypes"></a></P>
355    <br><b>
356  Generic character types  Generic character types
357  </b><br>  </b><br>
358  <P>  <P>
359  Another use of backslash is for specifying generic character types. The  Another use of backslash is for specifying generic character types:
 following are always recognized:  
360  <pre>  <pre>
361    \d     any decimal digit    \d     any decimal digit
362    \D     any character that is not a decimal digit    \D     any character that is not a decimal digit
# Line 287  following are always recognized: Line 369  following are always recognized:
369    \w     any "word" character    \w     any "word" character
370    \W     any "non-word" character    \W     any "non-word" character
371  </pre>  </pre>
372  Each pair of escape sequences partitions the complete set of characters into  There is also the single sequence \N, which matches a non-newline character.
373  two disjoint sets. Any given character matches one, and only one, of each pair.  This is the same as
374    <a href="#fullstopdot">the "." metacharacter</a>
375    when PCRE_DOTALL is not set.
376  </P>  </P>
377  <P>  <P>
378  These character type sequences can appear both inside and outside character  Each pair of lower and upper case escape sequences partitions the complete set
379    of characters into two disjoint sets. Any given character matches one, and only
380    one, of each pair. The sequences can appear both inside and outside character
381  classes. They each match one character of the appropriate type. If the current  classes. They each match one character of the appropriate type. If the current
382  matching point is at the end of the subject string, all of them fail, since  matching point is at the end of the subject string, all of them fail, because
383  there is no character to match.  there is no character to match.
384  </P>  </P>
385  <P>  <P>
# Line 304  included in a Perl script, \s may match Line 390  included in a Perl script, \s may match
390  does.  does.
391  </P>  </P>
392  <P>  <P>
393  In UTF-8 mode, characters with values greater than 128 never match \d, \s, or  A "word" character is an underscore or any character that is a letter or digit.
394  \w, and always match \D, \S, and \W. This is true even when Unicode  By default, the definition of letters and digits is controlled by PCRE's
395  character property support is available. These sequences retain their original  low-valued character tables, and may vary if locale-specific matching is taking
396  meanings from before UTF-8 support was available, mainly for efficiency  place (see
397  reasons.  <a href="pcreapi.html#localesupport">"Locale support"</a>
398    in the
399    <a href="pcreapi.html"><b>pcreapi</b></a>
400    page). For example, in a French locale such as "fr_FR" in Unix-like systems,
401    or "french" in Windows, some character codes greater than 128 are used for
402    accented letters, and these are then matched by \w. The use of locales with
403    Unicode is discouraged.
404    </P>
405    <P>
406    By default, in UTF-8 mode, characters with values greater than 128 never match
407    \d, \s, or \w, and always match \D, \S, and \W. These sequences retain
408    their original meanings from before UTF-8 support was available, mainly for
409    efficiency reasons. However, if PCRE is compiled with Unicode property support,
410    and the PCRE_UCP option is set, the behaviour is changed so that Unicode
411    properties are used to determine character types, as follows:
412    <pre>
413      \d  any character that \p{Nd} matches (decimal digit)
414      \s  any character that \p{Z} matches, plus HT, LF, FF, CR
415      \w  any character that \p{L} or \p{N} matches, plus underscore
416    </pre>
417    The upper case escapes match the inverse sets of characters. Note that \d
418    matches only decimal digits, whereas \w matches any Unicode digit, as well as
419    any Unicode letter, and underscore. Note also that PCRE_UCP affects \b, and
420    \B because they are defined in terms of \w and \W. Matching these sequences
421    is noticeably slower when PCRE_UCP is set.
422  </P>  </P>
423  <P>  <P>
424  The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to the  The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to the
425  other sequences, these do match certain high-valued codepoints in UTF-8 mode.  other sequences, which match only ASCII characters by default, these always
426  The horizontal space characters are:  match certain high-valued codepoints in UTF-8 mode, whether or not PCRE_UCP is
427    set. The horizontal space characters are:
428  <pre>  <pre>
429    U+0009     Horizontal tab    U+0009     Horizontal tab
430    U+0020     Space    U+0020     Space
# Line 344  The vertical space characters are: Line 455  The vertical space characters are:
455    U+0085     Next line    U+0085     Next line
456    U+2028     Line separator    U+2028     Line separator
457    U+2029     Paragraph separator    U+2029     Paragraph separator
458  </PRE>  <a name="newlineseq"></a></PRE>
 </P>  
 <P>  
 A "word" character is an underscore or any character less than 256 that is a  
 letter or digit. The definition of letters and digits is controlled by PCRE's  
 low-valued character tables, and may vary if locale-specific matching is taking  
 place (see  
 <a href="pcreapi.html#localesupport">"Locale support"</a>  
 in the  
 <a href="pcreapi.html"><b>pcreapi</b></a>  
 page). For example, in a French locale such as "fr_FR" in Unix-like systems,  
 or "french" in Windows, some character codes greater than 128 are used for  
 accented letters, and these are matched by \w. The use of locales with Unicode  
 is discouraged.  
459  </P>  </P>
460  <br><b>  <br><b>
461  Newline sequences  Newline sequences
462  </b><br>  </b><br>
463  <P>  <P>
464  Outside a character class, the escape sequence \R matches any Unicode newline  Outside a character class, by default, the escape sequence \R matches any
465  sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \R is equivalent to  Unicode newline sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \R is
466  the following:  equivalent to the following:
467  <pre>  <pre>
468    (?&#62;\r\n|\n|\x0b|\f|\r|\x85)    (?&#62;\r\n|\n|\x0b|\f|\r|\x85)
469  </pre>  </pre>
# Line 384  Unicode character property support is no Line 482  Unicode character property support is no
482  recognized.  recognized.
483  </P>  </P>
484  <P>  <P>
485  Inside a character class, \R matches the letter "R".  It is possible to restrict \R to match only CR, LF, or CRLF (instead of the
486    complete set of Unicode line endings) by setting the option PCRE_BSR_ANYCRLF
487    either at compile time or when the pattern is matched. (BSR is an abbrevation
488    for "backslash R".) This can be made the default when PCRE is built; if this is
489    the case, the other behaviour can be requested via the PCRE_BSR_UNICODE option.
490    It is also possible to specify these settings by starting a pattern string with
491    one of the following sequences:
492    <pre>
493      (*BSR_ANYCRLF)   CR, LF, or CRLF only
494      (*BSR_UNICODE)   any Unicode newline sequence
495    </pre>
496    These override the default and the options given to <b>pcre_compile()</b> or
497    <b>pcre_compile2()</b>, but they can be overridden by options given to
498    <b>pcre_exec()</b> or <b>pcre_dfa_exec()</b>. Note that these special settings,
499    which are not Perl-compatible, are recognized only at the very start of a
500    pattern, and that they must be in upper case. If more than one of them is
501    present, the last one is used. They can be combined with a change of newline
502    convention; for example, a pattern can start with:
503    <pre>
504      (*ANY)(*BSR_ANYCRLF)
505    </pre>
506    They can also be combined with the (*UTF8) or (*UCP) special sequences. Inside
507    a character class, \R is treated as an unrecognized escape sequence, and so
508    matches the letter "R" by default, but causes an error if PCRE_EXTRA is set.
509  <a name="uniextseq"></a></P>  <a name="uniextseq"></a></P>
510  <br><b>  <br><b>
511  Unicode character properties  Unicode character properties
# Line 401  The extra escape sequences are: Line 522  The extra escape sequences are:
522    \X       an extended Unicode sequence    \X       an extended Unicode sequence
523  </pre>  </pre>
524  The property names represented by <i>xx</i> above are limited to the Unicode  The property names represented by <i>xx</i> above are limited to the Unicode
525  script names, the general category properties, and "Any", which matches any  script names, the general category properties, "Any", which matches any
526  character (including newline). Other properties such as "InMusicalSymbols" are  character (including newline), and some special PCRE properties (described
527  not currently supported by PCRE. Note that \P{Any} does not match any  in the
528  characters, so always causes a match failure.  <a href="#extraprops">next section).</a>
529    Other Perl properties such as "InMusicalSymbols" are not currently supported by
530    PCRE. Note that \P{Any} does not match any characters, so always causes a
531    match failure.
532  </P>  </P>
533  <P>  <P>
534  Sets of Unicode characters are defined as belonging to certain scripts. A  Sets of Unicode characters are defined as belonging to certain scripts. A
# Line 420  Those that are not part of an identified Line 544  Those that are not part of an identified
544  <P>  <P>
545  Arabic,  Arabic,
546  Armenian,  Armenian,
547    Avestan,
548  Balinese,  Balinese,
549    Bamum,
550  Bengali,  Bengali,
551  Bopomofo,  Bopomofo,
552  Braille,  Braille,
553  Buginese,  Buginese,
554  Buhid,  Buhid,
555  Canadian_Aboriginal,  Canadian_Aboriginal,
556    Carian,
557    Cham,
558  Cherokee,  Cherokee,
559  Common,  Common,
560  Coptic,  Coptic,
# Line 435  Cypriot, Line 563  Cypriot,
563  Cyrillic,  Cyrillic,
564  Deseret,  Deseret,
565  Devanagari,  Devanagari,
566    Egyptian_Hieroglyphs,
567  Ethiopic,  Ethiopic,
568  Georgian,  Georgian,
569  Glagolitic,  Glagolitic,
# Line 447  Hangul, Line 576  Hangul,
576  Hanunoo,  Hanunoo,
577  Hebrew,  Hebrew,
578  Hiragana,  Hiragana,
579    Imperial_Aramaic,
580  Inherited,  Inherited,
581    Inscriptional_Pahlavi,
582    Inscriptional_Parthian,
583    Javanese,
584    Kaithi,
585  Kannada,  Kannada,
586  Katakana,  Katakana,
587    Kayah_Li,
588  Kharoshthi,  Kharoshthi,
589  Khmer,  Khmer,
590  Lao,  Lao,
591  Latin,  Latin,
592    Lepcha,
593  Limbu,  Limbu,
594  Linear_B,  Linear_B,
595    Lisu,
596    Lycian,
597    Lydian,
598  Malayalam,  Malayalam,
599    Meetei_Mayek,
600  Mongolian,  Mongolian,
601  Myanmar,  Myanmar,
602  New_Tai_Lue,  New_Tai_Lue,
# Line 464  Nko, Line 604  Nko,
604  Ogham,  Ogham,
605  Old_Italic,  Old_Italic,
606  Old_Persian,  Old_Persian,
607    Old_South_Arabian,
608    Old_Turkic,
609    Ol_Chiki,
610  Oriya,  Oriya,
611  Osmanya,  Osmanya,
612  Phags_Pa,  Phags_Pa,
613  Phoenician,  Phoenician,
614    Rejang,
615  Runic,  Runic,
616    Samaritan,
617    Saurashtra,
618  Shavian,  Shavian,
619  Sinhala,  Sinhala,
620    Sundanese,
621  Syloti_Nagri,  Syloti_Nagri,
622  Syriac,  Syriac,
623  Tagalog,  Tagalog,
624  Tagbanwa,  Tagbanwa,
625  Tai_Le,  Tai_Le,
626    Tai_Tham,
627    Tai_Viet,
628  Tamil,  Tamil,
629  Telugu,  Telugu,
630  Thaana,  Thaana,
# Line 483  Thai, Line 632  Thai,
632  Tibetan,  Tibetan,
633  Tifinagh,  Tifinagh,
634  Ugaritic,  Ugaritic,
635    Vai,
636  Yi.  Yi.
637  </P>  </P>
638  <P>  <P>
639  Each character has exactly one general category property, specified by a  Each character has exactly one Unicode general category property, specified by
640  two-letter abbreviation. For compatibility with Perl, negation can be specified  a two-letter abbreviation. For compatibility with Perl, negation can be
641  by including a circumflex between the opening brace and the property name. For  specified by including a circumflex between the opening brace and the property
642  example, \p{^Lu} is the same as \P{Lu}.  name. For example, \p{^Lu} is the same as \P{Lu}.
643  </P>  </P>
644  <P>  <P>
645  If only one letter is specified with \p or \P, it includes all the general  If only one letter is specified with \p or \P, it includes all the general
# Line 556  U+DFFF. Such characters are not valid in Line 706  U+DFFF. Such characters are not valid in
706  cannot be tested by PCRE, unless UTF-8 validity checking has been turned off  cannot be tested by PCRE, unless UTF-8 validity checking has been turned off
707  (see the discussion of PCRE_NO_UTF8_CHECK in the  (see the discussion of PCRE_NO_UTF8_CHECK in the
708  <a href="pcreapi.html"><b>pcreapi</b></a>  <a href="pcreapi.html"><b>pcreapi</b></a>
709  page).  page). Perl does not support the Cs property.
710  </P>  </P>
711  <P>  <P>
712  The long synonyms for these properties that Perl supports (such as \p{Letter})  The long synonyms for property names that Perl supports (such as \p{Letter})
713  are not supported by PCRE, nor is it permitted to prefix any of these  are not supported by PCRE, nor is it permitted to prefix any of these
714  properties with "Is".  properties with "Is".
715  </P>  </P>
# Line 590  non-UTF-8 mode \X matches any one charac Line 740  non-UTF-8 mode \X matches any one charac
740  Matching characters by Unicode property is not fast, because PCRE has to search  Matching characters by Unicode property is not fast, because PCRE has to search
741  a structure that contains data for over fifteen thousand characters. That is  a structure that contains data for over fifteen thousand characters. That is
742  why the traditional escape sequences such as \d and \w do not use Unicode  why the traditional escape sequences such as \d and \w do not use Unicode
743  properties in PCRE.  properties in PCRE by default, though you can make them do so by setting the
744    PCRE_UCP option for <b>pcre_compile()</b> or by starting the pattern with
745    (*UCP).
746    <a name="extraprops"></a></P>
747    <br><b>
748    PCRE's additional properties
749    </b><br>
750    <P>
751    As well as the standard Unicode properties described in the previous
752    section, PCRE supports four more that make it possible to convert traditional
753    escape sequences such as \w and \s and POSIX character classes to use Unicode
754    properties. PCRE uses these non-standard, non-Perl properties internally when
755    PCRE_UCP is set. They are:
756    <pre>
757      Xan   Any alphanumeric character
758      Xps   Any POSIX space character
759      Xsp   Any Perl space character
760      Xwd   Any Perl "word" character
761    </pre>
762    Xan matches characters that have either the L (letter) or the N (number)
763    property. Xps matches the characters tab, linefeed, vertical tab, formfeed, or
764    carriage return, and any other character that has the Z (separator) property.
765    Xsp is the same as Xps, except that vertical tab is excluded. Xwd matches the
766    same characters as Xan, plus underscore.
767  <a name="resetmatchstart"></a></P>  <a name="resetmatchstart"></a></P>
768  <br><b>  <br><b>
769  Resetting the match start  Resetting the match start
# Line 614  For example, when the pattern Line 787  For example, when the pattern
787    (foo)\Kbar    (foo)\Kbar
788  </pre>  </pre>
789  matches "foobar", the first substring is still set to "foo".  matches "foobar", the first substring is still set to "foo".
790    </P>
791    <P>
792    Perl documents that the use of \K within assertions is "not well defined". In
793    PCRE, \K is acted upon when it occurs inside positive assertions, but is
794    ignored in negative assertions.
795  <a name="smallassertions"></a></P>  <a name="smallassertions"></a></P>
796  <br><b>  <br><b>
797  Simple assertions  Simple assertions
# Line 634  The backslashed assertions are: Line 812  The backslashed assertions are:
812    \z     matches only at the end of the subject    \z     matches only at the end of the subject
813    \G     matches at the first matching position in the subject    \G     matches at the first matching position in the subject
814  </pre>  </pre>
815  These assertions may not appear in character classes (but note that \b has a  Inside a character class, \b has a different meaning; it matches the backspace
816  different meaning, namely the backspace character, inside a character class).  character. If any other of these assertions appears in a character class, by
817    default it matches the corresponding literal character (for example, \B
818    matches the letter B). However, if the PCRE_EXTRA option is set, an "invalid
819    escape sequence" error is generated instead.
820  </P>  </P>
821  <P>  <P>
822  A word boundary is a position in the subject string where the current character  A word boundary is a position in the subject string where the current character
823  and the previous character do not both match \w or \W (i.e. one matches  and the previous character do not both match \w or \W (i.e. one matches
824  \w and the other matches \W), or the start or end of the string if the  \w and the other matches \W), or the start or end of the string if the
825  first or last character matches \w, respectively.  first or last character matches \w, respectively. In UTF-8 mode, the meanings
826    of \w and \W can be changed by setting the PCRE_UCP option. When this is
827    done, it also affects \b and \B. Neither PCRE nor Perl has a separate "start
828    of word" or "end of word" metasequence. However, whatever follows \b normally
829    determines which it is. For example, the fragment \ba matches "a" at the start
830    of a word.
831  </P>  </P>
832  <P>  <P>
833  The \A, \Z, and \z assertions differ from the traditional circumflex and  The \A, \Z, and \z assertions differ from the traditional circumflex and
# Line 675  If all the alternatives of a pattern beg Line 861  If all the alternatives of a pattern beg
861  to the starting match position, and the "anchored" flag is set in the compiled  to the starting match position, and the "anchored" flag is set in the compiled
862  regular expression.  regular expression.
863  </P>  </P>
864  <br><a name="SEC4" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>  <br><a name="SEC5" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>
865  <P>  <P>
866  Outside a character class, in the default matching mode, the circumflex  Outside a character class, in the default matching mode, the circumflex
867  character is an assertion that is true only if the current matching point is  character is an assertion that is true only if the current matching point is
# Line 728  PCRE_DOLLAR_ENDONLY option is ignored if Line 914  PCRE_DOLLAR_ENDONLY option is ignored if
914  Note that the sequences \A, \Z, and \z can be used to match the start and  Note that the sequences \A, \Z, and \z can be used to match the start and
915  end of the subject in both modes, and if all branches of a pattern start with  end of the subject in both modes, and if all branches of a pattern start with
916  \A it is always anchored, whether or not PCRE_MULTILINE is set.  \A it is always anchored, whether or not PCRE_MULTILINE is set.
917  </P>  <a name="fullstopdot"></a></P>
918  <br><a name="SEC5" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br>  <br><a name="SEC6" href="#TOC1">FULL STOP (PERIOD, DOT) AND \N</a><br>
919  <P>  <P>
920  Outside a character class, a dot in the pattern matches any one character in  Outside a character class, a dot in the pattern matches any one character in
921  the subject string except (by default) a character that signifies the end of a  the subject string except (by default) a character that signifies the end of a
# Line 754  The handling of dot is entirely independ Line 940  The handling of dot is entirely independ
940  dollar, the only relationship being that they both involve newlines. Dot has no  dollar, the only relationship being that they both involve newlines. Dot has no
941  special meaning in a character class.  special meaning in a character class.
942  </P>  </P>
943  <br><a name="SEC6" href="#TOC1">MATCHING A SINGLE BYTE</a><br>  <P>
944    The escape sequence \N always behaves as a dot does when PCRE_DOTALL is not
945    set. In other words, it matches any one character except one that signifies the
946    end of a line.
947    </P>
948    <br><a name="SEC7" href="#TOC1">MATCHING A SINGLE BYTE</a><br>
949  <P>  <P>
950  Outside a character class, the escape sequence \C matches any one byte, both  Outside a character class, the escape sequence \C matches any one byte, both
951  in and out of UTF-8 mode. Unlike a dot, it always matches any line-ending  in and out of UTF-8 mode. Unlike a dot, it always matches any line-ending
# Line 769  PCRE does not allow \C to appear in look Line 960  PCRE does not allow \C to appear in look
960  because in UTF-8 mode this would make it impossible to calculate the length of  because in UTF-8 mode this would make it impossible to calculate the length of
961  the lookbehind.  the lookbehind.
962  <a name="characterclass"></a></P>  <a name="characterclass"></a></P>
963  <br><a name="SEC7" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br>  <br><a name="SEC8" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br>
964  <P>  <P>
965  An opening square bracket introduces a character class, terminated by a closing  An opening square bracket introduces a character class, terminated by a closing
966  square bracket. A closing square bracket on its own is not special. If a  square bracket. A closing square bracket on its own is not special by default.
967  closing square bracket is required as a member of the class, it should be the  However, if the PCRE_JAVASCRIPT_COMPAT option is set, a lone closing square
968  first data character in the class (after an initial circumflex, if present) or  bracket causes a compile-time error. If a closing square bracket is required as
969  escaped with a backslash.  a member of the class, it should be the first data character in the class
970    (after an initial circumflex, if present) or escaped with a backslash.
971  </P>  </P>
972  <P>  <P>
973  A character class matches a single character in the subject. In UTF-8 mode, the  A character class matches a single character in the subject. In UTF-8 mode, the
974  character may occupy more than one byte. A matched character must be in the set  character may be more than one byte long. A matched character must be in the
975  of characters defined by the class, unless the first character in the class  set of characters defined by the class, unless the first character in the class
976  definition is a circumflex, in which case the subject character must not be in  definition is a circumflex, in which case the subject character must not be in
977  the set defined by the class. If a circumflex is actually required as a member  the set defined by the class. If a circumflex is actually required as a member
978  of the class, ensure it is not the first character, or escape it with a  of the class, ensure it is not the first character, or escape it with a
# Line 791  For example, the character class [aeiou] Line 983  For example, the character class [aeiou]
983  [^aeiou] matches any character that is not a lower case vowel. Note that a  [^aeiou] matches any character that is not a lower case vowel. Note that a
984  circumflex is just a convenient notation for specifying the characters that  circumflex is just a convenient notation for specifying the characters that
985  are in the class by enumerating those that are not. A class that starts with a  are in the class by enumerating those that are not. A class that starts with a
986  circumflex is not an assertion: it still consumes a character from the subject  circumflex is not an assertion; it still consumes a character from the subject
987  string, and therefore it fails if the current pointer is at the end of the  string, and therefore it fails if the current pointer is at the end of the
988  string.  string.
989  </P>  </P>
# Line 807  caseful version would. In UTF-8 mode, PC Line 999  caseful version would. In UTF-8 mode, PC
999  case for characters whose values are less than 128, so caseless matching is  case for characters whose values are less than 128, so caseless matching is
1000  always possible. For characters with higher values, the concept of case is  always possible. For characters with higher values, the concept of case is
1001  supported if PCRE is compiled with Unicode property support, but not otherwise.  supported if PCRE is compiled with Unicode property support, but not otherwise.
1002  If you want to use caseless matching for characters 128 and above, you must  If you want to use caseless matching in UTF8-mode for characters 128 and above,
1003  ensure that PCRE is compiled with Unicode property support as well as with  you must ensure that PCRE is compiled with Unicode property support as well as
1004  UTF-8 support.  with UTF-8 support.
1005  </P>  </P>
1006  <P>  <P>
1007  Characters that might indicate line breaks are never treated in any special way  Characters that might indicate line breaks are never treated in any special way
# Line 849  characters with values greater than 128 Line 1041  characters with values greater than 128
1041  property support.  property support.
1042  </P>  </P>
1043  <P>  <P>
1044  The character types \d, \D, \p, \P, \s, \S, \w, and \W may also appear  The character types \d, \D, \h, \H, \p, \P, \s, \S, \v, \V, \w, and
1045  in a character class, and add the characters that they match to the class. For  \W may also appear in a character class, and add the characters that they
1046  example, [\dABCDEF] matches any hexadecimal digit. A circumflex can  match to the class. For example, [\dABCDEF] matches any hexadecimal digit. A
1047  conveniently be used with the upper case character types to specify a more  circumflex can conveniently be used with the upper case character types to
1048  restricted set of characters than the matching lower case type. For example,  specify a more restricted set of characters than the matching lower case type.
1049  the class [^\W_] matches any letter or digit, but not underscore.  For example, the class [^\W_] matches any letter or digit, but not underscore.
1050  </P>  </P>
1051  <P>  <P>
1052  The only metacharacters that are recognized in character classes are backslash,  The only metacharacters that are recognized in character classes are backslash,
# Line 864  introducing a POSIX class name - see the Line 1056  introducing a POSIX class name - see the
1056  closing square bracket. However, escaping other non-alphanumeric characters  closing square bracket. However, escaping other non-alphanumeric characters
1057  does no harm.  does no harm.
1058  </P>  </P>
1059  <br><a name="SEC8" href="#TOC1">POSIX CHARACTER CLASSES</a><br>  <br><a name="SEC9" href="#TOC1">POSIX CHARACTER CLASSES</a><br>
1060  <P>  <P>
1061  Perl supports the POSIX notation for character classes. This uses names  Perl supports the POSIX notation for character classes. This uses names
1062  enclosed by [: and :] within the enclosing square brackets. PCRE also supports  enclosed by [: and :] within the enclosing square brackets. PCRE also supports
# Line 873  this notation. For example, Line 1065  this notation. For example,
1065    [01[:alpha:]%]    [01[:alpha:]%]
1066  </pre>  </pre>
1067  matches "0", "1", any alphabetic character, or "%". The supported class names  matches "0", "1", any alphabetic character, or "%". The supported class names
1068  are  are:
1069  <pre>  <pre>
1070    alnum    letters and digits    alnum    letters and digits
1071    alpha    letters    alpha    letters
# Line 884  are Line 1076  are
1076    graph    printing characters, excluding space    graph    printing characters, excluding space
1077    lower    lower case letters    lower    lower case letters
1078    print    printing characters, including space    print    printing characters, including space
1079    punct    printing characters, excluding letters and digits    punct    printing characters, excluding letters and digits and space
1080    space    white space (not quite the same as \s)    space    white space (not quite the same as \s)
1081    upper    upper case letters    upper    upper case letters
1082    word     "word" characters (same as \w)    word     "word" characters (same as \w)
# Line 907  syntax [.ch.] and [=ch=] where "ch" is a Line 1099  syntax [.ch.] and [=ch=] where "ch" is a
1099  supported, and an error is given if they are encountered.  supported, and an error is given if they are encountered.
1100  </P>  </P>
1101  <P>  <P>
1102  In UTF-8 mode, characters with values greater than 128 do not match any of  By default, in UTF-8 mode, characters with values greater than 128 do not match
1103  the POSIX character classes.  any of the POSIX character classes. However, if the PCRE_UCP option is passed
1104    to <b>pcre_compile()</b>, some of the classes are changed so that Unicode
1105    character properties are used. This is achieved by replacing the POSIX classes
1106    by other sequences, as follows:
1107    <pre>
1108      [:alnum:]  becomes  \p{Xan}
1109      [:alpha:]  becomes  \p{L}
1110      [:blank:]  becomes  \h
1111      [:digit:]  becomes  \p{Nd}
1112      [:lower:]  becomes  \p{Ll}
1113      [:space:]  becomes  \p{Xps}
1114      [:upper:]  becomes  \p{Lu}
1115      [:word:]   becomes  \p{Xwd}
1116    </pre>
1117    Negated versions, such as [:^alpha:] use \P instead of \p. The other POSIX
1118    classes are unchanged, and match only characters with code points less than
1119    128.
1120  </P>  </P>
1121  <br><a name="SEC9" href="#TOC1">VERTICAL BAR</a><br>  <br><a name="SEC10" href="#TOC1">VERTICAL BAR</a><br>
1122  <P>  <P>
1123  Vertical bar characters are used to separate alternative patterns. For example,  Vertical bar characters are used to separate alternative patterns. For example,
1124  the pattern  the pattern
# Line 925  that succeeds is used. If the alternativ Line 1133  that succeeds is used. If the alternativ
1133  "succeeds" means matching the rest of the main pattern as well as the  "succeeds" means matching the rest of the main pattern as well as the
1134  alternative in the subpattern.  alternative in the subpattern.
1135  </P>  </P>
1136  <br><a name="SEC10" href="#TOC1">INTERNAL OPTION SETTING</a><br>  <br><a name="SEC11" href="#TOC1">INTERNAL OPTION SETTING</a><br>
1137  <P>  <P>
1138  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
1139  PCRE_EXTENDED options can be changed from within the pattern by a sequence of  PCRE_EXTENDED options (which are Perl-compatible) can be changed from within
1140  Perl option letters enclosed between "(?" and ")". The option letters are  the pattern by a sequence of Perl option letters enclosed between "(?" and ")".
1141    The option letters are
1142  <pre>  <pre>
1143    i  for PCRE_CASELESS    i  for PCRE_CASELESS
1144    m  for PCRE_MULTILINE    m  for PCRE_MULTILINE
# Line 944  permitted. If a letter appears both befo Line 1153  permitted. If a letter appears both befo
1153  unset.  unset.
1154  </P>  </P>
1155  <P>  <P>
1156  When an option change occurs at top level (that is, not inside subpattern  The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be
1157  parentheses), the change applies to the remainder of the pattern that follows.  changed in the same way as the Perl-compatible options by using the characters
1158  If the change is placed right at the start of a pattern, PCRE extracts it into  J, U and X respectively.
1159  the global options (and it will therefore show up in data extracted by the  </P>
1160  <b>pcre_fullinfo()</b> function).  <P>
1161    When one of these option changes occurs at top level (that is, not inside
1162    subpattern parentheses), the change applies to the remainder of the pattern
1163    that follows. If the change is placed right at the start of a pattern, PCRE
1164    extracts it into the global options (and it will therefore show up in data
1165    extracted by the <b>pcre_fullinfo()</b> function).
1166  </P>  </P>
1167  <P>  <P>
1168  An option change within a subpattern (see below for a description of  An option change within a subpattern (see below for a description of
# Line 969  option settings happen at compile time. Line 1183  option settings happen at compile time.
1183  behaviour otherwise.  behaviour otherwise.
1184  </P>  </P>
1185  <P>  <P>
1186  The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be  <b>Note:</b> There are other PCRE-specific options that can be set by the
1187  changed in the same way as the Perl-compatible options by using the characters  application when the compile or match functions are called. In some cases the
1188  J, U and X respectively.  pattern can contain special leading sequences such as (*CRLF) to override what
1189    the application has set or what has been defaulted. Details are given in the
1190    section entitled
1191    <a href="#newlineseq">"Newline sequences"</a>
1192    above. There are also the (*UTF8) and (*UCP) leading sequences that can be used
1193    to set UTF-8 and Unicode property modes; they are equivalent to setting the
1194    PCRE_UTF8 and the PCRE_UCP options, respectively.
1195  <a name="subpattern"></a></P>  <a name="subpattern"></a></P>
1196  <br><a name="SEC11" href="#TOC1">SUBPATTERNS</a><br>  <br><a name="SEC12" href="#TOC1">SUBPATTERNS</a><br>
1197  <P>  <P>
1198  Subpatterns are delimited by parentheses (round brackets), which can be nested.  Subpatterns are delimited by parentheses (round brackets), which can be nested.
1199  Turning part of a pattern into a subpattern does two things:  Turning part of a pattern into a subpattern does two things:
# Line 1026  match exactly the same set of strings. B Line 1246  match exactly the same set of strings. B
1246  from left to right, and options are not reset until the end of the subpattern  from left to right, and options are not reset until the end of the subpattern
1247  is reached, an option setting in one branch does affect subsequent branches, so  is reached, an option setting in one branch does affect subsequent branches, so
1248  the above patterns match "SUNDAY" as well as "Saturday".  the above patterns match "SUNDAY" as well as "Saturday".
1249  </P>  <a name="dupsubpatternnumber"></a></P>
1250  <br><a name="SEC12" href="#TOC1">DUPLICATE SUBPATTERN NUMBERS</a><br>  <br><a name="SEC13" href="#TOC1">DUPLICATE SUBPATTERN NUMBERS</a><br>
1251  <P>  <P>
1252  Perl 5.10 introduced a feature whereby each alternative in a subpattern uses  Perl 5.10 introduced a feature whereby each alternative in a subpattern uses
1253  the same numbers for its capturing parentheses. Such a subpattern starts with  the same numbers for its capturing parentheses. Such a subpattern starts with
# Line 1051  stored. Line 1271  stored.
1271    / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x    / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
1272    # 1            2         2  3        2     3     4    # 1            2         2  3        2     3     4
1273  </pre>  </pre>
1274  A backreference or a recursive call to a numbered subpattern always refers to  A back reference to a numbered subpattern uses the most recent value that is
1275  the first one in the pattern with the given number.  set for that number by any subpattern. The following pattern matches "abcabc"
1276    or "defdef":
1277    <pre>
1278      /(?|(abc)|(def))\1/
1279    </pre>
1280    In contrast, a recursive or "subroutine" call to a numbered subpattern always
1281    refers to the first one in the pattern with the given number. The following
1282    pattern matches "abcabc" or "defabc":
1283    <pre>
1284      /(?|(abc)|(def))(?1)/
1285    </pre>
1286    If a
1287    <a href="#conditions">condition test</a>
1288    for a subpattern's having matched refers to a non-unique number, the test is
1289    true if any of the subpatterns of that number have matched.
1290  </P>  </P>
1291  <P>  <P>
1292  An alternative approach to using this "branch reset" feature is to use  An alternative approach to using this "branch reset" feature is to use
1293  duplicate named subpatterns, as described in the next section.  duplicate named subpatterns, as described in the next section.
1294  </P>  </P>
1295  <br><a name="SEC13" href="#TOC1">NAMED SUBPATTERNS</a><br>  <br><a name="SEC14" href="#TOC1">NAMED SUBPATTERNS</a><br>
1296  <P>  <P>
1297  Identifying capturing parentheses by number is simple, but it can be very hard  Identifying capturing parentheses by number is simple, but it can be very hard
1298  to keep track of the numbers in complicated regular expressions. Furthermore,  to keep track of the numbers in complicated regular expressions. Furthermore,
# Line 1066  if an expression is modified, the number Line 1300  if an expression is modified, the number
1300  difficulty, PCRE supports the naming of subpatterns. This feature was not  difficulty, PCRE supports the naming of subpatterns. This feature was not
1301  added to Perl until release 5.10. Python had the feature earlier, and PCRE  added to Perl until release 5.10. Python had the feature earlier, and PCRE
1302  introduced it at release 4.0, using the Python syntax. PCRE now supports both  introduced it at release 4.0, using the Python syntax. PCRE now supports both
1303  the Perl and the Python syntax.  the Perl and the Python syntax. Perl allows identically numbered subpatterns to
1304    have different names, but PCRE does not.
1305  </P>  </P>
1306  <P>  <P>
1307  In PCRE, a subpattern can be named in one of three ways: (?&#60;name&#62;...) or  In PCRE, a subpattern can be named in one of three ways: (?&#60;name&#62;...) or
1308  (?'name'...) as in Perl, or (?P&#60;name&#62;...) as in Python. References to capturing  (?'name'...) as in Perl, or (?P&#60;name&#62;...) as in Python. References to capturing
1309  parentheses from other parts of the pattern, such as  parentheses from other parts of the pattern, such as
1310  <a href="#backreferences">backreferences,</a>  <a href="#backreferences">back references,</a>
1311  <a href="#recursion">recursion,</a>  <a href="#recursion">recursion,</a>
1312  and  and
1313  <a href="#conditions">conditions,</a>  <a href="#conditions">conditions,</a>
# Line 1087  is also a convenience function for extra Line 1322  is also a convenience function for extra
1322  </P>  </P>
1323  <P>  <P>
1324  By default, a name must be unique within a pattern, but it is possible to relax  By default, a name must be unique within a pattern, but it is possible to relax
1325  this constraint by setting the PCRE_DUPNAMES option at compile time. This can  this constraint by setting the PCRE_DUPNAMES option at compile time. (Duplicate
1326  be useful for patterns where only one instance of the named parentheses can  names are also always permitted for subpatterns with the same number, set up as
1327  match. Suppose you want to match the name of a weekday, either as a 3-letter  described in the previous section.) Duplicate names can be useful for patterns
1328  abbreviation or as the full name, and in both cases you want to extract the  where only one instance of the named parentheses can match. Suppose you want to
1329  abbreviation. This pattern (ignoring the line breaks) does the job:  match the name of a weekday, either as a 3-letter abbreviation or as the full
1330    name, and in both cases you want to extract the abbreviation. This pattern
1331    (ignoring the line breaks) does the job:
1332  <pre>  <pre>
1333    (?&#60;DN&#62;Mon|Fri|Sun)(?:day)?|    (?&#60;DN&#62;Mon|Fri|Sun)(?:day)?|
1334    (?&#60;DN&#62;Tue)(?:sday)?|    (?&#60;DN&#62;Tue)(?:sday)?|
# Line 1106  subpattern, as described in the previous Line 1343  subpattern, as described in the previous
1343  <P>  <P>
1344  The convenience function for extracting the data by name returns the substring  The convenience function for extracting the data by name returns the substring
1345  for the first (and in this example, the only) subpattern of that name that  for the first (and in this example, the only) subpattern of that name that
1346  matched. This saves searching to find which numbered subpattern it was. If you  matched. This saves searching to find which numbered subpattern it was.
1347  make a reference to a non-unique named subpattern from elsewhere in the  </P>
1348  pattern, the one that corresponds to the lowest number is used. For further  <P>
1349  details of the interfaces for handling named subpatterns, see the  If you make a back reference to a non-unique named subpattern from elsewhere in
1350    the pattern, the one that corresponds to the first occurrence of the name is
1351    used. In the absence of duplicate numbers (see the previous section) this is
1352    the one with the lowest number. If you use a named reference in a condition
1353    test (see the
1354    <a href="#conditions">section about conditions</a>
1355    below), either to check whether a subpattern has matched, or to check for
1356    recursion, all subpatterns with the same name are tested. If the condition is
1357    true for any one of them, the overall condition is true. This is the same
1358    behaviour as testing by number. For further details of the interfaces for
1359    handling named subpatterns, see the
1360  <a href="pcreapi.html"><b>pcreapi</b></a>  <a href="pcreapi.html"><b>pcreapi</b></a>
1361  documentation.  documentation.
1362  </P>  </P>
1363  <br><a name="SEC14" href="#TOC1">REPETITION</a><br>  <P>
1364    <b>Warning:</b> You cannot use different names to distinguish between two
1365    subpatterns with the same number because PCRE uses only the numbers when
1366    matching. For this reason, an error is given at compile time if different names
1367    are given to subpatterns with the same number. However, you can give the same
1368    name to subpatterns with the same number, even when PCRE_DUPNAMES is not set.
1369    </P>
1370    <br><a name="SEC15" href="#TOC1">REPETITION</a><br>
1371  <P>  <P>
1372  Repetition is specified by quantifiers, which can follow any of the following  Repetition is specified by quantifiers, which can follow any of the following
1373  items:  items:
# Line 1127  items: Line 1381  items:
1381    a character class    a character class
1382    a back reference (see next section)    a back reference (see next section)
1383    a parenthesized subpattern (unless it is an assertion)    a parenthesized subpattern (unless it is an assertion)
1384      a recursive or "subroutine" call to a subpattern
1385  </pre>  </pre>
1386  The general repetition quantifier specifies a minimum and maximum number of  The general repetition quantifier specifies a minimum and maximum number of
1387  permitted matches, by giving the two numbers in curly brackets (braces),  permitted matches, by giving the two numbers in curly brackets (braces),
# Line 1160  which may be several bytes long (and the Line 1415  which may be several bytes long (and the
1415  </P>  </P>
1416  <P>  <P>
1417  The quantifier {0} is permitted, causing the expression to behave as if the  The quantifier {0} is permitted, causing the expression to behave as if the
1418  previous item and the quantifier were not present.  previous item and the quantifier were not present. This may be useful for
1419    subpatterns that are referenced as
1420    <a href="#subpatternsassubroutines">subroutines</a>
1421    from elsewhere in the pattern. Items other than subpatterns that have a {0}
1422    quantifier are omitted from the compiled pattern.
1423  </P>  </P>
1424  <P>  <P>
1425  For convenience, the three most common quantifiers have single-character  For convenience, the three most common quantifiers have single-character
# Line 1240  alternatively using ^ to indicate anchor Line 1499  alternatively using ^ to indicate anchor
1499  </P>  </P>
1500  <P>  <P>
1501  However, there is one situation where the optimization cannot be used. When .*  However, there is one situation where the optimization cannot be used. When .*
1502  is inside capturing parentheses that are the subject of a backreference  is inside capturing parentheses that are the subject of a back reference
1503  elsewhere in the pattern, a match at the start may fail where a later one  elsewhere in the pattern, a match at the start may fail where a later one
1504  succeeds. Consider, for example:  succeeds. Consider, for example:
1505  <pre>  <pre>
# Line 1264  example, after Line 1523  example, after
1523  </pre>  </pre>
1524  matches "aba" the value of the second captured substring is "b".  matches "aba" the value of the second captured substring is "b".
1525  <a name="atomicgroup"></a></P>  <a name="atomicgroup"></a></P>
1526  <br><a name="SEC15" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br>  <br><a name="SEC16" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br>
1527  <P>  <P>
1528  With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")  With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
1529  repetition, failure of what follows normally causes the repeated item to be  repetition, failure of what follows normally causes the repeated item to be
# Line 1368  an atomic group, like this: Line 1627  an atomic group, like this:
1627  </pre>  </pre>
1628  sequences of non-digits cannot be broken, and failure happens quickly.  sequences of non-digits cannot be broken, and failure happens quickly.
1629  <a name="backreferences"></a></P>  <a name="backreferences"></a></P>
1630  <br><a name="SEC16" href="#TOC1">BACK REFERENCES</a><br>  <br><a name="SEC17" href="#TOC1">BACK REFERENCES</a><br>
1631  <P>  <P>
1632  Outside a character class, a backslash followed by a digit greater than 0 (and  Outside a character class, a backslash followed by a digit greater than 0 (and
1633  possibly further digits) is a back reference to a capturing subpattern earlier  possibly further digits) is a back reference to a capturing subpattern earlier
# Line 1454  after the reference. Line 1713  after the reference.
1713  <P>  <P>
1714  There may be more than one back reference to the same subpattern. If a  There may be more than one back reference to the same subpattern. If a
1715  subpattern has not actually been used in a particular match, any back  subpattern has not actually been used in a particular match, any back
1716  references to it always fail. For example, the pattern  references to it always fail by default. For example, the pattern
1717  <pre>  <pre>
1718    (a|(bc))\2    (a|(bc))\2
1719  </pre>  </pre>
1720  always fails if it starts to match "a" rather than "bc". Because there may be  always fails if it starts to match "a" rather than "bc". However, if the
1721  many capturing parentheses in a pattern, all digits following the backslash are  PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back reference to an
1722  taken as part of a potential back reference number. If the pattern continues  unset value matches an empty string.
1723  with a digit character, some delimiter must be used to terminate the back  </P>
1724  reference. If the PCRE_EXTENDED option is set, this can be whitespace.  <P>
1725  Otherwise an empty comment (see  Because there may be many capturing parentheses in a pattern, all digits
1726    following a backslash are taken as part of a potential back reference number.
1727    If the pattern continues with a digit character, some delimiter must be used to
1728    terminate the back reference. If the PCRE_EXTENDED option is set, this can be
1729    whitespace. Otherwise, the \g{ syntax or an empty comment (see
1730  <a href="#comments">"Comments"</a>  <a href="#comments">"Comments"</a>
1731  below) can be used.  below) can be used.
1732  </P>  </P>
1733    <br><b>
1734    Recursive back references
1735    </b><br>
1736  <P>  <P>
1737  A back reference that occurs inside the parentheses to which it refers fails  A back reference that occurs inside the parentheses to which it refers fails
1738  when the subpattern is first used, so, for example, (a\1) never matches.  when the subpattern is first used, so, for example, (a\1) never matches.
# Line 1481  to the previous iteration. In order for Line 1747  to the previous iteration. In order for
1747  that the first iteration does not need to match the back reference. This can be  that the first iteration does not need to match the back reference. This can be
1748  done using alternation, as in the example above, or by a quantifier with a  done using alternation, as in the example above, or by a quantifier with a
1749  minimum of zero.  minimum of zero.
1750    </P>
1751    <P>
1752    Back references of this type cause the group that they reference to be treated
1753    as an
1754    <a href="#atomicgroup">atomic group.</a>
1755    Once the whole group has been matched, a subsequent matching failure cannot
1756    cause backtracking into the middle of the group.
1757  <a name="bigassertions"></a></P>  <a name="bigassertions"></a></P>
1758  <br><a name="SEC17" href="#TOC1">ASSERTIONS</a><br>  <br><a name="SEC18" href="#TOC1">ASSERTIONS</a><br>
1759  <P>  <P>
1760  An assertion is a test on the characters following or preceding the current  An assertion is a test on the characters following or preceding the current
1761  matching point that does not actually consume any characters. The simple  matching point that does not actually consume any characters. The simple
# Line 1531  lookbehind assertion is needed to achiev Line 1804  lookbehind assertion is needed to achiev
1804  If you want to force a matching failure at some point in a pattern, the most  If you want to force a matching failure at some point in a pattern, the most
1805  convenient way to do it is with (?!) because an empty string always matches, so  convenient way to do it is with (?!) because an empty string always matches, so
1806  an assertion that requires there not to be an empty string must always fail.  an assertion that requires there not to be an empty string must always fail.
1807    The Perl 5.10 backtracking control verb (*FAIL) or (*F) is essentially a
1808    synonym for (?!).
1809  <a name="lookbehind"></a></P>  <a name="lookbehind"></a></P>
1810  <br><b>  <br><b>
1811  Lookbehind assertions  Lookbehind assertions
# Line 1554  is permitted, but Line 1829  is permitted, but
1829  </pre>  </pre>
1830  causes an error at compile time. Branches that match different length strings  causes an error at compile time. Branches that match different length strings
1831  are permitted only at the top level of a lookbehind assertion. This is an  are permitted only at the top level of a lookbehind assertion. This is an
1832  extension compared with Perl (at least for 5.8), which requires all branches to  extension compared with Perl (5.8 and 5.10), which requires all branches to
1833  match the same length of string. An assertion such as  match the same length of string. An assertion such as
1834  <pre>  <pre>
1835    (?&#60;=ab(c|de))    (?&#60;=ab(c|de))
1836  </pre>  </pre>
1837  is not permitted, because its single top-level branch can match two different  is not permitted, because its single top-level branch can match two different
1838  lengths, but it is acceptable if rewritten to use two top-level branches:  lengths, but it is acceptable to PCRE if rewritten to use two top-level
1839    branches:
1840  <pre>  <pre>
1841    (?&#60;=abc|abde)    (?&#60;=abc|abde)
1842  </pre>  </pre>
1843  In some cases, the Perl 5.10 escape sequence \K  In some cases, the Perl 5.10 escape sequence \K
1844  <a href="#resetmatchstart">(see above)</a>  <a href="#resetmatchstart">(see above)</a>
1845  can be used instead of a lookbehind assertion; this is not restricted to a  can be used instead of a lookbehind assertion to get round the fixed-length
1846  fixed-length.  restriction.
1847  </P>  </P>
1848  <P>  <P>
1849  The implementation of lookbehind assertions is, for each alternative, to  The implementation of lookbehind assertions is, for each alternative, to
# Line 1582  the length of the lookbehind. The \X and Line 1858  the length of the lookbehind. The \X and
1858  different numbers of bytes, are also not permitted.  different numbers of bytes, are also not permitted.
1859  </P>  </P>
1860  <P>  <P>
1861    <a href="#subpatternsassubroutines">"Subroutine"</a>
1862    calls (see below) such as (?2) or (?&X) are permitted in lookbehinds, as long
1863    as the subpattern matches a fixed-length string.
1864    <a href="#recursion">Recursion,</a>
1865    however, is not supported.
1866    </P>
1867    <P>
1868  Possessive quantifiers can be used in conjunction with lookbehind assertions to  Possessive quantifiers can be used in conjunction with lookbehind assertions to
1869  specify efficient matching at the end of the subject string. Consider a simple  specify efficient matching of fixed-length strings at the end of subject
1870  pattern such as  strings. Consider a simple pattern such as
1871  <pre>  <pre>
1872    abcd$    abcd$
1873  </pre>  </pre>
# Line 1642  preceded by "foo", while Line 1925  preceded by "foo", while
1925  is another pattern that matches "foo" preceded by three digits and any three  is another pattern that matches "foo" preceded by three digits and any three
1926  characters that are not "999".  characters that are not "999".
1927  <a name="conditions"></a></P>  <a name="conditions"></a></P>
1928  <br><a name="SEC18" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>  <br><a name="SEC19" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>
1929  <P>  <P>
1930  It is possible to cause the matching process to obey a subpattern  It is possible to cause the matching process to obey a subpattern
1931  conditionally or to choose between two alternative subpatterns, depending on  conditionally or to choose between two alternative subpatterns, depending on
1932  the result of an assertion, or whether a previous capturing subpattern matched  the result of an assertion, or whether a specific capturing subpattern has
1933  or not. The two possible forms of conditional subpattern are  already been matched. The two possible forms of conditional subpattern are:
1934  <pre>  <pre>
1935    (?(condition)yes-pattern)    (?(condition)yes-pattern)
1936    (?(condition)yes-pattern|no-pattern)    (?(condition)yes-pattern|no-pattern)
1937  </pre>  </pre>
1938  If the condition is satisfied, the yes-pattern is used; otherwise the  If the condition is satisfied, the yes-pattern is used; otherwise the
1939  no-pattern (if present) is used. If there are more than two alternatives in the  no-pattern (if present) is used. If there are more than two alternatives in the
1940  subpattern, a compile-time error occurs.  subpattern, a compile-time error occurs. Each of the two alternatives may
1941    itself contain nested subpatterns of any form, including conditional
1942    subpatterns; the restriction to two alternatives applies only at the level of
1943    the condition. This pattern fragment is an example where the alternatives are
1944    complex:
1945    <pre>
1946      (?(1) (A|B|C) | (D | (?(2)E|F) | E) )
1947    
1948    </PRE>
1949  </P>  </P>
1950  <P>  <P>
1951  There are four kinds of condition: references to subpatterns, references to  There are four kinds of condition: references to subpatterns, references to
# Line 1665  Checking for a used subpattern by number Line 1956  Checking for a used subpattern by number
1956  </b><br>  </b><br>
1957  <P>  <P>
1958  If the text between the parentheses consists of a sequence of digits, the  If the text between the parentheses consists of a sequence of digits, the
1959  condition is true if the capturing subpattern of that number has previously  condition is true if a capturing subpattern of that number has previously
1960  matched. An alternative notation is to precede the digits with a plus or minus  matched. If there is more than one capturing subpattern with the same number
1961  sign. In this case, the subpattern number is relative rather than absolute.  (see the earlier
1962  The most recently opened parentheses can be referenced by (?(-1), the next most  <a href="#recursion">section about duplicate subpattern numbers),</a>
1963  recent by (?(-2), and so on. In looping constructs it can also make sense to  the condition is true if any of them have been set. An alternative notation is
1964  refer to subsequent groups with constructs such as (?(+2).  to precede the digits with a plus or minus sign. In this case, the subpattern
1965    number is relative rather than absolute. The most recently opened parentheses
1966    can be referenced by (?(-1), the next most recent by (?(-2), and so on. In
1967    looping constructs it can also make sense to refer to subsequent groups with
1968    constructs such as (?(+2).
1969  </P>  </P>
1970  <P>  <P>
1971  Consider the following pattern, which contains non-significant white space to  Consider the following pattern, which contains non-significant white space to
# Line 1714  names that consist entirely of digits is Line 2009  names that consist entirely of digits is
2009  Rewriting the above example to use a named subpattern gives this:  Rewriting the above example to use a named subpattern gives this:
2010  <pre>  <pre>
2011    (?&#60;OPEN&#62; \( )?    [^()]+    (?(&#60;OPEN&#62;) \) )    (?&#60;OPEN&#62; \( )?    [^()]+    (?(&#60;OPEN&#62;) \) )
2012    </pre>
2013  </PRE>  If the name used in a condition of this kind is a duplicate, the test is
2014    applied to all subpatterns of the same name, and is true if any one of them has
2015    matched.
2016  </P>  </P>
2017  <br><b>  <br><b>
2018  Checking for pattern recursion  Checking for pattern recursion
# Line 1728  letter R, for example: Line 2025  letter R, for example:
2025  <pre>  <pre>
2026    (?(R3)...) or (?(R&name)...)    (?(R3)...) or (?(R&name)...)
2027  </pre>  </pre>
2028  the condition is true if the most recent recursion is into the subpattern whose  the condition is true if the most recent recursion is into a subpattern whose
2029  number or name is given. This condition does not check the entire recursion  number or name is given. This condition does not check the entire recursion
2030  stack.  stack. If the name used in a condition of this kind is a duplicate, the test is
2031    applied to all subpatterns of the same name, and is true if any one of them is
2032    the most recent recursion.
2033  </P>  </P>
2034  <P>  <P>
2035  At "top level", all these recursion test conditions are false. Recursive  At "top level", all these recursion test conditions are false.
2036  patterns are described below.  <a href="#recursion">The syntax for recursive patterns</a>
2037    is described below.
2038  </P>  </P>
2039  <br><b>  <br><b>
2040  Defining subpatterns for use by reference only  Defining subpatterns for use by reference only
# Line 1744  If the condition is the string (DEFINE), Line 2044  If the condition is the string (DEFINE),
2044  name DEFINE, the condition is always false. In this case, there may be only one  name DEFINE, the condition is always false. In this case, there may be only one
2045  alternative in the subpattern. It is always skipped if control reaches this  alternative in the subpattern. It is always skipped if control reaches this
2046  point in the pattern; the idea of DEFINE is that it can be used to define  point in the pattern; the idea of DEFINE is that it can be used to define
2047  "subroutines" that can be referenced from elsewhere. (The use of "subroutines"  "subroutines" that can be referenced from elsewhere. (The use of
2048    <a href="#subpatternsassubroutines">"subroutines"</a>
2049  is described below.) For example, a pattern to match an IPv4 address could be  is described below.) For example, a pattern to match an IPv4 address could be
2050  written like this (ignore whitespace and line breaks):  written like this (ignore whitespace and line breaks):
2051  <pre>  <pre>
# Line 1754  written like this (ignore whitespace and Line 2055  written like this (ignore whitespace and
2055  The first part of the pattern is a DEFINE group inside which a another group  The first part of the pattern is a DEFINE group inside which a another group
2056  named "byte" is defined. This matches an individual component of an IPv4  named "byte" is defined. This matches an individual component of an IPv4
2057  address (a number less than 256). When matching takes place, this part of the  address (a number less than 256). When matching takes place, this part of the
2058  pattern is skipped because DEFINE acts like a false condition.  pattern is skipped because DEFINE acts like a false condition. The rest of the
2059  </P>  pattern uses references to the named group to match the four dot-separated
2060  <P>  components of an IPv4 address, insisting on a word boundary at each end.
 The rest of the pattern uses references to the named group to match the four  
 dot-separated components of an IPv4 address, insisting on a word boundary at  
 each end.  
2061  </P>  </P>
2062  <br><b>  <br><b>
2063  Assertion conditions  Assertion conditions
# Line 1780  subject is matched against the first alt Line 2078  subject is matched against the first alt
2078  against the second. This pattern matches strings in one of the two forms  against the second. This pattern matches strings in one of the two forms
2079  dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.  dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.
2080  <a name="comments"></a></P>  <a name="comments"></a></P>
2081  <br><a name="SEC19" href="#TOC1">COMMENTS</a><br>  <br><a name="SEC20" href="#TOC1">COMMENTS</a><br>
2082  <P>  <P>
2083  The sequence (?# marks the start of a comment that continues up to the next  There are two ways of including comments in patterns that are processed by
2084  closing parenthesis. Nested parentheses are not permitted. The characters  PCRE. In both cases, the start of the comment must not be in a character class,
2085  that make up a comment play no part in the pattern matching at all.  nor in the middle of any other sequence of related characters such as (?: or a
2086    subpattern name or number. The characters that make up a comment play no part
2087    in the pattern matching.
2088  </P>  </P>
2089  <P>  <P>
2090  If the PCRE_EXTENDED option is set, an unescaped # character outside a  The sequence (?# marks the start of a comment that continues up to the next
2091  character class introduces a comment that continues to immediately after the  closing parenthesis. Nested parentheses are not permitted. If the PCRE_EXTENDED
2092  next newline in the pattern.  option is set, an unescaped # character also introduces a comment, which in
2093    this case continues to immediately after the next newline character or
2094    character sequence in the pattern. Which characters are interpreted as newlines
2095    is controlled by the options passed to <b>pcre_compile()</b> or by a special
2096    sequence at the start of the pattern, as described in the section entitled
2097    <a href="#recursion">"Newline conventions"</a>
2098    above. Note that end of this type of comment is a literal newline sequence in
2099    the pattern; escape sequences that happen to represent a newline do not count.
2100    For example, consider this pattern when PCRE_EXTENDED is set, and the default
2101    newline convention is in force:
2102    <pre>
2103      abc #comment \n still comment
2104    </pre>
2105    On encountering the # character, <b>pcre_compile()</b> skips along, looking for
2106    a newline in the pattern. The sequence \n is still literal at this stage, so
2107    it does not terminate the comment. Only an actual character with the code value
2108    0x0a (the default newline) does so.
2109  <a name="recursion"></a></P>  <a name="recursion"></a></P>
2110  <br><a name="SEC20" href="#TOC1">RECURSIVE PATTERNS</a><br>  <br><a name="SEC21" href="#TOC1">RECURSIVE PATTERNS</a><br>
2111  <P>  <P>
2112  Consider the problem of matching a string in parentheses, allowing for  Consider the problem of matching a string in parentheses, allowing for
2113  unlimited nested parentheses. Without the use of recursion, the best that can  unlimited nested parentheses. Without the use of recursion, the best that can
# Line 1814  recursively to the pattern in which it a Line 2130  recursively to the pattern in which it a
2130  Obviously, PCRE cannot support the interpolation of Perl code. Instead, it  Obviously, PCRE cannot support the interpolation of Perl code. Instead, it
2131  supports special syntax for recursion of the entire pattern, and also for  supports special syntax for recursion of the entire pattern, and also for
2132  individual subpattern recursion. After its introduction in PCRE and Python,  individual subpattern recursion. After its introduction in PCRE and Python,
2133  this kind of recursion was introduced into Perl at release 5.10.  this kind of recursion was subsequently introduced into Perl at release 5.10.
2134  </P>  </P>
2135  <P>  <P>
2136  A special item that consists of (? followed by a number greater than zero and a  A special item that consists of (? followed by a number greater than zero and a
2137  closing parenthesis is a recursive call of the subpattern of the given number,  closing parenthesis is a recursive call of the subpattern of the given number,
2138  provided that it occurs inside that subpattern. (If not, it is a "subroutine"  provided that it occurs inside that subpattern. (If not, it is a
2139    <a href="#subpatternsassubroutines">"subroutine"</a>
2140  call, which is described in the next section.) The special item (?R) or (?0) is  call, which is described in the next section.) The special item (?R) or (?0) is
2141  a recursive call of the entire regular expression.  a recursive call of the entire regular expression.
2142  </P>  </P>
2143  <P>  <P>
 In PCRE (like Python, but unlike Perl), a recursive subpattern call is always  
 treated as an atomic group. That is, once it has matched some of the subject  
 string, it is never re-entered, even if it contains untried alternatives and  
 there is a subsequent matching failure.  
 </P>  
 <P>  
2144  This PCRE pattern solves the nested parentheses problem (assume the  This PCRE pattern solves the nested parentheses problem (assume the
2145  PCRE_EXTENDED option is set so that white space is ignored):  PCRE_EXTENDED option is set so that white space is ignored):
2146  <pre>  <pre>
2147    \( ( (?&#62;[^()]+) | (?R) )* \)    \( ( [^()]++ | (?R) )* \)
2148  </pre>  </pre>
2149  First it matches an opening parenthesis. Then it matches any number of  First it matches an opening parenthesis. Then it matches any number of
2150  substrings which can either be a sequence of non-parentheses, or a recursive  substrings which can either be a sequence of non-parentheses, or a recursive
2151  match of the pattern itself (that is, a correctly parenthesized substring).  match of the pattern itself (that is, a correctly parenthesized substring).
2152  Finally there is a closing parenthesis.  Finally there is a closing parenthesis. Note the use of a possessive quantifier
2153    to avoid backtracking into sequences of non-parentheses.
2154  </P>  </P>
2155  <P>  <P>
2156  If this were part of a larger pattern, you would not want to recurse the entire  If this were part of a larger pattern, you would not want to recurse the entire
2157  pattern, so instead you could use this:  pattern, so instead you could use this:
2158  <pre>  <pre>
2159    ( \( ( (?&#62;[^()]+) | (?1) )* \) )    ( \( ( [^()]++ | (?1) )* \) )
2160  </pre>  </pre>
2161  We have put the pattern into parentheses, and caused the recursion to refer to  We have put the pattern into parentheses, and caused the recursion to refer to
2162  them instead of the whole pattern.  them instead of the whole pattern.
2163  </P>  </P>
2164  <P>  <P>
2165  In a larger pattern, keeping track of parenthesis numbers can be tricky. This  In a larger pattern, keeping track of parenthesis numbers can be tricky. This
2166  is made easier by the use of relative references. (A Perl 5.10 feature.)  is made easier by the use of relative references (a Perl 5.10 feature).
2167  Instead of (?1) in the pattern above you can write (?-2) to refer to the second  Instead of (?1) in the pattern above you can write (?-2) to refer to the second
2168  most recently opened parentheses preceding the recursion. In other words, a  most recently opened parentheses preceding the recursion. In other words, a
2169  negative number counts capturing parentheses leftwards from the point at which  negative number counts capturing parentheses leftwards from the point at which
# Line 1861  it is encountered. Line 2173  it is encountered.
2173  It is also possible to refer to subsequently opened parentheses, by writing  It is also possible to refer to subsequently opened parentheses, by writing
2174  references such as (?+2). However, these cannot be recursive because the  references such as (?+2). However, these cannot be recursive because the
2175  reference is not inside the parentheses that are referenced. They are always  reference is not inside the parentheses that are referenced. They are always
2176  "subroutine" calls, as described in the next section.  <a href="#subpatternsassubroutines">"subroutine"</a>
2177    calls, as described in the next section.
2178  </P>  </P>
2179  <P>  <P>
2180  An alternative approach is to use named parentheses instead. The Perl syntax  An alternative approach is to use named parentheses instead. The Perl syntax
2181  for this is (?&name); PCRE's earlier syntax (?P&#62;name) is also supported. We  for this is (?&name); PCRE's earlier syntax (?P&#62;name) is also supported. We
2182  could rewrite the above example as follows:  could rewrite the above example as follows:
2183  <pre>  <pre>
2184    (?&#60;pn&#62; \( ( (?&#62;[^()]+) | (?&pn) )* \) )    (?&#60;pn&#62; \( ( [^()]++ | (?&pn) )* \) )
2185  </pre>  </pre>
2186  If there is more than one subpattern with the same name, the earliest one is  If there is more than one subpattern with the same name, the earliest one is
2187  used.  used.
2188  </P>  </P>
2189  <P>  <P>
2190  This particular example pattern that we have been looking at contains nested  This particular example pattern that we have been looking at contains nested
2191  unlimited repeats, and so the use of atomic grouping for matching strings of  unlimited repeats, and so the use of a possessive quantifier for matching
2192  non-parentheses is important when applying the pattern to strings that do not  strings of non-parentheses is important when applying the pattern to strings
2193  match. For example, when this pattern is applied to  that do not match. For example, when this pattern is applied to
2194  <pre>  <pre>
2195    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
2196  </pre>  </pre>
2197  it yields "no match" quickly. However, if atomic grouping is not used,  it yields "no match" quickly. However, if a possessive quantifier is not used,
2198  the match runs for a very long time indeed because there are so many different  the match runs for a very long time indeed because there are so many different
2199  ways the + and * repeats can carve up the subject, and all have to be tested  ways the + and * repeats can carve up the subject, and all have to be tested
2200  before failure can be reported.  before failure can be reported.
2201  </P>  </P>
2202  <P>  <P>
2203  At the end of a match, the values set for any capturing subpatterns are those  At the end of a match, the values of capturing parentheses are those from
2204  from the outermost level of the recursion at which the subpattern value is set.  the outermost level. If you want to obtain intermediate values, a callout
2205  If you want to obtain intermediate values, a callout function can be used (see  function can be used (see below and the
 below and the  
2206  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
2207  documentation). If the pattern above is matched against  documentation). If the pattern above is matched against
2208  <pre>  <pre>
2209    (ab(cd)ef)    (ab(cd)ef)
2210  </pre>  </pre>
2211  the value for the capturing parentheses is "ef", which is the last value taken  the value for the inner capturing parentheses (numbered 2) is "ef", which is
2212  on at the top level. If additional parentheses are added, giving  the last value taken on at the top level. If a capturing subpattern is not
2213  <pre>  matched at the top level, its final value is unset, even if it is (temporarily)
2214    \( ( ( (?&#62;[^()]+) | (?R) )* ) \)  set at a deeper level.
2215       ^                        ^  </P>
2216       ^                        ^  <P>
2217  </pre>  If there are more than 15 capturing parentheses in a pattern, PCRE has to
2218  the string they capture is "ab(cd)ef", the contents of the top level  obtain extra memory to store data during a recursion, which it does by using
2219  parentheses. If there are more than 15 capturing parentheses in a pattern, PCRE  <b>pcre_malloc</b>, freeing it via <b>pcre_free</b> afterwards. If no memory can
2220  has to obtain extra memory to store data during a recursion, which it does by  be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.
 using <b>pcre_malloc</b>, freeing it via <b>pcre_free</b> afterwards. If no  
 memory can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.  
2221  </P>  </P>
2222  <P>  <P>
2223  Do not confuse the (?R) item with the condition (R), which tests for recursion.  Do not confuse the (?R) item with the condition (R), which tests for recursion.
# Line 1920  recursing), whereas any characters are p Line 2230  recursing), whereas any characters are p
2230  In this pattern, (?(R) is the start of a conditional subpattern, with two  In this pattern, (?(R) is the start of a conditional subpattern, with two
2231  different alternatives for the recursive and non-recursive cases. The (?R) item  different alternatives for the recursive and non-recursive cases. The (?R) item
2232  is the actual recursive call.  is the actual recursive call.
2233    <a name="recursiondifference"></a></P>
2234    <br><b>
2235    Recursion difference from Perl
2236    </b><br>
2237    <P>
2238    In PCRE (like Python, but unlike Perl), a recursive subpattern call is always
2239    treated as an atomic group. That is, once it has matched some of the subject
2240    string, it is never re-entered, even if it contains untried alternatives and
2241    there is a subsequent matching failure. This can be illustrated by the
2242    following pattern, which purports to match a palindromic string that contains
2243    an odd number of characters (for example, "a", "aba", "abcba", "abcdcba"):
2244    <pre>
2245      ^(.|(.)(?1)\2)$
2246    </pre>
2247    The idea is that it either matches a single character, or two identical
2248    characters surrounding a sub-palindrome. In Perl, this pattern works; in PCRE
2249    it does not if the pattern is longer than three characters. Consider the
2250    subject string "abcba":
2251    </P>
2252    <P>
2253    At the top level, the first character is matched, but as it is not at the end
2254    of the string, the first alternative fails; the second alternative is taken
2255    and the recursion kicks in. The recursive call to subpattern 1 successfully
2256    matches the next character ("b"). (Note that the beginning and end of line
2257    tests are not part of the recursion).
2258    </P>
2259    <P>
2260    Back at the top level, the next character ("c") is compared with what
2261    subpattern 2 matched, which was "a". This fails. Because the recursion is
2262    treated as an atomic group, there are now no backtracking points, and so the
2263    entire match fails. (Perl is able, at this point, to re-enter the recursion and
2264    try the second alternative.) However, if the pattern is written with the
2265    alternatives in the other order, things are different:
2266    <pre>
2267      ^((.)(?1)\2|.)$
2268    </pre>
2269    This time, the recursing alternative is tried first, and continues to recurse
2270    until it runs out of characters, at which point the recursion fails. But this
2271    time we do have another alternative to try at the higher level. That is the big
2272    difference: in the previous case the remaining alternative is at a deeper
2273    recursion level, which PCRE cannot use.
2274    </P>
2275    <P>
2276    To change the pattern so that matches all palindromic strings, not just those
2277    with an odd number of characters, it is tempting to change the pattern to this:
2278    <pre>
2279      ^((.)(?1)\2|.?)$
2280    </pre>
2281    Again, this works in Perl, but not in PCRE, and for the same reason. When a
2282    deeper recursion has matched a single character, it cannot be entered again in
2283    order to match an empty string. The solution is to separate the two cases, and
2284    write out the odd and even cases as alternatives at the higher level:
2285    <pre>
2286      ^(?:((.)(?1)\2|)|((.)(?3)\4|.))
2287    </pre>
2288    If you want to match typical palindromic phrases, the pattern has to ignore all
2289    non-word characters, which can be done like this:
2290    <pre>
2291      ^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$
2292    </pre>
2293    If run with the PCRE_CASELESS option, this pattern matches phrases such as "A
2294    man, a plan, a canal: Panama!" and it works well in both PCRE and Perl. Note
2295    the use of the possessive quantifier *+ to avoid backtracking into sequences of
2296    non-word characters. Without this, PCRE takes a great deal longer (ten times or
2297    more) to match typical phrases, and Perl takes so long that you think it has
2298    gone into a loop.
2299    </P>
2300    <P>
2301    <b>WARNING</b>: The palindrome-matching patterns above work only if the subject
2302    string does not start with a palindrome that is shorter than the entire string.
2303    For example, although "abcba" is correctly matched, if the subject is "ababa",
2304    PCRE finds the palindrome "aba" at the start, then fails at top level because
2305    the end of the string does not follow. Once again, it cannot jump back into the
2306    recursion to try other alternatives, so the entire match fails.
2307  <a name="subpatternsassubroutines"></a></P>  <a name="subpatternsassubroutines"></a></P>
2308  <br><a name="SEC21" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>  <br><a name="SEC22" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>
2309  <P>  <P>
2310  If the syntax for a recursive subpattern reference (either by number or by  If the syntax for a recursive subpattern reference (either by number or by
2311  name) is used outside the parentheses to which it refers, it operates like a  name) is used outside the parentheses to which it refers, it operates like a
# Line 1946  is used, it does match "sense and respon Line 2330  is used, it does match "sense and respon
2330  strings. Another example is given in the discussion of DEFINE above.  strings. Another example is given in the discussion of DEFINE above.
2331  </P>  </P>
2332  <P>  <P>
2333  Like recursive subpatterns, a "subroutine" call is always treated as an atomic  Like recursive subpatterns, a subroutine call is always treated as an atomic
2334  group. That is, once it has matched some of the subject string, it is never  group. That is, once it has matched some of the subject string, it is never
2335  re-entered, even if it contains untried alternatives and there is a subsequent  re-entered, even if it contains untried alternatives and there is a subsequent
2336  matching failure.  matching failure. Any capturing parentheses that are set during the subroutine
2337    call revert to their previous values afterwards.
2338  </P>  </P>
2339  <P>  <P>
2340  When a subpattern is used as a subroutine, processing options such as  When a subpattern is used as a subroutine, processing options such as
# Line 1960  changed for different calls. For example Line 2345  changed for different calls. For example
2345  </pre>  </pre>
2346  It matches "abcabc". It does not match "abcABC" because the change of  It matches "abcabc". It does not match "abcABC" because the change of
2347  processing option does not affect the called subpattern.  processing option does not affect the called subpattern.
2348    <a name="onigurumasubroutines"></a></P>
2349    <br><a name="SEC23" href="#TOC1">ONIGURUMA SUBROUTINE SYNTAX</a><br>
2350    <P>
2351    For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
2352    a number enclosed either in angle brackets or single quotes, is an alternative
2353    syntax for referencing a subpattern as a subroutine, possibly recursively. Here
2354    are two of the examples used above, rewritten using this syntax:
2355    <pre>
2356      (?&#60;pn&#62; \( ( (?&#62;[^()]+) | \g&#60;pn&#62; )* \) )
2357      (sens|respons)e and \g'1'ibility
2358    </pre>
2359    PCRE supports an extension to Oniguruma: if a number is preceded by a
2360    plus or a minus sign it is taken as a relative reference. For example:
2361    <pre>
2362      (abc)(?i:\g&#60;-1&#62;)
2363    </pre>
2364    Note that \g{...} (Perl syntax) and \g&#60;...&#62; (Oniguruma syntax) are <i>not</i>
2365    synonymous. The former is a back reference; the latter is a subroutine call.
2366  </P>  </P>
2367  <br><a name="SEC22" href="#TOC1">CALLOUTS</a><br>  <br><a name="SEC24" href="#TOC1">CALLOUTS</a><br>
2368  <P>  <P>
2369  Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl  Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl
2370  code to be obeyed in the middle of matching a regular expression. This makes it  code to be obeyed in the middle of matching a regular expression. This makes it
# Line 1995  may cause matching to proceed, to backtr Line 2398  may cause matching to proceed, to backtr
2398  description of the interface to the callout function is given in the  description of the interface to the callout function is given in the
2399  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
2400  documentation.  documentation.
2401  </P>  <a name="backtrackcontrol"></a></P>
2402  <br><a name="SEC23" href="#TOC1">BACTRACKING CONTROL</a><br>  <br><a name="SEC25" href="#TOC1">BACKTRACKING CONTROL</a><br>
2403  <P>  <P>
2404  Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which  Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which
2405  are described in the Perl documentation as "experimental and subject to change  are described in the Perl documentation as "experimental and subject to change
# Line 2005  production code should be noted to avoid Line 2408  production code should be noted to avoid
2408  remarks apply to the PCRE features described in this section.  remarks apply to the PCRE features described in this section.
2409  </P>  </P>
2410  <P>  <P>
2411  Since these verbs are specifically related to backtracking, they can be used  Since these verbs are specifically related to backtracking, most of them can be
2412  only when the pattern is to be matched using <b>pcre_exec()</b>, which uses a  used only when the pattern is to be matched using <b>pcre_exec()</b>, which uses
2413  backtracking algorithm. They cause an error if encountered by  a backtracking algorithm. With the exception of (*FAIL), which behaves like a
2414    failing negative assertion, they cause an error if encountered by
2415  <b>pcre_dfa_exec()</b>.  <b>pcre_dfa_exec()</b>.
2416  </P>  </P>
2417  <P>  <P>
2418    If any of these verbs are used in an assertion or subroutine subpattern
2419    (including recursive subpatterns), their effect is confined to that subpattern;
2420    it does not extend to the surrounding pattern. Note that such subpatterns are
2421    processed as anchored at the point where they are tested.
2422    </P>
2423    <P>
2424  The new verbs make use of what was previously invalid syntax: an opening  The new verbs make use of what was previously invalid syntax: an opening
2425  parenthesis followed by an asterisk. In Perl, they are generally of the form  parenthesis followed by an asterisk. They are generally of the form
2426  (*VERB:ARG) but PCRE does not support the use of arguments, so its general  (*VERB) or (*VERB:NAME). Some may take either form, with differing behaviour,
2427  form is just (*VERB). Any number of these verbs may occur in a pattern. There  depending on whether or not an argument is present. An name is a sequence of
2428  are two kinds:  letters, digits, and underscores. If the name is empty, that is, if the closing
2429    parenthesis immediately follows the colon, the effect is as if the colon were
2430    not there. Any number of these verbs may occur in a pattern.
2431    </P>
2432    <P>
2433    PCRE contains some optimizations that are used to speed up matching by running
2434    some checks at the start of each match attempt. For example, it may know the
2435    minimum length of matching subject, or that a particular character must be
2436    present. When one of these optimizations suppresses the running of a match, any
2437    included backtracking verbs will not, of course, be processed. You can suppress
2438    the start-of-match optimizations by setting the PCRE_NO_START_OPTIMIZE option
2439    when calling <b>pcre_exec()</b>.
2440  </P>  </P>
2441  <br><b>  <br><b>
2442  Verbs that act immediately  Verbs that act immediately
2443  </b><br>  </b><br>
2444  <P>  <P>
2445  The following verbs act as soon as they are encountered:  The following verbs act as soon as they are encountered. They may not be
2446    followed by a name.
2447  <pre>  <pre>
2448     (*ACCEPT)     (*ACCEPT)
2449  </pre>  </pre>
2450  This verb causes the match to end successfully, skipping the remainder of the  This verb causes the match to end successfully, skipping the remainder of the
2451  pattern. When inside a recursion, only the innermost pattern is ended  pattern. When inside a recursion, only the innermost pattern is ended
2452  immediately. PCRE differs from Perl in what happens if the (*ACCEPT) is inside  immediately. If (*ACCEPT) is inside capturing parentheses, the data so far is
2453  capturing parentheses. In Perl, the data so far is captured: in PCRE no data is  captured. (This feature was added to PCRE at release 8.00.) For example:
 captured. For example:  
2454  <pre>  <pre>
2455    A(A|B(*ACCEPT)|C)D    A((?:A|B(*ACCEPT)|C)D)
2456  </pre>  </pre>
2457  This matches "AB", "AAD", or "ACD", but when it matches "AB", no data is  This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is captured by
2458  captured.  the outer parentheses.
2459  <pre>  <pre>
2460    (*FAIL) or (*F)    (*FAIL) or (*F)
2461  </pre>  </pre>
# Line 2050  A match with the string "aaaa" always fa Line 2471  A match with the string "aaaa" always fa
2471  each backtrack happens (in this example, 10 times).  each backtrack happens (in this example, 10 times).
2472  </P>  </P>
2473  <br><b>  <br><b>
2474    Recording which path was taken
2475    </b><br>
2476    <P>
2477    There is one verb whose main purpose is to track how a match was arrived at,
2478    though it also has a secondary use in conjunction with advancing the match
2479    starting point (see (*SKIP) below).
2480    <pre>
2481      (*MARK:NAME) or (*:NAME)
2482    </pre>
2483    A name is always required with this verb. There may be as many instances of
2484    (*MARK) as you like in a pattern, and their names do not have to be unique.
2485    </P>
2486    <P>
2487    When a match succeeds, the name of the last-encountered (*MARK) is passed back
2488    to the caller via the <i>pcre_extra</i> data structure, as described in the
2489    <a href="pcreapi.html#extradata">section on <i>pcre_extra</i></a>
2490    in the
2491    <a href="pcreapi.html"><b>pcreapi</b></a>
2492    documentation. No data is returned for a partial match. Here is an example of
2493    <b>pcretest</b> output, where the /K modifier requests the retrieval and
2494    outputting of (*MARK) data:
2495    <pre>
2496      /X(*MARK:A)Y|X(*MARK:B)Z/K
2497      XY
2498       0: XY
2499      MK: A
2500      XZ
2501       0: XZ
2502      MK: B
2503    </pre>
2504    The (*MARK) name is tagged with "MK:" in this output, and in this example it
2505    indicates which of the two alternatives matched. This is a more efficient way
2506    of obtaining this information than putting each alternative in its own
2507    capturing parentheses.
2508    </P>
2509    <P>
2510    A name may also be returned after a failed match if the final path through the
2511    pattern involves (*MARK). However, unless (*MARK) used in conjunction with
2512    (*COMMIT), this is unlikely to happen for an unanchored pattern because, as the
2513    starting point for matching is advanced, the final check is often with an empty
2514    string, causing a failure before (*MARK) is reached. For example:
2515    <pre>
2516      /X(*MARK:A)Y|X(*MARK:B)Z/K
2517      XP
2518      No match
2519    </pre>
2520    There are three potential starting points for this match (starting with X,
2521    starting with P, and with an empty string). If the pattern is anchored, the
2522    result is different:
2523    <pre>
2524      /^X(*MARK:A)Y|^X(*MARK:B)Z/K
2525      XP
2526      No match, mark = B
2527    </pre>
2528    PCRE's start-of-match optimizations can also interfere with this. For example,
2529    if, as a result of a call to <b>pcre_study()</b>, it knows the minimum
2530    subject length for a match, a shorter subject will not be scanned at all.
2531    </P>
2532    <P>
2533    Note that similar anomalies (though different in detail) exist in Perl, no
2534    doubt for the same reasons. The use of (*MARK) data after a failed match of an
2535    unanchored pattern is not recommended, unless (*COMMIT) is involved.
2536    </P>
2537    <br><b>
2538  Verbs that act after backtracking  Verbs that act after backtracking
2539  </b><br>  </b><br>
2540  <P>  <P>
2541  The following verbs do nothing when they are encountered. Matching continues  The following verbs do nothing when they are encountered. Matching continues
2542  with what follows, but if there is no subsequent match, a failure is forced.  with what follows, but if there is no subsequent match, causing a backtrack to
2543  The verbs differ in exactly what kind of failure occurs.  the verb, a failure is forced. That is, backtracking cannot pass to the left of
2544    the verb. However, when one of these verbs appears inside an atomic group, its
2545    effect is confined to that group, because once the group has been matched,
2546    there is never any backtracking into it. In this situation, backtracking can
2547    "jump back" to the left of the entire atomic group. (Remember also, as stated
2548    above, that this localization also applies in subroutine calls and assertions.)
2549    </P>
2550    <P>
2551    These verbs differ in exactly what kind of failure occurs when backtracking
2552    reaches them.
2553  <pre>  <pre>
2554    (*COMMIT)    (*COMMIT)
2555  </pre>  </pre>
2556  This verb causes the whole match to fail outright if the rest of the pattern  This verb, which may not be followed by a name, causes the whole match to fail
2557  does not match. Even if the pattern is unanchored, no further attempts to find  outright if the rest of the pattern does not match. Even if the pattern is
2558  a match by advancing the start point take place. Once (*COMMIT) has been  unanchored, no further attempts to find a match by advancing the starting point
2559  passed, <b>pcre_exec()</b> is committed to finding a match at the current  take place. Once (*COMMIT) has been passed, <b>pcre_exec()</b> is committed to
2560  starting point, or not at all. For example:  finding a match at the current starting point, or not at all. For example:
2561  <pre>  <pre>
2562    a+(*COMMIT)b    a+(*COMMIT)b
2563  </pre>  </pre>
2564  This matches "xxaab" but not "aacaab". It can be thought of as a kind of  This matches "xxaab" but not "aacaab". It can be thought of as a kind of
2565  dynamic anchor, or "I've started, so I must finish."  dynamic anchor, or "I've started, so I must finish." The name of the most
2566  <pre>  recently passed (*MARK) in the path is passed back when (*COMMIT) forces a
2567    (*PRUNE)  match failure.
2568  </pre>  </P>
2569  This verb causes the match to fail at the current position if the rest of the  <P>
2570  pattern does not match. If the pattern is unanchored, the normal "bumpalong"  Note that (*COMMIT) at the start of a pattern is not the same as an anchor,
2571  advance to the next starting character then happens. Backtracking can occur as  unless PCRE's start-of-match optimizations are turned off, as shown in this
2572  usual to the left of (*PRUNE), or when matching to the right of (*PRUNE), but  <b>pcretest</b> example:
2573  if there is no match to the right, backtracking cannot cross (*PRUNE).  <pre>
2574  In simple cases, the use of (*PRUNE) is just an alternative to an atomic    /(*COMMIT)abc/
2575  group or possessive quantifier, but there are some uses of (*PRUNE) that cannot    xyzabc
2576  be expressed in any other way.     0: abc
2577      xyzabc\Y
2578      No match
2579    </pre>
2580    PCRE knows that any match must start with "a", so the optimization skips along
2581    the subject to "a" before running the first match attempt, which succeeds. When
2582    the optimization is disabled by the \Y escape in the second subject, the match
2583    starts at "x" and so the (*COMMIT) causes it to fail without trying any other
2584    starting points.
2585    <pre>
2586      (*PRUNE) or (*PRUNE:NAME)
2587    </pre>
2588    This verb causes the match to fail at the current starting position in the
2589    subject if the rest of the pattern does not match. If the pattern is
2590    unanchored, the normal "bumpalong" advance to the next starting character then
2591    happens. Backtracking can occur as usual to the left of (*PRUNE), before it is
2592    reached, or when matching to the right of (*PRUNE), but if there is no match to
2593    the right, backtracking cannot cross (*PRUNE). In simple cases, the use of
2594    (*PRUNE) is just an alternative to an atomic group or possessive quantifier,
2595    but there are some uses of (*PRUNE) that cannot be expressed in any other way.
2596    The behaviour of (*PRUNE:NAME) is the same as (*MARK:NAME)(*PRUNE) when the
2597    match fails completely; the name is passed back if this is the final attempt.
2598    (*PRUNE:NAME) does not pass back a name if the match succeeds. In an anchored
2599    pattern (*PRUNE) has the same effect as (*COMMIT).
2600  <pre>  <pre>
2601    (*SKIP)    (*SKIP)
2602  </pre>  </pre>
2603  This verb is like (*PRUNE), except that if the pattern is unanchored, the  This verb, when given without a name, is like (*PRUNE), except that if the
2604  "bumpalong" advance is not to the next character, but to the position in the  pattern is unanchored, the "bumpalong" advance is not to the next character,
2605  subject where (*SKIP) was encountered. (*SKIP) signifies that whatever text  but to the position in the subject where (*SKIP) was encountered. (*SKIP)
2606  was matched leading up to it cannot be part of a successful match. Consider:  signifies that whatever text was matched leading up to it cannot be part of a
2607    successful match. Consider:
2608  <pre>  <pre>
2609    a+(*SKIP)b    a+(*SKIP)b
2610  </pre>  </pre>
2611  If the subject is "aaaac...", after the first match attempt fails (starting at  If the subject is "aaaac...", after the first match attempt fails (starting at
2612  the first character in the string), the starting point skips on to start the  the first character in the string), the starting point skips on to start the
2613  next attempt at "c". Note that a possessive quantifer does not have the same  next attempt at "c". Note that a possessive quantifer does not have the same
2614  effect in this example; although it would suppress backtracking during the  effect as this example; although it would suppress backtracking during the
2615  first match attempt, the second attempt would start at the second character  first match attempt, the second attempt would start at the second character
2616  instead of skipping on to "c".  instead of skipping on to "c".
2617  <pre>  <pre>
2618    (*THEN)    (*SKIP:NAME)
2619  </pre>  </pre>
2620  This verb causes a skip to the next alternation if the rest of the pattern does  When (*SKIP) has an associated name, its behaviour is modified. If the
2621  not match. That is, it cancels pending backtracking, but only within the  following pattern fails to match, the previous path through the pattern is
2622  current alternation. Its name comes from the observation that it can be used  searched for the most recent (*MARK) that has the same name. If one is found,
2623  for a pattern-based if-then-else block:  the "bumpalong" advance is to the subject position that corresponds to that
2624    (*MARK) instead of to where (*SKIP) was encountered. If no (*MARK) with a
2625    matching name is found, normal "bumpalong" of one character happens (the
2626    (*SKIP) is ignored).
2627    <pre>
2628      (*THEN) or (*THEN:NAME)
2629    </pre>
2630    This verb causes a skip to the next alternation in the innermost enclosing
2631    group if the rest of the pattern does not match. That is, it cancels pending
2632    backtracking, but only within the current alternation. Its name comes from the
2633    observation that it can be used for a pattern-based if-then-else block:
2634  <pre>  <pre>
2635    ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...    ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
2636  </pre>  </pre>
2637  If the COND1 pattern matches, FOO is tried (and possibly further items after  If the COND1 pattern matches, FOO is tried (and possibly further items after
2638  the end of the group if FOO succeeds); on failure the matcher skips to the  the end of the group if FOO succeeds); on failure the matcher skips to the
2639  second alternative and tries COND2, without backtracking into COND1. If (*THEN)  second alternative and tries COND2, without backtracking into COND1. The
2640  is used outside of any alternation, it acts exactly like (*PRUNE).  behaviour of (*THEN:NAME) is exactly the same as (*MARK:NAME)(*THEN) if the
2641    overall match fails. If (*THEN) is not directly inside an alternation, it acts
2642    like (*PRUNE).
2643    </P>
2644    <P>
2645    The above verbs provide four different "strengths" of control when subsequent
2646    matching fails. (*THEN) is the weakest, carrying on the match at the next
2647    alternation. (*PRUNE) comes next, failing the match at the current starting
2648    position, but allowing an advance to the next character (for an unanchored
2649    pattern). (*SKIP) is similar, except that the advance may be more than one
2650    character. (*COMMIT) is the strongest, causing the entire match to fail.
2651    </P>
2652    <P>
2653    If more than one is present in a pattern, the "stongest" one wins. For example,
2654    consider this pattern, where A, B, etc. are complex pattern fragments:
2655    <pre>
2656      (A(*COMMIT)B(*THEN)C|D)
2657    </pre>
2658    Once A has matched, PCRE is committed to this match, at the current starting
2659    position. If subsequently B matches, but C does not, the normal (*THEN) action
2660    of trying the next alternation (that is, D) does not happen because (*COMMIT)
2661    overrides.
2662  </P>  </P>
2663  <br><a name="SEC24" href="#TOC1">SEE ALSO</a><br>  <br><a name="SEC26" href="#TOC1">SEE ALSO</a><br>
2664  <P>  <P>
2665  <b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3), <b>pcre</b>(3).  <b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3),
2666    <b>pcresyntax</b>(3), <b>pcre</b>(3).
2667  </P>  </P>
2668  <br><a name="SEC25" href="#TOC1">AUTHOR</a><br>  <br><a name="SEC27" href="#TOC1">AUTHOR</a><br>
2669  <P>  <P>
2670  Philip Hazel  Philip Hazel
2671  <br>  <br>
# Line 2124  University Computing Service Line 2674  University Computing Service
2674  Cambridge CB2 3QH, England.  Cambridge CB2 3QH, England.
2675  <br>  <br>
2676  </P>  </P>
2677  <br><a name="SEC26" href="#TOC1">REVISION</a><br>  <br><a name="SEC28" href="#TOC1">REVISION</a><br>
2678  <P>  <P>
2679  Last updated: 09 August 2007  Last updated: 31 October 2010
2680  <br>  <br>
2681  Copyright &copy; 1997-2007 University of Cambridge.  Copyright &copy; 1997-2010 University of Cambridge.
2682  <br>  <br>
2683  <p>  <p>
2684  Return to the <a href="index.html">PCRE index page</a>.  Return to the <a href="index.html">PCRE index page</a>.

Legend:
Removed from v.211  
changed lines
  Added in v.567

  ViewVC Help
Powered by ViewVC 1.1.5