/[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 91 by nigel, Sat Feb 24 21:41:34 2007 UTC revision 155 by ph10, Tue Apr 24 13:36:11 2007 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">BACKSLASH</a>  <li><a name="TOC2" href="#SEC2">CHARACTERS AND METACHARACTERS</a>
18  <li><a name="TOC3" href="#SEC3">CIRCUMFLEX AND DOLLAR</a>  <li><a name="TOC3" href="#SEC3">BACKSLASH</a>
19  <li><a name="TOC4" href="#SEC4">FULL STOP (PERIOD, DOT)</a>  <li><a name="TOC4" href="#SEC4">CIRCUMFLEX AND DOLLAR</a>
20  <li><a name="TOC5" href="#SEC5">MATCHING A SINGLE BYTE</a>  <li><a name="TOC5" href="#SEC5">FULL STOP (PERIOD, DOT)</a>
21  <li><a name="TOC6" href="#SEC6">SQUARE BRACKETS AND CHARACTER CLASSES</a>  <li><a name="TOC6" href="#SEC6">MATCHING A SINGLE BYTE</a>
22  <li><a name="TOC7" href="#SEC7">POSIX CHARACTER CLASSES</a>  <li><a name="TOC7" href="#SEC7">SQUARE BRACKETS AND CHARACTER CLASSES</a>
23  <li><a name="TOC8" href="#SEC8">VERTICAL BAR</a>  <li><a name="TOC8" href="#SEC8">POSIX CHARACTER CLASSES</a>
24  <li><a name="TOC9" href="#SEC9">INTERNAL OPTION SETTING</a>  <li><a name="TOC9" href="#SEC9">VERTICAL BAR</a>
25  <li><a name="TOC10" href="#SEC10">SUBPATTERNS</a>  <li><a name="TOC10" href="#SEC10">INTERNAL OPTION SETTING</a>
26  <li><a name="TOC11" href="#SEC11">NAMED SUBPATTERNS</a>  <li><a name="TOC11" href="#SEC11">SUBPATTERNS</a>
27  <li><a name="TOC12" href="#SEC12">REPETITION</a>  <li><a name="TOC12" href="#SEC12">NAMED SUBPATTERNS</a>
28  <li><a name="TOC13" href="#SEC13">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a>  <li><a name="TOC13" href="#SEC13">REPETITION</a>
29  <li><a name="TOC14" href="#SEC14">BACK REFERENCES</a>  <li><a name="TOC14" href="#SEC14">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a>
30  <li><a name="TOC15" href="#SEC15">ASSERTIONS</a>  <li><a name="TOC15" href="#SEC15">BACK REFERENCES</a>
31  <li><a name="TOC16" href="#SEC16">CONDITIONAL SUBPATTERNS</a>  <li><a name="TOC16" href="#SEC16">ASSERTIONS</a>
32  <li><a name="TOC17" href="#SEC17">COMMENTS</a>  <li><a name="TOC17" href="#SEC17">CONDITIONAL SUBPATTERNS</a>
33  <li><a name="TOC18" href="#SEC18">RECURSIVE PATTERNS</a>  <li><a name="TOC18" href="#SEC18">COMMENTS</a>
34  <li><a name="TOC19" href="#SEC19">SUBPATTERNS AS SUBROUTINES</a>  <li><a name="TOC19" href="#SEC19">RECURSIVE PATTERNS</a>
35  <li><a name="TOC20" href="#SEC20">CALLOUTS</a>  <li><a name="TOC20" href="#SEC20">SUBPATTERNS AS SUBROUTINES</a>
36    <li><a name="TOC21" href="#SEC21">CALLOUTS</a>
37    <li><a name="TOC22" href="#SEC22">SEE ALSO</a>
38    <li><a name="TOC23" href="#SEC23">AUTHOR</a>
39    <li><a name="TOC24" href="#SEC24">REVISION</a>
40  </ul>  </ul>
41  <br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br>  <br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br>
42  <P>  <P>
# Line 64  and how it differs from the normal funct Line 68  and how it differs from the normal funct
68  <a href="pcrematching.html"><b>pcrematching</b></a>  <a href="pcrematching.html"><b>pcrematching</b></a>
69  page.  page.
70  </P>  </P>
71    <br><a name="SEC2" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br>
72  <P>  <P>
73  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
74  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 90  interpreted in some special way. Line 95  interpreted in some special way.
95  <P>  <P>
96  There are two different sets of metacharacters: those that are recognized  There are two different sets of metacharacters: those that are recognized
97  anywhere in the pattern except within square brackets, and those that are  anywhere in the pattern except within square brackets, and those that are
98  recognized in square brackets. Outside square brackets, the metacharacters are  recognized within square brackets. Outside square brackets, the metacharacters
99  as follows:  are as follows:
100  <pre>  <pre>
101    \      general escape character with several uses    \      general escape character with several uses
102    ^      assert start of string (or line, in multiline mode)    ^      assert start of string (or line, in multiline mode)
# Line 120  a character class the only metacharacter Line 125  a character class the only metacharacter
125  </pre>  </pre>
126  The following sections describe the use of each of the metacharacters.  The following sections describe the use of each of the metacharacters.
127  </P>  </P>
128  <br><a name="SEC2" href="#TOC1">BACKSLASH</a><br>  <br><a name="SEC3" href="#TOC1">BACKSLASH</a><br>
129  <P>  <P>
130  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
131  non-alphanumeric character, it takes away any special meaning that character  non-alphanumeric character, it takes away any special meaning that character
# Line 216  following the discussion of Line 221  following the discussion of
221  <P>  <P>
222  Inside a character class, or if the decimal number is greater than 9 and there  Inside a character class, or if the decimal number is greater than 9 and there
223  have not been that many capturing subpatterns, PCRE re-reads up to three octal  have not been that many capturing subpatterns, PCRE re-reads up to three octal
224  digits following the backslash, ane uses them to generate a data character. Any  digits following the backslash, and uses them to generate a data character. Any
225  subsequent digits stand for themselves. In non-UTF-8 mode, the value of a  subsequent digits stand for themselves. In non-UTF-8 mode, the value of a
226  character specified in octal must be less than \400. In UTF-8 mode, values up  character specified in octal must be less than \400. In UTF-8 mode, values up
227  to \777 are permitted. For example:  to \777 are permitted. For example:
# Line 238  zero, because no more than three octal d Line 243  zero, because no more than three octal d
243  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
244  and outside character classes. In addition, inside a character class, the  and outside character classes. In addition, inside a character class, the
245  sequence \b is interpreted as the backspace character (hex 08), and the  sequence \b is interpreted as the backspace character (hex 08), and the
246  sequence \X is interpreted as the character "X". Outside a character class,  sequences \R and \X are interpreted as the characters "R" and "X",
247  these sequences have different meanings  respectively. Outside a character class, these sequences have different
248    meanings
249  <a href="#uniextseq">(see below).</a>  <a href="#uniextseq">(see below).</a>
250  </P>  </P>
251  <br><b>  <br><b>
252    Absolute and relative back references
253    </b><br>
254    <P>
255    The sequence \g followed by a positive or negative number, optionally enclosed
256    in braces, is an absolute or relative back reference. Back references are
257    discussed
258    <a href="#backreferences">later,</a>
259    following the discussion of
260    <a href="#subpattern">parenthesized subpatterns.</a>
261    </P>
262    <br><b>
263  Generic character types  Generic character types
264  </b><br>  </b><br>
265  <P>  <P>
266  The third use of backslash is for specifying generic character types. The  Another use of backslash is for specifying generic character types. The
267  following are always recognized:  following are always recognized:
268  <pre>  <pre>
269    \d     any decimal digit    \d     any decimal digit
# Line 280  place (see Line 297  place (see
297  <a href="pcreapi.html#localesupport">"Locale support"</a>  <a href="pcreapi.html#localesupport">"Locale support"</a>
298  in the  in the
299  <a href="pcreapi.html"><b>pcreapi</b></a>  <a href="pcreapi.html"><b>pcreapi</b></a>
300  page). For example, in the "fr_FR" (French) locale, some character codes  page). For example, in a French locale such as "fr_FR" in Unix-like systems,
301  greater than 128 are used for accented letters, and these are matched by \w.  or "french" in Windows, some character codes greater than 128 are used for
302    accented letters, and these are matched by \w.
303  </P>  </P>
304  <P>  <P>
305  In UTF-8 mode, characters with values greater than 128 never match \d, \s, or  In UTF-8 mode, characters with values greater than 128 never match \d, \s, or
306  \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
307  character property support is available. The use of locales with Unicode is  character property support is available. The use of locales with Unicode is
308  discouraged.  discouraged.
309    </P>
310    <br><b>
311    Newline sequences
312    </b><br>
313    <P>
314    Outside a character class, the escape sequence \R matches any Unicode newline
315    sequence. This is an extension to Perl. In non-UTF-8 mode \R is equivalent to
316    the following:
317    <pre>
318      (?&#62;\r\n|\n|\x0b|\f|\r|\x85)
319    </pre>
320    This is an example of an "atomic group", details of which are given
321    <a href="#atomicgroup">below.</a>
322    This particular group matches either the two-character sequence CR followed by
323    LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,
324    U+000B), FF (formfeed, U+000C), CR (carriage return, U+000D), or NEL (next
325    line, U+0085). The two-character sequence is treated as a single unit that
326    cannot be split.
327    </P>
328    <P>
329    In UTF-8 mode, two additional characters whose codepoints are greater than 255
330    are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).
331    Unicode character property support is not needed for these characters to be
332    recognized.
333    </P>
334    <P>
335    Inside a character class, \R matches the letter "R".
336  <a name="uniextseq"></a></P>  <a name="uniextseq"></a></P>
337  <br><b>  <br><b>
338  Unicode character properties  Unicode character properties
# Line 321  Those that are not part of an identified Line 366  Those that are not part of an identified
366  <P>  <P>
367  Arabic,  Arabic,
368  Armenian,  Armenian,
369    Balinese,
370  Bengali,  Bengali,
371  Bopomofo,  Bopomofo,
372  Braille,  Braille,
# Line 330  Canadian_Aboriginal, Line 376  Canadian_Aboriginal,
376  Cherokee,  Cherokee,
377  Common,  Common,
378  Coptic,  Coptic,
379    Cuneiform,
380  Cypriot,  Cypriot,
381  Cyrillic,  Cyrillic,
382  Deseret,  Deseret,
# Line 359  Malayalam, Line 406  Malayalam,
406  Mongolian,  Mongolian,
407  Myanmar,  Myanmar,
408  New_Tai_Lue,  New_Tai_Lue,
409    Nko,
410  Ogham,  Ogham,
411  Old_Italic,  Old_Italic,
412  Old_Persian,  Old_Persian,
413  Oriya,  Oriya,
414  Osmanya,  Osmanya,
415    Phags_Pa,
416    Phoenician,
417  Runic,  Runic,
418  Shavian,  Shavian,
419  Sinhala,  Sinhala,
# Line 483  properties in PCRE. Line 533  properties in PCRE.
533  Simple assertions  Simple assertions
534  </b><br>  </b><br>
535  <P>  <P>
536  The fourth use of backslash is for certain simple assertions. An assertion  The final use of backslash is for certain simple assertions. An assertion
537  specifies a condition that has to be met at a particular point in a match,  specifies a condition that has to be met at a particular point in a match,
538  without consuming any characters from the subject string. The use of  without consuming any characters from the subject string. The use of
539  subpatterns for more complicated assertions is described  subpatterns for more complicated assertions is described
# Line 492  The backslashed assertions are: Line 542  The backslashed assertions are:
542  <pre>  <pre>
543    \b     matches at a word boundary    \b     matches at a word boundary
544    \B     matches when not at a word boundary    \B     matches when not at a word boundary
545    \A     matches at start of subject    \A     matches at the start of the subject
546    \Z     matches at end of subject or before newline at end    \Z     matches at the end of the subject
547    \z     matches at end of subject            also matches before a newline at the end of the subject
548    \G     matches at first matching position in subject    \z     matches only at the end of the subject
549      \G     matches at the first matching position in the subject
550  </pre>  </pre>
551  These assertions may not appear in character classes (but note that \b has a  These assertions may not appear in character classes (but note that \b has a
552  different meaning, namely the backspace character, inside a character class).  different meaning, namely the backspace character, inside a character class).
# Line 538  If all the alternatives of a pattern beg Line 589  If all the alternatives of a pattern beg
589  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
590  regular expression.  regular expression.
591  </P>  </P>
592  <br><a name="SEC3" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>  <br><a name="SEC4" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>
593  <P>  <P>
594  Outside a character class, in the default matching mode, the circumflex  Outside a character class, in the default matching mode, the circumflex
595  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 592  Note that the sequences \A, \Z, and \z c Line 643  Note that the sequences \A, \Z, and \z c
643  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
644  \A it is always anchored, whether or not PCRE_MULTILINE is set.  \A it is always anchored, whether or not PCRE_MULTILINE is set.
645  </P>  </P>
646  <br><a name="SEC4" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br>  <br><a name="SEC5" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br>
647  <P>  <P>
648  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
649  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
650  line. In UTF-8 mode, the matched character may be more than one byte long. When  line. In UTF-8 mode, the matched character may be more than one byte long.
651  a line ending is defined as a single character (CR or LF), dot never matches  </P>
652  that character; when the two-character sequence CRLF is used, dot does not  <P>
653  match CR if it is immediately followed by LF, but otherwise it matches all  When a line ending is defined as a single character, dot never matches that
654  characters (including isolated CRs and LFs).  character; when the two-character sequence CRLF is used, dot does not match CR
655    if it is immediately followed by LF, but otherwise it matches all characters
656    (including isolated CRs and LFs). When any Unicode line endings are being
657    recognized, dot does not match CR or LF or any of the other line ending
658    characters.
659  </P>  </P>
660  <P>  <P>
661  The behaviour of dot with regard to newlines can be changed. If the PCRE_DOTALL  The behaviour of dot with regard to newlines can be changed. If the PCRE_DOTALL
662  option is set, a dot matches any one character, without exception. If newline  option is set, a dot matches any one character, without exception. If the
663  is defined as the two-character sequence CRLF, it takes two dots to match it.  two-character sequence CRLF is present in the subject string, it takes two dots
664    to match it.
665  </P>  </P>
666  <P>  <P>
667  The handling of dot is entirely independent of the handling of circumflex and  The handling of dot is entirely independent of the handling of circumflex and
668  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
669  special meaning in a character class.  special meaning in a character class.
670  </P>  </P>
671  <br><a name="SEC5" href="#TOC1">MATCHING A SINGLE BYTE</a><br>  <br><a name="SEC6" href="#TOC1">MATCHING A SINGLE BYTE</a><br>
672  <P>  <P>
673  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
674  in and out of UTF-8 mode. Unlike a dot, it always matches CR and LF. The  in and out of UTF-8 mode. Unlike a dot, it always matches any line-ending
675  feature is provided in Perl in order to match individual bytes in UTF-8 mode.  characters. The feature is provided in Perl in order to match individual bytes
676  Because it breaks up UTF-8 characters into individual bytes, what remains in  in UTF-8 mode. Because it breaks up UTF-8 characters into individual bytes,
677  the string may be a malformed UTF-8 string. For this reason, the \C escape  what remains in the string may be a malformed UTF-8 string. For this reason,
678  sequence is best avoided.  the \C escape sequence is best avoided.
679  </P>  </P>
680  <P>  <P>
681  PCRE does not allow \C to appear in lookbehind assertions  PCRE does not allow \C to appear in lookbehind assertions
# Line 627  PCRE does not allow \C to appear in look Line 683  PCRE does not allow \C to appear in look
683  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
684  the lookbehind.  the lookbehind.
685  <a name="characterclass"></a></P>  <a name="characterclass"></a></P>
686  <br><a name="SEC6" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br>  <br><a name="SEC7" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br>
687  <P>  <P>
688  An opening square bracket introduces a character class, terminated by a closing  An opening square bracket introduces a character class, terminated by a closing
689  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. If a
# Line 670  ensure that PCRE is compiled with Unicod Line 726  ensure that PCRE is compiled with Unicod
726  UTF-8 support.  UTF-8 support.
727  </P>  </P>
728  <P>  <P>
729  Characters that might indicate line breaks (CR and LF) are never treated in any  Characters that might indicate line breaks are never treated in any special way
730  special way when matching character classes, whatever line-ending sequence is  when matching character classes, whatever line-ending sequence is in use, and
731  in use, and whatever setting of the PCRE_DOTALL and PCRE_MULTILINE options is  whatever setting of the PCRE_DOTALL and PCRE_MULTILINE options is used. A class
732  used. A class such as [^a] always matches one of these characters.  such as [^a] always matches one of these characters.
733  </P>  </P>
734  <P>  <P>
735  The minus (hyphen) character can be used to specify a range of characters in a  The minus (hyphen) character can be used to specify a range of characters in a
# Line 701  example [\x{100}-\x{2ff}]. Line 757  example [\x{100}-\x{2ff}].
757  If a range that includes letters is used when caseless matching is set, it  If a range that includes letters is used when caseless matching is set, it
758  matches the letters in either case. For example, [W-c] is equivalent to  matches the letters in either case. For example, [W-c] is equivalent to
759  [][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character  [][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character
760  tables for the "fr_FR" locale are in use, [\xc8-\xcb] matches accented E  tables for a French locale are in use, [\xc8-\xcb] matches accented E
761  characters in both cases. In UTF-8 mode, PCRE supports the concept of case for  characters in both cases. In UTF-8 mode, PCRE supports the concept of case for
762  characters with values greater than 128 only when it is compiled with Unicode  characters with values greater than 128 only when it is compiled with Unicode
763  property support.  property support.
# Line 722  introducing a POSIX class name - see the Line 778  introducing a POSIX class name - see the
778  closing square bracket. However, escaping other non-alphanumeric characters  closing square bracket. However, escaping other non-alphanumeric characters
779  does no harm.  does no harm.
780  </P>  </P>
781  <br><a name="SEC7" href="#TOC1">POSIX CHARACTER CLASSES</a><br>  <br><a name="SEC8" href="#TOC1">POSIX CHARACTER CLASSES</a><br>
782  <P>  <P>
783  Perl supports the POSIX notation for character classes. This uses names  Perl supports the POSIX notation for character classes. This uses names
784  enclosed by [: and :] within the enclosing square brackets. PCRE also supports  enclosed by [: and :] within the enclosing square brackets. PCRE also supports
# Line 768  supported, and an error is given if they Line 824  supported, and an error is given if they
824  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
825  the POSIX character classes.  the POSIX character classes.
826  </P>  </P>
827  <br><a name="SEC8" href="#TOC1">VERTICAL BAR</a><br>  <br><a name="SEC9" href="#TOC1">VERTICAL BAR</a><br>
828  <P>  <P>
829  Vertical bar characters are used to separate alternative patterns. For example,  Vertical bar characters are used to separate alternative patterns. For example,
830  the pattern  the pattern
# Line 783  that succeeds is used. If the alternativ Line 839  that succeeds is used. If the alternativ
839  "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
840  alternative in the subpattern.  alternative in the subpattern.
841  </P>  </P>
842  <br><a name="SEC9" href="#TOC1">INTERNAL OPTION SETTING</a><br>  <br><a name="SEC10" href="#TOC1">INTERNAL OPTION SETTING</a><br>
843  <P>  <P>
844  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
845  PCRE_EXTENDED options can be changed from within the pattern by a sequence of  PCRE_EXTENDED options can be changed from within the pattern by a sequence of
# Line 809  the global options (and it will therefor Line 865  the global options (and it will therefor
865  <b>pcre_fullinfo()</b> function).  <b>pcre_fullinfo()</b> function).
866  </P>  </P>
867  <P>  <P>
868  An option change within a subpattern affects only that part of the current  An option change within a subpattern (see below for a description of
869  pattern that follows it, so  subpatterns) affects only that part of the current pattern that follows it, so
870  <pre>  <pre>
871    (a(?i)b)c    (a(?i)b)c
872  </pre>  </pre>
# Line 831  The PCRE-specific options PCRE_DUPNAMES, Line 887  The PCRE-specific options PCRE_DUPNAMES,
887  changed in the same way as the Perl-compatible options by using the characters  changed in the same way as the Perl-compatible options by using the characters
888  J, U and X respectively.  J, U and X respectively.
889  <a name="subpattern"></a></P>  <a name="subpattern"></a></P>
890  <br><a name="SEC10" href="#TOC1">SUBPATTERNS</a><br>  <br><a name="SEC11" href="#TOC1">SUBPATTERNS</a><br>
891  <P>  <P>
892  Subpatterns are delimited by parentheses (round brackets), which can be nested.  Subpatterns are delimited by parentheses (round brackets), which can be nested.
893  Turning part of a pattern into a subpattern does two things:  Turning part of a pattern into a subpattern does two things:
# Line 842  Turning part of a pattern into a subpatt Line 898  Turning part of a pattern into a subpatt
898    cat(aract|erpillar|)    cat(aract|erpillar|)
899  </pre>  </pre>
900  matches one of the words "cat", "cataract", or "caterpillar". Without the  matches one of the words "cat", "cataract", or "caterpillar". Without the
901  parentheses, it would match "cataract", "erpillar" or the empty string.  parentheses, it would match "cataract", "erpillar" or an empty string.
902  <br>  <br>
903  <br>  <br>
904  2. It sets up the subpattern as a capturing subpattern. This means that, when  2. It sets up the subpattern as a capturing subpattern. This means that, when
# Line 870  the string "the white queen" is matched Line 926  the string "the white queen" is matched
926    the ((?:red|white) (king|queen))    the ((?:red|white) (king|queen))
927  </pre>  </pre>
928  the captured substrings are "white queen" and "queen", and are numbered 1 and  the captured substrings are "white queen" and "queen", and are numbered 1 and
929  2. The maximum number of capturing subpatterns is 65535, and the maximum depth  2. The maximum number of capturing subpatterns is 65535.
 of nesting of all subpatterns, both capturing and non-capturing, is 200.  
930  </P>  </P>
931  <P>  <P>
932  As a convenient shorthand, if any option settings are required at the start of  As a convenient shorthand, if any option settings are required at the start of
# Line 886  from left to right, and options are not Line 941  from left to right, and options are not
941  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
942  the above patterns match "SUNDAY" as well as "Saturday".  the above patterns match "SUNDAY" as well as "Saturday".
943  </P>  </P>
944  <br><a name="SEC11" href="#TOC1">NAMED SUBPATTERNS</a><br>  <br><a name="SEC12" href="#TOC1">NAMED SUBPATTERNS</a><br>
945  <P>  <P>
946  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
947  to keep track of the numbers in complicated regular expressions. Furthermore,  to keep track of the numbers in complicated regular expressions. Furthermore,
948  if an expression is modified, the numbers may change. To help with this  if an expression is modified, the numbers may change. To help with this
949  difficulty, PCRE supports the naming of subpatterns, something that Perl does  difficulty, PCRE supports the naming of subpatterns. This feature was not
950  not provide. The Python syntax (?P&#60;name&#62;...) is used. References to capturing  added to Perl until release 5.10. Python had the feature earlier, and PCRE
951    introduced it at release 4.0, using the Python syntax. PCRE now supports both
952    the Perl and the Python syntax.
953    </P>
954    <P>
955    In PCRE, a subpattern can be named in one of three ways: (?&#60;name&#62;...) or
956    (?'name'...) as in Perl, or (?P&#60;name&#62;...) as in Python. References to capturing
957  parentheses from other parts of the pattern, such as  parentheses from other parts of the pattern, such as
958  <a href="#backreferences">backreferences,</a>  <a href="#backreferences">backreferences,</a>
959  <a href="#recursion">recursion,</a>  <a href="#recursion">recursion,</a>
# Line 902  can be made by name as well as by number Line 963  can be made by name as well as by number
963  </P>  </P>
964  <P>  <P>
965  Names consist of up to 32 alphanumeric characters and underscores. Named  Names consist of up to 32 alphanumeric characters and underscores. Named
966  capturing parentheses are still allocated numbers as well as names. The PCRE  capturing parentheses are still allocated numbers as well as names, exactly as
967  API provides function calls for extracting the name-to-number translation table  if the names were not present. The PCRE API provides function calls for
968  from a compiled pattern. There is also a convenience function for extracting a  extracting the name-to-number translation table from a compiled pattern. There
969  captured substring by name.  is also a convenience function for extracting a captured substring by name.
970  </P>  </P>
971  <P>  <P>
972  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
# Line 915  match. Suppose you want to match the nam Line 976  match. Suppose you want to match the nam
976  abbreviation or as the full name, and in both cases you want to extract the  abbreviation or as the full name, and in both cases you want to extract the
977  abbreviation. This pattern (ignoring the line breaks) does the job:  abbreviation. This pattern (ignoring the line breaks) does the job:
978  <pre>  <pre>
979    (?P&#60;DN&#62;Mon|Fri|Sun)(?:day)?|    (?&#60;DN&#62;Mon|Fri|Sun)(?:day)?|
980    (?P&#60;DN&#62;Tue)(?:sday)?|    (?&#60;DN&#62;Tue)(?:sday)?|
981    (?P&#60;DN&#62;Wed)(?:nesday)?|    (?&#60;DN&#62;Wed)(?:nesday)?|
982    (?P&#60;DN&#62;Thu)(?:rsday)?|    (?&#60;DN&#62;Thu)(?:rsday)?|
983    (?P&#60;DN&#62;Sat)(?:urday)?    (?&#60;DN&#62;Sat)(?:urday)?
984  </pre>  </pre>
985  There are five capturing substrings, but only one is ever set after a match.  There are five capturing substrings, but only one is ever set after a match.
986  The convenience function for extracting the data by name returns the substring  The convenience function for extracting the data by name returns the substring
987  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
988  matched. This saves searching to find which numbered subpattern it was. If you  matched. This saves searching to find which numbered subpattern it was. If you
989  make a reference to a non-unique named subpattern from elsewhere in the  make a reference to a non-unique named subpattern from elsewhere in the
990  pattern, the one that corresponds to the lowest number is used. For further  pattern, the one that corresponds to the lowest number is used. For further
# Line 931  details of the interfaces for handling n Line 992  details of the interfaces for handling n
992  <a href="pcreapi.html"><b>pcreapi</b></a>  <a href="pcreapi.html"><b>pcreapi</b></a>
993  documentation.  documentation.
994  </P>  </P>
995  <br><a name="SEC12" href="#TOC1">REPETITION</a><br>  <br><a name="SEC13" href="#TOC1">REPETITION</a><br>
996  <P>  <P>
997  Repetition is specified by quantifiers, which can follow any of the following  Repetition is specified by quantifiers, which can follow any of the following
998  items:  items:
999  <pre>  <pre>
1000    a literal data character    a literal data character
1001    the . metacharacter    the dot metacharacter
1002    the \C escape sequence    the \C escape sequence
1003    the \X escape sequence (in UTF-8 mode with Unicode properties)    the \X escape sequence (in UTF-8 mode with Unicode properties)
1004      the \R escape sequence
1005    an escape such as \d that matches a single character    an escape such as \d that matches a single character
1006    a character class    a character class
1007    a back reference (see next section)    a back reference (see next section)
# Line 980  The quantifier {0} is permitted, causing Line 1042  The quantifier {0} is permitted, causing
1042  previous item and the quantifier were not present.  previous item and the quantifier were not present.
1043  </P>  </P>
1044  <P>  <P>
1045  For convenience (and historical compatibility) the three most common  For convenience, the three most common quantifiers have single-character
1046  quantifiers have single-character abbreviations:  abbreviations:
1047  <pre>  <pre>
1048    *    is equivalent to {0,}    *    is equivalent to {0,}
1049    +    is equivalent to {1,}    +    is equivalent to {1,}
# Line 1032  which matches one digit by preference, b Line 1094  which matches one digit by preference, b
1094  way the rest of the pattern matches.  way the rest of the pattern matches.
1095  </P>  </P>
1096  <P>  <P>
1097  If the PCRE_UNGREEDY option is set (an option which is not available in Perl),  If the PCRE_UNGREEDY option is set (an option that is not available in Perl),
1098  the quantifiers are not greedy by default, but individual ones can be made  the quantifiers are not greedy by default, but individual ones can be made
1099  greedy by following them with a question mark. In other words, it inverts the  greedy by following them with a question mark. In other words, it inverts the
1100  default behaviour.  default behaviour.
# Line 1044  compiled pattern, in proportion to the s Line 1106  compiled pattern, in proportion to the s
1106  </P>  </P>
1107  <P>  <P>
1108  If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equivalent  If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equivalent
1109  to Perl's /s) is set, thus allowing the . to match newlines, the pattern is  to Perl's /s) is set, thus allowing the dot to match newlines, the pattern is
1110  implicitly anchored, because whatever follows will be tried against every  implicitly anchored, because whatever follows will be tried against every
1111  character position in the subject string, so there is no point in retrying the  character position in the subject string, so there is no point in retrying the
1112  overall match at any position after the first. PCRE normally treats such a  overall match at any position after the first. PCRE normally treats such a
# Line 1058  alternatively using ^ to indicate anchor Line 1120  alternatively using ^ to indicate anchor
1120  <P>  <P>
1121  However, there is one situation where the optimization cannot be used. When .*  However, there is one situation where the optimization cannot be used. When .*
1122  is inside capturing parentheses that are the subject of a backreference  is inside capturing parentheses that are the subject of a backreference
1123  elsewhere in the pattern, a match at the start may fail, and a later one  elsewhere in the pattern, a match at the start may fail where a later one
1124  succeed. Consider, for example:  succeeds. Consider, for example:
1125  <pre>  <pre>
1126    (.*)abc\1    (.*)abc\1
1127  </pre>  </pre>
# Line 1081  example, after Line 1143  example, after
1143  </pre>  </pre>
1144  matches "aba" the value of the second captured substring is "b".  matches "aba" the value of the second captured substring is "b".
1145  <a name="atomicgroup"></a></P>  <a name="atomicgroup"></a></P>
1146  <br><a name="SEC13" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br>  <br><a name="SEC14" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br>
1147  <P>  <P>
1148  With both maximizing and minimizing repetition, failure of what follows  With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
1149  normally causes the repeated item to be re-evaluated to see if a different  repetition, failure of what follows normally causes the repeated item to be
1150  number of repeats allows the rest of the pattern to match. Sometimes it is  re-evaluated to see if a different number of repeats allows the rest of the
1151  useful to prevent this, either to change the nature of the match, or to cause  pattern to match. Sometimes it is useful to prevent this, either to change the
1152  it fail earlier than it otherwise might, when the author of the pattern knows  nature of the match, or to cause it fail earlier than it otherwise might, when
1153  there is no point in carrying on.  the author of the pattern knows there is no point in carrying on.
1154  </P>  </P>
1155  <P>  <P>
1156  Consider, for example, the pattern \d+foo when applied to the subject line  Consider, for example, the pattern \d+foo when applied to the subject line
# Line 1102  item, and then with 4, and so on, before Line 1164  item, and then with 4, and so on, before
1164  that once a subpattern has matched, it is not to be re-evaluated in this way.  that once a subpattern has matched, it is not to be re-evaluated in this way.
1165  </P>  </P>
1166  <P>  <P>
1167  If we use atomic grouping for the previous example, the matcher would give up  If we use atomic grouping for the previous example, the matcher gives up
1168  immediately on failing to match "foo" the first time. The notation is a kind of  immediately on failing to match "foo" the first time. The notation is a kind of
1169  special parenthesis, starting with (?&#62; as in this example:  special parenthesis, starting with (?&#62; as in this example:
1170  <pre>  <pre>
# Line 1137  previous example can be rewritten as Line 1199  previous example can be rewritten as
1199  </pre>  </pre>
1200  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY
1201  option is ignored. They are a convenient notation for the simpler forms of  option is ignored. They are a convenient notation for the simpler forms of
1202  atomic group. However, there is no difference in the meaning or processing of a  atomic group. However, there is no difference in the meaning of a possessive
1203  possessive quantifier and the equivalent atomic group.  quantifier and the equivalent atomic group, though there may be a performance
1204    difference; possessive quantifiers should be slightly faster.
1205    </P>
1206    <P>
1207    The possessive quantifier syntax is an extension to the Perl 5.8 syntax.
1208    Jeffrey Friedl originated the idea (and the name) in the first edition of his
1209    book. Mike McCloskey liked it, so implemented it when he built Sun's Java
1210    package, and PCRE copied it from there. It ultimately found its way into Perl
1211    at release 5.10.
1212  </P>  </P>
1213  <P>  <P>
1214  The possessive quantifier syntax is an extension to the Perl syntax. Jeffrey  PCRE has an optimization that automatically "possessifies" certain simple
1215  Friedl originated the idea (and the name) in the first edition of his book.  pattern constructs. For example, the sequence A+B is treated as A++B because
1216  Mike McCloskey liked it, so implemented it when he built Sun's Java package,  there is no point in backtracking into a sequence of A's when B must follow.
 and PCRE copied it from there.  
1217  </P>  </P>
1218  <P>  <P>
1219  When a pattern contains an unlimited repeat inside a subpattern that can itself  When a pattern contains an unlimited repeat inside a subpattern that can itself
# Line 1173  an atomic group, like this: Line 1242  an atomic group, like this:
1242  </pre>  </pre>
1243  sequences of non-digits cannot be broken, and failure happens quickly.  sequences of non-digits cannot be broken, and failure happens quickly.
1244  <a name="backreferences"></a></P>  <a name="backreferences"></a></P>
1245  <br><a name="SEC14" href="#TOC1">BACK REFERENCES</a><br>  <br><a name="SEC15" href="#TOC1">BACK REFERENCES</a><br>
1246  <P>  <P>
1247  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
1248  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 1190  when a repetition is involved and the su Line 1259  when a repetition is involved and the su
1259  in an earlier iteration.  in an earlier iteration.
1260  </P>  </P>
1261  <P>  <P>
1262  It is not possible to have a numerical "forward back reference" to subpattern  It is not possible to have a numerical "forward back reference" to a subpattern
1263  whose number is 10 or more. However, a back reference to any subpattern is  whose number is 10 or more using this syntax because a sequence such as \50 is
1264  possible using named parentheses (see below). See also the subsection entitled  interpreted as a character defined in octal. See the subsection entitled
1265  "Non-printing characters"  "Non-printing characters"
1266  <a href="#digitsafterbackslash">above</a>  <a href="#digitsafterbackslash">above</a>
1267  for further details of the handling of digits following a backslash.  for further details of the handling of digits following a backslash. There is
1268    no such problem when named parentheses are used. A back reference to any
1269    subpattern is possible using named parentheses (see below).
1270    </P>
1271    <P>
1272    Another way of avoiding the ambiguity inherent in the use of digits following a
1273    backslash is to use the \g escape sequence, which is a feature introduced in
1274    Perl 5.10. This escape must be followed by a positive or a negative number,
1275    optionally enclosed in braces. These examples are all identical:
1276    <pre>
1277      (ring), \1
1278      (ring), \g1
1279      (ring), \g{1}
1280    </pre>
1281    A positive number specifies an absolute reference without the ambiguity that is
1282    present in the older syntax. It is also useful when literal digits follow the
1283    reference. A negative number is a relative reference. Consider this example:
1284    <pre>
1285      (abc(def)ghi)\g{-1}
1286    </pre>
1287    The sequence \g{-1} is a reference to the most recently started capturing
1288    subpattern before \g, that is, is it equivalent to \2. Similarly, \g{-2}
1289    would be equivalent to \1. The use of relative references can be helpful in
1290    long patterns, and also in patterns that are created by joining together
1291    fragments that contain references within themselves.
1292  </P>  </P>
1293  <P>  <P>
1294  A back reference matches whatever actually matched the capturing subpattern in  A back reference matches whatever actually matched the capturing subpattern in
# Line 1216  matches "rah rah" and "RAH RAH", but not Line 1309  matches "rah rah" and "RAH RAH", but not
1309  capturing subpattern is matched caselessly.  capturing subpattern is matched caselessly.
1310  </P>  </P>
1311  <P>  <P>
1312  Back references to named subpatterns use the Python syntax (?P=name). We could  Back references to named subpatterns use the Perl syntax \k&#60;name&#62; or \k'name'
1313  rewrite the above example as follows:  or the Python syntax (?P=name). We could rewrite the above example in either of
1314    the following ways:
1315  <pre>  <pre>
1316      (?&#60;p1&#62;(?i)rah)\s+\k&#60;p1&#62;
1317    (?P&#60;p1&#62;(?i)rah)\s+(?P=p1)    (?P&#60;p1&#62;(?i)rah)\s+(?P=p1)
1318  </pre>  </pre>
1319  A subpattern that is referenced by name may appear in the pattern before or  A subpattern that is referenced by name may appear in the pattern before or
# Line 1255  that the first iteration does not need t Line 1350  that the first iteration does not need t
1350  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
1351  minimum of zero.  minimum of zero.
1352  <a name="bigassertions"></a></P>  <a name="bigassertions"></a></P>
1353  <br><a name="SEC15" href="#TOC1">ASSERTIONS</a><br>  <br><a name="SEC16" href="#TOC1">ASSERTIONS</a><br>
1354  <P>  <P>
1355  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
1356  matching point that does not actually consume any characters. The simple  matching point that does not actually consume any characters. The simple
# Line 1338  lengths, but it is acceptable if rewritt Line 1433  lengths, but it is acceptable if rewritt
1433    (?&#60;=abc|abde)    (?&#60;=abc|abde)
1434  </pre>  </pre>
1435  The implementation of lookbehind assertions is, for each alternative, to  The implementation of lookbehind assertions is, for each alternative, to
1436  temporarily move the current position back by the fixed width and then try to  temporarily move the current position back by the fixed length and then try to
1437  match. If there are insufficient characters before the current position, the  match. If there are insufficient characters before the current position, the
1438  match is deemed to fail.  assertion fails.
1439  </P>  </P>
1440  <P>  <P>
1441  PCRE does not allow the \C escape (which matches a single byte in UTF-8 mode)  PCRE does not allow the \C escape (which matches a single byte in UTF-8 mode)
1442  to appear in lookbehind assertions, because it makes it impossible to calculate  to appear in lookbehind assertions, because it makes it impossible to calculate
1443  the length of the lookbehind. The \X escape, which can match different numbers  the length of the lookbehind. The \X and \R escapes, which can match
1444  of bytes, is also not permitted.  different numbers of bytes, are also not permitted.
1445  </P>  </P>
1446  <P>  <P>
1447  Atomic groups can be used in conjunction with lookbehind assertions to specify  Possessive quantifiers can be used in conjunction with lookbehind assertions to
1448  efficient matching at the end of the subject string. Consider a simple pattern  specify efficient matching at the end of the subject string. Consider a simple
1449  such as  pattern such as
1450  <pre>  <pre>
1451    abcd$    abcd$
1452  </pre>  </pre>
# Line 1367  then all but the last two characters, an Line 1462  then all but the last two characters, an
1462  covers the entire string, from right to left, so we are no better off. However,  covers the entire string, from right to left, so we are no better off. However,
1463  if the pattern is written as  if the pattern is written as
1464  <pre>  <pre>
   ^(?&#62;.*)(?&#60;=abcd)  
 </pre>  
 or, equivalently, using the possessive quantifier syntax,  
 <pre>  
1465    ^.*+(?&#60;=abcd)    ^.*+(?&#60;=abcd)
1466  </pre>  </pre>
1467  there can be no backtracking for the .* item; it can match only the entire  there can be no backtracking for the .*+ item; it can match only the entire
1468  string. The subsequent lookbehind assertion does a single test on the last four  string. The subsequent lookbehind assertion does a single test on the last four
1469  characters. If it fails, the match fails immediately. For long strings, this  characters. If it fails, the match fails immediately. For long strings, this
1470  approach makes a significant difference to the processing time.  approach makes a significant difference to the processing time.
# Line 1413  preceded by "foo", while Line 1504  preceded by "foo", while
1504  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
1505  characters that are not "999".  characters that are not "999".
1506  <a name="conditions"></a></P>  <a name="conditions"></a></P>
1507  <br><a name="SEC16" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>  <br><a name="SEC17" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>
1508  <P>  <P>
1509  It is possible to cause the matching process to obey a subpattern  It is possible to cause the matching process to obey a subpattern
1510  conditionally or to choose between two alternative subpatterns, depending on  conditionally or to choose between two alternative subpatterns, depending on
# Line 1428  no-pattern (if present) is used. If ther Line 1519  no-pattern (if present) is used. If ther
1519  subpattern, a compile-time error occurs.  subpattern, a compile-time error occurs.
1520  </P>  </P>
1521  <P>  <P>
1522  There are three kinds of condition. If the text between the parentheses  There are four kinds of condition: references to subpatterns, references to
1523  consists of a sequence of digits, or a sequence of alphanumeric characters and  recursion, a pseudo-condition called DEFINE, and assertions.
1524  underscores, the condition is satisfied if the capturing subpattern of that  </P>
1525  number or name has previously matched. There is a possible ambiguity here,  <br><b>
1526  because subpattern names may consist entirely of digits. PCRE looks first for a  Checking for a used subpattern by number
1527  named subpattern; if it cannot find one and the text consists entirely of  </b><br>
1528  digits, it looks for a subpattern of that number, which must be greater than  <P>
1529  zero. Using subpattern names that consist entirely of digits is not  If the text between the parentheses consists of a sequence of digits, the
1530  recommended.  condition is true if the capturing subpattern of that number has previously
1531    matched.
1532  </P>  </P>
1533  <P>  <P>
1534  Consider the following pattern, which contains non-significant white space to  Consider the following pattern, which contains non-significant white space to
# Line 1453  or not. If they did, that is, if subject Line 1545  or not. If they did, that is, if subject
1545  the condition is true, and so the yes-pattern is executed and a closing  the condition is true, and so the yes-pattern is executed and a closing
1546  parenthesis is required. Otherwise, since no-pattern is not present, the  parenthesis is required. Otherwise, since no-pattern is not present, the
1547  subpattern matches nothing. In other words, this pattern matches a sequence of  subpattern matches nothing. In other words, this pattern matches a sequence of
1548  non-parentheses, optionally enclosed in parentheses. Rewriting it to use a  non-parentheses, optionally enclosed in parentheses.
1549  named subpattern gives this:  </P>
1550    <br><b>
1551    Checking for a used subpattern by name
1552    </b><br>
1553    <P>
1554    Perl uses the syntax (?(&#60;name&#62;)...) or (?('name')...) to test for a used
1555    subpattern by name. For compatibility with earlier versions of PCRE, which had
1556    this facility before Perl, the syntax (?(name)...) is also recognized. However,
1557    there is a possible ambiguity with this syntax, because subpattern names may
1558    consist entirely of digits. PCRE looks first for a named subpattern; if it
1559    cannot find one and the name consists entirely of digits, PCRE looks for a
1560    subpattern of that number, which must be greater than zero. Using subpattern
1561    names that consist entirely of digits is not recommended.
1562    </P>
1563    <P>
1564    Rewriting the above example to use a named subpattern gives this:
1565  <pre>  <pre>
1566    (?P&#60;OPEN&#62; \( )?    [^()]+    (?(OPEN) \) )    (?&#60;OPEN&#62; \( )?    [^()]+    (?(&#60;OPEN&#62;) \) )
1567  </pre>  
1568    </PRE>
1569    </P>
1570    <br><b>
1571    Checking for pattern recursion
1572    </b><br>
1573    <P>
1574  If the condition is the string (R), and there is no subpattern with the name R,  If the condition is the string (R), and there is no subpattern with the name R,
1575  the condition is satisfied if a recursive call to the pattern or subpattern has  the condition is true if a recursive call to the whole pattern or any
1576  been made. At "top level", the condition is false. This is a PCRE extension.  subpattern has been made. If digits or a name preceded by ampersand follow the
1577  Recursive patterns are described in the next section.  letter R, for example:
1578    <pre>
1579      (?(R3)...) or (?(R&name)...)
1580    </pre>
1581    the condition is true if the most recent recursion is into the subpattern whose
1582    number or name is given. This condition does not check the entire recursion
1583    stack.
1584    </P>
1585    <P>
1586    At "top level", all these recursion test conditions are false. Recursive
1587    patterns are described below.
1588  </P>  </P>
1589    <br><b>
1590    Defining subpatterns for use by reference only
1591    </b><br>
1592  <P>  <P>
1593  If the condition is not a sequence of digits or (R), it must be an assertion.  If the condition is the string (DEFINE), and there is no subpattern with the
1594    name DEFINE, the condition is always false. In this case, there may be only one
1595    alternative in the subpattern. It is always skipped if control reaches this
1596    point in the pattern; the idea of DEFINE is that it can be used to define
1597    "subroutines" that can be referenced from elsewhere. (The use of "subroutines"
1598    is described below.) For example, a pattern to match an IPv4 address could be
1599    written like this (ignore whitespace and line breaks):
1600    <pre>
1601      (?(DEFINE) (?&#60;byte&#62; 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
1602      \b (?&byte) (\.(?&byte)){3} \b
1603    </pre>
1604    The first part of the pattern is a DEFINE group inside which a another group
1605    named "byte" is defined. This matches an individual component of an IPv4
1606    address (a number less than 256). When matching takes place, this part of the
1607    pattern is skipped because DEFINE acts like a false condition.
1608    </P>
1609    <P>
1610    The rest of the pattern uses references to the named group to match the four
1611    dot-separated components of an IPv4 address, insisting on a word boundary at
1612    each end.
1613    </P>
1614    <br><b>
1615    Assertion conditions
1616    </b><br>
1617    <P>
1618    If the condition is not in any of the above formats, it must be an assertion.
1619  This may be a positive or negative lookahead or lookbehind assertion. Consider  This may be a positive or negative lookahead or lookbehind assertion. Consider
1620  this pattern, again containing non-significant white space, and with the two  this pattern, again containing non-significant white space, and with the two
1621  alternatives on the second line:  alternatives on the second line:
# Line 1479  subject is matched against the first alt Line 1630  subject is matched against the first alt
1630  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
1631  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.
1632  <a name="comments"></a></P>  <a name="comments"></a></P>
1633  <br><a name="SEC17" href="#TOC1">COMMENTS</a><br>  <br><a name="SEC18" href="#TOC1">COMMENTS</a><br>
1634  <P>  <P>
1635  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
1636  closing parenthesis. Nested parentheses are not permitted. The characters  closing parenthesis. Nested parentheses are not permitted. The characters
# Line 1490  If the PCRE_EXTENDED option is set, an u Line 1641  If the PCRE_EXTENDED option is set, an u
1641  character class introduces a comment that continues to immediately after the  character class introduces a comment that continues to immediately after the
1642  next newline in the pattern.  next newline in the pattern.
1643  <a name="recursion"></a></P>  <a name="recursion"></a></P>
1644  <br><a name="SEC18" href="#TOC1">RECURSIVE PATTERNS</a><br>  <br><a name="SEC19" href="#TOC1">RECURSIVE PATTERNS</a><br>
1645  <P>  <P>
1646  Consider the problem of matching a string in parentheses, allowing for  Consider the problem of matching a string in parentheses, allowing for
1647  unlimited nested parentheses. Without the use of recursion, the best that can  unlimited nested parentheses. Without the use of recursion, the best that can
1648  be done is to use a pattern that matches up to some fixed depth of nesting. It  be done is to use a pattern that matches up to some fixed depth of nesting. It
1649  is not possible to handle an arbitrary nesting depth. Perl provides a facility  is not possible to handle an arbitrary nesting depth.
1650  that allows regular expressions to recurse (amongst other things). It does this  </P>
1651  by interpolating Perl code in the expression at run time, and the code can  <P>
1652  refer to the expression itself. A Perl pattern to solve the parentheses problem  For some time, Perl has provided a facility that allows regular expressions to
1653  can be created like this:  recurse (amongst other things). It does this by interpolating Perl code in the
1654    expression at run time, and the code can refer to the expression itself. A Perl
1655    pattern using code interpolation to solve the parentheses problem can be
1656    created like this:
1657  <pre>  <pre>
1658    $re = qr{\( (?: (?&#62;[^()]+) | (?p{$re}) )* \)}x;    $re = qr{\( (?: (?&#62;[^()]+) | (?p{$re}) )* \)}x;
1659  </pre>  </pre>
1660  The (?p{...}) item interpolates Perl code at run time, and in this case refers  The (?p{...}) item interpolates Perl code at run time, and in this case refers
1661  recursively to the pattern in which it appears. Obviously, PCRE cannot support  recursively to the pattern in which it appears.
 the interpolation of Perl code. Instead, it supports some special syntax for  
 recursion of the entire pattern, and also for individual subpattern recursion.  
1662  </P>  </P>
1663  <P>  <P>
1664  The special item that consists of (? followed by a number greater than zero and  Obviously, PCRE cannot support the interpolation of Perl code. Instead, it
1665  a closing parenthesis is a recursive call of the subpattern of the given  supports special syntax for recursion of the entire pattern, and also for
1666  number, provided that it occurs inside that subpattern. (If not, it is a  individual subpattern recursion. After its introduction in PCRE and Python,
1667  "subroutine" call, which is described in the next section.) The special item  this kind of recursion was introduced into Perl at release 5.10.
 (?R) is a recursive call of the entire regular expression.  
1668  </P>  </P>
1669  <P>  <P>
1670  A recursive subpattern call is always treated as an atomic group. That is, once  A special item that consists of (? followed by a number greater than zero and a
1671  it has matched some of the subject string, it is never re-entered, even if  closing parenthesis is a recursive call of the subpattern of the given number,
1672  it contains untried alternatives and there is a subsequent matching failure.  provided that it occurs inside that subpattern. (If not, it is a "subroutine"
1673    call, which is described in the next section.) The special item (?R) or (?0) is
1674    a recursive call of the entire regular expression.
1675    </P>
1676    <P>
1677    In PCRE (like Python, but unlike Perl), a recursive subpattern call is always
1678    treated as an atomic group. That is, once it has matched some of the subject
1679    string, it is never re-entered, even if it contains untried alternatives and
1680    there is a subsequent matching failure.
1681  </P>  </P>
1682  <P>  <P>
1683  This PCRE pattern solves the nested parentheses problem (assume the  This PCRE pattern solves the nested parentheses problem (assume the
# Line 1540  pattern, so instead you could use this: Line 1699  pattern, so instead you could use this:
1699  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
1700  them instead of the whole pattern. In a larger pattern, keeping track of  them instead of the whole pattern. In a larger pattern, keeping track of
1701  parenthesis numbers can be tricky. It may be more convenient to use named  parenthesis numbers can be tricky. It may be more convenient to use named
1702  parentheses instead. For this, PCRE uses (?P&#62;name), which is an extension to  parentheses instead. The Perl syntax for this is (?&name); PCRE's earlier
1703  the Python syntax that PCRE uses for named parentheses (Perl does not provide  syntax (?P&#62;name) is also supported. We could rewrite the above example as
1704  named parentheses). We could rewrite the above example as follows:  follows:
1705  <pre>  <pre>
1706    (?P&#60;pn&#62; \( ( (?&#62;[^()]+) | (?P&#62;pn) )* \) )    (?&#60;pn&#62; \( ( (?&#62;[^()]+) | (?&pn) )* \) )
1707  </pre>  </pre>
1708  This particular example pattern contains nested unlimited repeats, and so the  If there is more than one subpattern with the same name, the earliest one is
1709  use of atomic grouping for matching strings of non-parentheses is important  used. This particular example pattern contains nested unlimited repeats, and so
1710    the use of atomic grouping for matching strings of non-parentheses is important
1711  when applying the pattern to strings that do not match. For example, when this  when applying the pattern to strings that do not match. For example, when this
1712  pattern is applied to  pattern is applied to
1713  <pre>  <pre>
# Line 1562  before failure can be reported. Line 1722  before failure can be reported.
1722  At the end of a match, the values set for any capturing subpatterns are those  At the end of a match, the values set for any capturing subpatterns are those
1723  from the outermost level of the recursion at which the subpattern value is set.  from the outermost level of the recursion at which the subpattern value is set.
1724  If you want to obtain intermediate values, a callout function can be used (see  If you want to obtain intermediate values, a callout function can be used (see
1725  the next section and the  below and the
1726  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
1727  documentation). If the pattern above is matched against  documentation). If the pattern above is matched against
1728  <pre>  <pre>
# Line 1593  In this pattern, (?(R) is the start of a Line 1753  In this pattern, (?(R) is the start of a
1753  different alternatives for the recursive and non-recursive cases. The (?R) item  different alternatives for the recursive and non-recursive cases. The (?R) item
1754  is the actual recursive call.  is the actual recursive call.
1755  <a name="subpatternsassubroutines"></a></P>  <a name="subpatternsassubroutines"></a></P>
1756  <br><a name="SEC19" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>  <br><a name="SEC20" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>
1757  <P>  <P>
1758  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
1759  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
1760  subroutine in a programming language. An earlier example pointed out that the  subroutine in a programming language. The "called" subpattern may be defined
1761  pattern  before or after the reference. An earlier example pointed out that the pattern
1762  <pre>  <pre>
1763    (sens|respons)e and \1ibility    (sens|respons)e and \1ibility
1764  </pre>  </pre>
# Line 1608  matches "sense and sensibility" and "res Line 1768  matches "sense and sensibility" and "res
1768    (sens|respons)e and (?1)ibility    (sens|respons)e and (?1)ibility
1769  </pre>  </pre>
1770  is used, it does match "sense and responsibility" as well as the other two  is used, it does match "sense and responsibility" as well as the other two
1771  strings. Such references, if given numerically, must follow the subpattern to  strings. Another example is given in the discussion of DEFINE above.
 which they refer. However, named references can refer to later subpatterns.  
1772  </P>  </P>
1773  <P>  <P>
1774  Like recursive subpatterns, a "subroutine" call is always treated as an atomic  Like recursive subpatterns, a "subroutine" call is always treated as an atomic
# Line 1617  group. That is, once it has matched some Line 1776  group. That is, once it has matched some
1776  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
1777  matching failure.  matching failure.
1778  </P>  </P>
1779  <br><a name="SEC20" href="#TOC1">CALLOUTS</a><br>  <P>
1780    When a subpattern is used as a subroutine, processing options such as
1781    case-independence are fixed when the subpattern is defined. They cannot be
1782    changed for different calls. For example, consider this pattern:
1783    <pre>
1784      (abc)(?i:(?1))
1785    </pre>
1786    It matches "abcabc". It does not match "abcABC" because the change of
1787    processing option does not affect the called subpattern.
1788    </P>
1789    <br><a name="SEC21" href="#TOC1">CALLOUTS</a><br>
1790  <P>  <P>
1791  Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl  Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl
1792  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 1636  function is to be called. If you want to Line 1805  function is to be called. If you want to
1805  can put a number less than 256 after the letter C. The default value is zero.  can put a number less than 256 after the letter C. The default value is zero.
1806  For example, this pattern has two callout points:  For example, this pattern has two callout points:
1807  <pre>  <pre>
1808    (?C1)\dabc(?C2)def    (?C1)abc(?C2)def
1809  </pre>  </pre>
1810  If the PCRE_AUTO_CALLOUT flag is passed to <b>pcre_compile()</b>, callouts are  If the PCRE_AUTO_CALLOUT flag is passed to <b>pcre_compile()</b>, callouts are
1811  automatically installed before each item in the pattern. They are all numbered  automatically installed before each item in the pattern. They are all numbered
# Line 1652  description of the interface to the call Line 1821  description of the interface to the call
1821  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
1822  documentation.  documentation.
1823  </P>  </P>
1824    <br><a name="SEC22" href="#TOC1">SEE ALSO</a><br>
1825  <P>  <P>
1826  Last updated: 06 June 2006  <b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3), <b>pcre</b>(3).
1827    </P>
1828    <br><a name="SEC23" href="#TOC1">AUTHOR</a><br>
1829    <P>
1830    Philip Hazel
1831    <br>
1832    University Computing Service
1833    <br>
1834    Cambridge CB2 3QH, England.
1835    <br>
1836    </P>
1837    <br><a name="SEC24" href="#TOC1">REVISION</a><br>
1838    <P>
1839    Last updated: 06 March 2007
1840    <br>
1841    Copyright &copy; 1997-2007 University of Cambridge.
1842  <br>  <br>
 Copyright &copy; 1997-2006 University of Cambridge.  
1843  <p>  <p>
1844  Return to the <a href="index.html">PCRE index page</a>.  Return to the <a href="index.html">PCRE index page</a>.
1845  </p>  </p>

Legend:
Removed from v.91  
changed lines
  Added in v.155

  ViewVC Help
Powered by ViewVC 1.1.5