/[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 507 by ph10, Wed Mar 10 16:08:01 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)</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>
# Line 74  discussed in the Line 89  discussed in the
89  <a href="pcrematching.html"><b>pcrematching</b></a>  <a href="pcrematching.html"><b>pcrematching</b></a>
90  page.  page.
91  </P>  </P>
92  <br><a name="SEC2" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br>  <br><a name="SEC2" href="#TOC1">NEWLINE CONVENTIONS</a><br>
93    <P>
94    PCRE supports five different conventions for indicating line breaks in
95    strings: a single CR (carriage return) character, a single LF (linefeed)
96    character, the two-character sequence CRLF, any of the three preceding, or any
97    Unicode newline sequence. The
98    <a href="pcreapi.html"><b>pcreapi</b></a>
99    page has
100    <a href="pcreapi.html#newlines">further discussion</a>
101    about newlines, and shows how to set the newline convention in the
102    <i>options</i> arguments for the compiling and matching functions.
103    </P>
104    <P>
105    It is also possible to specify a newline convention by starting a pattern
106    string with one of the following five sequences:
107    <pre>
108      (*CR)        carriage return
109      (*LF)        linefeed
110      (*CRLF)      carriage return, followed by linefeed
111      (*ANYCRLF)   any of the three above
112      (*ANY)       all Unicode newline sequences
113    </pre>
114    These override the default and the options given to <b>pcre_compile()</b> or
115    <b>pcre_compile2()</b>. For example, on a Unix system where LF is the default
116    newline sequence, the pattern
117    <pre>
118      (*CR)a.b
119    </pre>
120    changes the convention to CR. That pattern matches "a\nb" because LF is no
121    longer a newline. Note that these special settings, which are not
122    Perl-compatible, are recognized only at the very start of a pattern, and that
123    they must be in upper case. If more than one of them is present, the last one
124    is used.
125    </P>
126    <P>
127    The newline convention does not affect what the \R escape sequence matches. By
128    default, this is any Unicode newline sequence, for Perl compatibility. However,
129    this can be changed; see the description of \R in the section entitled
130    <a href="#newlineseq">"Newline sequences"</a>
131    below. A change of \R setting can be combined with a change of newline
132    convention.
133    </P>
134    <br><a name="SEC3" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br>
135  <P>  <P>
136  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
137  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 188  a character class the only metacharacter
188  </pre>  </pre>
189  The following sections describe the use of each of the metacharacters.  The following sections describe the use of each of the metacharacters.
190  </P>  </P>
191  <br><a name="SEC3" href="#TOC1">BACKSLASH</a><br>  <br><a name="SEC4" href="#TOC1">BACKSLASH</a><br>
192  <P>  <P>
193  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
194  non-alphanumeric character, it takes away any special meaning that character  non-alphanumeric character, it takes away any special meaning that character
# Line 172  Non-printing characters Line 229  Non-printing characters
229  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
230  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
231  non-printing characters, apart from the binary zero that terminates a pattern,  non-printing characters, apart from the binary zero that terminates a pattern,
232  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
233  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:  
234  <pre>  <pre>
235    \a        alarm, that is, the BEL character (hex 07)    \a        alarm, that is, the BEL character (hex 07)
236    \cx       "control-x", where x is any character    \cx       "control-x", where x is any character
237    \e        escape (hex 1B)    \e        escape (hex 1B)
238    \f        formfeed (hex 0C)    \f        formfeed (hex 0C)
239    \n        newline (hex 0A)    \n        linefeed (hex 0A)
240    \r        carriage return (hex 0D)    \r        carriage return (hex 0D)
241    \t        tab (hex 09)    \t        tab (hex 09)
242    \ddd      character with octal code ddd, or backreference    \ddd      character with octal code ddd, or back reference
243    \xhh      character with hex code hh    \xhh      character with hex code hh
244    \x{hhh..} character with hex code hhh..    \x{hhh..} character with hex code hhh..
245  </pre>  </pre>
# Line 270  following the discussion of Line 326  following the discussion of
326  <a href="#subpattern">parenthesized subpatterns.</a>  <a href="#subpattern">parenthesized subpatterns.</a>
327  </P>  </P>
328  <br><b>  <br><b>
329    Absolute and relative subroutine calls
330    </b><br>
331    <P>
332    For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
333    a number enclosed either in angle brackets or single quotes, is an alternative
334    syntax for referencing a subpattern as a "subroutine". Details are discussed
335    <a href="#onigurumasubroutines">later.</a>
336    Note that \g{...} (Perl syntax) and \g&#60;...&#62; (Oniguruma syntax) are <i>not</i>
337    synonymous. The former is a back reference; the latter is a
338    <a href="#subpatternsassubroutines">subroutine</a>
339    call.
340    </P>
341    <br><b>
342  Generic character types  Generic character types
343  </b><br>  </b><br>
344  <P>  <P>
# Line 308  In UTF-8 mode, characters with values gr Line 377  In UTF-8 mode, characters with values gr
377  \w, and always match \D, \S, and \W. This is true even when Unicode  \w, and always match \D, \S, and \W. This is true even when Unicode
378  character property support is available. These sequences retain their original  character property support is available. These sequences retain their original
379  meanings from before UTF-8 support was available, mainly for efficiency  meanings from before UTF-8 support was available, mainly for efficiency
380  reasons.  reasons. Note that this also affects \b, because it is defined in terms of \w
381    and \W.
382  </P>  </P>
383  <P>  <P>
384  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
# Line 358  page). For example, in a French locale s Line 428  page). For example, in a French locale s
428  or "french" in Windows, some character codes greater than 128 are used for  or "french" in Windows, some character codes greater than 128 are used for
429  accented letters, and these are matched by \w. The use of locales with Unicode  accented letters, and these are matched by \w. The use of locales with Unicode
430  is discouraged.  is discouraged.
431  </P>  <a name="newlineseq"></a></P>
432  <br><b>  <br><b>
433  Newline sequences  Newline sequences
434  </b><br>  </b><br>
435  <P>  <P>
436  Outside a character class, the escape sequence \R matches any Unicode newline  Outside a character class, by default, the escape sequence \R matches any
437  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
438  the following:  equivalent to the following:
439  <pre>  <pre>
440    (?&#62;\r\n|\n|\x0b|\f|\r|\x85)    (?&#62;\r\n|\n|\x0b|\f|\r|\x85)
441  </pre>  </pre>
# Line 384  Unicode character property support is no Line 454  Unicode character property support is no
454  recognized.  recognized.
455  </P>  </P>
456  <P>  <P>
457    It is possible to restrict \R to match only CR, LF, or CRLF (instead of the
458    complete set of Unicode line endings) by setting the option PCRE_BSR_ANYCRLF
459    either at compile time or when the pattern is matched. (BSR is an abbrevation
460    for "backslash R".) This can be made the default when PCRE is built; if this is
461    the case, the other behaviour can be requested via the PCRE_BSR_UNICODE option.
462    It is also possible to specify these settings by starting a pattern string with
463    one of the following sequences:
464    <pre>
465      (*BSR_ANYCRLF)   CR, LF, or CRLF only
466      (*BSR_UNICODE)   any Unicode newline sequence
467    </pre>
468    These override the default and the options given to <b>pcre_compile()</b> or
469    <b>pcre_compile2()</b>, but they can be overridden by options given to
470    <b>pcre_exec()</b> or <b>pcre_dfa_exec()</b>. Note that these special settings,
471    which are not Perl-compatible, are recognized only at the very start of a
472    pattern, and that they must be in upper case. If more than one of them is
473    present, the last one is used. They can be combined with a change of newline
474    convention, for example, a pattern can start with:
475    <pre>
476      (*ANY)(*BSR_ANYCRLF)
477    </pre>
478  Inside a character class, \R matches the letter "R".  Inside a character class, \R matches the letter "R".
479  <a name="uniextseq"></a></P>  <a name="uniextseq"></a></P>
480  <br><b>  <br><b>
# Line 420  Those that are not part of an identified Line 511  Those that are not part of an identified
511  <P>  <P>
512  Arabic,  Arabic,
513  Armenian,  Armenian,
514    Avestan,
515  Balinese,  Balinese,
516    Bamum,
517  Bengali,  Bengali,
518  Bopomofo,  Bopomofo,
519  Braille,  Braille,
520  Buginese,  Buginese,
521  Buhid,  Buhid,
522  Canadian_Aboriginal,  Canadian_Aboriginal,
523    Carian,
524    Cham,
525  Cherokee,  Cherokee,
526  Common,  Common,
527  Coptic,  Coptic,
# Line 435  Cypriot, Line 530  Cypriot,
530  Cyrillic,  Cyrillic,
531  Deseret,  Deseret,
532  Devanagari,  Devanagari,
533    Egyptian_Hieroglyphs,
534  Ethiopic,  Ethiopic,
535  Georgian,  Georgian,
536  Glagolitic,  Glagolitic,
# Line 447  Hangul, Line 543  Hangul,
543  Hanunoo,  Hanunoo,
544  Hebrew,  Hebrew,
545  Hiragana,  Hiragana,
546    Imperial_Aramaic,
547  Inherited,  Inherited,
548    Inscriptional_Pahlavi,
549    Inscriptional_Parthian,
550    Javanese,
551    Kaithi,
552  Kannada,  Kannada,
553  Katakana,  Katakana,
554    Kayah_Li,
555  Kharoshthi,  Kharoshthi,
556  Khmer,  Khmer,
557  Lao,  Lao,
558  Latin,  Latin,
559    Lepcha,
560  Limbu,  Limbu,
561  Linear_B,  Linear_B,
562    Lisu,
563    Lycian,
564    Lydian,
565  Malayalam,  Malayalam,
566    Meetei_Mayek,
567  Mongolian,  Mongolian,
568  Myanmar,  Myanmar,
569  New_Tai_Lue,  New_Tai_Lue,
# Line 464  Nko, Line 571  Nko,
571  Ogham,  Ogham,
572  Old_Italic,  Old_Italic,
573  Old_Persian,  Old_Persian,
574    Old_South_Arabian,
575    Old_Turkic,
576    Ol_Chiki,
577  Oriya,  Oriya,
578  Osmanya,  Osmanya,
579  Phags_Pa,  Phags_Pa,
580  Phoenician,  Phoenician,
581    Rejang,
582  Runic,  Runic,
583    Samaritan,
584    Saurashtra,
585  Shavian,  Shavian,
586  Sinhala,  Sinhala,
587    Sundanese,
588  Syloti_Nagri,  Syloti_Nagri,
589  Syriac,  Syriac,
590  Tagalog,  Tagalog,
591  Tagbanwa,  Tagbanwa,
592  Tai_Le,  Tai_Le,
593    Tai_Tham,
594    Tai_Viet,
595  Tamil,  Tamil,
596  Telugu,  Telugu,
597  Thaana,  Thaana,
# Line 483  Thai, Line 599  Thai,
599  Tibetan,  Tibetan,
600  Tifinagh,  Tifinagh,
601  Ugaritic,  Ugaritic,
602    Vai,
603  Yi.  Yi.
604  </P>  </P>
605  <P>  <P>
# Line 556  U+DFFF. Such characters are not valid in Line 673  U+DFFF. Such characters are not valid in
673  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
674  (see the discussion of PCRE_NO_UTF8_CHECK in the  (see the discussion of PCRE_NO_UTF8_CHECK in the
675  <a href="pcreapi.html"><b>pcreapi</b></a>  <a href="pcreapi.html"><b>pcreapi</b></a>
676  page).  page). Perl does not support the Cs property.
677  </P>  </P>
678  <P>  <P>
679  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})
680  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
681  properties with "Is".  properties with "Is".
682  </P>  </P>
# Line 614  For example, when the pattern Line 731  For example, when the pattern
731    (foo)\Kbar    (foo)\Kbar
732  </pre>  </pre>
733  matches "foobar", the first substring is still set to "foo".  matches "foobar", the first substring is still set to "foo".
734    </P>
735    <P>
736    Perl documents that the use of \K within assertions is "not well defined". In
737    PCRE, \K is acted upon when it occurs inside positive assertions, but is
738    ignored in negative assertions.
739  <a name="smallassertions"></a></P>  <a name="smallassertions"></a></P>
740  <br><b>  <br><b>
741  Simple assertions  Simple assertions
# Line 641  different meaning, namely the backspace Line 763  different meaning, namely the backspace
763  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
764  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
765  \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
766  first or last character matches \w, respectively.  first or last character matches \w, respectively. Neither PCRE nor Perl has a
767    separte "start of word" or "end of word" metasequence. However, whatever
768    follows \b normally determines which it is. For example, the fragment
769    \ba matches "a" at the start of a word.
770  </P>  </P>
771  <P>  <P>
772  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 800  If all the alternatives of a pattern beg
800  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
801  regular expression.  regular expression.
802  </P>  </P>
803  <br><a name="SEC4" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>  <br><a name="SEC5" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>
804  <P>  <P>
805  Outside a character class, in the default matching mode, the circumflex  Outside a character class, in the default matching mode, the circumflex
806  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 729  Note that the sequences \A, \Z, and \z c Line 854  Note that the sequences \A, \Z, and \z c
854  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
855  \A it is always anchored, whether or not PCRE_MULTILINE is set.  \A it is always anchored, whether or not PCRE_MULTILINE is set.
856  </P>  </P>
857  <br><a name="SEC5" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br>  <br><a name="SEC6" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br>
858  <P>  <P>
859  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
860  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 879  The handling of dot is entirely independ
879  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
880  special meaning in a character class.  special meaning in a character class.
881  </P>  </P>
882  <br><a name="SEC6" href="#TOC1">MATCHING A SINGLE BYTE</a><br>  <br><a name="SEC7" href="#TOC1">MATCHING A SINGLE BYTE</a><br>
883  <P>  <P>
884  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
885  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 894  PCRE does not allow \C to appear in look
894  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
895  the lookbehind.  the lookbehind.
896  <a name="characterclass"></a></P>  <a name="characterclass"></a></P>
897  <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>
898  <P>  <P>
899  An opening square bracket introduces a character class, terminated by a closing  An opening square bracket introduces a character class, terminated by a closing
900  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.
901  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
902  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
903  escaped with a backslash.  a member of the class, it should be the first data character in the class
904    (after an initial circumflex, if present) or escaped with a backslash.
905  </P>  </P>
906  <P>  <P>
907  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
908  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
909  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
910  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
911  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
912  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 917  For example, the character class [aeiou]
917  [^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
918  circumflex is just a convenient notation for specifying the characters that  circumflex is just a convenient notation for specifying the characters that
919  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
920  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
921  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
922  string.  string.
923  </P>  </P>
# Line 807  caseful version would. In UTF-8 mode, PC Line 933  caseful version would. In UTF-8 mode, PC
933  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
934  always possible. For characters with higher values, the concept of case is  always possible. For characters with higher values, the concept of case is
935  supported if PCRE is compiled with Unicode property support, but not otherwise.  supported if PCRE is compiled with Unicode property support, but not otherwise.
936  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,
937  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
938  UTF-8 support.  with UTF-8 support.
939  </P>  </P>
940  <P>  <P>
941  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 864  introducing a POSIX class name - see the Line 990  introducing a POSIX class name - see the
990  closing square bracket. However, escaping other non-alphanumeric characters  closing square bracket. However, escaping other non-alphanumeric characters
991  does no harm.  does no harm.
992  </P>  </P>
993  <br><a name="SEC8" href="#TOC1">POSIX CHARACTER CLASSES</a><br>  <br><a name="SEC9" href="#TOC1">POSIX CHARACTER CLASSES</a><br>
994  <P>  <P>
995  Perl supports the POSIX notation for character classes. This uses names  Perl supports the POSIX notation for character classes. This uses names
996  enclosed by [: and :] within the enclosing square brackets. PCRE also supports  enclosed by [: and :] within the enclosing square brackets. PCRE also supports
# Line 910  supported, and an error is given if they Line 1036  supported, and an error is given if they
1036  In UTF-8 mode, characters with values greater than 128 do not match any of  In UTF-8 mode, characters with values greater than 128 do not match any of
1037  the POSIX character classes.  the POSIX character classes.
1038  </P>  </P>
1039  <br><a name="SEC9" href="#TOC1">VERTICAL BAR</a><br>  <br><a name="SEC10" href="#TOC1">VERTICAL BAR</a><br>
1040  <P>  <P>
1041  Vertical bar characters are used to separate alternative patterns. For example,  Vertical bar characters are used to separate alternative patterns. For example,
1042  the pattern  the pattern
# Line 925  that succeeds is used. If the alternativ Line 1051  that succeeds is used. If the alternativ
1051  "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
1052  alternative in the subpattern.  alternative in the subpattern.
1053  </P>  </P>
1054  <br><a name="SEC10" href="#TOC1">INTERNAL OPTION SETTING</a><br>  <br><a name="SEC11" href="#TOC1">INTERNAL OPTION SETTING</a><br>
1055  <P>  <P>
1056  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
1057  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
1058  Perl option letters enclosed between "(?" and ")". The option letters are  the pattern by a sequence of Perl option letters enclosed between "(?" and ")".
1059    The option letters are
1060  <pre>  <pre>
1061    i  for PCRE_CASELESS    i  for PCRE_CASELESS
1062    m  for PCRE_MULTILINE    m  for PCRE_MULTILINE
# Line 944  permitted. If a letter appears both befo Line 1071  permitted. If a letter appears both befo
1071  unset.  unset.
1072  </P>  </P>
1073  <P>  <P>
1074  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
1075  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
1076  If the change is placed right at the start of a pattern, PCRE extracts it into  J, U and X respectively.
1077  the global options (and it will therefore show up in data extracted by the  </P>
1078  <b>pcre_fullinfo()</b> function).  <P>
1079    When one of these option changes occurs at top level (that is, not inside
1080    subpattern parentheses), the change applies to the remainder of the pattern
1081    that follows. If the change is placed right at the start of a pattern, PCRE
1082    extracts it into the global options (and it will therefore show up in data
1083    extracted by the <b>pcre_fullinfo()</b> function).
1084  </P>  </P>
1085  <P>  <P>
1086  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 1101  option settings happen at compile time.
1101  behaviour otherwise.  behaviour otherwise.
1102  </P>  </P>
1103  <P>  <P>
1104  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
1105  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
1106  J, U and X respectively.  pattern can contain special leading sequences such as (*CRLF) to override what
1107    the application has set or what has been defaulted. Details are given in the
1108    section entitled
1109    <a href="#newlineseq">"Newline sequences"</a>
1110    above. There is also the (*UTF8) leading sequence that can be used to set UTF-8
1111    mode; this is equivalent to setting the PCRE_UTF8 option.
1112  <a name="subpattern"></a></P>  <a name="subpattern"></a></P>
1113  <br><a name="SEC11" href="#TOC1">SUBPATTERNS</a><br>  <br><a name="SEC12" href="#TOC1">SUBPATTERNS</a><br>
1114  <P>  <P>
1115  Subpatterns are delimited by parentheses (round brackets), which can be nested.  Subpatterns are delimited by parentheses (round brackets), which can be nested.
1116  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 1163  match exactly the same set of strings. B
1163  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
1164  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
1165  the above patterns match "SUNDAY" as well as "Saturday".  the above patterns match "SUNDAY" as well as "Saturday".
1166  </P>  <a name="dupsubpatternnumber"></a></P>
1167  <br><a name="SEC12" href="#TOC1">DUPLICATE SUBPATTERN NUMBERS</a><br>  <br><a name="SEC13" href="#TOC1">DUPLICATE SUBPATTERN NUMBERS</a><br>
1168  <P>  <P>
1169  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
1170  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 1188  stored.
1188    / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x    / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
1189    # 1            2         2  3        2     3     4    # 1            2         2  3        2     3     4
1190  </pre>  </pre>
1191  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
1192  the first one in the pattern with the given number.  set for that number by any subpattern. The following pattern matches "abcabc"
1193    or "defdef":
1194    <pre>
1195      /(?|(abc)|(def))\1/
1196    </pre>
1197    In contrast, a recursive or "subroutine" call to a numbered subpattern always
1198    refers to the first one in the pattern with the given number. The following
1199    pattern matches "abcabc" or "defabc":
1200    <pre>
1201      /(?|(abc)|(def))(?1)/
1202    </pre>
1203    If a
1204    <a href="#conditions">condition test</a>
1205    for a subpattern's having matched refers to a non-unique number, the test is
1206    true if any of the subpatterns of that number have matched.
1207  </P>  </P>
1208  <P>  <P>
1209  An alternative approach to using this "branch reset" feature is to use  An alternative approach to using this "branch reset" feature is to use
1210  duplicate named subpatterns, as described in the next section.  duplicate named subpatterns, as described in the next section.
1211  </P>  </P>
1212  <br><a name="SEC13" href="#TOC1">NAMED SUBPATTERNS</a><br>  <br><a name="SEC14" href="#TOC1">NAMED SUBPATTERNS</a><br>
1213  <P>  <P>
1214  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
1215  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 1217  if an expression is modified, the number
1217  difficulty, PCRE supports the naming of subpatterns. This feature was not  difficulty, PCRE supports the naming of subpatterns. This feature was not
1218  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
1219  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
1220  the Perl and the Python syntax.  the Perl and the Python syntax. Perl allows identically numbered subpatterns to
1221    have different names, but PCRE does not.
1222  </P>  </P>
1223  <P>  <P>
1224  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
1225  (?'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
1226  parentheses from other parts of the pattern, such as  parentheses from other parts of the pattern, such as
1227  <a href="#backreferences">backreferences,</a>  <a href="#backreferences">back references,</a>
1228  <a href="#recursion">recursion,</a>  <a href="#recursion">recursion,</a>
1229  and  and
1230  <a href="#conditions">conditions,</a>  <a href="#conditions">conditions,</a>
# Line 1087  is also a convenience function for extra Line 1239  is also a convenience function for extra
1239  </P>  </P>
1240  <P>  <P>
1241  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
1242  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
1243  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
1244  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
1245  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
1246  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
1247    name, and in both cases you want to extract the abbreviation. This pattern
1248    (ignoring the line breaks) does the job:
1249  <pre>  <pre>
1250    (?&#60;DN&#62;Mon|Fri|Sun)(?:day)?|    (?&#60;DN&#62;Mon|Fri|Sun)(?:day)?|
1251    (?&#60;DN&#62;Tue)(?:sday)?|    (?&#60;DN&#62;Tue)(?:sday)?|
# Line 1106  subpattern, as described in the previous Line 1260  subpattern, as described in the previous
1260  <P>  <P>
1261  The convenience function for extracting the data by name returns the substring  The convenience function for extracting the data by name returns the substring
1262  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
1263  matched. This saves searching to find which numbered subpattern it was. If you  matched. This saves searching to find which numbered subpattern it was.
1264  make a reference to a non-unique named subpattern from elsewhere in the  </P>
1265  pattern, the one that corresponds to the lowest number is used. For further  <P>
1266  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
1267    the pattern, the one that corresponds to the first occurrence of the name is
1268    used. In the absence of duplicate numbers (see the previous section) this is
1269    the one with the lowest number. If you use a named reference in a condition
1270    test (see the
1271    <a href="#conditions">section about conditions</a>
1272    below), either to check whether a subpattern has matched, or to check for
1273    recursion, all subpatterns with the same name are tested. If the condition is
1274    true for any one of them, the overall condition is true. This is the same
1275    behaviour as testing by number. For further details of the interfaces for
1276    handling named subpatterns, see the
1277  <a href="pcreapi.html"><b>pcreapi</b></a>  <a href="pcreapi.html"><b>pcreapi</b></a>
1278  documentation.  documentation.
1279  </P>  </P>
1280  <br><a name="SEC14" href="#TOC1">REPETITION</a><br>  <P>
1281    <b>Warning:</b> You cannot use different names to distinguish between two
1282    subpatterns with the same number because PCRE uses only the numbers when
1283    matching. For this reason, an error is given at compile time if different names
1284    are given to subpatterns with the same number. However, you can give the same
1285    name to subpatterns with the same number, even when PCRE_DUPNAMES is not set.
1286    </P>
1287    <br><a name="SEC15" href="#TOC1">REPETITION</a><br>
1288  <P>  <P>
1289  Repetition is specified by quantifiers, which can follow any of the following  Repetition is specified by quantifiers, which can follow any of the following
1290  items:  items:
# Line 1127  items: Line 1298  items:
1298    a character class    a character class
1299    a back reference (see next section)    a back reference (see next section)
1300    a parenthesized subpattern (unless it is an assertion)    a parenthesized subpattern (unless it is an assertion)
1301      a recursive or "subroutine" call to a subpattern
1302  </pre>  </pre>
1303  The general repetition quantifier specifies a minimum and maximum number of  The general repetition quantifier specifies a minimum and maximum number of
1304  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 1332  which may be several bytes long (and the
1332  </P>  </P>
1333  <P>  <P>
1334  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
1335  previous item and the quantifier were not present.  previous item and the quantifier were not present. This may be useful for
1336    subpatterns that are referenced as
1337    <a href="#subpatternsassubroutines">subroutines</a>
1338    from elsewhere in the pattern. Items other than subpatterns that have a {0}
1339    quantifier are omitted from the compiled pattern.
1340  </P>  </P>
1341  <P>  <P>
1342  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 1416  alternatively using ^ to indicate anchor
1416  </P>  </P>
1417  <P>  <P>
1418  However, there is one situation where the optimization cannot be used. When .*  However, there is one situation where the optimization cannot be used. When .*
1419  is inside capturing parentheses that are the subject of a backreference  is inside capturing parentheses that are the subject of a back reference
1420  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
1421  succeeds. Consider, for example:  succeeds. Consider, for example:
1422  <pre>  <pre>
# Line 1264  example, after Line 1440  example, after
1440  </pre>  </pre>
1441  matches "aba" the value of the second captured substring is "b".  matches "aba" the value of the second captured substring is "b".
1442  <a name="atomicgroup"></a></P>  <a name="atomicgroup"></a></P>
1443  <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>
1444  <P>  <P>
1445  With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")  With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
1446  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 1544  an atomic group, like this:
1544  </pre>  </pre>
1545  sequences of non-digits cannot be broken, and failure happens quickly.  sequences of non-digits cannot be broken, and failure happens quickly.
1546  <a name="backreferences"></a></P>  <a name="backreferences"></a></P>
1547  <br><a name="SEC16" href="#TOC1">BACK REFERENCES</a><br>  <br><a name="SEC17" href="#TOC1">BACK REFERENCES</a><br>
1548  <P>  <P>
1549  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
1550  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 1630  after the reference.
1630  <P>  <P>
1631  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
1632  subpattern has not actually been used in a particular match, any back  subpattern has not actually been used in a particular match, any back
1633  references to it always fail. For example, the pattern  references to it always fail by default. For example, the pattern
1634  <pre>  <pre>
1635    (a|(bc))\2    (a|(bc))\2
1636  </pre>  </pre>
1637  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
1638  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
1639  taken as part of a potential back reference number. If the pattern continues  unset value matches an empty string.
1640  with a digit character, some delimiter must be used to terminate the back  </P>
1641  reference. If the PCRE_EXTENDED option is set, this can be whitespace.  <P>
1642  Otherwise an empty comment (see  Because there may be many capturing parentheses in a pattern, all digits
1643    following a backslash are taken as part of a potential back reference number.
1644    If the pattern continues with a digit character, some delimiter must be used to
1645    terminate the back reference. If the PCRE_EXTENDED option is set, this can be
1646    whitespace. Otherwise, the \g{ syntax or an empty comment (see
1647  <a href="#comments">"Comments"</a>  <a href="#comments">"Comments"</a>
1648  below) can be used.  below) can be used.
1649  </P>  </P>
1650    <br><b>
1651    Recursive back references
1652    </b><br>
1653  <P>  <P>
1654  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
1655  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 1664  to the previous iteration. In order for
1664  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
1665  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
1666  minimum of zero.  minimum of zero.
1667    </P>
1668    <P>
1669    Back references of this type cause the group that they reference to be treated
1670    as an
1671    <a href="#atomicgroup">atomic group.</a>
1672    Once the whole group has been matched, a subsequent matching failure cannot
1673    cause backtracking into the middle of the group.
1674  <a name="bigassertions"></a></P>  <a name="bigassertions"></a></P>
1675  <br><a name="SEC17" href="#TOC1">ASSERTIONS</a><br>  <br><a name="SEC18" href="#TOC1">ASSERTIONS</a><br>
1676  <P>  <P>
1677  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
1678  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 1721  lookbehind assertion is needed to achiev
1721  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
1722  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
1723  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.
1724    The Perl 5.10 backtracking control verb (*FAIL) or (*F) is essentially a
1725    synonym for (?!).
1726  <a name="lookbehind"></a></P>  <a name="lookbehind"></a></P>
1727  <br><b>  <br><b>
1728  Lookbehind assertions  Lookbehind assertions
# Line 1554  is permitted, but Line 1746  is permitted, but
1746  </pre>  </pre>
1747  causes an error at compile time. Branches that match different length strings  causes an error at compile time. Branches that match different length strings
1748  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
1749  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
1750  match the same length of string. An assertion such as  match the same length of string. An assertion such as
1751  <pre>  <pre>
1752    (?&#60;=ab(c|de))    (?&#60;=ab(c|de))
1753  </pre>  </pre>
1754  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
1755  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
1756    branches:
1757  <pre>  <pre>
1758    (?&#60;=abc|abde)    (?&#60;=abc|abde)
1759  </pre>  </pre>
1760  In some cases, the Perl 5.10 escape sequence \K  In some cases, the Perl 5.10 escape sequence \K
1761  <a href="#resetmatchstart">(see above)</a>  <a href="#resetmatchstart">(see above)</a>
1762  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
1763  fixed-length.  restriction.
1764  </P>  </P>
1765  <P>  <P>
1766  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 1775  the length of the lookbehind. The \X and
1775  different numbers of bytes, are also not permitted.  different numbers of bytes, are also not permitted.
1776  </P>  </P>
1777  <P>  <P>
1778    <a href="#subpatternsassubroutines">"Subroutine"</a>
1779    calls (see below) such as (?2) or (?&X) are permitted in lookbehinds, as long
1780    as the subpattern matches a fixed-length string.
1781    <a href="#recursion">Recursion,</a>
1782    however, is not supported.
1783    </P>
1784    <P>
1785  Possessive quantifiers can be used in conjunction with lookbehind assertions to  Possessive quantifiers can be used in conjunction with lookbehind assertions to
1786  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
1787  pattern such as  strings. Consider a simple pattern such as
1788  <pre>  <pre>
1789    abcd$    abcd$
1790  </pre>  </pre>
# Line 1642  preceded by "foo", while Line 1842  preceded by "foo", while
1842  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
1843  characters that are not "999".  characters that are not "999".
1844  <a name="conditions"></a></P>  <a name="conditions"></a></P>
1845  <br><a name="SEC18" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>  <br><a name="SEC19" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>
1846  <P>  <P>
1847  It is possible to cause the matching process to obey a subpattern  It is possible to cause the matching process to obey a subpattern
1848  conditionally or to choose between two alternative subpatterns, depending on  conditionally or to choose between two alternative subpatterns, depending on
1849  the result of an assertion, or whether a previous capturing subpattern matched  the result of an assertion, or whether a specific capturing subpattern has
1850  or not. The two possible forms of conditional subpattern are  already been matched. The two possible forms of conditional subpattern are:
1851  <pre>  <pre>
1852    (?(condition)yes-pattern)    (?(condition)yes-pattern)
1853    (?(condition)yes-pattern|no-pattern)    (?(condition)yes-pattern|no-pattern)
# Line 1665  Checking for a used subpattern by number Line 1865  Checking for a used subpattern by number
1865  </b><br>  </b><br>
1866  <P>  <P>
1867  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
1868  condition is true if the capturing subpattern of that number has previously  condition is true if a capturing subpattern of that number has previously
1869  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
1870  sign. In this case, the subpattern number is relative rather than absolute.  (see the earlier
1871  The most recently opened parentheses can be referenced by (?(-1), the next most  <a href="#recursion">section about duplicate subpattern numbers),</a>
1872  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
1873  refer to subsequent groups with constructs such as (?(+2).  to precede the digits with a plus or minus sign. In this case, the subpattern
1874    number is relative rather than absolute. The most recently opened parentheses
1875    can be referenced by (?(-1), the next most recent by (?(-2), and so on. In
1876    looping constructs it can also make sense to refer to subsequent groups with
1877    constructs such as (?(+2).
1878  </P>  </P>
1879  <P>  <P>
1880  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 1918  names that consist entirely of digits is
1918  Rewriting the above example to use a named subpattern gives this:  Rewriting the above example to use a named subpattern gives this:
1919  <pre>  <pre>
1920    (?&#60;OPEN&#62; \( )?    [^()]+    (?(&#60;OPEN&#62;) \) )    (?&#60;OPEN&#62; \( )?    [^()]+    (?(&#60;OPEN&#62;) \) )
1921    </pre>
1922  </PRE>  If the name used in a condition of this kind is a duplicate, the test is
1923    applied to all subpatterns of the same name, and is true if any one of them has
1924    matched.
1925  </P>  </P>
1926  <br><b>  <br><b>
1927  Checking for pattern recursion  Checking for pattern recursion
# Line 1728  letter R, for example: Line 1934  letter R, for example:
1934  <pre>  <pre>
1935    (?(R3)...) or (?(R&name)...)    (?(R3)...) or (?(R&name)...)
1936  </pre>  </pre>
1937  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
1938  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
1939  stack.  stack. If the name used in a condition of this kind is a duplicate, the test is
1940    applied to all subpatterns of the same name, and is true if any one of them is
1941    the most recent recursion.
1942  </P>  </P>
1943  <P>  <P>
1944  At "top level", all these recursion test conditions are false. Recursive  At "top level", all these recursion test conditions are false.
1945  patterns are described below.  <a href="#recursion">The syntax for recursive patterns</a>
1946    is described below.
1947  </P>  </P>
1948  <br><b>  <br><b>
1949  Defining subpatterns for use by reference only  Defining subpatterns for use by reference only
# Line 1744  If the condition is the string (DEFINE), Line 1953  If the condition is the string (DEFINE),
1953  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
1954  alternative in the subpattern. It is always skipped if control reaches this  alternative in the subpattern. It is always skipped if control reaches this
1955  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
1956  "subroutines" that can be referenced from elsewhere. (The use of "subroutines"  "subroutines" that can be referenced from elsewhere. (The use of
1957    <a href="#subpatternsassubroutines">"subroutines"</a>
1958  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
1959  written like this (ignore whitespace and line breaks):  written like this (ignore whitespace and line breaks):
1960  <pre>  <pre>
# Line 1754  written like this (ignore whitespace and Line 1964  written like this (ignore whitespace and
1964  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
1965  named "byte" is defined. This matches an individual component of an IPv4  named "byte" is defined. This matches an individual component of an IPv4
1966  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
1967  pattern is skipped because DEFINE acts like a false condition.  pattern is skipped because DEFINE acts like a false condition. The rest of the
1968  </P>  pattern uses references to the named group to match the four dot-separated
1969  <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.  
1970  </P>  </P>
1971  <br><b>  <br><b>
1972  Assertion conditions  Assertion conditions
# Line 1780  subject is matched against the first alt Line 1987  subject is matched against the first alt
1987  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
1988  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.
1989  <a name="comments"></a></P>  <a name="comments"></a></P>
1990  <br><a name="SEC19" href="#TOC1">COMMENTS</a><br>  <br><a name="SEC20" href="#TOC1">COMMENTS</a><br>
1991  <P>  <P>
1992  The sequence (?# marks the start of a comment that continues up to the next  The sequence (?# marks the start of a comment that continues up to the next
1993  closing parenthesis. Nested parentheses are not permitted. The characters  closing parenthesis. Nested parentheses are not permitted. The characters
# Line 1791  If the PCRE_EXTENDED option is set, an u Line 1998  If the PCRE_EXTENDED option is set, an u
1998  character class introduces a comment that continues to immediately after the  character class introduces a comment that continues to immediately after the
1999  next newline in the pattern.  next newline in the pattern.
2000  <a name="recursion"></a></P>  <a name="recursion"></a></P>
2001  <br><a name="SEC20" href="#TOC1">RECURSIVE PATTERNS</a><br>  <br><a name="SEC21" href="#TOC1">RECURSIVE PATTERNS</a><br>
2002  <P>  <P>
2003  Consider the problem of matching a string in parentheses, allowing for  Consider the problem of matching a string in parentheses, allowing for
2004  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 2021  recursively to the pattern in which it a
2021  Obviously, PCRE cannot support the interpolation of Perl code. Instead, it  Obviously, PCRE cannot support the interpolation of Perl code. Instead, it
2022  supports special syntax for recursion of the entire pattern, and also for  supports special syntax for recursion of the entire pattern, and also for
2023  individual subpattern recursion. After its introduction in PCRE and Python,  individual subpattern recursion. After its introduction in PCRE and Python,
2024  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.
2025  </P>  </P>
2026  <P>  <P>
2027  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
2028  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,
2029  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
2030    <a href="#subpatternsassubroutines">"subroutine"</a>
2031  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
2032  a recursive call of the entire regular expression.  a recursive call of the entire regular expression.
2033  </P>  </P>
2034  <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>  
2035  This PCRE pattern solves the nested parentheses problem (assume the  This PCRE pattern solves the nested parentheses problem (assume the
2036  PCRE_EXTENDED option is set so that white space is ignored):  PCRE_EXTENDED option is set so that white space is ignored):
2037  <pre>  <pre>
2038    \( ( (?&#62;[^()]+) | (?R) )* \)    \( ( [^()]++ | (?R) )* \)
2039  </pre>  </pre>
2040  First it matches an opening parenthesis. Then it matches any number of  First it matches an opening parenthesis. Then it matches any number of
2041  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
2042  match of the pattern itself (that is, a correctly parenthesized substring).  match of the pattern itself (that is, a correctly parenthesized substring).
2043  Finally there is a closing parenthesis.  Finally there is a closing parenthesis. Note the use of a possessive quantifier
2044    to avoid backtracking into sequences of non-parentheses.
2045  </P>  </P>
2046  <P>  <P>
2047  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
2048  pattern, so instead you could use this:  pattern, so instead you could use this:
2049  <pre>  <pre>
2050    ( \( ( (?&#62;[^()]+) | (?1) )* \) )    ( \( ( [^()]++ | (?1) )* \) )
2051  </pre>  </pre>
2052  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
2053  them instead of the whole pattern.  them instead of the whole pattern.
2054  </P>  </P>
2055  <P>  <P>
2056  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
2057  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).
2058  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
2059  most recently opened parentheses preceding the recursion. In other words, a  most recently opened parentheses preceding the recursion. In other words, a
2060  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 2064  it is encountered.
2064  It is also possible to refer to subsequently opened parentheses, by writing  It is also possible to refer to subsequently opened parentheses, by writing
2065  references such as (?+2). However, these cannot be recursive because the  references such as (?+2). However, these cannot be recursive because the
2066  reference is not inside the parentheses that are referenced. They are always  reference is not inside the parentheses that are referenced. They are always
2067  "subroutine" calls, as described in the next section.  <a href="#subpatternsassubroutines">"subroutine"</a>
2068    calls, as described in the next section.
2069  </P>  </P>
2070  <P>  <P>
2071  An alternative approach is to use named parentheses instead. The Perl syntax  An alternative approach is to use named parentheses instead. The Perl syntax
2072  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
2073  could rewrite the above example as follows:  could rewrite the above example as follows:
2074  <pre>  <pre>
2075    (?&#60;pn&#62; \( ( (?&#62;[^()]+) | (?&pn) )* \) )    (?&#60;pn&#62; \( ( [^()]++ | (?&pn) )* \) )
2076  </pre>  </pre>
2077  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
2078  used.  used.
2079  </P>  </P>
2080  <P>  <P>
2081  This particular example pattern that we have been looking at contains nested  This particular example pattern that we have been looking at contains nested
2082  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
2083  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
2084  match. For example, when this pattern is applied to  that do not match. For example, when this pattern is applied to
2085  <pre>  <pre>
2086    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
2087  </pre>  </pre>
2088  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,
2089  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
2090  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
2091  before failure can be reported.  before failure can be reported.
2092  </P>  </P>
2093  <P>  <P>
2094  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
2095  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
2096  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  
2097  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
2098  documentation). If the pattern above is matched against  documentation). If the pattern above is matched against
2099  <pre>  <pre>
2100    (ab(cd)ef)    (ab(cd)ef)
2101  </pre>  </pre>
2102  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
2103  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
2104  <pre>  matched at the top level, its final value is unset, even if it is (temporarily)
2105    \( ( ( (?&#62;[^()]+) | (?R) )* ) \)  set at a deeper level.
2106       ^                        ^  </P>
2107       ^                        ^  <P>
2108  </pre>  If there are more than 15 capturing parentheses in a pattern, PCRE has to
2109  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
2110  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
2111  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.  
2112  </P>  </P>
2113  <P>  <P>
2114  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 2121  recursing), whereas any characters are p
2121  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
2122  different alternatives for the recursive and non-recursive cases. The (?R) item  different alternatives for the recursive and non-recursive cases. The (?R) item
2123  is the actual recursive call.  is the actual recursive call.
2124    <a name="recursiondifference"></a></P>
2125    <br><b>
2126    Recursion difference from Perl
2127    </b><br>
2128    <P>
2129    In PCRE (like Python, but unlike Perl), a recursive subpattern call is always
2130    treated as an atomic group. That is, once it has matched some of the subject
2131    string, it is never re-entered, even if it contains untried alternatives and
2132    there is a subsequent matching failure. This can be illustrated by the
2133    following pattern, which purports to match a palindromic string that contains
2134    an odd number of characters (for example, "a", "aba", "abcba", "abcdcba"):
2135    <pre>
2136      ^(.|(.)(?1)\2)$
2137    </pre>
2138    The idea is that it either matches a single character, or two identical
2139    characters surrounding a sub-palindrome. In Perl, this pattern works; in PCRE
2140    it does not if the pattern is longer than three characters. Consider the
2141    subject string "abcba":
2142    </P>
2143    <P>
2144    At the top level, the first character is matched, but as it is not at the end
2145    of the string, the first alternative fails; the second alternative is taken
2146    and the recursion kicks in. The recursive call to subpattern 1 successfully
2147    matches the next character ("b"). (Note that the beginning and end of line
2148    tests are not part of the recursion).
2149    </P>
2150    <P>
2151    Back at the top level, the next character ("c") is compared with what
2152    subpattern 2 matched, which was "a". This fails. Because the recursion is
2153    treated as an atomic group, there are now no backtracking points, and so the
2154    entire match fails. (Perl is able, at this point, to re-enter the recursion and
2155    try the second alternative.) However, if the pattern is written with the
2156    alternatives in the other order, things are different:
2157    <pre>
2158      ^((.)(?1)\2|.)$
2159    </pre>
2160    This time, the recursing alternative is tried first, and continues to recurse
2161    until it runs out of characters, at which point the recursion fails. But this
2162    time we do have another alternative to try at the higher level. That is the big
2163    difference: in the previous case the remaining alternative is at a deeper
2164    recursion level, which PCRE cannot use.
2165    </P>
2166    <P>
2167    To change the pattern so that matches all palindromic strings, not just those
2168    with an odd number of characters, it is tempting to change the pattern to this:
2169    <pre>
2170      ^((.)(?1)\2|.?)$
2171    </pre>
2172    Again, this works in Perl, but not in PCRE, and for the same reason. When a
2173    deeper recursion has matched a single character, it cannot be entered again in
2174    order to match an empty string. The solution is to separate the two cases, and
2175    write out the odd and even cases as alternatives at the higher level:
2176    <pre>
2177      ^(?:((.)(?1)\2|)|((.)(?3)\4|.))
2178    </pre>
2179    If you want to match typical palindromic phrases, the pattern has to ignore all
2180    non-word characters, which can be done like this:
2181    <pre>
2182      ^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$
2183    </pre>
2184    If run with the PCRE_CASELESS option, this pattern matches phrases such as "A
2185    man, a plan, a canal: Panama!" and it works well in both PCRE and Perl. Note
2186    the use of the possessive quantifier *+ to avoid backtracking into sequences of
2187    non-word characters. Without this, PCRE takes a great deal longer (ten times or
2188    more) to match typical phrases, and Perl takes so long that you think it has
2189    gone into a loop.
2190    </P>
2191    <P>
2192    <b>WARNING</b>: The palindrome-matching patterns above work only if the subject
2193    string does not start with a palindrome that is shorter than the entire string.
2194    For example, although "abcba" is correctly matched, if the subject is "ababa",
2195    PCRE finds the palindrome "aba" at the start, then fails at top level because
2196    the end of the string does not follow. Once again, it cannot jump back into the
2197    recursion to try other alternatives, so the entire match fails.
2198  <a name="subpatternsassubroutines"></a></P>  <a name="subpatternsassubroutines"></a></P>
2199  <br><a name="SEC21" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>  <br><a name="SEC22" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>
2200  <P>  <P>
2201  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
2202  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 2221  is used, it does match "sense and respon
2221  strings. Another example is given in the discussion of DEFINE above.  strings. Another example is given in the discussion of DEFINE above.
2222  </P>  </P>
2223  <P>  <P>
2224  Like recursive subpatterns, a "subroutine" call is always treated as an atomic  Like recursive subpatterns, a subroutine call is always treated as an atomic
2225  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
2226  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
2227  matching failure.  matching failure. Any capturing parentheses that are set during the subroutine
2228    call revert to their previous values afterwards.
2229  </P>  </P>
2230  <P>  <P>
2231  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 2236  changed for different calls. For example
2236  </pre>  </pre>
2237  It matches "abcabc". It does not match "abcABC" because the change of  It matches "abcabc". It does not match "abcABC" because the change of
2238  processing option does not affect the called subpattern.  processing option does not affect the called subpattern.
2239    <a name="onigurumasubroutines"></a></P>
2240    <br><a name="SEC23" href="#TOC1">ONIGURUMA SUBROUTINE SYNTAX</a><br>
2241    <P>
2242    For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
2243    a number enclosed either in angle brackets or single quotes, is an alternative
2244    syntax for referencing a subpattern as a subroutine, possibly recursively. Here
2245    are two of the examples used above, rewritten using this syntax:
2246    <pre>
2247      (?&#60;pn&#62; \( ( (?&#62;[^()]+) | \g&#60;pn&#62; )* \) )
2248      (sens|respons)e and \g'1'ibility
2249    </pre>
2250    PCRE supports an extension to Oniguruma: if a number is preceded by a
2251    plus or a minus sign it is taken as a relative reference. For example:
2252    <pre>
2253      (abc)(?i:\g&#60;-1&#62;)
2254    </pre>
2255    Note that \g{...} (Perl syntax) and \g&#60;...&#62; (Oniguruma syntax) are <i>not</i>
2256    synonymous. The former is a back reference; the latter is a subroutine call.
2257  </P>  </P>
2258  <br><a name="SEC22" href="#TOC1">CALLOUTS</a><br>  <br><a name="SEC24" href="#TOC1">CALLOUTS</a><br>
2259  <P>  <P>
2260  Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl  Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl
2261  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 1996  description of the interface to the call Line 2290  description of the interface to the call
2290  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
2291  documentation.  documentation.
2292  </P>  </P>
2293  <br><a name="SEC23" href="#TOC1">BACTRACKING CONTROL</a><br>  <br><a name="SEC25" href="#TOC1">BACKTRACKING CONTROL</a><br>
2294  <P>  <P>
2295  Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which  Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which
2296  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 2299  production code should be noted to avoid
2299  remarks apply to the PCRE features described in this section.  remarks apply to the PCRE features described in this section.
2300  </P>  </P>
2301  <P>  <P>
2302  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
2303  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
2304  backtracking algorithm. They cause an error if encountered by  a backtracking algorithm. With the exception of (*FAIL), which behaves like a
2305    failing negative assertion, they cause an error if encountered by
2306  <b>pcre_dfa_exec()</b>.  <b>pcre_dfa_exec()</b>.
2307  </P>  </P>
2308  <P>  <P>
2309    If any of these verbs are used in an assertion or subroutine subpattern
2310    (including recursive subpatterns), their effect is confined to that subpattern;
2311    it does not extend to the surrounding pattern. Note that such subpatterns are
2312    processed as anchored at the point where they are tested.
2313    </P>
2314    <P>
2315  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
2316  parenthesis followed by an asterisk. In Perl, they are generally of the form  parenthesis followed by an asterisk. In Perl, they are generally of the form
2317  (*VERB:ARG) but PCRE does not support the use of arguments, so its general  (*VERB:ARG) but PCRE does not support the use of arguments, so its general
# Line 2027  The following verbs act as soon as they Line 2328  The following verbs act as soon as they
2328  </pre>  </pre>
2329  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
2330  pattern. When inside a recursion, only the innermost pattern is ended  pattern. When inside a recursion, only the innermost pattern is ended
2331  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
2332  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:  
2333  <pre>  <pre>
2334    A(A|B(*ACCEPT)|C)D    A((?:A|B(*ACCEPT)|C)D)
2335  </pre>  </pre>
2336  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
2337  captured.  the outer parentheses.
2338  <pre>  <pre>
2339    (*FAIL) or (*F)    (*FAIL) or (*F)
2340  </pre>  </pre>
# Line 2061  The verbs differ in exactly what kind of Line 2361  The verbs differ in exactly what kind of
2361  </pre>  </pre>
2362  This verb causes the whole match to fail outright if the rest of the pattern  This verb causes the whole match to fail outright if the rest of the pattern
2363  does not match. Even if the pattern is unanchored, no further attempts to find  does not match. Even if the pattern is unanchored, no further attempts to find
2364  a match by advancing the start point take place. Once (*COMMIT) has been  a match by advancing the starting point take place. Once (*COMMIT) has been
2365  passed, <b>pcre_exec()</b> is committed to finding a match at the current  passed, <b>pcre_exec()</b> is committed to finding a match at the current
2366  starting point, or not at all. For example:  starting point, or not at all. For example:
2367  <pre>  <pre>
# Line 2093  was matched leading up to it cannot be p Line 2393  was matched leading up to it cannot be p
2393  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
2394  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
2395  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
2396  effect in this example; although it would suppress backtracking during the  effect as this example; although it would suppress backtracking during the
2397  first match attempt, the second attempt would start at the second character  first match attempt, the second attempt would start at the second character
2398  instead of skipping on to "c".  instead of skipping on to "c".
2399  <pre>  <pre>
# Line 2111  the end of the group if FOO succeeds); o Line 2411  the end of the group if FOO succeeds); o
2411  second alternative and tries COND2, without backtracking into COND1. If (*THEN)  second alternative and tries COND2, without backtracking into COND1. If (*THEN)
2412  is used outside of any alternation, it acts exactly like (*PRUNE).  is used outside of any alternation, it acts exactly like (*PRUNE).
2413  </P>  </P>
2414  <br><a name="SEC24" href="#TOC1">SEE ALSO</a><br>  <br><a name="SEC26" href="#TOC1">SEE ALSO</a><br>
2415  <P>  <P>
2416  <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),
2417    <b>pcresyntax</b>(3), <b>pcre</b>(3).
2418  </P>  </P>
2419  <br><a name="SEC25" href="#TOC1">AUTHOR</a><br>  <br><a name="SEC27" href="#TOC1">AUTHOR</a><br>
2420  <P>  <P>
2421  Philip Hazel  Philip Hazel
2422  <br>  <br>
# Line 2124  University Computing Service Line 2425  University Computing Service
2425  Cambridge CB2 3QH, England.  Cambridge CB2 3QH, England.
2426  <br>  <br>
2427  </P>  </P>
2428  <br><a name="SEC26" href="#TOC1">REVISION</a><br>  <br><a name="SEC28" href="#TOC1">REVISION</a><br>
2429  <P>  <P>
2430  Last updated: 09 August 2007  Last updated: 06 March 2010
2431  <br>  <br>
2432  Copyright &copy; 1997-2007 University of Cambridge.  Copyright &copy; 1997-2010 University of Cambridge.
2433  <br>  <br>
2434  <p>  <p>
2435  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.507

  ViewVC Help
Powered by ViewVC 1.1.5