/[pcre]/code/trunk/doc/pcre.txt
ViewVC logotype

Diff of /code/trunk/doc/pcre.txt

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

revision 518 by ph10, Tue May 18 15:47:01 2010 UTC revision 535 by ph10, Thu Jun 3 19:18:24 2010 UTC
# Line 2  Line 2 
2  This file contains a concatenation of the PCRE man pages, converted to plain  This file contains a concatenation of the PCRE man pages, converted to plain
3  text format for ease of searching with a text editor, or for use on systems  text format for ease of searching with a text editor, or for use on systems
4  that do not have a man page processor. The small individual files that give  that do not have a man page processor. The small individual files that give
5  synopses of each function in the library have not been included. Neither has  synopses of each function in the library have not been included. Neither has
6  the pcredemo program. There are separate text files for the pcregrep and  the pcredemo program. There are separate text files for the pcregrep and
7  pcretest commands.  pcretest commands.
8  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
# Line 270  REVISION Line 270  REVISION
270         Last updated: 12 May 2010         Last updated: 12 May 2010
271         Copyright (c) 1997-2010 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
272  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
273    
274    
275  PCREBUILD(3)                                                      PCREBUILD(3)  PCREBUILD(3)                                                      PCREBUILD(3)
276    
277    
# Line 601  REVISION Line 601  REVISION
601         Last updated: 29 September 2009         Last updated: 29 September 2009
602         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
603  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
604    
605    
606  PCREMATCHING(3)                                                PCREMATCHING(3)  PCREMATCHING(3)                                                PCREMATCHING(3)
607    
608    
# Line 801  REVISION Line 801  REVISION
801         Last updated: 29 September 2009         Last updated: 29 September 2009
802         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
803  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
804    
805    
806  PCREAPI(3)                                                          PCREAPI(3)  PCREAPI(3)                                                          PCREAPI(3)
807    
808    
# Line 906  PCRE API OVERVIEW Line 906  PCRE API OVERVIEW
906         bers for the library.  Applications can use these  to  include  support         bers for the library.  Applications can use these  to  include  support
907         for different releases of PCRE.         for different releases of PCRE.
908    
909           In a Windows environment, if you want to statically link an application
910           program against a non-dll pcre.a  file,  you  must  define  PCRE_STATIC
911           before  including  pcre.h or pcrecpp.h, because otherwise the pcre_mal-
912           loc()   and   pcre_free()   exported   functions   will   be   declared
913           __declspec(dllimport), with unwanted results.
914    
915         The   functions   pcre_compile(),  pcre_compile2(),  pcre_study(),  and         The   functions   pcre_compile(),  pcre_compile2(),  pcre_study(),  and
916         pcre_exec() are used for compiling and matching regular expressions  in         pcre_exec() are used for compiling and matching regular expressions  in
917         a  Perl-compatible  manner. A sample program that demonstrates the sim-         a  Perl-compatible  manner. A sample program that demonstrates the sim-
# Line 1376  COMPILING A PATTERN Line 1382  COMPILING A PATTERN
1382         be  used  for  capturing  (and  they acquire numbers in the usual way).         be  used  for  capturing  (and  they acquire numbers in the usual way).
1383         There is no equivalent of this option in Perl.         There is no equivalent of this option in Perl.
1384    
1385             PCRE_UCP
1386    
1387           This option changes the way PCRE processes \b, \d, \s, \w, and some  of
1388           the POSIX character classes. By default, only ASCII characters are rec-
1389           ognized, but if PCRE_UCP is set, Unicode properties are used instead to
1390           classify  characters.  More details are given in the section on generic
1391           character types in the pcrepattern page. If you set PCRE_UCP,  matching
1392           one  of the items it affects takes much longer. The option is available
1393           only if PCRE has been compiled with Unicode property support.
1394    
1395           PCRE_UNGREEDY           PCRE_UNGREEDY
1396    
1397         This option inverts the "greediness" of the quantifiers  so  that  they         This option inverts the "greediness" of the quantifiers  so  that  they
# Line 1483  COMPILATION ERROR CODES Line 1499  COMPILATION ERROR CODES
1499           65   different  names  for  subpatterns  of  the  same number are not           65   different  names  for  subpatterns  of  the  same number are not
1500         allowed         allowed
1501           66  (*MARK) must have an argument           66  (*MARK) must have an argument
1502             67  this version of PCRE is not compiled with PCRE_UCP support
1503    
1504         The numbers 32 and 10000 in errors 48 and 49  are  defaults;  different         The numbers 32 and 10000 in errors 48 and 49  are  defaults;  different
1505         values may be used if the limits were changed when PCRE was built.         values may be used if the limits were changed when PCRE was built.
# Line 1548  LOCALE SUPPORT Line 1565  LOCALE SUPPORT
1565         PCRE handles caseless matching, and determines whether  characters  are         PCRE handles caseless matching, and determines whether  characters  are
1566         letters,  digits, or whatever, by reference to a set of tables, indexed         letters,  digits, or whatever, by reference to a set of tables, indexed
1567         by character value. When running in UTF-8 mode, this  applies  only  to         by character value. When running in UTF-8 mode, this  applies  only  to
1568         characters  with  codes  less than 128. Higher-valued codes never match         characters  with  codes  less than 128. By default, higher-valued codes
1569         escapes such as \w or \d, but can be tested with \p if  PCRE  is  built         never match escapes such as \w or \d, but they can be tested with \p if
1570         with  Unicode  character property support. The use of locales with Uni-         PCRE  is  built with Unicode character property support. Alternatively,
1571         code is discouraged. If you are handling characters with codes  greater         the PCRE_UCP option can be set at compile  time;  this  causes  \w  and
1572         than  128, you should either use UTF-8 and Unicode, or use locales, but         friends to use Unicode property support instead of built-in tables. The
1573         not try to mix the two.         use of locales with Unicode is discouraged. If you are handling charac-
1574           ters  with codes greater than 128, you should either use UTF-8 and Uni-
1575           code, or use locales, but not try to mix the two.
1576    
1577         PCRE contains an internal set of tables that are used  when  the  final         PCRE contains an internal set of tables that are used  when  the  final
1578         argument  of  pcre_compile()  is  NULL.  These  are sufficient for many         argument  of  pcre_compile()  is  NULL.  These  are sufficient for many
# Line 2295  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2314  MATCHING A PATTERN: THE TRADITIONAL FUNC
2314         purpose.  If the call via pcre_malloc() fails, this error is given. The         purpose.  If the call via pcre_malloc() fails, this error is given. The
2315         memory is automatically freed at the end of matching.         memory is automatically freed at the end of matching.
2316    
2317           This error is also given if pcre_stack_malloc() fails  in  pcre_exec().
2318           This  can happen only when PCRE has been compiled with --disable-stack-
2319           for-recursion.
2320    
2321           PCRE_ERROR_NOSUBSTRING    (-7)           PCRE_ERROR_NOSUBSTRING    (-7)
2322    
2323         This error is used by the pcre_copy_substring(),  pcre_get_substring(),         This error is used by the pcre_copy_substring(),  pcre_get_substring(),
# Line 2730  AUTHOR Line 2753  AUTHOR
2753    
2754  REVISION  REVISION
2755    
2756         Last updated: 03 May 2010         Last updated: 01 June 2010
2757         Copyright (c) 1997-2010 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
2758  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2759    
2760    
2761  PCRECALLOUT(3)                                                  PCRECALLOUT(3)  PCRECALLOUT(3)                                                  PCRECALLOUT(3)
2762    
2763    
# Line 2914  REVISION Line 2937  REVISION
2937         Last updated: 29 September 2009         Last updated: 29 September 2009
2938         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
2939  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2940    
2941    
2942  PCRECOMPAT(3)                                                    PCRECOMPAT(3)  PCRECOMPAT(3)                                                    PCRECOMPAT(3)
2943    
2944    
# Line 2927  DIFFERENCES BETWEEN PCRE AND PERL Line 2950  DIFFERENCES BETWEEN PCRE AND PERL
2950    
2951         This  document describes the differences in the ways that PCRE and Perl         This  document describes the differences in the ways that PCRE and Perl
2952         handle regular expressions. The differences  described  here  are  with         handle regular expressions. The differences  described  here  are  with
2953         respect to Perl 5.10.         respect to Perl 5.10/5.11.
2954    
2955         1.  PCRE has only a subset of Perl's UTF-8 and Unicode support. Details         1.  PCRE has only a subset of Perl's UTF-8 and Unicode support. Details
2956         of what it does have are given in the section on UTF-8 support  in  the         of what it does have are given in the section on UTF-8 support  in  the
# Line 2998  DIFFERENCES BETWEEN PCRE AND PERL Line 3021  DIFFERENCES BETWEEN PCRE AND PERL
3021         matching "aba" against the  pattern  /^(a(b)?)+$/  in  Perl  leaves  $2         matching "aba" against the  pattern  /^(a(b)?)+$/  in  Perl  leaves  $2
3022         unset, but in PCRE it is set to "b".         unset, but in PCRE it is set to "b".
3023    
3024         11.  PCRE  does  support  Perl  5.10's  backtracking  verbs  (*ACCEPT),         11.  PCRE's handling of duplicate subpattern numbers and duplicate sub-
        (*FAIL), (*F), (*COMMIT), (*PRUNE), (*SKIP), and (*THEN), but  only  in  
        the forms without an argument. PCRE does not support (*MARK).  
   
        12.  PCRE's handling of duplicate subpattern numbers and duplicate sub-  
3025         pattern names is not as general as Perl's. This is a consequence of the         pattern names is not as general as Perl's. This is a consequence of the
3026         fact the PCRE works internally just with numbers, using an external ta-         fact the PCRE works internally just with numbers, using an external ta-
3027         ble to translate between numbers and names. In  particular,  a  pattern         ble to translate between numbers and names. In  particular,  a  pattern
# Line 3013  DIFFERENCES BETWEEN PCRE AND PERL Line 3032  DIFFERENCES BETWEEN PCRE AND PERL
3032         turing subpattern number 1. To avoid this confusing situation, an error         turing subpattern number 1. To avoid this confusing situation, an error
3033         is given at compile time.         is given at compile time.
3034    
3035         13. PCRE provides some extensions to the Perl regular expression facil-         12. PCRE provides some extensions to the Perl regular expression facil-
3036         ities.   Perl  5.10  includes new features that are not in earlier ver-         ities.   Perl  5.10  includes new features that are not in earlier ver-
3037         sions of Perl, some of which (such as named parentheses) have  been  in         sions of Perl, some of which (such as named parentheses) have  been  in
3038         PCRE for some time. This list is with respect to Perl 5.10:         PCRE for some time. This list is with respect to Perl 5.10:
# Line 3068  AUTHOR Line 3087  AUTHOR
3087    
3088  REVISION  REVISION
3089    
3090         Last updated: 04 October 2009         Last updated: 12 May 2010
3091         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
3092  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
3093    
3094    
3095  PCREPATTERN(3)                                                  PCREPATTERN(3)  PCREPATTERN(3)                                                  PCREPATTERN(3)
3096    
3097    
# Line 3111  PCRE REGULAR EXPRESSION DETAILS Line 3130  PCRE REGULAR EXPRESSION DETAILS
3130         below.  There  is  also  a  summary of UTF-8 features in the section on         below.  There  is  also  a  summary of UTF-8 features in the section on
3131         UTF-8 support in the main pcre page.         UTF-8 support in the main pcre page.
3132    
3133           Another special sequence that may appear at the start of a  pattern  or
3134           in combination with (*UTF8) is:
3135    
3136             (*UCP)
3137    
3138           This  has  the  same  effect  as setting the PCRE_UCP option: it causes
3139           sequences such as \d and \w to  use  Unicode  properties  to  determine
3140           character types, instead of recognizing only characters with codes less
3141           than 128 via a lookup table.
3142    
3143         The remainder of this document discusses the  patterns  that  are  sup-         The remainder of this document discusses the  patterns  that  are  sup-
3144         ported  by  PCRE when its main matching function, pcre_exec(), is used.         ported  by  PCRE when its main matching function, pcre_exec(), is used.
3145         From  release  6.0,   PCRE   offers   a   second   matching   function,         From  release  6.0,   PCRE   offers   a   second   matching   function,
# Line 3382  BACKSLASH Line 3411  BACKSLASH
3411    
3412         Each  pair of lower and upper case escape sequences partitions the com-         Each  pair of lower and upper case escape sequences partitions the com-
3413         plete set of characters into two disjoint  sets.  Any  given  character         plete set of characters into two disjoint  sets.  Any  given  character
3414         matches one, and only one, of each pair.         matches  one, and only one, of each pair. The sequences can appear both
3415           inside and outside character classes. They each match one character  of
3416         These character type sequences can appear both inside and outside char-         the  appropriate  type.  If the current matching point is at the end of
3417         acter classes. They each match one character of the  appropriate  type.         the subject string, all of them fail, because there is no character  to
3418         If  the current matching point is at the end of the subject string, all         match.
3419         of them fail, since there is no character to match.  
3420           For  compatibility  with Perl, \s does not match the VT character (code
3421         For compatibility with Perl, \s does not match the VT  character  (code         11).  This makes it different from the the POSIX "space" class. The  \s
3422         11).   This makes it different from the the POSIX "space" class. The \s         characters  are  HT  (9), LF (10), FF (12), CR (13), and space (32). If
        characters are HT (9), LF (10), FF (12), CR (13), and  space  (32).  If  
3423         "use locale;" is included in a Perl script, \s may match the VT charac-         "use locale;" is included in a Perl script, \s may match the VT charac-
3424         ter. In PCRE, it never does.         ter. In PCRE, it never does.
3425    
3426         In UTF-8 mode, characters with values greater than 128 never match  \d,         A  "word"  character is an underscore or any character that is a letter
3427         \s, or \w, and always match \D, \S, and \W. This is true even when Uni-         or digit.  By default, the definition of letters  and  digits  is  con-
3428         code character property support is available.  These  sequences  retain         trolled  by PCRE's low-valued character tables, and may vary if locale-
3429         their original meanings from before UTF-8 support was available, mainly         specific matching is taking place (see "Locale support" in the  pcreapi
3430         for efficiency reasons. Note that this also affects \b, because  it  is         page).  For  example,  in  a French locale such as "fr_FR" in Unix-like
3431         defined in terms of \w and \W.         systems, or "french" in Windows, some character codes greater than  128
3432           are  used  for  accented letters, and these are then matched by \w. The
3433           use of locales with Unicode is discouraged.
3434    
3435           By default, in UTF-8 mode, characters  with  values  greater  than  128
3436           never  match  \d,  \s,  or  \w,  and always match \D, \S, and \W. These
3437           sequences retain their original meanings from before UTF-8 support  was
3438           available,  mainly for efficiency reasons. However, if PCRE is compiled
3439           with Unicode property support, and the PCRE_UCP option is set, the  be-
3440           haviour  is  changed  so  that Unicode properties are used to determine
3441           character types, as follows:
3442    
3443             \d  any character that \p{Nd} matches (decimal digit)
3444             \s  any character that \p{Z} matches, plus HT, LF, FF, CR
3445             \w  any character that \p{L} or \p{N} matches, plus underscore
3446    
3447           The upper case escapes match the inverse sets of characters. Note  that
3448           \d  matches  only decimal digits, whereas \w matches any Unicode digit,
3449           as well as any Unicode letter, and underscore. Note also that  PCRE_UCP
3450           affects  \b,  and  \B  because  they are defined in terms of \w and \W.
3451           Matching these sequences is noticeably slower when PCRE_UCP is set.
3452    
3453         The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to         The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to
3454         the other sequences, these do match certain high-valued  codepoints  in         the  other  sequences,  which  match  only ASCII characters by default,
3455         UTF-8 mode.  The horizontal space characters are:         these always  match  certain  high-valued  codepoints  in  UTF-8  mode,
3456           whether or not PCRE_UCP is set. The horizontal space characters are:
3457    
3458           U+0009     Horizontal tab           U+0009     Horizontal tab
3459           U+0020     Space           U+0020     Space
# Line 3436  BACKSLASH Line 3485  BACKSLASH
3485           U+2028     Line separator           U+2028     Line separator
3486           U+2029     Paragraph separator           U+2029     Paragraph separator
3487    
        A "word" character is an underscore or any character less than 256 that  
        is a letter or digit. The definition of  letters  and  digits  is  con-  
        trolled  by PCRE's low-valued character tables, and may vary if locale-  
        specific matching is taking place (see "Locale support" in the  pcreapi  
        page).  For  example,  in  a French locale such as "fr_FR" in Unix-like  
        systems, or "french" in Windows, some character codes greater than  128  
        are  used for accented letters, and these are matched by \w. The use of  
        locales with Unicode is discouraged.  
   
3488     Newline sequences     Newline sequences
3489    
3490         Outside a character class, by default, the escape sequence  \R  matches         Outside  a  character class, by default, the escape sequence \R matches
3491         any Unicode newline sequence. This is a Perl 5.10 feature. In non-UTF-8         any Unicode newline sequence. This is a Perl 5.10 feature. In non-UTF-8
3492         mode \R is equivalent to the following:         mode \R is equivalent to the following:
3493    
3494           (?>\r\n|\n|\x0b|\f|\r|\x85)           (?>\r\n|\n|\x0b|\f|\r|\x85)
3495    
3496         This is an example of an "atomic group", details  of  which  are  given         This  is  an  example  of an "atomic group", details of which are given
3497         below.  This particular group matches either the two-character sequence         below.  This particular group matches either the two-character sequence
3498         CR followed by LF, or  one  of  the  single  characters  LF  (linefeed,         CR  followed  by  LF,  or  one  of  the single characters LF (linefeed,
3499         U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage         U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage
3500         return, U+000D), or NEL (next line, U+0085). The two-character sequence         return, U+000D), or NEL (next line, U+0085). The two-character sequence
3501         is treated as a single unit that cannot be split.         is treated as a single unit that cannot be split.
3502    
3503         In  UTF-8  mode, two additional characters whose codepoints are greater         In UTF-8 mode, two additional characters whose codepoints  are  greater
3504         than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa-         than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa-
3505         rator,  U+2029).   Unicode character property support is not needed for         rator, U+2029).  Unicode character property support is not  needed  for
3506         these characters to be recognized.         these characters to be recognized.
3507    
3508         It is possible to restrict \R to match only CR, LF, or CRLF (instead of         It is possible to restrict \R to match only CR, LF, or CRLF (instead of
3509         the  complete  set  of  Unicode  line  endings)  by  setting the option         the complete set  of  Unicode  line  endings)  by  setting  the  option
3510         PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched.         PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched.
3511         (BSR is an abbrevation for "backslash R".) This can be made the default         (BSR is an abbrevation for "backslash R".) This can be made the default
3512         when PCRE is built; if this is the case, the  other  behaviour  can  be         when  PCRE  is  built;  if this is the case, the other behaviour can be
3513         requested  via  the  PCRE_BSR_UNICODE  option.   It is also possible to         requested via the PCRE_BSR_UNICODE option.   It  is  also  possible  to
3514         specify these settings by starting a pattern string  with  one  of  the         specify  these  settings  by  starting a pattern string with one of the
3515         following sequences:         following sequences:
3516    
3517           (*BSR_ANYCRLF)   CR, LF, or CRLF only           (*BSR_ANYCRLF)   CR, LF, or CRLF only
3518           (*BSR_UNICODE)   any Unicode newline sequence           (*BSR_UNICODE)   any Unicode newline sequence
3519    
3520         These  override  the default and the options given to pcre_compile() or         These override the default and the options given to  pcre_compile()  or
3521         pcre_compile2(), but  they  can  be  overridden  by  options  given  to         pcre_compile2(),  but  they  can  be  overridden  by  options  given to
3522         pcre_exec() or pcre_dfa_exec(). Note that these special settings, which         pcre_exec() or pcre_dfa_exec(). Note that these special settings, which
3523         are not Perl-compatible, are recognized only at the  very  start  of  a         are  not  Perl-compatible,  are  recognized only at the very start of a
3524         pattern,  and that they must be in upper case. If more than one of them         pattern, and that they must be in upper case. If more than one of  them
3525         is present, the last one is used. They can be combined with a change of         is present, the last one is used. They can be combined with a change of
3526         newline convention, for example, a pattern can start with:         newline convention; for example, a pattern can start with:
3527    
3528           (*ANY)(*BSR_ANYCRLF)           (*ANY)(*BSR_ANYCRLF)
3529    
3530           They can also be combined with the (*UTF8) or (*UCP) special sequences.
3531         Inside  a  character  class,  \R  is  treated as an unrecognized escape         Inside  a  character  class,  \R  is  treated as an unrecognized escape
3532         sequence, and so matches the letter "R" by default, but causes an error         sequence, and so matches the letter "R" by default, but causes an error
3533         if PCRE_EXTRA is set.         if PCRE_EXTRA is set.
# Line 3631  BACKSLASH Line 3672  BACKSLASH
3672         Matching characters by Unicode property is not fast, because  PCRE  has         Matching characters by Unicode property is not fast, because  PCRE  has
3673         to  search  a  structure  that  contains data for over fifteen thousand         to  search  a  structure  that  contains data for over fifteen thousand
3674         characters. That is why the traditional escape sequences such as \d and         characters. That is why the traditional escape sequences such as \d and
3675         \w do not use Unicode properties in PCRE.         \w  do  not  use  Unicode properties in PCRE by default, though you can
3676           make them do so by setting the PCRE_UCP option for pcre_compile() or by
3677           starting the pattern with (*UCP).
3678    
3679     PCRE's additional properties     PCRE's additional properties
3680    
3681         As  well  as  the standard Unicode properties described in the previous         As  well  as  the standard Unicode properties described in the previous
3682         section, PCRE supports four more that make it possible to convert  tra-         section, PCRE supports four more that make it possible to convert  tra-
3683         ditional escape sequences such as \w and \s and POSIX character classes         ditional escape sequences such as \w and \s and POSIX character classes
3684         to use Unicode properties. These are:         to use Unicode properties. PCRE uses these non-standard, non-Perl prop-
3685           erties internally when PCRE_UCP is set. They are:
3686    
3687           Xan   Any alphanumeric character           Xan   Any alphanumeric character
3688           Xps   Any POSIX space character           Xps   Any POSIX space character
3689           Xsp   Any Perl space character           Xsp   Any Perl space character
3690           Xwd   Any Perl "word" character           Xwd   Any Perl "word" character
3691    
3692         Xan matches characters that have either the L (letter) or the  N  (num-         Xan  matches  characters that have either the L (letter) or the N (num-
3693         ber)  property. Xps matches the characters tab, linefeed, vertical tab,         ber) property. Xps matches the characters tab, linefeed, vertical  tab,
3694         formfeed, or carriage return, and any other character that  has  the  Z         formfeed,  or  carriage  return, and any other character that has the Z
3695         (separator) property.  Xsp is the same as Xps, except that vertical tab         (separator) property.  Xsp is the same as Xps, except that vertical tab
3696         is excluded. Xwd matches the same characters as Xan, plus underscore.         is excluded. Xwd matches the same characters as Xan, plus underscore.
3697    
3698     Resetting the match start     Resetting the match start
3699    
3700         The escape sequence \K, which is a Perl 5.10 feature, causes any previ-         The escape sequence \K, which is a Perl 5.10 feature, causes any previ-
3701         ously  matched  characters  not  to  be  included  in the final matched         ously matched characters not  to  be  included  in  the  final  matched
3702         sequence. For example, the pattern:         sequence. For example, the pattern:
3703    
3704           foo\Kbar           foo\Kbar
3705    
3706         matches "foobar", but reports that it has matched "bar".  This  feature         matches  "foobar",  but reports that it has matched "bar". This feature
3707         is  similar  to  a lookbehind assertion (described below).  However, in         is similar to a lookbehind assertion (described  below).   However,  in
3708         this case, the part of the subject before the real match does not  have         this  case, the part of the subject before the real match does not have
3709         to  be of fixed length, as lookbehind assertions do. The use of \K does         to be of fixed length, as lookbehind assertions do. The use of \K  does
3710         not interfere with the setting of captured  substrings.   For  example,         not  interfere  with  the setting of captured substrings.  For example,
3711         when the pattern         when the pattern
3712    
3713           (foo)\Kbar           (foo)\Kbar
3714    
3715         matches "foobar", the first substring is still set to "foo".         matches "foobar", the first substring is still set to "foo".
3716    
3717         Perl  documents  that  the  use  of  \K  within assertions is "not well         Perl documents that the use  of  \K  within  assertions  is  "not  well
3718         defined". In PCRE, \K is acted upon  when  it  occurs  inside  positive         defined".  In  PCRE,  \K  is  acted upon when it occurs inside positive
3719         assertions, but is ignored in negative assertions.         assertions, but is ignored in negative assertions.
3720    
3721     Simple assertions     Simple assertions
3722    
3723         The  final use of backslash is for certain simple assertions. An asser-         The final use of backslash is for certain simple assertions. An  asser-
3724         tion specifies a condition that has to be met at a particular point  in         tion  specifies a condition that has to be met at a particular point in
3725         a  match, without consuming any characters from the subject string. The         a match, without consuming any characters from the subject string.  The
3726         use of subpatterns for more complicated assertions is described  below.         use  of subpatterns for more complicated assertions is described below.
3727         The backslashed assertions are:         The backslashed assertions are:
3728    
3729           \b     matches at a word boundary           \b     matches at a word boundary
# Line 3690  BACKSLASH Line 3734  BACKSLASH
3734           \z     matches only at the end of the subject           \z     matches only at the end of the subject
3735           \G     matches at the first matching position in the subject           \G     matches at the first matching position in the subject
3736    
3737         Inside  a  character  class, \b has a different meaning; it matches the         Inside a character class, \b has a different meaning;  it  matches  the
3738         backspace character. If any other of  these  assertions  appears  in  a         backspace  character.  If  any  other  of these assertions appears in a
3739         character  class, by default it matches the corresponding literal char-         character class, by default it matches the corresponding literal  char-
3740         acter  (for  example,  \B  matches  the  letter  B).  However,  if  the         acter  (for  example,  \B  matches  the  letter  B).  However,  if  the
3741         PCRE_EXTRA  option is set, an "invalid escape sequence" error is gener-         PCRE_EXTRA option is set, an "invalid escape sequence" error is  gener-
3742         ated instead.         ated instead.
3743    
3744         A word boundary is a position in the subject string where  the  current         A  word  boundary is a position in the subject string where the current
3745         character  and  the previous character do not both match \w or \W (i.e.         character and the previous character do not both match \w or  \W  (i.e.
3746         one matches \w and the other matches \W), or the start or  end  of  the         one  matches  \w  and the other matches \W), or the start or end of the
3747         string if the first or last character matches \w, respectively. Neither         string if the first or last  character  matches  \w,  respectively.  In
3748         PCRE nor Perl has a separte "start of word" or "end  of  word"  metase-         UTF-8  mode,  the  meanings  of \w and \W can be changed by setting the
3749         quence.  However,  whatever follows \b normally determines which it is.         PCRE_UCP option. When this is done, it also affects \b and \B.  Neither
3750           PCRE  nor  Perl has a separate "start of word" or "end of word" metase-
3751           quence. However, whatever follows \b normally determines which  it  is.
3752         For example, the fragment \ba matches "a" at the start of a word.         For example, the fragment \ba matches "a" at the start of a word.
3753    
3754         The \A, \Z, and \z assertions differ from  the  traditional  circumflex         The  \A,  \Z,  and \z assertions differ from the traditional circumflex
3755         and dollar (described in the next section) in that they only ever match         and dollar (described in the next section) in that they only ever match
3756         at the very start and end of the subject string, whatever  options  are         at  the  very start and end of the subject string, whatever options are
3757         set.  Thus,  they are independent of multiline mode. These three asser-         set. Thus, they are independent of multiline mode. These  three  asser-
3758         tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which         tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which
3759         affect  only the behaviour of the circumflex and dollar metacharacters.         affect only the behaviour of the circumflex and dollar  metacharacters.
3760         However, if the startoffset argument of pcre_exec() is non-zero,  indi-         However,  if the startoffset argument of pcre_exec() is non-zero, indi-
3761         cating that matching is to start at a point other than the beginning of         cating that matching is to start at a point other than the beginning of
3762         the subject, \A can never match. The difference between \Z  and  \z  is         the  subject,  \A  can never match. The difference between \Z and \z is
3763         that \Z matches before a newline at the end of the string as well as at         that \Z matches before a newline at the end of the string as well as at
3764         the very end, whereas \z matches only at the end.         the very end, whereas \z matches only at the end.
3765    
3766         The \G assertion is true only when the current matching position is  at         The  \G assertion is true only when the current matching position is at
3767         the  start point of the match, as specified by the startoffset argument         the start point of the match, as specified by the startoffset  argument
3768         of pcre_exec(). It differs from \A when the  value  of  startoffset  is         of  pcre_exec().  It  differs  from \A when the value of startoffset is
3769         non-zero.  By calling pcre_exec() multiple times with appropriate argu-         non-zero. By calling pcre_exec() multiple times with appropriate  argu-
3770         ments, you can mimic Perl's /g option, and it is in this kind of imple-         ments, you can mimic Perl's /g option, and it is in this kind of imple-
3771         mentation where \G can be useful.         mentation where \G can be useful.
3772    
3773         Note,  however,  that  PCRE's interpretation of \G, as the start of the         Note, however, that PCRE's interpretation of \G, as the  start  of  the
3774         current match, is subtly different from Perl's, which defines it as the         current match, is subtly different from Perl's, which defines it as the
3775         end  of  the  previous  match. In Perl, these can be different when the         end of the previous match. In Perl, these can  be  different  when  the
3776         previously matched string was empty. Because PCRE does just  one  match         previously  matched  string was empty. Because PCRE does just one match
3777         at a time, it cannot reproduce this behaviour.         at a time, it cannot reproduce this behaviour.
3778    
3779         If  all  the alternatives of a pattern begin with \G, the expression is         If all the alternatives of a pattern begin with \G, the  expression  is
3780         anchored to the starting match position, and the "anchored" flag is set         anchored to the starting match position, and the "anchored" flag is set
3781         in the compiled regular expression.         in the compiled regular expression.
3782    
# Line 3738  BACKSLASH Line 3784  BACKSLASH
3784  CIRCUMFLEX AND DOLLAR  CIRCUMFLEX AND DOLLAR
3785    
3786         Outside a character class, in the default matching mode, the circumflex         Outside a character class, in the default matching mode, the circumflex
3787         character is an assertion that is true only  if  the  current  matching         character  is  an  assertion  that is true only if the current matching
3788         point  is  at the start of the subject string. If the startoffset argu-         point is at the start of the subject string. If the  startoffset  argu-
3789         ment of pcre_exec() is non-zero, circumflex  can  never  match  if  the         ment  of  pcre_exec()  is  non-zero,  circumflex can never match if the
3790         PCRE_MULTILINE  option  is  unset. Inside a character class, circumflex         PCRE_MULTILINE option is unset. Inside a  character  class,  circumflex
3791         has an entirely different meaning (see below).         has an entirely different meaning (see below).
3792    
3793         Circumflex need not be the first character of the pattern if  a  number         Circumflex  need  not be the first character of the pattern if a number
3794         of  alternatives are involved, but it should be the first thing in each         of alternatives are involved, but it should be the first thing in  each
3795         alternative in which it appears if the pattern is ever  to  match  that         alternative  in  which  it appears if the pattern is ever to match that
3796         branch.  If all possible alternatives start with a circumflex, that is,         branch. If all possible alternatives start with a circumflex, that  is,
3797         if the pattern is constrained to match only at the start  of  the  sub-         if  the  pattern  is constrained to match only at the start of the sub-
3798         ject,  it  is  said  to be an "anchored" pattern. (There are also other         ject, it is said to be an "anchored" pattern.  (There  are  also  other
3799         constructs that can cause a pattern to be anchored.)         constructs that can cause a pattern to be anchored.)
3800    
3801         A dollar character is an assertion that is true  only  if  the  current         A  dollar  character  is  an assertion that is true only if the current
3802         matching  point  is  at  the  end of the subject string, or immediately         matching point is at the end of  the  subject  string,  or  immediately
3803         before a newline at the end of the string (by default). Dollar need not         before a newline at the end of the string (by default). Dollar need not
3804         be  the  last  character of the pattern if a number of alternatives are         be the last character of the pattern if a number  of  alternatives  are
3805         involved, but it should be the last item in  any  branch  in  which  it         involved,  but  it  should  be  the last item in any branch in which it
3806         appears. Dollar has no special meaning in a character class.         appears. Dollar has no special meaning in a character class.
3807    
3808         The  meaning  of  dollar  can be changed so that it matches only at the         The meaning of dollar can be changed so that it  matches  only  at  the
3809         very end of the string, by setting the  PCRE_DOLLAR_ENDONLY  option  at         very  end  of  the string, by setting the PCRE_DOLLAR_ENDONLY option at
3810         compile time. This does not affect the \Z assertion.         compile time. This does not affect the \Z assertion.
3811    
3812         The meanings of the circumflex and dollar characters are changed if the         The meanings of the circumflex and dollar characters are changed if the
3813         PCRE_MULTILINE option is set. When  this  is  the  case,  a  circumflex         PCRE_MULTILINE  option  is  set.  When  this  is the case, a circumflex
3814         matches  immediately after internal newlines as well as at the start of         matches immediately after internal newlines as well as at the start  of
3815         the subject string. It does not match after a  newline  that  ends  the         the  subject  string.  It  does not match after a newline that ends the
3816         string.  A dollar matches before any newlines in the string, as well as         string. A dollar matches before any newlines in the string, as well  as
3817         at the very end, when PCRE_MULTILINE is set. When newline is  specified         at  the very end, when PCRE_MULTILINE is set. When newline is specified
3818         as  the  two-character  sequence CRLF, isolated CR and LF characters do         as the two-character sequence CRLF, isolated CR and  LF  characters  do
3819         not indicate newlines.         not indicate newlines.
3820    
3821         For example, the pattern /^abc$/ matches the subject string  "def\nabc"         For  example, the pattern /^abc$/ matches the subject string "def\nabc"
3822         (where  \n  represents a newline) in multiline mode, but not otherwise.         (where \n represents a newline) in multiline mode, but  not  otherwise.
3823         Consequently, patterns that are anchored in single  line  mode  because         Consequently,  patterns  that  are anchored in single line mode because
3824         all  branches  start  with  ^ are not anchored in multiline mode, and a         all branches start with ^ are not anchored in  multiline  mode,  and  a
3825         match for circumflex is  possible  when  the  startoffset  argument  of         match  for  circumflex  is  possible  when  the startoffset argument of
3826         pcre_exec()  is  non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if         pcre_exec() is non-zero. The PCRE_DOLLAR_ENDONLY option is  ignored  if
3827         PCRE_MULTILINE is set.         PCRE_MULTILINE is set.
3828    
3829         Note that the sequences \A, \Z, and \z can be used to match  the  start         Note  that  the sequences \A, \Z, and \z can be used to match the start
3830         and  end of the subject in both modes, and if all branches of a pattern         and end of the subject in both modes, and if all branches of a  pattern
3831         start with \A it is always anchored, whether or not  PCRE_MULTILINE  is         start  with  \A it is always anchored, whether or not PCRE_MULTILINE is
3832         set.         set.
3833    
3834    
3835  FULL STOP (PERIOD, DOT) AND \N  FULL STOP (PERIOD, DOT) AND \N
3836    
3837         Outside a character class, a dot in the pattern matches any one charac-         Outside a character class, a dot in the pattern matches any one charac-
3838         ter in the subject string except (by default) a character  that  signi-         ter  in  the subject string except (by default) a character that signi-
3839         fies  the  end  of  a line. In UTF-8 mode, the matched character may be         fies the end of a line. In UTF-8 mode, the  matched  character  may  be
3840         more than one byte long.         more than one byte long.
3841    
3842         When a line ending is defined as a single character, dot never  matches         When  a line ending is defined as a single character, dot never matches
3843         that  character; when the two-character sequence CRLF is used, dot does         that character; when the two-character sequence CRLF is used, dot  does
3844         not match CR if it is immediately followed  by  LF,  but  otherwise  it         not  match  CR  if  it  is immediately followed by LF, but otherwise it
3845         matches  all characters (including isolated CRs and LFs). When any Uni-         matches all characters (including isolated CRs and LFs). When any  Uni-
3846         code line endings are being recognized, dot does not match CR or LF  or         code  line endings are being recognized, dot does not match CR or LF or
3847         any of the other line ending characters.         any of the other line ending characters.
3848    
3849         The  behaviour  of  dot  with regard to newlines can be changed. If the         The behaviour of dot with regard to newlines can  be  changed.  If  the
3850         PCRE_DOTALL option is set, a dot matches  any  one  character,  without         PCRE_DOTALL  option  is  set,  a dot matches any one character, without
3851         exception. If the two-character sequence CRLF is present in the subject         exception. If the two-character sequence CRLF is present in the subject
3852         string, it takes two dots to match it.         string, it takes two dots to match it.
3853    
3854         The handling of dot is entirely independent of the handling of  circum-         The  handling of dot is entirely independent of the handling of circum-
3855         flex  and  dollar,  the  only relationship being that they both involve         flex and dollar, the only relationship being  that  they  both  involve
3856         newlines. Dot has no special meaning in a character class.         newlines. Dot has no special meaning in a character class.
3857    
3858         The escape sequence \N always behaves as a dot does when PCRE_DOTALL is         The escape sequence \N always behaves as a dot does when PCRE_DOTALL is
3859         not  set.  In other words, it matches any one character except one that         not set. In other words, it matches any one character except  one  that
3860         signifies the end of a line.         signifies the end of a line.
3861    
3862    
3863  MATCHING A SINGLE BYTE  MATCHING A SINGLE BYTE
3864    
3865         Outside a character class, the escape sequence \C matches any one byte,         Outside a character class, the escape sequence \C matches any one byte,
3866         both  in  and  out  of  UTF-8 mode. Unlike a dot, it always matches any         both in and out of UTF-8 mode. Unlike a  dot,  it  always  matches  any
3867         line-ending characters. The feature is provided in  Perl  in  order  to         line-ending  characters.  The  feature  is provided in Perl in order to
3868         match  individual bytes in UTF-8 mode. Because it breaks up UTF-8 char-         match individual bytes in UTF-8 mode. Because it breaks up UTF-8  char-
3869         acters into individual bytes, what remains in the string may be a  mal-         acters  into individual bytes, what remains in the string may be a mal-
3870         formed  UTF-8  string.  For this reason, the \C escape sequence is best         formed UTF-8 string. For this reason, the \C escape  sequence  is  best
3871         avoided.         avoided.
3872    
3873         PCRE does not allow \C to appear in  lookbehind  assertions  (described         PCRE  does  not  allow \C to appear in lookbehind assertions (described
3874         below),  because  in UTF-8 mode this would make it impossible to calcu-         below), because in UTF-8 mode this would make it impossible  to  calcu-
3875         late the length of the lookbehind.         late the length of the lookbehind.
3876    
3877    
# Line 3835  SQUARE BRACKETS AND CHARACTER CLASSES Line 3881  SQUARE BRACKETS AND CHARACTER CLASSES
3881         closing square bracket. A closing square bracket on its own is not spe-         closing square bracket. A closing square bracket on its own is not spe-
3882         cial by default.  However, if the PCRE_JAVASCRIPT_COMPAT option is set,         cial by default.  However, if the PCRE_JAVASCRIPT_COMPAT option is set,
3883         a lone closing square bracket causes a compile-time error. If a closing         a lone closing square bracket causes a compile-time error. If a closing
3884         square bracket is required as a member of the class, it should  be  the         square  bracket  is required as a member of the class, it should be the
3885         first  data  character  in  the  class (after an initial circumflex, if         first data character in the class  (after  an  initial  circumflex,  if
3886         present) or escaped with a backslash.         present) or escaped with a backslash.
3887    
3888         A character class matches a single character in the subject.  In  UTF-8         A  character  class matches a single character in the subject. In UTF-8
3889         mode, the character may be more than one byte long. A matched character         mode, the character may be more than one byte long. A matched character
3890         must be in the set of characters defined by the class, unless the first         must be in the set of characters defined by the class, unless the first
3891         character  in  the  class definition is a circumflex, in which case the         character in the class definition is a circumflex, in  which  case  the
3892         subject character must not be in the set defined by  the  class.  If  a         subject  character  must  not  be in the set defined by the class. If a
3893         circumflex  is actually required as a member of the class, ensure it is         circumflex is actually required as a member of the class, ensure it  is
3894         not the first character, or escape it with a backslash.         not the first character, or escape it with a backslash.
3895    
3896         For example, the character class [aeiou] matches any lower case  vowel,         For  example, the character class [aeiou] matches any lower case vowel,
3897         while  [^aeiou]  matches  any character that is not a lower case vowel.         while [^aeiou] matches any character that is not a  lower  case  vowel.
3898         Note that a circumflex is just a convenient notation for specifying the         Note that a circumflex is just a convenient notation for specifying the
3899         characters  that  are in the class by enumerating those that are not. A         characters that are in the class by enumerating those that are  not.  A
3900         class that starts with a circumflex is not an assertion; it still  con-         class  that starts with a circumflex is not an assertion; it still con-
3901         sumes  a  character  from the subject string, and therefore it fails if         sumes a character from the subject string, and therefore  it  fails  if
3902         the current pointer is at the end of the string.         the current pointer is at the end of the string.
3903    
3904         In UTF-8 mode, characters with values greater than 255 can be  included         In  UTF-8 mode, characters with values greater than 255 can be included
3905         in  a  class as a literal string of bytes, or by using the \x{ escaping         in a class as a literal string of bytes, or by using the  \x{  escaping
3906         mechanism.         mechanism.
3907    
3908         When caseless matching is set, any letters in a  class  represent  both         When  caseless  matching  is set, any letters in a class represent both
3909         their  upper  case  and lower case versions, so for example, a caseless         their upper case and lower case versions, so for  example,  a  caseless
3910         [aeiou] matches "A" as well as "a", and a caseless  [^aeiou]  does  not         [aeiou]  matches  "A"  as well as "a", and a caseless [^aeiou] does not
3911         match  "A", whereas a caseful version would. In UTF-8 mode, PCRE always         match "A", whereas a caseful version would. In UTF-8 mode, PCRE  always
3912         understands the concept of case for characters whose  values  are  less         understands  the  concept  of case for characters whose values are less
3913         than  128, so caseless matching is always possible. For characters with         than 128, so caseless matching is always possible. For characters  with
3914         higher values, the concept of case is supported  if  PCRE  is  compiled         higher  values,  the  concept  of case is supported if PCRE is compiled
3915         with  Unicode  property support, but not otherwise.  If you want to use         with Unicode property support, but not otherwise.  If you want  to  use
3916         caseless matching in UTF8-mode for characters 128 and above,  you  must         caseless  matching  in UTF8-mode for characters 128 and above, you must
3917         ensure  that  PCRE is compiled with Unicode property support as well as         ensure that PCRE is compiled with Unicode property support as  well  as
3918         with UTF-8 support.         with UTF-8 support.
3919    
3920         Characters that might indicate line breaks are  never  treated  in  any         Characters  that  might  indicate  line breaks are never treated in any
3921         special  way  when  matching  character  classes,  whatever line-ending         special way  when  matching  character  classes,  whatever  line-ending
3922         sequence is in  use,  and  whatever  setting  of  the  PCRE_DOTALL  and         sequence  is  in  use,  and  whatever  setting  of  the PCRE_DOTALL and
3923         PCRE_MULTILINE options is used. A class such as [^a] always matches one         PCRE_MULTILINE options is used. A class such as [^a] always matches one
3924         of these characters.         of these characters.
3925    
3926         The minus (hyphen) character can be used to specify a range of  charac-         The  minus (hyphen) character can be used to specify a range of charac-
3927         ters  in  a  character  class.  For  example,  [d-m] matches any letter         ters in a character  class.  For  example,  [d-m]  matches  any  letter
3928         between d and m, inclusive. If a  minus  character  is  required  in  a         between  d  and  m,  inclusive.  If  a minus character is required in a
3929         class,  it  must  be  escaped  with a backslash or appear in a position         class, it must be escaped with a backslash  or  appear  in  a  position
3930         where it cannot be interpreted as indicating a range, typically as  the         where  it cannot be interpreted as indicating a range, typically as the
3931         first or last character in the class.         first or last character in the class.
3932    
3933         It is not possible to have the literal character "]" as the end charac-         It is not possible to have the literal character "]" as the end charac-
3934         ter of a range. A pattern such as [W-]46] is interpreted as a class  of         ter  of a range. A pattern such as [W-]46] is interpreted as a class of
3935         two  characters ("W" and "-") followed by a literal string "46]", so it         two characters ("W" and "-") followed by a literal string "46]", so  it
3936         would match "W46]" or "-46]". However, if the "]"  is  escaped  with  a         would  match  "W46]"  or  "-46]". However, if the "]" is escaped with a
3937         backslash  it is interpreted as the end of range, so [W-\]46] is inter-         backslash it is interpreted as the end of range, so [W-\]46] is  inter-
3938         preted as a class containing a range followed by two other  characters.         preted  as a class containing a range followed by two other characters.
3939         The  octal or hexadecimal representation of "]" can also be used to end         The octal or hexadecimal representation of "]" can also be used to  end
3940         a range.         a range.
3941    
3942         Ranges operate in the collating sequence of character values. They  can         Ranges  operate in the collating sequence of character values. They can
3943         also   be  used  for  characters  specified  numerically,  for  example         also  be  used  for  characters  specified  numerically,  for   example
3944         [\000-\037]. In UTF-8 mode, ranges can include characters whose  values         [\000-\037].  In UTF-8 mode, ranges can include characters whose values
3945         are greater than 255, for example [\x{100}-\x{2ff}].         are greater than 255, for example [\x{100}-\x{2ff}].
3946    
3947         If a range that includes letters is used when caseless matching is set,         If a range that includes letters is used when caseless matching is set,
3948         it matches the letters in either case. For example, [W-c] is equivalent         it matches the letters in either case. For example, [W-c] is equivalent
3949         to  [][\\^_`wxyzabc],  matched  caselessly,  and  in non-UTF-8 mode, if         to [][\\^_`wxyzabc], matched caselessly,  and  in  non-UTF-8  mode,  if
3950         character tables for a French locale are in  use,  [\xc8-\xcb]  matches         character  tables  for  a French locale are in use, [\xc8-\xcb] matches
3951         accented  E  characters in both cases. In UTF-8 mode, PCRE supports the         accented E characters in both cases. In UTF-8 mode, PCRE  supports  the
3952         concept of case for characters with values greater than 128  only  when         concept  of  case for characters with values greater than 128 only when
3953         it is compiled with Unicode property support.         it is compiled with Unicode property support.
3954    
3955         The  character types \d, \D, \p, \P, \s, \S, \w, and \W may also appear         The character types \d, \D, \h, \H, \p, \P, \s, \S, \v, \V, \w, and  \W
3956         in a character class, and add the characters that  they  match  to  the         may  also appear in a character class, and add the characters that they
3957         class. For example, [\dABCDEF] matches any hexadecimal digit. A circum-         match to the class. For example,  [\dABCDEF]  matches  any  hexadecimal
3958         flex can conveniently be used with the upper case  character  types  to         digit.  A circumflex can conveniently be used with the upper case char-
3959         specify  a  more  restricted  set of characters than the matching lower         acter types to specify a more restricted set  of  characters  than  the
3960         case type. For example, the class [^\W_] matches any letter  or  digit,         matching  lower  case  type.  For example, the class [^\W_] matches any
3961         but not underscore.         letter or digit, but not underscore.
3962    
3963         The  only  metacharacters  that are recognized in character classes are         The only metacharacters that are recognized in  character  classes  are
3964         backslash, hyphen (only where it can be  interpreted  as  specifying  a         backslash,  hyphen  (only  where  it can be interpreted as specifying a
3965         range),  circumflex  (only  at the start), opening square bracket (only         range), circumflex (only at the start), opening  square  bracket  (only
3966         when it can be interpreted as introducing a POSIX class name - see  the         when  it can be interpreted as introducing a POSIX class name - see the
3967         next  section),  and  the  terminating closing square bracket. However,         next section), and the terminating  closing  square  bracket.  However,
3968         escaping other non-alphanumeric characters does no harm.         escaping other non-alphanumeric characters does no harm.
3969    
3970    
3971  POSIX CHARACTER CLASSES  POSIX CHARACTER CLASSES
3972    
3973         Perl supports the POSIX notation for character classes. This uses names         Perl supports the POSIX notation for character classes. This uses names
3974         enclosed  by  [: and :] within the enclosing square brackets. PCRE also         enclosed by [: and :] within the enclosing square brackets.  PCRE  also
3975         supports this notation. For example,         supports this notation. For example,
3976    
3977           [01[:alpha:]%]           [01[:alpha:]%]
3978    
3979         matches "0", "1", any alphabetic character, or "%". The supported class         matches "0", "1", any alphabetic character, or "%". The supported class
3980         names are         names are:
3981    
3982           alnum    letters and digits           alnum    letters and digits
3983           alpha    letters           alpha    letters
# Line 3942  POSIX CHARACTER CLASSES Line 3988  POSIX CHARACTER CLASSES
3988           graph    printing characters, excluding space           graph    printing characters, excluding space
3989           lower    lower case letters           lower    lower case letters
3990           print    printing characters, including space           print    printing characters, including space
3991           punct    printing characters, excluding letters and digits           punct    printing characters, excluding letters and digits and space
3992           space    white space (not quite the same as \s)           space    white space (not quite the same as \s)
3993           upper    upper case letters           upper    upper case letters
3994           word     "word" characters (same as \w)           word     "word" characters (same as \w)
3995           xdigit   hexadecimal digits           xdigit   hexadecimal digits
3996    
3997         The  "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13),         The "space" characters are HT (9), LF (10), VT (11), FF (12), CR  (13),
3998         and space (32). Notice that this list includes the VT  character  (code         and  space  (32). Notice that this list includes the VT character (code
3999         11). This makes "space" different to \s, which does not include VT (for         11). This makes "space" different to \s, which does not include VT (for
4000         Perl compatibility).         Perl compatibility).
4001    
4002         The name "word" is a Perl extension, and "blank"  is  a  GNU  extension         The  name  "word"  is  a Perl extension, and "blank" is a GNU extension
4003         from  Perl  5.8. Another Perl extension is negation, which is indicated         from Perl 5.8. Another Perl extension is negation, which  is  indicated
4004         by a ^ character after the colon. For example,         by a ^ character after the colon. For example,
4005    
4006           [12[:^digit:]]           [12[:^digit:]]
4007    
4008         matches "1", "2", or any non-digit. PCRE (and Perl) also recognize  the         matches  "1", "2", or any non-digit. PCRE (and Perl) also recognize the
4009         POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but         POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but
4010         these are not supported, and an error is given if they are encountered.         these are not supported, and an error is given if they are encountered.
4011    
4012         In UTF-8 mode, characters with values greater than 128 do not match any         By  default,  in UTF-8 mode, characters with values greater than 128 do
4013         of the POSIX character classes.         not match any of the POSIX character classes. However, if the  PCRE_UCP
4014           option  is passed to pcre_compile(), some of the classes are changed so
4015           that Unicode character properties are used. This is achieved by replac-
4016           ing the POSIX classes by other sequences, as follows:
4017    
4018             [:alnum:]  becomes  \p{Xan}
4019             [:alpha:]  becomes  \p{L}
4020             [:blank:]  becomes  \h
4021             [:digit:]  becomes  \p{Nd}
4022             [:lower:]  becomes  \p{Ll}
4023             [:space:]  becomes  \p{Xps}
4024             [:upper:]  becomes  \p{Lu}
4025             [:word:]   becomes  \p{Xwd}
4026    
4027           Negated  versions,  such  as [:^alpha:] use \P instead of \p. The other
4028           POSIX classes are unchanged, and match only characters with code points
4029           less than 128.
4030    
4031    
4032  VERTICAL BAR  VERTICAL BAR
# Line 4035  INTERNAL OPTION SETTING Line 4097  INTERNAL OPTION SETTING
4097         cases the pattern can contain special leading sequences such as (*CRLF)         cases the pattern can contain special leading sequences such as (*CRLF)
4098         to  override  what  the application has set or what has been defaulted.         to  override  what  the application has set or what has been defaulted.
4099         Details are given in the section entitled  "Newline  sequences"  above.         Details are given in the section entitled  "Newline  sequences"  above.
4100         There  is  also  the  (*UTF8)  leading sequence that can be used to set         There  are  also  the  (*UTF8) and (*UCP) leading sequences that can be
4101         UTF-8 mode; this is equivalent to setting the PCRE_UTF8 option.         used to set UTF-8 and Unicode property modes; they  are  equivalent  to
4102           setting the PCRE_UTF8 and the PCRE_UCP options, respectively.
4103    
4104    
4105  SUBPATTERNS  SUBPATTERNS
# Line 4048  SUBPATTERNS Line 4111  SUBPATTERNS
4111    
4112           cat(aract|erpillar|)           cat(aract|erpillar|)
4113    
4114         matches  one  of the words "cat", "cataract", or "caterpillar". Without         matches one of the words "cat", "cataract", or  "caterpillar".  Without
4115         the parentheses, it would match  "cataract",  "erpillar"  or  an  empty         the  parentheses,  it  would  match  "cataract", "erpillar" or an empty
4116         string.         string.
4117    
4118         2.  It  sets  up  the  subpattern as a capturing subpattern. This means         2. It sets up the subpattern as  a  capturing  subpattern.  This  means
4119         that, when the whole pattern  matches,  that  portion  of  the  subject         that,  when  the  whole  pattern  matches,  that portion of the subject
4120         string that matched the subpattern is passed back to the caller via the         string that matched the subpattern is passed back to the caller via the
4121         ovector argument of pcre_exec(). Opening parentheses are  counted  from         ovector  argument  of pcre_exec(). Opening parentheses are counted from
4122         left  to  right  (starting  from 1) to obtain numbers for the capturing         left to right (starting from 1) to obtain  numbers  for  the  capturing
4123         subpatterns.         subpatterns.
4124    
4125         For example, if the string "the red king" is matched against  the  pat-         For  example,  if the string "the red king" is matched against the pat-
4126         tern         tern
4127    
4128           the ((red|white) (king|queen))           the ((red|white) (king|queen))
# Line 4067  SUBPATTERNS Line 4130  SUBPATTERNS
4130         the captured substrings are "red king", "red", and "king", and are num-         the captured substrings are "red king", "red", and "king", and are num-
4131         bered 1, 2, and 3, respectively.         bered 1, 2, and 3, respectively.
4132    
4133         The fact that plain parentheses fulfil  two  functions  is  not  always         The  fact  that  plain  parentheses  fulfil two functions is not always
4134         helpful.   There are often times when a grouping subpattern is required         helpful.  There are often times when a grouping subpattern is  required
4135         without a capturing requirement. If an opening parenthesis is  followed         without  a capturing requirement. If an opening parenthesis is followed
4136         by  a question mark and a colon, the subpattern does not do any captur-         by a question mark and a colon, the subpattern does not do any  captur-
4137         ing, and is not counted when computing the  number  of  any  subsequent         ing,  and  is  not  counted when computing the number of any subsequent
4138         capturing  subpatterns. For example, if the string "the white queen" is         capturing subpatterns. For example, if the string "the white queen"  is
4139         matched against the pattern         matched against the pattern
4140    
4141           the ((?:red|white) (king|queen))           the ((?:red|white) (king|queen))
# Line 4080  SUBPATTERNS Line 4143  SUBPATTERNS
4143         the captured substrings are "white queen" and "queen", and are numbered         the captured substrings are "white queen" and "queen", and are numbered
4144         1 and 2. The maximum number of capturing subpatterns is 65535.         1 and 2. The maximum number of capturing subpatterns is 65535.
4145    
4146         As  a  convenient shorthand, if any option settings are required at the         As a convenient shorthand, if any option settings are required  at  the
4147         start of a non-capturing subpattern,  the  option  letters  may  appear         start  of  a  non-capturing  subpattern,  the option letters may appear
4148         between the "?" and the ":". Thus the two patterns         between the "?" and the ":". Thus the two patterns
4149    
4150           (?i:saturday|sunday)           (?i:saturday|sunday)
4151           (?:(?i)saturday|sunday)           (?:(?i)saturday|sunday)
4152    
4153         match exactly the same set of strings. Because alternative branches are         match exactly the same set of strings. Because alternative branches are
4154         tried from left to right, and options are not reset until  the  end  of         tried  from  left  to right, and options are not reset until the end of
4155         the  subpattern is reached, an option setting in one branch does affect         the subpattern is reached, an option setting in one branch does  affect
4156         subsequent branches, so the above patterns match "SUNDAY"  as  well  as         subsequent  branches,  so  the above patterns match "SUNDAY" as well as
4157         "Saturday".         "Saturday".
4158    
4159    
4160  DUPLICATE SUBPATTERN NUMBERS  DUPLICATE SUBPATTERN NUMBERS
4161    
4162         Perl 5.10 introduced a feature whereby each alternative in a subpattern         Perl 5.10 introduced a feature whereby each alternative in a subpattern
4163         uses the same numbers for its capturing parentheses. Such a  subpattern         uses  the same numbers for its capturing parentheses. Such a subpattern
4164         starts  with (?| and is itself a non-capturing subpattern. For example,         starts with (?| and is itself a non-capturing subpattern. For  example,
4165         consider this pattern:         consider this pattern:
4166    
4167           (?|(Sat)ur|(Sun))day           (?|(Sat)ur|(Sun))day
4168    
4169         Because the two alternatives are inside a (?| group, both sets of  cap-         Because  the two alternatives are inside a (?| group, both sets of cap-
4170         turing  parentheses  are  numbered one. Thus, when the pattern matches,         turing parentheses are numbered one. Thus, when  the  pattern  matches,
4171         you can look at captured substring number  one,  whichever  alternative         you  can  look  at captured substring number one, whichever alternative
4172         matched.  This  construct  is useful when you want to capture part, but         matched. This construct is useful when you want to  capture  part,  but
4173         not all, of one of a number of alternatives. Inside a (?| group, paren-         not all, of one of a number of alternatives. Inside a (?| group, paren-
4174         theses  are  numbered as usual, but the number is reset at the start of         theses are numbered as usual, but the number is reset at the  start  of
4175         each branch. The numbers of any capturing buffers that follow the  sub-         each  branch. The numbers of any capturing buffers that follow the sub-
4176         pattern  start after the highest number used in any branch. The follow-         pattern start after the highest number used in any branch. The  follow-
4177         ing example is taken from the Perl documentation.  The  numbers  under-         ing  example  is taken from the Perl documentation.  The numbers under-
4178         neath show in which buffer the captured content will be stored.         neath show in which buffer the captured content will be stored.
4179    
4180           # before  ---------------branch-reset----------- after           # before  ---------------branch-reset----------- after
4181           / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x           / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
4182           # 1            2         2  3        2     3     4           # 1            2         2  3        2     3     4
4183    
4184         A  back  reference  to a numbered subpattern uses the most recent value         A back reference to a numbered subpattern uses the  most  recent  value
4185         that is set for that number by any subpattern.  The  following  pattern         that  is  set  for that number by any subpattern. The following pattern
4186         matches "abcabc" or "defdef":         matches "abcabc" or "defdef":
4187    
4188           /(?|(abc)|(def))\1/           /(?|(abc)|(def))\1/
4189    
4190         In  contrast, a recursive or "subroutine" call to a numbered subpattern         In contrast, a recursive or "subroutine" call to a numbered  subpattern
4191         always refers to the first one in the pattern with  the  given  number.         always  refers  to  the first one in the pattern with the given number.
4192         The following pattern matches "abcabc" or "defabc":         The following pattern matches "abcabc" or "defabc":
4193    
4194           /(?|(abc)|(def))(?1)/           /(?|(abc)|(def))(?1)/
4195    
4196         If  a condition test for a subpattern's having matched refers to a non-         If a condition test for a subpattern's having matched refers to a  non-
4197         unique number, the test is true if any of the subpatterns of that  num-         unique  number, the test is true if any of the subpatterns of that num-
4198         ber have matched.         ber have matched.
4199    
4200         An  alternative approach to using this "branch reset" feature is to use         An alternative approach to using this "branch reset" feature is to  use
4201         duplicate named subpatterns, as described in the next section.         duplicate named subpatterns, as described in the next section.
4202    
4203    
4204  NAMED SUBPATTERNS  NAMED SUBPATTERNS
4205    
4206         Identifying capturing parentheses by number is simple, but  it  can  be         Identifying  capturing  parentheses  by number is simple, but it can be
4207         very  hard  to keep track of the numbers in complicated regular expres-         very hard to keep track of the numbers in complicated  regular  expres-
4208         sions. Furthermore, if an  expression  is  modified,  the  numbers  may         sions.  Furthermore,  if  an  expression  is  modified, the numbers may
4209         change.  To help with this difficulty, PCRE supports the naming of sub-         change. To help with this difficulty, PCRE supports the naming of  sub-
4210         patterns. This feature was not added to Perl until release 5.10. Python         patterns. This feature was not added to Perl until release 5.10. Python
4211         had  the  feature earlier, and PCRE introduced it at release 4.0, using         had the feature earlier, and PCRE introduced it at release  4.0,  using
4212         the Python syntax. PCRE now supports both the Perl and the Python  syn-         the  Python syntax. PCRE now supports both the Perl and the Python syn-
4213         tax.  Perl  allows  identically  numbered subpatterns to have different         tax. Perl allows identically numbered  subpatterns  to  have  different
4214         names, but PCRE does not.         names, but PCRE does not.
4215    
4216         In PCRE, a subpattern can be named in one of three  ways:  (?<name>...)         In  PCRE,  a subpattern can be named in one of three ways: (?<name>...)
4217         or  (?'name'...)  as in Perl, or (?P<name>...) as in Python. References         or (?'name'...) as in Perl, or (?P<name>...) as in  Python.  References
4218         to capturing parentheses from other parts of the pattern, such as  back         to  capturing parentheses from other parts of the pattern, such as back
4219         references,  recursion,  and conditions, can be made by name as well as         references, recursion, and conditions, can be made by name as  well  as
4220         by number.         by number.
4221    
4222         Names consist of up to  32  alphanumeric  characters  and  underscores.         Names  consist  of  up  to  32 alphanumeric characters and underscores.
4223         Named  capturing  parentheses  are  still  allocated numbers as well as         Named capturing parentheses are still  allocated  numbers  as  well  as
4224         names, exactly as if the names were not present. The PCRE API  provides         names,  exactly as if the names were not present. The PCRE API provides
4225         function calls for extracting the name-to-number translation table from         function calls for extracting the name-to-number translation table from
4226         a compiled pattern. There is also a convenience function for extracting         a compiled pattern. There is also a convenience function for extracting
4227         a captured substring by name.         a captured substring by name.
4228    
4229         By  default, a name must be unique within a pattern, but it is possible         By default, a name must be unique within a pattern, but it is  possible
4230         to relax this constraint by setting the PCRE_DUPNAMES option at compile         to relax this constraint by setting the PCRE_DUPNAMES option at compile
4231         time.  (Duplicate  names are also always permitted for subpatterns with         time. (Duplicate names are also always permitted for  subpatterns  with
4232         the same number, set up as described in the previous  section.)  Dupli-         the  same  number, set up as described in the previous section.) Dupli-
4233         cate  names  can  be useful for patterns where only one instance of the         cate names can be useful for patterns where only one  instance  of  the
4234         named parentheses can match. Suppose you want to match the  name  of  a         named  parentheses  can  match. Suppose you want to match the name of a
4235         weekday,  either as a 3-letter abbreviation or as the full name, and in         weekday, either as a 3-letter abbreviation or as the full name, and  in
4236         both cases you want to extract the abbreviation. This pattern (ignoring         both cases you want to extract the abbreviation. This pattern (ignoring
4237         the line breaks) does the job:         the line breaks) does the job:
4238    
# Line 4179  NAMED SUBPATTERNS Line 4242  NAMED SUBPATTERNS
4242           (?<DN>Thu)(?:rsday)?|           (?<DN>Thu)(?:rsday)?|
4243           (?<DN>Sat)(?:urday)?           (?<DN>Sat)(?:urday)?
4244    
4245         There  are  five capturing substrings, but only one is ever set after a         There are five capturing substrings, but only one is ever set  after  a
4246         match.  (An alternative way of solving this problem is to use a "branch         match.  (An alternative way of solving this problem is to use a "branch
4247         reset" subpattern, as described in the previous section.)         reset" subpattern, as described in the previous section.)
4248    
4249         The  convenience  function  for extracting the data by name returns the         The convenience function for extracting the data by  name  returns  the
4250         substring for the first (and in this example, the only)  subpattern  of         substring  for  the first (and in this example, the only) subpattern of
4251         that  name  that  matched.  This saves searching to find which numbered         that name that matched. This saves searching  to  find  which  numbered
4252         subpattern it was.         subpattern it was.
4253    
4254         If you make a back reference to  a  non-unique  named  subpattern  from         If  you  make  a  back  reference to a non-unique named subpattern from
4255         elsewhere  in the pattern, the one that corresponds to the first occur-         elsewhere in the pattern, the one that corresponds to the first  occur-
4256         rence of the name is used. In the absence of duplicate numbers (see the         rence of the name is used. In the absence of duplicate numbers (see the
4257         previous  section) this is the one with the lowest number. If you use a         previous section) this is the one with the lowest number. If you use  a
4258         named reference in a condition test (see the section  about  conditions         named  reference  in a condition test (see the section about conditions
4259         below),  either  to check whether a subpattern has matched, or to check         below), either to check whether a subpattern has matched, or  to  check
4260         for recursion, all subpatterns with the same name are  tested.  If  the         for  recursion,  all  subpatterns with the same name are tested. If the
4261         condition  is  true for any one of them, the overall condition is true.         condition is true for any one of them, the overall condition  is  true.
4262         This is the same behaviour as testing by number. For further details of         This is the same behaviour as testing by number. For further details of
4263         the interfaces for handling named subpatterns, see the pcreapi documen-         the interfaces for handling named subpatterns, see the pcreapi documen-
4264         tation.         tation.
4265    
4266         Warning: You cannot use different names to distinguish between two sub-         Warning: You cannot use different names to distinguish between two sub-
4267         patterns  with  the same number because PCRE uses only the numbers when         patterns with the same number because PCRE uses only the  numbers  when
4268         matching. For this reason, an error is given at compile time if differ-         matching. For this reason, an error is given at compile time if differ-
4269         ent  names  are given to subpatterns with the same number. However, you         ent names are given to subpatterns with the same number.  However,  you
4270         can give the same name to subpatterns with the same number,  even  when         can  give  the same name to subpatterns with the same number, even when
4271         PCRE_DUPNAMES is not set.         PCRE_DUPNAMES is not set.
4272    
4273    
4274  REPETITION  REPETITION
4275    
4276         Repetition  is  specified  by  quantifiers, which can follow any of the         Repetition is specified by quantifiers, which can  follow  any  of  the
4277         following items:         following items:
4278    
4279           a literal data character           a literal data character
# Line 4224  REPETITION Line 4287  REPETITION
4287           a parenthesized subpattern (unless it is an assertion)           a parenthesized subpattern (unless it is an assertion)
4288           a recursive or "subroutine" call to a subpattern           a recursive or "subroutine" call to a subpattern
4289    
4290         The general repetition quantifier specifies a minimum and maximum  num-         The  general repetition quantifier specifies a minimum and maximum num-
4291         ber  of  permitted matches, by giving the two numbers in curly brackets         ber of permitted matches, by giving the two numbers in  curly  brackets
4292         (braces), separated by a comma. The numbers must be  less  than  65536,         (braces),  separated  by  a comma. The numbers must be less than 65536,
4293         and the first must be less than or equal to the second. For example:         and the first must be less than or equal to the second. For example:
4294    
4295           z{2,4}           z{2,4}
4296    
4297         matches  "zz",  "zzz",  or  "zzzz". A closing brace on its own is not a         matches "zz", "zzz", or "zzzz". A closing brace on its  own  is  not  a
4298         special character. If the second number is omitted, but  the  comma  is         special  character.  If  the second number is omitted, but the comma is
4299         present,  there  is  no upper limit; if the second number and the comma         present, there is no upper limit; if the second number  and  the  comma
4300         are both omitted, the quantifier specifies an exact number of  required         are  both omitted, the quantifier specifies an exact number of required
4301         matches. Thus         matches. Thus
4302    
4303           [aeiou]{3,}           [aeiou]{3,}
# Line 4243  REPETITION Line 4306  REPETITION
4306    
4307           \d{8}           \d{8}
4308    
4309         matches  exactly  8  digits. An opening curly bracket that appears in a         matches exactly 8 digits. An opening curly bracket that  appears  in  a
4310         position where a quantifier is not allowed, or one that does not  match         position  where a quantifier is not allowed, or one that does not match
4311         the  syntax of a quantifier, is taken as a literal character. For exam-         the syntax of a quantifier, is taken as a literal character. For  exam-
4312         ple, {,6} is not a quantifier, but a literal string of four characters.         ple, {,6} is not a quantifier, but a literal string of four characters.
4313    
4314         In UTF-8 mode, quantifiers apply to UTF-8  characters  rather  than  to         In  UTF-8  mode,  quantifiers  apply to UTF-8 characters rather than to
4315         individual bytes. Thus, for example, \x{100}{2} matches two UTF-8 char-         individual bytes. Thus, for example, \x{100}{2} matches two UTF-8 char-
4316         acters, each of which is represented by a two-byte sequence. Similarly,         acters, each of which is represented by a two-byte sequence. Similarly,
4317         when Unicode property support is available, \X{3} matches three Unicode         when Unicode property support is available, \X{3} matches three Unicode
4318         extended sequences, each of which may be several bytes long  (and  they         extended  sequences,  each of which may be several bytes long (and they
4319         may be of different lengths).         may be of different lengths).
4320    
4321         The quantifier {0} is permitted, causing the expression to behave as if         The quantifier {0} is permitted, causing the expression to behave as if
4322         the previous item and the quantifier were not present. This may be use-         the previous item and the quantifier were not present. This may be use-
4323         ful  for  subpatterns that are referenced as subroutines from elsewhere         ful for subpatterns that are referenced as subroutines  from  elsewhere
4324         in the pattern. Items other than subpatterns that have a {0} quantifier         in the pattern. Items other than subpatterns that have a {0} quantifier
4325         are omitted from the compiled pattern.         are omitted from the compiled pattern.
4326    
4327         For  convenience, the three most common quantifiers have single-charac-         For convenience, the three most common quantifiers have  single-charac-
4328         ter abbreviations:         ter abbreviations:
4329    
4330           *    is equivalent to {0,}           *    is equivalent to {0,}
4331           +    is equivalent to {1,}           +    is equivalent to {1,}
4332           ?    is equivalent to {0,1}           ?    is equivalent to {0,1}
4333    
4334         It is possible to construct infinite loops by  following  a  subpattern         It  is  possible  to construct infinite loops by following a subpattern
4335         that can match no characters with a quantifier that has no upper limit,         that can match no characters with a quantifier that has no upper limit,
4336         for example:         for example:
4337    
4338           (a?)*           (a?)*
4339    
4340         Earlier versions of Perl and PCRE used to give an error at compile time         Earlier versions of Perl and PCRE used to give an error at compile time
4341         for  such  patterns. However, because there are cases where this can be         for such patterns. However, because there are cases where this  can  be
4342         useful, such patterns are now accepted, but if any  repetition  of  the         useful,  such  patterns  are now accepted, but if any repetition of the
4343         subpattern  does in fact match no characters, the loop is forcibly bro-         subpattern does in fact match no characters, the loop is forcibly  bro-
4344         ken.         ken.
4345    
4346         By default, the quantifiers are "greedy", that is, they match  as  much         By  default,  the quantifiers are "greedy", that is, they match as much
4347         as  possible  (up  to  the  maximum number of permitted times), without         as possible (up to the maximum  number  of  permitted  times),  without
4348         causing the rest of the pattern to fail. The classic example  of  where         causing  the  rest of the pattern to fail. The classic example of where
4349         this gives problems is in trying to match comments in C programs. These         this gives problems is in trying to match comments in C programs. These
4350         appear between /* and */ and within the comment,  individual  *  and  /         appear  between  /*  and  */ and within the comment, individual * and /
4351         characters  may  appear. An attempt to match C comments by applying the         characters may appear. An attempt to match C comments by  applying  the
4352         pattern         pattern
4353    
4354           /\*.*\*/           /\*.*\*/
# Line 4294  REPETITION Line 4357  REPETITION
4357    
4358           /* first comment */  not comment  /* second comment */           /* first comment */  not comment  /* second comment */
4359    
4360         fails, because it matches the entire string owing to the greediness  of         fails,  because it matches the entire string owing to the greediness of
4361         the .*  item.         the .*  item.
4362    
4363         However,  if  a quantifier is followed by a question mark, it ceases to         However, if a quantifier is followed by a question mark, it  ceases  to
4364         be greedy, and instead matches the minimum number of times possible, so         be greedy, and instead matches the minimum number of times possible, so
4365         the pattern         the pattern
4366    
4367           /\*.*?\*/           /\*.*?\*/
4368    
4369         does  the  right  thing with the C comments. The meaning of the various         does the right thing with the C comments. The meaning  of  the  various
4370         quantifiers is not otherwise changed,  just  the  preferred  number  of         quantifiers  is  not  otherwise  changed,  just the preferred number of
4371         matches.   Do  not  confuse this use of question mark with its use as a         matches.  Do not confuse this use of question mark with its  use  as  a
4372         quantifier in its own right. Because it has two uses, it can  sometimes         quantifier  in its own right. Because it has two uses, it can sometimes
4373         appear doubled, as in         appear doubled, as in
4374    
4375           \d??\d           \d??\d
# Line 4314  REPETITION Line 4377  REPETITION
4377         which matches one digit by preference, but can match two if that is the         which matches one digit by preference, but can match two if that is the
4378         only way the rest of the pattern matches.         only way the rest of the pattern matches.
4379    
4380         If the PCRE_UNGREEDY option is set (an option that is not available  in         If  the PCRE_UNGREEDY option is set (an option that is not available in
4381         Perl),  the  quantifiers are not greedy by default, but individual ones         Perl), the quantifiers are not greedy by default, but  individual  ones
4382         can be made greedy by following them with a  question  mark.  In  other         can  be  made  greedy  by following them with a question mark. In other
4383         words, it inverts the default behaviour.         words, it inverts the default behaviour.
4384    
4385         When  a  parenthesized  subpattern  is quantified with a minimum repeat         When a parenthesized subpattern is quantified  with  a  minimum  repeat
4386         count that is greater than 1 or with a limited maximum, more memory  is         count  that is greater than 1 or with a limited maximum, more memory is
4387         required  for  the  compiled  pattern, in proportion to the size of the         required for the compiled pattern, in proportion to  the  size  of  the
4388         minimum or maximum.         minimum or maximum.
4389    
4390         If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv-         If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv-
4391         alent  to  Perl's  /s) is set, thus allowing the dot to match newlines,         alent to Perl's /s) is set, thus allowing the dot  to  match  newlines,
4392         the pattern is implicitly anchored, because whatever  follows  will  be         the  pattern  is  implicitly anchored, because whatever follows will be
4393         tried  against every character position in the subject string, so there         tried against every character position in the subject string, so  there
4394         is no point in retrying the overall match at  any  position  after  the         is  no  point  in  retrying the overall match at any position after the
4395         first.  PCRE  normally treats such a pattern as though it were preceded         first. PCRE normally treats such a pattern as though it  were  preceded
4396         by \A.         by \A.
4397    
4398         In cases where it is known that the subject  string  contains  no  new-         In  cases  where  it  is known that the subject string contains no new-
4399         lines,  it  is  worth setting PCRE_DOTALL in order to obtain this opti-         lines, it is worth setting PCRE_DOTALL in order to  obtain  this  opti-
4400         mization, or alternatively using ^ to indicate anchoring explicitly.         mization, or alternatively using ^ to indicate anchoring explicitly.
4401    
4402         However, there is one situation where the optimization cannot be  used.         However,  there is one situation where the optimization cannot be used.
4403         When .*  is inside capturing parentheses that are the subject of a back         When .*  is inside capturing parentheses that are the subject of a back
4404         reference elsewhere in the pattern, a match at the start may fail where         reference elsewhere in the pattern, a match at the start may fail where
4405         a later one succeeds. Consider, for example:         a later one succeeds. Consider, for example:
4406    
4407           (.*)abc\1           (.*)abc\1
4408    
4409         If  the subject is "xyz123abc123" the match point is the fourth charac-         If the subject is "xyz123abc123" the match point is the fourth  charac-
4410         ter. For this reason, such a pattern is not implicitly anchored.         ter. For this reason, such a pattern is not implicitly anchored.
4411    
4412         When a capturing subpattern is repeated, the value captured is the sub-         When a capturing subpattern is repeated, the value captured is the sub-
# Line 4352  REPETITION Line 4415  REPETITION
4415           (tweedle[dume]{3}\s*)+           (tweedle[dume]{3}\s*)+
4416    
4417         has matched "tweedledum tweedledee" the value of the captured substring         has matched "tweedledum tweedledee" the value of the captured substring
4418         is "tweedledee". However, if there are  nested  capturing  subpatterns,         is  "tweedledee".  However,  if there are nested capturing subpatterns,
4419         the  corresponding captured values may have been set in previous itera-         the corresponding captured values may have been set in previous  itera-
4420         tions. For example, after         tions. For example, after
4421    
4422           /(a|(b))+/           /(a|(b))+/
# Line 4363  REPETITION Line 4426  REPETITION
4426    
4427  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS
4428    
4429         With both maximizing ("greedy") and minimizing ("ungreedy"  or  "lazy")         With  both  maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
4430         repetition,  failure  of what follows normally causes the repeated item         repetition, failure of what follows normally causes the  repeated  item
4431         to be re-evaluated to see if a different number of repeats  allows  the         to  be  re-evaluated to see if a different number of repeats allows the
4432         rest  of  the pattern to match. Sometimes it is useful to prevent this,         rest of the pattern to match. Sometimes it is useful to  prevent  this,
4433         either to change the nature of the match, or to cause it  fail  earlier         either  to  change the nature of the match, or to cause it fail earlier
4434         than  it otherwise might, when the author of the pattern knows there is         than it otherwise might, when the author of the pattern knows there  is
4435         no point in carrying on.         no point in carrying on.
4436    
4437         Consider, for example, the pattern \d+foo when applied to  the  subject         Consider,  for  example, the pattern \d+foo when applied to the subject
4438         line         line
4439    
4440           123456bar           123456bar
4441    
4442         After matching all 6 digits and then failing to match "foo", the normal         After matching all 6 digits and then failing to match "foo", the normal
4443         action of the matcher is to try again with only 5 digits  matching  the         action  of  the matcher is to try again with only 5 digits matching the
4444         \d+  item,  and  then  with  4,  and  so on, before ultimately failing.         \d+ item, and then with  4,  and  so  on,  before  ultimately  failing.
4445         "Atomic grouping" (a term taken from Jeffrey  Friedl's  book)  provides         "Atomic  grouping"  (a  term taken from Jeffrey Friedl's book) provides
4446         the  means for specifying that once a subpattern has matched, it is not         the means for specifying that once a subpattern has matched, it is  not
4447         to be re-evaluated in this way.         to be re-evaluated in this way.
4448    
4449         If we use atomic grouping for the previous example, the  matcher  gives         If  we  use atomic grouping for the previous example, the matcher gives
4450         up  immediately  on failing to match "foo" the first time. The notation         up immediately on failing to match "foo" the first time.  The  notation
4451         is a kind of special parenthesis, starting with (?> as in this example:         is a kind of special parenthesis, starting with (?> as in this example:
4452    
4453           (?>\d+)foo           (?>\d+)foo
4454    
4455         This kind of parenthesis "locks up" the  part of the  pattern  it  con-         This  kind  of  parenthesis "locks up" the  part of the pattern it con-
4456         tains  once  it  has matched, and a failure further into the pattern is         tains once it has matched, and a failure further into  the  pattern  is
4457         prevented from backtracking into it. Backtracking past it  to  previous         prevented  from  backtracking into it. Backtracking past it to previous
4458         items, however, works as normal.         items, however, works as normal.
4459    
4460         An  alternative  description  is that a subpattern of this type matches         An alternative description is that a subpattern of  this  type  matches
4461         the string of characters that an  identical  standalone  pattern  would         the  string  of  characters  that an identical standalone pattern would
4462         match, if anchored at the current point in the subject string.         match, if anchored at the current point in the subject string.
4463    
4464         Atomic grouping subpatterns are not capturing subpatterns. Simple cases         Atomic grouping subpatterns are not capturing subpatterns. Simple cases
4465         such as the above example can be thought of as a maximizing repeat that         such as the above example can be thought of as a maximizing repeat that
4466         must  swallow  everything  it can. So, while both \d+ and \d+? are pre-         must swallow everything it can. So, while both \d+ and  \d+?  are  pre-
4467         pared to adjust the number of digits they match in order  to  make  the         pared  to  adjust  the number of digits they match in order to make the
4468         rest of the pattern match, (?>\d+) can only match an entire sequence of         rest of the pattern match, (?>\d+) can only match an entire sequence of
4469         digits.         digits.
4470    
4471         Atomic groups in general can of course contain arbitrarily  complicated         Atomic  groups in general can of course contain arbitrarily complicated
4472         subpatterns,  and  can  be  nested. However, when the subpattern for an         subpatterns, and can be nested. However, when  the  subpattern  for  an
4473         atomic group is just a single repeated item, as in the example above, a         atomic group is just a single repeated item, as in the example above, a
4474         simpler  notation,  called  a "possessive quantifier" can be used. This         simpler notation, called a "possessive quantifier" can  be  used.  This
4475         consists of an additional + character  following  a  quantifier.  Using         consists  of  an  additional  + character following a quantifier. Using
4476         this notation, the previous example can be rewritten as         this notation, the previous example can be rewritten as
4477    
4478           \d++foo           \d++foo
# Line 4419  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE Line 4482  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE
4482    
4483           (abc|xyz){2,3}+           (abc|xyz){2,3}+
4484    
4485         Possessive  quantifiers  are  always  greedy;  the   setting   of   the         Possessive   quantifiers   are   always  greedy;  the  setting  of  the
4486         PCRE_UNGREEDY option is ignored. They are a convenient notation for the         PCRE_UNGREEDY option is ignored. They are a convenient notation for the
4487         simpler forms of atomic group. However, there is no difference  in  the         simpler  forms  of atomic group. However, there is no difference in the
4488         meaning  of  a  possessive  quantifier and the equivalent atomic group,         meaning of a possessive quantifier and  the  equivalent  atomic  group,
4489         though there may be a performance  difference;  possessive  quantifiers         though  there  may  be a performance difference; possessive quantifiers
4490         should be slightly faster.         should be slightly faster.
4491    
4492         The  possessive  quantifier syntax is an extension to the Perl 5.8 syn-         The possessive quantifier syntax is an extension to the Perl  5.8  syn-
4493         tax.  Jeffrey Friedl originated the idea (and the name)  in  the  first         tax.   Jeffrey  Friedl  originated the idea (and the name) in the first
4494         edition of his book. Mike McCloskey liked it, so implemented it when he         edition of his book. Mike McCloskey liked it, so implemented it when he
4495         built Sun's Java package, and PCRE copied it from there. It  ultimately         built  Sun's Java package, and PCRE copied it from there. It ultimately
4496         found its way into Perl at release 5.10.         found its way into Perl at release 5.10.
4497    
4498         PCRE has an optimization that automatically "possessifies" certain sim-         PCRE has an optimization that automatically "possessifies" certain sim-
4499         ple pattern constructs. For example, the sequence  A+B  is  treated  as         ple  pattern  constructs.  For  example, the sequence A+B is treated as
4500         A++B  because  there is no point in backtracking into a sequence of A's         A++B because there is no point in backtracking into a sequence  of  A's
4501         when B must follow.         when B must follow.
4502    
4503         When a pattern contains an unlimited repeat inside  a  subpattern  that         When  a  pattern  contains an unlimited repeat inside a subpattern that
4504         can  itself  be  repeated  an  unlimited number of times, the use of an         can itself be repeated an unlimited number of  times,  the  use  of  an
4505         atomic group is the only way to avoid some  failing  matches  taking  a         atomic  group  is  the  only way to avoid some failing matches taking a
4506         very long time indeed. The pattern         very long time indeed. The pattern
4507    
4508           (\D+|<\d+>)*[!?]           (\D+|<\d+>)*[!?]
4509    
4510         matches  an  unlimited number of substrings that either consist of non-         matches an unlimited number of substrings that either consist  of  non-
4511         digits, or digits enclosed in <>, followed by either ! or  ?.  When  it         digits,  or  digits  enclosed in <>, followed by either ! or ?. When it
4512         matches, it runs quickly. However, if it is applied to         matches, it runs quickly. However, if it is applied to
4513    
4514           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
4515    
4516         it  takes  a  long  time  before reporting failure. This is because the         it takes a long time before reporting  failure.  This  is  because  the
4517         string can be divided between the internal \D+ repeat and the  external         string  can be divided between the internal \D+ repeat and the external
4518         *  repeat  in  a  large  number of ways, and all have to be tried. (The         * repeat in a large number of ways, and all  have  to  be  tried.  (The
4519         example uses [!?] rather than a single character at  the  end,  because         example  uses  [!?]  rather than a single character at the end, because
4520         both  PCRE  and  Perl have an optimization that allows for fast failure         both PCRE and Perl have an optimization that allows  for  fast  failure
4521         when a single character is used. They remember the last single  charac-         when  a single character is used. They remember the last single charac-
4522         ter  that  is required for a match, and fail early if it is not present         ter that is required for a match, and fail early if it is  not  present
4523         in the string.) If the pattern is changed so that  it  uses  an  atomic         in  the  string.)  If  the pattern is changed so that it uses an atomic
4524         group, like this:         group, like this:
4525    
4526           ((?>\D+)|<\d+>)*[!?]           ((?>\D+)|<\d+>)*[!?]
# Line 4469  BACK REFERENCES Line 4532  BACK REFERENCES
4532    
4533         Outside a character class, a backslash followed by a digit greater than         Outside a character class, a backslash followed by a digit greater than
4534         0 (and possibly further digits) is a back reference to a capturing sub-         0 (and possibly further digits) is a back reference to a capturing sub-
4535         pattern  earlier  (that is, to its left) in the pattern, provided there         pattern earlier (that is, to its left) in the pattern,  provided  there
4536         have been that many previous capturing left parentheses.         have been that many previous capturing left parentheses.
4537    
4538         However, if the decimal number following the backslash is less than 10,         However, if the decimal number following the backslash is less than 10,
4539         it  is  always  taken  as a back reference, and causes an error only if         it is always taken as a back reference, and causes  an  error  only  if
4540         there are not that many capturing left parentheses in the  entire  pat-         there  are  not that many capturing left parentheses in the entire pat-
4541         tern.  In  other words, the parentheses that are referenced need not be         tern. In other words, the parentheses that are referenced need  not  be
4542         to the left of the reference for numbers less than 10. A "forward  back         to  the left of the reference for numbers less than 10. A "forward back
4543         reference"  of  this  type can make sense when a repetition is involved         reference" of this type can make sense when a  repetition  is  involved
4544         and the subpattern to the right has participated in an  earlier  itera-         and  the  subpattern to the right has participated in an earlier itera-
4545         tion.         tion.
4546    
4547         It  is  not  possible to have a numerical "forward back reference" to a         It is not possible to have a numerical "forward back  reference"  to  a
4548         subpattern whose number is 10 or  more  using  this  syntax  because  a         subpattern  whose  number  is  10  or  more using this syntax because a
4549         sequence  such  as  \50 is interpreted as a character defined in octal.         sequence such as \50 is interpreted as a character  defined  in  octal.
4550         See the subsection entitled "Non-printing characters" above for further         See the subsection entitled "Non-printing characters" above for further
4551         details  of  the  handling of digits following a backslash. There is no         details of the handling of digits following a backslash.  There  is  no
4552         such problem when named parentheses are used. A back reference  to  any         such  problem  when named parentheses are used. A back reference to any
4553         subpattern is possible using named parentheses (see below).         subpattern is possible using named parentheses (see below).
4554    
4555         Another  way  of  avoiding  the ambiguity inherent in the use of digits         Another way of avoiding the ambiguity inherent in  the  use  of  digits
4556         following a backslash is to use the \g escape sequence, which is a fea-         following a backslash is to use the \g escape sequence, which is a fea-
4557         ture  introduced  in  Perl  5.10.  This  escape  must be followed by an         ture introduced in Perl 5.10.  This  escape  must  be  followed  by  an
4558         unsigned number or a negative number, optionally  enclosed  in  braces.         unsigned  number  or  a negative number, optionally enclosed in braces.
4559         These examples are all identical:         These examples are all identical:
4560    
4561           (ring), \1           (ring), \1
4562           (ring), \g1           (ring), \g1
4563           (ring), \g{1}           (ring), \g{1}
4564    
4565         An  unsigned number specifies an absolute reference without the ambigu-         An unsigned number specifies an absolute reference without the  ambigu-
4566         ity that is present in the older syntax. It is also useful when literal         ity that is present in the older syntax. It is also useful when literal
4567         digits follow the reference. A negative number is a relative reference.         digits follow the reference. A negative number is a relative reference.
4568         Consider this example:         Consider this example:
# Line 4507  BACK REFERENCES Line 4570  BACK REFERENCES
4570           (abc(def)ghi)\g{-1}           (abc(def)ghi)\g{-1}
4571    
4572         The sequence \g{-1} is a reference to the most recently started captur-         The sequence \g{-1} is a reference to the most recently started captur-
4573         ing  subpattern  before \g, that is, is it equivalent to \2. Similarly,         ing subpattern before \g, that is, is it equivalent to  \2.  Similarly,
4574         \g{-2} would be equivalent to \1. The use of relative references can be         \g{-2} would be equivalent to \1. The use of relative references can be
4575         helpful  in  long  patterns,  and  also in patterns that are created by         helpful in long patterns, and also in  patterns  that  are  created  by
4576         joining together fragments that contain references within themselves.         joining together fragments that contain references within themselves.
4577    
4578         A back reference matches whatever actually matched the  capturing  sub-         A  back  reference matches whatever actually matched the capturing sub-
4579         pattern  in  the  current subject string, rather than anything matching         pattern in the current subject string, rather  than  anything  matching
4580         the subpattern itself (see "Subpatterns as subroutines" below for a way         the subpattern itself (see "Subpatterns as subroutines" below for a way
4581         of doing that). So the pattern         of doing that). So the pattern
4582    
4583           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
4584    
4585         matches  "sense and sensibility" and "response and responsibility", but         matches "sense and sensibility" and "response and responsibility",  but
4586         not "sense and responsibility". If caseful matching is in force at  the         not  "sense and responsibility". If caseful matching is in force at the
4587         time  of the back reference, the case of letters is relevant. For exam-         time of the back reference, the case of letters is relevant. For  exam-
4588         ple,         ple,
4589    
4590           ((?i)rah)\s+\1           ((?i)rah)\s+\1
4591    
4592         matches "rah rah" and "RAH RAH", but not "RAH  rah",  even  though  the         matches  "rah  rah"  and  "RAH RAH", but not "RAH rah", even though the
4593         original capturing subpattern is matched caselessly.         original capturing subpattern is matched caselessly.
4594    
4595         There  are  several  different ways of writing back references to named         There are several different ways of writing back  references  to  named
4596         subpatterns. The .NET syntax \k{name} and the Perl syntax  \k<name>  or         subpatterns.  The  .NET syntax \k{name} and the Perl syntax \k<name> or
4597         \k'name'  are supported, as is the Python syntax (?P=name). Perl 5.10's         \k'name' are supported, as is the Python syntax (?P=name). Perl  5.10's
4598         unified back reference syntax, in which \g can be used for both numeric         unified back reference syntax, in which \g can be used for both numeric
4599         and  named  references,  is  also supported. We could rewrite the above         and named references, is also supported. We  could  rewrite  the  above
4600         example in any of the following ways:         example in any of the following ways:
4601    
4602           (?<p1>(?i)rah)\s+\k<p1>           (?<p1>(?i)rah)\s+\k<p1>
# Line 4541  BACK REFERENCES Line 4604  BACK REFERENCES
4604           (?P<p1>(?i)rah)\s+(?P=p1)           (?P<p1>(?i)rah)\s+(?P=p1)
4605           (?<p1>(?i)rah)\s+\g{p1}           (?<p1>(?i)rah)\s+\g{p1}
4606    
4607         A subpattern that is referenced by  name  may  appear  in  the  pattern         A  subpattern  that  is  referenced  by  name may appear in the pattern
4608         before or after the reference.         before or after the reference.
4609    
4610         There  may be more than one back reference to the same subpattern. If a         There may be more than one back reference to the same subpattern. If  a
4611         subpattern has not actually been used in a particular match,  any  back         subpattern  has  not actually been used in a particular match, any back
4612         references to it always fail by default. For example, the pattern         references to it always fail by default. For example, the pattern
4613    
4614           (a|(bc))\2           (a|(bc))\2
4615    
4616         always  fails  if  it starts to match "a" rather than "bc". However, if         always fails if it starts to match "a" rather than  "bc".  However,  if
4617         the PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back refer-         the PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back refer-
4618         ence to an unset value matches an empty string.         ence to an unset value matches an empty string.
4619    
4620         Because  there may be many capturing parentheses in a pattern, all dig-         Because there may be many capturing parentheses in a pattern, all  dig-
4621         its following a backslash are taken as part of a potential back  refer-         its  following a backslash are taken as part of a potential back refer-
4622         ence  number.   If  the  pattern continues with a digit character, some         ence number.  If the pattern continues with  a  digit  character,  some
4623         delimiter must  be  used  to  terminate  the  back  reference.  If  the         delimiter  must  be  used  to  terminate  the  back  reference.  If the
4624         PCRE_EXTENDED option is set, this can be whitespace. Otherwise, the \g{         PCRE_EXTENDED option is set, this can be whitespace. Otherwise, the \g{
4625         syntax or an empty comment (see "Comments" below) can be used.         syntax or an empty comment (see "Comments" below) can be used.
4626    
4627     Recursive back references     Recursive back references
4628    
4629         A back reference that occurs inside the parentheses to which it  refers         A  back reference that occurs inside the parentheses to which it refers
4630         fails  when  the subpattern is first used, so, for example, (a\1) never         fails when the subpattern is first used, so, for example,  (a\1)  never
4631         matches.  However, such references can be useful inside  repeated  sub-         matches.   However,  such references can be useful inside repeated sub-
4632         patterns. For example, the pattern         patterns. For example, the pattern
4633    
4634           (a|b\1)+           (a|b\1)+
4635    
4636         matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-         matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-
4637         ation of the subpattern,  the  back  reference  matches  the  character         ation  of  the  subpattern,  the  back  reference matches the character
4638         string  corresponding  to  the previous iteration. In order for this to         string corresponding to the previous iteration. In order  for  this  to
4639         work, the pattern must be such that the first iteration does  not  need         work,  the  pattern must be such that the first iteration does not need
4640         to  match the back reference. This can be done using alternation, as in         to match the back reference. This can be done using alternation, as  in
4641         the example above, or by a quantifier with a minimum of zero.         the example above, or by a quantifier with a minimum of zero.
4642    
4643         Back references of this type cause the group that they reference to  be         Back  references of this type cause the group that they reference to be
4644         treated  as  an atomic group.  Once the whole group has been matched, a         treated as an atomic group.  Once the whole group has been  matched,  a
4645         subsequent matching failure cannot cause backtracking into  the  middle         subsequent  matching  failure cannot cause backtracking into the middle
4646         of the group.         of the group.
4647    
4648    
4649  ASSERTIONS  ASSERTIONS
4650    
4651         An  assertion  is  a  test on the characters following or preceding the         An assertion is a test on the characters  following  or  preceding  the
4652         current matching point that does not actually consume  any  characters.         current  matching  point that does not actually consume any characters.
4653         The  simple  assertions  coded  as  \b, \B, \A, \G, \Z, \z, ^ and $ are         The simple assertions coded as \b, \B, \A, \G, \Z,  \z,  ^  and  $  are
4654         described above.         described above.
4655    
4656         More complicated assertions are coded as  subpatterns.  There  are  two         More  complicated  assertions  are  coded as subpatterns. There are two
4657         kinds:  those  that  look  ahead of the current position in the subject         kinds: those that look ahead of the current  position  in  the  subject
4658         string, and those that look  behind  it.  An  assertion  subpattern  is         string,  and  those  that  look  behind  it. An assertion subpattern is
4659         matched  in  the  normal way, except that it does not cause the current         matched in the normal way, except that it does not  cause  the  current
4660         matching position to be changed.         matching position to be changed.
4661    
4662         Assertion subpatterns are not capturing subpatterns,  and  may  not  be         Assertion  subpatterns  are  not  capturing subpatterns, and may not be
4663         repeated,  because  it  makes no sense to assert the same thing several         repeated, because it makes no sense to assert the  same  thing  several
4664         times. If any kind of assertion contains capturing  subpatterns  within         times.  If  any kind of assertion contains capturing subpatterns within
4665         it,  these are counted for the purposes of numbering the capturing sub-         it, these are counted for the purposes of numbering the capturing  sub-
4666         patterns in the whole pattern.  However, substring capturing is carried         patterns in the whole pattern.  However, substring capturing is carried
4667         out  only  for  positive assertions, because it does not make sense for         out only for positive assertions, because it does not  make  sense  for
4668         negative assertions.         negative assertions.
4669    
4670     Lookahead assertions     Lookahead assertions
# Line 4611  ASSERTIONS Line 4674  ASSERTIONS
4674    
4675           \w+(?=;)           \w+(?=;)
4676    
4677         matches  a word followed by a semicolon, but does not include the semi-         matches a word followed by a semicolon, but does not include the  semi-
4678         colon in the match, and         colon in the match, and
4679    
4680           foo(?!bar)           foo(?!bar)
4681    
4682         matches any occurrence of "foo" that is not  followed  by  "bar".  Note         matches  any  occurrence  of  "foo" that is not followed by "bar". Note
4683         that the apparently similar pattern         that the apparently similar pattern
4684    
4685           (?!foo)bar           (?!foo)bar
4686    
4687         does  not  find  an  occurrence  of "bar" that is preceded by something         does not find an occurrence of "bar"  that  is  preceded  by  something
4688         other than "foo"; it finds any occurrence of "bar" whatsoever,  because         other  than "foo"; it finds any occurrence of "bar" whatsoever, because
4689         the assertion (?!foo) is always true when the next three characters are         the assertion (?!foo) is always true when the next three characters are
4690         "bar". A lookbehind assertion is needed to achieve the other effect.         "bar". A lookbehind assertion is needed to achieve the other effect.
4691    
4692         If you want to force a matching failure at some point in a pattern, the         If you want to force a matching failure at some point in a pattern, the
4693         most  convenient  way  to  do  it  is with (?!) because an empty string         most convenient way to do it is  with  (?!)  because  an  empty  string
4694         always matches, so an assertion that requires there not to be an  empty         always  matches, so an assertion that requires there not to be an empty
4695         string  must  always  fail.   The  Perl  5.10 backtracking control verb         string must always fail.   The  Perl  5.10  backtracking  control  verb
4696         (*FAIL) or (*F) is essentially a synonym for (?!).         (*FAIL) or (*F) is essentially a synonym for (?!).
4697    
4698     Lookbehind assertions     Lookbehind assertions
4699    
4700         Lookbehind assertions start with (?<= for positive assertions and  (?<!         Lookbehind  assertions start with (?<= for positive assertions and (?<!
4701         for negative assertions. For example,         for negative assertions. For example,
4702    
4703           (?<!foo)bar           (?<!foo)bar
4704    
4705         does  find  an  occurrence  of "bar" that is not preceded by "foo". The         does find an occurrence of "bar" that is not  preceded  by  "foo".  The
4706         contents of a lookbehind assertion are restricted  such  that  all  the         contents  of  a  lookbehind  assertion are restricted such that all the
4707         strings it matches must have a fixed length. However, if there are sev-         strings it matches must have a fixed length. However, if there are sev-
4708         eral top-level alternatives, they do not all  have  to  have  the  same         eral  top-level  alternatives,  they  do  not all have to have the same
4709         fixed length. Thus         fixed length. Thus
4710    
4711           (?<=bullock|donkey)           (?<=bullock|donkey)
# Line 4651  ASSERTIONS Line 4714  ASSERTIONS
4714    
4715           (?<!dogs?|cats?)           (?<!dogs?|cats?)
4716    
4717         causes  an  error at compile time. Branches that match different length         causes an error at compile time. Branches that match  different  length
4718         strings are permitted only at the top level of a lookbehind  assertion.         strings  are permitted only at the top level of a lookbehind assertion.
4719         This  is an extension compared with Perl (5.8 and 5.10), which requires         This is an extension compared with Perl (5.8 and 5.10), which  requires
4720         all branches to match the same length of string. An assertion such as         all branches to match the same length of string. An assertion such as
4721    
4722           (?<=ab(c|de))           (?<=ab(c|de))
4723    
4724         is not permitted, because its single top-level  branch  can  match  two         is  not  permitted,  because  its single top-level branch can match two
4725         different lengths, but it is acceptable to PCRE if rewritten to use two         different lengths, but it is acceptable to PCRE if rewritten to use two
4726         top-level branches:         top-level branches:
4727    
4728           (?<=abc|abde)           (?<=abc|abde)
4729    
4730         In some cases, the Perl 5.10 escape sequence \K (see above) can be used         In some cases, the Perl 5.10 escape sequence \K (see above) can be used
4731         instead  of  a  lookbehind  assertion  to  get  round  the fixed-length         instead of  a  lookbehind  assertion  to  get  round  the  fixed-length
4732         restriction.         restriction.
4733    
4734         The implementation of lookbehind assertions is, for  each  alternative,         The  implementation  of lookbehind assertions is, for each alternative,
4735         to  temporarily  move the current position back by the fixed length and         to temporarily move the current position back by the fixed  length  and
4736         then try to match. If there are insufficient characters before the cur-         then try to match. If there are insufficient characters before the cur-
4737         rent position, the assertion fails.         rent position, the assertion fails.
4738    
4739         PCRE does not allow the \C escape (which matches a single byte in UTF-8         PCRE does not allow the \C escape (which matches a single byte in UTF-8
4740         mode) to appear in lookbehind assertions, because it makes it  impossi-         mode)  to appear in lookbehind assertions, because it makes it impossi-
4741         ble  to  calculate the length of the lookbehind. The \X and \R escapes,         ble to calculate the length of the lookbehind. The \X and  \R  escapes,
4742         which can match different numbers of bytes, are also not permitted.         which can match different numbers of bytes, are also not permitted.
4743    
4744         "Subroutine" calls (see below) such as (?2) or (?&X) are  permitted  in         "Subroutine"  calls  (see below) such as (?2) or (?&X) are permitted in
4745         lookbehinds,  as  long as the subpattern matches a fixed-length string.         lookbehinds, as long as the subpattern matches a  fixed-length  string.
4746         Recursion, however, is not supported.         Recursion, however, is not supported.
4747    
4748         Possessive quantifiers can  be  used  in  conjunction  with  lookbehind         Possessive  quantifiers  can  be  used  in  conjunction with lookbehind
4749         assertions to specify efficient matching of fixed-length strings at the         assertions to specify efficient matching of fixed-length strings at the
4750         end of subject strings. Consider a simple pattern such as         end of subject strings. Consider a simple pattern such as
4751    
4752           abcd$           abcd$
4753    
4754         when applied to a long string that does  not  match.  Because  matching         when  applied  to  a  long string that does not match. Because matching
4755         proceeds from left to right, PCRE will look for each "a" in the subject         proceeds from left to right, PCRE will look for each "a" in the subject
4756         and then see if what follows matches the rest of the  pattern.  If  the         and  then  see  if what follows matches the rest of the pattern. If the
4757         pattern is specified as         pattern is specified as
4758    
4759           ^.*abcd$           ^.*abcd$
4760    
4761         the  initial .* matches the entire string at first, but when this fails         the initial .* matches the entire string at first, but when this  fails
4762         (because there is no following "a"), it backtracks to match all but the         (because there is no following "a"), it backtracks to match all but the
4763         last  character,  then all but the last two characters, and so on. Once         last character, then all but the last two characters, and so  on.  Once
4764         again the search for "a" covers the entire string, from right to  left,         again  the search for "a" covers the entire string, from right to left,
4765         so we are no better off. However, if the pattern is written as         so we are no better off. However, if the pattern is written as
4766    
4767           ^.*+(?<=abcd)           ^.*+(?<=abcd)
4768    
4769         there  can  be  no backtracking for the .*+ item; it can match only the         there can be no backtracking for the .*+ item; it can  match  only  the
4770         entire string. The subsequent lookbehind assertion does a  single  test         entire  string.  The subsequent lookbehind assertion does a single test
4771         on  the last four characters. If it fails, the match fails immediately.         on the last four characters. If it fails, the match fails  immediately.
4772         For long strings, this approach makes a significant difference  to  the         For  long  strings, this approach makes a significant difference to the
4773         processing time.         processing time.
4774    
4775     Using multiple assertions     Using multiple assertions
# Line 4715  ASSERTIONS Line 4778  ASSERTIONS
4778    
4779           (?<=\d{3})(?<!999)foo           (?<=\d{3})(?<!999)foo
4780    
4781         matches  "foo" preceded by three digits that are not "999". Notice that         matches "foo" preceded by three digits that are not "999". Notice  that
4782         each of the assertions is applied independently at the  same  point  in         each  of  the  assertions is applied independently at the same point in
4783         the  subject  string.  First  there  is a check that the previous three         the subject string. First there is a  check  that  the  previous  three
4784         characters are all digits, and then there is  a  check  that  the  same         characters  are  all  digits,  and  then there is a check that the same
4785         three characters are not "999".  This pattern does not match "foo" pre-         three characters are not "999".  This pattern does not match "foo" pre-
4786         ceded by six characters, the first of which are  digits  and  the  last         ceded  by  six  characters,  the first of which are digits and the last
4787         three  of  which  are not "999". For example, it doesn't match "123abc-         three of which are not "999". For example, it  doesn't  match  "123abc-
4788         foo". A pattern to do that is         foo". A pattern to do that is
4789    
4790           (?<=\d{3}...)(?<!999)foo           (?<=\d{3}...)(?<!999)foo
4791    
4792         This time the first assertion looks at the  preceding  six  characters,         This  time  the  first assertion looks at the preceding six characters,
4793         checking that the first three are digits, and then the second assertion         checking that the first three are digits, and then the second assertion
4794         checks that the preceding three characters are not "999".         checks that the preceding three characters are not "999".
4795    
# Line 4734  ASSERTIONS Line 4797  ASSERTIONS
4797    
4798           (?<=(?<!foo)bar)baz           (?<=(?<!foo)bar)baz
4799    
4800         matches an occurrence of "baz" that is preceded by "bar" which in  turn         matches  an occurrence of "baz" that is preceded by "bar" which in turn
4801         is not preceded by "foo", while         is not preceded by "foo", while
4802    
4803           (?<=\d{3}(?!999)...)foo           (?<=\d{3}(?!999)...)foo
4804    
4805         is  another pattern that matches "foo" preceded by three digits and any         is another pattern that matches "foo" preceded by three digits and  any
4806         three characters that are not "999".         three characters that are not "999".
4807    
4808    
4809  CONDITIONAL SUBPATTERNS  CONDITIONAL SUBPATTERNS
4810    
4811         It is possible to cause the matching process to obey a subpattern  con-         It  is possible to cause the matching process to obey a subpattern con-
4812         ditionally  or to choose between two alternative subpatterns, depending         ditionally or to choose between two alternative subpatterns,  depending
4813         on the result of an assertion, or whether a specific capturing  subpat-         on  the result of an assertion, or whether a specific capturing subpat-
4814         tern  has  already  been matched. The two possible forms of conditional         tern has already been matched. The two possible  forms  of  conditional
4815         subpattern are:         subpattern are:
4816    
4817           (?(condition)yes-pattern)           (?(condition)yes-pattern)
4818           (?(condition)yes-pattern|no-pattern)           (?(condition)yes-pattern|no-pattern)
4819    
4820         If the condition is satisfied, the yes-pattern is used;  otherwise  the         If  the  condition is satisfied, the yes-pattern is used; otherwise the
4821         no-pattern  (if  present)  is used. If there are more than two alterna-         no-pattern (if present) is used. If there are more  than  two  alterna-
4822         tives in the subpattern, a compile-time error occurs.         tives in the subpattern, a compile-time error occurs.
4823    
4824         There are four kinds of condition: references  to  subpatterns,  refer-         There  are  four  kinds of condition: references to subpatterns, refer-
4825         ences to recursion, a pseudo-condition called DEFINE, and assertions.         ences to recursion, a pseudo-condition called DEFINE, and assertions.
4826    
4827     Checking for a used subpattern by number     Checking for a used subpattern by number
4828    
4829         If  the  text between the parentheses consists of a sequence of digits,         If the text between the parentheses consists of a sequence  of  digits,
4830         the condition is true if a capturing subpattern of that number has pre-         the condition is true if a capturing subpattern of that number has pre-
4831         viously  matched.  If  there is more than one capturing subpattern with         viously matched. If there is more than one  capturing  subpattern  with
4832         the same number (see the earlier  section  about  duplicate  subpattern         the  same  number  (see  the earlier section about duplicate subpattern
4833         numbers), the condition is true if any of them have been set. An alter-         numbers), the condition is true if any of them have been set. An alter-
4834         native notation is to precede the digits with a plus or minus sign.  In         native  notation is to precede the digits with a plus or minus sign. In
4835         this  case, the subpattern number is relative rather than absolute. The         this case, the subpattern number is relative rather than absolute.  The
4836         most recently opened parentheses can be referenced by (?(-1), the  next         most  recently opened parentheses can be referenced by (?(-1), the next
4837         most  recent  by  (?(-2),  and so on. In looping constructs it can also         most recent by (?(-2), and so on. In looping  constructs  it  can  also
4838         make sense to refer  to  subsequent  groups  with  constructs  such  as         make  sense  to  refer  to  subsequent  groups  with constructs such as
4839         (?(+2).         (?(+2).
4840    
4841         Consider  the  following  pattern, which contains non-significant white         Consider the following pattern, which  contains  non-significant  white
4842         space to make it more readable (assume the PCRE_EXTENDED option) and to         space to make it more readable (assume the PCRE_EXTENDED option) and to
4843         divide it into three parts for ease of discussion:         divide it into three parts for ease of discussion:
4844    
4845           ( \( )?    [^()]+    (?(1) \) )           ( \( )?    [^()]+    (?(1) \) )
4846    
4847         The  first  part  matches  an optional opening parenthesis, and if that         The first part matches an optional opening  parenthesis,  and  if  that
4848         character is present, sets it as the first captured substring. The sec-         character is present, sets it as the first captured substring. The sec-
4849         ond  part  matches one or more characters that are not parentheses. The         ond part matches one or more characters that are not  parentheses.  The
4850         third part is a conditional subpattern that tests whether the first set         third part is a conditional subpattern that tests whether the first set
4851         of parentheses matched or not. If they did, that is, if subject started         of parentheses matched or not. If they did, that is, if subject started
4852         with an opening parenthesis, the condition is true, and so the yes-pat-         with an opening parenthesis, the condition is true, and so the yes-pat-
4853         tern  is  executed  and  a  closing parenthesis is required. Otherwise,         tern is executed and a  closing  parenthesis  is  required.  Otherwise,
4854         since no-pattern is not present, the  subpattern  matches  nothing.  In         since  no-pattern  is  not  present, the subpattern matches nothing. In
4855         other  words,  this  pattern  matches  a  sequence  of non-parentheses,         other words,  this  pattern  matches  a  sequence  of  non-parentheses,
4856         optionally enclosed in parentheses.         optionally enclosed in parentheses.
4857    
4858         If you were embedding this pattern in a larger one,  you  could  use  a         If  you  were  embedding  this pattern in a larger one, you could use a
4859         relative reference:         relative reference:
4860    
4861           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...
4862    
4863         This  makes  the  fragment independent of the parentheses in the larger         This makes the fragment independent of the parentheses  in  the  larger
4864         pattern.         pattern.
4865    
4866     Checking for a used subpattern by name     Checking for a used subpattern by name
4867    
4868         Perl uses the syntax (?(<name>)...) or (?('name')...)  to  test  for  a         Perl  uses  the  syntax  (?(<name>)...) or (?('name')...) to test for a
4869         used  subpattern  by  name.  For compatibility with earlier versions of         used subpattern by name. For compatibility  with  earlier  versions  of
4870         PCRE, which had this facility before Perl, the syntax  (?(name)...)  is         PCRE,  which  had this facility before Perl, the syntax (?(name)...) is
4871         also  recognized. However, there is a possible ambiguity with this syn-         also recognized. However, there is a possible ambiguity with this  syn-
4872         tax, because subpattern names may  consist  entirely  of  digits.  PCRE         tax,  because  subpattern  names  may  consist entirely of digits. PCRE
4873         looks  first for a named subpattern; if it cannot find one and the name         looks first for a named subpattern; if it cannot find one and the  name
4874         consists entirely of digits, PCRE looks for a subpattern of  that  num-         consists  entirely  of digits, PCRE looks for a subpattern of that num-
4875         ber,  which must be greater than zero. Using subpattern names that con-         ber, which must be greater than zero. Using subpattern names that  con-
4876         sist entirely of digits is not recommended.         sist entirely of digits is not recommended.
4877    
4878         Rewriting the above example to use a named subpattern gives this:         Rewriting the above example to use a named subpattern gives this:
4879    
4880           (?<OPEN> \( )?    [^()]+    (?(<OPEN>) \) )           (?<OPEN> \( )?    [^()]+    (?(<OPEN>) \) )
4881    
4882         If the name used in a condition of this kind is a duplicate,  the  test         If  the  name used in a condition of this kind is a duplicate, the test
4883         is  applied to all subpatterns of the same name, and is true if any one         is applied to all subpatterns of the same name, and is true if any  one
4884         of them has matched.         of them has matched.
4885    
4886     Checking for pattern recursion     Checking for pattern recursion
4887    
4888         If the condition is the string (R), and there is no subpattern with the         If the condition is the string (R), and there is no subpattern with the
4889         name  R, the condition is true if a recursive call to the whole pattern         name R, the condition is true if a recursive call to the whole  pattern
4890         or any subpattern has been made. If digits or a name preceded by amper-         or any subpattern has been made. If digits or a name preceded by amper-
4891         sand follow the letter R, for example:         sand follow the letter R, for example:
4892    
# Line 4831  CONDITIONAL SUBPATTERNS Line 4894  CONDITIONAL SUBPATTERNS
4894    
4895         the condition is true if the most recent recursion is into a subpattern         the condition is true if the most recent recursion is into a subpattern
4896         whose number or name is given. This condition does not check the entire         whose number or name is given. This condition does not check the entire
4897         recursion  stack.  If  the  name  used in a condition of this kind is a         recursion stack. If the name used in a condition  of  this  kind  is  a
4898         duplicate, the test is applied to all subpatterns of the same name, and         duplicate, the test is applied to all subpatterns of the same name, and
4899         is true if any one of them is the most recent recursion.         is true if any one of them is the most recent recursion.
4900    
4901         At  "top  level",  all  these recursion test conditions are false.  The         At "top level", all these recursion test  conditions  are  false.   The
4902         syntax for recursive patterns is described below.         syntax for recursive patterns is described below.
4903    
4904     Defining subpatterns for use by reference only     Defining subpatterns for use by reference only
4905    
4906         If the condition is the string (DEFINE), and  there  is  no  subpattern         If  the  condition  is  the string (DEFINE), and there is no subpattern
4907         with  the  name  DEFINE,  the  condition is always false. In this case,         with the name DEFINE, the condition is  always  false.  In  this  case,
4908         there may be only one alternative  in  the  subpattern.  It  is  always         there  may  be  only  one  alternative  in the subpattern. It is always
4909         skipped  if  control  reaches  this  point  in the pattern; the idea of         skipped if control reaches this point  in  the  pattern;  the  idea  of
4910         DEFINE is that it can be used to define "subroutines" that can be  ref-         DEFINE  is that it can be used to define "subroutines" that can be ref-
4911         erenced  from elsewhere. (The use of "subroutines" is described below.)         erenced from elsewhere. (The use of "subroutines" is described  below.)
4912         For example, a pattern to match an IPv4 address could be  written  like         For  example,  a pattern to match an IPv4 address could be written like
4913         this (ignore whitespace and line breaks):         this (ignore whitespace and line breaks):
4914    
4915           (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )           (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
4916           \b (?&byte) (\.(?&byte)){3} \b           \b (?&byte) (\.(?&byte)){3} \b
4917    
4918         The  first part of the pattern is a DEFINE group inside which a another         The first part of the pattern is a DEFINE group inside which a  another
4919         group named "byte" is defined. This matches an individual component  of         group  named "byte" is defined. This matches an individual component of
4920         an  IPv4  address  (a number less than 256). When matching takes place,         an IPv4 address (a number less than 256). When  matching  takes  place,
4921         this part of the pattern is skipped because DEFINE acts  like  a  false         this  part  of  the pattern is skipped because DEFINE acts like a false
4922         condition.  The  rest of the pattern uses references to the named group         condition. The rest of the pattern uses references to the  named  group
4923         to match the four dot-separated components of an IPv4 address,  insist-         to  match the four dot-separated components of an IPv4 address, insist-
4924         ing on a word boundary at each end.         ing on a word boundary at each end.
4925    
4926     Assertion conditions     Assertion conditions
4927    
4928         If  the  condition  is  not  in any of the above formats, it must be an         If the condition is not in any of the above  formats,  it  must  be  an
4929         assertion.  This may be a positive or negative lookahead or  lookbehind         assertion.   This may be a positive or negative lookahead or lookbehind
4930         assertion.  Consider  this  pattern,  again  containing non-significant         assertion. Consider  this  pattern,  again  containing  non-significant
4931         white space, and with the two alternatives on the second line:         white space, and with the two alternatives on the second line:
4932    
4933           (?(?=[^a-z]*[a-z])           (?(?=[^a-z]*[a-z])
4934           \d{2}-[a-z]{3}-\d{2}  |  \d{2}-\d{2}-\d{2} )           \d{2}-[a-z]{3}-\d{2}  |  \d{2}-\d{2}-\d{2} )
4935    
4936         The condition  is  a  positive  lookahead  assertion  that  matches  an         The  condition  is  a  positive  lookahead  assertion  that  matches an
4937         optional  sequence of non-letters followed by a letter. In other words,         optional sequence of non-letters followed by a letter. In other  words,
4938         it tests for the presence of at least one letter in the subject.  If  a         it  tests  for the presence of at least one letter in the subject. If a
4939         letter  is found, the subject is matched against the first alternative;         letter is found, the subject is matched against the first  alternative;
4940         otherwise it is  matched  against  the  second.  This  pattern  matches         otherwise  it  is  matched  against  the  second.  This pattern matches
4941         strings  in  one  of the two forms dd-aaa-dd or dd-dd-dd, where aaa are         strings in one of the two forms dd-aaa-dd or dd-dd-dd,  where  aaa  are
4942         letters and dd are digits.         letters and dd are digits.
4943    
4944    
4945  COMMENTS  COMMENTS
4946    
4947         The sequence (?# marks the start of a comment that continues up to  the         The  sequence (?# marks the start of a comment that continues up to the
4948         next  closing  parenthesis.  Nested  parentheses are not permitted. The         next closing parenthesis. Nested parentheses  are  not  permitted.  The
4949         characters that make up a comment play no part in the pattern  matching         characters  that make up a comment play no part in the pattern matching
4950         at all.         at all.
4951    
4952         If  the PCRE_EXTENDED option is set, an unescaped # character outside a         If the PCRE_EXTENDED option is set, an unescaped # character outside  a
4953         character class introduces a  comment  that  continues  to  immediately         character  class  introduces  a  comment  that continues to immediately
4954         after the next newline in the pattern.         after the next newline in the pattern.
4955    
4956    
4957  RECURSIVE PATTERNS  RECURSIVE PATTERNS
4958    
4959         Consider  the problem of matching a string in parentheses, allowing for         Consider the problem of matching a string in parentheses, allowing  for
4960         unlimited nested parentheses. Without the use of  recursion,  the  best         unlimited  nested  parentheses.  Without the use of recursion, the best
4961         that  can  be  done  is  to use a pattern that matches up to some fixed         that can be done is to use a pattern that  matches  up  to  some  fixed
4962         depth of nesting. It is not possible to  handle  an  arbitrary  nesting         depth  of  nesting.  It  is not possible to handle an arbitrary nesting
4963         depth.         depth.
4964    
4965         For some time, Perl has provided a facility that allows regular expres-         For some time, Perl has provided a facility that allows regular expres-
4966         sions to recurse (amongst other things). It does this by  interpolating         sions  to recurse (amongst other things). It does this by interpolating
4967         Perl  code in the expression at run time, and the code can refer to the         Perl code in the expression at run time, and the code can refer to  the
4968         expression itself. A Perl pattern using code interpolation to solve the         expression itself. A Perl pattern using code interpolation to solve the
4969         parentheses problem can be created like this:         parentheses problem can be created like this:
4970    
# Line 4911  RECURSIVE PATTERNS Line 4974  RECURSIVE PATTERNS
4974         refers recursively to the pattern in which it appears.         refers recursively to the pattern in which it appears.
4975    
4976         Obviously, PCRE cannot support the interpolation of Perl code. Instead,         Obviously, PCRE cannot support the interpolation of Perl code. Instead,
4977         it  supports  special  syntax  for recursion of the entire pattern, and         it supports special syntax for recursion of  the  entire  pattern,  and
4978         also for individual subpattern recursion.  After  its  introduction  in         also  for  individual  subpattern  recursion. After its introduction in
4979         PCRE  and  Python,  this  kind of recursion was subsequently introduced         PCRE and Python, this kind of  recursion  was  subsequently  introduced
4980         into Perl at release 5.10.         into Perl at release 5.10.
4981    
4982         A special item that consists of (? followed by a  number  greater  than         A  special  item  that consists of (? followed by a number greater than
4983         zero and a closing parenthesis is a recursive call of the subpattern of         zero and a closing parenthesis is a recursive call of the subpattern of
4984         the given number, provided that it occurs inside that  subpattern.  (If         the  given  number, provided that it occurs inside that subpattern. (If
4985         not,  it  is  a  "subroutine" call, which is described in the next sec-         not, it is a "subroutine" call, which is described  in  the  next  sec-
4986         tion.) The special item (?R) or (?0) is a recursive call of the  entire         tion.)  The special item (?R) or (?0) is a recursive call of the entire
4987         regular expression.         regular expression.
4988    
4989         This  PCRE  pattern  solves  the nested parentheses problem (assume the         This PCRE pattern solves the nested  parentheses  problem  (assume  the
4990         PCRE_EXTENDED option is set so that white space is ignored):         PCRE_EXTENDED option is set so that white space is ignored):
4991    
4992           \( ( [^()]++ | (?R) )* \)           \( ( [^()]++ | (?R) )* \)
4993    
4994         First it matches an opening parenthesis. Then it matches any number  of         First  it matches an opening parenthesis. Then it matches any number of
4995         substrings  which  can  either  be  a sequence of non-parentheses, or a         substrings which can either be a  sequence  of  non-parentheses,  or  a
4996         recursive match of the pattern itself (that is, a  correctly  parenthe-         recursive  match  of the pattern itself (that is, a correctly parenthe-
4997         sized substring).  Finally there is a closing parenthesis. Note the use         sized substring).  Finally there is a closing parenthesis. Note the use
4998         of a possessive quantifier to avoid backtracking into sequences of non-         of a possessive quantifier to avoid backtracking into sequences of non-
4999         parentheses.         parentheses.
5000    
5001         If  this  were  part of a larger pattern, you would not want to recurse         If this were part of a larger pattern, you would not  want  to  recurse
5002         the entire pattern, so instead you could use this:         the entire pattern, so instead you could use this:
5003    
5004           ( \( ( [^()]++ | (?1) )* \) )           ( \( ( [^()]++ | (?1) )* \) )
5005    
5006         We have put the pattern into parentheses, and caused the  recursion  to         We  have  put the pattern into parentheses, and caused the recursion to
5007         refer to them instead of the whole pattern.         refer to them instead of the whole pattern.
5008    
5009         In  a  larger  pattern,  keeping  track  of  parenthesis numbers can be         In a larger pattern,  keeping  track  of  parenthesis  numbers  can  be
5010         tricky. This is made easier by the use of relative references  (a  Perl         tricky.  This  is made easier by the use of relative references (a Perl
5011         5.10  feature).   Instead  of  (?1)  in the pattern above you can write         5.10 feature).  Instead of (?1) in the  pattern  above  you  can  write
5012         (?-2) to refer to the second most recently opened parentheses preceding         (?-2) to refer to the second most recently opened parentheses preceding
5013         the  recursion.  In  other  words,  a  negative number counts capturing         the recursion. In other  words,  a  negative  number  counts  capturing
5014         parentheses leftwards from the point at which it is encountered.         parentheses leftwards from the point at which it is encountered.
5015    
5016         It is also possible to refer to  subsequently  opened  parentheses,  by         It  is  also  possible  to refer to subsequently opened parentheses, by
5017         writing  references  such  as (?+2). However, these cannot be recursive         writing references such as (?+2). However, these  cannot  be  recursive
5018         because the reference is not inside the  parentheses  that  are  refer-         because  the  reference  is  not inside the parentheses that are refer-
5019         enced.  They  are  always  "subroutine" calls, as described in the next         enced. They are always "subroutine" calls, as  described  in  the  next
5020         section.         section.
5021    
5022         An alternative approach is to use named parentheses instead.  The  Perl         An  alternative  approach is to use named parentheses instead. The Perl
5023         syntax  for  this  is (?&name); PCRE's earlier syntax (?P>name) is also         syntax for this is (?&name); PCRE's earlier syntax  (?P>name)  is  also
5024         supported. We could rewrite the above example as follows:         supported. We could rewrite the above example as follows:
5025    
5026           (?<pn> \( ( [^()]++ | (?&pn) )* \) )           (?<pn> \( ( [^()]++ | (?&pn) )* \) )
5027    
5028         If there is more than one subpattern with the same name,  the  earliest         If  there  is more than one subpattern with the same name, the earliest
5029         one is used.         one is used.
5030    
5031         This  particular  example pattern that we have been looking at contains         This particular example pattern that we have been looking  at  contains
5032         nested unlimited repeats, and so the use of a possessive quantifier for         nested unlimited repeats, and so the use of a possessive quantifier for
5033         matching strings of non-parentheses is important when applying the pat-         matching strings of non-parentheses is important when applying the pat-
5034         tern to strings that do not match. For example, when  this  pattern  is         tern  to  strings  that do not match. For example, when this pattern is
5035         applied to         applied to
5036    
5037           (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()           (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
5038    
5039         it  yields  "no  match" quickly. However, if a possessive quantifier is         it yields "no match" quickly. However, if a  possessive  quantifier  is
5040         not used, the match runs for a very long time indeed because there  are         not  used, the match runs for a very long time indeed because there are
5041         so  many  different  ways the + and * repeats can carve up the subject,         so many different ways the + and * repeats can carve  up  the  subject,
5042         and all have to be tested before failure can be reported.         and all have to be tested before failure can be reported.
5043    
5044         At the end of a match, the values of capturing  parentheses  are  those         At  the  end  of a match, the values of capturing parentheses are those
5045         from  the outermost level. If you want to obtain intermediate values, a         from the outermost level. If you want to obtain intermediate values,  a
5046         callout function can be used (see below and the pcrecallout  documenta-         callout  function can be used (see below and the pcrecallout documenta-
5047         tion). If the pattern above is matched against         tion). If the pattern above is matched against
5048    
5049           (ab(cd)ef)           (ab(cd)ef)
5050    
5051         the  value  for  the  inner capturing parentheses (numbered 2) is "ef",         the value for the inner capturing parentheses  (numbered  2)  is  "ef",
5052         which is the last value taken on at the top level. If a capturing  sub-         which  is the last value taken on at the top level. If a capturing sub-
5053         pattern is not matched at the top level, its final value is unset, even         pattern is not matched at the top level, its final value is unset, even
5054         if it is (temporarily) set at a deeper level.         if it is (temporarily) set at a deeper level.
5055    
5056         If there are more than 15 capturing parentheses in a pattern, PCRE  has         If  there are more than 15 capturing parentheses in a pattern, PCRE has
5057         to  obtain extra memory to store data during a recursion, which it does         to obtain extra memory to store data during a recursion, which it  does
5058         by using pcre_malloc, freeing it via pcre_free afterwards. If no memory         by using pcre_malloc, freeing it via pcre_free afterwards. If no memory
5059         can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.         can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.
5060    
5061         Do  not  confuse  the (?R) item with the condition (R), which tests for         Do not confuse the (?R) item with the condition (R),  which  tests  for
5062         recursion.  Consider this pattern, which matches text in  angle  brack-         recursion.   Consider  this pattern, which matches text in angle brack-
5063         ets,  allowing for arbitrary nesting. Only digits are allowed in nested         ets, allowing for arbitrary nesting. Only digits are allowed in  nested
5064         brackets (that is, when recursing), whereas any characters are  permit-         brackets  (that is, when recursing), whereas any characters are permit-
5065         ted at the outer level.         ted at the outer level.
5066    
5067           < (?: (?(R) \d++  | [^<>]*+) | (?R)) * >           < (?: (?(R) \d++  | [^<>]*+) | (?R)) * >
5068    
5069         In  this  pattern, (?(R) is the start of a conditional subpattern, with         In this pattern, (?(R) is the start of a conditional  subpattern,  with
5070         two different alternatives for the recursive and  non-recursive  cases.         two  different  alternatives for the recursive and non-recursive cases.
5071         The (?R) item is the actual recursive call.         The (?R) item is the actual recursive call.
5072    
5073     Recursion difference from Perl     Recursion difference from Perl
5074    
5075         In  PCRE (like Python, but unlike Perl), a recursive subpattern call is         In PCRE (like Python, but unlike Perl), a recursive subpattern call  is
5076         always treated as an atomic group. That is, once it has matched some of         always treated as an atomic group. That is, once it has matched some of
5077         the subject string, it is never re-entered, even if it contains untried         the subject string, it is never re-entered, even if it contains untried
5078         alternatives and there is a subsequent matching failure.  This  can  be         alternatives  and  there  is a subsequent matching failure. This can be
5079         illustrated  by the following pattern, which purports to match a palin-         illustrated by the following pattern, which purports to match a  palin-
5080         dromic string that contains an odd number of characters  (for  example,         dromic  string  that contains an odd number of characters (for example,
5081         "a", "aba", "abcba", "abcdcba"):         "a", "aba", "abcba", "abcdcba"):
5082    
5083           ^(.|(.)(?1)\2)$           ^(.|(.)(?1)\2)$
5084    
5085         The idea is that it either matches a single character, or two identical         The idea is that it either matches a single character, or two identical
5086         characters surrounding a sub-palindrome. In Perl, this  pattern  works;         characters  surrounding  a sub-palindrome. In Perl, this pattern works;
5087         in  PCRE  it  does  not if the pattern is longer than three characters.         in PCRE it does not if the pattern is  longer  than  three  characters.
5088         Consider the subject string "abcba":         Consider the subject string "abcba":
5089    
5090         At the top level, the first character is matched, but as it is  not  at         At  the  top level, the first character is matched, but as it is not at
5091         the end of the string, the first alternative fails; the second alterna-         the end of the string, the first alternative fails; the second alterna-
5092         tive is taken and the recursion kicks in. The recursive call to subpat-         tive is taken and the recursion kicks in. The recursive call to subpat-
5093         tern  1  successfully  matches the next character ("b"). (Note that the         tern 1 successfully matches the next character ("b").  (Note  that  the
5094         beginning and end of line tests are not part of the recursion).         beginning and end of line tests are not part of the recursion).
5095    
5096         Back at the top level, the next character ("c") is compared  with  what         Back  at  the top level, the next character ("c") is compared with what
5097         subpattern  2 matched, which was "a". This fails. Because the recursion         subpattern 2 matched, which was "a". This fails. Because the  recursion
5098         is treated as an atomic group, there are now  no  backtracking  points,         is  treated  as  an atomic group, there are now no backtracking points,
5099         and  so  the  entire  match fails. (Perl is able, at this point, to re-         and so the entire match fails. (Perl is able, at  this  point,  to  re-
5100         enter the recursion and try the second alternative.)  However,  if  the         enter  the  recursion  and try the second alternative.) However, if the
5101         pattern is written with the alternatives in the other order, things are         pattern is written with the alternatives in the other order, things are
5102         different:         different:
5103    
5104           ^((.)(?1)\2|.)$           ^((.)(?1)\2|.)$
5105    
5106         This time, the recursing alternative is tried first, and  continues  to         This  time,  the recursing alternative is tried first, and continues to
5107         recurse  until  it runs out of characters, at which point the recursion         recurse until it runs out of characters, at which point  the  recursion
5108         fails. But this time we do have  another  alternative  to  try  at  the         fails.  But  this  time  we  do  have another alternative to try at the
5109         higher  level.  That  is  the  big difference: in the previous case the         higher level. That is the big difference:  in  the  previous  case  the
5110         remaining alternative is at a deeper recursion level, which PCRE cannot         remaining alternative is at a deeper recursion level, which PCRE cannot
5111         use.         use.
5112    
5113         To change the pattern so that matches all palindromic strings, not just         To change the pattern so that matches all palindromic strings, not just
5114         those with an odd number of characters, it is tempting  to  change  the         those  with  an  odd number of characters, it is tempting to change the
5115         pattern to this:         pattern to this:
5116    
5117           ^((.)(?1)\2|.?)$           ^((.)(?1)\2|.?)$
5118    
5119         Again,  this  works  in Perl, but not in PCRE, and for the same reason.         Again, this works in Perl, but not in PCRE, and for  the  same  reason.
5120         When a deeper recursion has matched a single character,  it  cannot  be         When  a  deeper  recursion has matched a single character, it cannot be
5121         entered  again  in  order  to match an empty string. The solution is to         entered again in order to match an empty string.  The  solution  is  to
5122         separate the two cases, and write out the odd and even cases as  alter-         separate  the two cases, and write out the odd and even cases as alter-
5123         natives at the higher level:         natives at the higher level:
5124    
5125           ^(?:((.)(?1)\2|)|((.)(?3)\4|.))           ^(?:((.)(?1)\2|)|((.)(?3)\4|.))
5126    
5127         If  you  want  to match typical palindromic phrases, the pattern has to         If you want to match typical palindromic phrases, the  pattern  has  to
5128         ignore all non-word characters, which can be done like this:         ignore all non-word characters, which can be done like this:
5129    
5130           ^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$           ^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$
5131    
5132         If run with the PCRE_CASELESS option, this pattern matches phrases such         If run with the PCRE_CASELESS option, this pattern matches phrases such
5133         as "A man, a plan, a canal: Panama!" and it works well in both PCRE and         as "A man, a plan, a canal: Panama!" and it works well in both PCRE and
5134         Perl. Note the use of the possessive quantifier *+ to avoid  backtrack-         Perl.  Note the use of the possessive quantifier *+ to avoid backtrack-
5135         ing  into  sequences of non-word characters. Without this, PCRE takes a         ing into sequences of non-word characters. Without this, PCRE  takes  a
5136         great deal longer (ten times or more) to  match  typical  phrases,  and         great  deal  longer  (ten  times or more) to match typical phrases, and
5137         Perl takes so long that you think it has gone into a loop.         Perl takes so long that you think it has gone into a loop.
5138    
5139         WARNING:  The  palindrome-matching patterns above work only if the sub-         WARNING: The palindrome-matching patterns above work only if  the  sub-
5140         ject string does not start with a palindrome that is shorter  than  the         ject  string  does not start with a palindrome that is shorter than the
5141         entire  string.  For example, although "abcba" is correctly matched, if         entire string.  For example, although "abcba" is correctly matched,  if
5142         the subject is "ababa", PCRE finds the palindrome "aba" at  the  start,         the  subject  is "ababa", PCRE finds the palindrome "aba" at the start,
5143         then  fails at top level because the end of the string does not follow.         then fails at top level because the end of the string does not  follow.
5144         Once again, it cannot jump back into the recursion to try other  alter-         Once  again, it cannot jump back into the recursion to try other alter-
5145         natives, so the entire match fails.         natives, so the entire match fails.
5146    
5147    
5148  SUBPATTERNS AS SUBROUTINES  SUBPATTERNS AS SUBROUTINES
5149    
5150         If the syntax for a recursive subpattern reference (either by number or         If the syntax for a recursive subpattern reference (either by number or
5151         by name) is used outside the parentheses to which it refers,  it  oper-         by  name)  is used outside the parentheses to which it refers, it oper-
5152         ates  like a subroutine in a programming language. The "called" subpat-         ates like a subroutine in a programming language. The "called"  subpat-
5153         tern may be defined before or after the reference. A numbered reference         tern may be defined before or after the reference. A numbered reference
5154         can be absolute or relative, as in these examples:         can be absolute or relative, as in these examples:
5155    
# Line 5098  SUBPATTERNS AS SUBROUTINES Line 5161  SUBPATTERNS AS SUBROUTINES
5161    
5162           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
5163    
5164         matches  "sense and sensibility" and "response and responsibility", but         matches "sense and sensibility" and "response and responsibility",  but
5165         not "sense and responsibility". If instead the pattern         not "sense and responsibility". If instead the pattern
5166    
5167           (sens|respons)e and (?1)ibility           (sens|respons)e and (?1)ibility
5168    
5169         is used, it does match "sense and responsibility" as well as the  other         is  used, it does match "sense and responsibility" as well as the other
5170         two  strings.  Another  example  is  given  in the discussion of DEFINE         two strings. Another example is  given  in  the  discussion  of  DEFINE
5171         above.         above.
5172    
5173         Like recursive subpatterns, a subroutine call is always treated  as  an         Like  recursive  subpatterns, a subroutine call is always treated as an
5174         atomic  group. That is, once it has matched some of the subject string,         atomic group. That is, once it has matched some of the subject  string,
5175         it is never re-entered, even if it contains  untried  alternatives  and         it  is  never  re-entered, even if it contains untried alternatives and
5176         there  is a subsequent matching failure. Any capturing parentheses that         there is a subsequent matching failure. Any capturing parentheses  that
5177         are set during the subroutine call  revert  to  their  previous  values         are  set  during  the  subroutine  call revert to their previous values
5178         afterwards.         afterwards.
5179    
5180         When  a  subpattern is used as a subroutine, processing options such as         When a subpattern is used as a subroutine, processing options  such  as
5181         case-independence are fixed when the subpattern is defined. They cannot         case-independence are fixed when the subpattern is defined. They cannot
5182         be changed for different calls. For example, consider this pattern:         be changed for different calls. For example, consider this pattern:
5183    
5184           (abc)(?i:(?-1))           (abc)(?i:(?-1))
5185    
5186         It  matches  "abcabc". It does not match "abcABC" because the change of         It matches "abcabc". It does not match "abcABC" because the  change  of
5187         processing option does not affect the called subpattern.         processing option does not affect the called subpattern.
5188    
5189    
5190  ONIGURUMA SUBROUTINE SYNTAX  ONIGURUMA SUBROUTINE SYNTAX
5191    
5192         For compatibility with Oniguruma, the non-Perl syntax \g followed by  a         For  compatibility with Oniguruma, the non-Perl syntax \g followed by a
5193         name or a number enclosed either in angle brackets or single quotes, is         name or a number enclosed either in angle brackets or single quotes, is
5194         an alternative syntax for referencing a  subpattern  as  a  subroutine,         an  alternative  syntax  for  referencing a subpattern as a subroutine,
5195         possibly  recursively. Here are two of the examples used above, rewrit-         possibly recursively. Here are two of the examples used above,  rewrit-
5196         ten using this syntax:         ten using this syntax:
5197    
5198           (?<pn> \( ( (?>[^()]+) | \g<pn> )* \) )           (?<pn> \( ( (?>[^()]+) | \g<pn> )* \) )
5199           (sens|respons)e and \g'1'ibility           (sens|respons)e and \g'1'ibility
5200    
5201         PCRE supports an extension to Oniguruma: if a number is preceded  by  a         PCRE  supports  an extension to Oniguruma: if a number is preceded by a
5202         plus or a minus sign it is taken as a relative reference. For example:         plus or a minus sign it is taken as a relative reference. For example:
5203    
5204           (abc)(?i:\g<-1>)           (abc)(?i:\g<-1>)
5205    
5206         Note  that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are not         Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are  not
5207         synonymous. The former is a back reference; the latter is a  subroutine         synonymous.  The former is a back reference; the latter is a subroutine
5208         call.         call.
5209    
5210    
5211  CALLOUTS  CALLOUTS
5212    
5213         Perl has a feature whereby using the sequence (?{...}) causes arbitrary         Perl has a feature whereby using the sequence (?{...}) causes arbitrary
5214         Perl code to be obeyed in the middle of matching a regular  expression.         Perl  code to be obeyed in the middle of matching a regular expression.
5215         This makes it possible, amongst other things, to extract different sub-         This makes it possible, amongst other things, to extract different sub-
5216         strings that match the same pair of parentheses when there is a repeti-         strings that match the same pair of parentheses when there is a repeti-
5217         tion.         tion.
5218    
5219         PCRE provides a similar feature, but of course it cannot obey arbitrary         PCRE provides a similar feature, but of course it cannot obey arbitrary
5220         Perl code. The feature is called "callout". The caller of PCRE provides         Perl code. The feature is called "callout". The caller of PCRE provides
5221         an  external function by putting its entry point in the global variable         an external function by putting its entry point in the global  variable
5222         pcre_callout.  By default, this variable contains NULL, which  disables         pcre_callout.   By default, this variable contains NULL, which disables
5223         all calling out.         all calling out.
5224    
5225         Within  a  regular  expression,  (?C) indicates the points at which the         Within a regular expression, (?C) indicates the  points  at  which  the
5226         external function is to be called. If you want  to  identify  different         external  function  is  to be called. If you want to identify different
5227         callout  points, you can put a number less than 256 after the letter C.         callout points, you can put a number less than 256 after the letter  C.
5228         The default value is zero.  For example, this pattern has  two  callout         The  default  value is zero.  For example, this pattern has two callout
5229         points:         points:
5230    
5231           (?C1)abc(?C2)def           (?C1)abc(?C2)def
5232    
5233         If the PCRE_AUTO_CALLOUT flag is passed to pcre_compile(), callouts are         If the PCRE_AUTO_CALLOUT flag is passed to pcre_compile(), callouts are
5234         automatically installed before each item in the pattern. They  are  all         automatically  installed  before each item in the pattern. They are all
5235         numbered 255.         numbered 255.
5236    
5237         During matching, when PCRE reaches a callout point (and pcre_callout is         During matching, when PCRE reaches a callout point (and pcre_callout is
5238         set), the external function is called. It is provided with  the  number         set),  the  external function is called. It is provided with the number
5239         of  the callout, the position in the pattern, and, optionally, one item         of the callout, the position in the pattern, and, optionally, one  item
5240         of data originally supplied by the caller of pcre_exec().  The  callout         of  data  originally supplied by the caller of pcre_exec(). The callout
5241         function  may cause matching to proceed, to backtrack, or to fail alto-         function may cause matching to proceed, to backtrack, or to fail  alto-
5242         gether. A complete description of the interface to the callout function         gether. A complete description of the interface to the callout function
5243         is given in the pcrecallout documentation.         is given in the pcrecallout documentation.
5244    
5245    
5246  BACKTRACKING CONTROL  BACKTRACKING CONTROL
5247    
5248         Perl  5.10 introduced a number of "Special Backtracking Control Verbs",         Perl 5.10 introduced a number of "Special Backtracking Control  Verbs",
5249         which are described in the Perl documentation as "experimental and sub-         which are described in the Perl documentation as "experimental and sub-
5250         ject  to  change or removal in a future version of Perl". It goes on to         ject to change or removal in a future version of Perl". It goes  on  to
5251         say: "Their usage in production code should be noted to avoid  problems         say:  "Their usage in production code should be noted to avoid problems
5252         during upgrades." The same remarks apply to the PCRE features described         during upgrades." The same remarks apply to the PCRE features described
5253         in this section.         in this section.
5254    
5255         Since these verbs are specifically related  to  backtracking,  most  of         Since  these  verbs  are  specifically related to backtracking, most of
5256         them  can  be  used  only  when  the  pattern  is  to  be matched using         them can be  used  only  when  the  pattern  is  to  be  matched  using
5257         pcre_exec(), which uses a backtracking algorithm. With the exception of         pcre_exec(), which uses a backtracking algorithm. With the exception of
5258         (*FAIL), which behaves like a failing negative assertion, they cause an         (*FAIL), which behaves like a failing negative assertion, they cause an
5259         error if encountered by pcre_dfa_exec().         error if encountered by pcre_dfa_exec().
5260    
5261         If any of these verbs are used in an assertion or subroutine subpattern         If any of these verbs are used in an assertion or subroutine subpattern
5262         (including  recursive  subpatterns),  their  effect is confined to that         (including recursive subpatterns), their effect  is  confined  to  that
5263         subpattern; it does not extend to the surrounding  pattern.  Note  that         subpattern;  it  does  not extend to the surrounding pattern. Note that
5264         such  subpatterns are processed as anchored at the point where they are         such subpatterns are processed as anchored at the point where they  are
5265         tested.         tested.
5266    
5267         The new verbs make use of what was previously invalid syntax: an  open-         The  new verbs make use of what was previously invalid syntax: an open-
5268         ing parenthesis followed by an asterisk. They are generally of the form         ing parenthesis followed by an asterisk. They are generally of the form
5269         (*VERB) or (*VERB:NAME). Some may take either form, with differing  be-         (*VERB)  or (*VERB:NAME). Some may take either form, with differing be-
5270         haviour, depending on whether or not an argument is present. An name is         haviour, depending on whether or not an argument is present. An name is
5271         a sequence of letters, digits, and underscores. If the name  is  empty,         a  sequence  of letters, digits, and underscores. If the name is empty,
5272         that  is, if the closing parenthesis immediately follows the colon, the         that is, if the closing parenthesis immediately follows the colon,  the
5273         effect is as if the colon were not there. Any number of these verbs may         effect is as if the colon were not there. Any number of these verbs may
5274         occur in a pattern.         occur in a pattern.
5275    
5276         PCRE  contains some optimizations that are used to speed up matching by         PCRE contains some optimizations that are used to speed up matching  by
5277         running some checks at the start of each match attempt. For example, it         running some checks at the start of each match attempt. For example, it
5278         may  know  the minimum length of matching subject, or that a particular         may know the minimum length of matching subject, or that  a  particular
5279         character must be present. When one of these  optimizations  suppresses         character  must  be present. When one of these optimizations suppresses
5280         the  running  of  a match, any included backtracking verbs will not, of         the running of a match, any included backtracking verbs  will  not,  of
5281         course, be processed. You can suppress the start-of-match optimizations         course, be processed. You can suppress the start-of-match optimizations
5282         by setting the PCRE_NO_START_OPTIMIZE option when calling pcre_exec().         by setting the PCRE_NO_START_OPTIMIZE option when calling pcre_exec().
5283    
5284     Verbs that act immediately     Verbs that act immediately
5285    
5286         The  following  verbs act as soon as they are encountered. They may not         The following verbs act as soon as they are encountered. They  may  not
5287         be followed by a name.         be followed by a name.
5288    
5289            (*ACCEPT)            (*ACCEPT)
5290    
5291         This verb causes the match to end successfully, skipping the  remainder         This  verb causes the match to end successfully, skipping the remainder
5292         of  the pattern. When inside a recursion, only the innermost pattern is         of the pattern. When inside a recursion, only the innermost pattern  is
5293         ended immediately. If (*ACCEPT) is inside  capturing  parentheses,  the         ended  immediately.  If  (*ACCEPT) is inside capturing parentheses, the
5294         data  so  far  is  captured. (This feature was added to PCRE at release         data so far is captured. (This feature was added  to  PCRE  at  release
5295         8.00.) For example:         8.00.) For example:
5296    
5297           A((?:A|B(*ACCEPT)|C)D)           A((?:A|B(*ACCEPT)|C)D)
5298    
5299         This matches "AB", "AAD", or "ACD"; when it matches "AB", "B"  is  cap-         This  matches  "AB", "AAD", or "ACD"; when it matches "AB", "B" is cap-
5300         tured by the outer parentheses.         tured by the outer parentheses.
5301    
5302           (*FAIL) or (*F)           (*FAIL) or (*F)
5303    
5304         This  verb  causes the match to fail, forcing backtracking to occur. It         This verb causes the match to fail, forcing backtracking to  occur.  It
5305         is equivalent to (?!) but easier to read. The Perl documentation  notes         is  equivalent to (?!) but easier to read. The Perl documentation notes
5306         that  it  is  probably  useful only when combined with (?{}) or (??{}).         that it is probably useful only when combined  with  (?{})  or  (??{}).
5307         Those are, of course, Perl features that are not present in  PCRE.  The         Those  are,  of course, Perl features that are not present in PCRE. The
5308         nearest  equivalent is the callout feature, as for example in this pat-         nearest equivalent is the callout feature, as for example in this  pat-
5309         tern:         tern:
5310    
5311           a+(?C)(*FAIL)           a+(?C)(*FAIL)
5312    
5313         A match with the string "aaaa" always fails, but the callout  is  taken         A  match  with the string "aaaa" always fails, but the callout is taken
5314         before each backtrack happens (in this example, 10 times).         before each backtrack happens (in this example, 10 times).
5315    
5316     Recording which path was taken     Recording which path was taken
5317    
5318         There  is  one  verb  whose  main  purpose  is to track how a match was         There is one verb whose main purpose  is  to  track  how  a  match  was
5319         arrived at, though it also has a  secondary  use  in  conjunction  with         arrived  at,  though  it  also  has a secondary use in conjunction with
5320         advancing the match starting point (see (*SKIP) below).         advancing the match starting point (see (*SKIP) below).
5321    
5322           (*MARK:NAME) or (*:NAME)           (*MARK:NAME) or (*:NAME)
5323    
5324         A  name  is  always  required  with  this  verb.  There  may be as many         A name is always  required  with  this  verb.  There  may  be  as  many
5325         instances of (*MARK) as you like in a pattern, and their names  do  not         instances  of  (*MARK) as you like in a pattern, and their names do not
5326         have to be unique.         have to be unique.
5327    
5328         When  a  match  succeeds,  the  name of the last-encountered (*MARK) is         When a match succeeds, the name  of  the  last-encountered  (*MARK)  is
5329         passed back to  the  caller  via  the  pcre_extra  data  structure,  as         passed  back  to  the  caller  via  the  pcre_extra  data structure, as
5330         described in the section on pcre_extra in the pcreapi documentation. No         described in the section on pcre_extra in the pcreapi documentation. No
5331         data is returned for a partial match. Here is an  example  of  pcretest         data  is  returned  for a partial match. Here is an example of pcretest
5332         output,  where the /K modifier requests the retrieval and outputting of         output, where the /K modifier requests the retrieval and outputting  of
5333         (*MARK) data:         (*MARK) data:
5334    
5335           /X(*MARK:A)Y|X(*MARK:B)Z/K           /X(*MARK:A)Y|X(*MARK:B)Z/K
# Line 5278  BACKTRACKING CONTROL Line 5341  BACKTRACKING CONTROL
5341           MK: B           MK: B
5342    
5343         The (*MARK) name is tagged with "MK:" in this output, and in this exam-         The (*MARK) name is tagged with "MK:" in this output, and in this exam-
5344         ple  it indicates which of the two alternatives matched. This is a more         ple it indicates which of the two alternatives matched. This is a  more
5345         efficient way of obtaining this information than putting each  alterna-         efficient  way of obtaining this information than putting each alterna-
5346         tive in its own capturing parentheses.         tive in its own capturing parentheses.
5347    
5348         A  name  may  also  be  returned after a failed match if the final path         A name may also be returned after a failed  match  if  the  final  path
5349         through the pattern involves (*MARK). However, unless (*MARK)  used  in         through  the  pattern involves (*MARK). However, unless (*MARK) used in
5350         conjunction  with  (*COMMIT),  this  is unlikely to happen for an unan-         conjunction with (*COMMIT), this is unlikely to  happen  for  an  unan-
5351         chored pattern because, as the starting point for matching is advanced,         chored pattern because, as the starting point for matching is advanced,
5352         the final check is often with an empty string, causing a failure before         the final check is often with an empty string, causing a failure before
5353         (*MARK) is reached. For example:         (*MARK) is reached. For example:
# Line 5294  BACKTRACKING CONTROL Line 5357  BACKTRACKING CONTROL
5357           No match           No match
5358    
5359         There are three potential starting points for this match (starting with         There are three potential starting points for this match (starting with
5360         X,  starting  with  P,  and  with  an  empty string). If the pattern is         X, starting with P, and with  an  empty  string).  If  the  pattern  is
5361         anchored, the result is different:         anchored, the result is different:
5362    
5363           /^X(*MARK:A)Y|^X(*MARK:B)Z/K           /^X(*MARK:A)Y|^X(*MARK:B)Z/K
5364           XP           XP
5365           No match, mark = B           No match, mark = B
5366    
5367         PCRE's start-of-match optimizations can also interfere with  this.  For         PCRE's  start-of-match  optimizations can also interfere with this. For
5368         example,  if, as a result of a call to pcre_study(), it knows the mini-         example, if, as a result of a call to pcre_study(), it knows the  mini-
5369         mum subject length for a match, a shorter subject will not  be  scanned         mum  subject  length for a match, a shorter subject will not be scanned
5370         at all.         at all.
5371    
5372         Note that similar anomalies (though different in detail) exist in Perl,         Note that similar anomalies (though different in detail) exist in Perl,
5373         no doubt for the same reasons. The use of (*MARK) data after  a  failed         no  doubt  for the same reasons. The use of (*MARK) data after a failed
5374         match  of an unanchored pattern is not recommended, unless (*COMMIT) is         match of an unanchored pattern is not recommended, unless (*COMMIT)  is
5375         involved.         involved.
5376    
5377     Verbs that act after backtracking     Verbs that act after backtracking
5378    
5379         The following verbs do nothing when they are encountered. Matching con-         The following verbs do nothing when they are encountered. Matching con-
5380         tinues  with what follows, but if there is no subsequent match, causing         tinues with what follows, but if there is no subsequent match,  causing
5381         a backtrack to the verb, a failure is  forced.  That  is,  backtracking         a  backtrack  to  the  verb, a failure is forced. That is, backtracking
5382         cannot  pass  to the left of the verb. However, when one of these verbs         cannot pass to the left of the verb. However, when one of  these  verbs
5383         appears inside an atomic group, its effect is confined to  that  group,         appears  inside  an atomic group, its effect is confined to that group,
5384         because  once the group has been matched, there is never any backtrack-         because once the group has been matched, there is never any  backtrack-
5385         ing into it. In this situation, backtracking can  "jump  back"  to  the         ing  into  it.  In  this situation, backtracking can "jump back" to the
5386         left  of the entire atomic group. (Remember also, as stated above, that         left of the entire atomic group. (Remember also, as stated above,  that
5387         this localization also applies in subroutine calls and assertions.)         this localization also applies in subroutine calls and assertions.)
5388    
5389         These verbs differ in exactly what kind of failure  occurs  when  back-         These  verbs  differ  in exactly what kind of failure occurs when back-
5390         tracking reaches them.         tracking reaches them.
5391    
5392           (*COMMIT)           (*COMMIT)
5393    
5394         This  verb, which may not be followed by a name, causes the whole match         This verb, which may not be followed by a name, causes the whole  match
5395         to fail outright if the rest of the pattern does not match. Even if the         to fail outright if the rest of the pattern does not match. Even if the
5396         pattern is unanchored, no further attempts to find a match by advancing         pattern is unanchored, no further attempts to find a match by advancing
5397         the  starting  point  take  place.  Once  (*COMMIT)  has  been  passed,         the  starting  point  take  place.  Once  (*COMMIT)  has  been  passed,
5398         pcre_exec()  is  committed  to  finding a match at the current starting         pcre_exec() is committed to finding a match  at  the  current  starting
5399         point, or not at all. For example:         point, or not at all. For example:
5400    
5401           a+(*COMMIT)b           a+(*COMMIT)b
5402    
5403         This matches "xxaab" but not "aacaab". It can be thought of as  a  kind         This  matches  "xxaab" but not "aacaab". It can be thought of as a kind
5404         of dynamic anchor, or "I've started, so I must finish." The name of the         of dynamic anchor, or "I've started, so I must finish." The name of the
5405         most recently passed (*MARK) in the path is passed back when  (*COMMIT)         most  recently passed (*MARK) in the path is passed back when (*COMMIT)
5406         forces a match failure.         forces a match failure.
5407    
5408         Note  that  (*COMMIT)  at  the start of a pattern is not the same as an         Note that (*COMMIT) at the start of a pattern is not  the  same  as  an
5409         anchor, unless PCRE's start-of-match optimizations are turned  off,  as         anchor,  unless  PCRE's start-of-match optimizations are turned off, as
5410         shown in this pcretest example:         shown in this pcretest example:
5411    
5412           /(*COMMIT)abc/           /(*COMMIT)abc/
# Line 5352  BACKTRACKING CONTROL Line 5415  BACKTRACKING CONTROL
5415           xyzabc\Y           xyzabc\Y
5416           No match           No match
5417    
5418         PCRE  knows  that  any  match  must start with "a", so the optimization         PCRE knows that any match must start  with  "a",  so  the  optimization
5419         skips along the subject to "a" before running the first match  attempt,         skips  along the subject to "a" before running the first match attempt,
5420         which  succeeds.  When the optimization is disabled by the \Y escape in         which succeeds. When the optimization is disabled by the \Y  escape  in
5421         the second subject, the match starts at "x" and so the (*COMMIT) causes         the second subject, the match starts at "x" and so the (*COMMIT) causes
5422         it to fail without trying any other starting points.         it to fail without trying any other starting points.
5423    
5424           (*PRUNE) or (*PRUNE:NAME)           (*PRUNE) or (*PRUNE:NAME)
5425    
5426         This  verb causes the match to fail at the current starting position in         This verb causes the match to fail at the current starting position  in
5427         the subject if the rest of the pattern does not match. If  the  pattern         the  subject  if the rest of the pattern does not match. If the pattern
5428         is  unanchored,  the  normal  "bumpalong"  advance to the next starting         is unanchored, the normal "bumpalong"  advance  to  the  next  starting
5429         character then happens. Backtracking can occur as usual to the left  of         character  then happens. Backtracking can occur as usual to the left of
5430         (*PRUNE),  before  it  is  reached,  or  when  matching to the right of         (*PRUNE), before it is reached,  or  when  matching  to  the  right  of
5431         (*PRUNE), but if there is no match to the  right,  backtracking  cannot         (*PRUNE),  but  if  there is no match to the right, backtracking cannot
5432         cross  (*PRUNE). In simple cases, the use of (*PRUNE) is just an alter-         cross (*PRUNE). In simple cases, the use of (*PRUNE) is just an  alter-
5433         native to an atomic group or possessive quantifier, but there are  some         native  to an atomic group or possessive quantifier, but there are some
5434         uses of (*PRUNE) that cannot be expressed in any other way.  The behav-         uses of (*PRUNE) that cannot be expressed in any other way.  The behav-
5435         iour of (*PRUNE:NAME) is the  same  as  (*MARK:NAME)(*PRUNE)  when  the         iour  of  (*PRUNE:NAME)  is  the  same as (*MARK:NAME)(*PRUNE) when the
5436         match  fails  completely;  the name is passed back if this is the final         match fails completely; the name is passed back if this  is  the  final
5437         attempt.  (*PRUNE:NAME) does not pass back a name  if  the  match  suc-         attempt.   (*PRUNE:NAME)  does  not  pass back a name if the match suc-
5438         ceeds.  In  an  anchored pattern (*PRUNE) has the same effect as (*COM-         ceeds. In an anchored pattern (*PRUNE) has the same  effect  as  (*COM-
5439         MIT).         MIT).
5440    
5441           (*SKIP)           (*SKIP)
5442    
5443         This verb, when given without a name, is like (*PRUNE), except that  if         This  verb, when given without a name, is like (*PRUNE), except that if
5444         the  pattern  is unanchored, the "bumpalong" advance is not to the next         the pattern is unanchored, the "bumpalong" advance is not to  the  next
5445         character, but to the position in the subject where (*SKIP) was encoun-         character, but to the position in the subject where (*SKIP) was encoun-
5446         tered.  (*SKIP)  signifies that whatever text was matched leading up to         tered. (*SKIP) signifies that whatever text was matched leading  up  to
5447         it cannot be part of a successful match. Consider:         it cannot be part of a successful match. Consider:
5448    
5449           a+(*SKIP)b           a+(*SKIP)b
5450    
5451         If the subject is "aaaac...",  after  the  first  match  attempt  fails         If  the  subject  is  "aaaac...",  after  the first match attempt fails
5452         (starting  at  the  first  character in the string), the starting point         (starting at the first character in the  string),  the  starting  point
5453         skips on to start the next attempt at "c". Note that a possessive quan-         skips on to start the next attempt at "c". Note that a possessive quan-
5454         tifer  does not have the same effect as this example; although it would         tifer does not have the same effect as this example; although it  would
5455         suppress backtracking  during  the  first  match  attempt,  the  second         suppress  backtracking  during  the  first  match  attempt,  the second
5456         attempt  would  start at the second character instead of skipping on to         attempt would start at the second character instead of skipping  on  to
5457         "c".         "c".
5458    
5459           (*SKIP:NAME)           (*SKIP:NAME)
5460    
5461         When (*SKIP) has an associated name, its behaviour is modified. If  the         When  (*SKIP) has an associated name, its behaviour is modified. If the
5462         following pattern fails to match, the previous path through the pattern         following pattern fails to match, the previous path through the pattern
5463         is searched for the most recent (*MARK) that has the same name. If  one         is  searched for the most recent (*MARK) that has the same name. If one
5464         is  found, the "bumpalong" advance is to the subject position that cor-         is found, the "bumpalong" advance is to the subject position that  cor-
5465         responds to that (*MARK) instead of to where (*SKIP)  was  encountered.         responds  to  that (*MARK) instead of to where (*SKIP) was encountered.
5466         If  no (*MARK) with a matching name is found, normal "bumpalong" of one         If no (*MARK) with a matching name is found, normal "bumpalong" of  one
5467         character happens (the (*SKIP) is ignored).         character happens (the (*SKIP) is ignored).
5468    
5469           (*THEN) or (*THEN:NAME)           (*THEN) or (*THEN:NAME)
5470    
5471         This verb causes a skip to the next alternation if the rest of the pat-         This verb causes a skip to the next alternation if the rest of the pat-
5472         tern does not match. That is, it cancels pending backtracking, but only         tern does not match. That is, it cancels pending backtracking, but only
5473         within the current alternation. Its name  comes  from  the  observation         within  the  current  alternation.  Its name comes from the observation
5474         that it can be used for a pattern-based if-then-else block:         that it can be used for a pattern-based if-then-else block:
5475    
5476           ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...           ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
5477    
5478         If  the COND1 pattern matches, FOO is tried (and possibly further items         If the COND1 pattern matches, FOO is tried (and possibly further  items
5479         after the end of the group if FOO succeeds);  on  failure  the  matcher         after  the  end  of  the group if FOO succeeds); on failure the matcher
5480         skips  to  the second alternative and tries COND2, without backtracking         skips to the second alternative and tries COND2,  without  backtracking
5481         into COND1. The behaviour  of  (*THEN:NAME)  is  exactly  the  same  as         into  COND1.  The  behaviour  of  (*THEN:NAME)  is  exactly the same as
5482         (*MARK:NAME)(*THEN)  if  the  overall  match  fails.  If (*THEN) is not         (*MARK:NAME)(*THEN) if the overall  match  fails.  If  (*THEN)  is  not
5483         directly inside an alternation, it acts like (*PRUNE).         directly inside an alternation, it acts like (*PRUNE).
5484    
5485    
# Line 5434  AUTHOR Line 5497  AUTHOR
5497    
5498  REVISION  REVISION
5499    
5500         Last updated: 05 May 2010         Last updated: 18 May 2010
5501         Copyright (c) 1997-2010 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
5502  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
5503    
5504    
5505  PCRESYNTAX(3)                                                    PCRESYNTAX(3)  PCRESYNTAX(3)                                                    PCRESYNTAX(3)
5506    
5507    
# Line 5494  CHARACTER TYPES Line 5557  CHARACTER TYPES
5557           \W         a "non-word" character           \W         a "non-word" character
5558           \X         an extended Unicode sequence           \X         an extended Unicode sequence
5559    
5560         In PCRE, \d, \D, \s, \S, \w, and \W recognize only ASCII characters.         In  PCRE,  by  default, \d, \D, \s, \S, \w, and \W recognize only ASCII
5561           characters, even in UTF-8 mode. However, this can be changed by setting
5562           the PCRE_UCP option.
5563    
5564    
5565  GENERAL CATEGORY PROPERTIES FOR \p and \P  GENERAL CATEGORY PROPERTIES FOR \p and \P
# Line 5594  CHARACTER CLASSES Line 5659  CHARACTER CLASSES
5659           word        same as \w           word        same as \w
5660           xdigit      hexadecimal digit           xdigit      hexadecimal digit
5661    
5662         In PCRE, POSIX character set names recognize only ASCII characters. You         In PCRE, POSIX character set names recognize only ASCII  characters  by
5663         can use \Q...\E inside a character class.         default,  but  some  of them use Unicode properties if PCRE_UCP is set.
5664           You can use \Q...\E inside a character class.
5665    
5666    
5667  QUANTIFIERS  QUANTIFIERS
# Line 5620  QUANTIFIERS Line 5686  QUANTIFIERS
5686    
5687  ANCHORS AND SIMPLE ASSERTIONS  ANCHORS AND SIMPLE ASSERTIONS
5688    
5689           \b          word boundary (only ASCII letters recognized)           \b          word boundary
5690           \B          not a word boundary           \B          not a word boundary
5691           ^           start of subject           ^           start of subject
5692                        also after internal newline in multiline mode                        also after internal newline in multiline mode
# Line 5675  OPTION SETTING Line 5741  OPTION SETTING
5741           (?x)            extended (ignore white space)           (?x)            extended (ignore white space)
5742           (?-...)         unset option(s)           (?-...)         unset option(s)
5743    
5744         The following is recognized only at the start of a pattern or after one         The following are recognized only at the start of a  pattern  or  after
5745         of the newline-setting options with similar syntax:         one of the newline-setting options with similar syntax:
5746    
5747           (*UTF8)         set UTF-8 mode           (*UTF8)         set UTF-8 mode (PCRE_UTF8)
5748             (*UCP)          set PCRE_UCP (use Unicode properties for \d etc)
5749    
5750    
5751  LOOKAHEAD AND LOOKBEHIND ASSERTIONS  LOOKAHEAD AND LOOKBEHIND ASSERTIONS
# Line 5747  BACKTRACKING CONTROL Line 5814  BACKTRACKING CONTROL
5814           (*ACCEPT)       force successful match           (*ACCEPT)       force successful match
5815           (*FAIL)         force backtrack; synonym (*F)           (*FAIL)         force backtrack; synonym (*F)
5816    
5817         The following act only when a subsequent match failure causes  a  back-         The  following  act only when a subsequent match failure causes a back-
5818         track to reach them. They all force a match failure, but they differ in         track to reach them. They all force a match failure, but they differ in
5819         what happens afterwards. Those that advance the start-of-match point do         what happens afterwards. Those that advance the start-of-match point do
5820         so only if the pattern is not anchored.         so only if the pattern is not anchored.
# Line 5760  BACKTRACKING CONTROL Line 5827  BACKTRACKING CONTROL
5827    
5828  NEWLINE CONVENTIONS  NEWLINE CONVENTIONS
5829    
5830         These  are  recognized only at the very start of the pattern or after a         These are recognized only at the very start of the pattern or  after  a
5831         (*BSR_...) or (*UTF8) option.         (*BSR_...) or (*UTF8) or (*UCP) option.
5832    
5833           (*CR)           carriage return only           (*CR)           carriage return only
5834           (*LF)           linefeed only           (*LF)           linefeed only
# Line 5772  NEWLINE CONVENTIONS Line 5839  NEWLINE CONVENTIONS
5839    
5840  WHAT \R MATCHES  WHAT \R MATCHES
5841    
5842         These are recognized only at the very start of the pattern or  after  a         These  are  recognized only at the very start of the pattern or after a
5843         (*...) option that sets the newline convention or UTF-8 mode.         (*...) option that sets the newline convention or UTF-8 or UCP mode.
5844    
5845           (*BSR_ANYCRLF)  CR, LF, or CRLF           (*BSR_ANYCRLF)  CR, LF, or CRLF
5846           (*BSR_UNICODE)  any Unicode newline sequence           (*BSR_UNICODE)  any Unicode newline sequence
# Line 5799  AUTHOR Line 5866  AUTHOR
5866    
5867  REVISION  REVISION
5868    
5869         Last updated: 05 May 2010         Last updated: 12 May 2010
5870         Copyright (c) 1997-2010 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
5871  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
5872    
5873    
5874  PCREPARTIAL(3)                                                  PCREPARTIAL(3)  PCREPARTIAL(3)                                                  PCREPARTIAL(3)
5875    
5876    
# Line 6184  REVISION Line 6251  REVISION
6251         Last updated: 19 October 2009         Last updated: 19 October 2009
6252         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
6253  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
6254    
6255    
6256  PCREPRECOMPILE(3)                                            PCREPRECOMPILE(3)  PCREPRECOMPILE(3)                                            PCREPRECOMPILE(3)
6257    
6258    
# Line 6308  REVISION Line 6375  REVISION
6375         Last updated: 13 June 2007         Last updated: 13 June 2007
6376         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
6377  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
6378    
6379    
6380  PCREPERFORM(3)                                                  PCREPERFORM(3)  PCREPERFORM(3)                                                  PCREPERFORM(3)
6381    
6382    
# Line 6400  PROCESSING TIME Line 6467  PROCESSING TIME
6467         If you can find an alternative pattern  that  does  not  use  character         If you can find an alternative pattern  that  does  not  use  character
6468         properties, it will probably be faster.         properties, it will probably be faster.
6469    
6470           By  default,  the  escape  sequences  \b, \d, \s, and \w, and the POSIX
6471           character classes such as [:alpha:]  do  not  use  Unicode  properties,
6472           partly for backwards compatibility, and partly for performance reasons.
6473           However, you can set PCRE_UCP if you want Unicode character  properties
6474           to  be  used.  This  can double the matching time for items such as \d,
6475           when matched with  pcre_exec();  the  performance  loss  is  less  with
6476           pcre_dfa_exec(), and in both cases there is not much difference for \b.
6477    
6478         When  a  pattern  begins  with .* not in parentheses, or in parentheses         When  a  pattern  begins  with .* not in parentheses, or in parentheses
6479         that are not the subject of a backreference, and the PCRE_DOTALL option         that are not the subject of a backreference, and the PCRE_DOTALL option
6480         is  set, the pattern is implicitly anchored by PCRE, since it can match         is  set, the pattern is implicitly anchored by PCRE, since it can match
# Line 6465  AUTHOR Line 6540  AUTHOR
6540    
6541  REVISION  REVISION
6542    
6543         Last updated: 07 March 2010         Last updated: 16 May 2010
6544         Copyright (c) 1997-2010 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
6545  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
6546    
6547    
6548  PCREPOSIX(3)                                                      PCREPOSIX(3)  PCREPOSIX(3)                                                      PCREPOSIX(3)
6549    
6550    
# Line 6571  COMPILING A PATTERN Line 6646  COMPILING A PATTERN
6646         ing,  the  nmatch  and  pmatch  arguments  are ignored, and no captured         ing,  the  nmatch  and  pmatch  arguments  are ignored, and no captured
6647         strings are returned.         strings are returned.
6648    
6649             REG_UCP
6650    
6651           The PCRE_UCP option is set when the regular expression  is  passed  for
6652           compilation  to  the  native  function. This causes PCRE to use Unicode
6653           properties when matchine \d, \w,  etc.,  instead  of  just  recognizing
6654           ASCII values. Note that REG_UTF8 is not part of the POSIX standard.
6655    
6656           REG_UNGREEDY           REG_UNGREEDY
6657    
6658         The PCRE_UNGREEDY option is set when the regular expression  is  passed         The  PCRE_UNGREEDY  option is set when the regular expression is passed
6659         for  compilation  to the native function. Note that REG_UNGREEDY is not         for compilation to the native function. Note that REG_UNGREEDY  is  not
6660         part of the POSIX standard.         part of the POSIX standard.
6661    
6662           REG_UTF8           REG_UTF8
6663    
6664         The PCRE_UTF8 option is set when the regular expression is  passed  for         The  PCRE_UTF8  option is set when the regular expression is passed for
6665         compilation  to the native function. This causes the pattern itself and         compilation to the native function. This causes the pattern itself  and
6666         all data strings used for matching it to be treated as  UTF-8  strings.         all  data  strings used for matching it to be treated as UTF-8 strings.
6667         Note that REG_UTF8 is not part of the POSIX standard.         Note that REG_UTF8 is not part of the POSIX standard.
6668    
6669         In  the  absence  of  these  flags, no options are passed to the native         In the absence of these flags, no options  are  passed  to  the  native
6670         function.  This means the the  regex  is  compiled  with  PCRE  default         function.   This  means  the  the  regex  is compiled with PCRE default
6671         semantics.  In particular, the way it handles newline characters in the         semantics. In particular, the way it handles newline characters in  the
6672         subject string is the Perl way, not the POSIX way.  Note  that  setting         subject  string  is  the Perl way, not the POSIX way. Note that setting
6673         PCRE_MULTILINE  has only some of the effects specified for REG_NEWLINE.         PCRE_MULTILINE has only some of the effects specified for  REG_NEWLINE.
6674         It does not affect the way newlines are matched by . (they are not)  or         It  does not affect the way newlines are matched by . (they are not) or
6675         by a negative class such as [^a] (they are).         by a negative class such as [^a] (they are).
6676    
6677         The  yield of regcomp() is zero on success, and non-zero otherwise. The         The yield of regcomp() is zero on success, and non-zero otherwise.  The
6678         preg structure is filled in on success, and one member of the structure         preg structure is filled in on success, and one member of the structure
6679         is  public: re_nsub contains the number of capturing subpatterns in the         is public: re_nsub contains the number of capturing subpatterns in  the
6680         regular expression. Various error codes are defined in the header file.         regular expression. Various error codes are defined in the header file.
6681    
6682         NOTE: If the yield of regcomp() is non-zero, you must  not  attempt  to         NOTE:  If  the  yield of regcomp() is non-zero, you must not attempt to
6683         use the contents of the preg structure. If, for example, you pass it to         use the contents of the preg structure. If, for example, you pass it to
6684         regexec(), the result is undefined and your program is likely to crash.         regexec(), the result is undefined and your program is likely to crash.
6685    
# Line 6605  COMPILING A PATTERN Line 6687  COMPILING A PATTERN
6687  MATCHING NEWLINE CHARACTERS  MATCHING NEWLINE CHARACTERS
6688    
6689         This area is not simple, because POSIX and Perl take different views of         This area is not simple, because POSIX and Perl take different views of
6690         things.   It  is  not possible to get PCRE to obey POSIX semantics, but         things.  It is not possible to get PCRE to obey  POSIX  semantics,  but
6691         then PCRE was never intended to be a POSIX engine. The following  table         then  PCRE was never intended to be a POSIX engine. The following table
6692         lists  the  different  possibilities for matching newline characters in         lists the different possibilities for matching  newline  characters  in
6693         PCRE:         PCRE:
6694    
6695                                   Default   Change with                                   Default   Change with
# Line 6629  MATCHING NEWLINE CHARACTERS Line 6711  MATCHING NEWLINE CHARACTERS
6711           ^ matches \n in middle     no     REG_NEWLINE           ^ matches \n in middle     no     REG_NEWLINE
6712    
6713         PCRE's behaviour is the same as Perl's, except that there is no equiva-         PCRE's behaviour is the same as Perl's, except that there is no equiva-
6714         lent  for  PCRE_DOLLAR_ENDONLY in Perl. In both PCRE and Perl, there is         lent for PCRE_DOLLAR_ENDONLY in Perl. In both PCRE and Perl,  there  is
6715         no way to stop newline from matching [^a].         no way to stop newline from matching [^a].
6716    
6717         The  default  POSIX  newline  handling  can  be  obtained  by   setting         The   default  POSIX  newline  handling  can  be  obtained  by  setting
6718         PCRE_DOTALL  and  PCRE_DOLLAR_ENDONLY, but there is no way to make PCRE         PCRE_DOTALL and PCRE_DOLLAR_ENDONLY, but there is no way to  make  PCRE
6719         behave exactly as for the REG_NEWLINE action.         behave exactly as for the REG_NEWLINE action.
6720    
6721    
6722  MATCHING A PATTERN  MATCHING A PATTERN
6723    
6724         The function regexec() is called  to  match  a  compiled  pattern  preg         The  function  regexec()  is  called  to  match a compiled pattern preg
6725         against  a  given string, which is by default terminated by a zero byte         against a given string, which is by default terminated by a  zero  byte
6726         (but see REG_STARTEND below), subject to the options in  eflags.  These         (but  see  REG_STARTEND below), subject to the options in eflags. These
6727         can be:         can be:
6728    
6729           REG_NOTBOL           REG_NOTBOL
# Line 6663  MATCHING A PATTERN Line 6745  MATCHING A PATTERN
6745    
6746           REG_STARTEND           REG_STARTEND
6747    
6748         The string is considered to start at string +  pmatch[0].rm_so  and  to         The  string  is  considered to start at string + pmatch[0].rm_so and to
6749         have  a terminating NUL located at string + pmatch[0].rm_eo (there need         have a terminating NUL located at string + pmatch[0].rm_eo (there  need
6750         not actually be a NUL at that location), regardless  of  the  value  of         not  actually  be  a  NUL at that location), regardless of the value of
6751         nmatch.  This  is a BSD extension, compatible with but not specified by         nmatch. This is a BSD extension, compatible with but not  specified  by
6752         IEEE Standard 1003.2 (POSIX.2), and should  be  used  with  caution  in         IEEE  Standard  1003.2  (POSIX.2),  and  should be used with caution in
6753         software intended to be portable to other systems. Note that a non-zero         software intended to be portable to other systems. Note that a non-zero
6754         rm_so does not imply REG_NOTBOL; REG_STARTEND affects only the location         rm_so does not imply REG_NOTBOL; REG_STARTEND affects only the location
6755         of the string, not how it is matched.         of the string, not how it is matched.
6756    
6757         If  the pattern was compiled with the REG_NOSUB flag, no data about any         If the pattern was compiled with the REG_NOSUB flag, no data about  any
6758         matched strings  is  returned.  The  nmatch  and  pmatch  arguments  of         matched  strings  is  returned.  The  nmatch  and  pmatch  arguments of
6759         regexec() are ignored.         regexec() are ignored.
6760    
6761         If the value of nmatch is zero, or if the value pmatch is NULL, no data         If the value of nmatch is zero, or if the value pmatch is NULL, no data
# Line 6681  MATCHING A PATTERN Line 6763  MATCHING A PATTERN
6763    
6764         Otherwise,the portion of the string that was matched, and also any cap-         Otherwise,the portion of the string that was matched, and also any cap-
6765         tured substrings, are returned via the pmatch argument, which points to         tured substrings, are returned via the pmatch argument, which points to
6766         an array of nmatch structures of type regmatch_t, containing  the  mem-         an  array  of nmatch structures of type regmatch_t, containing the mem-
6767         bers  rm_so  and rm_eo. These contain the offset to the first character         bers rm_so and rm_eo. These contain the offset to the  first  character
6768         of each substring and the offset to the first character after  the  end         of  each  substring and the offset to the first character after the end
6769         of  each substring, respectively. The 0th element of the vector relates         of each substring, respectively. The 0th element of the vector  relates
6770         to the entire portion of string that was matched;  subsequent  elements         to  the  entire portion of string that was matched; subsequent elements
6771         relate  to  the capturing subpatterns of the regular expression. Unused         relate to the capturing subpatterns of the regular  expression.  Unused
6772         entries in the array have both structure members set to -1.         entries in the array have both structure members set to -1.
6773    
6774         A successful match yields  a  zero  return;  various  error  codes  are         A  successful  match  yields  a  zero  return;  various error codes are
6775         defined  in  the  header  file,  of which REG_NOMATCH is the "expected"         defined in the header file, of  which  REG_NOMATCH  is  the  "expected"
6776         failure code.         failure code.
6777    
6778    
6779  ERROR MESSAGES  ERROR MESSAGES
6780    
6781         The regerror() function maps a non-zero errorcode from either regcomp()         The regerror() function maps a non-zero errorcode from either regcomp()
6782         or  regexec()  to  a  printable message. If preg is not NULL, the error         or regexec() to a printable message. If preg is  not  NULL,  the  error
6783         should have arisen from the use of that structure. A message terminated         should have arisen from the use of that structure. A message terminated
6784         by  a  binary  zero  is  placed  in  errbuf. The length of the message,         by a binary zero is placed  in  errbuf.  The  length  of  the  message,
6785         including the zero, is limited to errbuf_size. The yield of  the  func-         including  the  zero, is limited to errbuf_size. The yield of the func-
6786         tion is the size of buffer needed to hold the whole message.         tion is the size of buffer needed to hold the whole message.
6787    
6788    
6789  MEMORY USAGE  MEMORY USAGE
6790    
6791         Compiling  a regular expression causes memory to be allocated and asso-         Compiling a regular expression causes memory to be allocated and  asso-
6792         ciated with the preg structure. The function regfree() frees  all  such         ciated  with  the preg structure. The function regfree() frees all such
6793         memory,  after  which  preg may no longer be used as a compiled expres-         memory, after which preg may no longer be used as  a  compiled  expres-
6794         sion.         sion.
6795    
6796    
# Line 6721  AUTHOR Line 6803  AUTHOR
6803    
6804  REVISION  REVISION
6805    
6806         Last updated: 02 September 2009         Last updated: 16 May 2010
6807         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
6808  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
6809    
6810    
6811  PCRECPP(3)                                                          PCRECPP(3)  PCRECPP(3)                                                          PCRECPP(3)
6812    
6813    
# Line 7065  REVISION Line 7147  REVISION
7147    
7148         Last updated: 17 March 2009         Last updated: 17 March 2009
7149  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
7150    
7151    
7152  PCRESAMPLE(3)                                                    PCRESAMPLE(3)  PCRESAMPLE(3)                                                    PCRESAMPLE(3)
7153    
7154    
# Line 7108  PCRE SAMPLE PROGRAM Line 7190  PCRE SAMPLE PROGRAM
7190           gcc -o pcredemo -I/usr/local/include pcredemo.c \           gcc -o pcredemo -I/usr/local/include pcredemo.c \
7191               -L/usr/local/lib -lpcre               -L/usr/local/lib -lpcre
7192    
7193         Once you have compiled the demonstration program, you  can  run  simple         In a Windows environment, if you want to statically  link  the  program
7194         tests like this:         against a non-dll pcre.a file, you must uncomment the line that defines
7195           PCRE_STATIC before including pcre.h, because  otherwise  the  pcre_mal-
7196           loc()   and   pcre_free()   exported   functions   will   be   declared
7197           __declspec(dllimport), with unwanted results.
7198    
7199           Once you have compiled and linked the demonstration  program,  you  can
7200           run simple tests like this:
7201    
7202           ./pcredemo 'cat|dog' 'the cat sat on the mat'           ./pcredemo 'cat|dog' 'the cat sat on the mat'
7203           ./pcredemo -g 'cat|dog' 'the dog sat on the cat'           ./pcredemo -g 'cat|dog' 'the dog sat on the cat'
# Line 7143  AUTHOR Line 7231  AUTHOR
7231    
7232  REVISION  REVISION
7233    
7234         Last updated: 30 September 2009         Last updated: 26 May 2010
7235         Copyright (c) 1997-2009 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
7236  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
7237  PCRESTACK(3)                                                      PCRESTACK(3)  PCRESTACK(3)                                                      PCRESTACK(3)
7238    
# Line 7295  REVISION Line 7383  REVISION
7383         Last updated: 03 January 2010         Last updated: 03 January 2010
7384         Copyright (c) 1997-2010 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
7385  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
7386    
7387    

Legend:
Removed from v.518  
changed lines
  Added in v.535

  ViewVC Help
Powered by ViewVC 1.1.5