/[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 123 by ph10, Mon Mar 12 15:19:06 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
76             pcre-config       show PCRE installation configuration information
77           pcreapi           details of PCRE's native C API           pcreapi           details of PCRE's native C API
78           pcrebuild         options for building PCRE           pcrebuild         options for building PCRE
79           pcrecallout       details of the callout feature           pcrecallout       details of the callout feature
# Line 82  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 89  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 116  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 214  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 initial and sur-         so I've taken it away. If you want to email me, use  my  two  initials,
254         name, separated by a dot, at the domain ucs.cam.ac.uk.         followed by the two digits 10, at the domain cam.ac.uk.
255    
256    
257  REVISION  REVISION
258    
259         Last updated: 06 March 2007         Last updated: 09 August 2007
260         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
261  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
262    
# Line 244  PCRE BUILD-TIME OPTIONS Line 279  PCRE BUILD-TIME OPTIONS
279    
280           ./configure --help           ./configure --help
281    
282         The following sections describe certain options whose names begin  with         The following sections include  descriptions  of  options  whose  names
283         --enable  or  --disable. These settings specify changes to the defaults         begin with --enable or --disable. These settings specify changes to the
284         for the configure command. Because of the  way  that  configure  works,         defaults for the configure command. Because of the way  that  configure
285         --enable  and  --disable  always  come  in  pairs, so the complementary         works,  --enable  and --disable always come in pairs, so the complemen-
286         option always exists as well, but as it specifies the  default,  it  is         tary option always exists as well, but as it specifies the default,  it
287         not described.         is not described.
288    
289    
290  C++ SUPPORT  C++ SUPPORT
# Line 288  UNICODE CHARACTER PROPERTY SUPPORT Line 323  UNICODE CHARACTER PROPERTY SUPPORT
323         to the configure command. This implies UTF-8 support, even if you  have         to the configure command. This implies UTF-8 support, even if you  have
324         not explicitly requested it.         not explicitly requested it.
325    
326         Including  Unicode  property  support  adds around 90K of tables to the         Including  Unicode  property  support  adds around 30K of tables to the
327         PCRE library, approximately doubling its size. Only the  general  cate-         PCRE library. Only the general category properties such as  Lu  and  Nd
328         gory  properties  such as Lu and Nd are supported. Details are given in         are supported. Details are given in the pcrepattern documentation.
        the pcrepattern documentation.  
329    
330    
331  CODE VALUE OF NEWLINE  CODE VALUE OF NEWLINE
332    
333         By default, PCRE interprets character 10 (linefeed, LF)  as  indicating         By  default,  PCRE interprets character 10 (linefeed, LF) as indicating
334         the  end  of  a line. This is the normal newline character on Unix-like         the end of a line. This is the normal newline  character  on  Unix-like
335         systems. You can compile PCRE to use character 13 (carriage return, CR)         systems. You can compile PCRE to use character 13 (carriage return, CR)
336         instead, by adding         instead, by adding
337    
338           --enable-newline-is-cr           --enable-newline-is-cr
339    
340         to  the  configure  command.  There  is  also  a --enable-newline-is-lf         to the  configure  command.  There  is  also  a  --enable-newline-is-lf
341         option, which explicitly specifies linefeed as the newline character.         option, which explicitly specifies linefeed as the newline character.
342    
343         Alternatively, you can specify that line endings are to be indicated by         Alternatively, you can specify that line endings are to be indicated by
# Line 313  CODE VALUE OF NEWLINE Line 347  CODE VALUE OF NEWLINE
347    
348         to the configure command. There is a fourth option, specified by         to the configure command. There is a fourth option, specified by
349    
350             --enable-newline-is-anycrlf
351    
352           which causes PCRE to recognize any of the three sequences  CR,  LF,  or
353           CRLF as indicating a line ending. Finally, a fifth option, specified by
354    
355           --enable-newline-is-any           --enable-newline-is-any
356    
357         which causes PCRE to recognize any Unicode newline sequence.         causes PCRE to recognize any Unicode newline sequence.
358    
359         Whatever  line  ending convention is selected when PCRE is built can be         Whatever line ending convention is selected when PCRE is built  can  be
360         overridden when the library functions are called. At build time  it  is         overridden  when  the library functions are called. At build time it is
361         conventional to use the standard for your operating system.         conventional to use the standard for your operating system.
362    
363    
364  BUILDING SHARED AND STATIC LIBRARIES  BUILDING SHARED AND STATIC LIBRARIES
365    
366         The  PCRE building process uses libtool to build both shared and static         The PCRE building process uses libtool to build both shared and  static
367         Unix libraries by default. You can suppress one of these by adding  one         Unix  libraries by default. You can suppress one of these by adding one
368         of         of
369    
370           --disable-shared           --disable-shared
# Line 337  BUILDING SHARED AND STATIC LIBRARIES Line 376  BUILDING SHARED AND STATIC LIBRARIES
376  POSIX MALLOC USAGE  POSIX MALLOC USAGE
377    
378         When PCRE is called through the POSIX interface (see the pcreposix doc-         When PCRE is called through the POSIX interface (see the pcreposix doc-
379         umentation), additional working storage is  required  for  holding  the         umentation),  additional  working  storage  is required for holding the
380         pointers  to capturing substrings, because PCRE requires three integers         pointers to capturing substrings, because PCRE requires three  integers
381         per substring, whereas the POSIX interface provides only  two.  If  the         per  substring,  whereas  the POSIX interface provides only two. If the
382         number of expected substrings is small, the wrapper function uses space         number of expected substrings is small, the wrapper function uses space
383         on the stack, because this is faster than using malloc() for each call.         on the stack, because this is faster than using malloc() for each call.
384         The default threshold above which the stack is no longer used is 10; it         The default threshold above which the stack is no longer used is 10; it
# Line 352  POSIX MALLOC USAGE Line 391  POSIX MALLOC USAGE
391    
392  HANDLING VERY LARGE PATTERNS  HANDLING VERY LARGE PATTERNS
393    
394         Within a compiled pattern, offset values are used  to  point  from  one         Within  a  compiled  pattern,  offset values are used to point from one
395         part  to another (for example, from an opening parenthesis to an alter-         part to another (for example, from an opening parenthesis to an  alter-
396         nation metacharacter). By default, two-byte values are used  for  these         nation  metacharacter).  By default, two-byte values are used for these
397         offsets,  leading  to  a  maximum size for a compiled pattern of around         offsets, leading to a maximum size for a  compiled  pattern  of  around
398         64K. This is sufficient to handle all but the most  gigantic  patterns.         64K.  This  is sufficient to handle all but the most gigantic patterns.
399         Nevertheless,  some  people do want to process enormous patterns, so it         Nevertheless, some people do want to process enormous patterns,  so  it
400         is possible to compile PCRE to use three-byte or four-byte  offsets  by         is  possible  to compile PCRE to use three-byte or four-byte offsets by
401         adding a setting such as         adding a setting such as
402    
403           --with-link-size=3           --with-link-size=3
404    
405         to  the  configure  command.  The value given must be 2, 3, or 4. Using         to the configure command. The value given must be 2,  3,  or  4.  Using
406         longer offsets slows down the operation of PCRE because it has to  load         longer  offsets slows down the operation of PCRE because it has to load
407         additional bytes when handling them.         additional bytes when handling them.
408    
        If  you  build  PCRE with an increased link size, test 2 (and test 5 if  
        you are using UTF-8) will fail. Part of the output of these tests is  a  
        representation  of the compiled pattern, and this changes with the link  
        size.  
   
409    
410  AVOIDING EXCESSIVE STACK USAGE  AVOIDING EXCESSIVE STACK USAGE
411    
# 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 429  LIMITING PCRE RESOURCE USAGE Line 467  LIMITING PCRE RESOURCE USAGE
467         time.         time.
468    
469    
470    CREATING CHARACTER TABLES AT BUILD TIME
471    
472           PCRE uses fixed tables for processing characters whose code values  are
473           less  than 256. By default, PCRE is built with a set of tables that are
474           distributed in the file pcre_chartables.c.dist. These  tables  are  for
475           ASCII codes only. If you add
476    
477             --enable-rebuild-chartables
478    
479           to  the  configure  command, the distributed tables are no longer used.
480           Instead, a program called dftables is compiled and  run.  This  outputs
481           the source for new set of tables, created in the default locale of your
482           C runtime system. (This method of replacing the tables does not work if
483           you  are cross compiling, because dftables is run on the local host. If
484           you need to create alternative tables when cross  compiling,  you  will
485           have to do so "by hand".)
486    
487    
488  USING EBCDIC CODE  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.         to the configure command. This setting implies --enable-rebuild-charta-
498           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 455  AUTHOR Line 513  AUTHOR
513    
514  REVISION  REVISION
515    
516         Last updated: 06 March 2007         Last updated: 30 July 2007
517         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
518  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
519    
# Line 508  REGULAR EXPRESSIONS AS TREES Line 566  REGULAR EXPRESSIONS AS TREES
566    
567  THE STANDARD MATCHING ALGORITHM  THE STANDARD MATCHING ALGORITHM
568    
569         In the terminology of Jeffrey Friedl's book Mastering  Regular  Expres-         In the terminology of Jeffrey Friedl's book "Mastering Regular  Expres-
570         sions,  the  standard  algorithm  is  an "NFA algorithm". It conducts a         sions",  the  standard  algorithm  is an "NFA algorithm". It conducts a
571         depth-first search of the pattern tree. That is, it  proceeds  along  a         depth-first search of the pattern tree. That is, it  proceeds  along  a
572         single path through the tree, checking that the subject matches what is         single path through the tree, checking that the subject matches what is
573         required. When there is a mismatch, the algorithm  tries  any  alterna-         required. When there is a mismatch, the algorithm  tries  any  alterna-
# Line 591  THE ALTERNATIVE MATCHING ALGORITHM Line 649  THE ALTERNATIVE MATCHING ALGORITHM
649         ence  as  the  condition or test for a specific group recursion are not         ence  as  the  condition or test for a specific group recursion are not
650         supported.         supported.
651    
652         5. Callouts are supported, but the value of the  capture_top  field  is         5. Because many paths through the tree may be  active,  the  \K  escape
653           sequence, which resets the start of the match when encountered (but may
654           be on some paths and not on others), is not  supported.  It  causes  an
655           error if encountered.
656    
657           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         6.  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    
# Line 645  AUTHOR Line 711  AUTHOR
711    
712  REVISION  REVISION
713    
714         Last updated: 06 March 2007         Last updated: 08 August 2007
715         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
716  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
717    
# Line 828  PCRE API OVERVIEW Line 894  PCRE API OVERVIEW
894    
895  NEWLINES  NEWLINES
896    
897         PCRE  supports four different conventions for indicating line breaks in         PCRE  supports five different conventions for indicating line breaks in
898         strings: a single CR (carriage return) character, a  single  LF  (line-         strings: a single CR (carriage return) character, a  single  LF  (line-
899         feed)  character,  the two-character sequence CRLF, or any Unicode new-         feed) character, the two-character sequence CRLF, any of the three pre-
900         line sequence.  The Unicode newline sequences are the three  just  men-         ceding, or any Unicode newline sequence. The Unicode newline  sequences
901         tioned, plus the single characters VT (vertical tab, U+000B), FF (form-         are  the  three just mentioned, plus the single characters VT (vertical
902         feed, U+000C), NEL (next line, U+0085), LS  (line  separator,  U+2028),         tab, U+000B), FF (formfeed, U+000C), NEL (next line, U+0085), LS  (line
903         and PS (paragraph separator, U+2029).         separator, U+2028), and PS (paragraph separator, U+2029).
904    
905         Each  of  the first three conventions is used by at least one operating         Each  of  the first three conventions is used by at least one operating
906         system as its standard newline sequence. When PCRE is built, a  default         system as its standard newline sequence. When PCRE is built, a  default
# Line 842  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 868  SAVING PRECOMPILED PATTERNS FOR LATER US Line 940  SAVING PRECOMPILED PATTERNS FOR LATER US
940         The compiled form of a regular expression can be saved and re-used at a         The compiled form of a regular expression can be saved and re-used at a
941         later time, possibly by a different program, and even on a  host  other         later time, possibly by a different program, and even on a  host  other
942         than  the  one  on  which  it  was  compiled.  Details are given in the         than  the  one  on  which  it  was  compiled.  Details are given in the
943         pcreprecompile documentation.         pcreprecompile documentation. However, compiling a  regular  expression
944           with  one version of PCRE for use with a different version is not guar-
945           anteed to work and may cause crashes.
946    
947    
948  CHECKING BUILD-TIME OPTIONS  CHECKING BUILD-TIME OPTIONS
# Line 899  CHECKING BUILD-TIME OPTIONS Line 973  CHECKING BUILD-TIME OPTIONS
973    
974         The output is an integer whose value specifies  the  default  character         The output is an integer whose value specifies  the  default  character
975         sequence  that is recognized as meaning "newline". The four values that         sequence  that is recognized as meaning "newline". The four values that
976         are supported are: 10 for LF, 13 for CR, 3338 for CRLF, and -1 for ANY.         are supported are: 10 for LF, 13 for CR, 3338 for CRLF, -2 for ANYCRLF,
977         The default should normally be the standard sequence for your operating         and  -1  for  ANY. The default should normally be the standard sequence
978         system.         for your operating system.
979    
980           PCRE_CONFIG_LINK_SIZE           PCRE_CONFIG_LINK_SIZE
981    
# Line 1125  COMPILING A PATTERN Line 1199  COMPILING A PATTERN
1199           PCRE_NEWLINE_CR           PCRE_NEWLINE_CR
1200           PCRE_NEWLINE_LF           PCRE_NEWLINE_LF
1201           PCRE_NEWLINE_CRLF           PCRE_NEWLINE_CRLF
1202             PCRE_NEWLINE_ANYCRLF
1203           PCRE_NEWLINE_ANY           PCRE_NEWLINE_ANY
1204    
1205         These  options  override the default newline definition that was chosen         These  options  override the default newline definition that was chosen
1206         when PCRE was built. Setting the first or the second specifies  that  a         when PCRE was built. Setting the first or the second specifies  that  a
1207         newline  is  indicated  by a single character (CR or LF, respectively).         newline  is  indicated  by a single character (CR or LF, respectively).
1208         Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by  the         Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by  the
1209         two-character  CRLF  sequence.  Setting PCRE_NEWLINE_ANY specifies that         two-character  CRLF  sequence.  Setting  PCRE_NEWLINE_ANYCRLF specifies
1210         any Unicode newline sequence should be recognized. The Unicode  newline         that any of the three preceding sequences should be recognized. Setting
1211         sequences  are  the three just mentioned, plus the single characters VT         PCRE_NEWLINE_ANY  specifies that any Unicode newline sequence should be
1212         (vertical tab, U+000B), FF (formfeed, U+000C), NEL (next line, U+0085),         recognized. The Unicode newline sequences are the three just mentioned,
1213         LS  (line separator, U+2028), and PS (paragraph separator, U+2029). The         plus  the  single  characters  VT (vertical tab, U+000B), FF (formfeed,
1214         last two are recognized only in UTF-8 mode.         U+000C), NEL (next line, U+0085), LS (line separator, U+2028),  and  PS
1215           (paragraph  separator,  U+2029).  The  last  two are recognized only in
1216           UTF-8 mode.
1217    
1218         The newline setting in the  options  word  uses  three  bits  that  are         The newline setting in the  options  word  uses  three  bits  that  are
1219         treated  as  a  number, giving eight possibilities. Currently only five         treated as a number, giving eight possibilities. Currently only six are
1220         are used (default plus the four values above). This means that  if  you         used (default plus the five values above). This means that if  you  set
1221         set  more  than  one  newline option, the combination may or may not be         more  than one newline option, the combination may or may not be sensi-
1222         sensible. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is  equiva-         ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to
1223         lent  to PCRE_NEWLINE_CRLF, but other combinations yield unused numbers         PCRE_NEWLINE_CRLF,  but other combinations may yield unused numbers and
1224         and cause an error.         cause an error.
1225    
1226         The only time that a line break is specially recognized when  compiling         The only time that a line break is specially recognized when  compiling
1227         a  pattern  is  if  PCRE_EXTENDED  is set, and an unescaped # outside a         a  pattern  is  if  PCRE_EXTENDED  is set, and an unescaped # outside a
# Line 1184  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 1230  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 1251  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 1266  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 1303  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.         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
1401         An internal set of tables is created in the default C locale when  PCRE         not try to mix the two.
1402         is  built.  This  is  used when the final argument of pcre_compile() is  
1403         NULL, and is sufficient for many applications. An  alternative  set  of         PCRE  contains  an  internal set of tables that are used when the final
1404         tables  can,  however, be supplied. These may be created in a different         argument of pcre_compile() is  NULL.  These  are  sufficient  for  many
1405         locale from the default. As more and more applications change to  using         applications.  Normally, the internal tables recognize only ASCII char-
1406         Unicode, the need for this locale support is expected to die away.         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,
1408           which may cause them to be different.
1409    
1410           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
1412           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.
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
# Line 1335  LOCALE SUPPORT Line 1423  LOCALE SUPPORT
1423           tables = pcre_maketables();           tables = pcre_maketables();
1424           re = pcre_compile(..., tables);           re = pcre_compile(..., tables);
1425    
1426         When pcre_maketables() runs, the tables are built  in  memory  that  is         The locale name "fr_FR" is used on Linux and other  Unix-like  systems;
1427         obtained  via  pcre_malloc. It is the caller's responsibility to ensure         if you are using Windows, the name for the French locale is "french".
1428         that the memory containing the tables remains available for as long  as  
1429           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
1431           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 1358  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 1374  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 1387  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
1538    
1539           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-
1541           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 1451  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 1487  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
1598    
1599           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
1601           documentation lists the restrictions that apply to patterns  when  par-
1602           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:
# Line 1702  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1816  MATCHING A PATTERN: THE TRADITIONAL FUNC
1816           PCRE_NEWLINE_CR           PCRE_NEWLINE_CR
1817           PCRE_NEWLINE_LF           PCRE_NEWLINE_LF
1818           PCRE_NEWLINE_CRLF           PCRE_NEWLINE_CRLF
1819             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
# Line 1709  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1824  MATCHING A PATTERN: THE TRADITIONAL FUNC
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 or         match failure for an unanchored pattern.
1828         PCRE_NEWLINE_ANY is set, and a match attempt  fails  when  the  current  
1829         position  is  at a CRLF sequence, the match position is advanced by two         When  PCRE_NEWLINE_CRLF,  PCRE_NEWLINE_ANYCRLF,  or PCRE_NEWLINE_ANY is
1830         characters instead of one, in other words, to after the CRLF.         set, and a match attempt for an unanchored pattern fails when the  cur-
1831           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 1759  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 1901  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 1910  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 2013  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 2103  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 2112  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.         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).
2263    
2264    
2265  DUPLICATE SUBPATTERN NAMES  DUPLICATE SUBPATTERN NAMES
# Line 2144  DUPLICATE SUBPATTERN NAMES Line 2271  DUPLICATE SUBPATTERN NAMES
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.
2275    
2276           When    duplicates   are   present,   pcre_copy_named_substring()   and
2277         pcre_get_named_substring() return the first substring corresponding  to         pcre_get_named_substring() return the first substring corresponding  to
2278         the  given  name  that  is  set.  If  none  are set, an empty string is         the  given  name  that  is set. If none are set, PCRE_ERROR_NOSUBSTRING
2279         returned.  The pcre_get_stringnumber() function returns one of the num-         (-7) is returned; no  data  is  returned.  The  pcre_get_stringnumber()
2280         bers  that are associated with the name, but it is not defined which it         function  returns one of the numbers that are associated with the name,
2281         is.         but it is not defined which it is.
2282    
2283         If you want to get full details of all captured substrings for a  given         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         name,  you  must  use  the pcre_get_stringtable_entries() function. The
# Line 2351  AUTHOR Line 2480  AUTHOR
2480    
2481  REVISION  REVISION
2482    
2483         Last updated: 06 March 2007         Last updated: 21 August 2007
2484         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
2485  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2486    
# Line 2379  PCRE CALLOUTS Line 2508  PCRE CALLOUTS
2508         default value is zero.  For  example,  this  pattern  has  two  callout         default value is zero.  For  example,  this  pattern  has  two  callout
2509         points:         points:
2510    
2511           (?C1)eabc(?C2)def           (?C1)abc(?C2)def
2512    
2513         If  the  PCRE_AUTO_CALLOUT  option  bit  is  set when pcre_compile() is         If  the  PCRE_AUTO_CALLOUT  option  bit  is  set when pcre_compile() is
2514         called, PCRE automatically  inserts  callouts,  all  with  number  255,         called, PCRE automatically  inserts  callouts,  all  with  number  255,
# Line 2454  THE CALLOUT INTERFACE Line 2583  THE CALLOUT INTERFACE
2583         The subject and subject_length fields contain copies of the values that         The subject and subject_length fields contain copies of the values that
2584         were passed to pcre_exec().         were passed to pcre_exec().
2585    
2586         The start_match field contains the offset within the subject  at  which         The start_match field normally contains the offset within  the  subject
2587         the  current match attempt started. If the pattern is not anchored, the         at  which  the  current  match  attempt started. However, if the escape
2588         callout function may be called several times from the same point in the         sequence \K has been encountered, this value is changed to reflect  the
2589         pattern for different starting points in the subject.         modified  starting  point.  If the pattern is not anchored, the callout
2590           function may be called several times from the same point in the pattern
2591           for different starting points in the subject.
2592    
2593         The  current_position  field  contains the offset within the subject of         The  current_position  field  contains the offset within the subject of
2594         the current match pointer.         the current match pointer.
# Line 2520  AUTHOR Line 2651  AUTHOR
2651    
2652  REVISION  REVISION
2653    
2654         Last updated: 06 March 2007         Last updated: 29 May 2007
2655         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
2656  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2657    
# Line 2536  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 2602  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 2615  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 2648  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 2662  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 2681  PCRE REGULAR EXPRESSION DETAILS Line 2820  PCRE REGULAR EXPRESSION DETAILS
2820         ported  by  PCRE when its main matching function, pcre_exec(), is used.         ported  by  PCRE when its main matching function, pcre_exec(), is used.
2821         From  release  6.0,   PCRE   offers   a   second   matching   function,         From  release  6.0,   PCRE   offers   a   second   matching   function,
2822         pcre_dfa_exec(),  which matches using a different algorithm that is not         pcre_dfa_exec(),  which matches using a different algorithm that is not
2823         Perl-compatible. The advantages and disadvantages  of  the  alternative         Perl-compatible. Some of the features discussed below are not available
2824         function, and how it differs from the normal function, are discussed in         when  pcre_dfa_exec()  is used. The advantages and disadvantages of the
2825         the pcrematching page.         alternative function, and how it differs from the normal function,  are
2826           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
# Line 2793  BACKSLASH Line 2963  BACKSLASH
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
# Line 2808  BACKSLASH Line 2978  BACKSLASH
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-
2993         dled. For example, \xdc is exactly the same as \x{dc}.         dled. For example, \xdc is exactly the same as \x{dc}.
2994    
2995         After \0 up to two further octal digits are read. If  there  are  fewer         After  \0  up  to two further octal digits are read. If there are fewer
2996         than  two  digits,  just  those  that  are  present  are used. Thus the         than two digits, just  those  that  are  present  are  used.  Thus  the
2997         sequence \0\x\07 specifies two binary zeros followed by a BEL character         sequence \0\x\07 specifies two binary zeros followed by a BEL character
2998         (code  value 7). Make sure you supply two digits after the initial zero         (code value 7). Make sure you supply two digits after the initial  zero
2999         if the pattern character that follows is itself an octal digit.         if the pattern character that follows is itself an octal digit.
3000    
3001         The handling of a backslash followed by a digit other than 0 is compli-         The handling of a backslash followed by a digit other than 0 is compli-
3002         cated.  Outside a character class, PCRE reads it and any following dig-         cated.  Outside a character class, PCRE reads it and any following dig-
3003         its as a decimal number. If the number is less than  10,  or  if  there         its  as  a  decimal  number. If the number is less than 10, or if there
3004         have been at least that many previous capturing left parentheses in the         have been at least that many previous capturing left parentheses in the
3005         expression, the entire  sequence  is  taken  as  a  back  reference.  A         expression,  the  entire  sequence  is  taken  as  a  back reference. A
3006         description  of how this works is given later, following the discussion         description of how this works is given later, following the  discussion
3007         of parenthesized subpatterns.         of parenthesized subpatterns.
3008    
3009         Inside a character class, or if the decimal number is  greater  than  9         Inside  a  character  class, or if the decimal number is greater than 9
3010         and  there have not been that many capturing subpatterns, PCRE re-reads         and there have not been that many capturing subpatterns, PCRE  re-reads
3011         up to three octal digits following the backslash, and uses them to gen-         up to three octal digits following the backslash, and uses them to gen-
3012         erate  a data character. Any subsequent digits stand for themselves. In         erate a data character. Any subsequent digits stand for themselves.  In
3013         non-UTF-8 mode, the value of a character specified  in  octal  must  be         non-UTF-8  mode,  the  value  of a character specified in octal must be
3014         less  than  \400.  In  UTF-8 mode, values up to \777 are permitted. For         less than \400. In UTF-8 mode, values up to  \777  are  permitted.  For
3015         example:         example:
3016    
3017           \040   is another way of writing a space           \040   is another way of writing a space
# Line 2856  BACKSLASH Line 3029  BACKSLASH
3029           \81    is either a back reference, or a binary zero           \81    is either a back reference, or a binary zero
3030                     followed by the two characters "8" and "1"                     followed by the two characters "8" and "1"
3031    
3032         Note that octal values of 100 or greater must not be  introduced  by  a         Note  that  octal  values of 100 or greater must not be introduced by a
3033         leading zero, because no more than three octal digits are ever read.         leading zero, because no more than three octal digits are ever read.
3034    
3035         All the sequences that define a single character value can be used both         All the sequences that define a single character value can be used both
3036         inside and outside character classes. In addition, inside  a  character         inside  and  outside character classes. In addition, inside a character
3037         class,  the  sequence \b is interpreted as the backspace character (hex         class, the sequence \b is interpreted as the backspace  character  (hex
3038         08), and the sequences \R and \X are interpreted as the characters  "R"         08),  and the sequences \R and \X are interpreted as the characters "R"
3039         and  "X", respectively. Outside a character class, these sequences have         and "X", respectively. Outside a character class, these sequences  have
3040         different meanings (see below).         different meanings (see below).
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. Back         ally enclosed in braces, is an absolute or relative back  reference.  A
3046         references are discussed later, following the discussion  of  parenthe-         named back reference can be coded as \g{name}. Back references are dis-
3047         sized subpatterns.         cussed later, following the discussion of parenthesized subpatterns.
3048    
3049     Generic character types     Generic character types
3050    
# Line 2880  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    
3065         Each pair of escape sequences partitions the complete set of characters         Each pair of escape sequences partitions the complete set of characters
3066         into  two disjoint sets. Any given character matches one, and only one,         into two disjoint sets. Any given character matches one, and only  one,
3067         of each pair.         of each pair.
3068    
3069         These character type sequences can appear both inside and outside char-         These character type sequences can appear both inside and outside char-
3070         acter  classes.  They each match one character of the appropriate type.         acter classes. They each match one character of the  appropriate  type.
3071         If the current matching point is at the end of the subject string,  all         If  the current matching point is at the end of the subject string, all
3072         of them fail, since there is no character to match.         of them fail, since there is no character to match.
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         A "word" character is an underscore or any character less than 256 that         In UTF-8 mode, characters with values greater than 128 never match  \d,
        is a letter or digit. The definition of  letters  and  digits  is  con-  
        trolled  by PCRE's low-valued character tables, and may vary if locale-  
        specific matching is taking place (see "Locale support" in the  pcreapi  
        page).  For  example,  in  the  "fr_FR" (French) locale, some character  
        codes greater than 128 are used for accented  letters,  and  these  are  
        matched by \w.  
   
        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-         \s, or \w, and always match \D, \S, and \W. This is true even when Uni-
3082         code  character  property support is available. The use of locales with         code character property support is available.  These  sequences  retain
3083         Unicode is discouraged.         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
3121           is  a  letter  or  digit.  The definition of letters and digits is con-
3122           trolled by PCRE's low-valued character tables, and may vary if  locale-
3123           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
3125           systems,  or "french" in Windows, some character codes greater than 128
3126           are used for accented letters, and these are matched by \w. The use  of
3127           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)
3136    
3137         This is an example of an "atomic group", details  of  which  are  given         This  is  an  example  of an "atomic group", details of which are given
3138         below.  This particular group matches either the two-character sequence         below.  This particular group matches either the two-character sequence
3139         CR followed by LF, or  one  of  the  single  characters  LF  (linefeed,         CR  followed  by  LF,  or  one  of  the single characters LF (linefeed,
3140         U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage         U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage
3141         return, U+000D), or NEL (next line, U+0085). The two-character sequence         return, U+000D), or NEL (next line, U+0085). The two-character sequence
3142         is treated as a single unit that cannot be split.         is treated as a single unit that cannot be split.
3143    
3144         In  UTF-8  mode, two additional characters whose codepoints are greater         In UTF-8 mode, two additional characters whose codepoints  are  greater
3145         than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa-         than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa-
3146         rator,  U+2029).   Unicode character property support is not needed for         rator, U+2029).  Unicode character property support is not  needed  for
3147         these characters to be recognized.         these characters to be recognized.
3148    
3149         Inside a character class, \R matches the letter "R".         Inside a character class, \R matches the letter "R".
# Line 2938  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
3161           \X       an extended Unicode sequence           \X       an extended Unicode sequence
3162    
3163         The property names represented by xx above are limited to  the  Unicode         The  property  names represented by xx above are limited to the Unicode
3164         script names, the general category properties, and "Any", which matches         script names, the general category properties, and "Any", which matches
3165         any character (including newline). Other properties such as "InMusical-         any character (including newline). Other properties such as "InMusical-
3166         Symbols"  are  not  currently supported by PCRE. Note that \P{Any} does         Symbols" are not currently supported by PCRE. Note  that  \P{Any}  does
3167         not match any characters, so always causes a match failure.         not match any characters, so always causes a match failure.
3168    
3169         Sets of Unicode characters are defined as belonging to certain scripts.         Sets of Unicode characters are defined as belonging to certain scripts.
3170         A  character from one of these sets can be matched using a script name.         A character from one of these sets can be matched using a script  name.
3171         For example:         For example:
3172    
3173           \p{Greek}           \p{Greek}
3174           \P{Han}           \P{Han}
3175    
3176         Those that are not part of an identified script are lumped together  as         Those  that are not part of an identified script are lumped together as
3177         "Common". The current list of scripts is:         "Common". The current list of scripts is:
3178    
3179         Arabic,  Armenian,  Balinese,  Bengali,  Bopomofo,  Braille,  Buginese,         Arabic,  Armenian,  Balinese,  Bengali,  Bopomofo,  Braille,  Buginese,
3180         Buhid,  Canadian_Aboriginal,  Cherokee,  Common,   Coptic,   Cuneiform,         Buhid,   Canadian_Aboriginal,   Cherokee,  Common,  Coptic,  Cuneiform,
3181         Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic,         Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic,
3182         Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew,  Hira-         Gothic,  Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hira-
3183         gana,  Inherited,  Kannada,  Katakana,  Kharoshthi,  Khmer, Lao, Latin,         gana, Inherited, Kannada,  Katakana,  Kharoshthi,  Khmer,  Lao,  Latin,
3184         Limbu,  Linear_B,  Malayalam,  Mongolian,  Myanmar,  New_Tai_Lue,  Nko,         Limbu,  Linear_B,  Malayalam,  Mongolian,  Myanmar,  New_Tai_Lue,  Nko,
3185         Ogham,  Old_Italic,  Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician,         Ogham, Old_Italic, Old_Persian, Oriya, Osmanya,  Phags_Pa,  Phoenician,
3186         Runic,  Shavian,  Sinhala,  Syloti_Nagri,  Syriac,  Tagalog,  Tagbanwa,         Runic,  Shavian,  Sinhala,  Syloti_Nagri,  Syriac,  Tagalog,  Tagbanwa,
3187         Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi.         Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi.
3188    
3189         Each  character has exactly one general category property, specified by         Each character has exactly one general category property, specified  by
3190         a two-letter abbreviation. For compatibility with Perl, negation can be         a two-letter abbreviation. For compatibility with Perl, negation can be
3191         specified  by  including a circumflex between the opening brace and the         specified by including a circumflex between the opening brace  and  the
3192         property name. For example, \p{^Lu} is the same as \P{Lu}.         property name. For example, \p{^Lu} is the same as \P{Lu}.
3193    
3194         If only one letter is specified with \p or \P, it includes all the gen-         If only one letter is specified with \p or \P, it includes all the gen-
3195         eral  category properties that start with that letter. In this case, in         eral category properties that start with that letter. In this case,  in
3196         the absence of negation, the curly brackets in the escape sequence  are         the  absence of negation, the curly brackets in the escape sequence are
3197         optional; these two examples have the same effect:         optional; these two examples have the same effect:
3198    
3199           \p{L}           \p{L}
# Line 3030  BACKSLASH Line 3245  BACKSLASH
3245           Zp    Paragraph separator           Zp    Paragraph separator
3246           Zs    Space separator           Zs    Space separator
3247    
3248         The  special property L& is also supported: it matches a character that         The special property L& is also supported: it matches a character  that
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  long  synonyms  for  these  properties that Perl supports (such as         The Cs (Surrogate) property applies only to  characters  in  the  range
3253         \p{Letter}) are not supported by PCRE, nor is it  permitted  to  prefix         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
3259           \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".
3261    
3262         No character that is in the Unicode table has the Cn (unassigned) prop-         No character that is in the Unicode table has the Cn (unassigned) prop-
3263         erty.  Instead, this property is assumed for any code point that is not         erty.  Instead, this property is assumed for any code point that is not
3264         in the Unicode table.         in the Unicode table.
3265    
3266         Specifying  caseless  matching  does not affect these escape sequences.         Specifying caseless matching does not affect  these  escape  sequences.
3267         For example, \p{Lu} always matches only upper case letters.         For example, \p{Lu} always matches only upper case letters.
3268    
3269         The \X escape matches any number of Unicode  characters  that  form  an         The  \X  escape  matches  any number of Unicode characters that form an
3270         extended Unicode sequence. \X is equivalent to         extended Unicode sequence. \X is equivalent to
3271    
3272           (?>\PM\pM*)           (?>\PM\pM*)
3273    
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
3283         characters. That is why the traditional escape sequences such as \d and         characters. That is why the traditional escape sequences such as \d and
3284         \w do not use Unicode properties in PCRE.         \w do not use Unicode properties in PCRE.
3285    
3286       Resetting the match start
3287    
3288           The escape sequence \K, which is a Perl 5.10 feature, causes any previ-
3289           ously  matched  characters  not  to  be  included  in the final matched
3290           sequence. For example, the pattern:
3291    
3292             foo\Kbar
3293    
3294           matches "foobar", but reports that it has matched "bar".  This  feature
3295           is  similar  to  a lookbehind assertion (described below).  However, in
3296           this case, the part of the subject before the real match does not  have
3297           to  be of fixed length, as lookbehind assertions do. The use of \K does
3298           not interfere with the setting of captured  substrings.   For  example,
3299           when the pattern
3300    
3301             (foo)\Kbar
3302    
3303           matches "foobar", the first substring is still set to "foo".
3304    
3305     Simple assertions     Simple assertions
3306    
3307         The  final use of backslash is for certain simple assertions. An asser-         The  final use of backslash is for certain simple assertions. An asser-
# Line 3275  SQUARE BRACKETS AND CHARACTER CLASSES Line 3517  SQUARE BRACKETS AND CHARACTER CLASSES
3517         If a range that includes letters is used when caseless matching is set,         If a range that includes letters is used when caseless matching is set,
3518         it matches the letters in either case. For example, [W-c] is equivalent         it matches the letters in either case. For example, [W-c] is equivalent
3519         to  [][\\^_`wxyzabc],  matched  caselessly,  and  in non-UTF-8 mode, if         to  [][\\^_`wxyzabc],  matched  caselessly,  and  in non-UTF-8 mode, if
3520         character tables for the "fr_FR" locale are in use, [\xc8-\xcb] matches         character tables for a French locale are in  use,  [\xc8-\xcb]  matches
3521         accented  E  characters in both cases. In UTF-8 mode, PCRE supports the         accented  E  characters in both cases. In UTF-8 mode, PCRE supports the
3522         concept of case for characters with values greater than 128  only  when         concept of case for characters with values greater than 128  only  when
3523         it is compiled with Unicode property support.         it is compiled with Unicode property support.
# Line 3460  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 3499  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 3711  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         Back references to named subpatterns use the Perl  syntax  \k<name>  or         There  are  several  different ways of writing back references to named
4105         \k'name'  or  the  Python  syntax (?P=name). We could rewrite the above         subpatterns. The .NET syntax \k{name} and the Perl syntax  \k<name>  or
4106         example in either of the following ways:         \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
4108           and  named  references,  is  also supported. We could rewrite the above
4109           example in any of the following ways:
4110    
4111           (?<p1>(?i)rah)\s+\k<p1>           (?<p1>(?i)rah)\s+\k<p1>
4112             (?'p1'(?i)rah)\s+\k{p1}
4113           (?P<p1>(?i)rah)\s+(?P=p1)           (?P<p1>(?i)rah)\s+(?P=p1)
4114             (?<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.
# Line 3941  ASSERTIONS Line 4226  ASSERTIONS
4226    
4227           (?<=abc|abde)           (?<=abc|abde)
4228    
4229           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-
4231           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-
# Line 4034  CONDITIONAL SUBPATTERNS Line 4323  CONDITIONAL SUBPATTERNS
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.         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-
4328           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
4330           looping constructs it can also make sense to refer to subsequent groups
4331           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
4351           relative reference:
4352    
4353             ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...
4354    
4355           This makes the fragment independent of the parentheses  in  the  larger
4356           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 4073  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 4161  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. In a larger pattern,  keep-         refer to them instead of the whole pattern.
4500         ing  track  of parenthesis numbers can be tricky. It may be more conve-  
4501         nient to use named parentheses instead. The Perl  syntax  for  this  is         In  a  larger  pattern,  keeping  track  of  parenthesis numbers can be
4502         (?&name);  PCRE's  earlier syntax (?P>name) is also supported. We could         tricky. This is made easier by the use of relative references. (A  Perl
4503         rewrite the above example as follows:         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
4505           the  recursion.  In  other  words,  a  negative number counts capturing
4506           parentheses leftwards from the point at which it is encountered.
4507    
4508           It is also possible to refer to  subsequently  opened  parentheses,  by
4509           writing  references  such  as (?+2). However, these cannot be recursive
4510           because the reference is not inside the  parentheses  that  are  refer-
4511           enced.  They  are  always  "subroutine" calls, as described in the next
4512           section.
4513    
4514           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
4516           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. This particular example pattern contains nested unlimited         one is used.
4522         repeats, and so the use of atomic grouping for matching strings of non-  
4523         parentheses  is  important when applying the pattern to strings that do         This  particular  example pattern that we have been looking at contains
4524         not match. For example, when this pattern is applied to         nested unlimited repeats, and so the use of atomic grouping for  match-
4525           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
4527           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. An earlier example         tern may be defined before or after the reference. A numbered reference
4578         pointed out that the pattern         can be absolute or relative, as in these examples:
4579    
4580             (...(absolute)...)...(?2)...
4581             (...(relative)...)...(?-1)...
4582             (...(?+1)...(relative)...
4583    
4584           An earlier example pointed out that the pattern
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 4334  AUTHOR Line 4775  AUTHOR
4775    
4776  REVISION  REVISION
4777    
4778         Last updated: 06 March 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
4999             (?m)           multiline
5000             (?s)           single line (dotall)
5001             (?U)           default ungreedy (lazy)
5002             (?x)           extended (ignore white space)
5003             (?-...)        unset option(s)
5004    
5005    
5006    LOOKAHEAD AND LOOKBEHIND ASSERTIONS
5007    
5008             (?=...)        positive look ahead
5009             (?!...)        negative look ahead
5010             (?<=...)       positive look behind
5011             (?<!...)       negative look behind
5012    
5013           Each top-level branch of a look behind must be of a fixed length.
5014    
5015    
5016    BACKREFERENCES
5017    
5018             \n             reference by number (can be ambiguous)
5019             \gn            reference by number
5020             \g{n}          reference by number
5021             \g{-n}         relative reference by number
5022             \k<name>       reference by name (Perl)
5023             \k'name'       reference by name (Perl)
5024             \g{name}       reference by name (Perl)
5025             \k{name}       reference by name (.NET)
5026             (?P=name)      reference by name (Python)
5027    
5028    
5029    SUBROUTINE REFERENCES (POSSIBLY RECURSIVE)
5030    
5031             (?R)           recurse whole pattern
5032             (?n)           call subpattern by absolute number
5033             (?+n)          call subpattern by relative number
5034             (?-n)          call subpattern by relative number
5035             (?&name)       call subpattern by name (Perl)
5036             (?P>name)      call subpattern by name (Python)
5037    
5038    
5039    CONDITIONAL PATTERNS
5040    
5041             (?(condition)yes-pattern)
5042             (?(condition)yes-pattern|no-pattern)
5043    
5044             (?(n)...       absolute reference condition
5045             (?(+n)...      relative reference condition
5046             (?(-n)...      relative reference condition
5047             (?(<name>)...  named reference condition (Perl)
5048             (?('name')...  named reference condition (Perl)
5049             (?(name)...    named reference condition (PCRE)
5050             (?(R)...       overall recursion condition
5051             (?(Rn)...      specific group recursion condition
5052             (?(R&name)...  specific recursion condition
5053             (?(DEFINE)...  define subpattern for reference
5054             (?(assert)...  assertion condition
5055    
5056    
5057    BACKTRACKING CONTROL
5058    
5059           The following act immediately they are reached:
5060    
5061             (*ACCEPT)      force successful match
5062             (*FAIL)        force backtrack; synonym (*F)
5063    
5064           The following act only when a subsequent match failure causes  a  back-
5065           track to reach them. They all force a match failure, but they differ in
5066           what happens afterwards. Those that advance the start-of-match point do
5067           so only if the pattern is not anchored.
5068    
5069             (*COMMIT)      overall failure, no advance of starting point
5070             (*PRUNE)       advance to next starting character
5071             (*SKIP)        advance start to current matching position
5072             (*THEN)        local failure, backtrack to next alternation
5073    
5074    
5075    NEWLINE CONVENTIONS
5076    
5077           These are recognized only at the very start of a pattern.
5078    
5079             (*CR)
5080             (*LF)
5081             (*CRLF)
5082             (*ANYCRLF)
5083             (*ANY)
5084    
5085    
5086    CALLOUTS
5087    
5088             (?C)      callout
5089             (?Cn)     callout with data n
5090    
5091    
5092    SEE ALSO
5093    
5094           pcrepattern(3), pcreapi(3), pcrecallout(3), pcrematching(3), pcre(3).
5095    
5096    
5097    AUTHOR
5098    
5099           Philip Hazel
5100           University Computing Service
5101           Cambridge CB2 3QH, England.
5102    
5103    
5104    REVISION
5105    
5106           Last updated: 21 August 2007
5107         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
5108  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
5109    
# Line 4415  RESTRICTED PATTERNS FOR PCRE_PARTIAL Line 5184  RESTRICTED PATTERNS FOR PCRE_PARTIAL
5184    
5185         If PCRE_PARTIAL is set for a pattern  that  does  not  conform  to  the         If PCRE_PARTIAL is set for a pattern  that  does  not  conform  to  the
5186         restrictions,  pcre_exec() returns the error code PCRE_ERROR_BADPARTIAL         restrictions,  pcre_exec() returns the error code PCRE_ERROR_BADPARTIAL
5187         (-13).         (-13).  You can use the PCRE_INFO_OKPARTIAL call to pcre_fullinfo()  to
5188           find out if a compiled pattern can be used for partial matching.
5189    
5190    
5191  EXAMPLE OF PARTIAL MATCHING USING PCRETEST  EXAMPLE OF PARTIAL MATCHING USING PCRETEST
5192    
5193         If the escape sequence \P is present  in  a  pcretest  data  line,  the         If  the  escape  sequence  \P  is  present in a pcretest data line, the
5194         PCRE_PARTIAL flag is used for the match. Here is a run of pcretest that         PCRE_PARTIAL flag is used for the match. Here is a run of pcretest that
5195         uses the date example quoted above:         uses the date example quoted above:
5196    
# Line 4437  EXAMPLE OF PARTIAL MATCHING USING PCRETE Line 5207  EXAMPLE OF PARTIAL MATCHING USING PCRETE
5207           data> j\P           data> j\P
5208           No match           No match
5209    
5210         The first data string is matched  completely,  so  pcretest  shows  the         The  first  data  string  is  matched completely, so pcretest shows the
5211         matched  substrings.  The  remaining four strings do not match the com-         matched substrings. The remaining four strings do not  match  the  com-
5212         plete pattern, but the first two are partial matches.  The  same  test,         plete  pattern,  but  the first two are partial matches. The same test,
5213         using  pcre_dfa_exec()  matching  (by means of the \D escape sequence),         using pcre_dfa_exec() matching (by means of the  \D  escape  sequence),
5214         produces the following output:         produces the following output:
5215    
5216             re> /^?(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)$/             re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/
5217           data> 25jun04\P\D           data> 25jun04\P\D
5218            0: 25jun04            0: 25jun04
5219           data> 23dec3\P\D           data> 23dec3\P\D
# Line 4455  EXAMPLE OF PARTIAL MATCHING USING PCRETE Line 5225  EXAMPLE OF PARTIAL MATCHING USING PCRETE
5225           data> j\P\D           data> j\P\D
5226           No match           No match
5227    
5228         Notice that in this case the portion of the string that was matched  is         Notice  that in this case the portion of the string that was matched is
5229         made available.         made available.
5230    
5231    
5232  MULTI-SEGMENT MATCHING WITH pcre_dfa_exec()  MULTI-SEGMENT MATCHING WITH pcre_dfa_exec()
5233    
5234         When a partial match has been found using pcre_dfa_exec(), it is possi-         When a partial match has been found using pcre_dfa_exec(), it is possi-
5235         ble to continue the match by  providing  additional  subject  data  and         ble  to  continue  the  match  by providing additional subject data and
5236         calling  pcre_dfa_exec()  again  with the same compiled regular expres-         calling pcre_dfa_exec() again with the same  compiled  regular  expres-
5237         sion, this time setting the PCRE_DFA_RESTART option. You must also pass         sion, this time setting the PCRE_DFA_RESTART option. You must also pass
5238         the  same working space as before, because this is where details of the         the same working space as before, because this is where details of  the
5239         previous partial match are stored. Here is an example  using  pcretest,         previous  partial  match are stored. Here is an example using pcretest,
5240         using the \R escape sequence to set the PCRE_DFA_RESTART option (\P and         using the \R escape sequence to set the PCRE_DFA_RESTART option (\P and
5241         \D are as above):         \D are as above):
5242    
5243             re> /^?(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)$/             re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/
5244           data> 23ja\P\D           data> 23ja\P\D
5245           Partial match: 23ja           Partial match: 23ja
5246           data> n05\R\D           data> n05\R\D
5247            0: n05            0: n05
5248    
5249         The first call has "23ja" as the subject, and requests  partial  match-         The  first  call has "23ja" as the subject, and requests partial match-
5250         ing;  the  second  call  has  "n05"  as  the  subject for the continued         ing; the second call  has  "n05"  as  the  subject  for  the  continued
5251         (restarted) match.  Notice that when the match is  complete,  only  the         (restarted)  match.   Notice  that when the match is complete, only the
5252         last  part  is  shown;  PCRE  does not retain the previously partially-         last part is shown; PCRE does  not  retain  the  previously  partially-
5253         matched string. It is up to the calling program to do that if it  needs         matched  string. It is up to the calling program to do that if it needs
5254         to.         to.
5255    
5256         You  can  set  PCRE_PARTIAL  with  PCRE_DFA_RESTART to continue partial         You can set PCRE_PARTIAL  with  PCRE_DFA_RESTART  to  continue  partial
5257         matching over multiple segments. This facility can be used to pass very         matching over multiple segments. This facility can be used to pass very
5258         long  subject  strings to pcre_dfa_exec(). However, some care is needed         long subject strings to pcre_dfa_exec(). However, some care  is  needed
5259         for certain types of pattern.         for certain types of pattern.
5260    
5261         1. If the pattern contains tests for the beginning or end  of  a  line,         1.  If  the  pattern contains tests for the beginning or end of a line,
5262         you  need  to pass the PCRE_NOTBOL or PCRE_NOTEOL options, as appropri-         you need to pass the PCRE_NOTBOL or PCRE_NOTEOL options,  as  appropri-
5263         ate, when the subject string for any call does not contain  the  begin-         ate,  when  the subject string for any call does not contain the begin-
5264         ning or end of a line.         ning or end of a line.
5265    
5266         2.  If  the  pattern contains backward assertions (including \b or \B),         2. If the pattern contains backward assertions (including  \b  or  \B),
5267         you need to arrange for some overlap in the subject  strings  to  allow         you  need  to  arrange for some overlap in the subject strings to allow
5268         for  this.  For  example, you could pass the subject in chunks that are         for this. For example, you could pass the subject in  chunks  that  are
5269         500 bytes long, but in a buffer of 700 bytes, with the starting  offset         500  bytes long, but in a buffer of 700 bytes, with the starting offset
5270         set to 200 and the previous 200 bytes at the start of the buffer.         set to 200 and the previous 200 bytes at the start of the buffer.
5271    
5272         3.  Matching a subject string that is split into multiple segments does         3. Matching a subject string that is split into multiple segments  does
5273         not always produce exactly the same result as matching over one  single         not  always produce exactly the same result as matching over one single
5274         long  string.   The  difference arises when there are multiple matching         long string.  The difference arises when there  are  multiple  matching
5275         possibilities, because a partial match result is given only when  there         possibilities,  because a partial match result is given only when there
5276         are  no  completed  matches  in a call to fBpcre_dfa_exec(). This means         are no completed matches in a call to pcre_dfa_exec(). This means  that
5277         that as soon as the shortest match has been found,  continuation  to  a         as  soon  as  the  shortest match has been found, continuation to a new
5278         new  subject  segment  is  no  longer possible.  Consider this pcretest         subject segment is no longer possible.  Consider this pcretest example:
        example:  
5279    
5280             re> /dog(sbody)?/             re> /dog(sbody)?/
5281           data> do\P\D           data> do\P\D
# Line 4517  MULTI-SEGMENT MATCHING WITH pcre_dfa_exe Line 5286  MULTI-SEGMENT MATCHING WITH pcre_dfa_exe
5286            0: dogsbody            0: dogsbody
5287            1: dog            1: dog
5288    
5289         The pattern matches the words "dog" or "dogsbody". When the subject  is         The  pattern matches the words "dog" or "dogsbody". When the subject is
5290         presented  in  several  parts  ("do" and "gsb" being the first two) the         presented in several parts ("do" and "gsb" being  the  first  two)  the
5291         match stops when "dog" has been found, and it is not possible  to  con-         match  stops  when "dog" has been found, and it is not possible to con-
5292         tinue.  On  the  other  hand,  if  "dogsbody"  is presented as a single         tinue. On the other hand,  if  "dogsbody"  is  presented  as  a  single
5293         string, both matches are found.         string, both matches are found.
5294    
5295         Because of this phenomenon, it does not usually make  sense  to  end  a         Because  of  this  phenomenon,  it does not usually make sense to end a
5296         pattern that is going to be matched in this way with a variable repeat.         pattern that is going to be matched in this way with a variable repeat.
5297    
5298         4. Patterns that contain alternatives at the top level which do not all         4. Patterns that contain alternatives at the top level which do not all
# Line 4532  MULTI-SEGMENT MATCHING WITH pcre_dfa_exe Line 5301  MULTI-SEGMENT MATCHING WITH pcre_dfa_exe
5301    
5302           1234|3789           1234|3789
5303    
5304         If the first part of the subject is "ABC123", a partial  match  of  the         If  the  first  part of the subject is "ABC123", a partial match of the
5305         first  alternative  is found at offset 3. There is no partial match for         first alternative is found at offset 3. There is no partial  match  for
5306         the second alternative, because such a match does not start at the same         the second alternative, because such a match does not start at the same
5307         point  in  the  subject  string. Attempting to continue with the string         point in the subject string. Attempting to  continue  with  the  string
5308         "789" does not yield a match because only those alternatives that match         "789" does not yield a match because only those alternatives that match
5309         at  one point in the subject are remembered. The problem arises because         at one point in the subject are remembered. The problem arises  because
5310         the start of the second alternative matches within the  first  alterna-         the  start  of the second alternative matches within the first alterna-
5311         tive. There is no problem with anchored patterns or patterns such as:         tive. There is no problem with anchored patterns or patterns such as:
5312    
5313           1234|ABCD           1234|ABCD
# Line 4555  AUTHOR Line 5324  AUTHOR
5324    
5325  REVISION  REVISION
5326    
5327         Last updated: 06 March 2007         Last updated: 04 June 2007
5328         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
5329  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
5330    
# Line 4580  SAVING AND RE-USING PRECOMPILED PCRE PAT Line 5349  SAVING AND RE-USING PRECOMPILED PCRE PAT
5349         ent  host  and  run them there. This works even if the new host has the         ent  host  and  run them there. This works even if the new host has the
5350         opposite endianness to the one on which  the  patterns  were  compiled.         opposite endianness to the one on which  the  patterns  were  compiled.
5351         There  may  be a small performance penalty, but it should be insignifi-         There  may  be a small performance penalty, but it should be insignifi-
5352         cant.         cant. However, compiling regular expressions with one version  of  PCRE
5353           for  use  with  a  different  version is not guaranteed to work and may
5354           cause crashes.
5355    
5356    
5357  SAVING A COMPILED PATTERN  SAVING A COMPILED PATTERN
# Line 4663  RE-USING A PRECOMPILED PATTERN Line 5434  RE-USING A PRECOMPILED PATTERN
5434    
5435  COMPATIBILITY WITH DIFFERENT PCRE RELEASES  COMPATIBILITY WITH DIFFERENT PCRE RELEASES
5436    
5437         The layout of the control block that is at the start of the  data  that         In general, it is safest to  recompile  all  saved  patterns  when  you
5438         makes  up  a  compiled pattern was changed for release 5.0. If you have         update  to  a new PCRE release, though not all updates actually require
5439         any saved patterns that were compiled with  previous  releases  (not  a         this. Recompiling is definitely needed for release 7.2.
        facility  that  was  previously advertised), you will have to recompile  
        them for release 5.0 and above.  
   
        If you have any saved patterns in UTF-8 mode that use  \p  or  \P  that  
        were  compiled  with any release up to and including 6.4, you will have  
        to recompile them for release 6.5 and above.  
   
        All saved patterns from earlier releases must be recompiled for release  
        7.0  or  higher,  because  there was an internal reorganization at that  
        release.  
5440    
5441    
5442  AUTHOR  AUTHOR
# Line 4687  AUTHOR Line 5448  AUTHOR
5448    
5449  REVISION  REVISION
5450    
5451         Last updated: 06 March 2007         Last updated: 13 June 2007
5452         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
5453  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
5454    
# Line 5155  MATCHING INTERFACE Line 5916  MATCHING INTERFACE
5916         return false (because the empty string is not a valid number):         return false (because the empty string is not a valid number):
5917    
5918            int number;            int number;
5919            pcrecpp::RE::FullMatch("abc", "[a-z]+(\d+)?", &number);            pcrecpp::RE::FullMatch("abc", "[a-z]+(\\d+)?", &number);
5920    
5921         The matching interface supports at most 16 arguments per call.  If  you         The matching interface supports at most 16 arguments per call.  If  you
5922         need    more,    consider    using    the    more   general   interface         need    more,    consider    using    the    more   general   interface
# Line 5422  PCRE SAMPLE PROGRAM Line 6183  PCRE SAMPLE PROGRAM
6183         bility  of  matching an empty string. Comments in the code explain what         bility  of  matching an empty string. Comments in the code explain what
6184         is going on.         is going on.
6185    
6186         If PCRE is installed in the standard include  and  library  directories         The demonstration program is automatically built if you use  "./config-
6187         for  your  system, you should be able to compile the demonstration pro-         ure;make"  to  build PCRE. Otherwise, if PCRE is installed in the stan-
6188         gram using this command:         dard include and library directories for your  system,  you  should  be
6189           able to compile the demonstration program using this command:
6190    
6191           gcc -o pcredemo pcredemo.c -lpcre           gcc -o pcredemo pcredemo.c -lpcre
6192    
6193         If PCRE is installed elsewhere, you may need to add additional  options         If  PCRE is installed elsewhere, you may need to add additional options
6194         to  the  command line. For example, on a Unix-like system that has PCRE         to the command line. For example, on a Unix-like system that  has  PCRE
6195         installed in /usr/local, you  can  compile  the  demonstration  program         installed  in  /usr/local,  you  can  compile the demonstration program
6196         using a command like this:         using a command like this:
6197    
6198           gcc -o pcredemo -I/usr/local/include pcredemo.c \           gcc -o pcredemo -I/usr/local/include pcredemo.c \
6199               -L/usr/local/lib -lpcre               -L/usr/local/lib -lpcre
6200    
6201         Once  you  have  compiled the demonstration program, you can run simple         Once you have compiled the demonstration program, you  can  run  simple
6202         tests like this:         tests like this:
6203    
6204           ./pcredemo 'cat|dog' 'the cat sat on the mat'           ./pcredemo 'cat|dog' 'the cat sat on the mat'
6205           ./pcredemo -g 'cat|dog' 'the dog sat on the cat'           ./pcredemo -g 'cat|dog' 'the dog sat on the cat'
6206    
6207         Note that there is a  much  more  comprehensive  test  program,  called         Note  that  there  is  a  much  more comprehensive test program, called
6208         pcretest,  which  supports  many  more  facilities  for testing regular         pcretest, which supports  many  more  facilities  for  testing  regular
6209         expressions and the PCRE library. The pcredemo program is provided as a         expressions and the PCRE library. The pcredemo program is provided as a
6210         simple coding example.         simple coding example.
6211    
# Line 5451  PCRE SAMPLE PROGRAM Line 6213  PCRE SAMPLE PROGRAM
6213         the standard library directory, you may get an error like this when you         the standard library directory, you may get an error like this when you
6214         try to run pcredemo:         try to run pcredemo:
6215    
6216           ld.so.1:  a.out:  fatal:  libpcre.so.0:  open failed: No such file or           ld.so.1: a.out: fatal: libpcre.so.0: open failed:  No  such  file  or
6217         directory         directory
6218    
6219         This is caused by the way shared library support works  on  those  sys-         This  is  caused  by the way shared library support works on those sys-
6220         tems. You need to add         tems. You need to add
6221    
6222           -R/usr/local/lib           -R/usr/local/lib
# Line 5471  AUTHOR Line 6233  AUTHOR
6233    
6234  REVISION  REVISION
6235    
6236         Last updated: 06 March 2007         Last updated: 13 June 2007
6237         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
6238  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
6239  PCRESTACK(3)                                                      PCRESTACK(3)  PCRESTACK(3)                                                      PCRESTACK(3)
# Line 5541  PCRE DISCUSSION OF STACK USAGE Line 6303  PCRE DISCUSSION OF STACK USAGE
6303         In environments where stack memory is constrained, you  might  want  to         In environments where stack memory is constrained, you  might  want  to
6304         compile  PCRE to use heap memory instead of stack for remembering back-         compile  PCRE to use heap memory instead of stack for remembering back-
6305         up points. This makes it run a lot more slowly, however. Details of how         up points. This makes it run a lot more slowly, however. Details of how
6306         to do this are given in the pcrebuild documentation.         to do this are given in the pcrebuild documentation. When built in this
6307           way, instead of using the stack, PCRE obtains and frees memory by call-
6308         In  Unix-like environments, there is not often a problem with the stack         ing  the  functions  that  are  pointed to by the pcre_stack_malloc and
6309         unless very long strings are involved,  though  the  default  limit  on         pcre_stack_free variables. By default,  these  point  to  malloc()  and
6310         stack  size  varies  from system to system. Values from 8Mb to 64Mb are         free(),  but you can replace the pointers to cause PCRE to use your own
6311           functions. Since the block sizes are always the same,  and  are  always
6312           freed in reverse order, it may be possible to implement customized mem-
6313           ory handlers that are more efficient than the standard functions.
6314    
6315           In Unix-like environments, there is not often a problem with the  stack