/[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 518 by ph10, Tue May 18 15:47: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) 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>
# 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 affects the interpretation of the dot metacharacter when
128    PCRE_DOTALL is not set, and also the behaviour of \N. However, it does not
129    affect what the \R escape sequence matches. By default, this is any Unicode
130    newline sequence, for Perl compatibility. However, this can be changed; see the
131    description of \R in the section entitled
132    <a href="#newlineseq">"Newline sequences"</a>
133    below. A change of \R setting can be combined with a change of newline
134    convention.
135    </P>
136    <br><a name="SEC3" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br>
137  <P>  <P>
138  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
139  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 190  a character class the only metacharacter
190  </pre>  </pre>
191  The following sections describe the use of each of the metacharacters.  The following sections describe the use of each of the metacharacters.
192  </P>  </P>
193  <br><a name="SEC3" href="#TOC1">BACKSLASH</a><br>  <br><a name="SEC4" href="#TOC1">BACKSLASH</a><br>
194  <P>  <P>
195  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
196  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 231  Non-printing characters
231  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
232  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
233  non-printing characters, apart from the binary zero that terminates a pattern,  non-printing characters, apart from the binary zero that terminates a pattern,
234  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
235  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:  
236  <pre>  <pre>
237    \a        alarm, that is, the BEL character (hex 07)    \a        alarm, that is, the BEL character (hex 07)
238    \cx       "control-x", where x is any character    \cx       "control-x", where x is any character
239    \e        escape (hex 1B)    \e        escape (hex 1B)
240    \f        formfeed (hex 0C)    \f        formfeed (hex 0C)
241    \n        newline (hex 0A)    \n        linefeed (hex 0A)
242    \r        carriage return (hex 0D)    \r        carriage return (hex 0D)
243    \t        tab (hex 09)    \t        tab (hex 09)
244    \ddd      character with octal code ddd, or backreference    \ddd      character with octal code ddd, or back reference
245    \xhh      character with hex code hh    \xhh      character with hex code hh
246    \x{hhh..} character with hex code hhh..    \x{hhh..} character with hex code hhh..
247  </pre>  </pre>
# Line 252  zero, because no more than three octal d Line 310  zero, because no more than three octal d
310  <P>  <P>
311  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
312  and outside character classes. In addition, inside a character class, the  and outside character classes. In addition, inside a character class, the
313  sequence \b is interpreted as the backspace character (hex 08), and the  sequence \b is interpreted as the backspace character (hex 08). The sequences
314  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
315  respectively. Outside a character class, these sequences have different  unrecognized escape sequences, they are treated as the literal characters "B",
316  meanings  "N", "R", and "X" by default, but cause an error if the PCRE_EXTRA option is
317  <a href="#uniextseq">(see below).</a>  set. Outside a character class, these sequences have different meanings.
318  </P>  </P>
319  <br><b>  <br><b>
320  Absolute and relative back references  Absolute and relative back references
# Line 270  following the discussion of Line 328  following the discussion of
328  <a href="#subpattern">parenthesized subpatterns.</a>  <a href="#subpattern">parenthesized subpatterns.</a>
329  </P>  </P>
330  <br><b>  <br><b>
331    Absolute and relative subroutine calls
332    </b><br>
333    <P>
334    For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
335    a number enclosed either in angle brackets or single quotes, is an alternative
336    syntax for referencing a subpattern as a "subroutine". Details are discussed
337    <a href="#onigurumasubroutines">later.</a>
338    Note that \g{...} (Perl syntax) and \g&#60;...&#62; (Oniguruma syntax) are <i>not</i>
339    synonymous. The former is a back reference; the latter is a
340    <a href="#subpatternsassubroutines">subroutine</a>
341    call.
342    <a name="genericchartypes"></a></P>
343    <br><b>
344  Generic character types  Generic character types
345  </b><br>  </b><br>
346  <P>  <P>
347  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:  
348  <pre>  <pre>
349    \d     any decimal digit    \d     any decimal digit
350    \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 357  following are always recognized:
357    \w     any "word" character    \w     any "word" character
358    \W     any "non-word" character    \W     any "non-word" character
359  </pre>  </pre>
360  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.
361  two disjoint sets. Any given character matches one, and only one, of each pair.  This is the same as
362    <a href="#fullstopdot">the "." metacharacter</a>
363    when PCRE_DOTALL is not set.
364    </P>
365    <P>
366    Each pair of lower and upper case escape sequences partitions the complete set
367    of characters into two disjoint sets. Any given character matches one, and only
368    one, of each pair.
369  </P>  </P>
370  <P>  <P>
371  These character type sequences can appear both inside and outside character  These character type sequences can appear both inside and outside character
# Line 308  In UTF-8 mode, characters with values gr Line 385  In UTF-8 mode, characters with values gr
385  \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
386  character property support is available. These sequences retain their original  character property support is available. These sequences retain their original
387  meanings from before UTF-8 support was available, mainly for efficiency  meanings from before UTF-8 support was available, mainly for efficiency
388  reasons.  reasons. Note that this also affects \b, because it is defined in terms of \w
389    and \W.
390  </P>  </P>
391  <P>  <P>
392  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 436  page). For example, in a French locale s
436  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
437  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
438  is discouraged.  is discouraged.
439  </P>  <a name="newlineseq"></a></P>
440  <br><b>  <br><b>
441  Newline sequences  Newline sequences
442  </b><br>  </b><br>
443  <P>  <P>
444  Outside a character class, the escape sequence \R matches any Unicode newline  Outside a character class, by default, the escape sequence \R matches any
445  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
446  the following:  equivalent to the following:
447  <pre>  <pre>
448    (?&#62;\r\n|\n|\x0b|\f|\r|\x85)    (?&#62;\r\n|\n|\x0b|\f|\r|\x85)
449  </pre>  </pre>
# Line 384  Unicode character property support is no Line 462  Unicode character property support is no
462  recognized.  recognized.
463  </P>  </P>
464  <P>  <P>
465  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
466    complete set of Unicode line endings) by setting the option PCRE_BSR_ANYCRLF
467    either at compile time or when the pattern is matched. (BSR is an abbrevation
468    for "backslash R".) This can be made the default when PCRE is built; if this is
469    the case, the other behaviour can be requested via the PCRE_BSR_UNICODE option.
470    It is also possible to specify these settings by starting a pattern string with
471    one of the following sequences:
472    <pre>
473      (*BSR_ANYCRLF)   CR, LF, or CRLF only
474      (*BSR_UNICODE)   any Unicode newline sequence
475    </pre>
476    These override the default and the options given to <b>pcre_compile()</b> or
477    <b>pcre_compile2()</b>, but they can be overridden by options given to
478    <b>pcre_exec()</b> or <b>pcre_dfa_exec()</b>. Note that these special settings,
479    which are not Perl-compatible, are recognized only at the very start of a
480    pattern, and that they must be in upper case. If more than one of them is
481    present, the last one is used. They can be combined with a change of newline
482    convention, for example, a pattern can start with:
483    <pre>
484      (*ANY)(*BSR_ANYCRLF)
485    </pre>
486    Inside a character class, \R is treated as an unrecognized escape sequence,
487    and so matches the letter "R" by default, but causes an error if PCRE_EXTRA is
488    set.
489  <a name="uniextseq"></a></P>  <a name="uniextseq"></a></P>
490  <br><b>  <br><b>
491  Unicode character properties  Unicode character properties
# Line 401  The extra escape sequences are: Line 502  The extra escape sequences are:
502    \X       an extended Unicode sequence    \X       an extended Unicode sequence
503  </pre>  </pre>
504  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
505  script names, the general category properties, and "Any", which matches any  script names, the general category properties, "Any", which matches any
506  character (including newline). Other properties such as "InMusicalSymbols" are  character (including newline), and some special PCRE properties (described
507  not currently supported by PCRE. Note that \P{Any} does not match any  in the
508  characters, so always causes a match failure.  <a href="#extraprops">next section).</a>
509    Other Perl properties such as "InMusicalSymbols" are not currently supported by
510    PCRE. Note that \P{Any} does not match any characters, so always causes a
511    match failure.
512  </P>  </P>
513  <P>  <P>
514  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 524  Those that are not part of an identified
524  <P>  <P>
525  Arabic,  Arabic,
526  Armenian,  Armenian,
527    Avestan,
528  Balinese,  Balinese,
529    Bamum,
530  Bengali,  Bengali,
531  Bopomofo,  Bopomofo,
532  Braille,  Braille,
533  Buginese,  Buginese,
534  Buhid,  Buhid,
535  Canadian_Aboriginal,  Canadian_Aboriginal,
536    Carian,
537    Cham,
538  Cherokee,  Cherokee,
539  Common,  Common,
540  Coptic,  Coptic,
# Line 435  Cypriot, Line 543  Cypriot,
543  Cyrillic,  Cyrillic,
544  Deseret,  Deseret,
545  Devanagari,  Devanagari,
546    Egyptian_Hieroglyphs,
547  Ethiopic,  Ethiopic,
548  Georgian,  Georgian,
549  Glagolitic,  Glagolitic,
# Line 447  Hangul, Line 556  Hangul,
556  Hanunoo,  Hanunoo,
557  Hebrew,  Hebrew,
558  Hiragana,  Hiragana,
559    Imperial_Aramaic,
560  Inherited,  Inherited,
561    Inscriptional_Pahlavi,
562    Inscriptional_Parthian,
563    Javanese,
564    Kaithi,
565  Kannada,  Kannada,
566  Katakana,  Katakana,
567    Kayah_Li,
568  Kharoshthi,  Kharoshthi,
569  Khmer,  Khmer,
570  Lao,  Lao,
571  Latin,  Latin,
572    Lepcha,
573  Limbu,  Limbu,
574  Linear_B,  Linear_B,
575    Lisu,
576    Lycian,
577    Lydian,
578  Malayalam,  Malayalam,
579    Meetei_Mayek,
580  Mongolian,  Mongolian,
581  Myanmar,  Myanmar,
582  New_Tai_Lue,  New_Tai_Lue,
# Line 464  Nko, Line 584  Nko,
584  Ogham,  Ogham,
585  Old_Italic,  Old_Italic,
586  Old_Persian,  Old_Persian,
587    Old_South_Arabian,
588    Old_Turkic,
589    Ol_Chiki,
590  Oriya,  Oriya,
591  Osmanya,  Osmanya,
592  Phags_Pa,  Phags_Pa,
593  Phoenician,  Phoenician,
594    Rejang,
595  Runic,  Runic,
596    Samaritan,
597    Saurashtra,
598  Shavian,  Shavian,
599  Sinhala,  Sinhala,
600    Sundanese,
601  Syloti_Nagri,  Syloti_Nagri,
602  Syriac,  Syriac,
603  Tagalog,  Tagalog,
604  Tagbanwa,  Tagbanwa,
605  Tai_Le,  Tai_Le,
606    Tai_Tham,
607    Tai_Viet,
608  Tamil,  Tamil,
609  Telugu,  Telugu,
610  Thaana,  Thaana,
# Line 483  Thai, Line 612  Thai,
612  Tibetan,  Tibetan,
613  Tifinagh,  Tifinagh,
614  Ugaritic,  Ugaritic,
615    Vai,
616  Yi.  Yi.
617  </P>  </P>
618  <P>  <P>
619  Each character has exactly one general category property, specified by a  Each character has exactly one Unicode general category property, specified by
620  two-letter abbreviation. For compatibility with Perl, negation can be specified  a two-letter abbreviation. For compatibility with Perl, negation can be
621  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
622  example, \p{^Lu} is the same as \P{Lu}.  name. For example, \p{^Lu} is the same as \P{Lu}.
623  </P>  </P>
624  <P>  <P>
625  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 686  U+DFFF. Such characters are not valid in
686  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
687  (see the discussion of PCRE_NO_UTF8_CHECK in the  (see the discussion of PCRE_NO_UTF8_CHECK in the
688  <a href="pcreapi.html"><b>pcreapi</b></a>  <a href="pcreapi.html"><b>pcreapi</b></a>
689  page).  page). Perl does not support the Cs property.
690  </P>  </P>
691  <P>  <P>
692  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})
693  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
694  properties with "Is".  properties with "Is".
695  </P>  </P>
# Line 591  Matching characters by Unicode property Line 721  Matching characters by Unicode property
721  a structure that contains data for over fifteen thousand characters. That is  a structure that contains data for over fifteen thousand characters. That is
722  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
723  properties in PCRE.  properties in PCRE.
724    <a name="extraprops"></a></P>
725    <br><b>
726    PCRE's additional properties
727    </b><br>
728    <P>
729    As well as the standard Unicode properties described in the previous
730    section, PCRE supports four more that make it possible to convert traditional
731    escape sequences such as \w and \s and POSIX character classes to use Unicode
732    properties. These are:
733    <pre>
734      Xan   Any alphanumeric character
735      Xps   Any POSIX space character
736      Xsp   Any Perl space character
737      Xwd   Any Perl "word" character
738    </pre>
739    Xan matches characters that have either the L (letter) or the N (number)
740    property. Xps matches the characters tab, linefeed, vertical tab, formfeed, or
741    carriage return, and any other character that has the Z (separator) property.
742    Xsp is the same as Xps, except that vertical tab is excluded. Xwd matches the
743    same characters as Xan, plus underscore.
744  <a name="resetmatchstart"></a></P>  <a name="resetmatchstart"></a></P>
745  <br><b>  <br><b>
746  Resetting the match start  Resetting the match start
# Line 614  For example, when the pattern Line 764  For example, when the pattern
764    (foo)\Kbar    (foo)\Kbar
765  </pre>  </pre>
766  matches "foobar", the first substring is still set to "foo".  matches "foobar", the first substring is still set to "foo".
767    </P>
768    <P>
769    Perl documents that the use of \K within assertions is "not well defined". In
770    PCRE, \K is acted upon when it occurs inside positive assertions, but is
771    ignored in negative assertions.
772  <a name="smallassertions"></a></P>  <a name="smallassertions"></a></P>
773  <br><b>  <br><b>
774  Simple assertions  Simple assertions
# Line 634  The backslashed assertions are: Line 789  The backslashed assertions are:
789    \z     matches only at the end of the subject    \z     matches only at the end of the subject
790    \G     matches at the first matching position in the subject    \G     matches at the first matching position in the subject
791  </pre>  </pre>
792  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
793  different meaning, namely the backspace character, inside a character class).  character. If any other of these assertions appears in a character class, by
794    default it matches the corresponding literal character (for example, \B
795    matches the letter B). However, if the PCRE_EXTRA option is set, an "invalid
796    escape sequence" error is generated instead.
797  </P>  </P>
798  <P>  <P>
799  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
800  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
801  \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
802  first or last character matches \w, respectively.  first or last character matches \w, respectively. Neither PCRE nor Perl has a
803    separte "start of word" or "end of word" metasequence. However, whatever
804    follows \b normally determines which it is. For example, the fragment
805    \ba matches "a" at the start of a word.
806  </P>  </P>
807  <P>  <P>
808  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 836  If all the alternatives of a pattern beg
836  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
837  regular expression.  regular expression.
838  </P>  </P>
839  <br><a name="SEC4" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>  <br><a name="SEC5" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>
840  <P>  <P>
841  Outside a character class, in the default matching mode, the circumflex  Outside a character class, in the default matching mode, the circumflex
842  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 889  PCRE_DOLLAR_ENDONLY option is ignored if
889  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
890  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
891  \A it is always anchored, whether or not PCRE_MULTILINE is set.  \A it is always anchored, whether or not PCRE_MULTILINE is set.
892  </P>  <a name="fullstopdot"></a></P>
893  <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>
894  <P>  <P>
895  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
896  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 915  The handling of dot is entirely independ
915  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
916  special meaning in a character class.  special meaning in a character class.
917  </P>  </P>
918  <br><a name="SEC6" href="#TOC1">MATCHING A SINGLE BYTE</a><br>  <P>
919    The escape sequence \N always behaves as a dot does when PCRE_DOTALL is not
920    set. In other words, it matches any one character except one that signifies the
921    end of a line.
922    </P>
923    <br><a name="SEC7" href="#TOC1">MATCHING A SINGLE BYTE</a><br>
924  <P>  <P>
925  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
926  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 935  PCRE does not allow \C to appear in look
935  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
936  the lookbehind.  the lookbehind.
937  <a name="characterclass"></a></P>  <a name="characterclass"></a></P>
938  <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>
939  <P>  <P>
940  An opening square bracket introduces a character class, terminated by a closing  An opening square bracket introduces a character class, terminated by a closing
941  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.
942  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
943  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
944  escaped with a backslash.  a member of the class, it should be the first data character in the class
945    (after an initial circumflex, if present) or escaped with a backslash.
946  </P>  </P>
947  <P>  <P>
948  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
949  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
950  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
951  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
952  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
953  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 958  For example, the character class [aeiou]
958  [^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
959  circumflex is just a convenient notation for specifying the characters that  circumflex is just a convenient notation for specifying the characters that
960  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
961  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
962  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
963  string.  string.
964  </P>  </P>
# Line 807  caseful version would. In UTF-8 mode, PC Line 974  caseful version would. In UTF-8 mode, PC
974  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
975  always possible. For characters with higher values, the concept of case is  always possible. For characters with higher values, the concept of case is
976  supported if PCRE is compiled with Unicode property support, but not otherwise.  supported if PCRE is compiled with Unicode property support, but not otherwise.
977  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,
978  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
979  UTF-8 support.  with UTF-8 support.
980  </P>  </P>
981  <P>  <P>
982  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 1031  introducing a POSIX class name - see the
1031  closing square bracket. However, escaping other non-alphanumeric characters  closing square bracket. However, escaping other non-alphanumeric characters
1032  does no harm.  does no harm.
1033  </P>  </P>
1034  <br><a name="SEC8" href="#TOC1">POSIX CHARACTER CLASSES</a><br>  <br><a name="SEC9" href="#TOC1">POSIX CHARACTER CLASSES</a><br>
1035  <P>  <P>
1036  Perl supports the POSIX notation for character classes. This uses names  Perl supports the POSIX notation for character classes. This uses names
1037  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 1077  supported, and an error is given if they
1077  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
1078  the POSIX character classes.  the POSIX character classes.
1079  </P>  </P>
1080  <br><a name="SEC9" href="#TOC1">VERTICAL BAR</a><br>  <br><a name="SEC10" href="#TOC1">VERTICAL BAR</a><br>
1081  <P>  <P>
1082  Vertical bar characters are used to separate alternative patterns. For example,  Vertical bar characters are used to separate alternative patterns. For example,
1083  the pattern  the pattern
# Line 925  that succeeds is used. If the alternativ Line 1092  that succeeds is used. If the alternativ
1092  "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
1093  alternative in the subpattern.  alternative in the subpattern.
1094  </P>  </P>
1095  <br><a name="SEC10" href="#TOC1">INTERNAL OPTION SETTING</a><br>  <br><a name="SEC11" href="#TOC1">INTERNAL OPTION SETTING</a><br>
1096  <P>  <P>
1097  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
1098  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
1099  Perl option letters enclosed between "(?" and ")". The option letters are  the pattern by a sequence of Perl option letters enclosed between "(?" and ")".
1100    The option letters are
1101  <pre>  <pre>
1102    i  for PCRE_CASELESS    i  for PCRE_CASELESS
1103    m  for PCRE_MULTILINE    m  for PCRE_MULTILINE
# Line 944  permitted. If a letter appears both befo Line 1112  permitted. If a letter appears both befo
1112  unset.  unset.
1113  </P>  </P>
1114  <P>  <P>
1115  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
1116  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
1117  If the change is placed right at the start of a pattern, PCRE extracts it into  J, U and X respectively.
1118  the global options (and it will therefore show up in data extracted by the  </P>
1119  <b>pcre_fullinfo()</b> function).  <P>
1120    When one of these option changes occurs at top level (that is, not inside
1121    subpattern parentheses), the change applies to the remainder of the pattern
1122    that follows. If the change is placed right at the start of a pattern, PCRE
1123    extracts it into the global options (and it will therefore show up in data
1124    extracted by the <b>pcre_fullinfo()</b> function).
1125  </P>  </P>
1126  <P>  <P>
1127  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 1142  option settings happen at compile time.
1142  behaviour otherwise.  behaviour otherwise.
1143  </P>  </P>
1144  <P>  <P>
1145  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
1146  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
1147  J, U and X respectively.  pattern can contain special leading sequences such as (*CRLF) to override what
1148    the application has set or what has been defaulted. Details are given in the
1149    section entitled
1150    <a href="#newlineseq">"Newline sequences"</a>
1151    above. There is also the (*UTF8) leading sequence that can be used to set UTF-8
1152    mode; this is equivalent to setting the PCRE_UTF8 option.
1153  <a name="subpattern"></a></P>  <a name="subpattern"></a></P>
1154  <br><a name="SEC11" href="#TOC1">SUBPATTERNS</a><br>  <br><a name="SEC12" href="#TOC1">SUBPATTERNS</a><br>
1155  <P>  <P>
1156  Subpatterns are delimited by parentheses (round brackets), which can be nested.  Subpatterns are delimited by parentheses (round brackets), which can be nested.
1157  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 1204  match exactly the same set of strings. B
1204  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
1205  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
1206  the above patterns match "SUNDAY" as well as "Saturday".  the above patterns match "SUNDAY" as well as "Saturday".
1207  </P>  <a name="dupsubpatternnumber"></a></P>
1208  <br><a name="SEC12" href="#TOC1">DUPLICATE SUBPATTERN NUMBERS</a><br>  <br><a name="SEC13" href="#TOC1">DUPLICATE SUBPATTERN NUMBERS</a><br>
1209  <P>  <P>
1210  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
1211  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 1229  stored.
1229    / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x    / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
1230    # 1            2         2  3        2     3     4    # 1            2         2  3        2     3     4
1231  </pre>  </pre>
1232  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
1233  the first one in the pattern with the given number.  set for that number by any subpattern. The following pattern matches "abcabc"
1234    or "defdef":
1235    <pre>
1236      /(?|(abc)|(def))\1/
1237    </pre>
1238    In contrast, a recursive or "subroutine" call to a numbered subpattern always
1239    refers to the first one in the pattern with the given number. The following
1240    pattern matches "abcabc" or "defabc":
1241    <pre>
1242      /(?|(abc)|(def))(?1)/
1243    </pre>
1244    If a
1245    <a href="#conditions">condition test</a>
1246    for a subpattern's having matched refers to a non-unique number, the test is
1247    true if any of the subpatterns of that number have matched.
1248  </P>  </P>
1249  <P>  <P>
1250  An alternative approach to using this "branch reset" feature is to use  An alternative approach to using this "branch reset" feature is to use
1251  duplicate named subpatterns, as described in the next section.  duplicate named subpatterns, as described in the next section.
1252  </P>  </P>
1253  <br><a name="SEC13" href="#TOC1">NAMED SUBPATTERNS</a><br>  <br><a name="SEC14" href="#TOC1">NAMED SUBPATTERNS</a><br>
1254  <P>  <P>
1255  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
1256  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 1258  if an expression is modified, the number
1258  difficulty, PCRE supports the naming of subpatterns. This feature was not  difficulty, PCRE supports the naming of subpatterns. This feature was not
1259  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
1260  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
1261  the Perl and the Python syntax.  the Perl and the Python syntax. Perl allows identically numbered subpatterns to
1262    have different names, but PCRE does not.
1263  </P>  </P>
1264  <P>  <P>
1265  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
1266  (?'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
1267  parentheses from other parts of the pattern, such as  parentheses from other parts of the pattern, such as
1268  <a href="#backreferences">backreferences,</a>  <a href="#backreferences">back references,</a>
1269  <a href="#recursion">recursion,</a>  <a href="#recursion">recursion,</a>
1270  and  and
1271  <a href="#conditions">conditions,</a>  <a href="#conditions">conditions,</a>
# Line 1087  is also a convenience function for extra Line 1280  is also a convenience function for extra
1280  </P>  </P>
1281  <P>  <P>
1282  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
1283  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
1284  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
1285  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
1286  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
1287  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
1288    name, and in both cases you want to extract the abbreviation. This pattern
1289    (ignoring the line breaks) does the job:
1290  <pre>  <pre>
1291    (?&#60;DN&#62;Mon|Fri|Sun)(?:day)?|    (?&#60;DN&#62;Mon|Fri|Sun)(?:day)?|
1292    (?&#60;DN&#62;Tue)(?:sday)?|    (?&#60;DN&#62;Tue)(?:sday)?|
# Line 1106  subpattern, as described in the previous Line 1301  subpattern, as described in the previous
1301  <P>  <P>
1302  The convenience function for extracting the data by name returns the substring  The convenience function for extracting the data by name returns the substring
1303  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
1304  matched. This saves searching to find which numbered subpattern it was. If you  matched. This saves searching to find which numbered subpattern it was.
1305  make a reference to a non-unique named subpattern from elsewhere in the  </P>
1306  pattern, the one that corresponds to the lowest number is used. For further  <P>
1307  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
1308    the pattern, the one that corresponds to the first occurrence of the name is
1309    used. In the absence of duplicate numbers (see the previous section) this is
1310    the one with the lowest number. If you use a named reference in a condition
1311    test (see the
1312    <a href="#conditions">section about conditions</a>
1313    below), either to check whether a subpattern has matched, or to check for
1314    recursion, all subpatterns with the same name are tested. If the condition is
1315    true for any one of them, the overall condition is true. This is the same
1316    behaviour as testing by number. For further details of the interfaces for
1317    handling named subpatterns, see the
1318  <a href="pcreapi.html"><b>pcreapi</b></a>  <a href="pcreapi.html"><b>pcreapi</b></a>
1319  documentation.  documentation.
1320  </P>  </P>
1321  <br><a name="SEC14" href="#TOC1">REPETITION</a><br>  <P>
1322    <b>Warning:</b> You cannot use different names to distinguish between two
1323    subpatterns with the same number because PCRE uses only the numbers when
1324    matching. For this reason, an error is given at compile time if different names
1325    are given to subpatterns with the same number. However, you can give the same
1326    name to subpatterns with the same number, even when PCRE_DUPNAMES is not set.
1327    </P>
1328    <br><a name="SEC15" href="#TOC1">REPETITION</a><br>
1329  <P>  <P>
1330  Repetition is specified by quantifiers, which can follow any of the following  Repetition is specified by quantifiers, which can follow any of the following
1331  items:  items:
# Line 1127  items: Line 1339  items:
1339    a character class    a character class
1340    a back reference (see next section)    a back reference (see next section)
1341    a parenthesized subpattern (unless it is an assertion)    a parenthesized subpattern (unless it is an assertion)
1342      a recursive or "subroutine" call to a subpattern
1343  </pre>  </pre>
1344  The general repetition quantifier specifies a minimum and maximum number of  The general repetition quantifier specifies a minimum and maximum number of
1345  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 1373  which may be several bytes long (and the
1373  </P>  </P>
1374  <P>  <P>
1375  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
1376  previous item and the quantifier were not present.  previous item and the quantifier were not present. This may be useful for
1377    subpatterns that are referenced as
1378    <a href="#subpatternsassubroutines">subroutines</a>
1379    from elsewhere in the pattern. Items other than subpatterns that have a {0}
1380    quantifier are omitted from the compiled pattern.
1381  </P>  </P>
1382  <P>  <P>
1383  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 1457  alternatively using ^ to indicate anchor
1457  </P>  </P>
1458  <P>  <P>
1459  However, there is one situation where the optimization cannot be used. When .*  However, there is one situation where the optimization cannot be used. When .*
1460  is inside capturing parentheses that are the subject of a backreference  is inside capturing parentheses that are the subject of a back reference
1461  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
1462  succeeds. Consider, for example:  succeeds. Consider, for example:
1463  <pre>  <pre>
# Line 1264  example, after Line 1481  example, after
1481  </pre>  </pre>
1482  matches "aba" the value of the second captured substring is "b".  matches "aba" the value of the second captured substring is "b".
1483  <a name="atomicgroup"></a></P>  <a name="atomicgroup"></a></P>
1484  <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>
1485  <P>  <P>
1486  With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")  With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
1487  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 1585  an atomic group, like this:
1585  </pre>  </pre>
1586  sequences of non-digits cannot be broken, and failure happens quickly.  sequences of non-digits cannot be broken, and failure happens quickly.
1587  <a name="backreferences"></a></P>  <a name="backreferences"></a></P>
1588  <br><a name="SEC16" href="#TOC1">BACK REFERENCES</a><br>  <br><a name="SEC17" href="#TOC1">BACK REFERENCES</a><br>
1589  <P>  <P>
1590  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
1591  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 1671  after the reference.
1671  <P>  <P>
1672  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
1673  subpattern has not actually been used in a particular match, any back  subpattern has not actually been used in a particular match, any back
1674  references to it always fail. For example, the pattern  references to it always fail by default. For example, the pattern
1675  <pre>  <pre>
1676    (a|(bc))\2    (a|(bc))\2
1677  </pre>  </pre>
1678  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
1679  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
1680  taken as part of a potential back reference number. If the pattern continues  unset value matches an empty string.
1681  with a digit character, some delimiter must be used to terminate the back  </P>
1682  reference. If the PCRE_EXTENDED option is set, this can be whitespace.  <P>
1683  Otherwise an empty comment (see  Because there may be many capturing parentheses in a pattern, all digits
1684    following a backslash are taken as part of a potential back reference number.
1685    If the pattern continues with a digit character, some delimiter must be used to
1686    terminate the back reference. If the PCRE_EXTENDED option is set, this can be
1687    whitespace. Otherwise, the \g{ syntax or an empty comment (see
1688  <a href="#comments">"Comments"</a>  <a href="#comments">"Comments"</a>
1689  below) can be used.  below) can be used.
1690  </P>  </P>
1691    <br><b>
1692    Recursive back references
1693    </b><br>
1694  <P>  <P>
1695  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
1696  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 1705  to the previous iteration. In order for
1705  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
1706  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
1707  minimum of zero.  minimum of zero.
1708    </P>
1709    <P>
1710    Back references of this type cause the group that they reference to be treated
1711    as an
1712    <a href="#atomicgroup">atomic group.</a>
1713    Once the whole group has been matched, a subsequent matching failure cannot
1714    cause backtracking into the middle of the group.
1715  <a name="bigassertions"></a></P>  <a name="bigassertions"></a></P>
1716  <br><a name="SEC17" href="#TOC1">ASSERTIONS</a><br>  <br><a name="SEC18" href="#TOC1">ASSERTIONS</a><br>
1717  <P>  <P>
1718  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
1719  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 1762  lookbehind assertion is needed to achiev
1762  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
1763  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
1764  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.
1765    The Perl 5.10 backtracking control verb (*FAIL) or (*F) is essentially a
1766    synonym for (?!).
1767  <a name="lookbehind"></a></P>  <a name="lookbehind"></a></P>
1768  <br><b>  <br><b>
1769  Lookbehind assertions  Lookbehind assertions
# Line 1554  is permitted, but Line 1787  is permitted, but
1787  </pre>  </pre>
1788  causes an error at compile time. Branches that match different length strings  causes an error at compile time. Branches that match different length strings
1789  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
1790  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
1791  match the same length of string. An assertion such as  match the same length of string. An assertion such as
1792  <pre>  <pre>
1793    (?&#60;=ab(c|de))    (?&#60;=ab(c|de))
1794  </pre>  </pre>
1795  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
1796  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
1797    branches:
1798  <pre>  <pre>
1799    (?&#60;=abc|abde)    (?&#60;=abc|abde)
1800  </pre>  </pre>
1801  In some cases, the Perl 5.10 escape sequence \K  In some cases, the Perl 5.10 escape sequence \K
1802  <a href="#resetmatchstart">(see above)</a>  <a href="#resetmatchstart">(see above)</a>
1803  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
1804  fixed-length.  restriction.
1805  </P>  </P>
1806  <P>  <P>
1807  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 1816  the length of the lookbehind. The \X and
1816  different numbers of bytes, are also not permitted.  different numbers of bytes, are also not permitted.
1817  </P>  </P>
1818  <P>  <P>
1819    <a href="#subpatternsassubroutines">"Subroutine"</a>
1820    calls (see below) such as (?2) or (?&X) are permitted in lookbehinds, as long
1821    as the subpattern matches a fixed-length string.
1822    <a href="#recursion">Recursion,</a>
1823    however, is not supported.
1824    </P>
1825    <P>
1826  Possessive quantifiers can be used in conjunction with lookbehind assertions to  Possessive quantifiers can be used in conjunction with lookbehind assertions to
1827  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
1828  pattern such as  strings. Consider a simple pattern such as
1829  <pre>  <pre>
1830    abcd$    abcd$
1831  </pre>  </pre>
# Line 1642  preceded by "foo", while Line 1883  preceded by "foo", while
1883  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
1884  characters that are not "999".  characters that are not "999".
1885  <a name="conditions"></a></P>  <a name="conditions"></a></P>
1886  <br><a name="SEC18" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>  <br><a name="SEC19" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>
1887  <P>  <P>
1888  It is possible to cause the matching process to obey a subpattern  It is possible to cause the matching process to obey a subpattern
1889  conditionally or to choose between two alternative subpatterns, depending on  conditionally or to choose between two alternative subpatterns, depending on
1890  the result of an assertion, or whether a previous capturing subpattern matched  the result of an assertion, or whether a specific capturing subpattern has
1891  or not. The two possible forms of conditional subpattern are  already been matched. The two possible forms of conditional subpattern are:
1892  <pre>  <pre>
1893    (?(condition)yes-pattern)    (?(condition)yes-pattern)
1894    (?(condition)yes-pattern|no-pattern)    (?(condition)yes-pattern|no-pattern)
# Line 1665  Checking for a used subpattern by number Line 1906  Checking for a used subpattern by number
1906  </b><br>  </b><br>
1907  <P>  <P>
1908  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
1909  condition is true if the capturing subpattern of that number has previously  condition is true if a capturing subpattern of that number has previously
1910  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
1911  sign. In this case, the subpattern number is relative rather than absolute.  (see the earlier
1912  The most recently opened parentheses can be referenced by (?(-1), the next most  <a href="#recursion">section about duplicate subpattern numbers),</a>
1913  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
1914  refer to subsequent groups with constructs such as (?(+2).  to precede the digits with a plus or minus sign. In this case, the subpattern
1915    number is relative rather than absolute. The most recently opened parentheses
1916    can be referenced by (?(-1), the next most recent by (?(-2), and so on. In
1917    looping constructs it can also make sense to refer to subsequent groups with
1918    constructs such as (?(+2).
1919  </P>  </P>
1920  <P>  <P>
1921  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 1959  names that consist entirely of digits is
1959  Rewriting the above example to use a named subpattern gives this:  Rewriting the above example to use a named subpattern gives this:
1960  <pre>  <pre>
1961    (?&#60;OPEN&#62; \( )?    [^()]+    (?(&#60;OPEN&#62;) \) )    (?&#60;OPEN&#62; \( )?    [^()]+    (?(&#60;OPEN&#62;) \) )
1962    </pre>
1963  </PRE>  If the name used in a condition of this kind is a duplicate, the test is
1964    applied to all subpatterns of the same name, and is true if any one of them has
1965    matched.
1966  </P>  </P>
1967  <br><b>  <br><b>
1968  Checking for pattern recursion  Checking for pattern recursion
# Line 1728  letter R, for example: Line 1975  letter R, for example:
1975  <pre>  <pre>
1976    (?(R3)...) or (?(R&name)...)    (?(R3)...) or (?(R&name)...)
1977  </pre>  </pre>
1978  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
1979  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
1980  stack.  stack. If the name used in a condition of this kind is a duplicate, the test is
1981    applied to all subpatterns of the same name, and is true if any one of them is
1982    the most recent recursion.
1983  </P>  </P>
1984  <P>  <P>
1985  At "top level", all these recursion test conditions are false. Recursive  At "top level", all these recursion test conditions are false.
1986  patterns are described below.  <a href="#recursion">The syntax for recursive patterns</a>
1987    is described below.
1988  </P>  </P>
1989  <br><b>  <br><b>
1990  Defining subpatterns for use by reference only  Defining subpatterns for use by reference only
# Line 1744  If the condition is the string (DEFINE), Line 1994  If the condition is the string (DEFINE),
1994  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
1995  alternative in the subpattern. It is always skipped if control reaches this  alternative in the subpattern. It is always skipped if control reaches this
1996  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
1997  "subroutines" that can be referenced from elsewhere. (The use of "subroutines"  "subroutines" that can be referenced from elsewhere. (The use of
1998    <a href="#subpatternsassubroutines">"subroutines"</a>
1999  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
2000  written like this (ignore whitespace and line breaks):  written like this (ignore whitespace and line breaks):
2001  <pre>  <pre>
# Line 1754  written like this (ignore whitespace and Line 2005  written like this (ignore whitespace and
2005  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
2006  named "byte" is defined. This matches an individual component of an IPv4  named "byte" is defined. This matches an individual component of an IPv4
2007  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
2008  pattern is skipped because DEFINE acts like a false condition.  pattern is skipped because DEFINE acts like a false condition. The rest of the
2009  </P>  pattern uses references to the named group to match the four dot-separated
2010  <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.  
2011  </P>  </P>
2012  <br><b>  <br><b>
2013  Assertion conditions  Assertion conditions
# Line 1780  subject is matched against the first alt Line 2028  subject is matched against the first alt
2028  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
2029  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.
2030  <a name="comments"></a></P>  <a name="comments"></a></P>
2031  <br><a name="SEC19" href="#TOC1">COMMENTS</a><br>  <br><a name="SEC20" href="#TOC1">COMMENTS</a><br>
2032  <P>  <P>
2033  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
2034  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 2039  If the PCRE_EXTENDED option is set, an u
2039  character class introduces a comment that continues to immediately after the  character class introduces a comment that continues to immediately after the
2040  next newline in the pattern.  next newline in the pattern.
2041  <a name="recursion"></a></P>  <a name="recursion"></a></P>
2042  <br><a name="SEC20" href="#TOC1">RECURSIVE PATTERNS</a><br>  <br><a name="SEC21" href="#TOC1">RECURSIVE PATTERNS</a><br>
2043  <P>  <P>
2044  Consider the problem of matching a string in parentheses, allowing for  Consider the problem of matching a string in parentheses, allowing for
2045  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 2062  recursively to the pattern in which it a
2062  Obviously, PCRE cannot support the interpolation of Perl code. Instead, it  Obviously, PCRE cannot support the interpolation of Perl code. Instead, it
2063  supports special syntax for recursion of the entire pattern, and also for  supports special syntax for recursion of the entire pattern, and also for
2064  individual subpattern recursion. After its introduction in PCRE and Python,  individual subpattern recursion. After its introduction in PCRE and Python,
2065  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.
2066  </P>  </P>
2067  <P>  <P>
2068  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
2069  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,
2070  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
2071    <a href="#subpatternsassubroutines">"subroutine"</a>
2072  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
2073  a recursive call of the entire regular expression.  a recursive call of the entire regular expression.
2074  </P>  </P>
2075  <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>  
2076  This PCRE pattern solves the nested parentheses problem (assume the  This PCRE pattern solves the nested parentheses problem (assume the
2077  PCRE_EXTENDED option is set so that white space is ignored):  PCRE_EXTENDED option is set so that white space is ignored):
2078  <pre>  <pre>
2079    \( ( (?&#62;[^()]+) | (?R) )* \)    \( ( [^()]++ | (?R) )* \)
2080  </pre>  </pre>
2081  First it matches an opening parenthesis. Then it matches any number of  First it matches an opening parenthesis. Then it matches any number of
2082  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
2083  match of the pattern itself (that is, a correctly parenthesized substring).  match of the pattern itself (that is, a correctly parenthesized substring).
2084  Finally there is a closing parenthesis.  Finally there is a closing parenthesis. Note the use of a possessive quantifier
2085    to avoid backtracking into sequences of non-parentheses.
2086  </P>  </P>
2087  <P>  <P>
2088  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
2089  pattern, so instead you could use this:  pattern, so instead you could use this:
2090  <pre>  <pre>
2091    ( \( ( (?&#62;[^()]+) | (?1) )* \) )    ( \( ( [^()]++ | (?1) )* \) )
2092  </pre>  </pre>
2093  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
2094  them instead of the whole pattern.  them instead of the whole pattern.
2095  </P>  </P>
2096  <P>  <P>
2097  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
2098  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).
2099  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
2100  most recently opened parentheses preceding the recursion. In other words, a  most recently opened parentheses preceding the recursion. In other words, a
2101  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 2105  it is encountered.
2105  It is also possible to refer to subsequently opened parentheses, by writing  It is also possible to refer to subsequently opened parentheses, by writing
2106  references such as (?+2). However, these cannot be recursive because the  references such as (?+2). However, these cannot be recursive because the
2107  reference is not inside the parentheses that are referenced. They are always  reference is not inside the parentheses that are referenced. They are always
2108  "subroutine" calls, as described in the next section.  <a href="#subpatternsassubroutines">"subroutine"</a>
2109    calls, as described in the next section.
2110  </P>  </P>
2111  <P>  <P>
2112  An alternative approach is to use named parentheses instead. The Perl syntax  An alternative approach is to use named parentheses instead. The Perl syntax
2113  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
2114  could rewrite the above example as follows:  could rewrite the above example as follows:
2115  <pre>  <pre>
2116    (?&#60;pn&#62; \( ( (?&#62;[^()]+) | (?&pn) )* \) )    (?&#60;pn&#62; \( ( [^()]++ | (?&pn) )* \) )
2117  </pre>  </pre>
2118  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
2119  used.  used.
2120  </P>  </P>
2121  <P>  <P>
2122  This particular example pattern that we have been looking at contains nested  This particular example pattern that we have been looking at contains nested
2123  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
2124  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
2125  match. For example, when this pattern is applied to  that do not match. For example, when this pattern is applied to
2126  <pre>  <pre>
2127    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
2128  </pre>  </pre>
2129  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,
2130  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
2131  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
2132  before failure can be reported.  before failure can be reported.
2133  </P>  </P>
2134  <P>  <P>
2135  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
2136  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
2137  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  
2138  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
2139  documentation). If the pattern above is matched against  documentation). If the pattern above is matched against
2140  <pre>  <pre>
2141    (ab(cd)ef)    (ab(cd)ef)
2142  </pre>  </pre>
2143  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
2144  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
2145  <pre>  matched at the top level, its final value is unset, even if it is (temporarily)
2146    \( ( ( (?&#62;[^()]+) | (?R) )* ) \)  set at a deeper level.
2147       ^                        ^  </P>
2148       ^                        ^  <P>
2149  </pre>  If there are more than 15 capturing parentheses in a pattern, PCRE has to
2150  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
2151  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
2152  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.  
2153  </P>  </P>
2154  <P>  <P>
2155  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 2162  recursing), whereas any characters are p
2162  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
2163  different alternatives for the recursive and non-recursive cases. The (?R) item  different alternatives for the recursive and non-recursive cases. The (?R) item
2164  is the actual recursive call.  is the actual recursive call.
2165    <a name="recursiondifference"></a></P>
2166    <br><b>
2167    Recursion difference from Perl
2168    </b><br>
2169    <P>
2170    In PCRE (like Python, but unlike Perl), a recursive subpattern call is always
2171    treated as an atomic group. That is, once it has matched some of the subject
2172    string, it is never re-entered, even if it contains untried alternatives and
2173    there is a subsequent matching failure. This can be illustrated by the
2174    following pattern, which purports to match a palindromic string that contains
2175    an odd number of characters (for example, "a", "aba", "abcba", "abcdcba"):
2176    <pre>
2177      ^(.|(.)(?1)\2)$
2178    </pre>
2179    The idea is that it either matches a single character, or two identical
2180    characters surrounding a sub-palindrome. In Perl, this pattern works; in PCRE
2181    it does not if the pattern is longer than three characters. Consider the
2182    subject string "abcba":
2183    </P>
2184    <P>
2185    At the top level, the first character is matched, but as it is not at the end
2186    of the string, the first alternative fails; the second alternative is taken
2187    and the recursion kicks in. The recursive call to subpattern 1 successfully
2188    matches the next character ("b"). (Note that the beginning and end of line
2189    tests are not part of the recursion).
2190    </P>
2191    <P>
2192    Back at the top level, the next character ("c") is compared with what
2193    subpattern 2 matched, which was "a". This fails. Because the recursion is
2194    treated as an atomic group, there are now no backtracking points, and so the
2195    entire match fails. (Perl is able, at this point, to re-enter the recursion and
2196    try the second alternative.) However, if the pattern is written with the
2197    alternatives in the other order, things are different:
2198    <pre>
2199      ^((.)(?1)\2|.)$
2200    </pre>
2201    This time, the recursing alternative is tried first, and continues to recurse
2202    until it runs out of characters, at which point the recursion fails. But this
2203    time we do have another alternative to try at the higher level. That is the big
2204    difference: in the previous case the remaining alternative is at a deeper
2205    recursion level, which PCRE cannot use.
2206    </P>
2207    <P>
2208    To change the pattern so that matches all palindromic strings, not just those
2209    with an odd number of characters, it is tempting to change the pattern to this:
2210    <pre>
2211      ^((.)(?1)\2|.?)$
2212    </pre>
2213    Again, this works in Perl, but not in PCRE, and for the same reason. When a
2214    deeper recursion has matched a single character, it cannot be entered again in
2215    order to match an empty string. The solution is to separate the two cases, and
2216    write out the odd and even cases as alternatives at the higher level:
2217    <pre>
2218      ^(?:((.)(?1)\2|)|((.)(?3)\4|.))
2219    </pre>
2220    If you want to match typical palindromic phrases, the pattern has to ignore all
2221    non-word characters, which can be done like this:
2222    <pre>
2223      ^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$
2224    </pre>
2225    If run with the PCRE_CASELESS option, this pattern matches phrases such as "A
2226    man, a plan, a canal: Panama!" and it works well in both PCRE and Perl. Note
2227    the use of the possessive quantifier *+ to avoid backtracking into sequences of
2228    non-word characters. Without this, PCRE takes a great deal longer (ten times or
2229    more) to match typical phrases, and Perl takes so long that you think it has
2230    gone into a loop.
2231    </P>
2232    <P>
2233    <b>WARNING</b>: The palindrome-matching patterns above work only if the subject
2234    string does not start with a palindrome that is shorter than the entire string.
2235    For example, although "abcba" is correctly matched, if the subject is "ababa",
2236    PCRE finds the palindrome "aba" at the start, then fails at top level because
2237    the end of the string does not follow. Once again, it cannot jump back into the
2238    recursion to try other alternatives, so the entire match fails.
2239  <a name="subpatternsassubroutines"></a></P>  <a name="subpatternsassubroutines"></a></P>
2240  <br><a name="SEC21" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>  <br><a name="SEC22" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>
2241  <P>  <P>
2242  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
2243  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 2262  is used, it does match "sense and respon
2262  strings. Another example is given in the discussion of DEFINE above.  strings. Another example is given in the discussion of DEFINE above.
2263  </P>  </P>
2264  <P>  <P>
2265  Like recursive subpatterns, a "subroutine" call is always treated as an atomic  Like recursive subpatterns, a subroutine call is always treated as an atomic
2266  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
2267  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
2268  matching failure.  matching failure. Any capturing parentheses that are set during the subroutine
2269    call revert to their previous values afterwards.
2270  </P>  </P>
2271  <P>  <P>
2272  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 2277  changed for different calls. For example
2277  </pre>  </pre>
2278  It matches "abcabc". It does not match "abcABC" because the change of  It matches "abcabc". It does not match "abcABC" because the change of
2279  processing option does not affect the called subpattern.  processing option does not affect the called subpattern.
2280    <a name="onigurumasubroutines"></a></P>
2281    <br><a name="SEC23" href="#TOC1">ONIGURUMA SUBROUTINE SYNTAX</a><br>
2282    <P>
2283    For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
2284    a number enclosed either in angle brackets or single quotes, is an alternative
2285    syntax for referencing a subpattern as a subroutine, possibly recursively. Here
2286    are two of the examples used above, rewritten using this syntax:
2287    <pre>
2288      (?&#60;pn&#62; \( ( (?&#62;[^()]+) | \g&#60;pn&#62; )* \) )
2289      (sens|respons)e and \g'1'ibility
2290    </pre>
2291    PCRE supports an extension to Oniguruma: if a number is preceded by a
2292    plus or a minus sign it is taken as a relative reference. For example:
2293    <pre>
2294      (abc)(?i:\g&#60;-1&#62;)
2295    </pre>
2296    Note that \g{...} (Perl syntax) and \g&#60;...&#62; (Oniguruma syntax) are <i>not</i>
2297    synonymous. The former is a back reference; the latter is a subroutine call.
2298  </P>  </P>
2299  <br><a name="SEC22" href="#TOC1">CALLOUTS</a><br>  <br><a name="SEC24" href="#TOC1">CALLOUTS</a><br>
2300  <P>  <P>
2301  Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl  Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl
2302  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 2330  may cause matching to proceed, to backtr
2330  description of the interface to the callout function is given in the  description of the interface to the callout function is given in the
2331  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
2332  documentation.  documentation.
2333  </P>  <a name="backtrackcontrol"></a></P>
2334  <br><a name="SEC23" href="#TOC1">BACTRACKING CONTROL</a><br>  <br><a name="SEC25" href="#TOC1">BACKTRACKING CONTROL</a><br>
2335  <P>  <P>
2336  Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which  Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which
2337  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 2340  production code should be noted to avoid
2340  remarks apply to the PCRE features described in this section.  remarks apply to the PCRE features described in this section.
2341  </P>  </P>
2342  <P>  <P>
2343  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
2344  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
2345  backtracking algorithm. They cause an error if encountered by  a backtracking algorithm. With the exception of (*FAIL), which behaves like a
2346    failing negative assertion, they cause an error if encountered by
2347  <b>pcre_dfa_exec()</b>.  <b>pcre_dfa_exec()</b>.
2348  </P>  </P>
2349  <P>  <P>
2350    If any of these verbs are used in an assertion or subroutine subpattern
2351    (including recursive subpatterns), their effect is confined to that subpattern;
2352    it does not extend to the surrounding pattern. Note that such subpatterns are
2353    processed as anchored at the point where they are tested.
2354    </P>
2355    <P>
2356  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
2357  parenthesis followed by an asterisk. In Perl, they are generally of the form  parenthesis followed by an asterisk. They are generally of the form
2358  (*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,
2359  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
2360  are two kinds:  letters, digits, and underscores. If the name is empty, that is, if the closing
2361    parenthesis immediately follows the colon, the effect is as if the colon were
2362    not there. Any number of these verbs may occur in a pattern.
2363    </P>
2364    <P>
2365    PCRE contains some optimizations that are used to speed up matching by running
2366    some checks at the start of each match attempt. For example, it may know the
2367    minimum length of matching subject, or that a particular character must be
2368    present. When one of these optimizations suppresses the running of a match, any
2369    included backtracking verbs will not, of course, be processed. You can suppress
2370    the start-of-match optimizations by setting the PCRE_NO_START_OPTIMIZE option
2371    when calling <b>pcre_exec()</b>.
2372  </P>  </P>
2373  <br><b>  <br><b>
2374  Verbs that act immediately  Verbs that act immediately
2375  </b><br>  </b><br>
2376  <P>  <P>
2377  The following verbs act as soon as they are encountered:  The following verbs act as soon as they are encountered. They may not be
2378    followed by a name.
2379  <pre>  <pre>
2380     (*ACCEPT)     (*ACCEPT)
2381  </pre>  </pre>
2382  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
2383  pattern. When inside a recursion, only the innermost pattern is ended  pattern. When inside a recursion, only the innermost pattern is ended
2384  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
2385  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:  
2386  <pre>  <pre>
2387    A(A|B(*ACCEPT)|C)D    A((?:A|B(*ACCEPT)|C)D)
2388  </pre>  </pre>
2389  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
2390  captured.  the outer parentheses.
2391  <pre>  <pre>
2392    (*FAIL) or (*F)    (*FAIL) or (*F)
2393  </pre>  </pre>
# Line 2050  A match with the string "aaaa" always fa Line 2403  A match with the string "aaaa" always fa
2403  each backtrack happens (in this example, 10 times).  each backtrack happens (in this example, 10 times).
2404  </P>  </P>
2405  <br><b>  <br><b>
2406    Recording which path was taken
2407    </b><br>
2408    <P>
2409    There is one verb whose main purpose is to track how a match was arrived at,
2410    though it also has a secondary use in conjunction with advancing the match
2411    starting point (see (*SKIP) below).
2412    <pre>
2413      (*MARK:NAME) or (*:NAME)
2414    </pre>
2415    A name is always required with this verb. There may be as many instances of
2416    (*MARK) as you like in a pattern, and their names do not have to be unique.
2417    </P>
2418    <P>
2419    When a match succeeds, the name of the last-encountered (*MARK) is passed back
2420    to the caller via the <i>pcre_extra</i> data structure, as described in the
2421    <a href="pcreapi.html#extradata">section on <i>pcre_extra</i></a>
2422    in the
2423    <a href="pcreapi.html"><b>pcreapi</b></a>
2424    documentation. No data is returned for a partial match. Here is an example of
2425    <b>pcretest</b> output, where the /K modifier requests the retrieval and
2426    outputting of (*MARK) data:
2427    <pre>
2428      /X(*MARK:A)Y|X(*MARK:B)Z/K
2429      XY
2430       0: XY
2431      MK: A
2432      XZ
2433       0: XZ
2434      MK: B
2435    </pre>
2436    The (*MARK) name is tagged with "MK:" in this output, and in this example it
2437    indicates which of the two alternatives matched. This is a more efficient way
2438    of obtaining this information than putting each alternative in its own
2439    capturing parentheses.
2440    </P>
2441    <P>
2442    A name may also be returned after a failed match if the final path through the
2443    pattern involves (*MARK). However, unless (*MARK) used in conjunction with
2444    (*COMMIT), this is unlikely to happen for an unanchored pattern because, as the
2445    starting point for matching is advanced, the final check is often with an empty
2446    string, causing a failure before (*MARK) is reached. For example:
2447    <pre>
2448      /X(*MARK:A)Y|X(*MARK:B)Z/K
2449      XP
2450      No match
2451    </pre>
2452    There are three potential starting points for this match (starting with X,
2453    starting with P, and with an empty string). If the pattern is anchored, the
2454    result is different:
2455    <pre>
2456      /^X(*MARK:A)Y|^X(*MARK:B)Z/K
2457      XP
2458      No match, mark = B
2459    </pre>
2460    PCRE's start-of-match optimizations can also interfere with this. For example,
2461    if, as a result of a call to <b>pcre_study()</b>, it knows the minimum
2462    subject length for a match, a shorter subject will not be scanned at all.
2463    </P>
2464    <P>
2465    Note that similar anomalies (though different in detail) exist in Perl, no
2466    doubt for the same reasons. The use of (*MARK) data after a failed match of an
2467    unanchored pattern is not recommended, unless (*COMMIT) is involved.
2468    </P>
2469    <br><b>
2470  Verbs that act after backtracking  Verbs that act after backtracking
2471  </b><br>  </b><br>
2472  <P>  <P>
2473  The following verbs do nothing when they are encountered. Matching continues  The following verbs do nothing when they are encountered. Matching continues
2474  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
2475  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
2476    the verb. However, when one of these verbs appears inside an atomic group, its
2477    effect is confined to that group, because once the group has been matched,
2478    there is never any backtracking into it. In this situation, backtracking can
2479    "jump back" to the left of the entire atomic group. (Remember also, as stated
2480    above, that this localization also applies in subroutine calls and assertions.)
2481    </P>
2482    <P>
2483    These verbs differ in exactly what kind of failure occurs when backtracking
2484    reaches them.
2485  <pre>  <pre>
2486    (*COMMIT)    (*COMMIT)
2487  </pre>  </pre>
2488  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
2489  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
2490  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
2491  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
2492  starting point, or not at all. For example:  finding a match at the current starting point, or not at all. For example:
2493  <pre>  <pre>
2494    a+(*COMMIT)b    a+(*COMMIT)b
2495  </pre>  </pre>
2496  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
2497  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
2498  <pre>  recently passed (*MARK) in the path is passed back when (*COMMIT) forces a
2499    (*PRUNE)  match failure.
2500  </pre>  </P>
2501  This verb causes the match to fail at the current position if the rest of the  <P>
2502  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,
2503  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
2504  usual to the left of (*PRUNE), or when matching to the right of (*PRUNE), but  <b>pcretest</b> example:
2505  if there is no match to the right, backtracking cannot cross (*PRUNE).  <pre>
2506  In simple cases, the use of (*PRUNE) is just an alternative to an atomic    /(*COMMIT)abc/
2507  group or possessive quantifier, but there are some uses of (*PRUNE) that cannot    xyzabc
2508  be expressed in any other way.     0: abc
2509      xyzabc\Y
2510      No match
2511    </pre>
2512    PCRE knows that any match must start with "a", so the optimization skips along
2513    the subject to "a" before running the first match attempt, which succeeds. When
2514    the optimization is disabled by the \Y escape in the second subject, the match
2515    starts at "x" and so the (*COMMIT) causes it to fail without trying any other
2516    starting points.
2517    <pre>
2518      (*PRUNE) or (*PRUNE:NAME)
2519    </pre>
2520    This verb causes the match to fail at the current starting position in the
2521    subject if the rest of the pattern does not match. If the pattern is
2522    unanchored, the normal "bumpalong" advance to the next starting character then
2523    happens. Backtracking can occur as usual to the left of (*PRUNE), before it is
2524    reached, or when matching to the right of (*PRUNE), but if there is no match to
2525    the right, backtracking cannot cross (*PRUNE). In simple cases, the use of
2526    (*PRUNE) is just an alternative to an atomic group or possessive quantifier,
2527    but there are some uses of (*PRUNE) that cannot be expressed in any other way.
2528    The behaviour of (*PRUNE:NAME) is the same as (*MARK:NAME)(*PRUNE) when the
2529    match fails completely; the name is passed back if this is the final attempt.
2530    (*PRUNE:NAME) does not pass back a name if the match succeeds. In an anchored
2531    pattern (*PRUNE) has the same effect as (*COMMIT).
2532  <pre>  <pre>
2533    (*SKIP)    (*SKIP)
2534  </pre>  </pre>
2535  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
2536  "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,
2537  subject where (*SKIP) was encountered. (*SKIP) signifies that whatever text  but to the position in the subject where (*SKIP) was encountered. (*SKIP)
2538  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
2539    successful match. Consider:
2540  <pre>  <pre>
2541    a+(*SKIP)b    a+(*SKIP)b
2542  </pre>  </pre>
2543  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
2544  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
2545  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
2546  effect in this example; although it would suppress backtracking during the  effect as this example; although it would suppress backtracking during the
2547  first match attempt, the second attempt would start at the second character  first match attempt, the second attempt would start at the second character
2548  instead of skipping on to "c".  instead of skipping on to "c".
2549  <pre>  <pre>
2550    (*THEN)    (*SKIP:NAME)
2551    </pre>
2552    When (*SKIP) has an associated name, its behaviour is modified. If the
2553    following pattern fails to match, the previous path through the pattern is
2554    searched for the most recent (*MARK) that has the same name. If one is found,
2555    the "bumpalong" advance is to the subject position that corresponds to that
2556    (*MARK) instead of to where (*SKIP) was encountered. If no (*MARK) with a
2557    matching name is found, normal "bumpalong" of one character happens (the
2558    (*SKIP) is ignored).
2559    <pre>
2560      (*THEN) or (*THEN:NAME)
2561  </pre>  </pre>
2562  This verb causes a skip to the next alternation if the rest of the pattern does  This verb causes a skip to the next alternation if the rest of the pattern does
2563  not match. That is, it cancels pending backtracking, but only within the  not match. That is, it cancels pending backtracking, but only within the
# Line 2108  for a pattern-based if-then-else block: Line 2568  for a pattern-based if-then-else block:
2568  </pre>  </pre>
2569  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
2570  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
2571  second alternative and tries COND2, without backtracking into COND1. If (*THEN)  second alternative and tries COND2, without backtracking into COND1. The
2572  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
2573    overall match fails. If (*THEN) is not directly inside an alternation, it acts
2574    like (*PRUNE).
2575  </P>  </P>
2576  <br><a name="SEC24" href="#TOC1">SEE ALSO</a><br>  <br><a name="SEC26" href="#TOC1">SEE ALSO</a><br>
2577  <P>  <P>
2578  <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),
2579    <b>pcresyntax</b>(3), <b>pcre</b>(3).
2580  </P>  </P>
2581  <br><a name="SEC25" href="#TOC1">AUTHOR</a><br>  <br><a name="SEC27" href="#TOC1">AUTHOR</a><br>
2582  <P>  <P>
2583  Philip Hazel  Philip Hazel
2584  <br>  <br>
# Line 2124  University Computing Service Line 2587  University Computing Service
2587  Cambridge CB2 3QH, England.  Cambridge CB2 3QH, England.
2588  <br>  <br>
2589  </P>  </P>
2590  <br><a name="SEC26" href="#TOC1">REVISION</a><br>  <br><a name="SEC28" href="#TOC1">REVISION</a><br>
2591  <P>  <P>
2592  Last updated: 09 August 2007  Last updated: 05 May 2010
2593  <br>  <br>
2594  Copyright &copy; 1997-2007 University of Cambridge.  Copyright &copy; 1997-2010 University of Cambridge.
2595  <br>  <br>
2596  <p>  <p>
2597  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.518

  ViewVC Help
Powered by ViewVC 1.1.5