/[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 172 by ph10, Tue Jun 5 10:40:13 2007 UTC revision 227 by ph10, Tue Aug 21 15:00:15 2007 UTC
# Line 45  INTRODUCTION Line 45  INTRODUCTION
45    
46         Details of exactly which Perl regular expression features are  and  are         Details of exactly which Perl regular expression features are  and  are
47         not supported by PCRE are given in separate documents. See the pcrepat-         not supported by PCRE are given in separate documents. See the pcrepat-
48         tern and pcrecompat pages.         tern and pcrecompat pages. There is a syntax summary in the  pcresyntax
49           page.
50    
51         Some features of PCRE can be included, excluded, or  changed  when  the         Some  features  of  PCRE can be included, excluded, or changed when the
52         library  is  built.  The pcre_config() function makes it possible for a         library is built. The pcre_config() function makes it  possible  for  a
53         client to discover which features are  available.  The  features  them-         client  to  discover  which  features are available. The features them-
54         selves  are described in the pcrebuild page. Documentation about build-         selves are described in the pcrebuild page. Documentation about  build-
55         ing PCRE for various operating systems can be found in the README  file         ing  PCRE for various operating systems can be found in the README file
56         in the source distribution.         in the source distribution.
57    
58         The  library  contains  a number of undocumented internal functions and         The library contains a number of undocumented  internal  functions  and
59         data tables that are used by more than one  of  the  exported  external         data  tables  that  are  used by more than one of the exported external
60         functions,  but  which  are  not  intended for use by external callers.         functions, but which are not intended  for  use  by  external  callers.
61         Their names all begin with "_pcre_", which hopefully will  not  provoke         Their  names  all begin with "_pcre_", which hopefully will not provoke
62         any name clashes. In some environments, it is possible to control which         any name clashes. In some environments, it is possible to control which
63         external symbols are exported when a shared library is  built,  and  in         external  symbols  are  exported when a shared library is built, and in
64         these cases the undocumented symbols are not exported.         these cases the undocumented symbols are not exported.
65    
66    
67  USER DOCUMENTATION  USER DOCUMENTATION
68    
69         The  user  documentation  for PCRE comprises a number of different sec-         The user documentation for PCRE comprises a number  of  different  sec-
70         tions. In the "man" format, each of these is a separate "man page".  In         tions.  In the "man" format, each of these is a separate "man page". In
71         the  HTML  format, each is a separate page, linked from the index page.         the HTML format, each is a separate page, linked from the  index  page.
72         In the plain text format, all the sections are concatenated,  for  ease         In  the  plain text format, all the sections are concatenated, for ease
73         of searching. The sections are as follows:         of searching. The sections are as follows:
74    
75           pcre              this document           pcre              this document
# Line 83  USER DOCUMENTATION Line 84  USER DOCUMENTATION
84           pcrepartial       details of the partial matching facility           pcrepartial       details of the partial matching facility
85           pcrepattern       syntax and semantics of supported           pcrepattern       syntax and semantics of supported
86                               regular expressions                               regular expressions
87             pcresyntax        quick syntax reference
88           pcreperform       discussion of performance issues           pcreperform       discussion of performance issues
89           pcreposix         the POSIX-compatible C API           pcreposix         the POSIX-compatible C API
90           pcreprecompile    details of saving and re-using precompiled patterns           pcreprecompile    details of saving and re-using precompiled patterns
# Line 90  USER DOCUMENTATION Line 92  USER DOCUMENTATION
92           pcrestack         discussion of stack usage           pcrestack         discussion of stack usage
93           pcretest          description of the pcretest testing command           pcretest          description of the pcretest testing command
94    
95         In addition, in the "man" and HTML formats, there is a short  page  for         In  addition,  in the "man" and HTML formats, there is a short page for
96         each C library function, listing its arguments and results.         each C library function, listing its arguments and results.
97    
98    
99  LIMITATIONS  LIMITATIONS
100    
101         There  are some size limitations in PCRE but it is hoped that they will         There are some size limitations in PCRE but it is hoped that they  will
102         never in practice be relevant.         never in practice be relevant.
103    
104         The maximum length of a compiled pattern is 65539 (sic) bytes  if  PCRE         The  maximum  length of a compiled pattern is 65539 (sic) bytes if PCRE
105         is compiled with the default internal linkage size of 2. If you want to         is compiled with the default internal linkage size of 2. If you want to
106         process regular expressions that are truly enormous,  you  can  compile         process  regular  expressions  that are truly enormous, you can compile
107         PCRE  with  an  internal linkage size of 3 or 4 (see the README file in         PCRE with an internal linkage size of 3 or 4 (see the  README  file  in
108         the source distribution and the pcrebuild documentation  for  details).         the  source  distribution and the pcrebuild documentation for details).
109         In  these  cases the limit is substantially larger.  However, the speed         In these cases the limit is substantially larger.  However,  the  speed
110         of execution is slower.         of execution is slower.
111    
112         All values in repeating quantifiers must be less than 65536. The  maxi-         All values in repeating quantifiers must be less than 65536.
        mum  compiled  length  of  subpattern  with an explicit repeat count is  
        30000 bytes. The maximum number of capturing subpatterns is 65535.  
113    
114         There is no limit to the number of parenthesized subpatterns, but there         There is no limit to the number of parenthesized subpatterns, but there
115         can be no more than 65535 capturing subpatterns.         can be no more than 65535 capturing subpatterns.
# Line 117  LIMITATIONS Line 117  LIMITATIONS
117         The maximum length of name for a named subpattern is 32 characters, and         The maximum length of name for a named subpattern is 32 characters, and
118         the maximum number of named subpatterns is 10000.         the maximum number of named subpatterns is 10000.
119    
120         The maximum length of a subject string is the largest  positive  number         The  maximum  length of a subject string is the largest positive number
121         that  an integer variable can hold. However, when using the traditional         that an integer variable can hold. However, when using the  traditional
122         matching function, PCRE uses recursion to handle subpatterns and indef-         matching function, PCRE uses recursion to handle subpatterns and indef-
123         inite  repetition.  This means that the available stack space may limit         inite repetition.  This means that the available stack space may  limit
124         the size of a subject string that can be processed by certain patterns.         the size of a subject string that can be processed by certain patterns.
125         For a discussion of stack issues, see the pcrestack documentation.         For a discussion of stack issues, see the pcrestack documentation.
126    
127    
128  UTF-8 AND UNICODE PROPERTY SUPPORT  UTF-8 AND UNICODE PROPERTY SUPPORT
129    
130         From  release  3.3,  PCRE  has  had  some support for character strings         From release 3.3, PCRE has  had  some  support  for  character  strings
131         encoded in the UTF-8 format. For release 4.0 this was greatly  extended         encoded  in the UTF-8 format. For release 4.0 this was greatly extended
132         to  cover  most common requirements, and in release 5.0 additional sup-         to cover most common requirements, and in release 5.0  additional  sup-
133         port for Unicode general category properties was added.         port for Unicode general category properties was added.
134    
135         In order process UTF-8 strings, you must build PCRE  to  include  UTF-8         In  order  process  UTF-8 strings, you must build PCRE to include UTF-8
136         support  in  the  code,  and, in addition, you must call pcre_compile()         support in the code, and, in addition,  you  must  call  pcre_compile()
137         with the PCRE_UTF8 option flag. When you do this, both the pattern  and         with  the PCRE_UTF8 option flag. When you do this, both the pattern and
138         any  subject  strings  that are matched against it are treated as UTF-8         any subject strings that are matched against it are  treated  as  UTF-8
139         strings instead of just strings of bytes.         strings instead of just strings of bytes.
140    
141         If you compile PCRE with UTF-8 support, but do not use it at run  time,         If  you compile PCRE with UTF-8 support, but do not use it at run time,
142         the  library will be a bit bigger, but the additional run time overhead         the library will be a bit bigger, but the additional run time  overhead
143         is limited to testing the PCRE_UTF8 flag occasionally, so should not be         is limited to testing the PCRE_UTF8 flag occasionally, so should not be
144         very big.         very big.
145    
146         If PCRE is built with Unicode character property support (which implies         If PCRE is built with Unicode character property support (which implies
147         UTF-8 support), the escape sequences \p{..}, \P{..}, and  \X  are  sup-         UTF-8  support),  the  escape sequences \p{..}, \P{..}, and \X are sup-
148         ported.  The available properties that can be tested are limited to the         ported.  The available properties that can be tested are limited to the
149         general category properties such as Lu for an upper case letter  or  Nd         general  category  properties such as Lu for an upper case letter or Nd
150         for  a  decimal number, the Unicode script names such as Arabic or Han,         for a decimal number, the Unicode script names such as Arabic  or  Han,
151         and the derived properties Any and L&. A full  list  is  given  in  the         and  the  derived  properties  Any  and L&. A full list is given in the
152         pcrepattern documentation. Only the short names for properties are sup-         pcrepattern documentation. Only the short names for properties are sup-
153         ported. For example, \p{L} matches a letter. Its Perl synonym,  \p{Let-         ported.  For example, \p{L} matches a letter. Its Perl synonym, \p{Let-
154         ter},  is  not  supported.   Furthermore,  in Perl, many properties may         ter}, is not supported.  Furthermore,  in  Perl,  many  properties  may
155         optionally be prefixed by "Is", for compatibility with Perl  5.6.  PCRE         optionally  be  prefixed by "Is", for compatibility with Perl 5.6. PCRE
156         does not support this.         does not support this.
157    
158         The following comments apply when PCRE is running in UTF-8 mode:     Validity of UTF-8 strings
159    
160           When you set the PCRE_UTF8 flag, the strings  passed  as  patterns  and
161           subjects are (by default) checked for validity on entry to the relevant
162           functions. From release 7.3 of PCRE, the check is according  the  rules
163           of  RFC  3629, which are themselves derived from the Unicode specifica-
164           tion. Earlier releases of PCRE followed the rules of  RFC  2279,  which
165           allows  the  full range of 31-bit values (0 to 0x7FFFFFFF). The current
166           check allows only values in the range U+0 to U+10FFFF, excluding U+D800
167           to U+DFFF.
168    
169           The  excluded  code  points are the "Low Surrogate Area" of Unicode, of
170           which the Unicode Standard says this: "The Low Surrogate Area does  not
171           contain  any  character  assignments,  consequently  no  character code
172           charts or namelists are provided for this area. Surrogates are reserved
173           for  use  with  UTF-16 and then must be used in pairs." The code points
174           that are encoded by UTF-16 pairs  are  available  as  independent  code
175           points  in  the  UTF-8  encoding.  (In other words, the whole surrogate
176           thing is a fudge for UTF-16 which unfortunately messes up UTF-8.)
177    
178           If an  invalid  UTF-8  string  is  passed  to  PCRE,  an  error  return
179           (PCRE_ERROR_BADUTF8) is given. In some situations, you may already know
180           that your strings are valid, and therefore want to skip these checks in
181           order to improve performance. If you set the PCRE_NO_UTF8_CHECK flag at
182           compile time or at run time, PCRE assumes that the pattern  or  subject
183           it  is  given  (respectively)  contains only valid UTF-8 codes. In this
184           case, it does not diagnose an invalid UTF-8 string.
185    
186           If you pass an invalid UTF-8 string  when  PCRE_NO_UTF8_CHECK  is  set,
187           what  happens  depends on why the string is invalid. If the string con-
188           forms to the "old" definition of UTF-8 (RFC 2279), it is processed as a
189           string  of  characters  in  the  range 0 to 0x7FFFFFFF. In other words,
190           apart from the initial validity test, PCRE (when in UTF-8 mode) handles
191           strings  according  to  the more liberal rules of RFC 2279. However, if
192           the string does not even conform to RFC 2279, the result is  undefined.
193           Your program may crash.
194    
195           If  you  want  to  process  strings  of  values  in the full range 0 to
196           0x7FFFFFFF, encoded in a UTF-8-like manner as per the old RFC, you  can
197           set PCRE_NO_UTF8_CHECK to bypass the more restrictive test. However, in
198           this situation, you will have to apply your own validity check.
199    
200         1.  When you set the PCRE_UTF8 flag, the strings passed as patterns and     General comments about UTF-8 mode
        subjects are checked for validity on entry to the  relevant  functions.  
        If an invalid UTF-8 string is passed, an error return is given. In some  
        situations, you may already know  that  your  strings  are  valid,  and  
        therefore want to skip these checks in order to improve performance. If  
        you set the PCRE_NO_UTF8_CHECK flag at compile time  or  at  run  time,  
        PCRE  assumes  that  the  pattern or subject it is given (respectively)  
        contains only valid UTF-8 codes. In this case, it does not diagnose  an  
        invalid  UTF-8 string. If you pass an invalid UTF-8 string to PCRE when  
        PCRE_NO_UTF8_CHECK is set, the results are undefined. Your program  may  
        crash.  
201    
202         2.  An  unbraced  hexadecimal  escape sequence (such as \xb3) matches a         1. An unbraced hexadecimal escape sequence (such  as  \xb3)  matches  a
203         two-byte UTF-8 character if the value is greater than 127.         two-byte UTF-8 character if the value is greater than 127.
204    
205         3. Octal numbers up to \777 are recognized, and  match  two-byte  UTF-8         2.  Octal  numbers  up to \777 are recognized, and match two-byte UTF-8
206         characters for values greater than \177.         characters for values greater than \177.
207    
208         4.  Repeat quantifiers apply to complete UTF-8 characters, not to indi-         3. Repeat quantifiers apply to complete UTF-8 characters, not to  indi-
209         vidual bytes, for example: \x{100}{3}.         vidual bytes, for example: \x{100}{3}.
210    
211         5. The dot metacharacter matches one UTF-8 character instead of a  sin-         4.  The dot metacharacter matches one UTF-8 character instead of a sin-
212         gle byte.         gle byte.
213    
214         6.  The  escape sequence \C can be used to match a single byte in UTF-8         5. The escape sequence \C can be used to match a single byte  in  UTF-8
215         mode, but its use can lead to some strange effects.  This  facility  is         mode,  but  its  use can lead to some strange effects. This facility is
216         not available in the alternative matching function, pcre_dfa_exec().         not available in the alternative matching function, pcre_dfa_exec().
217    
218         7.  The  character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly         6. The character escapes \b, \B, \d, \D, \s, \S, \w, and  \W  correctly
219         test characters of any code value, but the characters that PCRE  recog-         test  characters of any code value, but the characters that PCRE recog-
220         nizes  as  digits,  spaces,  or  word characters remain the same set as         nizes as digits, spaces, or word characters  remain  the  same  set  as
221         before, all with values less than 256. This remains true even when PCRE         before, all with values less than 256. This remains true even when PCRE
222         includes  Unicode  property support, because to do otherwise would slow         includes Unicode property support, because to do otherwise  would  slow
223         down PCRE in many common cases. If you really want to test for a  wider         down  PCRE in many common cases. If you really want to test for a wider
224         sense  of,  say,  "digit",  you must use Unicode property tests such as         sense of, say, "digit", you must use Unicode  property  tests  such  as
225         \p{Nd}.         \p{Nd}.
226    
227         8. Similarly, characters that match the POSIX named  character  classes         7.  Similarly,  characters that match the POSIX named character classes
228         are all low-valued characters.         are all low-valued characters.
229    
230         9.  Case-insensitive  matching  applies only to characters whose values         8. However, the Perl 5.10 horizontal and vertical  whitespace  matching
231         are less than 128, unless PCRE is built with Unicode property  support.         escapes (\h, \H, \v, and \V) do match all the appropriate Unicode char-
232         Even  when  Unicode  property support is available, PCRE still uses its         acters.
233         own character tables when checking the case of  low-valued  characters,  
234         so  as not to degrade performance.  The Unicode property information is         9. Case-insensitive matching applies only to  characters  whose  values
235           are  less than 128, unless PCRE is built with Unicode property support.
236           Even when Unicode property support is available, PCRE  still  uses  its
237           own  character  tables when checking the case of low-valued characters,
238           so as not to degrade performance.  The Unicode property information  is
239         used only for characters with higher values. Even when Unicode property         used only for characters with higher values. Even when Unicode property
240         support is available, PCRE supports case-insensitive matching only when         support is available, PCRE supports case-insensitive matching only when
241         there is a one-to-one mapping between a letter's  cases.  There  are  a         there  is  a  one-to-one  mapping between a letter's cases. There are a
242         small  number  of  many-to-one  mappings in Unicode; these are not sup-         small number of many-to-one mappings in Unicode;  these  are  not  sup-
243         ported by PCRE.         ported by PCRE.
244    
245    
# Line 215  AUTHOR Line 249  AUTHOR
249         University Computing Service         University Computing Service
250         Cambridge CB2 3QH, England.         Cambridge CB2 3QH, England.
251    
252         Putting an actual email address here seems to have been a spam  magnet,         Putting  an actual email address here seems to have been a spam magnet,
253         so  I've  taken  it away. If you want to email me, use my two initials,         so I've taken it away. If you want to email me, use  my  two  initials,
254         followed by the two digits 10, at the domain cam.ac.uk.         followed by the two digits 10, at the domain cam.ac.uk.
255    
256    
257  REVISION  REVISION
258    
259         Last updated: 18 April 2007         Last updated: 09 August 2007
260         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
261  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
262    
# Line 390  AVOIDING EXCESSIVE STACK USAGE Line 424  AVOIDING EXCESSIVE STACK USAGE
424    
425         to  the  configure  command. With this configuration, PCRE will use the         to  the  configure  command. With this configuration, PCRE will use the
426         pcre_stack_malloc and pcre_stack_free variables to call memory  manage-         pcre_stack_malloc and pcre_stack_free variables to call memory  manage-
427         ment  functions.  Separate  functions are provided because the usage is         ment  functions. By default these point to malloc() and free(), but you
428         very predictable: the block sizes requested are always  the  same,  and         can replace the pointers so that your own functions are used.
429         the  blocks  are always freed in reverse order. A calling program might  
430         be able to implement optimized functions that perform better  than  the         Separate functions are  provided  rather  than  using  pcre_malloc  and
431         standard  malloc()  and  free()  functions.  PCRE  runs noticeably more         pcre_free  because  the  usage  is  very  predictable:  the block sizes
432         slowly when built in this way. This option affects only the pcre_exec()         requested are always the same, and  the  blocks  are  always  freed  in
433         function; it is not relevant for the the pcre_dfa_exec() function.         reverse  order.  A calling program might be able to implement optimized
434           functions that perform better  than  malloc()  and  free().  PCRE  runs
435           noticeably more slowly when built in this way. This option affects only
436           the  pcre_exec()  function;  it   is   not   relevant   for   the   the
437           pcre_dfa_exec() function.
438    
439    
440  LIMITING PCRE RESOURCE USAGE  LIMITING PCRE RESOURCE USAGE
# Line 451  USING EBCDIC CODE Line 489  USING EBCDIC CODE
489    
490         PCRE  assumes  by  default that it will run in an environment where the         PCRE  assumes  by  default that it will run in an environment where the
491         character code is ASCII (or Unicode, which is  a  superset  of  ASCII).         character code is ASCII (or Unicode, which is  a  superset  of  ASCII).
492         PCRE  can,  however,  be  compiled  to  run in an EBCDIC environment by         This  is  the  case for most computer operating systems. PCRE can, how-
493         adding         ever, be compiled to run in an EBCDIC environment by adding
494    
495           --enable-ebcdic           --enable-ebcdic
496    
497         to the configure command. This setting implies --enable-rebuild-charta-         to the configure command. This setting implies --enable-rebuild-charta-
498         bles.         bles.  You  should  only  use  it if you know that you are in an EBCDIC
499           environment (for example, an IBM mainframe operating system).
500    
501    
502  SEE ALSO  SEE ALSO
# Line 474  AUTHOR Line 513  AUTHOR
513    
514  REVISION  REVISION
515    
516         Last updated: 16 April 2007         Last updated: 30 July 2007
517         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
518  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
519    
# Line 618  THE ALTERNATIVE MATCHING ALGORITHM Line 657  THE ALTERNATIVE MATCHING ALGORITHM
657         6.  Callouts  are  supported, but the value of the capture_top field is         6.  Callouts  are  supported, but the value of the capture_top field is
658         always 1, and the value of the capture_last field is always -1.         always 1, and the value of the capture_last field is always -1.
659    
660         7.  The \C escape sequence, which (in the standard algorithm) matches a         7. The \C escape sequence, which (in the standard algorithm) matches  a
661         single  byte, even in UTF-8 mode, is not supported because the alterna-         single  byte, even in UTF-8 mode, is not supported because the alterna-
662         tive algorithm moves through the subject  string  one  character  at  a         tive algorithm moves through the subject  string  one  character  at  a
663         time, for all active paths through the tree.         time, for all active paths through the tree.
664    
665           8.  None  of  the  backtracking control verbs such as (*PRUNE) are sup-
666           ported.
667    
668    
669  ADVANTAGES OF THE ALTERNATIVE ALGORITHM  ADVANTAGES OF THE ALTERNATIVE ALGORITHM
670    
671         Using  the alternative matching algorithm provides the following advan-         Using the alternative matching algorithm provides the following  advan-
672         tages:         tages:
673    
674         1. All possible matches (at a single point in the subject) are automat-         1. All possible matches (at a single point in the subject) are automat-
675         ically  found,  and  in particular, the longest match is found. To find         ically found, and in particular, the longest match is  found.  To  find
676         more than one match using the standard algorithm, you have to do kludgy         more than one match using the standard algorithm, you have to do kludgy
677         things with callouts.         things with callouts.
678    
679         2.  There is much better support for partial matching. The restrictions         2. There is much better support for partial matching. The  restrictions
680         on the content of the pattern that apply when using the standard  algo-         on  the content of the pattern that apply when using the standard algo-
681         rithm  for  partial matching do not apply to the alternative algorithm.         rithm for partial matching do not apply to the  alternative  algorithm.
682         For non-anchored patterns, the starting position of a partial match  is         For  non-anchored patterns, the starting position of a partial match is
683         available.         available.
684    
685         3.  Because  the  alternative  algorithm  scans the subject string just         3. Because the alternative algorithm  scans  the  subject  string  just
686         once, and never needs to backtrack, it is possible to  pass  very  long         once,  and  never  needs to backtrack, it is possible to pass very long
687         subject  strings  to  the matching function in several pieces, checking         subject strings to the matching function in  several  pieces,  checking
688         for partial matching each time.         for partial matching each time.
689    
690    
# Line 650  DISADVANTAGES OF THE ALTERNATIVE ALGORIT Line 692  DISADVANTAGES OF THE ALTERNATIVE ALGORIT
692    
693         The alternative algorithm suffers from a number of disadvantages:         The alternative algorithm suffers from a number of disadvantages:
694    
695         1. It is substantially slower than  the  standard  algorithm.  This  is         1.  It  is  substantially  slower  than the standard algorithm. This is
696         partly  because  it has to search for all possible matches, but is also         partly because it has to search for all possible matches, but  is  also
697         because it is less susceptible to optimization.         because it is less susceptible to optimization.
698    
699         2. Capturing parentheses and back references are not supported.         2. Capturing parentheses and back references are not supported.
# Line 669  AUTHOR Line 711  AUTHOR
711    
712  REVISION  REVISION
713    
714         Last updated: 29 May 2007         Last updated: 08 August 2007
715         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
716  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
717    
# Line 866  NEWLINES Line 908  NEWLINES
908         dard. When PCRE is run, the default can be overridden,  either  when  a         dard. When PCRE is run, the default can be overridden,  either  when  a
909         pattern is compiled, or when it is matched.         pattern is compiled, or when it is matched.
910    
911           At compile time, the newline convention can be specified by the options
912           argument of pcre_compile(), or it can be specified by special  text  at
913           the start of the pattern itself; this overrides any other settings. See
914           the pcrepattern page for details of the special character sequences.
915    
916         In the PCRE documentation the word "newline" is used to mean "the char-         In the PCRE documentation the word "newline" is used to mean "the char-
917         acter or pair of characters that indicate a line break". The choice  of         acter  or pair of characters that indicate a line break". The choice of
918         newline  convention  affects  the  handling of the dot, circumflex, and         newline convention affects the handling of  the  dot,  circumflex,  and
919         dollar metacharacters, the handling of #-comments in /x mode, and, when         dollar metacharacters, the handling of #-comments in /x mode, and, when
920         CRLF  is a recognized line ending sequence, the match position advance-         CRLF is a recognized line ending sequence, the match position  advance-
921         ment for a non-anchored pattern. The choice of newline convention  does         ment for a non-anchored pattern. There is more detail about this in the
922         not affect the interpretation of the \n or \r escape sequences.         section on pcre_exec() options below. The choice of newline  convention
923           does not affect the interpretation of the \n or \r escape sequences.
924    
925    
926  MULTITHREADING  MULTITHREADING
# Line 1213  COMPILING A PATTERN Line 1261  COMPILING A PATTERN
1261           PCRE_NO_UTF8_CHECK           PCRE_NO_UTF8_CHECK
1262    
1263         When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is         When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is
1264         automatically  checked. If an invalid UTF-8 sequence of bytes is found,         automatically  checked.  There  is  a  discussion about the validity of
1265         pcre_compile() returns an error. If you already know that your  pattern         UTF-8 strings in the main pcre page. If an invalid  UTF-8  sequence  of
1266         is  valid, and you want to skip this check for performance reasons, you         bytes  is  found,  pcre_compile() returns an error. If you already know
1267         can set the PCRE_NO_UTF8_CHECK option. When it is set,  the  effect  of         that your pattern is valid, and you want to skip this check for perfor-
1268         passing an invalid UTF-8 string as a pattern is undefined. It may cause         mance  reasons,  you  can set the PCRE_NO_UTF8_CHECK option. When it is
1269         your program to crash.  Note that this option can  also  be  passed  to         set, the effect of passing an invalid UTF-8  string  as  a  pattern  is
1270         pcre_exec()  and pcre_dfa_exec(), to suppress the UTF-8 validity check-         undefined.  It  may  cause your program to crash. Note that this option
1271         ing of subject strings.         can also be passed to pcre_exec() and pcre_dfa_exec(), to suppress  the
1272           UTF-8 validity checking of subject strings.
1273    
1274    
1275  COMPILATION ERROR CODES  COMPILATION ERROR CODES
1276    
1277         The following table lists the error  codes  than  may  be  returned  by         The  following  table  lists  the  error  codes than may be returned by
1278         pcre_compile2(),  along with the error messages that may be returned by         pcre_compile2(), along with the error messages that may be returned  by
1279         both compiling functions. As PCRE has developed, some error codes  have         both  compiling functions. As PCRE has developed, some error codes have
1280         fallen out of use. To avoid confusion, they have not been re-used.         fallen out of use. To avoid confusion, they have not been re-used.
1281    
1282            0  no error            0  no error
# Line 1259  COMPILATION ERROR CODES Line 1308  COMPILATION ERROR CODES
1308           26  malformed number or name after (?(           26  malformed number or name after (?(
1309           27  conditional group contains more than two branches           27  conditional group contains more than two branches
1310           28  assertion expected after (?(           28  assertion expected after (?(
1311           29  (?R or (?digits must be followed by )           29  (?R or (?[+-]digits must be followed by )
1312           30  unknown POSIX class name           30  unknown POSIX class name
1313           31  POSIX collating elements are not supported           31  POSIX collating elements are not supported
1314           32  this version of PCRE is not compiled with PCRE_UTF8 support           32  this version of PCRE is not compiled with PCRE_UTF8 support
# Line 1280  COMPILATION ERROR CODES Line 1329  COMPILATION ERROR CODES
1329           47  unknown property name after \P or \p           47  unknown property name after \P or \p
1330           48  subpattern name is too long (maximum 32 characters)           48  subpattern name is too long (maximum 32 characters)
1331           49  too many named subpatterns (maximum 10,000)           49  too many named subpatterns (maximum 10,000)
1332           50  repeated subpattern is too long           50  [this code is not in use]
1333           51  octal value is greater than \377 (not in UTF-8 mode)           51  octal value is greater than \377 (not in UTF-8 mode)
1334           52  internal error: overran compiling workspace           52  internal error: overran compiling workspace
1335           53   internal  error:  previously-checked  referenced  subpattern not           53  internal  error:  previously-checked  referenced  subpattern  not
1336         found         found
1337           54  DEFINE group contains more than one branch           54  DEFINE group contains more than one branch
1338           55  repeating a DEFINE group is not allowed           55  repeating a DEFINE group is not allowed
1339           56  inconsistent NEWLINE options"           56  inconsistent NEWLINE options"
1340             57  \g is not followed by a braced name or an optionally braced
1341                   non-zero number
1342             58  (?+ or (?- or (?(+ or (?(- must be followed by a non-zero number
1343    
1344    
1345  STUDYING A PATTERN  STUDYING A PATTERN
# Line 1295  STUDYING A PATTERN Line 1347  STUDYING A PATTERN
1347         pcre_extra *pcre_study(const pcre *code, int options         pcre_extra *pcre_study(const pcre *code, int options
1348              const char **errptr);              const char **errptr);
1349    
1350         If a compiled pattern is going to be used several times,  it  is  worth         If  a  compiled  pattern is going to be used several times, it is worth
1351         spending more time analyzing it in order to speed up the time taken for         spending more time analyzing it in order to speed up the time taken for
1352         matching. The function pcre_study() takes a pointer to a compiled  pat-         matching.  The function pcre_study() takes a pointer to a compiled pat-
1353         tern as its first argument. If studying the pattern produces additional         tern as its first argument. If studying the pattern produces additional
1354         information that will help speed up matching,  pcre_study()  returns  a         information  that  will  help speed up matching, pcre_study() returns a
1355         pointer  to a pcre_extra block, in which the study_data field points to         pointer to a pcre_extra block, in which the study_data field points  to
1356         the results of the study.         the results of the study.
1357    
1358         The  returned  value  from  pcre_study()  can  be  passed  directly  to         The  returned  value  from  pcre_study()  can  be  passed  directly  to
1359         pcre_exec().  However,  a  pcre_extra  block also contains other fields         pcre_exec(). However, a pcre_extra block  also  contains  other  fields
1360         that can be set by the caller before the block  is  passed;  these  are         that  can  be  set  by the caller before the block is passed; these are
1361         described below in the section on matching a pattern.         described below in the section on matching a pattern.
1362    
1363         If  studying  the  pattern  does not produce any additional information         If studying the pattern does not  produce  any  additional  information
1364         pcre_study() returns NULL. In that circumstance, if the calling program         pcre_study() returns NULL. In that circumstance, if the calling program
1365         wants  to  pass  any of the other fields to pcre_exec(), it must set up         wants to pass any of the other fields to pcre_exec(), it  must  set  up
1366         its own pcre_extra block.         its own pcre_extra block.
1367    
1368         The second argument of pcre_study() contains option bits.  At  present,         The  second  argument of pcre_study() contains option bits. At present,
1369         no options are defined, and this argument should always be zero.         no options are defined, and this argument should always be zero.
1370    
1371         The  third argument for pcre_study() is a pointer for an error message.         The third argument for pcre_study() is a pointer for an error  message.
1372         If studying succeeds (even if no data is  returned),  the  variable  it         If  studying  succeeds  (even  if no data is returned), the variable it
1373         points  to  is  set  to NULL. Otherwise it is set to point to a textual         points to is set to NULL. Otherwise it is set to  point  to  a  textual
1374         error message. This is a static string that is part of the library. You         error message. This is a static string that is part of the library. You
1375         must  not  try  to  free it. You should test the error pointer for NULL         must not try to free it. You should test the  error  pointer  for  NULL
1376         after calling pcre_study(), to be sure that it has run successfully.         after calling pcre_study(), to be sure that it has run successfully.
1377    
1378         This is a typical call to pcre_study():         This is a typical call to pcre_study():
# Line 1332  STUDYING A PATTERN Line 1384  STUDYING A PATTERN
1384             &error);        /* set to NULL or points to a message */             &error);        /* set to NULL or points to a message */
1385    
1386         At present, studying a pattern is useful only for non-anchored patterns         At present, studying a pattern is useful only for non-anchored patterns
1387         that  do not have a single fixed starting character. A bitmap of possi-         that do not have a single fixed starting character. A bitmap of  possi-
1388         ble starting bytes is created.         ble starting bytes is created.
1389    
1390    
1391  LOCALE SUPPORT  LOCALE SUPPORT
1392    
1393         PCRE handles caseless matching, and determines whether  characters  are         PCRE  handles  caseless matching, and determines whether characters are
1394         letters,  digits, or whatever, by reference to a set of tables, indexed         letters, digits, or whatever, by reference to a set of tables,  indexed
1395         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
1396         characters  with  codes  less than 128. Higher-valued codes never match         characters with codes less than 128. Higher-valued  codes  never  match
1397         escapes such as \w or \d, but can be tested with \p if  PCRE  is  built         escapes  such  as  \w or \d, but can be tested with \p if PCRE is built
1398         with  Unicode  character property support. The use of locales with Uni-         with Unicode character property support. The use of locales  with  Uni-
1399         code is discouraged. If you are handling characters with codes  greater         code  is discouraged. If you are handling characters with codes greater
1400         than  128, you should either use UTF-8 and Unicode, or use locales, but         than 128, you should either use UTF-8 and Unicode, or use locales,  but
1401         not try to mix the two.         not try to mix the two.
1402    
1403         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
1404         argument  of  pcre_compile()  is  NULL.  These  are sufficient for many         argument of pcre_compile() is  NULL.  These  are  sufficient  for  many
1405         applications.  Normally, the internal tables recognize only ASCII char-         applications.  Normally, the internal tables recognize only ASCII char-
1406         acters. However, when PCRE is built, it is possible to cause the inter-         acters. However, when PCRE is built, it is possible to cause the inter-
1407         nal tables to be rebuilt in the default "C" locale of the local system,         nal tables to be rebuilt in the default "C" locale of the local system,
1408         which may cause them to be different.         which may cause them to be different.
1409    
1410         The  internal tables can always be overridden by tables supplied by the         The internal tables can always be overridden by tables supplied by  the
1411         application that calls PCRE. These may be created in a different locale         application that calls PCRE. These may be created in a different locale
1412         from  the  default.  As more and more applications change to using Uni-         from the default. As more and more applications change  to  using  Uni-
1413         code, the need for this locale support is expected to die away.         code, the need for this locale support is expected to die away.
1414    
1415         External tables are built by calling  the  pcre_maketables()  function,         External  tables  are  built by calling the pcre_maketables() function,
1416         which  has no arguments, in the relevant locale. The result can then be         which has no arguments, in the relevant locale. The result can then  be
1417         passed to pcre_compile() or pcre_exec()  as  often  as  necessary.  For         passed  to  pcre_compile()  or  pcre_exec()  as often as necessary. For
1418         example,  to  build  and use tables that are appropriate for the French         example, to build and use tables that are appropriate  for  the  French
1419         locale (where accented characters with  values  greater  than  128  are         locale  (where  accented  characters  with  values greater than 128 are
1420         treated as letters), the following code could be used:         treated as letters), the following code could be used:
1421    
1422           setlocale(LC_CTYPE, "fr_FR");           setlocale(LC_CTYPE, "fr_FR");
1423           tables = pcre_maketables();           tables = pcre_maketables();
1424           re = pcre_compile(..., tables);           re = pcre_compile(..., tables);
1425    
1426         The  locale  name "fr_FR" is used on Linux and other Unix-like systems;         The locale name "fr_FR" is used on Linux and other  Unix-like  systems;
1427         if you are using Windows, the name for the French locale is "french".         if you are using Windows, the name for the French locale is "french".
1428    
1429         When pcre_maketables() runs, the tables are built  in  memory  that  is         When  pcre_maketables()  runs,  the  tables are built in memory that is
1430         obtained  via  pcre_malloc. It is the caller's responsibility to ensure         obtained via pcre_malloc. It is the caller's responsibility  to  ensure
1431         that the memory containing the tables remains available for as long  as         that  the memory containing the tables remains available for as long as
1432         it is needed.         it is needed.
1433    
1434         The pointer that is passed to pcre_compile() is saved with the compiled         The pointer that is passed to pcre_compile() is saved with the compiled
1435         pattern, and the same tables are used via this pointer by  pcre_study()         pattern,  and the same tables are used via this pointer by pcre_study()
1436         and normally also by pcre_exec(). Thus, by default, for any single pat-         and normally also by pcre_exec(). Thus, by default, for any single pat-
1437         tern, compilation, studying and matching all happen in the same locale,         tern, compilation, studying and matching all happen in the same locale,
1438         but different patterns can be compiled in different locales.         but different patterns can be compiled in different locales.
1439    
1440         It  is  possible to pass a table pointer or NULL (indicating the use of         It is possible to pass a table pointer or NULL (indicating the  use  of
1441         the internal tables) to pcre_exec(). Although  not  intended  for  this         the  internal  tables)  to  pcre_exec(). Although not intended for this
1442         purpose,  this facility could be used to match a pattern in a different         purpose, this facility could be used to match a pattern in a  different
1443         locale from the one in which it was compiled. Passing table pointers at         locale from the one in which it was compiled. Passing table pointers at
1444         run time is discussed below in the section on matching a pattern.         run time is discussed below in the section on matching a pattern.
1445    
# Line 1397  INFORMATION ABOUT A PATTERN Line 1449  INFORMATION ABOUT A PATTERN
1449         int pcre_fullinfo(const pcre *code, const pcre_extra *extra,         int pcre_fullinfo(const pcre *code, const pcre_extra *extra,
1450              int what, void *where);              int what, void *where);
1451    
1452         The  pcre_fullinfo() function returns information about a compiled pat-         The pcre_fullinfo() function returns information about a compiled  pat-
1453         tern. It replaces the obsolete pcre_info() function, which is neverthe-         tern. It replaces the obsolete pcre_info() function, which is neverthe-
1454         less retained for backwards compability (and is documented below).         less retained for backwards compability (and is documented below).
1455    
1456         The  first  argument  for  pcre_fullinfo() is a pointer to the compiled         The first argument for pcre_fullinfo() is a  pointer  to  the  compiled
1457         pattern. The second argument is the result of pcre_study(), or NULL  if         pattern.  The second argument is the result of pcre_study(), or NULL if
1458         the  pattern  was not studied. The third argument specifies which piece         the pattern was not studied. The third argument specifies  which  piece
1459         of information is required, and the fourth argument is a pointer  to  a         of  information  is required, and the fourth argument is a pointer to a
1460         variable  to  receive  the  data. The yield of the function is zero for         variable to receive the data. The yield of the  function  is  zero  for
1461         success, or one of the following negative numbers:         success, or one of the following negative numbers:
1462    
1463           PCRE_ERROR_NULL       the argument code was NULL           PCRE_ERROR_NULL       the argument code was NULL
# Line 1413  INFORMATION ABOUT A PATTERN Line 1465  INFORMATION ABOUT A PATTERN
1465           PCRE_ERROR_BADMAGIC   the "magic number" was not found           PCRE_ERROR_BADMAGIC   the "magic number" was not found
1466           PCRE_ERROR_BADOPTION  the value of what was invalid           PCRE_ERROR_BADOPTION  the value of what was invalid
1467    
1468         The "magic number" is placed at the start of each compiled  pattern  as         The  "magic  number" is placed at the start of each compiled pattern as
1469         an  simple check against passing an arbitrary memory pointer. Here is a         an simple check against passing an arbitrary memory pointer. Here is  a
1470         typical call of pcre_fullinfo(), to obtain the length of  the  compiled         typical  call  of pcre_fullinfo(), to obtain the length of the compiled
1471         pattern:         pattern:
1472    
1473           int rc;           int rc;
# Line 1426  INFORMATION ABOUT A PATTERN Line 1478  INFORMATION ABOUT A PATTERN
1478             PCRE_INFO_SIZE,   /* what is required */             PCRE_INFO_SIZE,   /* what is required */
1479             &length);         /* where to put the data */             &length);         /* where to put the data */
1480    
1481         The  possible  values for the third argument are defined in pcre.h, and         The possible values for the third argument are defined in  pcre.h,  and
1482         are as follows:         are as follows:
1483    
1484           PCRE_INFO_BACKREFMAX           PCRE_INFO_BACKREFMAX
1485    
1486         Return the number of the highest back reference  in  the  pattern.  The         Return  the  number  of  the highest back reference in the pattern. The
1487         fourth  argument  should  point to an int variable. Zero is returned if         fourth argument should point to an int variable. Zero  is  returned  if
1488         there are no back references.         there are no back references.
1489    
1490           PCRE_INFO_CAPTURECOUNT           PCRE_INFO_CAPTURECOUNT
1491    
1492         Return the number of capturing subpatterns in the pattern.  The  fourth         Return  the  number of capturing subpatterns in the pattern. The fourth
1493         argument should point to an int variable.         argument should point to an int variable.
1494    
1495           PCRE_INFO_DEFAULT_TABLES           PCRE_INFO_DEFAULT_TABLES
1496    
1497         Return  a pointer to the internal default character tables within PCRE.         Return a pointer to the internal default character tables within  PCRE.
1498         The fourth argument should point to an unsigned char *  variable.  This         The  fourth  argument should point to an unsigned char * variable. This
1499         information call is provided for internal use by the pcre_study() func-         information call is provided for internal use by the pcre_study() func-
1500         tion. External callers can cause PCRE to use  its  internal  tables  by         tion.  External  callers  can  cause PCRE to use its internal tables by
1501         passing a NULL table pointer.         passing a NULL table pointer.
1502    
1503           PCRE_INFO_FIRSTBYTE           PCRE_INFO_FIRSTBYTE
1504    
1505         Return  information  about  the first byte of any matched string, for a         Return information about the first byte of any matched  string,  for  a
1506         non-anchored pattern. The fourth argument should point to an int  vari-         non-anchored  pattern. The fourth argument should point to an int vari-
1507         able.  (This option used to be called PCRE_INFO_FIRSTCHAR; the old name         able. (This option used to be called PCRE_INFO_FIRSTCHAR; the old  name
1508         is still recognized for backwards compatibility.)         is still recognized for backwards compatibility.)
1509    
1510         If there is a fixed first byte, for example, from  a  pattern  such  as         If  there  is  a  fixed first byte, for example, from a pattern such as
1511         (cat|cow|coyote), its value is returned. Otherwise, if either         (cat|cow|coyote), its value is returned. Otherwise, if either
1512    
1513         (a)  the pattern was compiled with the PCRE_MULTILINE option, and every         (a) the pattern was compiled with the PCRE_MULTILINE option, and  every
1514         branch starts with "^", or         branch starts with "^", or
1515    
1516         (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not         (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not
1517         set (if it were set, the pattern would be anchored),         set (if it were set, the pattern would be anchored),
1518    
1519         -1  is  returned, indicating that the pattern matches only at the start         -1 is returned, indicating that the pattern matches only at  the  start
1520         of a subject string or after any newline within the  string.  Otherwise         of  a  subject string or after any newline within the string. Otherwise
1521         -2 is returned. For anchored patterns, -2 is returned.         -2 is returned. For anchored patterns, -2 is returned.
1522    
1523           PCRE_INFO_FIRSTTABLE           PCRE_INFO_FIRSTTABLE
1524    
1525         If  the pattern was studied, and this resulted in the construction of a         If the pattern was studied, and this resulted in the construction of  a
1526         256-bit table indicating a fixed set of bytes for the first byte in any         256-bit table indicating a fixed set of bytes for the first byte in any
1527         matching  string, a pointer to the table is returned. Otherwise NULL is         matching string, a pointer to the table is returned. Otherwise NULL  is
1528         returned. The fourth argument should point to an unsigned char *  vari-         returned.  The fourth argument should point to an unsigned char * vari-
1529         able.         able.
1530    
1531             PCRE_INFO_HASCRORLF
1532    
1533           Return 1 if the pattern contains any explicit  matches  for  CR  or  LF
1534           characters,  otherwise  0.  The  fourth argument should point to an int
1535           variable.
1536    
1537           PCRE_INFO_JCHANGED           PCRE_INFO_JCHANGED
1538    
1539         Return  1  if the (?J) option setting is used in the pattern, otherwise         Return 1 if the (?J) option setting is used in the  pattern,  otherwise
1540         0. The fourth argument should point to an int variable. The (?J) inter-         0. The fourth argument should point to an int variable. The (?J) inter-
1541         nal option setting changes the local PCRE_DUPNAMES value.         nal option setting changes the local PCRE_DUPNAMES option.
1542    
1543           PCRE_INFO_LASTLITERAL           PCRE_INFO_LASTLITERAL
1544    
1545         Return  the  value of the rightmost literal byte that must exist in any         Return the value of the rightmost literal byte that must exist  in  any
1546         matched string, other than at its  start,  if  such  a  byte  has  been         matched  string,  other  than  at  its  start,  if such a byte has been
1547         recorded. The fourth argument should point to an int variable. If there         recorded. The fourth argument should point to an int variable. If there
1548         is no such byte, -1 is returned. For anchored patterns, a last  literal         is  no such byte, -1 is returned. For anchored patterns, a last literal
1549         byte  is  recorded only if it follows something of variable length. For         byte is recorded only if it follows something of variable  length.  For
1550         example, for the pattern /^a\d+z\d+/ the returned value is "z", but for         example, for the pattern /^a\d+z\d+/ the returned value is "z", but for
1551         /^a\dz\d/ the returned value is -1.         /^a\dz\d/ the returned value is -1.
1552    
# Line 1496  INFORMATION ABOUT A PATTERN Line 1554  INFORMATION ABOUT A PATTERN
1554           PCRE_INFO_NAMEENTRYSIZE           PCRE_INFO_NAMEENTRYSIZE
1555           PCRE_INFO_NAMETABLE           PCRE_INFO_NAMETABLE
1556    
1557         PCRE  supports the use of named as well as numbered capturing parenthe-         PCRE supports the use of named as well as numbered capturing  parenthe-
1558         ses. The names are just an additional way of identifying the  parenthe-         ses.  The names are just an additional way of identifying the parenthe-
1559         ses, which still acquire numbers. Several convenience functions such as         ses, which still acquire numbers. Several convenience functions such as
1560         pcre_get_named_substring() are provided for  extracting  captured  sub-         pcre_get_named_substring()  are  provided  for extracting captured sub-
1561         strings  by  name. It is also possible to extract the data directly, by         strings by name. It is also possible to extract the data  directly,  by
1562         first converting the name to a number in order to  access  the  correct         first  converting  the  name to a number in order to access the correct
1563         pointers in the output vector (described with pcre_exec() below). To do         pointers in the output vector (described with pcre_exec() below). To do
1564         the conversion, you need  to  use  the  name-to-number  map,  which  is         the  conversion,  you  need  to  use  the  name-to-number map, which is
1565         described by these three values.         described by these three values.
1566    
1567         The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT         The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT
1568         gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size         gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size
1569         of  each  entry;  both  of  these  return  an int value. The entry size         of each entry; both of these  return  an  int  value.  The  entry  size
1570         depends on the length of the longest name. PCRE_INFO_NAMETABLE  returns         depends  on the length of the longest name. PCRE_INFO_NAMETABLE returns
1571         a  pointer  to  the  first  entry of the table (a pointer to char). The         a pointer to the first entry of the table  (a  pointer  to  char).  The
1572         first two bytes of each entry are the number of the capturing parenthe-         first two bytes of each entry are the number of the capturing parenthe-
1573         sis,  most  significant byte first. The rest of the entry is the corre-         sis, most significant byte first. The rest of the entry is  the  corre-
1574         sponding name, zero terminated. The names are  in  alphabetical  order.         sponding  name,  zero  terminated. The names are in alphabetical order.
1575         When PCRE_DUPNAMES is set, duplicate names are in order of their paren-         When PCRE_DUPNAMES is set, duplicate names are in order of their paren-
1576         theses numbers. For example, consider  the  following  pattern  (assume         theses  numbers.  For  example,  consider the following pattern (assume
1577         PCRE_EXTENDED  is  set,  so  white  space  -  including  newlines  - is         PCRE_EXTENDED is  set,  so  white  space  -  including  newlines  -  is
1578         ignored):         ignored):
1579    
1580           (?<date> (?<year>(\d\d)?\d\d) -           (?<date> (?<year>(\d\d)?\d\d) -
1581           (?<month>\d\d) - (?<day>\d\d) )           (?<month>\d\d) - (?<day>\d\d) )
1582    
1583         There are four named subpatterns, so the table has  four  entries,  and         There  are  four  named subpatterns, so the table has four entries, and
1584         each  entry  in the table is eight bytes long. The table is as follows,         each entry in the table is eight bytes long. The table is  as  follows,
1585         with non-printing bytes shows in hexadecimal, and undefined bytes shown         with non-printing bytes shows in hexadecimal, and undefined bytes shown
1586         as ??:         as ??:
1587    
# Line 1532  INFORMATION ABOUT A PATTERN Line 1590  INFORMATION ABOUT A PATTERN
1590           00 04 m  o  n  t  h  00           00 04 m  o  n  t  h  00
1591           00 02 y  e  a  r  00 ??           00 02 y  e  a  r  00 ??
1592    
1593         When  writing  code  to  extract  data from named subpatterns using the         When writing code to extract data  from  named  subpatterns  using  the
1594         name-to-number map, remember that the length of the entries  is  likely         name-to-number  map,  remember that the length of the entries is likely
1595         to be different for each compiled pattern.         to be different for each compiled pattern.
1596    
1597           PCRE_INFO_OKPARTIAL           PCRE_INFO_OKPARTIAL
1598    
1599         Return  1 if the pattern can be used for partial matching, otherwise 0.         Return 1 if the pattern can be used for partial matching, otherwise  0.
1600         The fourth argument should point to an int  variable.  The  pcrepartial         The  fourth  argument  should point to an int variable. The pcrepartial
1601         documentation  lists  the restrictions that apply to patterns when par-         documentation lists the restrictions that apply to patterns  when  par-
1602         tial matching is used.         tial matching is used.
1603    
1604           PCRE_INFO_OPTIONS           PCRE_INFO_OPTIONS
1605    
1606         Return a copy of the options with which the pattern was  compiled.  The         Return  a  copy of the options with which the pattern was compiled. The
1607         fourth  argument  should  point to an unsigned long int variable. These         fourth argument should point to an unsigned long  int  variable.  These
1608         option bits are those specified in the call to pcre_compile(), modified         option bits are those specified in the call to pcre_compile(), modified
1609         by any top-level option settings within the pattern itself.         by any top-level option settings at the start of the pattern itself. In
1610           other  words,  they are the options that will be in force when matching
1611           starts. For example, if the pattern /(?im)abc(?-i)d/ is  compiled  with
1612           the  PCRE_EXTENDED option, the result is PCRE_CASELESS, PCRE_MULTILINE,
1613           and PCRE_EXTENDED.
1614    
1615         A  pattern  is  automatically  anchored by PCRE if all of its top-level         A pattern is automatically anchored by PCRE if  all  of  its  top-level
1616         alternatives begin with one of the following:         alternatives begin with one of the following:
1617    
1618           ^     unless PCRE_MULTILINE is set           ^     unless PCRE_MULTILINE is set
# Line 1564  INFORMATION ABOUT A PATTERN Line 1626  INFORMATION ABOUT A PATTERN
1626    
1627           PCRE_INFO_SIZE           PCRE_INFO_SIZE
1628    
1629         Return  the  size  of the compiled pattern, that is, the value that was         Return the size of the compiled pattern, that is, the  value  that  was
1630         passed as the argument to pcre_malloc() when PCRE was getting memory in         passed as the argument to pcre_malloc() when PCRE was getting memory in
1631         which to place the compiled data. The fourth argument should point to a         which to place the compiled data. The fourth argument should point to a
1632         size_t variable.         size_t variable.
# Line 1572  INFORMATION ABOUT A PATTERN Line 1634  INFORMATION ABOUT A PATTERN
1634           PCRE_INFO_STUDYSIZE           PCRE_INFO_STUDYSIZE
1635    
1636         Return the size of the data block pointed to by the study_data field in         Return the size of the data block pointed to by the study_data field in
1637         a  pcre_extra  block.  That  is,  it  is  the  value that was passed to         a pcre_extra block. That is,  it  is  the  value  that  was  passed  to
1638         pcre_malloc() when PCRE was getting memory into which to place the data         pcre_malloc() when PCRE was getting memory into which to place the data
1639         created  by  pcre_study(). The fourth argument should point to a size_t         created by pcre_study(). The fourth argument should point to  a  size_t
1640         variable.         variable.
1641    
1642    
# Line 1582  OBSOLETE INFO FUNCTION Line 1644  OBSOLETE INFO FUNCTION
1644    
1645         int pcre_info(const pcre *code, int *optptr, int *firstcharptr);         int pcre_info(const pcre *code, int *optptr, int *firstcharptr);
1646    
1647         The pcre_info() function is now obsolete because its interface  is  too         The  pcre_info()  function is now obsolete because its interface is too
1648         restrictive  to return all the available data about a compiled pattern.         restrictive to return all the available data about a compiled  pattern.
1649         New  programs  should  use  pcre_fullinfo()  instead.  The   yield   of         New   programs   should  use  pcre_fullinfo()  instead.  The  yield  of
1650         pcre_info()  is the number of capturing subpatterns, or one of the fol-         pcre_info() is the number of capturing subpatterns, or one of the  fol-
1651         lowing negative numbers:         lowing negative numbers:
1652    
1653           PCRE_ERROR_NULL       the argument code was NULL           PCRE_ERROR_NULL       the argument code was NULL
1654           PCRE_ERROR_BADMAGIC   the "magic number" was not found           PCRE_ERROR_BADMAGIC   the "magic number" was not found
1655    
1656         If the optptr argument is not NULL, a copy of the  options  with  which         If  the  optptr  argument is not NULL, a copy of the options with which
1657         the  pattern  was  compiled  is placed in the integer it points to (see         the pattern was compiled is placed in the integer  it  points  to  (see
1658         PCRE_INFO_OPTIONS above).         PCRE_INFO_OPTIONS above).
1659    
1660         If the pattern is not anchored and the  firstcharptr  argument  is  not         If  the  pattern  is  not anchored and the firstcharptr argument is not
1661         NULL,  it is used to pass back information about the first character of         NULL, it is used to pass back information about the first character  of
1662         any matched string (see PCRE_INFO_FIRSTBYTE above).         any matched string (see PCRE_INFO_FIRSTBYTE above).
1663    
1664    
# Line 1604  REFERENCE COUNTS Line 1666  REFERENCE COUNTS
1666    
1667         int pcre_refcount(pcre *code, int adjust);         int pcre_refcount(pcre *code, int adjust);
1668    
1669         The pcre_refcount() function is used to maintain a reference  count  in         The  pcre_refcount()  function is used to maintain a reference count in
1670         the data block that contains a compiled pattern. It is provided for the         the data block that contains a compiled pattern. It is provided for the
1671         benefit of applications that  operate  in  an  object-oriented  manner,         benefit  of  applications  that  operate  in an object-oriented manner,
1672         where different parts of the application may be using the same compiled         where different parts of the application may be using the same compiled
1673         pattern, but you want to free the block when they are all done.         pattern, but you want to free the block when they are all done.
1674    
1675         When a pattern is compiled, the reference count field is initialized to         When a pattern is compiled, the reference count field is initialized to
1676         zero.   It is changed only by calling this function, whose action is to         zero.  It is changed only by calling this function, whose action is  to
1677         add the adjust value (which may be positive or  negative)  to  it.  The         add  the  adjust  value  (which may be positive or negative) to it. The
1678         yield of the function is the new value. However, the value of the count         yield of the function is the new value. However, the value of the count
1679         is constrained to lie between 0 and 65535, inclusive. If the new  value         is  constrained to lie between 0 and 65535, inclusive. If the new value
1680         is outside these limits, it is forced to the appropriate limit value.         is outside these limits, it is forced to the appropriate limit value.
1681    
1682         Except  when it is zero, the reference count is not correctly preserved         Except when it is zero, the reference count is not correctly  preserved
1683         if a pattern is compiled on one host and then  transferred  to  a  host         if  a  pattern  is  compiled on one host and then transferred to a host
1684         whose byte-order is different. (This seems a highly unlikely scenario.)         whose byte-order is different. (This seems a highly unlikely scenario.)
1685    
1686    
# Line 1628  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1690  MATCHING A PATTERN: THE TRADITIONAL FUNC
1690              const char *subject, int length, int startoffset,              const char *subject, int length, int startoffset,
1691              int options, int *ovector, int ovecsize);              int options, int *ovector, int ovecsize);
1692    
1693         The function pcre_exec() is called to match a subject string against  a         The  function pcre_exec() is called to match a subject string against a
1694         compiled  pattern, which is passed in the code argument. If the pattern         compiled pattern, which is passed in the code argument. If the  pattern
1695         has been studied, the result of the study should be passed in the extra         has been studied, the result of the study should be passed in the extra
1696         argument.  This  function is the main matching facility of the library,         argument. This function is the main matching facility of  the  library,
1697         and it operates in a Perl-like manner. For specialist use there is also         and it operates in a Perl-like manner. For specialist use there is also
1698         an  alternative matching function, which is described below in the sec-         an alternative matching function, which is described below in the  sec-
1699         tion about the pcre_dfa_exec() function.         tion about the pcre_dfa_exec() function.
1700    
1701         In most applications, the pattern will have been compiled (and  option-         In  most applications, the pattern will have been compiled (and option-
1702         ally  studied)  in the same process that calls pcre_exec(). However, it         ally studied) in the same process that calls pcre_exec().  However,  it
1703         is possible to save compiled patterns and study data, and then use them         is possible to save compiled patterns and study data, and then use them
1704         later  in  different processes, possibly even on different hosts. For a         later in different processes, possibly even on different hosts.  For  a
1705         discussion about this, see the pcreprecompile documentation.         discussion about this, see the pcreprecompile documentation.
1706    
1707         Here is an example of a simple call to pcre_exec():         Here is an example of a simple call to pcre_exec():
# Line 1658  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1720  MATCHING A PATTERN: THE TRADITIONAL FUNC
1720    
1721     Extra data for pcre_exec()     Extra data for pcre_exec()
1722    
1723         If the extra argument is not NULL, it must point to a  pcre_extra  data         If  the  extra argument is not NULL, it must point to a pcre_extra data
1724         block.  The pcre_study() function returns such a block (when it doesn't         block. The pcre_study() function returns such a block (when it  doesn't
1725         return NULL), but you can also create one for yourself, and pass  addi-         return  NULL), but you can also create one for yourself, and pass addi-
1726         tional  information  in it. The pcre_extra block contains the following         tional information in it. The pcre_extra block contains  the  following
1727         fields (not necessarily in this order):         fields (not necessarily in this order):
1728    
1729           unsigned long int flags;           unsigned long int flags;
# Line 1671  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1733  MATCHING A PATTERN: THE TRADITIONAL FUNC
1733           void *callout_data;           void *callout_data;
1734           const unsigned char *tables;           const unsigned char *tables;
1735    
1736         The flags field is a bitmap that specifies which of  the  other  fields         The  flags  field  is a bitmap that specifies which of the other fields
1737         are set. The flag bits are:         are set. The flag bits are:
1738    
1739           PCRE_EXTRA_STUDY_DATA           PCRE_EXTRA_STUDY_DATA
# Line 1680  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1742  MATCHING A PATTERN: THE TRADITIONAL FUNC
1742           PCRE_EXTRA_CALLOUT_DATA           PCRE_EXTRA_CALLOUT_DATA
1743           PCRE_EXTRA_TABLES           PCRE_EXTRA_TABLES
1744    
1745         Other  flag  bits should be set to zero. The study_data field is set in         Other flag bits should be set to zero. The study_data field is  set  in
1746         the pcre_extra block that is returned by  pcre_study(),  together  with         the  pcre_extra  block  that is returned by pcre_study(), together with
1747         the appropriate flag bit. You should not set this yourself, but you may         the appropriate flag bit. You should not set this yourself, but you may
1748         add to the block by setting the other fields  and  their  corresponding         add  to  the  block by setting the other fields and their corresponding
1749         flag bits.         flag bits.
1750    
1751         The match_limit field provides a means of preventing PCRE from using up         The match_limit field provides a means of preventing PCRE from using up
1752         a vast amount of resources when running patterns that are not going  to         a  vast amount of resources when running patterns that are not going to
1753         match,  but  which  have  a very large number of possibilities in their         match, but which have a very large number  of  possibilities  in  their
1754         search trees. The classic  example  is  the  use  of  nested  unlimited         search  trees.  The  classic  example  is  the  use of nested unlimited
1755         repeats.         repeats.
1756    
1757         Internally,  PCRE uses a function called match() which it calls repeat-         Internally, PCRE uses a function called match() which it calls  repeat-
1758         edly (sometimes recursively). The limit set by match_limit  is  imposed         edly  (sometimes  recursively). The limit set by match_limit is imposed
1759         on  the  number  of times this function is called during a match, which         on the number of times this function is called during  a  match,  which
1760         has the effect of limiting the amount of  backtracking  that  can  take         has  the  effect  of  limiting the amount of backtracking that can take
1761         place. For patterns that are not anchored, the count restarts from zero         place. For patterns that are not anchored, the count restarts from zero
1762         for each position in the subject string.         for each position in the subject string.
1763    
1764         The default value for the limit can be set  when  PCRE  is  built;  the         The  default  value  for  the  limit can be set when PCRE is built; the
1765         default  default  is 10 million, which handles all but the most extreme         default default is 10 million, which handles all but the  most  extreme
1766         cases. You can override the default  by  suppling  pcre_exec()  with  a         cases.  You  can  override  the  default by suppling pcre_exec() with a
1767         pcre_extra     block    in    which    match_limit    is    set,    and         pcre_extra    block    in    which    match_limit    is    set,     and
1768         PCRE_EXTRA_MATCH_LIMIT is set in the  flags  field.  If  the  limit  is         PCRE_EXTRA_MATCH_LIMIT  is  set  in  the  flags  field. If the limit is
1769         exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT.         exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT.
1770    
1771         The  match_limit_recursion field is similar to match_limit, but instead         The match_limit_recursion field is similar to match_limit, but  instead
1772         of limiting the total number of times that match() is called, it limits         of limiting the total number of times that match() is called, it limits
1773         the  depth  of  recursion. The recursion depth is a smaller number than         the depth of recursion. The recursion depth is a  smaller  number  than
1774         the total number of calls, because not all calls to match() are  recur-         the  total number of calls, because not all calls to match() are recur-
1775         sive.  This limit is of use only if it is set smaller than match_limit.         sive.  This limit is of use only if it is set smaller than match_limit.
1776    
1777         Limiting the recursion depth limits the amount of  stack  that  can  be         Limiting  the  recursion  depth  limits the amount of stack that can be
1778         used, or, when PCRE has been compiled to use memory on the heap instead         used, or, when PCRE has been compiled to use memory on the heap instead
1779         of the stack, the amount of heap memory that can be used.         of the stack, the amount of heap memory that can be used.
1780    
1781         The default value for match_limit_recursion can be  set  when  PCRE  is         The  default  value  for  match_limit_recursion can be set when PCRE is
1782         built;  the  default  default  is  the  same  value  as the default for         built; the default default  is  the  same  value  as  the  default  for
1783         match_limit. You can override the default by suppling pcre_exec()  with         match_limit.  You can override the default by suppling pcre_exec() with
1784         a   pcre_extra   block  in  which  match_limit_recursion  is  set,  and         a  pcre_extra  block  in  which  match_limit_recursion  is   set,   and
1785         PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in  the  flags  field.  If  the         PCRE_EXTRA_MATCH_LIMIT_RECURSION  is  set  in  the  flags field. If the
1786         limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT.         limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT.
1787    
1788         The  pcre_callout  field is used in conjunction with the "callout" fea-         The pcre_callout field is used in conjunction with the  "callout"  fea-
1789         ture, which is described in the pcrecallout documentation.         ture, which is described in the pcrecallout documentation.
1790    
1791         The tables field  is  used  to  pass  a  character  tables  pointer  to         The  tables  field  is  used  to  pass  a  character  tables pointer to
1792         pcre_exec();  this overrides the value that is stored with the compiled         pcre_exec(); this overrides the value that is stored with the  compiled
1793         pattern. A non-NULL value is stored with the compiled pattern  only  if         pattern.  A  non-NULL value is stored with the compiled pattern only if
1794         custom  tables  were  supplied to pcre_compile() via its tableptr argu-         custom tables were supplied to pcre_compile() via  its  tableptr  argu-
1795         ment.  If NULL is passed to pcre_exec() using this mechanism, it forces         ment.  If NULL is passed to pcre_exec() using this mechanism, it forces
1796         PCRE's  internal  tables  to be used. This facility is helpful when re-         PCRE's internal tables to be used. This facility is  helpful  when  re-
1797         using patterns that have been saved after compiling  with  an  external         using  patterns  that  have been saved after compiling with an external
1798         set  of  tables,  because  the  external tables might be at a different         set of tables, because the external tables  might  be  at  a  different
1799         address when pcre_exec() is called. See the  pcreprecompile  documenta-         address  when  pcre_exec() is called. See the pcreprecompile documenta-
1800         tion for a discussion of saving compiled patterns for later use.         tion for a discussion of saving compiled patterns for later use.
1801    
1802     Option bits for pcre_exec()     Option bits for pcre_exec()
1803    
1804         The  unused  bits of the options argument for pcre_exec() must be zero.         The unused bits of the options argument for pcre_exec() must  be  zero.
1805         The only bits that may  be  set  are  PCRE_ANCHORED,  PCRE_NEWLINE_xxx,         The  only  bits  that  may  be set are PCRE_ANCHORED, PCRE_NEWLINE_xxx,
1806         PCRE_NOTBOL,   PCRE_NOTEOL,   PCRE_NOTEMPTY,   PCRE_NO_UTF8_CHECK   and         PCRE_NOTBOL,   PCRE_NOTEOL,   PCRE_NOTEMPTY,   PCRE_NO_UTF8_CHECK   and
1807         PCRE_PARTIAL.         PCRE_PARTIAL.
1808    
1809           PCRE_ANCHORED           PCRE_ANCHORED
1810    
1811         The PCRE_ANCHORED option limits pcre_exec() to matching  at  the  first         The  PCRE_ANCHORED  option  limits pcre_exec() to matching at the first
1812         matching  position.  If  a  pattern was compiled with PCRE_ANCHORED, or         matching position. If a pattern was  compiled  with  PCRE_ANCHORED,  or
1813         turned out to be anchored by virtue of its contents, it cannot be  made         turned  out to be anchored by virtue of its contents, it cannot be made
1814         unachored at matching time.         unachored at matching time.
1815    
1816           PCRE_NEWLINE_CR           PCRE_NEWLINE_CR
# Line 1757  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1819  MATCHING A PATTERN: THE TRADITIONAL FUNC
1819           PCRE_NEWLINE_ANYCRLF           PCRE_NEWLINE_ANYCRLF
1820           PCRE_NEWLINE_ANY           PCRE_NEWLINE_ANY
1821    
1822         These  options  override  the  newline  definition  that  was chosen or         These options override  the  newline  definition  that  was  chosen  or
1823         defaulted when the pattern was compiled. For details, see the  descrip-         defaulted  when the pattern was compiled. For details, see the descrip-
1824         tion  of  pcre_compile()  above.  During  matching,  the newline choice         tion of pcre_compile()  above.  During  matching,  the  newline  choice
1825         affects the behaviour of the dot, circumflex,  and  dollar  metacharac-         affects  the  behaviour  of the dot, circumflex, and dollar metacharac-
1826         ters.  It may also alter the way the match position is advanced after a         ters. It may also alter the way the match position is advanced after  a
1827         match  failure  for  an  unanchored  pattern.  When  PCRE_NEWLINE_CRLF,         match failure for an unanchored pattern.
1828         PCRE_NEWLINE_ANYCRLF,  or  PCRE_NEWLINE_ANY is set, and a match attempt  
1829         fails when the current position is at a CRLF sequence, the match  posi-         When  PCRE_NEWLINE_CRLF,  PCRE_NEWLINE_ANYCRLF,  or PCRE_NEWLINE_ANY is
1830         tion  is  advanced by two characters instead of one, in other words, to         set, and a match attempt for an unanchored pattern fails when the  cur-
1831         after the CRLF.         rent  position  is  at  a  CRLF  sequence,  and the pattern contains no
1832           explicit matches for  CR  or  NL  characters,  the  match  position  is
1833           advanced by two characters instead of one, in other words, to after the
1834           CRLF.
1835    
1836           The above rule is a compromise that makes the most common cases work as
1837           expected.  For  example,  if  the  pattern  is .+A (and the PCRE_DOTALL
1838           option is not set), it does not match the string "\r\nA" because, after
1839           failing  at the start, it skips both the CR and the LF before retrying.
1840           However, the pattern [\r\n]A does match that string,  because  it  con-
1841           tains an explicit CR or LF reference, and so advances only by one char-
1842           acter after the first failure.  Note than an explicit CR or  LF  refer-
1843           ence occurs for negated character classes such as [^X] because they can
1844           match CR or LF characters.
1845    
1846           Notwithstanding the above, anomalous effects may still occur when  CRLF
1847           is a valid newline sequence and explicit \r or \n escapes appear in the
1848           pattern.
1849    
1850           PCRE_NOTBOL           PCRE_NOTBOL
1851    
# Line 1813  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1892  MATCHING A PATTERN: THE TRADITIONAL FUNC
1892         When PCRE_UTF8 is set at compile time, the validity of the subject as a         When PCRE_UTF8 is set at compile time, the validity of the subject as a
1893         UTF-8  string is automatically checked when pcre_exec() is subsequently         UTF-8  string is automatically checked when pcre_exec() is subsequently
1894         called.  The value of startoffset is also checked  to  ensure  that  it         called.  The value of startoffset is also checked  to  ensure  that  it
1895         points  to the start of a UTF-8 character. If an invalid UTF-8 sequence         points  to  the start of a UTF-8 character. There is a discussion about
1896         of bytes is found, pcre_exec() returns the error PCRE_ERROR_BADUTF8. If         the validity of UTF-8 strings in the section on UTF-8  support  in  the
1897         startoffset  contains  an  invalid  value, PCRE_ERROR_BADUTF8_OFFSET is         main  pcre  page.  If  an  invalid  UTF-8  sequence  of bytes is found,
1898         returned.         pcre_exec() returns the error PCRE_ERROR_BADUTF8. If  startoffset  con-
1899           tains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is returned.
1900         If you already know that your subject is valid, and you  want  to  skip  
1901         these    checks    for   performance   reasons,   you   can   set   the         If  you  already  know that your subject is valid, and you want to skip
1902         PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might  want  to         these   checks   for   performance   reasons,   you   can    set    the
1903         do  this  for the second and subsequent calls to pcre_exec() if you are         PCRE_NO_UTF8_CHECK  option  when calling pcre_exec(). You might want to
1904         making repeated calls to find all  the  matches  in  a  single  subject         do this for the second and subsequent calls to pcre_exec() if  you  are
1905         string.  However,  you  should  be  sure  that the value of startoffset         making  repeated  calls  to  find  all  the matches in a single subject
1906         points to the start of a UTF-8 character.  When  PCRE_NO_UTF8_CHECK  is         string. However, you should be  sure  that  the  value  of  startoffset
1907         set,  the  effect of passing an invalid UTF-8 string as a subject, or a         points  to  the  start of a UTF-8 character. When PCRE_NO_UTF8_CHECK is
1908         value of startoffset that does not point to the start of a UTF-8  char-         set, the effect of passing an invalid UTF-8 string as a subject,  or  a
1909           value  of startoffset that does not point to the start of a UTF-8 char-
1910         acter, is undefined. Your program may crash.         acter, is undefined. Your program may crash.
1911    
1912           PCRE_PARTIAL           PCRE_PARTIAL
1913    
1914         This  option  turns  on  the  partial  matching feature. If the subject         This option turns on the  partial  matching  feature.  If  the  subject
1915         string fails to match the pattern, but at some point during the  match-         string  fails to match the pattern, but at some point during the match-
1916         ing  process  the  end of the subject was reached (that is, the subject         ing process the end of the subject was reached (that  is,  the  subject
1917         partially matches the pattern and the failure to  match  occurred  only         partially  matches  the  pattern and the failure to match occurred only
1918         because  there were not enough subject characters), pcre_exec() returns         because there were not enough subject characters), pcre_exec()  returns
1919         PCRE_ERROR_PARTIAL instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL  is         PCRE_ERROR_PARTIAL  instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is
1920         used,  there  are restrictions on what may appear in the pattern. These         used, there are restrictions on what may appear in the  pattern.  These
1921         are discussed in the pcrepartial documentation.         are discussed in the pcrepartial documentation.
1922    
1923     The string to be matched by pcre_exec()     The string to be matched by pcre_exec()
1924    
1925         The subject string is passed to pcre_exec() as a pointer in subject,  a         The  subject string is passed to pcre_exec() as a pointer in subject, a
1926         length  in  length, and a starting byte offset in startoffset. In UTF-8         length in length, and a starting byte offset in startoffset.  In  UTF-8
1927         mode, the byte offset must point to the start  of  a  UTF-8  character.         mode,  the  byte  offset  must point to the start of a UTF-8 character.
1928         Unlike  the  pattern string, the subject may contain binary zero bytes.         Unlike the pattern string, the subject may contain binary  zero  bytes.
1929         When the starting offset is zero, the search for a match starts at  the         When  the starting offset is zero, the search for a match starts at the
1930         beginning of the subject, and this is by far the most common case.         beginning of the subject, and this is by far the most common case.
1931    
1932         A  non-zero  starting offset is useful when searching for another match         A non-zero starting offset is useful when searching for  another  match
1933         in the same subject by calling pcre_exec() again after a previous  suc-         in  the same subject by calling pcre_exec() again after a previous suc-
1934         cess.   Setting  startoffset differs from just passing over a shortened         cess.  Setting startoffset differs from just passing over  a  shortened
1935         string and setting PCRE_NOTBOL in the case of  a  pattern  that  begins         string  and  setting  PCRE_NOTBOL  in the case of a pattern that begins
1936         with any kind of lookbehind. For example, consider the pattern         with any kind of lookbehind. For example, consider the pattern
1937    
1938           \Biss\B           \Biss\B
1939    
1940         which  finds  occurrences  of "iss" in the middle of words. (\B matches         which finds occurrences of "iss" in the middle of  words.  (\B  matches
1941         only if the current position in the subject is not  a  word  boundary.)         only  if  the  current position in the subject is not a word boundary.)
1942         When  applied  to the string "Mississipi" the first call to pcre_exec()         When applied to the string "Mississipi" the first call  to  pcre_exec()
1943         finds the first occurrence. If pcre_exec() is called  again  with  just         finds  the  first  occurrence. If pcre_exec() is called again with just
1944         the  remainder  of  the  subject,  namely  "issipi", it does not match,         the remainder of the subject,  namely  "issipi",  it  does  not  match,
1945         because \B is always false at the start of the subject, which is deemed         because \B is always false at the start of the subject, which is deemed
1946         to  be  a  word  boundary. However, if pcre_exec() is passed the entire         to be a word boundary. However, if pcre_exec()  is  passed  the  entire
1947         string again, but with startoffset set to 4, it finds the second occur-         string again, but with startoffset set to 4, it finds the second occur-
1948         rence  of "iss" because it is able to look behind the starting point to         rence of "iss" because it is able to look behind the starting point  to
1949         discover that it is preceded by a letter.         discover that it is preceded by a letter.
1950    
1951         If a non-zero starting offset is passed when the pattern  is  anchored,         If  a  non-zero starting offset is passed when the pattern is anchored,
1952         one attempt to match at the given offset is made. This can only succeed         one attempt to match at the given offset is made. This can only succeed
1953         if the pattern does not require the match to be at  the  start  of  the         if  the  pattern  does  not require the match to be at the start of the
1954         subject.         subject.
1955    
1956     How pcre_exec() returns captured substrings     How pcre_exec() returns captured substrings
1957    
1958         In  general, a pattern matches a certain portion of the subject, and in         In general, a pattern matches a certain portion of the subject, and  in
1959         addition, further substrings from the subject  may  be  picked  out  by         addition,  further  substrings  from  the  subject may be picked out by
1960         parts  of  the  pattern.  Following the usage in Jeffrey Friedl's book,         parts of the pattern. Following the usage  in  Jeffrey  Friedl's  book,
1961         this is called "capturing" in what follows, and the  phrase  "capturing         this  is  called "capturing" in what follows, and the phrase "capturing
1962         subpattern"  is  used for a fragment of a pattern that picks out a sub-         subpattern" is used for a fragment of a pattern that picks out  a  sub-
1963         string. PCRE supports several other kinds of  parenthesized  subpattern         string.  PCRE  supports several other kinds of parenthesized subpattern
1964         that do not cause substrings to be captured.         that do not cause substrings to be captured.
1965    
1966         Captured  substrings are returned to the caller via a vector of integer         Captured substrings are returned to the caller via a vector of  integer
1967         offsets whose address is passed in ovector. The number of  elements  in         offsets  whose  address is passed in ovector. The number of elements in
1968         the  vector is passed in ovecsize, which must be a non-negative number.         the vector is passed in ovecsize, which must be a non-negative  number.
1969         Note: this argument is NOT the size of ovector in bytes.         Note: this argument is NOT the size of ovector in bytes.
1970    
1971         The first two-thirds of the vector is used to pass back  captured  sub-         The  first  two-thirds of the vector is used to pass back captured sub-
1972         strings,  each  substring using a pair of integers. The remaining third         strings, each substring using a pair of integers. The  remaining  third
1973         of the vector is used as workspace by pcre_exec() while  matching  cap-         of  the  vector is used as workspace by pcre_exec() while matching cap-
1974         turing  subpatterns, and is not available for passing back information.         turing subpatterns, and is not available for passing back  information.
1975         The length passed in ovecsize should always be a multiple of three.  If         The  length passed in ovecsize should always be a multiple of three. If
1976         it is not, it is rounded down.         it is not, it is rounded down.
1977    
1978         When  a  match  is successful, information about captured substrings is         When a match is successful, information about  captured  substrings  is
1979         returned in pairs of integers, starting at the  beginning  of  ovector,         returned  in  pairs  of integers, starting at the beginning of ovector,
1980         and  continuing  up  to two-thirds of its length at the most. The first         and continuing up to two-thirds of its length at the  most.  The  first
1981         element of a pair is set to the offset of the first character in a sub-         element of a pair is set to the offset of the first character in a sub-
1982         string,  and  the  second  is  set to the offset of the first character         string, and the second is set to the  offset  of  the  first  character
1983         after the end of a substring. The  first  pair,  ovector[0]  and  ovec-         after  the  end  of  a  substring. The first pair, ovector[0] and ovec-
1984         tor[1],  identify  the  portion  of  the  subject string matched by the         tor[1], identify the portion of  the  subject  string  matched  by  the
1985         entire pattern. The next pair is used for the first  capturing  subpat-         entire  pattern.  The next pair is used for the first capturing subpat-
1986         tern, and so on. The value returned by pcre_exec() is one more than the         tern, and so on. The value returned by pcre_exec() is one more than the
1987         highest numbered pair that has been set. For example, if two substrings         highest numbered pair that has been set. For example, if two substrings
1988         have  been captured, the returned value is 3. If there are no capturing         have been captured, the returned value is 3. If there are no  capturing
1989         subpatterns, the return value from a successful match is 1,  indicating         subpatterns,  the return value from a successful match is 1, indicating
1990         that just the first pair of offsets has been set.         that just the first pair of offsets has been set.
1991    
1992         If a capturing subpattern is matched repeatedly, it is the last portion         If a capturing subpattern is matched repeatedly, it is the last portion
1993         of the string that it matched that is returned.         of the string that it matched that is returned.
1994    
1995         If the vector is too small to hold all the captured substring  offsets,         If  the vector is too small to hold all the captured substring offsets,
1996         it is used as far as possible (up to two-thirds of its length), and the         it is used as far as possible (up to two-thirds of its length), and the
1997         function returns a value of zero. In particular, if the substring  off-         function  returns a value of zero. In particular, if the substring off-
1998         sets are not of interest, pcre_exec() may be called with ovector passed         sets are not of interest, pcre_exec() may be called with ovector passed
1999         as NULL and ovecsize as zero. However, if  the  pattern  contains  back         as  NULL  and  ovecsize  as zero. However, if the pattern contains back
2000         references  and  the  ovector is not big enough to remember the related         references and the ovector is not big enough to  remember  the  related
2001         substrings, PCRE has to get additional memory for use during  matching.         substrings,  PCRE has to get additional memory for use during matching.
2002         Thus it is usually advisable to supply an ovector.         Thus it is usually advisable to supply an ovector.
2003    
2004         The  pcre_info()  function  can  be used to find out how many capturing         The pcre_info() function can be used to find  out  how  many  capturing
2005         subpatterns there are in a compiled  pattern.  The  smallest  size  for         subpatterns  there  are  in  a  compiled pattern. The smallest size for
2006         ovector  that  will allow for n captured substrings, in addition to the         ovector that will allow for n captured substrings, in addition  to  the
2007         offsets of the substring matched by the whole pattern, is (n+1)*3.         offsets of the substring matched by the whole pattern, is (n+1)*3.
2008    
2009         It is possible for capturing subpattern number n+1 to match  some  part         It  is  possible for capturing subpattern number n+1 to match some part
2010         of the subject when subpattern n has not been used at all. For example,         of the subject when subpattern n has not been used at all. For example,
2011         if the string "abc" is matched  against  the  pattern  (a|(z))(bc)  the         if  the  string  "abc"  is  matched against the pattern (a|(z))(bc) the
2012         return from the function is 4, and subpatterns 1 and 3 are matched, but         return from the function is 4, and subpatterns 1 and 3 are matched, but
2013         2 is not. When this happens, both values in  the  offset  pairs  corre-         2  is  not.  When  this happens, both values in the offset pairs corre-
2014         sponding to unused subpatterns are set to -1.         sponding to unused subpatterns are set to -1.
2015    
2016         Offset  values  that correspond to unused subpatterns at the end of the         Offset values that correspond to unused subpatterns at the end  of  the
2017         expression are also set to -1. For example,  if  the  string  "abc"  is         expression  are  also  set  to  -1. For example, if the string "abc" is
2018         matched  against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not         matched against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are  not
2019         matched. The return from the function is 2, because  the  highest  used         matched.  The  return  from the function is 2, because the highest used
2020         capturing subpattern number is 1. However, you can refer to the offsets         capturing subpattern number is 1. However, you can refer to the offsets
2021         for the second and third capturing subpatterns if  you  wish  (assuming         for  the  second  and third capturing subpatterns if you wish (assuming
2022         the vector is large enough, of course).         the vector is large enough, of course).
2023    
2024         Some  convenience  functions  are  provided for extracting the captured         Some convenience functions are provided  for  extracting  the  captured
2025         substrings as separate strings. These are described below.         substrings as separate strings. These are described below.
2026    
2027     Error return values from pcre_exec()     Error return values from pcre_exec()
2028    
2029         If pcre_exec() fails, it returns a negative number. The  following  are         If  pcre_exec()  fails, it returns a negative number. The following are
2030         defined in the header file:         defined in the header file:
2031    
2032           PCRE_ERROR_NOMATCH        (-1)           PCRE_ERROR_NOMATCH        (-1)
# Line 1955  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2035  MATCHING A PATTERN: THE TRADITIONAL FUNC
2035    
2036           PCRE_ERROR_NULL           (-2)           PCRE_ERROR_NULL           (-2)
2037    
2038         Either  code  or  subject  was  passed as NULL, or ovector was NULL and         Either code or subject was passed as NULL,  or  ovector  was  NULL  and
2039         ovecsize was not zero.         ovecsize was not zero.
2040    
2041           PCRE_ERROR_BADOPTION      (-3)           PCRE_ERROR_BADOPTION      (-3)
# Line 1964  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2044  MATCHING A PATTERN: THE TRADITIONAL FUNC
2044    
2045           PCRE_ERROR_BADMAGIC       (-4)           PCRE_ERROR_BADMAGIC       (-4)
2046    
2047         PCRE stores a 4-byte "magic number" at the start of the compiled  code,         PCRE  stores a 4-byte "magic number" at the start of the compiled code,
2048         to catch the case when it is passed a junk pointer and to detect when a         to catch the case when it is passed a junk pointer and to detect when a
2049         pattern that was compiled in an environment of one endianness is run in         pattern that was compiled in an environment of one endianness is run in
2050         an  environment  with the other endianness. This is the error that PCRE         an environment with the other endianness. This is the error  that  PCRE
2051         gives when the magic number is not present.         gives when the magic number is not present.
2052    
2053           PCRE_ERROR_UNKNOWN_OPCODE (-5)           PCRE_ERROR_UNKNOWN_OPCODE (-5)
2054    
2055         While running the pattern match, an unknown item was encountered in the         While running the pattern match, an unknown item was encountered in the
2056         compiled  pattern.  This  error  could be caused by a bug in PCRE or by         compiled pattern. This error could be caused by a bug  in  PCRE  or  by
2057         overwriting of the compiled pattern.         overwriting of the compiled pattern.
2058    
2059           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2060    
2061         If a pattern contains back references, but the ovector that  is  passed         If  a  pattern contains back references, but the ovector that is passed
2062         to pcre_exec() is not big enough to remember the referenced substrings,         to pcre_exec() is not big enough to remember the referenced substrings,
2063         PCRE gets a block of memory at the start of matching to  use  for  this         PCRE  gets  a  block of memory at the start of matching to use for this
2064         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
2065         memory is automatically freed at the end of matching.         memory is automatically freed at the end of matching.
2066    
2067           PCRE_ERROR_NOSUBSTRING    (-7)           PCRE_ERROR_NOSUBSTRING    (-7)
2068    
2069         This error is used by the pcre_copy_substring(),  pcre_get_substring(),         This  error is used by the pcre_copy_substring(), pcre_get_substring(),
2070         and  pcre_get_substring_list()  functions  (see  below).  It  is  never         and  pcre_get_substring_list()  functions  (see  below).  It  is  never
2071         returned by pcre_exec().         returned by pcre_exec().
2072    
2073           PCRE_ERROR_MATCHLIMIT     (-8)           PCRE_ERROR_MATCHLIMIT     (-8)
2074    
2075         The backtracking limit, as specified by  the  match_limit  field  in  a         The  backtracking  limit,  as  specified  by the match_limit field in a
2076         pcre_extra  structure  (or  defaulted) was reached. See the description         pcre_extra structure (or defaulted) was reached.  See  the  description
2077         above.         above.
2078    
2079           PCRE_ERROR_CALLOUT        (-9)           PCRE_ERROR_CALLOUT        (-9)
2080    
2081         This error is never generated by pcre_exec() itself. It is provided for         This error is never generated by pcre_exec() itself. It is provided for
2082         use  by  callout functions that want to yield a distinctive error code.         use by callout functions that want to yield a distinctive  error  code.
2083         See the pcrecallout documentation for details.         See the pcrecallout documentation for details.
2084    
2085           PCRE_ERROR_BADUTF8        (-10)           PCRE_ERROR_BADUTF8        (-10)
2086    
2087         A string that contains an invalid UTF-8 byte sequence was passed  as  a         A  string  that contains an invalid UTF-8 byte sequence was passed as a
2088         subject.         subject.
2089    
2090           PCRE_ERROR_BADUTF8_OFFSET (-11)           PCRE_ERROR_BADUTF8_OFFSET (-11)
2091    
2092         The UTF-8 byte sequence that was passed as a subject was valid, but the         The UTF-8 byte sequence that was passed as a subject was valid, but the
2093         value of startoffset did not point to the beginning of a UTF-8  charac-         value  of startoffset did not point to the beginning of a UTF-8 charac-
2094         ter.         ter.
2095    
2096           PCRE_ERROR_PARTIAL        (-12)           PCRE_ERROR_PARTIAL        (-12)
2097    
2098         The  subject  string did not match, but it did match partially. See the         The subject string did not match, but it did match partially.  See  the
2099         pcrepartial documentation for details of partial matching.         pcrepartial documentation for details of partial matching.
2100    
2101           PCRE_ERROR_BADPARTIAL     (-13)           PCRE_ERROR_BADPARTIAL     (-13)
2102    
2103         The PCRE_PARTIAL option was used with  a  compiled  pattern  containing         The  PCRE_PARTIAL  option  was  used with a compiled pattern containing
2104         items  that are not supported for partial matching. See the pcrepartial         items that are not supported for partial matching. See the  pcrepartial
2105         documentation for details of partial matching.         documentation for details of partial matching.
2106    
2107           PCRE_ERROR_INTERNAL       (-14)           PCRE_ERROR_INTERNAL       (-14)
2108    
2109         An unexpected internal error has occurred. This error could  be  caused         An  unexpected  internal error has occurred. This error could be caused
2110         by a bug in PCRE or by overwriting of the compiled pattern.         by a bug in PCRE or by overwriting of the compiled pattern.
2111    
2112           PCRE_ERROR_BADCOUNT       (-15)           PCRE_ERROR_BADCOUNT       (-15)
2113    
2114         This  error is given if the value of the ovecsize argument is negative.         This error is given if the value of the ovecsize argument is  negative.
2115    
2116           PCRE_ERROR_RECURSIONLIMIT (-21)           PCRE_ERROR_RECURSIONLIMIT (-21)
2117    
2118         The internal recursion limit, as specified by the match_limit_recursion         The internal recursion limit, as specified by the match_limit_recursion
2119         field  in  a  pcre_extra  structure (or defaulted) was reached. See the         field in a pcre_extra structure (or defaulted)  was  reached.  See  the
2120         description above.         description above.
2121    
          PCRE_ERROR_NULLWSLIMIT    (-22)  
   
        When a group that can match an empty  substring  is  repeated  with  an  
        unbounded  upper  limit, the subject position at the start of the group  
        must be remembered, so that a test for an empty string can be made when  
        the  end  of the group is reached. Some workspace is required for this;  
        if it runs out, this error is given.  
   
2122           PCRE_ERROR_BADNEWLINE     (-23)           PCRE_ERROR_BADNEWLINE     (-23)
2123    
2124         An invalid combination of PCRE_NEWLINE_xxx options was given.         An invalid combination of PCRE_NEWLINE_xxx options was given.
2125    
2126         Error numbers -16 to -20 are not used by pcre_exec().         Error numbers -16 to -20 and -22 are not used by pcre_exec().
2127    
2128    
2129  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER
# Line 2067  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER Line 2139  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER
2139         int pcre_get_substring_list(const char *subject,         int pcre_get_substring_list(const char *subject,
2140              int *ovector, int stringcount, const char ***listptr);              int *ovector, int stringcount, const char ***listptr);
2141    
2142         Captured substrings can be  accessed  directly  by  using  the  offsets         Captured  substrings  can  be  accessed  directly  by using the offsets
2143         returned  by  pcre_exec()  in  ovector.  For convenience, the functions         returned by pcre_exec() in  ovector.  For  convenience,  the  functions
2144         pcre_copy_substring(),    pcre_get_substring(),    and    pcre_get_sub-         pcre_copy_substring(),    pcre_get_substring(),    and    pcre_get_sub-
2145         string_list()  are  provided for extracting captured substrings as new,         string_list() are provided for extracting captured substrings  as  new,
2146         separate, zero-terminated strings. These functions identify  substrings         separate,  zero-terminated strings. These functions identify substrings
2147         by  number.  The  next section describes functions for extracting named         by number. The next section describes functions  for  extracting  named
2148         substrings.         substrings.
2149    
2150         A substring that contains a binary zero is correctly extracted and  has         A  substring that contains a binary zero is correctly extracted and has
2151         a  further zero added on the end, but the result is not, of course, a C         a further zero added on the end, but the result is not, of course, a  C
2152         string.  However, you can process such a string  by  referring  to  the         string.   However,  you  can  process such a string by referring to the
2153         length  that  is  returned  by  pcre_copy_substring() and pcre_get_sub-         length that is  returned  by  pcre_copy_substring()  and  pcre_get_sub-
2154         string().  Unfortunately, the interface to pcre_get_substring_list() is         string().  Unfortunately, the interface to pcre_get_substring_list() is
2155         not  adequate for handling strings containing binary zeros, because the         not adequate for handling strings containing binary zeros, because  the
2156         end of the final string is not independently indicated.         end of the final string is not independently indicated.
2157    
2158         The first three arguments are the same for all  three  of  these  func-         The  first  three  arguments  are the same for all three of these func-
2159         tions:  subject  is  the subject string that has just been successfully         tions: subject is the subject string that has  just  been  successfully
2160         matched, ovector is a pointer to the vector of integer offsets that was         matched, ovector is a pointer to the vector of integer offsets that was
2161         passed to pcre_exec(), and stringcount is the number of substrings that         passed to pcre_exec(), and stringcount is the number of substrings that
2162         were captured by the match, including the substring  that  matched  the         were  captured  by  the match, including the substring that matched the
2163         entire regular expression. This is the value returned by pcre_exec() if         entire regular expression. This is the value returned by pcre_exec() if
2164         it is greater than zero. If pcre_exec() returned zero, indicating  that         it  is greater than zero. If pcre_exec() returned zero, indicating that
2165         it  ran out of space in ovector, the value passed as stringcount should         it ran out of space in ovector, the value passed as stringcount  should
2166         be the number of elements in the vector divided by three.         be the number of elements in the vector divided by three.
2167    
2168         The functions pcre_copy_substring() and pcre_get_substring() extract  a         The  functions pcre_copy_substring() and pcre_get_substring() extract a
2169         single  substring,  whose  number  is given as stringnumber. A value of         single substring, whose number is given as  stringnumber.  A  value  of
2170         zero extracts the substring that matched the  entire  pattern,  whereas         zero  extracts  the  substring that matched the entire pattern, whereas
2171         higher  values  extract  the  captured  substrings.  For pcre_copy_sub-         higher values  extract  the  captured  substrings.  For  pcre_copy_sub-
2172         string(), the string is placed in buffer,  whose  length  is  given  by         string(),  the  string  is  placed  in buffer, whose length is given by
2173         buffersize,  while  for  pcre_get_substring()  a new block of memory is         buffersize, while for pcre_get_substring() a new  block  of  memory  is
2174         obtained via pcre_malloc, and its address is  returned  via  stringptr.         obtained  via  pcre_malloc,  and its address is returned via stringptr.
2175         The  yield  of  the function is the length of the string, not including         The yield of the function is the length of the  string,  not  including
2176         the terminating zero, or one of these error codes:         the terminating zero, or one of these error codes:
2177    
2178           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2179    
2180         The buffer was too small for pcre_copy_substring(), or the  attempt  to         The  buffer  was too small for pcre_copy_substring(), or the attempt to
2181         get memory failed for pcre_get_substring().         get memory failed for pcre_get_substring().
2182    
2183           PCRE_ERROR_NOSUBSTRING    (-7)           PCRE_ERROR_NOSUBSTRING    (-7)
2184    
2185         There is no substring whose number is stringnumber.         There is no substring whose number is stringnumber.
2186    
2187         The  pcre_get_substring_list()  function  extracts  all  available sub-         The pcre_get_substring_list()  function  extracts  all  available  sub-
2188         strings and builds a list of pointers to them. All this is  done  in  a         strings  and  builds  a list of pointers to them. All this is done in a
2189         single block of memory that is obtained via pcre_malloc. The address of         single block of memory that is obtained via pcre_malloc. The address of
2190         the memory block is returned via listptr, which is also  the  start  of         the  memory  block  is returned via listptr, which is also the start of
2191         the  list  of  string pointers. The end of the list is marked by a NULL         the list of string pointers. The end of the list is marked  by  a  NULL
2192         pointer. The yield of the function is zero if all  went  well,  or  the         pointer.  The  yield  of  the function is zero if all went well, or the
2193         error code         error code
2194    
2195           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2196    
2197         if the attempt to get the memory block failed.         if the attempt to get the memory block failed.
2198    
2199         When  any of these functions encounter a substring that is unset, which         When any of these functions encounter a substring that is unset,  which
2200         can happen when capturing subpattern number n+1 matches  some  part  of         can  happen  when  capturing subpattern number n+1 matches some part of
2201         the  subject, but subpattern n has not been used at all, they return an         the subject, but subpattern n has not been used at all, they return  an
2202         empty string. This can be distinguished from a genuine zero-length sub-         empty string. This can be distinguished from a genuine zero-length sub-
2203         string  by inspecting the appropriate offset in ovector, which is nega-         string by inspecting the appropriate offset in ovector, which is  nega-
2204         tive for unset substrings.         tive for unset substrings.
2205    
2206         The two convenience functions pcre_free_substring() and  pcre_free_sub-         The  two convenience functions pcre_free_substring() and pcre_free_sub-
2207         string_list()  can  be  used  to free the memory returned by a previous         string_list() can be used to free the memory  returned  by  a  previous
2208         call  of  pcre_get_substring()  or  pcre_get_substring_list(),  respec-         call  of  pcre_get_substring()  or  pcre_get_substring_list(),  respec-
2209         tively.  They  do  nothing  more  than  call the function pointed to by         tively. They do nothing more than  call  the  function  pointed  to  by
2210         pcre_free, which of course could be called directly from a  C  program.         pcre_free,  which  of course could be called directly from a C program.
2211         However,  PCRE is used in some situations where it is linked via a spe-         However, PCRE is used in some situations where it is linked via a  spe-
2212         cial  interface  to  another  programming  language  that  cannot   use         cial   interface  to  another  programming  language  that  cannot  use
2213         pcre_free  directly;  it is for these cases that the functions are pro-         pcre_free directly; it is for these cases that the functions  are  pro-
2214         vided.         vided.
2215    
2216    
# Line 2157  EXTRACTING CAPTURED SUBSTRINGS BY NAME Line 2229  EXTRACTING CAPTURED SUBSTRINGS BY NAME
2229              int stringcount, const char *stringname,              int stringcount, const char *stringname,
2230              const char **stringptr);              const char **stringptr);
2231    
2232         To extract a substring by name, you first have to find associated  num-         To  extract a substring by name, you first have to find associated num-
2233         ber.  For example, for this pattern         ber.  For example, for this pattern
2234    
2235           (a+)b(?<xxx>\d+)...           (a+)b(?<xxx>\d+)...
# Line 2166  EXTRACTING CAPTURED SUBSTRINGS BY NAME Line 2238  EXTRACTING CAPTURED SUBSTRINGS BY NAME
2238         be unique (PCRE_DUPNAMES was not set), you can find the number from the         be unique (PCRE_DUPNAMES was not set), you can find the number from the
2239         name by calling pcre_get_stringnumber(). The first argument is the com-         name by calling pcre_get_stringnumber(). The first argument is the com-
2240         piled pattern, and the second is the name. The yield of the function is         piled pattern, and the second is the name. The yield of the function is
2241         the  subpattern  number,  or PCRE_ERROR_NOSUBSTRING (-7) if there is no         the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if  there  is  no
2242         subpattern of that name.         subpattern of that name.
2243    
2244         Given the number, you can extract the substring directly, or use one of         Given the number, you can extract the substring directly, or use one of
2245         the functions described in the previous section. For convenience, there         the functions described in the previous section. For convenience, there
2246         are also two functions that do the whole job.         are also two functions that do the whole job.
2247    
2248         Most   of   the   arguments    of    pcre_copy_named_substring()    and         Most    of    the    arguments   of   pcre_copy_named_substring()   and
2249         pcre_get_named_substring()  are  the  same  as  those for the similarly         pcre_get_named_substring() are the same  as  those  for  the  similarly
2250         named functions that extract by number. As these are described  in  the         named  functions  that extract by number. As these are described in the
2251         previous  section,  they  are not re-described here. There are just two         previous section, they are not re-described here. There  are  just  two
2252         differences:         differences:
2253    
2254         First, instead of a substring number, a substring name is  given.  Sec-         First,  instead  of a substring number, a substring name is given. Sec-
2255         ond, there is an extra argument, given at the start, which is a pointer         ond, there is an extra argument, given at the start, which is a pointer
2256         to the compiled pattern. This is needed in order to gain access to  the         to  the compiled pattern. This is needed in order to gain access to the
2257         name-to-number translation table.         name-to-number translation table.
2258    
2259         These  functions call pcre_get_stringnumber(), and if it succeeds, they         These functions call pcre_get_stringnumber(), and if it succeeds,  they
2260         then call pcre_copy_substring() or pcre_get_substring(),  as  appropri-         then  call  pcre_copy_substring() or pcre_get_substring(), as appropri-
2261         ate.  NOTE:  If PCRE_DUPNAMES is set and there are duplicate names, the         ate. NOTE: If PCRE_DUPNAMES is set and there are duplicate  names,  the
2262         behaviour may not be what you want (see the next section).         behaviour may not be what you want (see the next section).
2263    
2264    
# Line 2195  DUPLICATE SUBPATTERN NAMES Line 2267  DUPLICATE SUBPATTERN NAMES
2267         int pcre_get_stringtable_entries(const pcre *code,         int pcre_get_stringtable_entries(const pcre *code,
2268              const char *name, char **first, char **last);              const char *name, char **first, char **last);
2269    
2270         When a pattern is compiled with the  PCRE_DUPNAMES  option,  names  for         When  a  pattern  is  compiled with the PCRE_DUPNAMES option, names for
2271         subpatterns  are  not  required  to  be unique. Normally, patterns with         subpatterns are not required to  be  unique.  Normally,  patterns  with
2272         duplicate names are such that in any one match, only one of  the  named         duplicate  names  are such that in any one match, only one of the named
2273         subpatterns  participates. An example is shown in the pcrepattern docu-         subpatterns participates. An example is shown in the pcrepattern  docu-
2274         mentation. When duplicates are present, pcre_copy_named_substring() and         mentation.
        pcre_get_named_substring()  return the first substring corresponding to  
        the given name that is set.  If  none  are  set,  an  empty  string  is  
        returned.  The pcre_get_stringnumber() function returns one of the num-  
        bers that are associated with the name, but it is not defined which  it  
        is.  
2275    
2276         If  you want to get full details of all captured substrings for a given         When    duplicates   are   present,   pcre_copy_named_substring()   and
2277         name, you must use  the  pcre_get_stringtable_entries()  function.  The         pcre_get_named_substring() return the first substring corresponding  to
2278           the  given  name  that  is set. If none are set, PCRE_ERROR_NOSUBSTRING
2279           (-7) is returned; no  data  is  returned.  The  pcre_get_stringnumber()
2280           function  returns one of the numbers that are associated with the name,
2281           but it is not defined which it is.
2282    
2283           If you want to get full details of all captured substrings for a  given
2284           name,  you  must  use  the pcre_get_stringtable_entries() function. The
2285         first argument is the compiled pattern, and the second is the name. The         first argument is the compiled pattern, and the second is the name. The
2286         third and fourth are pointers to variables which  are  updated  by  the         third  and  fourth  are  pointers to variables which are updated by the
2287         function. After it has run, they point to the first and last entries in         function. After it has run, they point to the first and last entries in
2288         the name-to-number table  for  the  given  name.  The  function  itself         the  name-to-number  table  for  the  given  name.  The function itself
2289         returns  the  length  of  each entry, or PCRE_ERROR_NOSUBSTRING (-7) if         returns the length of each entry,  or  PCRE_ERROR_NOSUBSTRING  (-7)  if
2290         there are none. The format of the table is described above in the  sec-         there  are none. The format of the table is described above in the sec-
2291         tion  entitled  Information  about  a  pattern.  Given all the relevant         tion entitled Information about a  pattern.   Given  all  the  relevant
2292         entries for the name, you can extract each of their numbers, and  hence         entries  for the name, you can extract each of their numbers, and hence
2293         the captured data, if any.         the captured data, if any.
2294    
2295    
2296  FINDING ALL POSSIBLE MATCHES  FINDING ALL POSSIBLE MATCHES
2297    
2298         The  traditional  matching  function  uses a similar algorithm to Perl,         The traditional matching function uses a  similar  algorithm  to  Perl,
2299         which stops when it finds the first match, starting at a given point in         which stops when it finds the first match, starting at a given point in
2300         the  subject.  If you want to find all possible matches, or the longest         the subject. If you want to find all possible matches, or  the  longest
2301         possible match, consider using the alternative matching  function  (see         possible  match,  consider using the alternative matching function (see
2302         below)  instead.  If you cannot use the alternative function, but still         below) instead. If you cannot use the alternative function,  but  still
2303         need to find all possible matches, you can kludge it up by  making  use         need  to  find all possible matches, you can kludge it up by making use
2304         of the callout facility, which is described in the pcrecallout documen-         of the callout facility, which is described in the pcrecallout documen-
2305         tation.         tation.
2306    
2307         What you have to do is to insert a callout right at the end of the pat-         What you have to do is to insert a callout right at the end of the pat-
2308         tern.   When your callout function is called, extract and save the cur-         tern.  When your callout function is called, extract and save the  cur-
2309         rent matched substring. Then return  1,  which  forces  pcre_exec()  to         rent  matched  substring.  Then  return  1, which forces pcre_exec() to
2310         backtrack  and  try other alternatives. Ultimately, when it runs out of         backtrack and try other alternatives. Ultimately, when it runs  out  of
2311         matches, pcre_exec() will yield PCRE_ERROR_NOMATCH.         matches, pcre_exec() will yield PCRE_ERROR_NOMATCH.
2312    
2313    
# Line 2244  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2318  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2318              int options, int *ovector, int ovecsize,              int options, int *ovector, int ovecsize,
2319              int *workspace, int wscount);              int *workspace, int wscount);
2320    
2321         The function pcre_dfa_exec()  is  called  to  match  a  subject  string         The  function  pcre_dfa_exec()  is  called  to  match  a subject string
2322         against  a  compiled pattern, using a matching algorithm that scans the         against a compiled pattern, using a matching algorithm that  scans  the
2323         subject string just once, and does not backtrack.  This  has  different         subject  string  just  once, and does not backtrack. This has different
2324         characteristics  to  the  normal  algorithm, and is not compatible with         characteristics to the normal algorithm, and  is  not  compatible  with
2325         Perl. Some of the features of PCRE patterns are not  supported.  Never-         Perl.  Some  of the features of PCRE patterns are not supported. Never-
2326         theless,  there are times when this kind of matching can be useful. For         theless, there are times when this kind of matching can be useful.  For
2327         a discussion of the two matching algorithms, see the pcrematching docu-         a discussion of the two matching algorithms, see the pcrematching docu-
2328         mentation.         mentation.
2329    
2330         The  arguments  for  the  pcre_dfa_exec()  function are the same as for         The arguments for the pcre_dfa_exec() function  are  the  same  as  for
2331         pcre_exec(), plus two extras. The ovector argument is used in a differ-         pcre_exec(), plus two extras. The ovector argument is used in a differ-
2332         ent  way,  and  this is described below. The other common arguments are         ent way, and this is described below. The other  common  arguments  are
2333         used in the same way as for pcre_exec(), so their  description  is  not         used  in  the  same way as for pcre_exec(), so their description is not
2334         repeated here.         repeated here.
2335    
2336         The  two  additional  arguments provide workspace for the function. The         The two additional arguments provide workspace for  the  function.  The
2337         workspace vector should contain at least 20 elements. It  is  used  for         workspace  vector  should  contain at least 20 elements. It is used for
2338         keeping  track  of  multiple  paths  through  the  pattern  tree.  More         keeping  track  of  multiple  paths  through  the  pattern  tree.  More
2339         workspace will be needed for patterns and subjects where  there  are  a         workspace  will  be  needed for patterns and subjects where there are a
2340         lot of potential matches.         lot of potential matches.
2341    
2342         Here is an example of a simple call to pcre_dfa_exec():         Here is an example of a simple call to pcre_dfa_exec():
# Line 2284  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2358  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2358    
2359     Option bits for pcre_dfa_exec()     Option bits for pcre_dfa_exec()
2360    
2361         The  unused  bits  of  the options argument for pcre_dfa_exec() must be         The unused bits of the options argument  for  pcre_dfa_exec()  must  be
2362         zero. The only bits  that  may  be  set  are  PCRE_ANCHORED,  PCRE_NEW-         zero.  The  only  bits  that  may  be  set are PCRE_ANCHORED, PCRE_NEW-
2363         LINE_xxx,  PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK,         LINE_xxx, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY,  PCRE_NO_UTF8_CHECK,
2364         PCRE_PARTIAL, PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last         PCRE_PARTIAL, PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last
2365         three of these are the same as for pcre_exec(), so their description is         three of these are the same as for pcre_exec(), so their description is
2366         not repeated here.         not repeated here.
2367    
2368           PCRE_PARTIAL           PCRE_PARTIAL
2369    
2370         This has the same general effect as it does for  pcre_exec(),  but  the         This  has  the  same general effect as it does for pcre_exec(), but the
2371         details   are   slightly   different.  When  PCRE_PARTIAL  is  set  for         details  are  slightly  different.  When  PCRE_PARTIAL   is   set   for
2372         pcre_dfa_exec(), the return code PCRE_ERROR_NOMATCH is  converted  into         pcre_dfa_exec(),  the  return code PCRE_ERROR_NOMATCH is converted into
2373         PCRE_ERROR_PARTIAL  if  the  end  of the subject is reached, there have         PCRE_ERROR_PARTIAL if the end of the subject  is  reached,  there  have
2374         been no complete matches, but there is still at least one matching pos-         been no complete matches, but there is still at least one matching pos-
2375         sibility.  The portion of the string that provided the partial match is         sibility. The portion of the string that provided the partial match  is
2376         set as the first matching string.         set as the first matching string.
2377    
2378           PCRE_DFA_SHORTEST           PCRE_DFA_SHORTEST
2379    
2380         Setting the PCRE_DFA_SHORTEST option causes the matching  algorithm  to         Setting  the  PCRE_DFA_SHORTEST option causes the matching algorithm to
2381         stop as soon as it has found one match. Because of the way the alterna-         stop as soon as it has found one match. Because of the way the alterna-
2382         tive algorithm works, this is necessarily the shortest  possible  match         tive  algorithm  works, this is necessarily the shortest possible match
2383         at the first possible matching point in the subject string.         at the first possible matching point in the subject string.
2384    
2385           PCRE_DFA_RESTART           PCRE_DFA_RESTART
2386    
2387         When  pcre_dfa_exec()  is  called  with  the  PCRE_PARTIAL  option, and         When pcre_dfa_exec()  is  called  with  the  PCRE_PARTIAL  option,  and
2388         returns a partial match, it is possible to call it  again,  with  addi-         returns  a  partial  match, it is possible to call it again, with addi-
2389         tional  subject  characters,  and have it continue with the same match.         tional subject characters, and have it continue with  the  same  match.
2390         The PCRE_DFA_RESTART option requests this action; when it is  set,  the         The  PCRE_DFA_RESTART  option requests this action; when it is set, the
2391         workspace  and wscount options must reference the same vector as before         workspace and wscount options must reference the same vector as  before
2392         because data about the match so far is left in  them  after  a  partial         because  data  about  the  match so far is left in them after a partial
2393         match.  There  is  more  discussion of this facility in the pcrepartial         match. There is more discussion of this  facility  in  the  pcrepartial
2394         documentation.         documentation.
2395    
2396     Successful returns from pcre_dfa_exec()     Successful returns from pcre_dfa_exec()
2397    
2398         When pcre_dfa_exec() succeeds, it may have matched more than  one  sub-         When  pcre_dfa_exec()  succeeds, it may have matched more than one sub-
2399         string in the subject. Note, however, that all the matches from one run         string in the subject. Note, however, that all the matches from one run
2400         of the function start at the same point in  the  subject.  The  shorter         of  the  function  start  at the same point in the subject. The shorter
2401         matches  are all initial substrings of the longer matches. For example,         matches are all initial substrings of the longer matches. For  example,
2402         if the pattern         if the pattern
2403    
2404           <.*>           <.*>
# Line 2339  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2413  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2413           <something> <something else>           <something> <something else>
2414           <something> <something else> <something further>           <something> <something else> <something further>
2415    
2416         On success, the yield of the function is a number  greater  than  zero,         On  success,  the  yield of the function is a number greater than zero,
2417         which  is  the  number of matched substrings. The substrings themselves         which is the number of matched substrings.  The  substrings  themselves
2418         are returned in ovector. Each string uses two elements;  the  first  is         are  returned  in  ovector. Each string uses two elements; the first is
2419         the  offset  to  the start, and the second is the offset to the end. In         the offset to the start, and the second is the offset to  the  end.  In
2420         fact, all the strings have the same start  offset.  (Space  could  have         fact,  all  the  strings  have the same start offset. (Space could have
2421         been  saved by giving this only once, but it was decided to retain some         been saved by giving this only once, but it was decided to retain  some
2422         compatibility with the way pcre_exec() returns data,  even  though  the         compatibility  with  the  way pcre_exec() returns data, even though the
2423         meaning of the strings is different.)         meaning of the strings is different.)
2424    
2425         The strings are returned in reverse order of length; that is, the long-         The strings are returned in reverse order of length; that is, the long-
2426         est matching string is given first. If there were too many  matches  to         est  matching  string is given first. If there were too many matches to
2427         fit  into ovector, the yield of the function is zero, and the vector is         fit into ovector, the yield of the function is zero, and the vector  is
2428         filled with the longest matches.         filled with the longest matches.
2429    
2430     Error returns from pcre_dfa_exec()     Error returns from pcre_dfa_exec()
2431    
2432         The pcre_dfa_exec() function returns a negative number when  it  fails.         The  pcre_dfa_exec()  function returns a negative number when it fails.
2433         Many  of  the  errors  are  the  same as for pcre_exec(), and these are         Many of the errors are the same  as  for  pcre_exec(),  and  these  are
2434         described above.  There are in addition the following errors  that  are         described  above.   There are in addition the following errors that are
2435         specific to pcre_dfa_exec():         specific to pcre_dfa_exec():
2436    
2437           PCRE_ERROR_DFA_UITEM      (-16)           PCRE_ERROR_DFA_UITEM      (-16)
2438    
2439         This  return is given if pcre_dfa_exec() encounters an item in the pat-         This return is given if pcre_dfa_exec() encounters an item in the  pat-
2440         tern that it does not support, for instance, the use of \C  or  a  back         tern  that  it  does not support, for instance, the use of \C or a back
2441         reference.         reference.
2442    
2443           PCRE_ERROR_DFA_UCOND      (-17)           PCRE_ERROR_DFA_UCOND      (-17)
2444    
2445         This  return  is  given  if pcre_dfa_exec() encounters a condition item         This return is given if pcre_dfa_exec()  encounters  a  condition  item
2446         that uses a back reference for the condition, or a test  for  recursion         that  uses  a back reference for the condition, or a test for recursion
2447         in a specific group. These are not supported.         in a specific group. These are not supported.
2448    
2449           PCRE_ERROR_DFA_UMLIMIT    (-18)           PCRE_ERROR_DFA_UMLIMIT    (-18)
2450    
2451         This  return  is given if pcre_dfa_exec() is called with an extra block         This return is given if pcre_dfa_exec() is called with an  extra  block
2452         that contains a setting of the match_limit field. This is not supported         that contains a setting of the match_limit field. This is not supported
2453         (it is meaningless).         (it is meaningless).
2454    
2455           PCRE_ERROR_DFA_WSSIZE     (-19)           PCRE_ERROR_DFA_WSSIZE     (-19)
2456    
2457         This  return  is  given  if  pcre_dfa_exec()  runs  out of space in the         This return is given if  pcre_dfa_exec()  runs  out  of  space  in  the
2458         workspace vector.         workspace vector.
2459    
2460           PCRE_ERROR_DFA_RECURSE    (-20)           PCRE_ERROR_DFA_RECURSE    (-20)
2461    
2462         When a recursive subpattern is processed, the matching  function  calls         When  a  recursive subpattern is processed, the matching function calls
2463         itself  recursively,  using  private vectors for ovector and workspace.         itself recursively, using private vectors for  ovector  and  workspace.
2464         This error is given if the output vector  is  not  large  enough.  This         This  error  is  given  if  the output vector is not large enough. This
2465         should be extremely rare, as a vector of size 1000 is used.         should be extremely rare, as a vector of size 1000 is used.
2466    
2467    
2468  SEE ALSO  SEE ALSO
2469    
2470         pcrebuild(3),  pcrecallout(3), pcrecpp(3)(3), pcrematching(3), pcrepar-         pcrebuild(3), pcrecallout(3), pcrecpp(3)(3), pcrematching(3),  pcrepar-
2471         tial(3), pcreposix(3), pcreprecompile(3), pcresample(3),  pcrestack(3).         tial(3),  pcreposix(3), pcreprecompile(3), pcresample(3), pcrestack(3).
2472    
2473    
2474  AUTHOR  AUTHOR
# Line 2406  AUTHOR Line 2480  AUTHOR
2480    
2481  REVISION  REVISION
2482    
2483         Last updated: 04 June 2007         Last updated: 21 August 2007
2484         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
2485  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2486    
# Line 2593  DIFFERENCES BETWEEN PCRE AND PERL Line 2667  DIFFERENCES BETWEEN PCRE AND PERL
2667    
2668         This  document describes the differences in the ways that PCRE and Perl         This  document describes the differences in the ways that PCRE and Perl
2669         handle regular expressions. The differences described here  are  mainly         handle regular expressions. The differences described here  are  mainly
2670         with  respect  to  Perl 5.8, though PCRE version 7.0 contains some fea-         with  respect  to  Perl 5.8, though PCRE versions 7.0 and later contain
2671         tures that are expected to be in the forthcoming Perl 5.10.         some features that are expected to be in the forthcoming Perl 5.10.
2672    
2673         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
2674         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 2659  DIFFERENCES BETWEEN PCRE AND PERL Line 2733  DIFFERENCES BETWEEN PCRE AND PERL
2733         matching  "aba"  against  the  pattern  /^(a(b)?)+$/  in Perl leaves $2         matching  "aba"  against  the  pattern  /^(a(b)?)+$/  in Perl leaves $2
2734         unset, but in PCRE it is set to "b".         unset, but in PCRE it is set to "b".
2735    
2736         11. PCRE provides some extensions to the Perl regular expression facil-         11.  PCRE  does  support  Perl  5.10's  backtracking  verbs  (*ACCEPT),
2737           (*FAIL),  (*F),  (*COMMIT), (*PRUNE), (*SKIP), and (*THEN), but only in
2738           the forms without an  argument.  PCRE  does  not  support  (*MARK).  If
2739           (*ACCEPT)  is within capturing parentheses, PCRE does not set that cap-
2740           ture group; this is different to Perl.
2741    
2742           12. PCRE provides some extensions to the Perl regular expression facil-
2743         ities.   Perl  5.10  will  include new features that are not in earlier         ities.   Perl  5.10  will  include new features that are not in earlier
2744         versions, some of which (such as named parentheses) have been  in  PCRE         versions, some of which (such as named parentheses) have been  in  PCRE
2745         for some time. This list is with respect to Perl 5.10:         for some time. This list is with respect to Perl 5.10:
# Line 2672  DIFFERENCES BETWEEN PCRE AND PERL Line 2752  DIFFERENCES BETWEEN PCRE AND PERL
2752         meta-character matches only at the very end of the string.         meta-character matches only at the very end of the string.
2753    
2754         (c) If PCRE_EXTRA is set, a backslash followed by a letter with no spe-         (c) If PCRE_EXTRA is set, a backslash followed by a letter with no spe-
2755         cial  meaning  is  faulted.  Otherwise,  like  Perl,  the  backslash is         cial meaning is faulted. Otherwise, like Perl, the backslash is quietly
2756         ignored. (Perl can be made to issue a warning.)         ignored.  (Perl can be made to issue a warning.)
2757    
2758         (d) If PCRE_UNGREEDY is set, the greediness of the  repetition  quanti-         (d) If PCRE_UNGREEDY is set, the greediness of the  repetition  quanti-
2759         fiers is inverted, that is, by default they are not greedy, but if fol-         fiers is inverted, that is, by default they are not greedy, but if fol-
# Line 2705  AUTHOR Line 2785  AUTHOR
2785    
2786  REVISION  REVISION
2787    
2788         Last updated: 06 March 2007         Last updated: 08 August 2007
2789         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
2790  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2791    
# Line 2719  NAME Line 2799  NAME
2799    
2800  PCRE REGULAR EXPRESSION DETAILS  PCRE REGULAR EXPRESSION DETAILS
2801    
2802         The  syntax  and semantics of the regular expressions supported by PCRE         The  syntax and semantics of the regular expressions that are supported
2803         are described below. Regular expressions are also described in the Perl         by PCRE are described in detail below. There is a quick-reference  syn-
2804         documentation  and  in  a  number  of books, some of which have copious         tax  summary  in  the  pcresyntax  page. Perl's regular expressions are
2805         examples.  Jeffrey Friedl's "Mastering Regular Expressions",  published         described in its own documentation, and regular expressions in  general
2806         by  O'Reilly, covers regular expressions in great detail. This descrip-         are  covered in a number of books, some of which have copious examples.
2807         tion of PCRE's regular expressions is intended as reference material.         Jeffrey  Friedl's  "Mastering  Regular   Expressions",   published   by
2808           O'Reilly,  covers regular expressions in great detail. This description
2809           of PCRE's regular expressions is intended as reference material.
2810    
2811         The original operation of PCRE was on strings of  one-byte  characters.         The original operation of PCRE was on strings of  one-byte  characters.
2812         However,  there is now also support for UTF-8 character strings. To use         However,  there is now also support for UTF-8 character strings. To use
# Line 2744  PCRE REGULAR EXPRESSION DETAILS Line 2826  PCRE REGULAR EXPRESSION DETAILS
2826         discussed in the pcrematching page.         discussed in the pcrematching page.
2827    
2828    
2829    NEWLINE CONVENTIONS
2830    
2831           PCRE  supports five different conventions for indicating line breaks in
2832           strings: a single CR (carriage return) character, a  single  LF  (line-
2833           feed) character, the two-character sequence CRLF, any of the three pre-
2834           ceding, or any Unicode newline sequence. The pcreapi page  has  further
2835           discussion  about newlines, and shows how to set the newline convention
2836           in the options arguments for the compiling and matching functions.
2837    
2838           It is also possible to specify a newline convention by starting a  pat-
2839           tern string with one of the following five sequences:
2840    
2841             (*CR)        carriage return
2842             (*LF)        linefeed
2843             (*CRLF)      carriage return, followed by linefeed
2844             (*ANYCRLF)   any of the three above
2845             (*ANY)       all Unicode newline sequences
2846    
2847           These override the default and the options given to pcre_compile(). For
2848           example, on a Unix system where LF is the default newline sequence, the
2849           pattern
2850    
2851             (*CR)a.b
2852    
2853           changes the convention to CR. That pattern matches "a\nb" because LF is
2854           no longer a newline. Note that these special settings,  which  are  not
2855           Perl-compatible,  are  recognized  only at the very start of a pattern,
2856           and that they must be in upper case.
2857    
2858    
2859  CHARACTERS AND METACHARACTERS  CHARACTERS AND METACHARACTERS
2860    
2861         A  regular  expression  is  a pattern that is matched against a subject         A regular expression is a pattern that is  matched  against  a  subject
2862         string from left to right. Most characters stand for  themselves  in  a         string  from  left  to right. Most characters stand for themselves in a
2863         pattern,  and  match  the corresponding characters in the subject. As a         pattern, and match the corresponding characters in the  subject.  As  a
2864         trivial example, the pattern         trivial example, the pattern
2865    
2866           The quick brown fox           The quick brown fox
2867    
2868         matches a portion of a subject string that is identical to itself. When         matches a portion of a subject string that is identical to itself. When
2869         caseless  matching is specified (the PCRE_CASELESS option), letters are         caseless matching is specified (the PCRE_CASELESS option), letters  are
2870         matched independently of case. In UTF-8 mode, PCRE  always  understands         matched  independently  of case. In UTF-8 mode, PCRE always understands
2871         the  concept  of case for characters whose values are less than 128, so         the concept of case for characters whose values are less than  128,  so
2872         caseless matching is always possible. For characters with  higher  val-         caseless  matching  is always possible. For characters with higher val-
2873         ues,  the concept of case is supported if PCRE is compiled with Unicode         ues, the concept of case is supported if PCRE is compiled with  Unicode
2874         property support, but not otherwise.   If  you  want  to  use  caseless         property  support,  but  not  otherwise.   If  you want to use caseless
2875         matching  for  characters  128  and above, you must ensure that PCRE is         matching for characters 128 and above, you must  ensure  that  PCRE  is
2876         compiled with Unicode property support as well as with UTF-8 support.         compiled with Unicode property support as well as with UTF-8 support.
2877    
2878         The power of regular expressions comes  from  the  ability  to  include         The  power  of  regular  expressions  comes from the ability to include
2879         alternatives  and  repetitions in the pattern. These are encoded in the         alternatives and repetitions in the pattern. These are encoded  in  the
2880         pattern by the use of metacharacters, which do not stand for themselves         pattern by the use of metacharacters, which do not stand for themselves
2881         but instead are interpreted in some special way.         but instead are interpreted in some special way.
2882    
2883         There  are  two different sets of metacharacters: those that are recog-         There are two different sets of metacharacters: those that  are  recog-
2884         nized anywhere in the pattern except within square brackets, and  those         nized  anywhere in the pattern except within square brackets, and those
2885         that  are  recognized  within square brackets. Outside square brackets,         that are recognized within square brackets.  Outside  square  brackets,
2886         the metacharacters are as follows:         the metacharacters are as follows:
2887    
2888           \      general escape character with several uses           \      general escape character with several uses
# Line 2789  CHARACTERS AND METACHARACTERS Line 2901  CHARACTERS AND METACHARACTERS
2901                  also "possessive quantifier"                  also "possessive quantifier"
2902           {      start min/max quantifier           {      start min/max quantifier
2903    
2904         Part of a pattern that is in square brackets  is  called  a  "character         Part  of  a  pattern  that is in square brackets is called a "character
2905         class". In a character class the only metacharacters are:         class". In a character class the only metacharacters are:
2906    
2907           \      general escape character           \      general escape character
# Line 2799  CHARACTERS AND METACHARACTERS Line 2911  CHARACTERS AND METACHARACTERS
2911                    syntax)                    syntax)
2912           ]      terminates the character class           ]      terminates the character class
2913    
2914         The  following sections describe the use of each of the metacharacters.         The following sections describe the use of each of the  metacharacters.
2915    
2916    
2917  BACKSLASH  BACKSLASH
2918    
2919         The backslash character has several uses. Firstly, if it is followed by         The backslash character has several uses. Firstly, if it is followed by
2920         a  non-alphanumeric  character,  it takes away any special meaning that         a non-alphanumeric character, it takes away any  special  meaning  that
2921         character may have. This  use  of  backslash  as  an  escape  character         character  may  have.  This  use  of  backslash  as an escape character
2922         applies both inside and outside character classes.         applies both inside and outside character classes.
2923    
2924         For  example,  if  you want to match a * character, you write \* in the         For example, if you want to match a * character, you write  \*  in  the
2925         pattern.  This escaping action applies whether  or  not  the  following         pattern.   This  escaping  action  applies whether or not the following
2926         character  would  otherwise be interpreted as a metacharacter, so it is         character would otherwise be interpreted as a metacharacter, so  it  is
2927         always safe to precede a non-alphanumeric  with  backslash  to  specify         always  safe  to  precede  a non-alphanumeric with backslash to specify
2928         that  it stands for itself. In particular, if you want to match a back-         that it stands for itself. In particular, if you want to match a  back-
2929         slash, you write \\.         slash, you write \\.
2930    
2931         If a pattern is compiled with the PCRE_EXTENDED option,  whitespace  in         If  a  pattern is compiled with the PCRE_EXTENDED option, whitespace in
2932         the  pattern (other than in a character class) and characters between a         the pattern (other than in a character class) and characters between  a
2933         # outside a character class and the next newline are ignored. An escap-         # outside a character class and the next newline are ignored. An escap-
2934         ing  backslash  can  be  used to include a whitespace or # character as         ing backslash can be used to include a whitespace  or  #  character  as
2935         part of the pattern.         part of the pattern.
2936    
2937         If you want to remove the special meaning from a  sequence  of  charac-         If  you  want  to remove the special meaning from a sequence of charac-
2938         ters,  you can do so by putting them between \Q and \E. This is differ-         ters, you can do so by putting them between \Q and \E. This is  differ-
2939         ent from Perl in that $ and  @  are  handled  as  literals  in  \Q...\E         ent  from  Perl  in  that  $  and  @ are handled as literals in \Q...\E
2940         sequences  in  PCRE, whereas in Perl, $ and @ cause variable interpola-         sequences in PCRE, whereas in Perl, $ and @ cause  variable  interpola-
2941         tion. Note the following examples:         tion. Note the following examples:
2942    
2943           Pattern            PCRE matches   Perl matches           Pattern            PCRE matches   Perl matches
# Line 2835  BACKSLASH Line 2947  BACKSLASH
2947           \Qabc\$xyz\E       abc\$xyz       abc\$xyz           \Qabc\$xyz\E       abc\$xyz       abc\$xyz
2948           \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz           \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz
2949    
2950         The \Q...\E sequence is recognized both inside  and  outside  character         The  \Q...\E  sequence  is recognized both inside and outside character
2951         classes.         classes.
2952    
2953     Non-printing characters     Non-printing characters
2954    
2955         A second use of backslash provides a way of encoding non-printing char-         A second use of backslash provides a way of encoding non-printing char-
2956         acters in patterns in a visible manner. There is no restriction on  the         acters  in patterns in a visible manner. There is no restriction on the
2957         appearance  of non-printing characters, apart from the binary zero that         appearance of non-printing characters, apart from the binary zero  that
2958         terminates a pattern, but when a pattern  is  being  prepared  by  text         terminates  a  pattern,  but  when  a pattern is being prepared by text
2959         editing,  it  is  usually  easier  to  use  one of the following escape         editing, it is usually easier  to  use  one  of  the  following  escape
2960         sequences than the binary character it represents:         sequences than the binary character it represents:
2961    
2962           \a        alarm, that is, the BEL character (hex 07)           \a        alarm, that is, the BEL character (hex 07)
2963           \cx       "control-x", where x is any character           \cx       "control-x", where x is any character
2964           \e        escape (hex 1B)           \e        escape (hex 1B)
2965           \f        formfeed (hex 0C)           \f        formfeed (hex 0C)
2966           \n        newline (hex 0A)           \n        linefeed (hex 0A)
2967           \r        carriage return (hex 0D)           \r        carriage return (hex 0D)
2968           \t        tab (hex 09)           \t        tab (hex 09)
2969           \ddd      character with octal code ddd, or backreference           \ddd      character with octal code ddd, or backreference
2970           \xhh      character with hex code hh           \xhh      character with hex code hh
2971           \x{hhh..} character with hex code hhh..           \x{hhh..} character with hex code hhh..
2972    
2973         The precise effect of \cx is as follows: if x is a lower  case  letter,         The  precise  effect of \cx is as follows: if x is a lower case letter,
2974         it  is converted to upper case. Then bit 6 of the character (hex 40) is         it is converted to upper case. Then bit 6 of the character (hex 40)  is
2975         inverted.  Thus \cz becomes hex 1A, but \c{ becomes hex 3B,  while  \c;         inverted.   Thus  \cz becomes hex 1A, but \c{ becomes hex 3B, while \c;
2976         becomes hex 7B.         becomes hex 7B.
2977    
2978         After  \x, from zero to two hexadecimal digits are read (letters can be         After \x, from zero to two hexadecimal digits are read (letters can  be
2979         in upper or lower case). Any number of hexadecimal  digits  may  appear         in  upper  or  lower case). Any number of hexadecimal digits may appear
2980         between  \x{  and  },  but the value of the character code must be less         between \x{ and }, but the value of the character  code  must  be  less
2981         than 256 in non-UTF-8 mode, and less than 2**31 in UTF-8 mode (that is,         than 256 in non-UTF-8 mode, and less than 2**31 in UTF-8 mode. That is,
2982         the  maximum  hexadecimal  value is 7FFFFFFF). If characters other than         the maximum value in hexadecimal is 7FFFFFFF. Note that this is  bigger
2983         hexadecimal digits appear between \x{ and }, or if there is  no  termi-         than the largest Unicode code point, which is 10FFFF.
2984         nating  }, this form of escape is not recognized.  Instead, the initial  
2985         \x will be interpreted as a basic hexadecimal escape, with no following         If  characters  other than hexadecimal digits appear between \x{ and },
2986         digits, giving a character whose value is zero.         or if there is no terminating }, this form of escape is not recognized.
2987           Instead,  the  initial  \x  will  be interpreted as a basic hexadecimal
2988           escape, with no following digits, giving a  character  whose  value  is
2989           zero.
2990    
2991         Characters whose value is less than 256 can be defined by either of the         Characters whose value is less than 256 can be defined by either of the
2992         two syntaxes for \x. There is no difference in the way  they  are  han-         two syntaxes for \x. There is no difference in the way  they  are  han-
# Line 2926  BACKSLASH Line 3041  BACKSLASH
3041    
3042     Absolute and relative back references     Absolute and relative back references
3043    
3044         The  sequence  \g followed by a positive or negative number, optionally         The  sequence  \g followed by an unsigned or a negative number, option-
3045         enclosed in braces, is an absolute or relative back reference. A  named         ally enclosed in braces, is an absolute or relative back  reference.  A
3046         back  reference can be coded as \g{name}. Back references are discussed         named back reference can be coded as \g{name}. Back references are dis-
3047         later, following the discussion of parenthesized subpatterns.         cussed later, following the discussion of parenthesized subpatterns.
3048    
3049     Generic character types     Generic character types
3050    
# Line 2938  BACKSLASH Line 3053  BACKSLASH
3053    
3054           \d     any decimal digit           \d     any decimal digit
3055           \D     any character that is not a decimal digit           \D     any character that is not a decimal digit
3056             \h     any horizontal whitespace character
3057             \H     any character that is not a horizontal whitespace character
3058           \s     any whitespace character           \s     any whitespace character
3059           \S     any character that is not a whitespace character           \S     any character that is not a whitespace character
3060             \v     any vertical whitespace character
3061             \V     any character that is not a vertical whitespace character
3062           \w     any "word" character           \w     any "word" character
3063           \W     any "non-word" character           \W     any "non-word" character
3064    
# Line 2954  BACKSLASH Line 3073  BACKSLASH
3073    
3074         For compatibility with Perl, \s does not match the VT  character  (code         For compatibility with Perl, \s does not match the VT  character  (code
3075         11).   This makes it different from the the POSIX "space" class. The \s         11).   This makes it different from the the POSIX "space" class. The \s
3076         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
3077         "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-
3078         ter. In PCRE, it never does.)         ter. In PCRE, it never does.
3079    
3080           In UTF-8 mode, characters with values greater than 128 never match  \d,
3081           \s, or \w, and always match \D, \S, and \W. This is true even when Uni-
3082           code character property support is available.  These  sequences  retain
3083           their original meanings from before UTF-8 support was available, mainly
3084           for efficiency reasons.
3085    
3086           The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to
3087           the  other  sequences, these do match certain high-valued codepoints in
3088           UTF-8 mode.  The horizontal space characters are:
3089    
3090             U+0009     Horizontal tab
3091             U+0020     Space
3092             U+00A0     Non-break space
3093             U+1680     Ogham space mark
3094             U+180E     Mongolian vowel separator
3095             U+2000     En quad
3096             U+2001     Em quad
3097             U+2002     En space
3098             U+2003     Em space
3099             U+2004     Three-per-em space
3100             U+2005     Four-per-em space
3101             U+2006     Six-per-em space
3102             U+2007     Figure space
3103             U+2008     Punctuation space
3104             U+2009     Thin space
3105             U+200A     Hair space
3106             U+202F     Narrow no-break space
3107             U+205F     Medium mathematical space
3108             U+3000     Ideographic space
3109    
3110           The vertical space characters are:
3111    
3112             U+000A     Linefeed
3113             U+000B     Vertical tab
3114             U+000C     Formfeed
3115             U+000D     Carriage return
3116             U+0085     Next line
3117             U+2028     Line separator
3118             U+2029     Paragraph separator
3119    
3120         A "word" character is an underscore or any character less than 256 that         A "word" character is an underscore or any character less than 256 that
3121         is  a  letter  or  digit.  The definition of letters and digits is con-         is  a  letter  or  digit.  The definition of letters and digits is con-
# Line 2964  BACKSLASH Line 3123  BACKSLASH
3123         specific  matching is taking place (see "Locale support" in the pcreapi         specific  matching is taking place (see "Locale support" in the pcreapi
3124         page). For example, in a French locale such  as  "fr_FR"  in  Unix-like         page). For example, in a French locale such  as  "fr_FR"  in  Unix-like
3125         systems,  or "french" in Windows, some character codes greater than 128         systems,  or "french" in Windows, some character codes greater than 128
3126         are used for accented letters, and these are matched by \w.         are used for accented letters, and these are matched by \w. The use  of
3127           locales with Unicode is discouraged.
        In UTF-8 mode, characters with values greater than 128 never match  \d,  
        \s, or \w, and always match \D, \S, and \W. This is true even when Uni-  
        code character property support is available. The use of  locales  with  
        Unicode is discouraged.  
3128    
3129     Newline sequences     Newline sequences
3130    
3131         Outside  a  character class, the escape sequence \R matches any Unicode         Outside  a  character class, the escape sequence \R matches any Unicode
3132         newline sequence. This is an extension to Perl. In non-UTF-8 mode \R is         newline sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \R  is
3133         equivalent to the following:         equivalent to the following:
3134    
3135           (?>\r\n|\n|\x0b|\f|\r|\x85)           (?>\r\n|\n|\x0b|\f|\r|\x85)
# Line 2996  BACKSLASH Line 3151  BACKSLASH
3151     Unicode character properties     Unicode character properties
3152    
3153         When PCRE is built with Unicode character property support, three addi-         When PCRE is built with Unicode character property support, three addi-
3154         tional escape sequences to match  character  properties  are  available         tional escape sequences that match characters with specific  properties
3155         when UTF-8 mode is selected. They are:         are  available.   When not in UTF-8 mode, these sequences are of course
3156           limited to testing characters whose codepoints are less than  256,  but
3157           they do work in this mode.  The extra escape sequences are:
3158    
3159           \p{xx}   a character with the xx property           \p{xx}   a character with the xx property
3160           \P{xx}   a character without the xx property           \P{xx}   a character without the xx property
# Line 3092  BACKSLASH Line 3249  BACKSLASH
3249         has  the  Lu,  Ll, or Lt property, in other words, a letter that is not         has  the  Lu,  Ll, or Lt property, in other words, a letter that is not
3250         classified as a modifier or "other".         classified as a modifier or "other".
3251    
3252           The Cs (Surrogate) property applies only to  characters  in  the  range
3253           U+D800  to  U+DFFF. Such characters are not valid in UTF-8 strings (see
3254           RFC 3629) and so cannot be tested by PCRE, unless UTF-8 validity check-
3255           ing  has  been  turned off (see the discussion of PCRE_NO_UTF8_CHECK in
3256           the pcreapi page).
3257    
3258         The long synonyms for these properties  that  Perl  supports  (such  as         The long synonyms for these properties  that  Perl  supports  (such  as
3259         \p{Letter})  are  not  supported by PCRE, nor is it permitted to prefix         \p{Letter})  are  not  supported by PCRE, nor is it permitted to prefix
3260         any of these properties with "Is".         any of these properties with "Is".
# Line 3111  BACKSLASH Line 3274  BACKSLASH
3274         That is, it matches a character without the "mark"  property,  followed         That is, it matches a character without the "mark"  property,  followed
3275         by  zero  or  more  characters with the "mark" property, and treats the         by  zero  or  more  characters with the "mark" property, and treats the
3276         sequence as an atomic group (see below).  Characters  with  the  "mark"         sequence as an atomic group (see below).  Characters  with  the  "mark"
3277         property are typically accents that affect the preceding character.         property  are  typically  accents  that affect the preceding character.
3278           None of them have codepoints less than 256, so  in  non-UTF-8  mode  \X
3279           matches any one character.
3280    
3281         Matching  characters  by Unicode property is not fast, because PCRE has         Matching  characters  by Unicode property is not fast, because PCRE has
3282         to search a structure that contains  data  for  over  fifteen  thousand         to search a structure that contains  data  for  over  fifteen  thousand
# Line 3537  SUBPATTERNS Line 3702  SUBPATTERNS
3702         "Saturday".         "Saturday".
3703    
3704    
3705    DUPLICATE SUBPATTERN NUMBERS
3706    
3707           Perl 5.10 introduced a feature whereby each alternative in a subpattern
3708           uses the same numbers for its capturing parentheses. Such a  subpattern
3709           starts  with (?| and is itself a non-capturing subpattern. For example,
3710           consider this pattern:
3711    
3712             (?|(Sat)ur|(Sun))day
3713    
3714           Because the two alternatives are inside a (?| group, both sets of  cap-
3715           turing  parentheses  are  numbered one. Thus, when the pattern matches,
3716           you can look at captured substring number  one,  whichever  alternative
3717           matched.  This  construct  is useful when you want to capture part, but
3718           not all, of one of a number of alternatives. Inside a (?| group, paren-
3719           theses  are  numbered as usual, but the number is reset at the start of
3720           each branch. The numbers of any capturing buffers that follow the  sub-
3721           pattern  start after the highest number used in any branch. The follow-
3722           ing example is taken from the Perl documentation.  The  numbers  under-
3723           neath show in which buffer the captured content will be stored.
3724    
3725             # before  ---------------branch-reset----------- after
3726             / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
3727             # 1            2         2  3        2     3     4
3728    
3729           A  backreference  or  a  recursive call to a numbered subpattern always
3730           refers to the first one in the pattern with the given number.
3731    
3732           An alternative approach to using this "branch reset" feature is to  use
3733           duplicate named subpatterns, as described in the next section.
3734    
3735    
3736  NAMED SUBPATTERNS  NAMED SUBPATTERNS
3737    
3738         Identifying  capturing  parentheses  by number is simple, but it can be         Identifying  capturing  parentheses  by number is simple, but it can be
# Line 3576  NAMED SUBPATTERNS Line 3772  NAMED SUBPATTERNS
3772           (?<DN>Sat)(?:urday)?           (?<DN>Sat)(?:urday)?
3773    
3774         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
3775         match.  The convenience  function  for  extracting  the  data  by  name         match.  (An alternative way of solving this problem is to use a "branch
3776         returns  the  substring  for  the first (and in this example, the only)         reset" subpattern, as described in the previous section.)
3777         subpattern of that name that matched.  This  saves  searching  to  find  
3778         which  numbered  subpattern  it  was. If you make a reference to a non-         The  convenience  function  for extracting the data by name returns the
3779         unique named subpattern from elsewhere in the  pattern,  the  one  that         substring for the first (and in this example, the only)  subpattern  of
3780         corresponds  to  the  lowest number is used. For further details of the         that  name  that  matched.  This saves searching to find which numbered
3781         interfaces for handling named subpatterns, see the  pcreapi  documenta-         subpattern it was. If you make a reference to a non-unique  named  sub-
3782         tion.         pattern  from elsewhere in the pattern, the one that corresponds to the
3783           lowest number is used. For further details of the interfaces  for  han-
3784           dling named subpatterns, see the pcreapi documentation.
3785    
3786    
3787  REPETITION  REPETITION
# Line 3788  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE Line 3986  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE
3986    
3987           \d++foo           \d++foo
3988    
3989         Possessive  quantifiers  are  always  greedy;  the   setting   of   the         Note that a possessive quantifier can be used with an entire group, for
3990           example:
3991    
3992             (abc|xyz){2,3}+
3993    
3994           Possessive   quantifiers   are   always  greedy;  the  setting  of  the
3995         PCRE_UNGREEDY option is ignored. They are a convenient notation for the         PCRE_UNGREEDY option is ignored. They are a convenient notation for the
3996         simpler forms of atomic group. However, there is no difference  in  the         simpler  forms  of atomic group. However, there is no difference in the
3997         meaning  of  a  possessive  quantifier and the equivalent atomic group,         meaning of a possessive quantifier and  the  equivalent  atomic  group,
3998         though there may be a performance  difference;  possessive  quantifiers         though  there  may  be a performance difference; possessive quantifiers
3999         should be slightly faster.         should be slightly faster.
4000    
4001         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-
4002         tax.  Jeffrey Friedl originated the idea (and the name)  in  the  first         tax.   Jeffrey  Friedl  originated the idea (and the name) in the first
4003         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
4004         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
4005         found its way into Perl at release 5.10.         found its way into Perl at release 5.10.
4006    
4007         PCRE has an optimization that automatically "possessifies" certain sim-         PCRE has an optimization that automatically "possessifies" certain sim-
4008         ple pattern constructs. For example, the sequence  A+B  is  treated  as         ple  pattern  constructs.  For  example, the sequence A+B is treated as
4009         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
4010         when B must follow.         when B must follow.
4011    
4012         When a pattern contains an unlimited repeat inside  a  subpattern  that         When  a  pattern  contains an unlimited repeat inside a subpattern that
4013         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
4014         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
4015         very long time indeed. The pattern         very long time indeed. The pattern
4016    
4017           (\D+|<\d+>)*[!?]           (\D+|<\d+>)*[!?]
4018    
4019         matches  an  unlimited number of substrings that either consist of non-         matches an unlimited number of substrings that either consist  of  non-
4020         digits, or digits enclosed in <>, followed by either ! or  ?.  When  it         digits,  or  digits  enclosed in <>, followed by either ! or ?. When it
4021         matches, it runs quickly. However, if it is applied to         matches, it runs quickly. However, if it is applied to
4022    
4023           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
4024    
4025         it  takes  a  long  time  before reporting failure. This is because the         it takes a long time before reporting  failure.  This  is  because  the
4026         string can be divided between the internal \D+ repeat and the  external         string  can be divided between the internal \D+ repeat and the external
4027         *  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
4028         example uses [!?] rather than a single character at  the  end,  because         example  uses  [!?]  rather than a single character at the end, because
4029         both  PCRE  and  Perl have an optimization that allows for fast failure         both PCRE and Perl have an optimization that allows  for  fast  failure
4030         when a single character is used. They remember the last single  charac-         when  a single character is used. They remember the last single charac-
4031         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
4032         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
4033         group, like this:         group, like this:
4034    
4035           ((?>\D+)|<\d+>)*[!?]           ((?>\D+)|<\d+>)*[!?]
4036    
4037         sequences  of non-digits cannot be broken, and failure happens quickly.         sequences of non-digits cannot be broken, and failure happens  quickly.
4038    
4039    
4040  BACK REFERENCES  BACK REFERENCES
4041    
4042         Outside a character class, a backslash followed by a digit greater than         Outside a character class, a backslash followed by a digit greater than
4043         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-
4044         pattern earlier (that is, to its left) in the pattern,  provided  there         pattern  earlier  (that is, to its left) in the pattern, provided there
4045         have been that many previous capturing left parentheses.         have been that many previous capturing left parentheses.
4046    
4047         However, if the decimal number following the backslash is less than 10,         However, if the decimal number following the backslash is less than 10,
4048         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
4049         there  are  not that many capturing left parentheses in the entire pat-         there are not that many capturing left parentheses in the  entire  pat-
4050         tern. In other words, the parentheses that are referenced need  not  be         tern.  In  other words, the parentheses that are referenced need not be
4051         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
4052         reference" of this type can make sense when a  repetition  is  involved         reference"  of  this  type can make sense when a repetition is involved
4053         and  the  subpattern to the right has participated in an earlier itera-         and the subpattern to the right has participated in an  earlier  itera-
4054         tion.         tion.
4055    
4056         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
4057         subpattern  whose  number  is  10  or  more using this syntax because a         subpattern whose number is 10 or  more  using  this  syntax  because  a
4058         sequence such as \50 is interpreted as a character  defined  in  octal.         sequence  such  as  \50 is interpreted as a character defined in octal.
4059         See the subsection entitled "Non-printing characters" above for further         See the subsection entitled "Non-printing characters" above for further
4060         details of the handling of digits following a backslash.  There  is  no         details  of  the  handling of digits following a backslash. There is no
4061         such  problem  when named parentheses are used. A back reference to any         such problem when named parentheses are used. A back reference  to  any
4062         subpattern is possible using named parentheses (see below).         subpattern is possible using named parentheses (see below).
4063    
4064         Another way of avoiding the ambiguity inherent in  the  use  of  digits         Another  way  of  avoiding  the ambiguity inherent in the use of digits
4065         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-
4066         ture introduced in Perl 5.10. This escape must be followed by  a  posi-         ture  introduced  in  Perl  5.10.  This  escape  must be followed by an
4067         tive  or  a negative number, optionally enclosed in braces. These exam-         unsigned number or a negative number, optionally  enclosed  in  braces.
4068         ples are all identical:         These examples are all identical:
4069    
4070           (ring), \1           (ring), \1
4071           (ring), \g1           (ring), \g1
4072           (ring), \g{1}           (ring), \g{1}
4073    
4074         A positive number specifies an absolute reference without the ambiguity         An  unsigned number specifies an absolute reference without the ambigu-
4075         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
4076         digits follow the reference. A negative number is a relative reference.         digits follow the reference. A negative number is a relative reference.
4077         Consider this example:         Consider this example:
4078    
4079           (abc(def)ghi)\g{-1}           (abc(def)ghi)\g{-1}
4080    
4081         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-
4082         ing subpattern before \g, that is, is it equivalent to  \2.  Similarly,         ing  subpattern  before \g, that is, is it equivalent to \2. Similarly,
4083         \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
4084         helpful in long patterns, and also in  patterns  that  are  created  by         helpful  in  long  patterns,  and  also in patterns that are created by
4085         joining together fragments that contain references within themselves.         joining together fragments that contain references within themselves.
4086    
4087         A  back  reference matches whatever actually matched the capturing sub-         A back reference matches whatever actually matched the  capturing  sub-
4088         pattern in the current subject string, rather  than  anything  matching         pattern  in  the  current subject string, rather than anything matching
4089         the subpattern itself (see "Subpatterns as subroutines" below for a way         the subpattern itself (see "Subpatterns as subroutines" below for a way
4090         of doing that). So the pattern         of doing that). So the pattern
4091    
4092           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
4093    
4094         matches "sense and sensibility" and "response and responsibility",  but         matches  "sense and sensibility" and "response and responsibility", but
4095         not  "sense and responsibility". If caseful matching is in force at the         not "sense and responsibility". If caseful matching is in force at  the
4096         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-
4097         ple,         ple,
4098    
4099           ((?i)rah)\s+\1           ((?i)rah)\s+\1
4100    
4101         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
4102         original capturing subpattern is matched caselessly.         original capturing subpattern is matched caselessly.
4103    
4104         There are several different ways of writing back  references  to  named         There  are  several  different ways of writing back references to named
4105         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
4106         \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
4107         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
4108         and named references, is also supported. We  could  rewrite  the  above         and  named  references,  is  also supported. We could rewrite the above
4109         example in any of the following ways:         example in any of the following ways:
4110    
4111           (?<p1>(?i)rah)\s+\k<p1>           (?<p1>(?i)rah)\s+\k<p1>
# Line 3910  BACK REFERENCES Line 4113  BACK REFERENCES
4113           (?P<p1>(?i)rah)\s+(?P=p1)           (?P<p1>(?i)rah)\s+(?P=p1)
4114           (?<p1>(?i)rah)\s+\g{p1}           (?<p1>(?i)rah)\s+\g{p1}
4115    
4116         A  subpattern  that  is  referenced  by  name may appear in the pattern         A subpattern that is referenced by  name  may  appear  in  the  pattern
4117         before or after the reference.         before or after the reference.
4118    
4119         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
4120         subpattern  has  not actually been used in a particular match, any back         subpattern has not actually been used in a particular match,  any  back
4121         references to it always fail. For example, the pattern         references to it always fail. For example, the pattern
4122    
4123           (a|(bc))\2           (a|(bc))\2
4124    
4125         always fails if it starts to match "a" rather than "bc". Because  there         always  fails if it starts to match "a" rather than "bc". Because there
4126         may  be  many  capturing parentheses in a pattern, all digits following         may be many capturing parentheses in a pattern,  all  digits  following
4127         the backslash are taken as part of a potential back  reference  number.         the  backslash  are taken as part of a potential back reference number.
4128         If the pattern continues with a digit character, some delimiter must be         If the pattern continues with a digit character, some delimiter must be
4129         used to terminate the back reference. If the  PCRE_EXTENDED  option  is         used  to  terminate  the back reference. If the PCRE_EXTENDED option is
4130         set,  this  can  be  whitespace.  Otherwise an empty comment (see "Com-         set, this can be whitespace.  Otherwise an  empty  comment  (see  "Com-
4131         ments" below) can be used.         ments" below) can be used.
4132    
4133         A back reference that occurs inside the parentheses to which it  refers         A  back reference that occurs inside the parentheses to which it refers
4134         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
4135         matches.  However, such references can be useful inside  repeated  sub-         matches.   However,  such references can be useful inside repeated sub-
4136         patterns. For example, the pattern         patterns. For example, the pattern
4137    
4138           (a|b\1)+           (a|b\1)+
4139    
4140         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-
4141         ation of the subpattern,  the  back  reference  matches  the  character         ation  of  the  subpattern,  the  back  reference matches the character
4142         string  corresponding  to  the previous iteration. In order for this to         string corresponding to the previous iteration. In order  for  this  to
4143         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
4144         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
4145         the example above, or by a quantifier with a minimum of zero.         the example above, or by a quantifier with a minimum of zero.
4146    
4147    
4148  ASSERTIONS  ASSERTIONS
4149    
4150         An assertion is a test on the characters  following  or  preceding  the         An  assertion  is  a  test on the characters following or preceding the
4151         current  matching  point that does not actually consume any characters.         current matching point that does not actually consume  any  characters.
4152         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
4153         described above.         described above.
4154    
4155         More  complicated  assertions  are  coded as subpatterns. There are two         More complicated assertions are coded as  subpatterns.  There  are  two
4156         kinds: those that look ahead of the current  position  in  the  subject         kinds:  those  that  look  ahead of the current position in the subject
4157         string,  and  those  that  look  behind  it. An assertion subpattern is         string, and those that look  behind  it.  An  assertion  subpattern  is
4158         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
4159         matching position to be changed.         matching position to be changed.
4160    
4161         Assertion  subpatterns  are  not  capturing subpatterns, and may not be         Assertion subpatterns are not capturing subpatterns,  and  may  not  be
4162         repeated, because it makes no sense to assert the  same  thing  several         repeated,  because  it  makes no sense to assert the same thing several
4163         times.  If  any kind of assertion contains capturing subpatterns within         times. If any kind of assertion contains capturing  subpatterns  within
4164         it, these are counted for the purposes of numbering the capturing  sub-         it,  these are counted for the purposes of numbering the capturing sub-
4165         patterns in the whole pattern.  However, substring capturing is carried         patterns in the whole pattern.  However, substring capturing is carried
4166         out only for positive assertions, because it does not  make  sense  for         out  only  for  positive assertions, because it does not make sense for
4167         negative assertions.         negative assertions.
4168    
4169     Lookahead assertions     Lookahead assertions
# Line 3970  ASSERTIONS Line 4173  ASSERTIONS
4173    
4174           \w+(?=;)           \w+(?=;)
4175    
4176         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-
4177         colon in the match, and         colon in the match, and
4178    
4179           foo(?!bar)           foo(?!bar)
4180    
4181         matches  any  occurrence  of  "foo" that is not followed by "bar". Note         matches any occurrence of "foo" that is not  followed  by  "bar".  Note
4182         that the apparently similar pattern         that the apparently similar pattern
4183    
4184           (?!foo)bar           (?!foo)bar
4185    
4186         does not find an occurrence of "bar"  that  is  preceded  by  something         does  not  find  an  occurrence  of "bar" that is preceded by something
4187         other  than "foo"; it finds any occurrence of "bar" whatsoever, because         other than "foo"; it finds any occurrence of "bar" whatsoever,  because
4188         the assertion (?!foo) is always true when the next three characters are         the assertion (?!foo) is always true when the next three characters are
4189         "bar". A lookbehind assertion is needed to achieve the other effect.         "bar". A lookbehind assertion is needed to achieve the other effect.
4190    
4191         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
4192         most convenient way to do it is  with  (?!)  because  an  empty  string         most  convenient  way  to  do  it  is with (?!) because an empty string
4193         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
4194         string must always fail.         string must always fail.
4195    
4196     Lookbehind assertions     Lookbehind assertions
4197    
4198         Lookbehind assertions start with (?<= for positive assertions and  (?<!         Lookbehind  assertions start with (?<= for positive assertions and (?<!
4199         for negative assertions. For example,         for negative assertions. For example,
4200    
4201           (?<!foo)bar           (?<!foo)bar
4202    
4203         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
4204         contents of a lookbehind assertion are restricted  such  that  all  the         contents  of  a  lookbehind  assertion are restricted such that all the
4205         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-
4206         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
4207         fixed length. Thus         fixed length. Thus
4208    
4209           (?<=bullock|donkey)           (?<=bullock|donkey)
# Line 4009  ASSERTIONS Line 4212  ASSERTIONS
4212    
4213           (?<!dogs?|cats?)           (?<!dogs?|cats?)
4214    
4215         causes  an  error at compile time. Branches that match different length         causes an error at compile time. Branches that match  different  length
4216         strings are permitted only at the top level of a lookbehind  assertion.         strings  are permitted only at the top level of a lookbehind assertion.
4217         This  is  an  extension  compared  with  Perl (at least for 5.8), which         This is an extension compared with  Perl  (at  least  for  5.8),  which
4218         requires all branches to match the same length of string. An  assertion         requires  all branches to match the same length of string. An assertion
4219         such as         such as
4220    
4221           (?<=ab(c|de))           (?<=ab(c|de))
4222    
4223         is  not  permitted,  because  its single top-level branch can match two         is not permitted, because its single top-level  branch  can  match  two
4224         different lengths, but it is acceptable if rewritten to  use  two  top-         different  lengths,  but  it is acceptable if rewritten to use two top-
4225         level branches:         level branches:
4226    
4227           (?<=abc|abde)           (?<=abc|abde)
4228    
4229         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
4230         instead of a lookbehind assertion; this is not restricted to  a  fixed-         instead  of  a lookbehind assertion; this is not restricted to a fixed-
4231         length.         length.
4232    
4233         The  implementation  of lookbehind assertions is, for each alternative,         The implementation of lookbehind assertions is, for  each  alternative,
4234         to temporarily move the current position back by the fixed  length  and         to  temporarily  move the current position back by the fixed length and
4235         then try to match. If there are insufficient characters before the cur-         then try to match. If there are insufficient characters before the cur-
4236         rent position, the assertion fails.         rent position, the assertion fails.
4237    
4238         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
4239         mode)  to appear in lookbehind assertions, because it makes it impossi-         mode) to appear in lookbehind assertions, because it makes it  impossi-
4240         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,
4241         which can match different numbers of bytes, are also not permitted.         which can match different numbers of bytes, are also not permitted.
4242    
4243         Possessive  quantifiers  can  be  used  in  conjunction with lookbehind         Possessive quantifiers can  be  used  in  conjunction  with  lookbehind
4244         assertions to specify efficient matching at  the  end  of  the  subject         assertions  to  specify  efficient  matching  at the end of the subject
4245         string. Consider a simple pattern such as         string. Consider a simple pattern such as
4246    
4247           abcd$           abcd$
4248    
4249         when  applied  to  a  long string that does not match. Because matching         when applied to a long string that does  not  match.  Because  matching
4250         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
4251         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
4252         pattern is specified as         pattern is specified as
4253    
4254           ^.*abcd$           ^.*abcd$
4255    
4256         the initial .* matches the entire string at first, but when this  fails         the  initial .* matches the entire string at first, but when this fails
4257         (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
4258         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
4259         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,
4260         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
4261    
4262           ^.*+(?<=abcd)           ^.*+(?<=abcd)
4263    
4264         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
4265         entire  string.  The subsequent lookbehind assertion does a single test         entire string. The subsequent lookbehind assertion does a  single  test
4266         on the last four characters. If it fails, the match fails  immediately.         on  the last four characters. If it fails, the match fails immediately.
4267         For  long  strings, this approach makes a significant difference to the         For long strings, this approach makes a significant difference  to  the
4268         processing time.         processing time.
4269    
4270     Using multiple assertions     Using multiple assertions
# Line 4070  ASSERTIONS Line 4273  ASSERTIONS
4273    
4274           (?<=\d{3})(?<!999)foo           (?<=\d{3})(?<!999)foo
4275    
4276         matches "foo" preceded by three digits that are not "999". Notice  that         matches  "foo" preceded by three digits that are not "999". Notice that
4277         each  of  the  assertions is applied independently at the same point in         each of the assertions is applied independently at the  same  point  in
4278         the subject string. First there is a  check  that  the  previous  three         the  subject  string.  First  there  is a check that the previous three
4279         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
4280         three characters are not "999".  This pattern does not match "foo" pre-         three characters are not "999".  This pattern does not match "foo" pre-
4281         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
4282         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-
4283         foo". A pattern to do that is         foo". A pattern to do that is
4284    
4285           (?<=\d{3}...)(?<!999)foo           (?<=\d{3}...)(?<!999)foo
4286    
4287         This  time  the  first assertion looks at the preceding six characters,         This time the first assertion looks at the  preceding  six  characters,
4288         checking that the first three are digits, and then the second assertion         checking that the first three are digits, and then the second assertion
4289         checks that the preceding three characters are not "999".         checks that the preceding three characters are not "999".
4290    
# Line 4089  ASSERTIONS Line 4292  ASSERTIONS
4292    
4293           (?<=(?<!foo)bar)baz           (?<=(?<!foo)bar)baz
4294    
4295         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
4296         is not preceded by "foo", while         is not preceded by "foo", while
4297    
4298           (?<=\d{3}(?!999)...)foo           (?<=\d{3}(?!999)...)foo
4299    
4300         is another pattern that matches "foo" preceded by three digits and  any         is  another pattern that matches "foo" preceded by three digits and any
4301         three characters that are not "999".         three characters that are not "999".
4302    
4303    
4304  CONDITIONAL SUBPATTERNS  CONDITIONAL SUBPATTERNS
4305    
4306         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-
4307         ditionally or to choose between two alternative subpatterns,  depending         ditionally  or to choose between two alternative subpatterns, depending
4308         on  the result of an assertion, or whether a previous capturing subpat-         on the result of an assertion, or whether a previous capturing  subpat-
4309         tern matched or not. The two possible forms of  conditional  subpattern         tern  matched  or not. The two possible forms of conditional subpattern
4310         are         are
4311    
4312           (?(condition)yes-pattern)           (?(condition)yes-pattern)
4313           (?(condition)yes-pattern|no-pattern)           (?(condition)yes-pattern|no-pattern)
4314    
4315         If  the  condition is satisfied, the yes-pattern is used; otherwise the         If the condition is satisfied, the yes-pattern is used;  otherwise  the
4316         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-
4317         tives in the subpattern, a compile-time error occurs.         tives in the subpattern, a compile-time error occurs.
4318    
4319         There  are  four  kinds of condition: references to subpatterns, refer-         There are four kinds of condition: references  to  subpatterns,  refer-
4320         ences to recursion, a pseudo-condition called DEFINE, and assertions.         ences to recursion, a pseudo-condition called DEFINE, and assertions.
4321    
4322     Checking for a used subpattern by number     Checking for a used subpattern by number
4323    
4324         If the text between the parentheses consists of a sequence  of  digits,         If  the  text between the parentheses consists of a sequence of digits,
4325         the  condition  is  true if the capturing subpattern of that number has         the condition is true if the capturing subpattern of  that  number  has
4326         previously matched. An alternative notation is to  precede  the  digits         previously  matched.  An  alternative notation is to precede the digits
4327         with a plus or minus sign. In this case, the subpattern number is rela-         with a plus or minus sign. In this case, the subpattern number is rela-
4328         tive rather than absolute.  The most recently opened parentheses can be         tive rather than absolute.  The most recently opened parentheses can be
4329         referenced  by  (?(-1),  the  next most recent by (?(-2), and so on. In         referenced by (?(-1), the next most recent by (?(-2),  and  so  on.  In
4330         looping constructs it can also make sense to refer to subsequent groups         looping constructs it can also make sense to refer to subsequent groups
4331         with constructs such as (?(+2).         with constructs such as (?(+2).
4332    
4333         Consider  the  following  pattern, which contains non-significant white         Consider the following pattern, which  contains  non-significant  white
4334         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
4335         divide it into three parts for ease of discussion:         divide it into three parts for ease of discussion:
4336    
4337           ( \( )?    [^()]+    (?(1) \) )           ( \( )?    [^()]+    (?(1) \) )
4338    
4339         The  first  part  matches  an optional opening parenthesis, and if that         The first part matches an optional opening  parenthesis,  and  if  that
4340         character is present, sets it as the first captured substring. The sec-         character is present, sets it as the first captured substring. The sec-
4341         ond  part  matches one or more characters that are not parentheses. The         ond part matches one or more characters that are not  parentheses.  The
4342         third part is a conditional subpattern that tests whether the first set         third part is a conditional subpattern that tests whether the first set
4343         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
4344         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-
4345         tern  is  executed  and  a  closing parenthesis is required. Otherwise,         tern is executed and a  closing  parenthesis  is  required.  Otherwise,
4346         since no-pattern is not present, the  subpattern  matches  nothing.  In         since  no-pattern  is  not  present, the subpattern matches nothing. In
4347         other  words,  this  pattern  matches  a  sequence  of non-parentheses,         other words,  this  pattern  matches  a  sequence  of  non-parentheses,
4348         optionally enclosed in parentheses.         optionally enclosed in parentheses.
4349    
4350         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
4351         relative reference:         relative reference:
4352    
4353           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...
4354    
4355         This  makes  the  fragment independent of the parentheses in the larger         This makes the fragment independent of the parentheses  in  the  larger
4356         pattern.         pattern.
4357    
4358     Checking for a used subpattern by name     Checking for a used subpattern by name
4359    
4360         Perl uses the syntax (?(<name>)...) or (?('name')...)  to  test  for  a         Perl  uses  the  syntax  (?(<name>)...) or (?('name')...) to test for a
4361         used  subpattern  by  name.  For compatibility with earlier versions of         used subpattern by name. For compatibility  with  earlier  versions  of
4362         PCRE, which had this facility before Perl, the syntax  (?(name)...)  is         PCRE,  which  had this facility before Perl, the syntax (?(name)...) is
4363         also  recognized. However, there is a possible ambiguity with this syn-         also recognized. However, there is a possible ambiguity with this  syn-
4364         tax, because subpattern names may  consist  entirely  of  digits.  PCRE         tax,  because  subpattern  names  may  consist entirely of digits. PCRE
4365         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
4366         consists entirely of digits, PCRE looks for a subpattern of  that  num-         consists  entirely  of digits, PCRE looks for a subpattern of that num-
4367         ber,  which must be greater than zero. Using subpattern names that con-         ber, which must be greater than zero. Using subpattern names that  con-
4368         sist entirely of digits is not recommended.         sist entirely of digits is not recommended.
4369    
4370         Rewriting the above example to use a named subpattern gives this:         Rewriting the above example to use a named subpattern gives this:
# Line 4172  CONDITIONAL SUBPATTERNS Line 4375  CONDITIONAL SUBPATTERNS
4375     Checking for pattern recursion     Checking for pattern recursion
4376    
4377         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
4378         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
4379         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-
4380         sand follow the letter R, for example:         sand follow the letter R, for example:
4381    
4382           (?(R3)...) or (?(R&name)...)           (?(R3)...) or (?(R&name)...)
4383    
4384         the  condition is true if the most recent recursion is into the subpat-         the condition is true if the most recent recursion is into the  subpat-
4385         tern whose number or name is given. This condition does not  check  the         tern  whose  number or name is given. This condition does not check the
4386         entire recursion stack.         entire recursion stack.
4387    
4388         At  "top  level", all these recursion test conditions are false. Recur-         At "top level", all these recursion test conditions are  false.  Recur-
4389         sive patterns are described below.         sive patterns are described below.
4390    
4391     Defining subpatterns for use by reference only     Defining subpatterns for use by reference only
4392    
4393         If the condition is the string (DEFINE), and  there  is  no  subpattern         If  the  condition  is  the string (DEFINE), and there is no subpattern
4394         with  the  name  DEFINE,  the  condition is always false. In this case,         with the name DEFINE, the condition is  always  false.  In  this  case,
4395         there may be only one alternative  in  the  subpattern.  It  is  always         there  may  be  only  one  alternative  in the subpattern. It is always
4396         skipped  if  control  reaches  this  point  in the pattern; the idea of         skipped if control reaches this point  in  the  pattern;  the  idea  of
4397         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-
4398         erenced  from elsewhere. (The use of "subroutines" is described below.)         erenced from elsewhere. (The use of "subroutines" is described  below.)
4399         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
4400         this (ignore whitespace and line breaks):         this (ignore whitespace and line breaks):
4401    
4402           (?(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) )
4403           \b (?&byte) (\.(?&byte)){3} \b           \b (?&byte) (\.(?&byte)){3} \b
4404    
4405         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
4406         group named "byte" is defined. This matches an individual component  of         group  named "byte" is defined. This matches an individual component of
4407         an  IPv4  address  (a number less than 256). When matching takes place,         an IPv4 address (a number less than 256). When  matching  takes  place,
4408         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
4409         condition.         condition.
4410    
4411         The rest of the pattern uses references to the named group to match the         The rest of the pattern uses references to the named group to match the
4412         four dot-separated components of an IPv4 address, insisting on  a  word         four  dot-separated  components of an IPv4 address, insisting on a word
4413         boundary at each end.         boundary at each end.
4414    
4415     Assertion conditions     Assertion conditions
4416    
4417         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
4418         assertion.  This may be a positive or negative lookahead or  lookbehind         assertion.   This may be a positive or negative lookahead or lookbehind
4419         assertion.  Consider  this  pattern,  again  containing non-significant         assertion. Consider  this  pattern,  again  containing  non-significant
4420         white space, and with the two alternatives on the second line:         white space, and with the two alternatives on the second line:
4421    
4422           (?(?=[^a-z]*[a-z])           (?(?=[^a-z]*[a-z])
4423           \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} )
4424    
4425         The condition  is  a  positive  lookahead  assertion  that  matches  an         The  condition  is  a  positive  lookahead  assertion  that  matches an
4426         optional  sequence of non-letters followed by a letter. In other words,         optional sequence of non-letters followed by a letter. In other  words,
4427         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
4428         letter  is found, the subject is matched against the first alternative;         letter is found, the subject is matched against the first  alternative;
4429         otherwise it is  matched  against  the  second.  This  pattern  matches         otherwise  it  is  matched  against  the  second.  This pattern matches
4430         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
4431         letters and dd are digits.         letters and dd are digits.
4432    
4433    
4434  COMMENTS  COMMENTS
4435    
4436         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
4437         next  closing  parenthesis.  Nested  parentheses are not permitted. The         next closing parenthesis. Nested parentheses  are  not  permitted.  The
4438         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
4439         at all.         at all.
4440    
4441         If  the PCRE_EXTENDED option is set, an unescaped # character outside a         If the PCRE_EXTENDED option is set, an unescaped # character outside  a
4442         character class introduces a  comment  that  continues  to  immediately         character  class  introduces  a  comment  that continues to immediately
4443         after the next newline in the pattern.         after the next newline in the pattern.
4444    
4445    
4446  RECURSIVE PATTERNS  RECURSIVE PATTERNS
4447    
4448         Consider  the problem of matching a string in parentheses, allowing for         Consider the problem of matching a string in parentheses, allowing  for
4449         unlimited nested parentheses. Without the use of  recursion,  the  best         unlimited  nested  parentheses.  Without the use of recursion, the best
4450         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
4451         depth of nesting. It is not possible to  handle  an  arbitrary  nesting         depth  of  nesting.  It  is not possible to handle an arbitrary nesting
4452         depth.         depth.
4453    
4454         For some time, Perl has provided a facility that allows regular expres-         For some time, Perl has provided a facility that allows regular expres-
4455         sions to recurse (amongst other things). It does this by  interpolating         sions  to recurse (amongst other things). It does this by interpolating
4456         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
4457         expression itself. A Perl pattern using code interpolation to solve the         expression itself. A Perl pattern using code interpolation to solve the
4458         parentheses problem can be created like this:         parentheses problem can be created like this:
4459    
# Line 4260  RECURSIVE PATTERNS Line 4463  RECURSIVE PATTERNS
4463         refers recursively to the pattern in which it appears.         refers recursively to the pattern in which it appears.
4464    
4465         Obviously, PCRE cannot support the interpolation of Perl code. Instead,         Obviously, PCRE cannot support the interpolation of Perl code. Instead,
4466         it  supports  special  syntax  for recursion of the entire pattern, and         it supports special syntax for recursion of  the  entire  pattern,  and
4467         also for individual subpattern recursion.  After  its  introduction  in         also  for  individual  subpattern  recursion. After its introduction in
4468         PCRE  and  Python,  this  kind of recursion was introduced into Perl at         PCRE and Python, this kind of recursion was  introduced  into  Perl  at
4469         release 5.10.         release 5.10.
4470    
4471         A special item that consists of (? followed by a  number  greater  than         A  special  item  that consists of (? followed by a number greater than
4472         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
4473         the given number, provided that it occurs inside that  subpattern.  (If         the  given  number, provided that it occurs inside that subpattern. (If
4474         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-
4475         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
4476         regular expression.         regular expression.
4477    
4478         In  PCRE (like Python, but unlike Perl), a recursive subpattern call is         In PCRE (like Python, but unlike Perl), a recursive subpattern call  is
4479         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
4480         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
4481         alternatives and there is a subsequent matching failure.         alternatives and there is a subsequent matching failure.
4482    
4483         This PCRE pattern solves the nested  parentheses  problem  (assume  the         This  PCRE  pattern  solves  the nested parentheses problem (assume the
4484         PCRE_EXTENDED option is set so that white space is ignored):         PCRE_EXTENDED option is set so that white space is ignored):
4485    
4486           \( ( (?>[^()]+) | (?R) )* \)           \( ( (?>[^()]+) | (?R) )* \)
4487    
4488         First  it matches an opening parenthesis. Then it matches any number of         First it matches an opening parenthesis. Then it matches any number  of
4489         substrings which can either be a  sequence  of  non-parentheses,  or  a         substrings  which  can  either  be  a sequence of non-parentheses, or a
4490         recursive  match  of the pattern itself (that is, a correctly parenthe-         recursive match of the pattern itself (that is, a  correctly  parenthe-
4491         sized substring).  Finally there is a closing parenthesis.         sized substring).  Finally there is a closing parenthesis.
4492    
4493         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
4494         the entire pattern, so instead you could use this:         the entire pattern, so instead you could use this:
4495    
4496           ( \( ( (?>[^()]+) | (?1) )* \) )           ( \( ( (?>[^()]+) | (?1) )* \) )
4497    
4498         We  have  put the pattern into parentheses, and caused the recursion to         We have put the pattern into parentheses, and caused the  recursion  to
4499         refer to them instead of the whole pattern.         refer to them instead of the whole pattern.
4500    
4501         In a larger pattern,  keeping  track  of  parenthesis  numbers  can  be         In  a  larger  pattern,  keeping  track  of  parenthesis numbers can be
4502         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
4503         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
4504         (?-2) to refer to the second most recently opened parentheses preceding         (?-2) to refer to the second most recently opened parentheses preceding
4505         the recursion. In other  words,  a  negative  number  counts  capturing         the  recursion.  In  other  words,  a  negative number counts capturing
4506         parentheses leftwards from the point at which it is encountered.         parentheses leftwards from the point at which it is encountered.
4507    
4508         It  is  also  possible  to refer to subsequently opened parentheses, by         It is also possible to refer to  subsequently  opened  parentheses,  by
4509         writing references such as (?+2). However, these  cannot  be  recursive         writing  references  such  as (?+2). However, these cannot be recursive
4510         because  the  reference  is  not inside the parentheses that are refer-         because the reference is not inside the  parentheses  that  are  refer-
4511         enced. They are always "subroutine" calls, as  described  in  the  next         enced.  They  are  always  "subroutine" calls, as described in the next
4512         section.         section.
4513    
4514         An  alternative  approach is to use named parentheses instead. The Perl         An alternative approach is to use named parentheses instead.  The  Perl
4515         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
4516         supported. We could rewrite the above example as follows:         supported. We could rewrite the above example as follows:
4517    
4518           (?<pn> \( ( (?>[^()]+) | (?&pn) )* \) )           (?<pn> \( ( (?>[^()]+) | (?&pn) )* \) )
4519    
4520         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
4521         one is used.         one is used.
4522    
4523         This particular example pattern that we have been looking  at  contains         This  particular  example pattern that we have been looking at contains
4524         nested  unlimited repeats, and so the use of atomic grouping for match-         nested unlimited repeats, and so the use of atomic grouping for  match-
4525         ing strings of non-parentheses is important when applying  the  pattern         ing  strings  of non-parentheses is important when applying the pattern
4526         to strings that do not match. For example, when this pattern is applied         to strings that do not match. For example, when this pattern is applied
4527         to         to
4528    
4529           (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()           (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
4530    
4531         it yields "no match" quickly. However, if atomic grouping is not  used,         it  yields "no match" quickly. However, if atomic grouping is not used,
4532         the  match  runs  for a very long time indeed because there are so many         the match runs for a very long time indeed because there  are  so  many
4533         different ways the + and * repeats can carve up the  subject,  and  all         different  ways  the  + and * repeats can carve up the subject, and all
4534         have to be tested before failure can be reported.         have to be tested before failure can be reported.
4535    
4536         At the end of a match, the values set for any capturing subpatterns are         At the end of a match, the values set for any capturing subpatterns are
4537         those from the outermost level of the recursion at which the subpattern         those from the outermost level of the recursion at which the subpattern
4538         value  is  set.   If  you want to obtain intermediate values, a callout         value is set.  If you want to obtain  intermediate  values,  a  callout
4539         function can be used (see below and the pcrecallout documentation).  If         function  can be used (see below and the pcrecallout documentation). If
4540         the pattern above is matched against         the pattern above is matched against
4541    
4542           (ab(cd)ef)           (ab(cd)ef)
4543    
4544         the  value  for  the  capturing  parentheses is "ef", which is the last         the value for the capturing parentheses is  "ef",  which  is  the  last
4545         value taken on at the top level. If additional parentheses  are  added,         value  taken  on at the top level. If additional parentheses are added,
4546         giving         giving
4547    
4548           \( ( ( (?>[^()]+) | (?R) )* ) \)           \( ( ( (?>[^()]+) | (?R) )* ) \)
4549              ^                        ^              ^                        ^
4550              ^                        ^              ^                        ^
4551    
4552         the  string  they  capture is "ab(cd)ef", the contents of the top level         the string they capture is "ab(cd)ef", the contents of  the  top  level
4553         parentheses. If there are more than 15 capturing parentheses in a  pat-         parentheses.  If there are more than 15 capturing parentheses in a pat-
4554         tern, PCRE has to obtain extra memory to store data during a recursion,         tern, PCRE has to obtain extra memory to store data during a recursion,
4555         which it does by using pcre_malloc, freeing  it  via  pcre_free  after-         which  it  does  by  using pcre_malloc, freeing it via pcre_free after-
4556         wards.  If  no  memory  can  be  obtained,  the  match  fails  with the         wards. If  no  memory  can  be  obtained,  the  match  fails  with  the
4557         PCRE_ERROR_NOMEMORY error.         PCRE_ERROR_NOMEMORY error.
4558    
4559         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
4560         recursion.   Consider  this pattern, which matches text in angle brack-         recursion.  Consider this pattern, which matches text in  angle  brack-
4561         ets, allowing for arbitrary nesting. Only digits are allowed in  nested         ets,  allowing for arbitrary nesting. Only digits are allowed in nested
4562         brackets  (that is, when recursing), whereas any characters are permit-         brackets (that is, when recursing), whereas any characters are  permit-
4563         ted at the outer level.         ted at the outer level.
4564    
4565           < (?: (?(R) \d++  | [^<>]*+) | (?R)) * >           < (?: (?(R) \d++  | [^<>]*+) | (?R)) * >
4566    
4567         In this pattern, (?(R) is the start of a conditional  subpattern,  with         In  this  pattern, (?(R) is the start of a conditional subpattern, with
4568         two  different  alternatives for the recursive and non-recursive cases.         two different alternatives for the recursive and  non-recursive  cases.
4569         The (?R) item is the actual recursive call.         The (?R) item is the actual recursive call.
4570    
4571    
4572  SUBPATTERNS AS SUBROUTINES  SUBPATTERNS AS SUBROUTINES
4573    
4574         If the syntax for a recursive subpattern reference (either by number or         If the syntax for a recursive subpattern reference (either by number or
4575         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-
4576         ates like a subroutine in a programming language. The "called"  subpat-         ates  like a subroutine in a programming language. The "called" subpat-
4577         tern may be defined before or after the reference. A numbered reference         tern may be defined before or after the reference. A numbered reference
4578         can be absolute or relative, as in these examples:         can be absolute or relative, as in these examples:
4579    
# Line 4382  SUBPATTERNS AS SUBROUTINES Line 4585  SUBPATTERNS AS SUBROUTINES
4585    
4586           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
4587    
4588         matches "sense and sensibility" and "response and responsibility",  but         matches  "sense and sensibility" and "response and responsibility", but
4589         not "sense and responsibility". If instead the pattern         not "sense and responsibility". If instead the pattern
4590    
4591           (sens|respons)e and (?1)ibility           (sens|respons)e and (?1)ibility
4592    
4593         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
4594         two strings. Another example is  given  in  the  discussion  of  DEFINE         two  strings.  Another  example  is  given  in the discussion of DEFINE
4595         above.         above.
4596    
4597         Like recursive subpatterns, a "subroutine" call is always treated as an         Like recursive subpatterns, a "subroutine" call is always treated as an
4598         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,
4599         it  is  never  re-entered, even if it contains untried alternatives and         it is never re-entered, even if it contains  untried  alternatives  and
4600         there is a subsequent matching failure.         there is a subsequent matching failure.
4601    
4602         When a subpattern is used as a subroutine, processing options  such  as         When  a  subpattern is used as a subroutine, processing options such as
4603         case-independence are fixed when the subpattern is defined. They cannot         case-independence are fixed when the subpattern is defined. They cannot
4604         be changed for different calls. For example, consider this pattern:         be changed for different calls. For example, consider this pattern:
4605    
4606           (abc)(?i:(?-1))           (abc)(?i:(?-1))
4607    
4608         It matches "abcabc". It does not match "abcABC" because the  change  of         It  matches  "abcabc". It does not match "abcABC" because the change of
4609         processing option does not affect the called subpattern.         processing option does not affect the called subpattern.
4610    
4611    
4612  CALLOUTS  CALLOUTS
4613    
4614         Perl has a feature whereby using the sequence (?{...}) causes arbitrary         Perl has a feature whereby using the sequence (?{...}) causes arbitrary
4615         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.
4616         This makes it possible, amongst other things, to extract different sub-         This makes it possible, amongst other things, to extract different sub-
4617         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-
4618         tion.         tion.
4619    
4620         PCRE provides a similar feature, but of course it cannot obey arbitrary         PCRE provides a similar feature, but of course it cannot obey arbitrary
4621         Perl code. The feature is called "callout". The caller of PCRE provides         Perl code. The feature is called "callout". The caller of PCRE provides
4622         an  external function by putting its entry point in the global variable         an external function by putting its entry point in the global  variable
4623         pcre_callout.  By default, this variable contains NULL, which  disables         pcre_callout.   By default, this variable contains NULL, which disables
4624         all calling out.         all calling out.
4625    
4626         Within  a  regular  expression,  (?C) indicates the points at which the         Within a regular expression, (?C) indicates the  points  at  which  the
4627         external function is to be called. If you want  to  identify  different         external  function  is  to be called. If you want to identify different
4628         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.
4629         The default value is zero.  For example, this pattern has  two  callout         The  default  value is zero.  For example, this pattern has two callout
4630         points:         points:
4631    
4632           (?C1)abc(?C2)def           (?C1)abc(?C2)def
4633    
4634         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
4635         automatically installed before each item in the pattern. They  are  all         automatically  installed  before each item in the pattern. They are all
4636         numbered 255.         numbered 255.
4637    
4638         During matching, when PCRE reaches a callout point (and pcre_callout is         During matching, when PCRE reaches a callout point (and pcre_callout is
4639         set), the external function is called. It is provided with  the  number         set),  the  external function is called. It is provided with the number
4640         of  the callout, the position in the pattern, and, optionally, one item         of the callout, the position in the pattern, and, optionally, one  item
4641         of data originally supplied by the caller of pcre_exec().  The  callout         of  data  originally supplied by the caller of pcre_exec(). The callout
4642         function  may cause matching to proceed, to backtrack, or to fail alto-         function may cause matching to proceed, to backtrack, or to fail  alto-
4643         gether. A complete description of the interface to the callout function         gether. A complete description of the interface to the callout function
4644         is given in the pcrecallout documentation.         is given in the pcrecallout documentation.
4645    
4646    
4647    BACTRACKING CONTROL
4648    
4649           Perl 5.10 introduced a number of "Special Backtracking Control  Verbs",
4650           which are described in the Perl documentation as "experimental and sub-
4651           ject to change or removal in a future version of Perl". It goes  on  to
4652           say:  "Their usage in production code should be noted to avoid problems
4653           during upgrades." The same remarks apply to the PCRE features described
4654           in this section.
4655    
4656           Since these verbs are specifically related to backtracking, they can be
4657           used only when the pattern is to be matched  using  pcre_exec(),  which
4658           uses  a  backtracking  algorithm. They cause an error if encountered by
4659           pcre_dfa_exec().
4660    
4661           The new verbs make use of what was previously invalid syntax: an  open-
4662           ing parenthesis followed by an asterisk. In Perl, they are generally of
4663           the form (*VERB:ARG) but PCRE does not support the use of arguments, so
4664           its  general  form is just (*VERB). Any number of these verbs may occur
4665           in a pattern. There are two kinds:
4666    
4667       Verbs that act immediately
4668    
4669           The following verbs act as soon as they are encountered:
4670    
4671              (*ACCEPT)
4672    
4673           This verb causes the match to end successfully, skipping the  remainder
4674           of  the pattern. When inside a recursion, only the innermost pattern is
4675           ended immediately. PCRE differs  from  Perl  in  what  happens  if  the
4676           (*ACCEPT)  is inside capturing parentheses. In Perl, the data so far is
4677           captured: in PCRE no data is captured. For example:
4678    
4679             A(A|B(*ACCEPT)|C)D
4680    
4681           This matches "AB", "AAD", or "ACD", but when it matches "AB",  no  data
4682           is captured.
4683    
4684             (*FAIL) or (*F)
4685    
4686           This  verb  causes the match to fail, forcing backtracking to occur. It
4687           is equivalent to (?!) but easier to read. The Perl documentation  notes
4688           that  it  is  probably  useful only when combined with (?{}) or (??{}).
4689           Those are, of course, Perl features that are not present in  PCRE.  The
4690           nearest  equivalent is the callout feature, as for example in this pat-
4691           tern:
4692    
4693             a+(?C)(*FAIL)
4694    
4695           A match with the string "aaaa" always fails, but the callout  is  taken
4696           before each backtrack happens (in this example, 10 times).
4697    
4698       Verbs that act after backtracking
4699    
4700           The following verbs do nothing when they are encountered. Matching con-
4701           tinues with what follows, but if there is no subsequent match, a  fail-
4702           ure  is  forced.   The  verbs  differ  in  exactly what kind of failure
4703           occurs.
4704    
4705             (*COMMIT)
4706    
4707           This verb causes the whole match to fail outright if the  rest  of  the
4708           pattern  does  not match. Even if the pattern is unanchored, no further
4709           attempts to find a match by advancing the start point take place.  Once
4710           (*COMMIT)  has been passed, pcre_exec() is committed to finding a match
4711           at the current starting point, or not at all. For example:
4712    
4713             a+(*COMMIT)b
4714    
4715           This matches "xxaab" but not "aacaab". It can be thought of as  a  kind
4716           of dynamic anchor, or "I've started, so I must finish."
4717    
4718             (*PRUNE)
4719    
4720           This  verb causes the match to fail at the current position if the rest
4721           of the pattern does not match. If the pattern is unanchored, the normal
4722           "bumpalong"  advance to the next starting character then happens. Back-
4723           tracking can occur as usual to the left of (*PRUNE), or  when  matching
4724           to  the right of (*PRUNE), but if there is no match to the right, back-
4725           tracking cannot cross (*PRUNE).  In simple cases, the use  of  (*PRUNE)
4726           is just an alternative to an atomic group or possessive quantifier, but
4727           there are some uses of (*PRUNE) that cannot be expressed in  any  other
4728           way.
4729    
4730             (*SKIP)
4731    
4732           This  verb  is like (*PRUNE), except that if the pattern is unanchored,
4733           the "bumpalong" advance is not to the next character, but to the  posi-
4734           tion  in  the  subject where (*SKIP) was encountered. (*SKIP) signifies
4735           that whatever text was matched leading up to it cannot  be  part  of  a
4736           successful match. Consider:
4737    
4738             a+(*SKIP)b
4739    
4740           If  the  subject  is  "aaaac...",  after  the first match attempt fails
4741           (starting at the first character in the  string),  the  starting  point
4742           skips on to start the next attempt at "c". Note that a possessive quan-
4743           tifer does not have the same effect in this example; although it  would
4744           suppress  backtracking  during  the  first  match  attempt,  the second
4745           attempt would start at the second character instead of skipping  on  to
4746           "c".
4747    
4748             (*THEN)
4749    
4750           This verb causes a skip to the next alternation if the rest of the pat-
4751           tern does not match. That is, it cancels pending backtracking, but only
4752           within  the  current  alternation.  Its name comes from the observation
4753           that it can be used for a pattern-based if-then-else block:
4754    
4755             ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
4756    
4757           If the COND1 pattern matches, FOO is tried (and possibly further  items
4758           after  the  end  of  the group if FOO succeeds); on failure the matcher
4759           skips to the second alternative and tries COND2,  without  backtracking
4760           into  COND1.  If  (*THEN)  is  used outside of any alternation, it acts
4761           exactly like (*PRUNE).
4762    
4763    
4764  SEE ALSO  SEE ALSO
4765    
4766         pcreapi(3), pcrecallout(3), pcrematching(3), pcre(3).         pcreapi(3), pcrecallout(3), pcrematching(3), pcre(3).
# Line 4455  AUTHOR Line 4775  AUTHOR
4775    
4776  REVISION  REVISION
4777    
4778         Last updated: 29 May 2007         Last updated: 21 August 2007
4779           Copyright (c) 1997-2007 University of Cambridge.
4780    ------------------------------------------------------------------------------
4781    
4782    
4783    PCRESYNTAX(3)                                                    PCRESYNTAX(3)
4784    
4785    
4786    NAME
4787           PCRE - Perl-compatible regular expressions
4788    
4789    
4790    PCRE REGULAR EXPRESSION SYNTAX SUMMARY
4791    
4792           The  full syntax and semantics of the regular expressions that are sup-
4793           ported by PCRE are described in  the  pcrepattern  documentation.  This
4794           document contains just a quick-reference summary of the syntax.
4795    
4796    
4797    QUOTING
4798    
4799             \x         where x is non-alphanumeric is a literal x
4800             \Q...\E    treat enclosed characters as literal
4801    
4802    
4803    CHARACTERS
4804    
4805             \a         alarm, that is, the BEL character (hex 07)
4806             \cx        "control-x", where x is any character
4807             \e         escape (hex 1B)
4808             \f         formfeed (hex 0C)
4809             \n         newline (hex 0A)
4810             \r         carriage return (hex 0D)
4811             \t         tab (hex 09)
4812             \ddd       character with octal code ddd, or backreference
4813             \xhh       character with hex code hh
4814             \x{hhh..}  character with hex code hhh..
4815    
4816    
4817    CHARACTER TYPES
4818    
4819             .          any character except newline;
4820                          in dotall mode, any character whatsoever
4821             \C         one byte, even in UTF-8 mode (best avoided)
4822             \d         a decimal digit
4823             \D         a character that is not a decimal digit
4824             \h         a horizontal whitespace character
4825             \H         a character that is not a horizontal whitespace character
4826             \p{xx}     a character with the xx property
4827             \P{xx}     a character without the xx property
4828             \R         a newline sequence
4829             \s         a whitespace character
4830             \S         a character that is not a whitespace character
4831             \v         a vertical whitespace character
4832             \V         a character that is not a vertical whitespace character
4833             \w         a "word" character
4834             \W         a "non-word" character
4835             \X         an extended Unicode sequence
4836    
4837           In PCRE, \d, \D, \s, \S, \w, and \W recognize only ASCII characters.
4838    
4839    
4840    GENERAL CATEGORY PROPERTY CODES FOR \p and \P
4841    
4842             C          Other
4843             Cc         Control
4844             Cf         Format
4845             Cn         Unassigned
4846             Co         Private use
4847             Cs         Surrogate
4848    
4849             L          Letter
4850             Ll         Lower case letter
4851             Lm         Modifier letter
4852             Lo         Other letter
4853             Lt         Title case letter
4854             Lu         Upper case letter
4855             L&         Ll, Lu, or Lt
4856    
4857             M          Mark
4858             Mc         Spacing mark
4859             Me         Enclosing mark
4860             Mn         Non-spacing mark
4861    
4862             N          Number
4863             Nd         Decimal number
4864             Nl         Letter number
4865             No         Other number
4866    
4867             P          Punctuation
4868             Pc         Connector punctuation
4869             Pd         Dash punctuation
4870             Pe         Close punctuation
4871             Pf         Final punctuation
4872             Pi         Initial punctuation
4873             Po         Other punctuation
4874             Ps         Open punctuation
4875    
4876             S          Symbol
4877             Sc         Currency symbol
4878             Sk         Modifier symbol
4879             Sm         Mathematical symbol
4880             So         Other symbol
4881    
4882             Z          Separator
4883             Zl         Line separator
4884             Zp         Paragraph separator
4885             Zs         Space separator
4886    
4887    
4888    SCRIPT NAMES FOR \p AND \P
4889    
4890           Arabic,  Armenian,  Balinese,  Bengali,  Bopomofo,  Braille,  Buginese,
4891           Buhid,  Canadian_Aboriginal,  Cherokee,  Common,   Coptic,   Cuneiform,
4892           Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic,
4893           Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew,  Hira-
4894           gana,  Inherited,  Kannada,  Katakana,  Kharoshthi,  Khmer, Lao, Latin,
4895           Limbu,  Linear_B,  Malayalam,  Mongolian,  Myanmar,  New_Tai_Lue,  Nko,
4896           Ogham,  Old_Italic,  Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician,
4897           Runic,  Shavian,  Sinhala,  Syloti_Nagri,  Syriac,  Tagalog,  Tagbanwa,
4898           Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi.
4899    
4900    
4901    CHARACTER CLASSES
4902    
4903             [...]       positive character class
4904             [^...]      negative character class
4905             [x-y]       range (can be used for hex characters)
4906             [[:xxx:]]   positive POSIX named set
4907             [[^:xxx:]]  negative POSIX named set
4908    
4909             alnum       alphanumeric
4910             alpha       alphabetic
4911             ascii       0-127
4912             blank       space or tab
4913             cntrl       control character
4914             digit       decimal digit
4915             graph       printing, excluding space
4916             lower       lower case letter
4917             print       printing, including space
4918             punct       printing, excluding alphanumeric
4919             space       whitespace
4920             upper       upper case letter
4921             word        same as \w
4922             xdigit      hexadecimal digit
4923    
4924           In PCRE, POSIX character set names recognize only ASCII characters. You
4925           can use \Q...\E inside a character class.
4926    
4927    
4928    QUANTIFIERS
4929    
4930             ?           0 or 1, greedy
4931             ?+          0 or 1, possessive
4932             ??          0 or 1, lazy
4933             *           0 or more, greedy
4934             *+          0 or more, possessive
4935             *?          0 or more, lazy
4936             +           1 or more, greedy
4937             ++          1 or more, possessive
4938             +?          1 or more, lazy
4939             {n}         exactly n
4940             {n,m}       at least n, no more than m, greedy
4941             {n,m}+      at least n, no more than m, possessive
4942             {n,m}?      at least n, no more than m, lazy
4943             {n,}        n or more, greedy
4944             {n,}+       n or more, possessive
4945             {n,}?       n or more, lazy
4946    
4947    
4948    ANCHORS AND SIMPLE ASSERTIONS
4949    
4950             \b          word boundary
4951             \B          not a word boundary
4952             ^           start of subject
4953                          also after internal newline in multiline mode
4954             \A          start of subject
4955             $           end of subject
4956                          also before newline at end of subject
4957                          also before internal newline in multiline mode
4958             \Z          end of subject
4959                          also before newline at end of subject
4960             \z          end of subject
4961             \G          first matching position in subject
4962    
4963    
4964    MATCH POINT RESET
4965    
4966             \K          reset start of match
4967    
4968    
4969    ALTERNATION
4970    
4971             expr|expr|expr...
4972    
4973    
4974    CAPTURING
4975    
4976             (...)          capturing group
4977             (?<name>...)   named capturing group (Perl)
4978             (?'name'...)   named capturing group (Perl)
4979             (?P<name>...)  named capturing group (Python)
4980             (?:...)        non-capturing group
4981             (?|...)        non-capturing group; reset group numbers for
4982                             capturing groups in each alternative
4983    
4984    
4985    ATOMIC GROUPS
4986    
4987             (?>...)        atomic, non-capturing group
4988    
4989    
4990    COMMENT
4991    
4992             (?#....)       comment (not nestable)
4993    
4994    
4995    OPTION SETTING
4996    
4997             (?i)           caseless
4998             (?J)           allow duplicate names