/[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 93 by nigel, Sat Feb 24 21:41:42 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  </ul>  </ul>
39  <br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br>  <br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br>
40  <P>  <P>
# Line 64  and how it differs from the normal funct Line 66  and how it differs from the normal funct
66  <a href="pcrematching.html"><b>pcrematching</b></a>  <a href="pcrematching.html"><b>pcrematching</b></a>
67  page.  page.
68  </P>  </P>
69    <br><a name="SEC2" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br>
70  <P>  <P>
71  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
72  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 93  interpreted in some special way.
93  <P>  <P>
94  There are two different sets of metacharacters: those that are recognized  There are two different sets of metacharacters: those that are recognized
95  anywhere in the pattern except within square brackets, and those that are  anywhere in the pattern except within square brackets, and those that are
96  recognized in square brackets. Outside square brackets, the metacharacters are  recognized within square brackets. Outside square brackets, the metacharacters
97  as follows:  are as follows:
98  <pre>  <pre>
99    \      general escape character with several uses    \      general escape character with several uses
100    ^      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 123  a character class the only metacharacter
123  </pre>  </pre>
124  The following sections describe the use of each of the metacharacters.  The following sections describe the use of each of the metacharacters.
125  </P>  </P>
126  <br><a name="SEC2" href="#TOC1">BACKSLASH</a><br>  <br><a name="SEC3" href="#TOC1">BACKSLASH</a><br>
127  <P>  <P>
128  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
129  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 219  following the discussion of
219  <P>  <P>
220  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
221  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
222  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
223  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
224  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
225  to \777 are permitted. For example:  to \777 are permitted. For example:
# Line 238  zero, because no more than three octal d Line 241  zero, because no more than three octal d
241  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
242  and outside character classes. In addition, inside a character class, the  and outside character classes. In addition, inside a character class, the
243  sequence \b is interpreted as the backspace character (hex 08), and the  sequence \b is interpreted as the backspace character (hex 08), and the
244  sequence \X is interpreted as the character "X". Outside a character class,  sequences \R and \X are interpreted as the characters "R" and "X",
245  these sequences have different meanings  respectively. Outside a character class, these sequences have different
246    meanings
247  <a href="#uniextseq">(see below).</a>  <a href="#uniextseq">(see below).</a>
248  </P>  </P>
249  <br><b>  <br><b>
250    Absolute and relative back references
251    </b><br>
252    <P>
253    The sequence \g followed by a positive or negative number, optionally enclosed
254    in braces, is an absolute or relative back reference. Back references are
255    discussed
256    <a href="#backreferences">later,</a>
257    following the discussion of
258    <a href="#subpattern">parenthesized subpatterns.</a>
259    </P>
260    <br><b>
261  Generic character types  Generic character types
262  </b><br>  </b><br>
263  <P>  <P>
264  The third use of backslash is for specifying generic character types. The  Another use of backslash is for specifying generic character types. The
265  following are always recognized:  following are always recognized:
266  <pre>  <pre>
267    \d     any decimal digit    \d     any decimal digit
# Line 288  In UTF-8 mode, characters with values gr Line 303  In UTF-8 mode, characters with values gr
303  \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
304  character property support is available. The use of locales with Unicode is  character property support is available. The use of locales with Unicode is
305  discouraged.  discouraged.
306    </P>
307    <br><b>
308    Newline sequences
309    </b><br>
310    <P>
311    Outside a character class, the escape sequence \R matches any Unicode newline
312    sequence. This is an extension to Perl. In non-UTF-8 mode \R is equivalent to
313    the following:
314    <pre>
315      (?&#62;\r\n|\n|\x0b|\f|\r|\x85)
316    </pre>
317    This is an example of an "atomic group", details of which are given
318    <a href="#atomicgroup">below.</a>
319    This particular group matches either the two-character sequence CR followed by
320    LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,
321    U+000B), FF (formfeed, U+000C), CR (carriage return, U+000D), or NEL (next
322    line, U+0085). The two-character sequence is treated as a single unit that
323    cannot be split.
324    </P>
325    <P>
326    In UTF-8 mode, two additional characters whose codepoints are greater than 255
327    are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).
328    Unicode character property support is not needed for these characters to be
329    recognized.
330    </P>
331    <P>
332    Inside a character class, \R matches the letter "R".
333  <a name="uniextseq"></a></P>  <a name="uniextseq"></a></P>
334  <br><b>  <br><b>
335  Unicode character properties  Unicode character properties
# Line 321  Those that are not part of an identified Line 363  Those that are not part of an identified
363  <P>  <P>
364  Arabic,  Arabic,
365  Armenian,  Armenian,
366    Balinese,
367  Bengali,  Bengali,
368  Bopomofo,  Bopomofo,
369  Braille,  Braille,
# Line 330  Canadian_Aboriginal, Line 373  Canadian_Aboriginal,
373  Cherokee,  Cherokee,
374  Common,  Common,
375  Coptic,  Coptic,
376    Cuneiform,
377  Cypriot,  Cypriot,
378  Cyrillic,  Cyrillic,
379  Deseret,  Deseret,
# Line 359  Malayalam, Line 403  Malayalam,
403  Mongolian,  Mongolian,
404  Myanmar,  Myanmar,
405  New_Tai_Lue,  New_Tai_Lue,
406    Nko,
407  Ogham,  Ogham,
408  Old_Italic,  Old_Italic,
409  Old_Persian,  Old_Persian,
410  Oriya,  Oriya,
411  Osmanya,  Osmanya,
412    Phags_Pa,
413    Phoenician,
414  Runic,  Runic,
415  Shavian,  Shavian,
416  Sinhala,  Sinhala,
# Line 483  properties in PCRE. Line 530  properties in PCRE.
530  Simple assertions  Simple assertions
531  </b><br>  </b><br>
532  <P>  <P>
533  The fourth use of backslash is for certain simple assertions. An assertion  The final use of backslash is for certain simple assertions. An assertion
534  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,
535  without consuming any characters from the subject string. The use of  without consuming any characters from the subject string. The use of
536  subpatterns for more complicated assertions is described  subpatterns for more complicated assertions is described
# Line 492  The backslashed assertions are: Line 539  The backslashed assertions are:
539  <pre>  <pre>
540    \b     matches at a word boundary    \b     matches at a word boundary
541    \B     matches when not at a word boundary    \B     matches when not at a word boundary
542    \A     matches at start of subject    \A     matches at the start of the subject
543    \Z     matches at end of subject or before newline at end    \Z     matches at the end of the subject
544    \z     matches at end of subject            also matches before a newline at the end of the subject
545    \G     matches at first matching position in subject    \z     matches only at the end of the subject
546      \G     matches at the first matching position in the subject
547  </pre>  </pre>
548  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
549  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 586  If all the alternatives of a pattern beg
586  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
587  regular expression.  regular expression.
588  </P>  </P>
589  <br><a name="SEC3" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>  <br><a name="SEC4" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>
590  <P>  <P>
591  Outside a character class, in the default matching mode, the circumflex  Outside a character class, in the default matching mode, the circumflex
592  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 640  Note that the sequences \A, \Z, and \z c
640  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
641  \A it is always anchored, whether or not PCRE_MULTILINE is set.  \A it is always anchored, whether or not PCRE_MULTILINE is set.
642  </P>  </P>
643  <br><a name="SEC4" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br>  <br><a name="SEC5" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br>
644  <P>  <P>
645  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
646  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
647  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.
648  a line ending is defined as a single character (CR or LF), dot never matches  </P>
649  that character; when the two-character sequence CRLF is used, dot does not  <P>
650  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
651  characters (including isolated CRs and LFs).  character; when the two-character sequence CRLF is used, dot does not match CR
652    if it is immediately followed by LF, but otherwise it matches all characters
653    (including isolated CRs and LFs). When any Unicode line endings are being
654    recognized, dot does not match CR or LF or any of the other line ending
655    characters.
656  </P>  </P>
657  <P>  <P>
658  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
659  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
660  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
661    to match it.
662  </P>  </P>
663  <P>  <P>
664  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
665  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
666  special meaning in a character class.  special meaning in a character class.
667  </P>  </P>
668  <br><a name="SEC5" href="#TOC1">MATCHING A SINGLE BYTE</a><br>  <br><a name="SEC6" href="#TOC1">MATCHING A SINGLE BYTE</a><br>
669  <P>  <P>
670  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
671  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
672  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
673  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,
674  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,
675  sequence is best avoided.  the \C escape sequence is best avoided.
676  </P>  </P>
677  <P>  <P>
678  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 680  PCRE does not allow \C to appear in look
680  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
681  the lookbehind.  the lookbehind.
682  <a name="characterclass"></a></P>  <a name="characterclass"></a></P>
683  <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>
684  <P>  <P>
685  An opening square bracket introduces a character class, terminated by a closing  An opening square bracket introduces a character class, terminated by a closing
686  square bracket. A closing square bracket on its own is not special. If a  square bracket. A closing square bracket on its own is not special. If a
# Line 670  ensure that PCRE is compiled with Unicod Line 723  ensure that PCRE is compiled with Unicod
723  UTF-8 support.  UTF-8 support.
724  </P>  </P>
725  <P>  <P>
726  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
727  special way when matching character classes, whatever line-ending sequence is  when matching character classes, whatever line-ending sequence is in use, and
728  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
729  used. A class such as [^a] always matches one of these characters.  such as [^a] always matches one of these characters.
730  </P>  </P>
731  <P>  <P>
732  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 722  introducing a POSIX class name - see the Line 775  introducing a POSIX class name - see the
775  closing square bracket. However, escaping other non-alphanumeric characters  closing square bracket. However, escaping other non-alphanumeric characters
776  does no harm.  does no harm.
777  </P>  </P>
778  <br><a name="SEC7" href="#TOC1">POSIX CHARACTER CLASSES</a><br>  <br><a name="SEC8" href="#TOC1">POSIX CHARACTER CLASSES</a><br>
779  <P>  <P>
780  Perl supports the POSIX notation for character classes. This uses names  Perl supports the POSIX notation for character classes. This uses names
781  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 821  supported, and an error is given if they
821  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
822  the POSIX character classes.  the POSIX character classes.
823  </P>  </P>
824  <br><a name="SEC8" href="#TOC1">VERTICAL BAR</a><br>  <br><a name="SEC9" href="#TOC1">VERTICAL BAR</a><br>
825  <P>  <P>
826  Vertical bar characters are used to separate alternative patterns. For example,  Vertical bar characters are used to separate alternative patterns. For example,
827  the pattern  the pattern
# Line 783  that succeeds is used. If the alternativ Line 836  that succeeds is used. If the alternativ
836  "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
837  alternative in the subpattern.  alternative in the subpattern.
838  </P>  </P>
839  <br><a name="SEC9" href="#TOC1">INTERNAL OPTION SETTING</a><br>  <br><a name="SEC10" href="#TOC1">INTERNAL OPTION SETTING</a><br>
840  <P>  <P>
841  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
842  PCRE_EXTENDED options can be changed from within the pattern by a sequence of  PCRE_EXTENDED options can be changed from within the pattern by a sequence of
# Line 809  the global options (and it will therefor Line 862  the global options (and it will therefor
862  <b>pcre_fullinfo()</b> function).  <b>pcre_fullinfo()</b> function).
863  </P>  </P>
864  <P>  <P>
865  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
866  pattern that follows it, so  subpatterns) affects only that part of the current pattern that follows it, so
867  <pre>  <pre>
868    (a(?i)b)c    (a(?i)b)c
869  </pre>  </pre>
# Line 831  The PCRE-specific options PCRE_DUPNAMES, Line 884  The PCRE-specific options PCRE_DUPNAMES,
884  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
885  J, U and X respectively.  J, U and X respectively.
886  <a name="subpattern"></a></P>  <a name="subpattern"></a></P>
887  <br><a name="SEC10" href="#TOC1">SUBPATTERNS</a><br>  <br><a name="SEC11" href="#TOC1">SUBPATTERNS</a><br>
888  <P>  <P>
889  Subpatterns are delimited by parentheses (round brackets), which can be nested.  Subpatterns are delimited by parentheses (round brackets), which can be nested.
890  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 895  Turning part of a pattern into a subpatt
895    cat(aract|erpillar|)    cat(aract|erpillar|)
896  </pre>  </pre>
897  matches one of the words "cat", "cataract", or "caterpillar". Without the  matches one of the words "cat", "cataract", or "caterpillar". Without the
898  parentheses, it would match "cataract", "erpillar" or the empty string.  parentheses, it would match "cataract", "erpillar" or an empty string.
899  <br>  <br>
900  <br>  <br>
901  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 923  the string "the white queen" is matched
923    the ((?:red|white) (king|queen))    the ((?:red|white) (king|queen))
924  </pre>  </pre>
925  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
926  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.  
927  </P>  </P>
928  <P>  <P>
929  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 938  from left to right, and options are not
938  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
939  the above patterns match "SUNDAY" as well as "Saturday".  the above patterns match "SUNDAY" as well as "Saturday".
940  </P>  </P>
941  <br><a name="SEC11" href="#TOC1">NAMED SUBPATTERNS</a><br>  <br><a name="SEC12" href="#TOC1">NAMED SUBPATTERNS</a><br>
942  <P>  <P>
943  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
944  to keep track of the numbers in complicated regular expressions. Furthermore,  to keep track of the numbers in complicated regular expressions. Furthermore,
945  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
946  difficulty, PCRE supports the naming of subpatterns, something that Perl does  difficulty, PCRE supports the naming of subpatterns. This feature was not
947  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
948    introduced it at release 4.0, using the Python syntax. PCRE now supports both
949    the Perl and the Python syntax.
950    </P>
951    <P>
952    In PCRE, a subpattern can be named in one of three ways: (?&#60;name&#62;...) or
953    (?'name'...) as in Perl, or (?P&#60;name&#62;...) as in Python. References to capturing
954  parentheses from other parts of the pattern, such as  parentheses from other parts of the pattern, such as
955  <a href="#backreferences">backreferences,</a>  <a href="#backreferences">backreferences,</a>
956  <a href="#recursion">recursion,</a>  <a href="#recursion">recursion,</a>
# Line 902  can be made by name as well as by number Line 960  can be made by name as well as by number
960  </P>  </P>
961  <P>  <P>
962  Names consist of up to 32 alphanumeric characters and underscores. Named  Names consist of up to 32 alphanumeric characters and underscores. Named
963  capturing parentheses are still allocated numbers as well as names. The PCRE  capturing parentheses are still allocated numbers as well as names, exactly as
964  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
965  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
966  captured substring by name.  is also a convenience function for extracting a captured substring by name.
967  </P>  </P>
968  <P>  <P>
969  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 973  match. Suppose you want to match the nam
973  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
974  abbreviation. This pattern (ignoring the line breaks) does the job:  abbreviation. This pattern (ignoring the line breaks) does the job:
975  <pre>  <pre>
976    (?P&#60;DN&#62;Mon|Fri|Sun)(?:day)?|    (?&#60;DN&#62;Mon|Fri|Sun)(?:day)?|
977    (?P&#60;DN&#62;Tue)(?:sday)?|    (?&#60;DN&#62;Tue)(?:sday)?|
978    (?P&#60;DN&#62;Wed)(?:nesday)?|    (?&#60;DN&#62;Wed)(?:nesday)?|
979    (?P&#60;DN&#62;Thu)(?:rsday)?|    (?&#60;DN&#62;Thu)(?:rsday)?|
980    (?P&#60;DN&#62;Sat)(?:urday)?    (?&#60;DN&#62;Sat)(?:urday)?
981  </pre>  </pre>
982  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.
983  The convenience function for extracting the data by name returns the substring  The convenience function for extracting the data by name returns the substring
984  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
985  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
986  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
987  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 989  details of the interfaces for handling n
989  <a href="pcreapi.html"><b>pcreapi</b></a>  <a href="pcreapi.html"><b>pcreapi</b></a>
990  documentation.  documentation.
991  </P>  </P>
992  <br><a name="SEC12" href="#TOC1">REPETITION</a><br>  <br><a name="SEC13" href="#TOC1">REPETITION</a><br>
993  <P>  <P>
994  Repetition is specified by quantifiers, which can follow any of the following  Repetition is specified by quantifiers, which can follow any of the following
995  items:  items:
996  <pre>  <pre>
997    a literal data character    a literal data character
998    the . metacharacter    the dot metacharacter
999    the \C escape sequence    the \C escape sequence
1000    the \X escape sequence (in UTF-8 mode with Unicode properties)    the \X escape sequence (in UTF-8 mode with Unicode properties)
1001      the \R escape sequence
1002    an escape such as \d that matches a single character    an escape such as \d that matches a single character
1003    a character class    a character class
1004    a back reference (see next section)    a back reference (see next section)
# Line 980  The quantifier {0} is permitted, causing Line 1039  The quantifier {0} is permitted, causing
1039  previous item and the quantifier were not present.  previous item and the quantifier were not present.
1040  </P>  </P>
1041  <P>  <P>
1042  For convenience (and historical compatibility) the three most common  For convenience, the three most common quantifiers have single-character
1043  quantifiers have single-character abbreviations:  abbreviations:
1044  <pre>  <pre>
1045    *    is equivalent to {0,}    *    is equivalent to {0,}
1046    +    is equivalent to {1,}    +    is equivalent to {1,}
# Line 1032  which matches one digit by preference, b Line 1091  which matches one digit by preference, b
1091  way the rest of the pattern matches.  way the rest of the pattern matches.
1092  </P>  </P>
1093  <P>  <P>
1094  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),
1095  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
1096  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
1097  default behaviour.  default behaviour.
# Line 1044  compiled pattern, in proportion to the s Line 1103  compiled pattern, in proportion to the s
1103  </P>  </P>
1104  <P>  <P>
1105  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
1106  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
1107  implicitly anchored, because whatever follows will be tried against every  implicitly anchored, because whatever follows will be tried against every
1108  character position in the subject string, so there is no point in retrying the  character position in the subject string, so there is no point in retrying the
1109  overall match at any position after the first. PCRE normally treats such a  overall match at any position after the first. PCRE normally treats such a
# Line 1058  alternatively using ^ to indicate anchor Line 1117  alternatively using ^ to indicate anchor
1117  <P>  <P>
1118  However, there is one situation where the optimization cannot be used. When .*  However, there is one situation where the optimization cannot be used. When .*
1119  is inside capturing parentheses that are the subject of a backreference  is inside capturing parentheses that are the subject of a backreference
1120  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
1121  succeed. Consider, for example:  succeeds. Consider, for example:
1122  <pre>  <pre>
1123    (.*)abc\1    (.*)abc\1
1124  </pre>  </pre>
# Line 1081  example, after Line 1140  example, after
1140  </pre>  </pre>
1141  matches "aba" the value of the second captured substring is "b".  matches "aba" the value of the second captured substring is "b".
1142  <a name="atomicgroup"></a></P>  <a name="atomicgroup"></a></P>
1143  <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>
1144  <P>  <P>
1145  With both maximizing and minimizing repetition, failure of what follows  With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
1146  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
1147  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
1148  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
1149  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
1150  there is no point in carrying on.  the author of the pattern knows there is no point in carrying on.
1151  </P>  </P>
1152  <P>  <P>
1153  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 1161  item, and then with 4, and so on, before
1161  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.
1162  </P>  </P>
1163  <P>  <P>
1164  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
1165  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
1166  special parenthesis, starting with (?&#62; as in this example:  special parenthesis, starting with (?&#62; as in this example:
1167  <pre>  <pre>
# Line 1137  previous example can be rewritten as Line 1196  previous example can be rewritten as
1196  </pre>  </pre>
1197  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY
1198  option is ignored. They are a convenient notation for the simpler forms of  option is ignored. They are a convenient notation for the simpler forms of
1199  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
1200  possessive quantifier and the equivalent atomic group.  quantifier and the equivalent atomic group, though there may be a performance
1201    difference; possessive quantifiers should be slightly faster.
1202    </P>
1203    <P>
1204    The possessive quantifier syntax is an extension to the Perl 5.8 syntax.
1205    Jeffrey Friedl originated the idea (and the name) in the first edition of his
1206    book. Mike McCloskey liked it, so implemented it when he built Sun's Java
1207    package, and PCRE copied it from there. It ultimately found its way into Perl
1208    at release 5.10.
1209  </P>  </P>
1210  <P>  <P>
1211  The possessive quantifier syntax is an extension to the Perl syntax. Jeffrey  PCRE has an optimization that automatically "possessifies" certain simple
1212  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
1213  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.  
1214  </P>  </P>
1215  <P>  <P>
1216  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 1239  an atomic group, like this:
1239  </pre>  </pre>
1240  sequences of non-digits cannot be broken, and failure happens quickly.  sequences of non-digits cannot be broken, and failure happens quickly.
1241  <a name="backreferences"></a></P>  <a name="backreferences"></a></P>
1242  <br><a name="SEC14" href="#TOC1">BACK REFERENCES</a><br>  <br><a name="SEC15" href="#TOC1">BACK REFERENCES</a><br>
1243  <P>  <P>
1244  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
1245  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 1256  when a repetition is involved and the su
1256  in an earlier iteration.  in an earlier iteration.
1257  </P>  </P>
1258  <P>  <P>
1259  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
1260  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
1261  possible using named parentheses (see below). See also the subsection entitled  interpreted as a character defined in octal. See the subsection entitled
1262  "Non-printing characters"  "Non-printing characters"
1263  <a href="#digitsafterbackslash">above</a>  <a href="#digitsafterbackslash">above</a>
1264  for further details of the handling of digits following a backslash.  for further details of the handling of digits following a backslash. There is
1265    no such problem when named parentheses are used. A back reference to any
1266    subpattern is possible using named parentheses (see below).
1267    </P>
1268    <P>
1269    Another way of avoiding the ambiguity inherent in the use of digits following a
1270    backslash is to use the \g escape sequence, which is a feature introduced in
1271    Perl 5.10. This escape must be followed by a positive or a negative number,
1272    optionally enclosed in braces. These examples are all identical:
1273    <pre>
1274      (ring), \1
1275      (ring), \g1
1276      (ring), \g{1}
1277    </pre>
1278    A positive number specifies an absolute reference without the ambiguity that is
1279    present in the older syntax. It is also useful when literal digits follow the
1280    reference. A negative number is a relative reference. Consider this example:
1281    <pre>
1282      (abc(def)ghi)\g{-1}
1283    </pre>
1284    The sequence \g{-1} is a reference to the most recently started capturing
1285    subpattern before \g, that is, is it equivalent to \2. Similarly, \g{-2}
1286    would be equivalent to \1. The use of relative references can be helpful in
1287    long patterns, and also in patterns that are created by joining together
1288    fragments that contain references within themselves.
1289  </P>  </P>
1290  <P>  <P>
1291  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 1306  matches "rah rah" and "RAH RAH", but not
1306  capturing subpattern is matched caselessly.  capturing subpattern is matched caselessly.
1307  </P>  </P>
1308  <P>  <P>
1309  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'
1310  rewrite the above example as follows:  or the Python syntax (?P=name). We could rewrite the above example in either of
1311    the following ways:
1312  <pre>  <pre>
1313      (?&#60;p1&#62;(?i)rah)\s+\k&#60;p1&#62;
1314    (?P&#60;p1&#62;(?i)rah)\s+(?P=p1)    (?P&#60;p1&#62;(?i)rah)\s+(?P=p1)
1315  </pre>  </pre>
1316  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 1347  that the first iteration does not need t
1347  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
1348  minimum of zero.  minimum of zero.
1349  <a name="bigassertions"></a></P>  <a name="bigassertions"></a></P>
1350  <br><a name="SEC15" href="#TOC1">ASSERTIONS</a><br>  <br><a name="SEC16" href="#TOC1">ASSERTIONS</a><br>
1351  <P>  <P>
1352  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
1353  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 1430  lengths, but it is acceptable if rewritt
1430    (?&#60;=abc|abde)    (?&#60;=abc|abde)
1431  </pre>  </pre>
1432  The implementation of lookbehind assertions is, for each alternative, to  The implementation of lookbehind assertions is, for each alternative, to
1433  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
1434  match. If there are insufficient characters before the current position, the  match. If there are insufficient characters before the current position, the
1435  match is deemed to fail.  assertion fails.
1436  </P>  </P>
1437  <P>  <P>
1438  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)
1439  to appear in lookbehind assertions, because it makes it impossible to calculate  to appear in lookbehind assertions, because it makes it impossible to calculate
1440  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
1441  of bytes, is also not permitted.  different numbers of bytes, are also not permitted.
1442  </P>  </P>
1443  <P>  <P>
1444  Atomic groups can be used in conjunction with lookbehind assertions to specify  Possessive quantifiers can be used in conjunction with lookbehind assertions to
1445  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
1446  such as  pattern such as
1447  <pre>  <pre>
1448    abcd$    abcd$
1449  </pre>  </pre>
# Line 1367  then all but the last two characters, an Line 1459  then all but the last two characters, an
1459  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,
1460  if the pattern is written as  if the pattern is written as
1461  <pre>  <pre>
   ^(?&#62;.*)(?&#60;=abcd)  
 </pre>  
 or, equivalently, using the possessive quantifier syntax,  
 <pre>  
1462    ^.*+(?&#60;=abcd)    ^.*+(?&#60;=abcd)
1463  </pre>  </pre>
1464  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
1465  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
1466  characters. If it fails, the match fails immediately. For long strings, this  characters. If it fails, the match fails immediately. For long strings, this
1467  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 1501  preceded by "foo", while
1501  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
1502  characters that are not "999".  characters that are not "999".
1503  <a name="conditions"></a></P>  <a name="conditions"></a></P>
1504  <br><a name="SEC16" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>  <br><a name="SEC17" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>
1505  <P>  <P>
1506  It is possible to cause the matching process to obey a subpattern  It is possible to cause the matching process to obey a subpattern
1507  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 1516  no-pattern (if present) is used. If ther
1516  subpattern, a compile-time error occurs.  subpattern, a compile-time error occurs.
1517  </P>  </P>
1518  <P>  <P>
1519  There are three kinds of condition. If the text between the parentheses  There are four kinds of condition: references to subpatterns, references to
1520  consists of a sequence of digits, or a sequence of alphanumeric characters and  recursion, a pseudo-condition called DEFINE, and assertions.
1521  underscores, the condition is satisfied if the capturing subpattern of that  </P>
1522  number or name has previously matched. There is a possible ambiguity here,  <br><b>
1523  because subpattern names may consist entirely of digits. PCRE looks first for a  Checking for a used subpattern by number
1524  named subpattern; if it cannot find one and the text consists entirely of  </b><br>
1525  digits, it looks for a subpattern of that number, which must be greater than  <P>
1526  zero. Using subpattern names that consist entirely of digits is not  If the text between the parentheses consists of a sequence of digits, the
1527  recommended.  condition is true if the capturing subpattern of that number has previously
1528    matched.
1529  </P>  </P>
1530  <P>  <P>
1531  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 1542  or not. If they did, that is, if subject
1542  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
1543  parenthesis is required. Otherwise, since no-pattern is not present, the  parenthesis is required. Otherwise, since no-pattern is not present, the
1544  subpattern matches nothing. In other words, this pattern matches a sequence of  subpattern matches nothing. In other words, this pattern matches a sequence of
1545  non-parentheses, optionally enclosed in parentheses. Rewriting it to use a  non-parentheses, optionally enclosed in parentheses.
1546  named subpattern gives this:  </P>
1547    <br><b>
1548    Checking for a used subpattern by name
1549    </b><br>
1550    <P>
1551    Perl uses the syntax (?(&#60;name&#62;)...) or (?('name')...) to test for a used
1552    subpattern by name. For compatibility with earlier versions of PCRE, which had
1553    this facility before Perl, the syntax (?(name)...) is also recognized. However,
1554    there is a possible ambiguity with this syntax, because subpattern names may
1555    consist entirely of digits. PCRE looks first for a named subpattern; if it
1556    cannot find one and the name consists entirely of digits, PCRE looks for a
1557    subpattern of that number, which must be greater than zero. Using subpattern
1558    names that consist entirely of digits is not recommended.
1559    </P>
1560    <P>
1561    Rewriting the above example to use a named subpattern gives this:
1562  <pre>  <pre>
1563    (?P&#60;OPEN&#62; \( )?    [^()]+    (?(OPEN) \) )    (?&#60;OPEN&#62; \( )?    [^()]+    (?(&#60;OPEN&#62;) \) )
1564  </pre>  
1565    </PRE>
1566    </P>
1567    <br><b>
1568    Checking for pattern recursion
1569    </b><br>
1570    <P>
1571  If the condition is the string (R), and there is no subpattern with the name R,  If the condition is the string (R), and there is no subpattern with the name R,
1572  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
1573  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
1574  Recursive patterns are described in the next section.  letter R, for example:
1575    <pre>
1576      (?(R3)...) or (?(R&name)...)
1577    </pre>
1578    the condition is true if the most recent recursion is into the subpattern whose
1579    number or name is given. This condition does not check the entire recursion
1580    stack.
1581    </P>
1582    <P>
1583    At "top level", all these recursion test conditions are false. Recursive
1584    patterns are described below.
1585    </P>
1586    <br><b>
1587    Defining subpatterns for use by reference only
1588    </b><br>
1589    <P>
1590    If the condition is the string (DEFINE), and there is no subpattern with the
1591    name DEFINE, the condition is always false. In this case, there may be only one
1592    alternative in the subpattern. It is always skipped if control reaches this
1593    point in the pattern; the idea of DEFINE is that it can be used to define
1594    "subroutines" that can be referenced from elsewhere. (The use of "subroutines"
1595    is described below.) For example, a pattern to match an IPv4 address could be
1596    written like this (ignore whitespace and line breaks):
1597    <pre>
1598      (?(DEFINE) (?&#60;byte&#62; 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
1599      \b (?&byte) (\.(?&byte)){3} \b
1600    </pre>
1601    The first part of the pattern is a DEFINE group inside which a another group
1602    named "byte" is defined. This matches an individual component of an IPv4
1603    address (a number less than 256). When matching takes place, this part of the
1604    pattern is skipped because DEFINE acts like a false condition.
1605    </P>
1606    <P>
1607    The rest of the pattern uses references to the named group to match the four
1608    dot-separated components of an IPv4 address, insisting on a word boundary at
1609    each end.
1610  </P>  </P>
1611    <br><b>
1612    Assertion conditions
1613    </b><br>
1614  <P>  <P>
1615  If the condition is not a sequence of digits or (R), it must be an assertion.  If the condition is not in any of the above formats, it must be an assertion.
1616  This may be a positive or negative lookahead or lookbehind assertion. Consider  This may be a positive or negative lookahead or lookbehind assertion. Consider
1617  this pattern, again containing non-significant white space, and with the two  this pattern, again containing non-significant white space, and with the two
1618  alternatives on the second line:  alternatives on the second line:
# Line 1479  subject is matched against the first alt Line 1627  subject is matched against the first alt
1627  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
1628  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.
1629  <a name="comments"></a></P>  <a name="comments"></a></P>
1630  <br><a name="SEC17" href="#TOC1">COMMENTS</a><br>  <br><a name="SEC18" href="#TOC1">COMMENTS</a><br>
1631  <P>  <P>
1632  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
1633  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 1638  If the PCRE_EXTENDED option is set, an u
1638  character class introduces a comment that continues to immediately after the  character class introduces a comment that continues to immediately after the
1639  next newline in the pattern.  next newline in the pattern.
1640  <a name="recursion"></a></P>  <a name="recursion"></a></P>
1641  <br><a name="SEC18" href="#TOC1">RECURSIVE PATTERNS</a><br>  <br><a name="SEC19" href="#TOC1">RECURSIVE PATTERNS</a><br>
1642  <P>  <P>
1643  Consider the problem of matching a string in parentheses, allowing for  Consider the problem of matching a string in parentheses, allowing for
1644  unlimited nested parentheses. Without the use of recursion, the best that can  unlimited nested parentheses. Without the use of recursion, the best that can
1645  be done is to use a pattern that matches up to some fixed depth of nesting. It  be done is to use a pattern that matches up to some fixed depth of nesting. It
1646  is not possible to handle an arbitrary nesting depth. Perl provides a facility  is not possible to handle an arbitrary nesting depth.
1647  that allows regular expressions to recurse (amongst other things). It does this  </P>
1648  by interpolating Perl code in the expression at run time, and the code can  <P>
1649  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
1650  can be created like this:  recurse (amongst other things). It does this by interpolating Perl code in the
1651    expression at run time, and the code can refer to the expression itself. A Perl
1652    pattern using code interpolation to solve the parentheses problem can be
1653    created like this:
1654  <pre>  <pre>
1655    $re = qr{\( (?: (?&#62;[^()]+) | (?p{$re}) )* \)}x;    $re = qr{\( (?: (?&#62;[^()]+) | (?p{$re}) )* \)}x;
1656  </pre>  </pre>
1657  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
1658  recursively to the pattern in which it appears. Obviously, PCRE cannot support  recursively to the pattern in which it appears.
1659  the interpolation of Perl code. Instead, it supports some special syntax for  </P>
1660  recursion of the entire pattern, and also for individual subpattern recursion.  <P>
1661    Obviously, PCRE cannot support the interpolation of Perl code. Instead, it
1662    supports special syntax for recursion of the entire pattern, and also for
1663    individual subpattern recursion. After its introduction in PCRE and Python,
1664    this kind of recursion was introduced into Perl at release 5.10.
1665  </P>  </P>
1666  <P>  <P>
1667  The special item that consists of (? followed by a number greater than zero and  A special item that consists of (? followed by a number greater than zero and a
1668  a closing parenthesis is a recursive call of the subpattern of the given  closing parenthesis is a recursive call of the subpattern of the given number,
1669  number, provided that it occurs inside that subpattern. (If not, it is a  provided that it occurs inside that subpattern. (If not, it is a "subroutine"
1670  "subroutine" call, which is described in the next section.) The special item  call, which is described in the next section.) The special item (?R) or (?0) is
1671  (?R) is a recursive call of the entire regular expression.  a recursive call of the entire regular expression.
1672  </P>  </P>
1673  <P>  <P>
1674  A recursive subpattern call is always treated as an atomic group. That is, once  In PCRE (like Python, but unlike Perl), a recursive subpattern call is always
1675  it has matched some of the subject string, it is never re-entered, even if  treated as an atomic group. That is, once it has matched some of the subject
1676  it contains untried alternatives and there is a subsequent matching failure.  string, it is never re-entered, even if it contains untried alternatives and
1677    there is a subsequent matching failure.
1678  </P>  </P>
1679  <P>  <P>
1680  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 1696  pattern, so instead you could use this:
1696  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
1697  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
1698  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
1699  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
1700  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
1701  named parentheses). We could rewrite the above example as follows:  follows:
1702  <pre>  <pre>
1703    (?P&#60;pn&#62; \( ( (?&#62;[^()]+) | (?P&#62;pn) )* \) )    (?&#60;pn&#62; \( ( (?&#62;[^()]+) | (?&pn) )* \) )
1704  </pre>  </pre>
1705  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
1706  use of atomic grouping for matching strings of non-parentheses is important  used. This particular example pattern contains nested unlimited repeats, and so
1707    the use of atomic grouping for matching strings of non-parentheses is important
1708  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
1709  pattern is applied to  pattern is applied to
1710  <pre>  <pre>
# Line 1562  before failure can be reported. Line 1719  before failure can be reported.
1719  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
1720  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.
1721  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
1722  the next section and the  below and the
1723  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
1724  documentation). If the pattern above is matched against  documentation). If the pattern above is matched against
1725  <pre>  <pre>
# Line 1593  In this pattern, (?(R) is the start of a Line 1750  In this pattern, (?(R) is the start of a
1750  different alternatives for the recursive and non-recursive cases. The (?R) item  different alternatives for the recursive and non-recursive cases. The (?R) item
1751  is the actual recursive call.  is the actual recursive call.
1752  <a name="subpatternsassubroutines"></a></P>  <a name="subpatternsassubroutines"></a></P>
1753  <br><a name="SEC19" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>  <br><a name="SEC20" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>
1754  <P>  <P>
1755  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
1756  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
1757  subroutine in a programming language. An earlier example pointed out that the  subroutine in a programming language. The "called" subpattern may be defined
1758  pattern  before or after the reference. An earlier example pointed out that the pattern
1759  <pre>  <pre>
1760    (sens|respons)e and \1ibility    (sens|respons)e and \1ibility
1761  </pre>  </pre>
# Line 1608  matches "sense and sensibility" and "res Line 1765  matches "sense and sensibility" and "res
1765    (sens|respons)e and (?1)ibility    (sens|respons)e and (?1)ibility
1766  </pre>  </pre>
1767  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
1768  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.  
1769  </P>  </P>
1770  <P>  <P>
1771  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 1773  group. That is, once it has matched some
1773  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
1774  matching failure.  matching failure.
1775  </P>  </P>
1776  <br><a name="SEC20" href="#TOC1">CALLOUTS</a><br>  <P>
1777    When a subpattern is used as a subroutine, processing options such as
1778    case-independence are fixed when the subpattern is defined. They cannot be
1779    changed for different calls. For example, consider this pattern:
1780    <pre>
1781      (abc)(?i:(?1))
1782    </pre>
1783    It matches "abcabc". It does not match "abcABC" because the change of
1784    processing option does not affect the called subpattern.
1785    </P>
1786    <br><a name="SEC21" href="#TOC1">CALLOUTS</a><br>
1787  <P>  <P>
1788  Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl  Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl
1789  code to be obeyed in the middle of matching a regular expression. This makes it  code to be obeyed in the middle of matching a regular expression. This makes it
# Line 1652  description of the interface to the call Line 1818  description of the interface to the call
1818  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
1819  documentation.  documentation.
1820  </P>  </P>
1821    <br><a name="SEC22" href="#TOC1">SEE ALSO</a><br>
1822    <P>
1823    <b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3), <b>pcre</b>(3).
1824    </P>
1825  <P>  <P>
1826  Last updated: 06 June 2006  Last updated: 06 December 2006
1827  <br>  <br>
1828  Copyright &copy; 1997-2006 University of Cambridge.  Copyright &copy; 1997-2006 University of Cambridge.
1829  <p>  <p>

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12