/[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 358 by ph10, Wed Jul 9 11:03:07 2008 UTC revision 535 by ph10, Thu Jun 3 19:18:24 2010 UTC
# Line 2  Line 2 
2  This file contains a concatenation of the PCRE man pages, converted to plain  This file contains a concatenation of the PCRE man pages, converted to plain
3  text format for ease of searching with a text editor, or for use on systems  text format for ease of searching with a text editor, or for use on systems
4  that do not have a man page processor. The small individual files that give  that do not have a man page processor. The small individual files that give
5  synopses of each function in the library have not been included. There are  synopses of each function in the library have not been included. Neither has
6  separate text files for the pcregrep and pcretest commands.  the pcredemo program. There are separate text files for the pcregrep and
7    pcretest commands.
8  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
9    
10    
# Line 18  INTRODUCTION Line 19  INTRODUCTION
19    
20         The  PCRE  library is a set of functions that implement regular expres-         The  PCRE  library is a set of functions that implement regular expres-
21         sion pattern matching using the same syntax and semantics as Perl, with         sion pattern matching using the same syntax and semantics as Perl, with
22         just  a  few  differences. Certain features that appeared in Python and         just  a few differences. Some features that appeared in Python and PCRE
23         PCRE before they appeared in Perl are also available using  the  Python         before they appeared in Perl are also available using the  Python  syn-
24         syntax.  There is also some support for certain .NET and Oniguruma syn-         tax,  there  is  some  support for one or two .NET and Oniguruma syntax
25         tax items, and there is an option for  requesting  some  minor  changes         items, and there is an option for requesting some  minor  changes  that
26         that give better JavaScript compatibility.         give better JavaScript compatibility.
27    
28         The  current  implementation of PCRE (release 7.x) corresponds approxi-         The  current implementation of PCRE corresponds approximately with Perl
29         mately with Perl 5.10, including support for UTF-8 encoded strings  and         5.10/5.11, including support for UTF-8 encoded strings and Unicode gen-
30         Unicode general category properties. However, UTF-8 and Unicode support         eral  category properties. However, UTF-8 and Unicode support has to be
31         has to be explicitly enabled; it is not the default. The Unicode tables         explicitly enabled; it is not the default. The  Unicode  tables  corre-
32         correspond to Unicode release 5.0.0.         spond to Unicode release 5.2.0.
33    
34         In  addition to the Perl-compatible matching function, PCRE contains an         In  addition to the Perl-compatible matching function, PCRE contains an
35         alternative matching function that matches the same  compiled  patterns         alternative function that matches the same compiled patterns in a  dif-
36         in  a different way. In certain circumstances, the alternative function         ferent way. In certain circumstances, the alternative function has some
37         has some advantages. For a discussion of the two  matching  algorithms,         advantages.  For a discussion of the two matching algorithms,  see  the
38         see the pcrematching page.         pcrematching page.
39    
40         PCRE  is  written  in C and released as a C library. A number of people         PCRE  is  written  in C and released as a C library. A number of people
41         have written wrappers and interfaces of various kinds.  In  particular,         have written wrappers and interfaces of various kinds.  In  particular,
# Line 54  INTRODUCTION Line 55  INTRODUCTION
55         library is built. The pcre_config() function makes it  possible  for  a         library is built. The pcre_config() function makes it  possible  for  a
56         client  to  discover  which  features are available. The features them-         client  to  discover  which  features are available. The features them-
57         selves are described in the pcrebuild page. Documentation about  build-         selves are described in the pcrebuild page. Documentation about  build-
58         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 and
59         in the source distribution.         NON-UNIX-USE files in the source distribution.
60    
61         The library contains a number of undocumented  internal  functions  and         The library contains a number of undocumented  internal  functions  and
62         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
# Line 71  USER DOCUMENTATION Line 72  USER DOCUMENTATION
72         The user documentation for PCRE comprises a number  of  different  sec-         The user documentation for PCRE comprises a number  of  different  sec-
73         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
74         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.
75         In  the  plain text format, all the sections are concatenated, for ease         In  the  plain  text format, all the sections, except the pcredemo sec-
76         of searching. The sections are as follows:         tion, are concatenated, for ease of searching. The sections are as fol-
77           lows:
78    
79           pcre              this document           pcre              this document
80           pcre-config       show PCRE installation configuration information           pcre-config       show PCRE installation configuration information
# Line 81  USER DOCUMENTATION Line 83  USER DOCUMENTATION
83           pcrecallout       details of the callout feature           pcrecallout       details of the callout feature
84           pcrecompat        discussion of Perl compatibility           pcrecompat        discussion of Perl compatibility
85           pcrecpp           details of the C++ wrapper           pcrecpp           details of the C++ wrapper
86             pcredemo          a demonstration C program that uses PCRE
87           pcregrep          description of the pcregrep command           pcregrep          description of the pcregrep command
88           pcrematching      discussion of the two matching algorithms           pcrematching      discussion of the two matching algorithms
89           pcrepartial       details of the partial matching facility           pcrepartial       details of the partial matching facility
90           pcrepattern       syntax and semantics of supported           pcrepattern       syntax and semantics of supported
91                               regular expressions                               regular expressions
          pcresyntax        quick syntax reference  
92           pcreperform       discussion of performance issues           pcreperform       discussion of performance issues
93           pcreposix         the POSIX-compatible C API           pcreposix         the POSIX-compatible C API
94           pcreprecompile    details of saving and re-using precompiled patterns           pcreprecompile    details of saving and re-using precompiled patterns
95           pcresample        discussion of the sample program           pcresample        discussion of the pcredemo program
96           pcrestack         discussion of stack usage           pcrestack         discussion of stack usage
97             pcresyntax        quick syntax reference
98           pcretest          description of the pcretest testing command           pcretest          description of the pcretest testing command
99    
100         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
# Line 136  UTF-8 AND UNICODE PROPERTY SUPPORT Line 139  UTF-8 AND UNICODE PROPERTY SUPPORT
139    
140         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
141         support in the code, and, in addition,  you  must  call  pcre_compile()         support in the code, and, in addition,  you  must  call  pcre_compile()
142         with  the PCRE_UTF8 option flag. When you do this, both the pattern and         with  the  PCRE_UTF8  option  flag,  or the pattern must start with the
143         any subject strings that are matched against it are  treated  as  UTF-8         sequence (*UTF8). When either of these is the case,  both  the  pattern
144         strings instead of just strings of bytes.         and  any  subject  strings  that  are matched against it are treated as
145           UTF-8 strings instead of strings of 1-byte characters.
146    
147         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,
148         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
149         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
150         very big.         very big.
151    
152         If PCRE is built with Unicode character property support (which implies         If PCRE is built with Unicode character property support (which implies
153         UTF-8  support),  the  escape sequences \p{..}, \P{..}, and \X are sup-         UTF-8 support), the escape sequences \p{..}, \P{..}, and  \X  are  sup-
154         ported.  The available properties that can be tested are limited to the         ported.  The available properties that can be tested are limited to the
155         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
156         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,
157         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
158         pcrepattern documentation. Only the short names for properties are sup-         pcrepattern documentation. Only the short names for properties are sup-
159         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-
160         ter}, is not supported.  Furthermore,  in  Perl,  many  properties  may         ter},  is  not  supported.   Furthermore,  in Perl, many properties may
161         optionally  be  prefixed by "Is", for compatibility with Perl 5.6. PCRE         optionally be prefixed by "Is", for compatibility with Perl  5.6.  PCRE
162         does not support this.         does not support this.
163    
164     Validity of UTF-8 strings     Validity of UTF-8 strings
165    
166         When you set the PCRE_UTF8 flag, the strings  passed  as  patterns  and         When  you  set  the  PCRE_UTF8 flag, the strings passed as patterns and
167         subjects are (by default) checked for validity on entry to the relevant         subjects are (by default) checked for validity on entry to the relevant
168         functions. From release 7.3 of PCRE, the check is according  the  rules         functions.  From  release 7.3 of PCRE, the check is according the rules
169         of  RFC  3629, which are themselves derived from the Unicode specifica-         of RFC 3629, which are themselves derived from the  Unicode  specifica-
170         tion. Earlier releases of PCRE followed the rules of  RFC  2279,  which         tion.  Earlier  releases  of PCRE followed the rules of RFC 2279, which
171         allows  the  full range of 31-bit values (0 to 0x7FFFFFFF). The current         allows the full range of 31-bit values (0 to 0x7FFFFFFF).  The  current
172         check allows only values in the range U+0 to U+10FFFF, excluding U+D800         check allows only values in the range U+0 to U+10FFFF, excluding U+D800
173         to U+DFFF.         to U+DFFF.
174    
175         The  excluded  code  points are the "Low Surrogate Area" of Unicode, of         The excluded code points are the "Low Surrogate Area"  of  Unicode,  of
176         which the Unicode Standard says this: "The Low Surrogate Area does  not         which  the Unicode Standard says this: "The Low Surrogate Area does not
177         contain  any  character  assignments,  consequently  no  character code         contain any  character  assignments,  consequently  no  character  code
178         charts or namelists are provided for this area. Surrogates are reserved         charts or namelists are provided for this area. Surrogates are reserved
179         for  use  with  UTF-16 and then must be used in pairs." The code points         for use with UTF-16 and then must be used in pairs."  The  code  points
180         that are encoded by UTF-16 pairs  are  available  as  independent  code         that  are  encoded  by  UTF-16  pairs are available as independent code
181         points  in  the  UTF-8  encoding.  (In other words, the whole surrogate         points in the UTF-8 encoding. (In  other  words,  the  whole  surrogate
182         thing is a fudge for UTF-16 which unfortunately messes up UTF-8.)         thing is a fudge for UTF-16 which unfortunately messes up UTF-8.)
183    
184         If an  invalid  UTF-8  string  is  passed  to  PCRE,  an  error  return         If  an  invalid  UTF-8  string  is  passed  to  PCRE,  an  error return
185         (PCRE_ERROR_BADUTF8) is given. In some situations, you may already know         (PCRE_ERROR_BADUTF8) is given. In some situations, you may already know
186         that your strings are valid, and therefore want to skip these checks in         that your strings are valid, and therefore want to skip these checks in
187         order to improve performance. If you set the PCRE_NO_UTF8_CHECK flag at         order to improve performance. If you set the PCRE_NO_UTF8_CHECK flag at
188         compile time or at run time, PCRE assumes that the pattern  or  subject         compile  time  or at run time, PCRE assumes that the pattern or subject
189         it  is  given  (respectively)  contains only valid UTF-8 codes. In this         it is given (respectively) contains only valid  UTF-8  codes.  In  this
190         case, it does not diagnose an invalid UTF-8 string.         case, it does not diagnose an invalid UTF-8 string.
191    
192         If you pass an invalid UTF-8 string  when  PCRE_NO_UTF8_CHECK  is  set,         If  you  pass  an  invalid UTF-8 string when PCRE_NO_UTF8_CHECK is set,
193         what  happens  depends on why the string is invalid. If the string con-         what happens depends on why the string is invalid. If the  string  con-
194         forms to the "old" definition of UTF-8 (RFC 2279), it is processed as a         forms to the "old" definition of UTF-8 (RFC 2279), it is processed as a
195         string  of  characters  in  the  range 0 to 0x7FFFFFFF. In other words,         string of characters in the range 0  to  0x7FFFFFFF.  In  other  words,
196         apart from the initial validity test, PCRE (when in UTF-8 mode) handles         apart from the initial validity test, PCRE (when in UTF-8 mode) handles
197         strings  according  to  the more liberal rules of RFC 2279. However, if         strings according to the more liberal rules of RFC  2279.  However,  if
198         the string does not even conform to RFC 2279, the result is  undefined.         the  string does not even conform to RFC 2279, the result is undefined.
199         Your program may crash.         Your program may crash.
200    
201         If  you  want  to  process  strings  of  values  in the full range 0 to         If you want to process strings  of  values  in  the  full  range  0  to
202         0x7FFFFFFF, encoded in a UTF-8-like manner as per the old RFC, you  can         0x7FFFFFFF,  encoded in a UTF-8-like manner as per the old RFC, you can
203         set PCRE_NO_UTF8_CHECK to bypass the more restrictive test. However, in         set PCRE_NO_UTF8_CHECK to bypass the more restrictive test. However, in
204         this situation, you will have to apply your own validity check.         this situation, you will have to apply your own validity check.
205    
206     General comments about UTF-8 mode     General comments about UTF-8 mode
207    
208         1. An unbraced hexadecimal escape sequence (such  as  \xb3)  matches  a         1.  An  unbraced  hexadecimal  escape sequence (such as \xb3) matches a
209         two-byte UTF-8 character if the value is greater than 127.         two-byte UTF-8 character if the value is greater than 127.
210    
211         2.  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
212         characters for values greater than \177.         characters for values greater than \177.
213    
214         3. Repeat quantifiers apply to complete UTF-8 characters, not to  indi-         3.  Repeat quantifiers apply to complete UTF-8 characters, not to indi-
215         vidual bytes, for example: \x{100}{3}.         vidual bytes, for example: \x{100}{3}.
216    
217         4.  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-
218         gle byte.         gle byte.
219    
220         5. 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
221         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
222         not available in the alternative matching function, pcre_dfa_exec().         not available in the alternative matching function, pcre_dfa_exec().
223    
224         6. 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
225         test  characters of any code value, but the characters that PCRE recog-         test characters of any code value, but, by default, the characters that
226         nizes as digits, spaces, or word characters  remain  the  same  set  as         PCRE  recognizes  as digits, spaces, or word characters remain the same
227         before, all with values less than 256. This remains true even when PCRE         set as before, all with values less than 256. This  remains  true  even
228         includes Unicode property support, because to do otherwise  would  slow         when  PCRE  is built to include Unicode property support, because to do
229         down  PCRE in many common cases. If you really want to test for a wider         otherwise would slow down PCRE in many common  cases.  Note  that  this
230         sense of, say, "digit", you must use Unicode  property  tests  such  as         also applies to \b, because it is defined in terms of \w and \W. If you
231         \p{Nd}.         really want to test for a wider sense of, say,  "digit",  you  can  use
232           explicit  Unicode property tests such as \p{Nd}.  Alternatively, if you
233           set the PCRE_UCP option, the way that the  character  escapes  work  is
234           changed  so that Unicode properties are used to determine which charac-
235           ters match. There are more details in the section on generic  character
236           types in the pcrepattern documentation.
237    
238         7.  Similarly,  characters that match the POSIX named character classes         7.  Similarly,  characters that match the POSIX named character classes
239         are all low-valued characters.         are all low-valued characters, unless the PCRE_UCP option is set.
240    
241         8. However, the Perl 5.10 horizontal and vertical  whitespace  matching         8. However, the Perl 5.10 horizontal and vertical  whitespace  matching
242         escapes (\h, \H, \v, and \V) do match all the appropriate Unicode char-         escapes (\h, \H, \v, and \V) do match all the appropriate Unicode char-
243         acters.         acters, whether or not PCRE_UCP is set.
244    
245         9. Case-insensitive matching applies only to  characters  whose  values         9. Case-insensitive matching applies only to  characters  whose  values
246         are  less than 128, unless PCRE is built with Unicode property support.         are  less than 128, unless PCRE is built with Unicode property support.
# Line 258  AUTHOR Line 267  AUTHOR
267    
268  REVISION  REVISION
269    
270         Last updated: 12 April 2008         Last updated: 12 May 2010
271         Copyright (c) 1997-2008 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
272  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
273    
274    
# Line 277  PCRE BUILD-TIME OPTIONS Line 286  PCRE BUILD-TIME OPTIONS
286         script,  where the optional features are selected or deselected by pro-         script,  where the optional features are selected or deselected by pro-
287         viding options to configure before running the make  command.  However,         viding options to configure before running the make  command.  However,
288         the  same  options  can be selected in both Unix-like and non-Unix-like         the  same  options  can be selected in both Unix-like and non-Unix-like
289         environments using the GUI facility of  CMakeSetup  if  you  are  using         environments using the GUI facility of cmake-gui if you are using CMake
290         CMake instead of configure to build PCRE.         instead of configure to build PCRE.
291    
292           There  is  a  lot more information about building PCRE in non-Unix-like
293           environments in the file called NON_UNIX_USE, which is part of the PCRE
294           distribution.  You  should consult this file as well as the README file
295           if you are building in a non-Unix-like environment.
296    
297         The complete list of options for configure (which includes the standard         The complete list of options for configure (which includes the standard
298         ones such as the  selection  of  the  installation  directory)  can  be         ones  such  as  the  selection  of  the  installation directory) can be
299         obtained by running         obtained by running
300    
301           ./configure --help           ./configure --help
302    
303         The  following  sections  include  descriptions  of options whose names         The following sections include  descriptions  of  options  whose  names
304         begin with --enable or --disable. These settings specify changes to the         begin with --enable or --disable. These settings specify changes to the
305         defaults  for  the configure command. Because of the way that configure         defaults for the configure command. Because of the way  that  configure
306         works, --enable and --disable always come in pairs, so  the  complemen-         works,  --enable  and --disable always come in pairs, so the complemen-
307         tary  option always exists as well, but as it specifies the default, it         tary option always exists as well, but as it specifies the default,  it
308         is not described.         is not described.
309    
310    
# Line 307  C++ SUPPORT Line 321  C++ SUPPORT
321    
322  UTF-8 SUPPORT  UTF-8 SUPPORT
323    
324         To build PCRE with support for UTF-8 character strings, add         To build PCRE with support for UTF-8 Unicode character strings, add
325    
326           --enable-utf8           --enable-utf8
327    
328         to the configure command. Of itself, this  does  not  make  PCRE  treat         to  the  configure  command.  Of  itself, this does not make PCRE treat
329         strings  as UTF-8. As well as compiling PCRE with this option, you also         strings as UTF-8. As well as compiling PCRE with this option, you  also
330         have have to set the PCRE_UTF8 option when you call the  pcre_compile()         have  have to set the PCRE_UTF8 option when you call the pcre_compile()
331         function.         or pcre_compile2() functions.
332    
333           If you set --enable-utf8 when compiling in an EBCDIC environment,  PCRE
334           expects its input to be either ASCII or UTF-8 (depending on the runtime
335           option). It is not possible to support both EBCDIC and UTF-8  codes  in
336           the  same  version  of  the  library.  Consequently,  --enable-utf8 and
337           --enable-ebcdic are mutually exclusive.
338    
339    
340  UNICODE CHARACTER PROPERTY SUPPORT  UNICODE CHARACTER PROPERTY SUPPORT
341    
342         UTF-8  support allows PCRE to process character values greater than 255         UTF-8 support allows PCRE to process character values greater than  255
343         in the strings that it handles. On its own, however, it does  not  pro-         in  the  strings that it handles. On its own, however, it does not pro-
344         vide any facilities for accessing the properties of such characters. If         vide any facilities for accessing the properties of such characters. If
345         you want to be able to use the pattern escapes \P, \p,  and  \X,  which         you  want  to  be able to use the pattern escapes \P, \p, and \X, which
346         refer to Unicode character properties, you must add         refer to Unicode character properties, you must add
347    
348           --enable-unicode-properties           --enable-unicode-properties
349    
350         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
351         not explicitly requested it.         not explicitly requested it.
352    
353         Including Unicode property support adds around 30K  of  tables  to  the         Including  Unicode  property  support  adds around 30K of tables to the
354         PCRE  library.  Only  the general category properties such as Lu and Nd         PCRE library. Only the general category properties such as  Lu  and  Nd
355         are supported. Details are given in the pcrepattern documentation.         are supported. Details are given in the pcrepattern documentation.
356    
357    
358  CODE VALUE OF NEWLINE  CODE VALUE OF NEWLINE
359    
360         By default, PCRE interprets character 10 (linefeed, LF)  as  indicating         By  default,  PCRE interprets the linefeed (LF) character as indicating
361         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
362         systems. You can compile PCRE to use character 13 (carriage return, CR)         systems.  You  can compile PCRE to use carriage return (CR) instead, by
363         instead, by adding         adding
364    
365           --enable-newline-is-cr           --enable-newline-is-cr
366    
367         to  the  configure  command.  There  is  also  a --enable-newline-is-lf         to the  configure  command.  There  is  also  a  --enable-newline-is-lf
368         option, which explicitly specifies linefeed as the newline character.         option, which explicitly specifies linefeed as the newline character.
369    
370         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 356  CODE VALUE OF NEWLINE Line 376  CODE VALUE OF NEWLINE
376    
377           --enable-newline-is-anycrlf           --enable-newline-is-anycrlf
378    
379         which  causes  PCRE  to recognize any of the three sequences CR, LF, or         which causes PCRE to recognize any of the three sequences  CR,  LF,  or
380         CRLF as indicating a line ending. Finally, a fifth option, specified by         CRLF as indicating a line ending. Finally, a fifth option, specified by
381    
382           --enable-newline-is-any           --enable-newline-is-any
# Line 416  HANDLING VERY LARGE PATTERNS Line 436  HANDLING VERY LARGE PATTERNS
436         nation metacharacter). By default, two-byte values are used  for  these         nation metacharacter). By default, two-byte values are used  for  these
437         offsets,  leading  to  a  maximum size for a compiled pattern of around         offsets,  leading  to  a  maximum size for a compiled pattern of around
438         64K. This is sufficient to handle all but the most  gigantic  patterns.         64K. This is sufficient to handle all but the most  gigantic  patterns.
439         Nevertheless,  some  people do want to process enormous patterns, so it         Nevertheless,  some  people do want to process truyl enormous patterns,
440         is possible to compile PCRE to use three-byte or four-byte  offsets  by         so it is possible to compile PCRE to use three-byte or  four-byte  off-
441         adding a setting such as         sets by adding a setting such as
442    
443           --with-link-size=3           --with-link-size=3
444    
# Line 445  AVOIDING EXCESSIVE STACK USAGE Line 465  AVOIDING EXCESSIVE STACK USAGE
465         to the configure command. With this configuration, PCRE  will  use  the         to the configure command. With this configuration, PCRE  will  use  the
466         pcre_stack_malloc  and pcre_stack_free variables to call memory manage-         pcre_stack_malloc  and pcre_stack_free variables to call memory manage-
467         ment functions. By default these point to malloc() and free(), but  you         ment functions. By default these point to malloc() and free(), but  you
468         can replace the pointers so that your own functions are used.         can replace the pointers so that your own functions are used instead.
469    
470         Separate  functions  are  provided  rather  than  using pcre_malloc and         Separate  functions  are  provided  rather  than  using pcre_malloc and
471         pcre_free because the  usage  is  very  predictable:  the  block  sizes         pcre_free because the  usage  is  very  predictable:  the  block  sizes
# Line 453  AVOIDING EXCESSIVE STACK USAGE Line 473  AVOIDING EXCESSIVE STACK USAGE
473         reverse order. A calling program might be able to  implement  optimized         reverse order. A calling program might be able to  implement  optimized
474         functions  that  perform  better  than  malloc()  and free(). PCRE runs         functions  that  perform  better  than  malloc()  and free(). PCRE runs
475         noticeably more slowly when built in this way. This option affects only         noticeably more slowly when built in this way. This option affects only
476         the   pcre_exec()   function;   it   is   not   relevant  for  the  the         the pcre_exec() function; it is not relevant for pcre_dfa_exec().
        pcre_dfa_exec() function.  
477    
478    
479  LIMITING PCRE RESOURCE USAGE  LIMITING PCRE RESOURCE USAGE
480    
481         Internally, PCRE has a function called match(), which it calls  repeat-         Internally,  PCRE has a function called match(), which it calls repeat-
482         edly   (sometimes   recursively)  when  matching  a  pattern  with  the         edly  (sometimes  recursively)  when  matching  a  pattern   with   the
483         pcre_exec() function. By controlling the maximum number of  times  this         pcre_exec()  function.  By controlling the maximum number of times this
484         function  may be called during a single matching operation, a limit can         function may be called during a single matching operation, a limit  can
485         be placed on the resources used by a single call  to  pcre_exec().  The         be  placed  on  the resources used by a single call to pcre_exec(). The
486         limit  can be changed at run time, as described in the pcreapi documen-         limit can be changed at run time, as described in the pcreapi  documen-
487         tation. The default is 10 million, but this can be changed by adding  a         tation.  The default is 10 million, but this can be changed by adding a
488         setting such as         setting such as
489    
490           --with-match-limit=500000           --with-match-limit=500000
491    
492         to   the   configure  command.  This  setting  has  no  effect  on  the         to  the  configure  command.  This  setting  has  no  effect   on   the
493         pcre_dfa_exec() matching function.         pcre_dfa_exec() matching function.
494    
495         In some environments it is desirable to limit the  depth  of  recursive         In  some  environments  it is desirable to limit the depth of recursive
496         calls of match() more strictly than the total number of calls, in order         calls of match() more strictly than the total number of calls, in order
497         to restrict the maximum amount of stack (or heap,  if  --disable-stack-         to  restrict  the maximum amount of stack (or heap, if --disable-stack-
498         for-recursion is specified) that is used. A second limit controls this;         for-recursion is specified) that is used. A second limit controls this;
499         it defaults to the value that  is  set  for  --with-match-limit,  which         it  defaults  to  the  value  that is set for --with-match-limit, which
500         imposes  no  additional constraints. However, you can set a lower limit         imposes no additional constraints. However, you can set a  lower  limit
501         by adding, for example,         by adding, for example,
502    
503           --with-match-limit-recursion=10000           --with-match-limit-recursion=10000
504    
505         to the configure command. This value can  also  be  overridden  at  run         to  the  configure  command.  This  value can also be overridden at run
506         time.         time.
507    
508    
509  CREATING CHARACTER TABLES AT BUILD TIME  CREATING CHARACTER TABLES AT BUILD TIME
510    
511         PCRE  uses fixed tables for processing characters whose code values are         PCRE uses fixed tables for processing characters whose code values  are
512         less than 256. By default, PCRE is built with a set of tables that  are         less  than 256. By default, PCRE is built with a set of tables that are
513         distributed  in  the  file pcre_chartables.c.dist. These tables are for         distributed in the file pcre_chartables.c.dist. These  tables  are  for
514         ASCII codes only. If you add         ASCII codes only. If you add
515    
516           --enable-rebuild-chartables           --enable-rebuild-chartables
517    
518         to the configure command, the distributed tables are  no  longer  used.         to  the  configure  command, the distributed tables are no longer used.
519         Instead,  a  program  called dftables is compiled and run. This outputs         Instead, a program called dftables is compiled and  run.  This  outputs
520         the source for new set of tables, created in the default locale of your         the source for new set of tables, created in the default locale of your
521         C runtime system. (This method of replacing the tables does not work if         C runtime system. (This method of replacing the tables does not work if
522         you are cross compiling, because dftables is run on the local host.  If         you  are cross compiling, because dftables is run on the local host. If
523         you  need  to  create alternative tables when cross compiling, you will         you need to create alternative tables when cross  compiling,  you  will
524         have to do so "by hand".)         have to do so "by hand".)
525    
526    
527  USING EBCDIC CODE  USING EBCDIC CODE
528    
529         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
530         character  code  is  ASCII  (or Unicode, which is a superset of ASCII).         character code is ASCII (or Unicode, which is  a  superset  of  ASCII).
531         This is the case for most computer operating systems.  PCRE  can,  how-         This  is  the  case for most computer operating systems. PCRE can, how-
532         ever, be compiled to run in an EBCDIC environment by adding         ever, be compiled to run in an EBCDIC environment by adding
533    
534           --enable-ebcdic           --enable-ebcdic
535    
536         to the configure command. This setting implies --enable-rebuild-charta-         to the configure command. This setting implies --enable-rebuild-charta-
537         bles. You should only use it if you know that  you  are  in  an  EBCDIC         bles.  You  should  only  use  it if you know that you are in an EBCDIC
538         environment (for example, an IBM mainframe operating system).         environment (for example,  an  IBM  mainframe  operating  system).  The
539           --enable-ebcdic option is incompatible with --enable-utf8.
540    
541    
542  PCREGREP OPTIONS FOR COMPRESSED FILE SUPPORT  PCREGREP OPTIONS FOR COMPRESSED FILE SUPPORT
# Line 542  PCRETEST OPTION FOR LIBREADLINE SUPPORT Line 562  PCRETEST OPTION FOR LIBREADLINE SUPPORT
562         to  the  configure  command,  pcretest  is  linked with the libreadline         to  the  configure  command,  pcretest  is  linked with the libreadline
563         library, and when its input is from a terminal, it reads it  using  the         library, and when its input is from a terminal, it reads it  using  the
564         readline() function. This provides line-editing and history facilities.         readline() function. This provides line-editing and history facilities.
565         Note that libreadline is GPL-licenced, so if you distribute a binary of         Note that libreadline is GPL-licensed, so if you distribute a binary of
566         pcretest linked in this way, there may be licensing issues.         pcretest linked in this way, there may be licensing issues.
567    
568         Setting  this  option  causes  the -lreadline option to be added to the         Setting  this  option  causes  the -lreadline option to be added to the
# Line 578  AUTHOR Line 598  AUTHOR
598    
599  REVISION  REVISION
600    
601         Last updated: 13 April 2008         Last updated: 29 September 2009
602         Copyright (c) 1997-2008 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
603  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
604    
605    
# Line 666  THE ALTERNATIVE MATCHING ALGORITHM Line 686  THE ALTERNATIVE MATCHING ALGORITHM
686         though  it is not implemented as a traditional finite state machine (it         though  it is not implemented as a traditional finite state machine (it
687         keeps multiple states active simultaneously).         keeps multiple states active simultaneously).
688    
689           Although the general principle of this matching algorithm  is  that  it
690           scans  the subject string only once, without backtracking, there is one
691           exception: when a lookaround assertion is encountered,  the  characters
692           following  or  preceding  the  current  point  have to be independently
693           inspected.
694    
695         The scan continues until either the end of the subject is  reached,  or         The scan continues until either the end of the subject is  reached,  or
696         there  are  no more unterminated paths. At this point, terminated paths         there  are  no more unterminated paths. At this point, terminated paths
697         represent the different matching possibilities (if there are none,  the         represent the different matching possibilities (if there are none,  the
698         match  has  failed).   Thus,  if there is more than one possible match,         match  has  failed).   Thus,  if there is more than one possible match,
699         this algorithm finds all of them, and in particular, it finds the long-         this algorithm finds all of them, and in particular, it finds the long-
700         est.  In PCRE, there is an option to stop the algorithm after the first         est.  There  is  an  option to stop the algorithm after the first match
701         match (which is necessarily the shortest) has been found.         (which is necessarily the shortest) is found.
702    
703         Note that all the matches that are found start at the same point in the         Note that all the matches that are found start at the same point in the
704         subject. If the pattern         subject. If the pattern
# Line 742  ADVANTAGES OF THE ALTERNATIVE ALGORITHM Line 768  ADVANTAGES OF THE ALTERNATIVE ALGORITHM
768         more than one match using the standard algorithm, you have to do kludgy         more than one match using the standard algorithm, you have to do kludgy
769         things with callouts.         things with callouts.
770    
771         2.  There is much better support for partial matching. The restrictions         2.  Because  the  alternative  algorithm  scans the subject string just
        on the content of the pattern that apply when using the standard  algo-  
        rithm  for  partial matching do not apply to the alternative algorithm.  
        For non-anchored patterns, the starting position of a partial match  is  
        available.  
   
        3.  Because  the  alternative  algorithm  scans the subject string just  
772         once, and never needs to backtrack, it is possible to  pass  very  long         once, and never needs to backtrack, it is possible to  pass  very  long
773         subject  strings  to  the matching function in several pieces, checking         subject  strings  to  the matching function in several pieces, checking
774         for partial matching each time.         for partial matching each time.  The  pcrepartial  documentation  gives
775           details of partial matching.
776    
777    
778  DISADVANTAGES OF THE ALTERNATIVE ALGORITHM  DISADVANTAGES OF THE ALTERNATIVE ALGORITHM
779    
780         The alternative algorithm suffers from a number of disadvantages:         The alternative algorithm suffers from a number of disadvantages:
781    
782         1. It is substantially slower than  the  standard  algorithm.  This  is         1.  It  is  substantially  slower  than the standard algorithm. This is
783         partly  because  it has to search for all possible matches, but is also         partly because it has to search for all possible matches, but  is  also
784         because it is less susceptible to optimization.         because it is less susceptible to optimization.
785    
786         2. Capturing parentheses and back references are not supported.         2. Capturing parentheses and back references are not supported.
# Line 777  AUTHOR Line 798  AUTHOR
798    
799  REVISION  REVISION
800    
801         Last updated: 19 April 2008         Last updated: 29 September 2009
802         Copyright (c) 1997-2008 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
803  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
804    
805    
# Line 885  PCRE API OVERVIEW Line 906  PCRE API OVERVIEW
906         bers for the library.  Applications can use these  to  include  support         bers for the library.  Applications can use these  to  include  support
907         for different releases of PCRE.         for different releases of PCRE.
908    
909           In a Windows environment, if you want to statically link an application
910           program against a non-dll pcre.a  file,  you  must  define  PCRE_STATIC
911           before  including  pcre.h or pcrecpp.h, because otherwise the pcre_mal-
912           loc()   and   pcre_free()   exported   functions   will   be   declared
913           __declspec(dllimport), with unwanted results.
914    
915         The   functions   pcre_compile(),  pcre_compile2(),  pcre_study(),  and         The   functions   pcre_compile(),  pcre_compile2(),  pcre_study(),  and
916         pcre_exec() are used for compiling and matching regular expressions  in         pcre_exec() are used for compiling and matching regular expressions  in
917         a  Perl-compatible  manner. A sample program that demonstrates the sim-         a  Perl-compatible  manner. A sample program that demonstrates the sim-
918         plest way of using them is provided in the file  called  pcredemo.c  in         plest way of using them is provided in the file  called  pcredemo.c  in
919         the  source distribution. The pcresample documentation describes how to         the PCRE source distribution. A listing of this program is given in the
920         compile and run it.         pcredemo documentation, and the pcresample documentation describes  how
921           to compile and run it.
922    
923         A second matching function, pcre_dfa_exec(), which is not Perl-compati-         A second matching function, pcre_dfa_exec(), which is not Perl-compati-
924         ble,  is  also provided. This uses a different algorithm for the match-         ble, is also provided. This uses a different algorithm for  the  match-
925         ing. The alternative algorithm finds all possible matches (at  a  given         ing.  The  alternative algorithm finds all possible matches (at a given
926         point  in  the subject), and scans the subject just once. However, this         point in the subject), and scans the subject just  once  (unless  there
927         algorithm does not return captured substrings. A description of the two         are  lookbehind  assertions).  However,  this algorithm does not return
928         matching  algorithms and their advantages and disadvantages is given in         captured substrings. A description of the two matching  algorithms  and
929         the pcrematching documentation.         their  advantages  and disadvantages is given in the pcrematching docu-
930           mentation.
931    
932         In addition to the main compiling and  matching  functions,  there  are         In addition to the main compiling and  matching  functions,  there  are
933         convenience functions for extracting captured substrings from a subject         convenience functions for extracting captured substrings from a subject
# Line 999  MULTITHREADING Line 1028  MULTITHREADING
1028         pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the         pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the
1029         callout function pointed to by pcre_callout, are shared by all threads.         callout function pointed to by pcre_callout, are shared by all threads.
1030    
1031         The compiled form of a regular expression is not altered during  match-         The  compiled form of a regular expression is not altered during match-
1032         ing, so the same compiled pattern can safely be used by several threads         ing, so the same compiled pattern can safely be used by several threads
1033         at once.         at once.
1034    
# Line 1007  MULTITHREADING Line 1036  MULTITHREADING
1036  SAVING PRECOMPILED PATTERNS FOR LATER USE  SAVING PRECOMPILED PATTERNS FOR LATER USE
1037    
1038         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
1039         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
1040         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
1041         pcreprecompile  documentation.  However, compiling a regular expression         pcreprecompile documentation. However, compiling a  regular  expression
1042         with one version of PCRE for use with a different version is not  guar-         with  one version of PCRE for use with a different version is not guar-
1043         anteed to work and may cause crashes.         anteed to work and may cause crashes.
1044    
1045    
# Line 1018  CHECKING BUILD-TIME OPTIONS Line 1047  CHECKING BUILD-TIME OPTIONS
1047    
1048         int pcre_config(int what, void *where);         int pcre_config(int what, void *where);
1049    
1050         The  function pcre_config() makes it possible for a PCRE client to dis-         The function pcre_config() makes it possible for a PCRE client to  dis-
1051         cover which optional features have been compiled into the PCRE library.         cover which optional features have been compiled into the PCRE library.
1052         The  pcrebuild documentation has more details about these optional fea-         The pcrebuild documentation has more details about these optional  fea-
1053         tures.         tures.
1054    
1055         The first argument for pcre_config() is an  integer,  specifying  which         The  first  argument  for pcre_config() is an integer, specifying which
1056         information is required; the second argument is a pointer to a variable         information is required; the second argument is a pointer to a variable
1057         into which the information is  placed.  The  following  information  is         into  which  the  information  is  placed. The following information is
1058         available:         available:
1059    
1060           PCRE_CONFIG_UTF8           PCRE_CONFIG_UTF8
1061    
1062         The  output is an integer that is set to one if UTF-8 support is avail-         The output is an integer that is set to one if UTF-8 support is  avail-
1063         able; otherwise it is set to zero.         able; otherwise it is set to zero.
1064    
1065           PCRE_CONFIG_UNICODE_PROPERTIES           PCRE_CONFIG_UNICODE_PROPERTIES
1066    
1067         The output is an integer that is set to  one  if  support  for  Unicode         The  output  is  an  integer  that is set to one if support for Unicode
1068         character properties is available; otherwise it is set to zero.         character properties is available; otherwise it is set to zero.
1069    
1070           PCRE_CONFIG_NEWLINE           PCRE_CONFIG_NEWLINE
1071    
1072         The  output  is  an integer whose value specifies the default character         The output is an integer whose value specifies  the  default  character
1073         sequence that is recognized as meaning "newline". The four values  that         sequence  that is recognized as meaning "newline". The four values that
1074         are supported are: 10 for LF, 13 for CR, 3338 for CRLF, -2 for ANYCRLF,         are supported are: 10 for LF, 13 for CR, 3338 for CRLF, -2 for ANYCRLF,
1075         and -1 for ANY. The default should normally be  the  standard  sequence         and  -1  for  ANY.  Though they are derived from ASCII, the same values
1076         for your operating system.         are returned in EBCDIC environments. The default should normally corre-
1077           spond to the standard sequence for your operating system.
1078    
1079           PCRE_CONFIG_BSR           PCRE_CONFIG_BSR
1080    
# Line 1071  CHECKING BUILD-TIME OPTIONS Line 1101  CHECKING BUILD-TIME OPTIONS
1101    
1102           PCRE_CONFIG_MATCH_LIMIT           PCRE_CONFIG_MATCH_LIMIT
1103    
1104         The output is an integer that gives the default limit for the number of         The output is a long integer that gives the default limit for the  num-
1105         internal  matching  function  calls in a pcre_exec() execution. Further         ber  of  internal  matching  function calls in a pcre_exec() execution.
1106         details are given with pcre_exec() below.         Further details are given with pcre_exec() below.
1107    
1108           PCRE_CONFIG_MATCH_LIMIT_RECURSION           PCRE_CONFIG_MATCH_LIMIT_RECURSION
1109    
1110         The output is an integer that gives the default limit for the depth  of         The output is a long integer that gives the default limit for the depth
1111         recursion  when calling the internal matching function in a pcre_exec()         of   recursion  when  calling  the  internal  matching  function  in  a
1112         execution. Further details are given with pcre_exec() below.         pcre_exec() execution.  Further  details  are  given  with  pcre_exec()
1113           below.
1114    
1115           PCRE_CONFIG_STACKRECURSE           PCRE_CONFIG_STACKRECURSE
1116    
1117         The output is an integer that is set to one if internal recursion  when         The  output is an integer that is set to one if internal recursion when
1118         running pcre_exec() is implemented by recursive function calls that use         running pcre_exec() is implemented by recursive function calls that use
1119         the stack to remember their state. This is the usual way that  PCRE  is         the  stack  to remember their state. This is the usual way that PCRE is
1120         compiled. The output is zero if PCRE was compiled to use blocks of data         compiled. The output is zero if PCRE was compiled to use blocks of data
1121         on the  heap  instead  of  recursive  function  calls.  In  this  case,         on  the  heap  instead  of  recursive  function  calls.  In  this case,
1122         pcre_stack_malloc  and  pcre_stack_free  are  called  to  manage memory         pcre_stack_malloc and  pcre_stack_free  are  called  to  manage  memory
1123         blocks on the heap, thus avoiding the use of the stack.         blocks on the heap, thus avoiding the use of the stack.
1124    
1125    
# Line 1105  COMPILING A PATTERN Line 1136  COMPILING A PATTERN
1136    
1137         Either of the functions pcre_compile() or pcre_compile2() can be called         Either of the functions pcre_compile() or pcre_compile2() can be called
1138         to compile a pattern into an internal form. The only difference between         to compile a pattern into an internal form. The only difference between
1139         the two interfaces is that pcre_compile2() has an additional  argument,         the  two interfaces is that pcre_compile2() has an additional argument,
1140         errorcodeptr, via which a numerical error code can be returned.         errorcodeptr, via which a numerical error  code  can  be  returned.  To
1141           avoid  too  much repetition, we refer just to pcre_compile() below, but
1142           the information applies equally to pcre_compile2().
1143    
1144         The pattern is a C string terminated by a binary zero, and is passed in         The pattern is a C string terminated by a binary zero, and is passed in
1145         the pattern argument. A pointer to a single block  of  memory  that  is         the  pattern  argument.  A  pointer to a single block of memory that is
1146         obtained  via  pcre_malloc is returned. This contains the compiled code         obtained via pcre_malloc is returned. This contains the  compiled  code
1147         and related data. The pcre type is defined for the returned block; this         and related data. The pcre type is defined for the returned block; this
1148         is a typedef for a structure whose contents are not externally defined.         is a typedef for a structure whose contents are not externally defined.
1149         It is up to the caller to free the memory (via pcre_free) when it is no         It is up to the caller to free the memory (via pcre_free) when it is no
1150         longer required.         longer required.
1151    
1152         Although  the compiled code of a PCRE regex is relocatable, that is, it         Although the compiled code of a PCRE regex is relocatable, that is,  it
1153         does not depend on memory location, the complete pcre data block is not         does not depend on memory location, the complete pcre data block is not
1154         fully  relocatable, because it may contain a copy of the tableptr argu-         fully relocatable, because it may contain a copy of the tableptr  argu-
1155         ment, which is an address (see below).         ment, which is an address (see below).
1156    
1157         The options argument contains various bit settings that affect the com-         The options argument contains various bit settings that affect the com-
1158         pilation.  It  should be zero if no options are required. The available         pilation. It should be zero if no options are required.  The  available
1159         options are described below. Some of them, in  particular,  those  that         options  are  described  below. Some of them (in particular, those that
1160         are  compatible  with  Perl,  can also be set and unset from within the         are compatible with Perl, but some others as well) can also be set  and
1161         pattern (see the detailed description  in  the  pcrepattern  documenta-         unset  from  within  the  pattern  (see the detailed description in the
1162         tion).  For  these options, the contents of the options argument speci-         pcrepattern documentation). For those options that can be different  in
1163         fies their initial settings at the start of compilation and  execution.         different  parts  of  the pattern, the contents of the options argument
1164         The  PCRE_ANCHORED  and PCRE_NEWLINE_xxx options can be set at the time         specifies their settings at the start of compilation and execution. The
1165         of matching as well as at compile time.         PCRE_ANCHORED, PCRE_BSR_xxx, and PCRE_NEWLINE_xxx options can be set at
1166           the time of matching as well as at compile time.
1167    
1168         If errptr is NULL, pcre_compile() returns NULL immediately.  Otherwise,         If errptr is NULL, pcre_compile() returns NULL immediately.  Otherwise,
1169         if  compilation  of  a  pattern fails, pcre_compile() returns NULL, and         if  compilation  of  a  pattern fails, pcre_compile() returns NULL, and
1170         sets the variable pointed to by errptr to point to a textual error mes-         sets the variable pointed to by errptr to point to a textual error mes-
1171         sage. This is a static string that is part of the library. You must not         sage. This is a static string that is part of the library. You must not
1172         try to free it. The offset from the start of the pattern to the charac-         try to free it. The byte offset from the start of the  pattern  to  the
1173         ter where the error was discovered is placed in the variable pointed to         character  that  was  being  processed when the error was discovered is
1174         by erroffset, which must not be NULL. If it is, an immediate  error  is         placed in the variable pointed to by erroffset, which must not be NULL.
1175         given.         If  it  is,  an  immediate error is given. Some errors are not detected
1176           until checks are carried out when the whole pattern has  been  scanned;
1177           in this case the offset is set to the end of the pattern.
1178    
1179         If  pcre_compile2()  is  used instead of pcre_compile(), and the error-         If  pcre_compile2()  is  used instead of pcre_compile(), and the error-
1180         codeptr argument is not NULL, a non-zero error code number is  returned         codeptr argument is not NULL, a non-zero error code number is  returned
# Line 1255  COMPILING A PATTERN Line 1291  COMPILING A PATTERN
1291         letter that has no special meaning  causes  an  error,  thus  reserving         letter that has no special meaning  causes  an  error,  thus  reserving
1292         these  combinations  for  future  expansion.  By default, as in Perl, a         these  combinations  for  future  expansion.  By default, as in Perl, a
1293         backslash followed by a letter with no special meaning is treated as  a         backslash followed by a letter with no special meaning is treated as  a
1294         literal.  (Perl can, however, be persuaded to give a warning for this.)         literal. (Perl can, however, be persuaded to give an error for this, by
1295         There are at present no other features controlled by  this  option.  It         running it with the -w option.) There are at present no other  features
1296         can also be set by a (?X) option setting within a pattern.         controlled  by this option. It can also be set by a (?X) option setting
1297           within a pattern.
1298    
1299           PCRE_FIRSTLINE           PCRE_FIRSTLINE
1300    
1301         If  this  option  is  set,  an  unanchored pattern is required to match         If this option is set, an  unanchored  pattern  is  required  to  match
1302         before or at the first  newline  in  the  subject  string,  though  the         before  or  at  the  first  newline  in  the subject string, though the
1303         matched text may continue over the newline.         matched text may continue over the newline.
1304    
1305           PCRE_JAVASCRIPT_COMPAT           PCRE_JAVASCRIPT_COMPAT
1306    
1307         If this option is set, PCRE's behaviour is changed in some ways so that         If this option is set, PCRE's behaviour is changed in some ways so that
1308         it is compatible with JavaScript rather than Perl. The changes  are  as         it  is  compatible with JavaScript rather than Perl. The changes are as
1309         follows:         follows:
1310    
1311         (1)  A  lone  closing square bracket in a pattern causes a compile-time         (1) A lone closing square bracket in a pattern  causes  a  compile-time
1312         error, because this is illegal in JavaScript (by default it is  treated         error,  because this is illegal in JavaScript (by default it is treated
1313         as a data character). Thus, the pattern AB]CD becomes illegal when this         as a data character). Thus, the pattern AB]CD becomes illegal when this
1314         option is set.         option is set.
1315    
1316         (2) At run time, a back reference to an unset subpattern group  matches         (2)  At run time, a back reference to an unset subpattern group matches
1317         an  empty  string (by default this causes the current matching alterna-         an empty string (by default this causes the current  matching  alterna-
1318         tive to fail). A pattern such as (\1)(a) succeeds when this  option  is         tive  to  fail). A pattern such as (\1)(a) succeeds when this option is
1319         set  (assuming  it can find an "a" in the subject), whereas it fails by         set (assuming it can find an "a" in the subject), whereas it  fails  by
1320         default, for Perl compatibility.         default, for Perl compatibility.
1321    
1322           PCRE_MULTILINE           PCRE_MULTILINE
1323    
1324         By default, PCRE treats the subject string as consisting  of  a  single         By  default,  PCRE  treats the subject string as consisting of a single
1325         line  of characters (even if it actually contains newlines). The "start         line of characters (even if it actually contains newlines). The  "start
1326         of line" metacharacter (^) matches only at the  start  of  the  string,         of  line"  metacharacter  (^)  matches only at the start of the string,
1327         while  the  "end  of line" metacharacter ($) matches only at the end of         while the "end of line" metacharacter ($) matches only at  the  end  of
1328         the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY         the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY
1329         is set). This is the same as Perl.         is set). This is the same as Perl.
1330    
1331         When  PCRE_MULTILINE  it  is set, the "start of line" and "end of line"         When PCRE_MULTILINE it is set, the "start of line" and  "end  of  line"
1332         constructs match immediately following or immediately  before  internal         constructs  match  immediately following or immediately before internal
1333         newlines  in  the  subject string, respectively, as well as at the very         newlines in the subject string, respectively, as well as  at  the  very
1334         start and end. This is equivalent to Perl's /m option, and  it  can  be         start  and  end.  This is equivalent to Perl's /m option, and it can be
1335         changed within a pattern by a (?m) option setting. If there are no new-         changed within a pattern by a (?m) option setting. If there are no new-
1336         lines in a subject string, or no occurrences of ^ or $  in  a  pattern,         lines  in  a  subject string, or no occurrences of ^ or $ in a pattern,
1337         setting PCRE_MULTILINE has no effect.         setting PCRE_MULTILINE has no effect.
1338    
1339           PCRE_NEWLINE_CR           PCRE_NEWLINE_CR
# Line 1305  COMPILING A PATTERN Line 1342  COMPILING A PATTERN
1342           PCRE_NEWLINE_ANYCRLF           PCRE_NEWLINE_ANYCRLF
1343           PCRE_NEWLINE_ANY           PCRE_NEWLINE_ANY
1344    
1345         These  options  override the default newline definition that was chosen         These options override the default newline definition that  was  chosen
1346         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
1347         newline  is  indicated  by a single character (CR or LF, respectively).         newline is indicated by a single character (CR  or  LF,  respectively).
1348         Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by  the         Setting  PCRE_NEWLINE_CRLF specifies that a newline is indicated by the
1349         two-character  CRLF  sequence.  Setting  PCRE_NEWLINE_ANYCRLF specifies         two-character CRLF  sequence.  Setting  PCRE_NEWLINE_ANYCRLF  specifies
1350         that any of the three preceding sequences should be recognized. Setting         that any of the three preceding sequences should be recognized. Setting
1351         PCRE_NEWLINE_ANY  specifies that any Unicode newline sequence should be         PCRE_NEWLINE_ANY specifies that any Unicode newline sequence should  be
1352         recognized. The Unicode newline sequences are the three just mentioned,         recognized. The Unicode newline sequences are the three just mentioned,
1353         plus  the  single  characters  VT (vertical tab, U+000B), FF (formfeed,         plus the single characters VT (vertical  tab,  U+000B),  FF  (formfeed,
1354         U+000C), NEL (next line, U+0085), LS (line separator, U+2028),  and  PS         U+000C),  NEL  (next line, U+0085), LS (line separator, U+2028), and PS
1355         (paragraph  separator,  U+2029).  The  last  two are recognized only in         (paragraph separator, U+2029). The last  two  are  recognized  only  in
1356         UTF-8 mode.         UTF-8 mode.
1357    
1358         The newline setting in the  options  word  uses  three  bits  that  are         The  newline  setting  in  the  options  word  uses three bits that are
1359         treated as a number, giving eight possibilities. Currently only six are         treated as a number, giving eight possibilities. Currently only six are
1360         used (default plus the five values above). This means that if  you  set         used  (default  plus the five values above). This means that if you set
1361         more  than one newline option, the combination may or may not be sensi-         more than one newline option, the combination may or may not be  sensi-
1362         ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to         ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to
1363         PCRE_NEWLINE_CRLF,  but other combinations may yield unused numbers and         PCRE_NEWLINE_CRLF, but other combinations may yield unused numbers  and
1364         cause an error.         cause an error.
1365    
1366         The only time that a line break is specially recognized when  compiling         The  only time that a line break is specially recognized when compiling
1367         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
1368         character class is encountered. This indicates  a  comment  that  lasts         character  class  is  encountered.  This indicates a comment that lasts
1369         until  after the next line break sequence. In other circumstances, line         until after the next line break sequence. In other circumstances,  line
1370         break  sequences  are  treated  as  literal  data,   except   that   in         break   sequences   are   treated  as  literal  data,  except  that  in
1371         PCRE_EXTENDED mode, both CR and LF are treated as whitespace characters         PCRE_EXTENDED mode, both CR and LF are treated as whitespace characters
1372         and are therefore ignored.         and are therefore ignored.
1373    
1374         The newline option that is set at compile time becomes the default that         The newline option that is set at compile time becomes the default that
1375         is  used for pcre_exec() and pcre_dfa_exec(), but it can be overridden.         is used for pcre_exec() and pcre_dfa_exec(), but it can be overridden.
1376    
1377           PCRE_NO_AUTO_CAPTURE           PCRE_NO_AUTO_CAPTURE
1378    
# Line 1345  COMPILING A PATTERN Line 1382  COMPILING A PATTERN
1382         be  used  for  capturing  (and  they acquire numbers in the usual way).         be  used  for  capturing  (and  they acquire numbers in the usual way).
1383         There is no equivalent of this option in Perl.         There is no equivalent of this option in Perl.
1384    
1385             PCRE_UCP
1386    
1387           This option changes the way PCRE processes \b, \d, \s, \w, and some  of
1388           the POSIX character classes. By default, only ASCII characters are rec-
1389           ognized, but if PCRE_UCP is set, Unicode properties are used instead to
1390           classify  characters.  More details are given in the section on generic
1391           character types in the pcrepattern page. If you set PCRE_UCP,  matching
1392           one  of the items it affects takes much longer. The option is available
1393           only if PCRE has been compiled with Unicode property support.
1394    
1395           PCRE_UNGREEDY           PCRE_UNGREEDY
1396    
1397         This option inverts the "greediness" of the quantifiers  so  that  they         This option inverts the "greediness" of the quantifiers  so  that  they
# Line 1443  COMPILATION ERROR CODES Line 1490  COMPILATION ERROR CODES
1490           57  \g is not followed by a braced, angle-bracketed, or quoted           57  \g is not followed by a braced, angle-bracketed, or quoted
1491                 name/number or by a plain number                 name/number or by a plain number
1492           58  a numbered reference must not be zero           58  a numbered reference must not be zero
1493           59  (*VERB) with an argument is not supported           59  an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)
1494           60  (*VERB) not recognized           60  (*VERB) not recognized
1495           61  number is too big           61  number is too big
1496           62  subpattern name expected           62  subpattern name expected
1497           63  digit expected after (?+           63  digit expected after (?+
1498           64  ] is an invalid data character in JavaScript compatibility mode           64  ] is an invalid data character in JavaScript compatibility mode
1499             65   different  names  for  subpatterns  of  the  same number are not
1500           allowed
1501             66  (*MARK) must have an argument
1502             67  this version of PCRE is not compiled with PCRE_UCP support
1503    
1504         The  numbers  32  and 10000 in errors 48 and 49 are defaults; different         The numbers 32 and 10000 in errors 48 and 49  are  defaults;  different
1505         values may be used if the limits were changed when PCRE was built.         values may be used if the limits were changed when PCRE was built.
1506    
1507    
# Line 1459  STUDYING A PATTERN Line 1510  STUDYING A PATTERN
1510         pcre_extra *pcre_study(const pcre *code, int options         pcre_extra *pcre_study(const pcre *code, int options
1511              const char **errptr);              const char **errptr);
1512    
1513         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
1514         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
1515         matching. The function pcre_study() takes a pointer to a compiled  pat-         matching.  The function pcre_study() takes a pointer to a compiled pat-
1516         tern as its first argument. If studying the pattern produces additional         tern as its first argument. If studying the pattern produces additional
1517         information that will help speed up matching,  pcre_study()  returns  a         information  that  will  help speed up matching, pcre_study() returns a
1518         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
1519         the results of the study.         the results of the study.
1520    
1521         The  returned  value  from  pcre_study()  can  be  passed  directly  to         The  returned  value  from  pcre_study()  can  be  passed  directly  to
1522         pcre_exec().  However,  a  pcre_extra  block also contains other fields         pcre_exec() or pcre_dfa_exec(). However, a pcre_extra block  also  con-
1523         that can be set by the caller before the block  is  passed;  these  are         tains  other  fields  that can be set by the caller before the block is
1524         described below in the section on matching a pattern.         passed; these are described below in the section on matching a pattern.
1525    
1526         If  studying  the  pattern  does not produce any additional information         If studying the  pattern  does  not  produce  any  useful  information,
1527         pcre_study() returns NULL. In that circumstance, if the calling program         pcre_study() returns NULL. In that circumstance, if the calling program
1528         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()   or
1529         its own pcre_extra block.         pcre_dfa_exec(), it must set up its own pcre_extra block.
1530    
1531         The second argument of pcre_study() contains option bits.  At  present,         The  second  argument of pcre_study() contains option bits. At present,
1532         no options are defined, and this argument should always be zero.         no options are defined, and this argument should always be zero.
1533    
1534         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.
1535         If studying succeeds (even if no data is  returned),  the  variable  it         If  studying  succeeds  (even  if no data is returned), the variable it
1536         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
1537         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
1538         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
1539         after calling pcre_study(), to be sure that it has run successfully.         after calling pcre_study(), to be sure that it has run successfully.
1540    
1541         This is a typical call to pcre_study():         This is a typical call to pcre_study():
# Line 1495  STUDYING A PATTERN Line 1546  STUDYING A PATTERN
1546             0,              /* no options exist */             0,              /* no options exist */
1547             &error);        /* set to NULL or points to a message */             &error);        /* set to NULL or points to a message */
1548    
1549         At present, studying a pattern is useful only for non-anchored patterns         Studying a pattern does two things: first, a lower bound for the length
1550         that  do not have a single fixed starting character. A bitmap of possi-         of subject string that is needed to match the pattern is computed. This
1551         ble starting bytes is created.         does not mean that there are any strings of that length that match, but
1552           it does guarantee that no shorter strings match. The value is  used  by
1553           pcre_exec()  and  pcre_dfa_exec()  to  avoid  wasting time by trying to
1554           match strings that are shorter than the lower bound. You can  find  out
1555           the value in a calling program via the pcre_fullinfo() function.
1556    
1557           Studying a pattern is also useful for non-anchored patterns that do not
1558           have a single fixed starting character. A bitmap of  possible  starting
1559           bytes  is  created. This speeds up finding a position in the subject at
1560           which to start matching.
1561    
1562    
1563  LOCALE SUPPORT  LOCALE SUPPORT
# Line 1505  LOCALE SUPPORT Line 1565  LOCALE SUPPORT
1565         PCRE handles caseless matching, and determines whether  characters  are         PCRE handles caseless matching, and determines whether  characters  are
1566         letters,  digits, or whatever, by reference to a set of tables, indexed         letters,  digits, or whatever, by reference to a set of tables, indexed
1567         by character value. When running in UTF-8 mode, this  applies  only  to         by character value. When running in UTF-8 mode, this  applies  only  to
1568         characters  with  codes  less than 128. Higher-valued codes never match         characters  with  codes  less than 128. By default, higher-valued codes
1569         escapes such as \w or \d, but can be tested with \p if  PCRE  is  built         never match escapes such as \w or \d, but they can be tested with \p if
1570         with  Unicode  character property support. The use of locales with Uni-         PCRE  is  built with Unicode character property support. Alternatively,
1571         code is discouraged. If you are handling characters with codes  greater         the PCRE_UCP option can be set at compile  time;  this  causes  \w  and
1572         than  128, you should either use UTF-8 and Unicode, or use locales, but         friends to use Unicode property support instead of built-in tables. The
1573         not try to mix the two.         use of locales with Unicode is discouraged. If you are handling charac-
1574           ters  with codes greater than 128, you should either use UTF-8 and Uni-
1575           code, or use locales, but not try to mix the two.
1576    
1577         PCRE contains an internal set of tables that are used  when  the  final         PCRE contains an internal set of tables that are used  when  the  final
1578         argument  of  pcre_compile()  is  NULL.  These  are sufficient for many         argument  of  pcre_compile()  is  NULL.  These  are sufficient for many
# Line 1663  INFORMATION ABOUT A PATTERN Line 1725  INFORMATION ABOUT A PATTERN
1725         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
1726         /^a\dz\d/ the returned value is -1.         /^a\dz\d/ the returned value is -1.
1727    
1728             PCRE_INFO_MINLENGTH
1729    
1730           If the pattern was studied and a minimum length  for  matching  subject
1731           strings  was  computed,  its  value is returned. Otherwise the returned
1732           value is -1. The value is a number of characters, not bytes  (this  may
1733           be  relevant in UTF-8 mode). The fourth argument should point to an int
1734           variable. A non-negative value is a lower bound to the  length  of  any
1735           matching  string.  There  may not be any strings of that length that do
1736           actually match, but every string that does match is at least that long.
1737    
1738           PCRE_INFO_NAMECOUNT           PCRE_INFO_NAMECOUNT
1739           PCRE_INFO_NAMEENTRYSIZE           PCRE_INFO_NAMEENTRYSIZE
1740           PCRE_INFO_NAMETABLE           PCRE_INFO_NAMETABLE
# Line 1684  INFORMATION ABOUT A PATTERN Line 1756  INFORMATION ABOUT A PATTERN
1756         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
1757         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-
1758         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-
1759         sponding  name,  zero  terminated. The names are in alphabetical order.         sponding name, zero terminated.
1760         When PCRE_DUPNAMES is set, duplicate names are in order of their paren-  
1761         theses  numbers.  For  example,  consider the following pattern (assume         The  names are in alphabetical order. Duplicate names may appear if (?|
1762         PCRE_EXTENDED is  set,  so  white  space  -  including  newlines  -  is         is used to create multiple groups with the same number, as described in
1763         ignored):         the  section  on  duplicate subpattern numbers in the pcrepattern page.
1764           Duplicate names for subpatterns with different  numbers  are  permitted
1765           only  if  PCRE_DUPNAMES  is  set. In all cases of duplicate names, they
1766           appear in the table in the order in which they were found in  the  pat-
1767           tern.  In  the  absence  of (?| this is the order of increasing number;
1768           when (?| is used this is not necessarily the case because later subpat-
1769           terns may have lower numbers.
1770    
1771           As  a  simple  example of the name/number table, consider the following
1772           pattern (assume PCRE_EXTENDED is set, so white space -  including  new-
1773           lines - is ignored):
1774    
1775           (?<date> (?<year>(\d\d)?\d\d) -           (?<date> (?<year>(\d\d)?\d\d) -
1776           (?<month>\d\d) - (?<day>\d\d) )           (?<month>\d\d) - (?<day>\d\d) )
# Line 1709  INFORMATION ABOUT A PATTERN Line 1791  INFORMATION ABOUT A PATTERN
1791    
1792           PCRE_INFO_OKPARTIAL           PCRE_INFO_OKPARTIAL
1793    
1794         Return 1 if the pattern can be used for partial matching, otherwise  0.         Return 1  if  the  pattern  can  be  used  for  partial  matching  with
1795         The  fourth  argument  should point to an int variable. The pcrepartial         pcre_exec(),  otherwise  0.  The fourth argument should point to an int
1796         documentation lists the restrictions that apply to patterns  when  par-         variable. From  release  8.00,  this  always  returns  1,  because  the
1797         tial matching is used.         restrictions  that  previously  applied  to  partial matching have been
1798           lifted. The pcrepartial documentation gives details of  partial  match-
1799           ing.
1800    
1801           PCRE_INFO_OPTIONS           PCRE_INFO_OPTIONS
1802    
# Line 1749  INFORMATION ABOUT A PATTERN Line 1833  INFORMATION ABOUT A PATTERN
1833         Return the size of the data block pointed to by the study_data field in         Return the size of the data block pointed to by the study_data field in
1834         a pcre_extra block. That is,  it  is  the  value  that  was  passed  to         a pcre_extra block. That is,  it  is  the  value  that  was  passed  to
1835         pcre_malloc() when PCRE was getting memory into which to place the data         pcre_malloc() when PCRE was getting memory into which to place the data
1836         created by pcre_study(). The fourth argument should point to  a  size_t         created by pcre_study(). If pcre_extra is NULL, or there  is  no  study
1837           data,  zero  is  returned. The fourth argument should point to a size_t
1838         variable.         variable.
1839    
1840    
# Line 1757  OBSOLETE INFO FUNCTION Line 1842  OBSOLETE INFO FUNCTION
1842    
1843         int pcre_info(const pcre *code, int *optptr, int *firstcharptr);         int pcre_info(const pcre *code, int *optptr, int *firstcharptr);
1844    
1845         The  pcre_info()  function is now obsolete because its interface is too         The pcre_info() function is now obsolete because its interface  is  too
1846         restrictive to return all the available data about a compiled  pattern.         restrictive  to return all the available data about a compiled pattern.
1847         New   programs   should  use  pcre_fullinfo()  instead.  The  yield  of         New  programs  should  use  pcre_fullinfo()  instead.  The   yield   of
1848         pcre_info() is the number of capturing subpatterns, or one of the  fol-         pcre_info()  is the number of capturing subpatterns, or one of the fol-
1849         lowing negative numbers:         lowing negative numbers:
1850    
1851           PCRE_ERROR_NULL       the argument code was NULL           PCRE_ERROR_NULL       the argument code was NULL
1852           PCRE_ERROR_BADMAGIC   the "magic number" was not found           PCRE_ERROR_BADMAGIC   the "magic number" was not found
1853    
1854         If  the  optptr  argument is not NULL, a copy of the options with which         If the optptr argument is not NULL, a copy of the  options  with  which
1855         the pattern was compiled is placed in the integer  it  points  to  (see         the  pattern  was  compiled  is placed in the integer it points to (see
1856         PCRE_INFO_OPTIONS above).         PCRE_INFO_OPTIONS above).
1857    
1858         If  the  pattern  is  not anchored and the firstcharptr argument is not         If the pattern is not anchored and the  firstcharptr  argument  is  not
1859         NULL, it is used to pass back information about the first character  of         NULL,  it is used to pass back information about the first character of
1860         any matched string (see PCRE_INFO_FIRSTBYTE above).         any matched string (see PCRE_INFO_FIRSTBYTE above).
1861    
1862    
# Line 1779  REFERENCE COUNTS Line 1864  REFERENCE COUNTS
1864    
1865         int pcre_refcount(pcre *code, int adjust);         int pcre_refcount(pcre *code, int adjust);
1866    
1867         The  pcre_refcount()  function is used to maintain a reference count in         The pcre_refcount() function is used to maintain a reference  count  in
1868         the data block that contains a compiled pattern. It is provided for the         the data block that contains a compiled pattern. It is provided for the
1869         benefit  of  applications  that  operate  in an object-oriented manner,         benefit of applications that  operate  in  an  object-oriented  manner,
1870         where different parts of the application may be using the same compiled         where different parts of the application may be using the same compiled
1871         pattern, but you want to free the block when they are all done.         pattern, but you want to free the block when they are all done.
1872    
1873         When a pattern is compiled, the reference count field is initialized to         When a pattern is compiled, the reference count field is initialized to
1874         zero.  It is changed only by calling this function, whose action is  to         zero.   It is changed only by calling this function, whose action is to
1875         add  the  adjust  value  (which may be positive or negative) to it. The         add the adjust value (which may be positive or  negative)  to  it.  The
1876         yield of the function is the new value. However, the value of the count         yield of the function is the new value. However, the value of the count
1877         is  constrained to lie between 0 and 65535, inclusive. If the new value         is constrained to lie between 0 and 65535, inclusive. If the new  value
1878         is outside these limits, it is forced to the appropriate limit value.         is outside these limits, it is forced to the appropriate limit value.
1879    
1880         Except when it is zero, the reference count is not correctly  preserved         Except  when it is zero, the reference count is not correctly preserved
1881         if  a  pattern  is  compiled on one host and then transferred to a host         if a pattern is compiled on one host and then  transferred  to  a  host
1882         whose byte-order is different. (This seems a highly unlikely scenario.)         whose byte-order is different. (This seems a highly unlikely scenario.)
1883    
1884    
# Line 1805  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1890  MATCHING A PATTERN: THE TRADITIONAL FUNC
1890    
1891         The  function pcre_exec() is called to match a subject string against a         The  function pcre_exec() is called to match a subject string against a
1892         compiled pattern, which is passed in the code argument. If the  pattern         compiled pattern, which is passed in the code argument. If the  pattern
1893         has been studied, the result of the study should be passed in the extra         was  studied,  the  result  of  the study should be passed in the extra
1894         argument. This function is the main matching facility of  the  library,         argument. This function is the main matching facility of  the  library,
1895         and it operates in a Perl-like manner. For specialist use there is also         and it operates in a Perl-like manner. For specialist use there is also
1896         an alternative matching function, which is described below in the  sec-         an alternative matching function, which is described below in the  sec-
# Line 1845  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1930  MATCHING A PATTERN: THE TRADITIONAL FUNC
1930           unsigned long int match_limit_recursion;           unsigned long int match_limit_recursion;
1931           void *callout_data;           void *callout_data;
1932           const unsigned char *tables;           const unsigned char *tables;
1933             unsigned char **mark;
1934    
1935         The  flags  field  is a bitmap that specifies which of the other fields         The  flags  field  is a bitmap that specifies which of the other fields
1936         are set. The flag bits are:         are set. The flag bits are:
# Line 1854  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1940  MATCHING A PATTERN: THE TRADITIONAL FUNC
1940           PCRE_EXTRA_MATCH_LIMIT_RECURSION           PCRE_EXTRA_MATCH_LIMIT_RECURSION
1941           PCRE_EXTRA_CALLOUT_DATA           PCRE_EXTRA_CALLOUT_DATA
1942           PCRE_EXTRA_TABLES           PCRE_EXTRA_TABLES
1943             PCRE_EXTRA_MARK
1944    
1945         Other flag bits should be set to zero. The study_data field is  set  in         Other flag bits should be set to zero. The study_data field is  set  in
1946         the  pcre_extra  block  that is returned by pcre_study(), together with         the  pcre_extra  block  that is returned by pcre_study(), together with
# Line 1864  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1951  MATCHING A PATTERN: THE TRADITIONAL FUNC
1951         The match_limit field provides a means of preventing PCRE from using up         The match_limit field provides a means of preventing PCRE from using up
1952         a  vast amount of resources when running patterns that are not going to         a  vast amount of resources when running patterns that are not going to
1953         match, but which have a very large number  of  possibilities  in  their         match, but which have a very large number  of  possibilities  in  their
1954         search  trees.  The  classic  example  is  the  use of nested unlimited         search  trees. The classic example is a pattern that uses nested unlim-
1955         repeats.         ited repeats.
1956    
1957         Internally, PCRE uses a function called match() which it calls  repeat-         Internally, PCRE uses a function called match() which it calls  repeat-
1958         edly  (sometimes  recursively). The limit set by match_limit is imposed         edly  (sometimes  recursively). The limit set by match_limit is imposed
# Line 1887  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1974  MATCHING A PATTERN: THE TRADITIONAL FUNC
1974         the  total number of calls, because not all calls to match() are recur-         the  total number of calls, because not all calls to match() are recur-
1975         sive.  This limit is of use only if it is set smaller than match_limit.         sive.  This limit is of use only if it is set smaller than match_limit.
1976    
1977         Limiting  the  recursion  depth  limits the amount of stack that can be         Limiting the recursion depth limits the amount of  stack  that  can  be
1978         used, or, when PCRE has been compiled to use memory on the heap instead         used, or, when PCRE has been compiled to use memory on the heap instead
1979         of the stack, the amount of heap memory that can be used.         of the stack, the amount of heap memory that can be used.
1980    
1981         The  default  value  for  match_limit_recursion can be set when PCRE is         The default value for match_limit_recursion can be  set  when  PCRE  is
1982         built; the default default  is  the  same  value  as  the  default  for         built;  the  default  default  is  the  same  value  as the default for
1983         match_limit.  You can override the default by suppling pcre_exec() with         match_limit. You can override the default by suppling pcre_exec()  with
1984         a  pcre_extra  block  in  which  match_limit_recursion  is   set,   and         a   pcre_extra   block  in  which  match_limit_recursion  is  set,  and
1985         PCRE_EXTRA_MATCH_LIMIT_RECURSION  is  set  in  the  flags field. If the         PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in  the  flags  field.  If  the
1986         limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT.         limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT.
1987    
1988         The pcre_callout field is used in conjunction with the  "callout"  fea-         The  callout_data  field is used in conjunction with the "callout" fea-
1989         ture, which is described in the pcrecallout documentation.         ture, and is described in the pcrecallout documentation.
1990    
1991         The  tables  field  is  used  to  pass  a  character  tables pointer to         The tables field  is  used  to  pass  a  character  tables  pointer  to
1992         pcre_exec(); this overrides the value that is stored with the  compiled         pcre_exec();  this overrides the value that is stored with the compiled
1993         pattern.  A  non-NULL value is stored with the compiled pattern only if         pattern. A non-NULL value is stored with the compiled pattern  only  if
1994         custom tables were supplied to pcre_compile() via  its  tableptr  argu-         custom  tables  were  supplied to pcre_compile() via its tableptr argu-
1995         ment.  If NULL is passed to pcre_exec() using this mechanism, it forces         ment.  If NULL is passed to pcre_exec() using this mechanism, it forces
1996         PCRE's internal tables to be used. This facility is  helpful  when  re-         PCRE's  internal  tables  to be used. This facility is helpful when re-
1997         using  patterns  that  have been saved after compiling with an external         using patterns that have been saved after compiling  with  an  external
1998         set of tables, because the external tables  might  be  at  a  different         set  of  tables,  because  the  external tables might be at a different
1999         address  when  pcre_exec() is called. See the pcreprecompile documenta-         address when pcre_exec() is called. See the  pcreprecompile  documenta-
2000         tion for a discussion of saving compiled patterns for later use.         tion for a discussion of saving compiled patterns for later use.
2001    
2002           If  PCRE_EXTRA_MARK  is  set in the flags field, the mark field must be
2003           set to point to a char * variable. If the pattern  contains  any  back-
2004           tracking  control verbs such as (*MARK:NAME), and the execution ends up
2005           with a name to pass back, a pointer to the  name  string  (zero  termi-
2006           nated)  is  placed  in  the  variable pointed to by the mark field. The
2007           names are within the compiled pattern; if you wish  to  retain  such  a
2008           name  you must copy it before freeing the memory of a compiled pattern.
2009           If there is no name to pass back, the variable pointed to by  the  mark
2010           field  set  to NULL. For details of the backtracking control verbs, see
2011           the section entitled "Backtracking control" in the pcrepattern documen-
2012           tation.
2013    
2014     Option bits for pcre_exec()     Option bits for pcre_exec()
2015    
2016         The unused bits of the options argument for pcre_exec() must  be  zero.         The  unused  bits of the options argument for pcre_exec() must be zero.
2017         The  only  bits  that  may  be set are PCRE_ANCHORED, PCRE_NEWLINE_xxx,         The only bits that may  be  set  are  PCRE_ANCHORED,  PCRE_NEWLINE_xxx,
2018         PCRE_NOTBOL,   PCRE_NOTEOL,   PCRE_NOTEMPTY,   PCRE_NO_UTF8_CHECK   and         PCRE_NOTBOL,    PCRE_NOTEOL,    PCRE_NOTEMPTY,   PCRE_NOTEMPTY_ATSTART,
2019         PCRE_PARTIAL.         PCRE_NO_START_OPTIMIZE,  PCRE_NO_UTF8_CHECK,   PCRE_PARTIAL_SOFT,   and
2020           PCRE_PARTIAL_HARD.
2021    
2022           PCRE_ANCHORED           PCRE_ANCHORED
2023    
# Line 1997  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2097  MATCHING A PATTERN: THE TRADITIONAL FUNC
2097    
2098           a?b?           a?b?
2099    
2100         is applied to a string not beginning with "a" or "b",  it  matches  the         is applied to a string not beginning with "a" or  "b",  it  matches  an
2101         empty  string at the start of the subject. With PCRE_NOTEMPTY set, this         empty  string at the start of the subject. With PCRE_NOTEMPTY set, this
2102         match is not valid, so PCRE searches further into the string for occur-         match is not valid, so PCRE searches further into the string for occur-
2103         rences of "a" or "b".         rences of "a" or "b".
2104    
2105         Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a spe-           PCRE_NOTEMPTY_ATSTART
2106         cial case of a pattern match of the empty  string  within  its  split()  
2107         function,  and  when  using  the /g modifier. It is possible to emulate         This  is  like PCRE_NOTEMPTY, except that an empty string match that is
2108         Perl's behaviour after matching a null string by first trying the match         not at the start of  the  subject  is  permitted.  If  the  pattern  is
2109         again at the same offset with PCRE_NOTEMPTY and PCRE_ANCHORED, and then         anchored, such a match can occur only if the pattern contains \K.
2110         if that fails by advancing the starting offset (see below)  and  trying  
2111         an ordinary match again. There is some code that demonstrates how to do         Perl     has    no    direct    equivalent    of    PCRE_NOTEMPTY    or
2112         this in the pcredemo.c sample program.         PCRE_NOTEMPTY_ATSTART, but it does make a special  case  of  a  pattern
2113           match  of  the empty string within its split() function, and when using
2114           the /g modifier. It is  possible  to  emulate  Perl's  behaviour  after
2115           matching a null string by first trying the match again at the same off-
2116           set with PCRE_NOTEMPTY_ATSTART and  PCRE_ANCHORED,  and  then  if  that
2117           fails, by advancing the starting offset (see below) and trying an ordi-
2118           nary match again. There is some code that demonstrates how to  do  this
2119           in the pcredemo sample program.
2120    
2121             PCRE_NO_START_OPTIMIZE
2122    
2123           There  are a number of optimizations that pcre_exec() uses at the start
2124           of a match, in order to speed up the process. For  example,  if  it  is
2125           known  that  a  match must start with a specific character, it searches
2126           the subject for that character, and fails immediately if it cannot find
2127           it,  without actually running the main matching function. When callouts
2128           are in use, these optimizations can cause  them  to  be  skipped.  This
2129           option  disables  the  "start-up" optimizations, causing performance to
2130           suffer, but ensuring that the callouts do occur.
2131    
2132           PCRE_NO_UTF8_CHECK           PCRE_NO_UTF8_CHECK
2133    
# Line 2033  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2151  MATCHING A PATTERN: THE TRADITIONAL FUNC
2151         value  of startoffset that does not point to the start of a UTF-8 char-         value  of startoffset that does not point to the start of a UTF-8 char-
2152         acter, is undefined. Your program may crash.         acter, is undefined. Your program may crash.
2153    
2154           PCRE_PARTIAL           PCRE_PARTIAL_HARD
2155             PCRE_PARTIAL_SOFT
2156    
2157         This option turns on the  partial  matching  feature.  If  the  subject         These options turn on the partial matching feature. For backwards  com-
2158         string  fails to match the pattern, but at some point during the match-         patibility,  PCRE_PARTIAL is a synonym for PCRE_PARTIAL_SOFT. A partial
2159         ing process the end of the subject was reached (that  is,  the  subject         match occurs if the end of the subject string is reached  successfully,
2160         partially  matches  the  pattern and the failure to match occurred only         but  there  are not enough subject characters to complete the match. If
2161         because there were not enough subject characters), pcre_exec()  returns         this happens when PCRE_PARTIAL_HARD  is  set,  pcre_exec()  immediately
2162         PCRE_ERROR_PARTIAL  instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is         returns  PCRE_ERROR_PARTIAL.  Otherwise,  if  PCRE_PARTIAL_SOFT is set,
2163         used, there are restrictions on what may appear in the  pattern.  These         matching continues by testing any other alternatives. Only if they  all
2164         are discussed in the pcrepartial documentation.         fail  is  PCRE_ERROR_PARTIAL  returned (instead of PCRE_ERROR_NOMATCH).
2165           The portion of the string that was inspected when the partial match was
2166           found  is  set  as  the first matching string. There is a more detailed
2167           discussion in the pcrepartial documentation.
2168    
2169     The string to be matched by pcre_exec()     The string to be matched by pcre_exec()
2170    
2171         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
2172         length in length, and a starting byte offset in startoffset.  In  UTF-8         length (in bytes) in length, and a starting byte offset in startoffset.
2173         mode,  the  byte  offset  must point to the start of a UTF-8 character.         In UTF-8 mode, the byte offset must point to the start of a UTF-8 char-
2174         Unlike the pattern string, the subject may contain binary  zero  bytes.         acter.  Unlike  the pattern string, the subject may contain binary zero
2175         When  the starting offset is zero, the search for a match starts at the         bytes. When the starting offset is zero, the search for a match  starts
2176         beginning of the subject, and this is by far the most common case.         at  the  beginning  of  the subject, and this is by far the most common
2177           case.
2178    
2179         A non-zero starting offset is useful when searching for  another  match         A non-zero starting offset is useful when searching for  another  match
2180         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-
# Line 2087  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2210  MATCHING A PATTERN: THE TRADITIONAL FUNC
2210         string.  PCRE  supports several other kinds of parenthesized subpattern         string.  PCRE  supports several other kinds of parenthesized subpattern
2211         that do not cause substrings to be captured.         that do not cause substrings to be captured.
2212    
2213         Captured substrings are returned to the caller via a vector of  integer         Captured substrings are returned to the caller via a vector of integers
2214         offsets  whose  address is passed in ovector. The number of elements in         whose  address is passed in ovector. The number of elements in the vec-
2215         the vector is passed in ovecsize, which must be a non-negative  number.         tor is passed in ovecsize, which must be a non-negative  number.  Note:
2216         Note: this argument is NOT the size of ovector in bytes.         this argument is NOT the size of ovector in bytes.
2217    
2218         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-
2219         strings, each substring using a pair of integers. The  remaining  third         strings, each substring using a pair of integers. The  remaining  third
2220         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-
2221         turing subpatterns, and is not available for passing back  information.         turing subpatterns, and is not available for passing back  information.
2222         The  length passed in ovecsize should always be a multiple of three. If         The  number passed in ovecsize should always be a multiple of three. If
2223         it is not, it is rounded down.         it is not, it is rounded down.
2224    
2225         When a match is successful, information about  captured  substrings  is         When a match is successful, information about  captured  substrings  is
2226         returned  in  pairs  of integers, starting at the beginning of ovector,         returned  in  pairs  of integers, starting at the beginning of ovector,
2227         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
2228         element of a pair is set to the offset of the first character in a sub-         element  of  each pair is set to the byte offset of the first character
2229         string, and the second is set to the  offset  of  the  first  character         in a substring, and the second is set to the byte offset of  the  first
2230         after  the  end  of  a  substring. The first pair, ovector[0] and ovec-         character  after  the end of a substring. Note: these values are always
2231         tor[1], identify the portion of  the  subject  string  matched  by  the         byte offsets, even in UTF-8 mode. They are not character counts.
2232         entire  pattern.  The next pair is used for the first capturing subpat-  
2233         tern, and so on. The value returned by pcre_exec() is one more than the         The first pair of integers, ovector[0]  and  ovector[1],  identify  the
2234         highest numbered pair that has been set. For example, if two substrings         portion  of  the subject string matched by the entire pattern. The next
2235         have been captured, the returned value is 3. If there are no  capturing         pair is used for the first capturing subpattern, and so on.  The  value
2236         subpatterns,  the return value from a successful match is 1, indicating         returned by pcre_exec() is one more than the highest numbered pair that
2237         that just the first pair of offsets has been set.         has been set.  For example, if two substrings have been  captured,  the
2238           returned  value is 3. If there are no capturing subpatterns, the return
2239           value from a successful match is 1, indicating that just the first pair
2240           of offsets has been set.
2241    
2242         If a capturing subpattern is matched repeatedly, it is the last portion         If a capturing subpattern is matched repeatedly, it is the last portion
2243         of the string that it matched that is returned.         of the string that it matched that is returned.
2244    
2245         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,
2246         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
2247         function  returns a value of zero. In particular, if the substring off-         function returns a value of zero. If the substring offsets are  not  of
2248         sets are not of interest, pcre_exec() may be called with ovector passed         interest,  pcre_exec()  may  be  called with ovector passed as NULL and
2249         as  NULL  and  ovecsize  as zero. However, if the pattern contains back         ovecsize as zero. However, if the pattern contains back references  and
2250         references and the ovector is not big enough to  remember  the  related         the  ovector is not big enough to remember the related substrings, PCRE
2251         substrings,  PCRE has to get additional memory for use during matching.         has to get additional memory for use during matching. Thus it  is  usu-
2252         Thus it is usually advisable to supply an ovector.         ally advisable to supply an ovector.
2253    
2254         The pcre_info() function can be used to find  out  how  many  capturing         The pcre_fullinfo() function can be used to find out how many capturing
2255         subpatterns  there  are  in  a  compiled pattern. The smallest size for         subpatterns there are in a compiled  pattern.  The  smallest  size  for
2256         ovector that will allow for n captured substrings, in addition  to  the         ovector  that  will allow for n captured substrings, in addition to the
2257         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.
2258    
2259         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
2260         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,
2261         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
2262         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
2263         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-
2264         sponding to unused subpatterns are set to -1.         sponding to unused subpatterns are set to -1.
2265    
2266         Offset values that correspond to unused subpatterns at the end  of  the         Offset  values  that correspond to unused subpatterns at the end of the
2267         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
2268         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
2269         matched.  The  return  from the function is 2, because the highest used         matched. The return from the function is 2, because  the  highest  used
2270         capturing subpattern number is 1. However, you can refer to the offsets         capturing subpattern number is 1. However, you can refer to the offsets
2271         for  the  second  and third capturing subpatterns if you wish (assuming         for the second and third capturing subpatterns if  you  wish  (assuming
2272         the vector is large enough, of course).         the vector is large enough, of course).
2273    
2274         Some convenience functions are provided  for  extracting  the  captured         Some  convenience  functions  are  provided for extracting the captured
2275         substrings as separate strings. These are described below.         substrings as separate strings. These are described below.
2276    
2277     Error return values from pcre_exec()     Error return values from pcre_exec()
2278    
2279         If  pcre_exec()  fails, it returns a negative number. The following are         If pcre_exec() fails, it returns a negative number. The  following  are
2280         defined in the header file:         defined in the header file:
2281    
2282           PCRE_ERROR_NOMATCH        (-1)           PCRE_ERROR_NOMATCH        (-1)
# Line 2159  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2285  MATCHING A PATTERN: THE TRADITIONAL FUNC
2285    
2286           PCRE_ERROR_NULL           (-2)           PCRE_ERROR_NULL           (-2)
2287    
2288         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
2289         ovecsize was not zero.         ovecsize was not zero.
2290    
2291           PCRE_ERROR_BADOPTION      (-3)           PCRE_ERROR_BADOPTION      (-3)
# Line 2168  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2294  MATCHING A PATTERN: THE TRADITIONAL FUNC
2294    
2295           PCRE_ERROR_BADMAGIC       (-4)           PCRE_ERROR_BADMAGIC       (-4)
2296    
2297         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,
2298         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
2299         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
2300         an environment with the other endianness. This is the error  that  PCRE         an  environment  with the other endianness. This is the error that PCRE
2301         gives when the magic number is not present.         gives when the magic number is not present.
2302    
2303           PCRE_ERROR_UNKNOWN_OPCODE (-5)           PCRE_ERROR_UNKNOWN_OPCODE (-5)
2304    
2305         While running the pattern match, an unknown item was encountered in the         While running the pattern match, an unknown item was encountered in the
2306         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
2307         overwriting of the compiled pattern.         overwriting of the compiled pattern.
2308    
2309           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2310    
2311         If  a  pattern contains back references, but the ovector that is passed         If a pattern contains back references, but the ovector that  is  passed
2312         to pcre_exec() is not big enough to remember the referenced substrings,         to pcre_exec() is not big enough to remember the referenced substrings,
2313         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
2314         purpose. If the call via pcre_malloc() fails, this error is given.  The         purpose.  If the call via pcre_malloc() fails, this error is given. The
2315         memory is automatically freed at the end of matching.         memory is automatically freed at the end of matching.
2316    
2317           This error is also given if pcre_stack_malloc() fails  in  pcre_exec().
2318           This  can happen only when PCRE has been compiled with --disable-stack-
2319           for-recursion.
2320    
2321           PCRE_ERROR_NOSUBSTRING    (-7)           PCRE_ERROR_NOSUBSTRING    (-7)
2322    
2323         This  error is used by the pcre_copy_substring(), pcre_get_substring(),         This error is used by the pcre_copy_substring(),  pcre_get_substring(),
2324         and  pcre_get_substring_list()  functions  (see  below).  It  is  never         and  pcre_get_substring_list()  functions  (see  below).  It  is  never
2325         returned by pcre_exec().         returned by pcre_exec().
2326    
2327           PCRE_ERROR_MATCHLIMIT     (-8)           PCRE_ERROR_MATCHLIMIT     (-8)
2328    
2329         The  backtracking  limit,  as  specified  by the match_limit field in a         The backtracking limit, as specified by  the  match_limit  field  in  a
2330         pcre_extra structure (or defaulted) was reached.  See  the  description         pcre_extra  structure  (or  defaulted) was reached. See the description
2331         above.         above.
2332    
2333           PCRE_ERROR_CALLOUT        (-9)           PCRE_ERROR_CALLOUT        (-9)
2334    
2335         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
2336         use by callout functions that want to yield a distinctive  error  code.         use  by  callout functions that want to yield a distinctive error code.
2337         See the pcrecallout documentation for details.         See the pcrecallout documentation for details.
2338    
2339           PCRE_ERROR_BADUTF8        (-10)           PCRE_ERROR_BADUTF8        (-10)
2340    
2341         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
2342         subject.         subject.
2343    
2344           PCRE_ERROR_BADUTF8_OFFSET (-11)           PCRE_ERROR_BADUTF8_OFFSET (-11)
2345    
2346         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
2347         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-
2348         ter.         ter.
2349    
2350           PCRE_ERROR_PARTIAL        (-12)           PCRE_ERROR_PARTIAL        (-12)
2351    
2352         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
2353         pcrepartial documentation for details of partial matching.         pcrepartial documentation for details of partial matching.
2354    
2355           PCRE_ERROR_BADPARTIAL     (-13)           PCRE_ERROR_BADPARTIAL     (-13)
2356    
2357         The  PCRE_PARTIAL  option  was  used with a compiled pattern containing         This code is no longer in  use.  It  was  formerly  returned  when  the
2358         items that are not supported for partial matching. See the  pcrepartial         PCRE_PARTIAL  option  was used with a compiled pattern containing items
2359         documentation for details of partial matching.         that were  not  supported  for  partial  matching.  From  release  8.00
2360           onwards, there are no restrictions on partial matching.
2361    
2362           PCRE_ERROR_INTERNAL       (-14)           PCRE_ERROR_INTERNAL       (-14)
2363    
# Line 2235  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2366  MATCHING A PATTERN: THE TRADITIONAL FUNC
2366    
2367           PCRE_ERROR_BADCOUNT       (-15)           PCRE_ERROR_BADCOUNT       (-15)
2368    
2369         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.
2370    
2371           PCRE_ERROR_RECURSIONLIMIT (-21)           PCRE_ERROR_RECURSIONLIMIT (-21)
2372    
2373         The internal recursion limit, as specified by the match_limit_recursion         The internal recursion limit, as specified by the match_limit_recursion
2374         field in a pcre_extra structure (or defaulted)  was  reached.  See  the         field  in  a  pcre_extra  structure (or defaulted) was reached. See the
2375         description above.         description above.
2376    
2377           PCRE_ERROR_BADNEWLINE     (-23)           PCRE_ERROR_BADNEWLINE     (-23)
# Line 2263  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER Line 2394  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER
2394         int pcre_get_substring_list(const char *subject,         int pcre_get_substring_list(const char *subject,
2395              int *ovector, int stringcount, const char ***listptr);              int *ovector, int stringcount, const char ***listptr);
2396    
2397         Captured  substrings  can  be  accessed  directly  by using the offsets         Captured substrings can be  accessed  directly  by  using  the  offsets
2398         returned by pcre_exec() in  ovector.  For  convenience,  the  functions         returned  by  pcre_exec()  in  ovector.  For convenience, the functions
2399         pcre_copy_substring(),    pcre_get_substring(),    and    pcre_get_sub-         pcre_copy_substring(),    pcre_get_substring(),    and    pcre_get_sub-
2400         string_list() are provided for extracting captured substrings  as  new,         string_list()  are  provided for extracting captured substrings as new,
2401         separate,  zero-terminated strings. These functions identify substrings         separate, zero-terminated strings. These functions identify  substrings
2402         by number. The next section describes functions  for  extracting  named         by  number.  The  next section describes functions for extracting named
2403         substrings.         substrings.
2404    
2405         A  substring that contains a binary zero is correctly extracted and has         A substring that contains a binary zero is correctly extracted and  has
2406         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
2407         string.   However,  you  can  process such a string by referring to the         string.  However, you can process such a string  by  referring  to  the
2408         length that is  returned  by  pcre_copy_substring()  and  pcre_get_sub-         length  that  is  returned  by  pcre_copy_substring() and pcre_get_sub-
2409         string().  Unfortunately, the interface to pcre_get_substring_list() is         string().  Unfortunately, the interface to pcre_get_substring_list() is
2410         not adequate for handling strings containing binary zeros, because  the         not  adequate for handling strings containing binary zeros, because the
2411         end of the final string is not independently indicated.         end of the final string is not independently indicated.
2412    
2413         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-
2414         tions: subject is the subject string that has  just  been  successfully         tions:  subject  is  the subject string that has just been successfully
2415         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
2416         passed to pcre_exec(), and stringcount is the number of substrings that         passed to pcre_exec(), and stringcount is the number of substrings that
2417         were  captured  by  the match, including the substring that matched the         were captured by the match, including the substring  that  matched  the
2418         entire regular expression. This is the value returned by pcre_exec() if         entire regular expression. This is the value returned by pcre_exec() if
2419         it  is greater than zero. If pcre_exec() returned zero, indicating that         it is greater than zero. If pcre_exec() returned zero, indicating  that
2420         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
2421         be the number of elements in the vector divided by three.         be the number of elements in the vector divided by three.
2422    
2423         The  functions pcre_copy_substring() and pcre_get_substring() extract a         The functions pcre_copy_substring() and pcre_get_substring() extract  a
2424         single substring, whose number is given as  stringnumber.  A  value  of         single  substring,  whose  number  is given as stringnumber. A value of
2425         zero  extracts  the  substring that matched the entire pattern, whereas         zero extracts the substring that matched the  entire  pattern,  whereas
2426         higher values  extract  the  captured  substrings.  For  pcre_copy_sub-         higher  values  extract  the  captured  substrings.  For pcre_copy_sub-
2427         string(),  the  string  is  placed  in buffer, whose length is given by         string(), the string is placed in buffer,  whose  length  is  given  by
2428         buffersize, while for pcre_get_substring() a new  block  of  memory  is         buffersize,  while  for  pcre_get_substring()  a new block of memory is
2429         obtained  via  pcre_malloc,  and its address is returned via stringptr.         obtained via pcre_malloc, and its address is  returned  via  stringptr.
2430         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
2431         the terminating zero, or one of these error codes:         the terminating zero, or one of these error codes:
2432    
2433           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2434    
2435         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
2436         get memory failed for pcre_get_substring().         get memory failed for pcre_get_substring().
2437    
2438           PCRE_ERROR_NOSUBSTRING    (-7)           PCRE_ERROR_NOSUBSTRING    (-7)
2439    
2440         There is no substring whose number is stringnumber.         There is no substring whose number is stringnumber.
2441    
2442         The pcre_get_substring_list()  function  extracts  all  available  sub-         The  pcre_get_substring_list()  function  extracts  all  available sub-
2443         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
2444         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
2445         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
2446         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
2447         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
2448         error code         error code
2449    
2450           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2451    
2452         if the attempt to get the memory block failed.         if the attempt to get the memory block failed.
2453    
2454         When any of these functions encounter a substring that is unset,  which         When  any of these functions encounter a substring that is unset, which
2455         can  happen  when  capturing subpattern number n+1 matches some part of         can happen when capturing subpattern number n+1 matches  some  part  of
2456         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
2457         empty string. This can be distinguished from a genuine zero-length sub-         empty string. This can be distinguished from a genuine zero-length sub-
2458         string by inspecting the appropriate offset in ovector, which is  nega-         string  by inspecting the appropriate offset in ovector, which is nega-
2459         tive for unset substrings.         tive for unset substrings.
2460    
2461         The  two convenience functions pcre_free_substring() and pcre_free_sub-         The two convenience functions pcre_free_substring() and  pcre_free_sub-
2462         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
2463         call  of  pcre_get_substring()  or  pcre_get_substring_list(),  respec-         call  of  pcre_get_substring()  or  pcre_get_substring_list(),  respec-
2464         tively. They do nothing more than  call  the  function  pointed  to  by         tively.  They  do  nothing  more  than  call the function pointed to by
2465         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.
2466         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-
2467         cial   interface  to  another  programming  language  that  cannot  use         cial  interface  to  another  programming  language  that  cannot   use
2468         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-
2469         vided.         vided.
2470    
2471    
# Line 2353  EXTRACTING CAPTURED SUBSTRINGS BY NAME Line 2484  EXTRACTING CAPTURED SUBSTRINGS BY NAME
2484              int stringcount, const char *stringname,              int stringcount, const char *stringname,
2485              const char **stringptr);              const char **stringptr);
2486    
2487         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-
2488         ber.  For example, for this pattern         ber.  For example, for this pattern
2489    
2490           (a+)b(?<xxx>\d+)...           (a+)b(?<xxx>\d+)...
# Line 2362  EXTRACTING CAPTURED SUBSTRINGS BY NAME Line 2493  EXTRACTING CAPTURED SUBSTRINGS BY NAME
2493         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
2494         name by calling pcre_get_stringnumber(). The first argument is the com-         name by calling pcre_get_stringnumber(). The first argument is the com-
2495         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
2496         the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if  there  is  no         the  subpattern  number,  or PCRE_ERROR_NOSUBSTRING (-7) if there is no
2497         subpattern of that name.         subpattern of that name.
2498    
2499         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
2500         the functions described in the previous section. For convenience, there         the functions described in the previous section. For convenience, there
2501         are also two functions that do the whole job.         are also two functions that do the whole job.
2502    
2503         Most    of    the    arguments   of   pcre_copy_named_substring()   and         Most   of   the   arguments    of    pcre_copy_named_substring()    and
2504         pcre_get_named_substring() are the same  as  those  for  the  similarly         pcre_get_named_substring()  are  the  same  as  those for the similarly
2505         named  functions  that extract by number. As these are described in the         named functions that extract by number. As these are described  in  the
2506         previous section, they are not re-described here. There  are  just  two         previous  section,  they  are not re-described here. There are just two
2507         differences:         differences:
2508    
2509         First,  instead  of a substring number, a substring name is given. Sec-         First, instead of a substring number, a substring name is  given.  Sec-
2510         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
2511         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
2512         name-to-number translation table.         name-to-number translation table.
2513    
2514         These functions call pcre_get_stringnumber(), and if it succeeds,  they         These  functions call pcre_get_stringnumber(), and if it succeeds, they
2515         then  call  pcre_copy_substring() or pcre_get_substring(), as appropri-         then call pcre_copy_substring() or pcre_get_substring(),  as  appropri-
2516         ate. NOTE: If PCRE_DUPNAMES is set and there are duplicate  names,  the         ate.  NOTE:  If PCRE_DUPNAMES is set and there are duplicate names, the
2517         behaviour may not be what you want (see the next section).         behaviour may not be what you want (see the next section).
2518    
2519           Warning: If the pattern uses the (?| feature to set up multiple subpat-
2520           terns  with  the  same number, as described in the section on duplicate
2521           subpattern numbers in the pcrepattern page, you  cannot  use  names  to
2522           distinguish  the  different subpatterns, because names are not included
2523           in the compiled code. The matching process uses only numbers. For  this
2524           reason,  the  use of different names for subpatterns of the same number
2525           causes an error at compile time.
2526    
2527    
2528  DUPLICATE SUBPATTERN NAMES  DUPLICATE SUBPATTERN NAMES
2529    
2530         int pcre_get_stringtable_entries(const pcre *code,         int pcre_get_stringtable_entries(const pcre *code,
2531              const char *name, char **first, char **last);              const char *name, char **first, char **last);
2532    
2533         When  a  pattern  is  compiled with the PCRE_DUPNAMES option, names for         When a pattern is compiled with the  PCRE_DUPNAMES  option,  names  for
2534         subpatterns are not required to  be  unique.  Normally,  patterns  with         subpatterns  are not required to be unique. (Duplicate names are always
2535         duplicate  names  are such that in any one match, only one of the named         allowed for subpatterns with the same number, created by using the  (?|
2536         subpatterns participates. An example is shown in the pcrepattern  docu-         feature.  Indeed,  if  such subpatterns are named, they are required to
2537         mentation.         use the same names.)
2538    
2539           Normally, patterns with duplicate names are such that in any one match,
2540           only  one of the named subpatterns participates. An example is shown in
2541           the pcrepattern documentation.
2542    
2543         When    duplicates   are   present,   pcre_copy_named_substring()   and         When   duplicates   are   present,   pcre_copy_named_substring()    and
2544         pcre_get_named_substring() return the first substring corresponding  to         pcre_get_named_substring()  return the first substring corresponding to
2545         the  given  name  that  is set. If none are set, PCRE_ERROR_NOSUBSTRING         the given name that is set. If  none  are  set,  PCRE_ERROR_NOSUBSTRING
2546         (-7) is returned; no  data  is  returned.  The  pcre_get_stringnumber()         (-7)  is  returned;  no  data  is returned. The pcre_get_stringnumber()
2547         function  returns one of the numbers that are associated with the name,         function returns one of the numbers that are associated with the  name,
2548         but it is not defined which it is.         but it is not defined which it is.
2549    
2550         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
2551         name,  you  must  use  the pcre_get_stringtable_entries() function. The         name, you must use  the  pcre_get_stringtable_entries()  function.  The
2552         first argument is the compiled pattern, and the second is the name. The         first argument is the compiled pattern, and the second is the name. The
2553         third  and  fourth  are  pointers to variables which are updated by the         third and fourth are pointers to variables which  are  updated  by  the
2554         function. After it has run, they point to the first and last entries in         function. After it has run, they point to the first and last entries in
2555         the  name-to-number  table  for  the  given  name.  The function itself         the name-to-number table  for  the  given  name.  The  function  itself
2556         returns the length of each entry,  or  PCRE_ERROR_NOSUBSTRING  (-7)  if         returns  the  length  of  each entry, or PCRE_ERROR_NOSUBSTRING (-7) if
2557         there  are none. The format of the table is described above in the sec-         there are none. The format of the table is described above in the  sec-
2558         tion entitled Information about a  pattern.   Given  all  the  relevant         tion  entitled  Information  about  a  pattern.  Given all the relevant
2559         entries  for the name, you can extract each of their numbers, and hence         entries for the name, you can extract each of their numbers, and  hence
2560         the captured data, if any.         the captured data, if any.
2561    
2562    
2563  FINDING ALL POSSIBLE MATCHES  FINDING ALL POSSIBLE MATCHES
2564    
2565         The traditional matching function uses a  similar  algorithm  to  Perl,         The  traditional  matching  function  uses a similar algorithm to Perl,
2566         which stops when it finds the first match, starting at a given point in         which stops when it finds the first match, starting at a given point in
2567         the subject. If you want to find all possible matches, or  the  longest         the  subject.  If you want to find all possible matches, or the longest
2568         possible  match,  consider using the alternative matching function (see         possible match, consider using the alternative matching  function  (see
2569         below) instead. If you cannot use the alternative function,  but  still         below)  instead.  If you cannot use the alternative function, but still
2570         need  to  find all possible matches, you can kludge it up by making use         need to find all possible matches, you can kludge it up by  making  use
2571         of the callout facility, which is described in the pcrecallout documen-         of the callout facility, which is described in the pcrecallout documen-
2572         tation.         tation.
2573    
2574         What you have to do is to insert a callout right at the end of the pat-         What you have to do is to insert a callout right at the end of the pat-
2575         tern.  When your callout function is called, extract and save the  cur-         tern.   When your callout function is called, extract and save the cur-
2576         rent  matched  substring.  Then  return  1, which forces pcre_exec() to         rent matched substring. Then return  1,  which  forces  pcre_exec()  to
2577         backtrack and try other alternatives. Ultimately, when it runs  out  of         backtrack  and  try other alternatives. Ultimately, when it runs out of
2578         matches, pcre_exec() will yield PCRE_ERROR_NOMATCH.         matches, pcre_exec() will yield PCRE_ERROR_NOMATCH.
2579    
2580    
# Line 2442  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2585  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2585              int options, int *ovector, int ovecsize,              int options, int *ovector, int ovecsize,
2586              int *workspace, int wscount);              int *workspace, int wscount);
2587    
2588         The  function  pcre_dfa_exec()  is  called  to  match  a subject string         The function pcre_dfa_exec()  is  called  to  match  a  subject  string
2589         against a compiled pattern, using a matching algorithm that  scans  the         against  a  compiled pattern, using a matching algorithm that scans the
2590         subject  string  just  once, and does not backtrack. This has different         subject string just once, and does not backtrack.  This  has  different
2591         characteristics to the normal algorithm, and  is  not  compatible  with         characteristics  to  the  normal  algorithm, and is not compatible with
2592         Perl.  Some  of the features of PCRE patterns are not supported. Never-         Perl. Some of the features of PCRE patterns are not  supported.  Never-
2593         theless, there are times when this kind of matching can be useful.  For         theless,  there are times when this kind of matching can be useful. For
2594         a discussion of the two matching algorithms, see the pcrematching docu-         a discussion of the two matching algorithms, and  a  list  of  features
2595         mentation.         that  pcre_dfa_exec() does not support, see the pcrematching documenta-
2596           tion.
2597    
2598         The arguments for the pcre_dfa_exec() function  are  the  same  as  for         The arguments for the pcre_dfa_exec() function  are  the  same  as  for
2599         pcre_exec(), plus two extras. The ovector argument is used in a differ-         pcre_exec(), plus two extras. The ovector argument is used in a differ-
# Line 2484  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2628  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2628    
2629         The unused bits of the options argument  for  pcre_dfa_exec()  must  be         The unused bits of the options argument  for  pcre_dfa_exec()  must  be
2630         zero.  The  only  bits  that  may  be  set are PCRE_ANCHORED, PCRE_NEW-         zero.  The  only  bits  that  may  be  set are PCRE_ANCHORED, PCRE_NEW-
2631         LINE_xxx, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY,  PCRE_NO_UTF8_CHECK,         LINE_xxx,        PCRE_NOTBOL,        PCRE_NOTEOL,        PCRE_NOTEMPTY,
2632         PCRE_PARTIAL, PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last         PCRE_NOTEMPTY_ATSTART, PCRE_NO_UTF8_CHECK, PCRE_PARTIAL_HARD, PCRE_PAR-
2633         three of these are the same as for pcre_exec(), so their description is         TIAL_SOFT, PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All  but  the  last
2634         not repeated here.         four  of  these  are  exactly  the  same  as  for pcre_exec(), so their
2635           description is not repeated here.
2636           PCRE_PARTIAL  
2637             PCRE_PARTIAL_HARD
2638         This  has  the  same general effect as it does for pcre_exec(), but the           PCRE_PARTIAL_SOFT
2639         details  are  slightly  different.  When  PCRE_PARTIAL   is   set   for  
2640         pcre_dfa_exec(),  the  return code PCRE_ERROR_NOMATCH is converted into         These have the same general effect as they do for pcre_exec(), but  the
2641         PCRE_ERROR_PARTIAL if the end of the subject  is  reached,  there  have         details  are  slightly  different.  When  PCRE_PARTIAL_HARD  is set for
2642         been no complete matches, but there is still at least one matching pos-         pcre_dfa_exec(), it returns PCRE_ERROR_PARTIAL if the end of  the  sub-
2643         sibility. The portion of the string that provided the partial match  is         ject  is  reached  and there is still at least one matching possibility
2644         set as the first matching string.         that requires additional characters. This happens even if some complete
2645           matches have also been found. When PCRE_PARTIAL_SOFT is set, the return
2646           code PCRE_ERROR_NOMATCH is converted into PCRE_ERROR_PARTIAL if the end
2647           of  the  subject  is  reached, there have been no complete matches, but
2648           there is still at least one matching possibility. The  portion  of  the
2649           string  that  was inspected when the longest partial match was found is
2650           set as the first matching string in both cases.
2651    
2652           PCRE_DFA_SHORTEST           PCRE_DFA_SHORTEST
2653    
2654         Setting  the  PCRE_DFA_SHORTEST option causes the matching algorithm to         Setting the PCRE_DFA_SHORTEST option causes the matching  algorithm  to
2655         stop as soon as it has found one match. Because of the way the alterna-         stop as soon as it has found one match. Because of the way the alterna-
2656         tive  algorithm  works, this is necessarily the shortest possible match         tive algorithm works, this is necessarily the shortest  possible  match
2657         at the first possible matching point in the subject string.         at the first possible matching point in the subject string.
2658    
2659           PCRE_DFA_RESTART           PCRE_DFA_RESTART
2660    
2661         When pcre_dfa_exec()  is  called  with  the  PCRE_PARTIAL  option,  and         When pcre_dfa_exec() returns a partial match, it is possible to call it
2662         returns  a  partial  match, it is possible to call it again, with addi-         again, with additional subject characters, and have  it  continue  with
2663         tional subject characters, and have it continue with  the  same  match.         the  same match. The PCRE_DFA_RESTART option requests this action; when
2664         The  PCRE_DFA_RESTART  option requests this action; when it is set, the         it is set, the workspace and wscount options must  reference  the  same
2665         workspace and wscount options must reference the same vector as  before         vector  as  before  because data about the match so far is left in them
2666         because  data  about  the  match so far is left in them after a partial         after a partial match. There is more discussion of this facility in the
2667         match. There is more discussion of this  facility  in  the  pcrepartial         pcrepartial documentation.
        documentation.  
2668    
2669     Successful returns from pcre_dfa_exec()     Successful returns from pcre_dfa_exec()
2670    
# Line 2592  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2741  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2741  SEE ALSO  SEE ALSO
2742    
2743         pcrebuild(3), pcrecallout(3), pcrecpp(3)(3), pcrematching(3),  pcrepar-         pcrebuild(3), pcrecallout(3), pcrecpp(3)(3), pcrematching(3),  pcrepar-
2744         tial(3),  pcreposix(3), pcreprecompile(3), pcresample(3), pcrestack(3).         tial(3), pcreposix(3), pcreprecompile(3), pcresample(3), pcrestack(3).
2745    
2746    
2747  AUTHOR  AUTHOR
# Line 2604  AUTHOR Line 2753  AUTHOR
2753    
2754  REVISION  REVISION
2755    
2756         Last updated: 12 April 2008         Last updated: 01 June 2010
2757         Copyright (c) 1997-2008 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
2758  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2759    
2760    
# Line 2634  PCRE CALLOUTS Line 2783  PCRE CALLOUTS
2783    
2784           (?C1)abc(?C2)def           (?C1)abc(?C2)def
2785    
2786         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() or
2787         called, PCRE automatically  inserts  callouts,  all  with  number  255,         pcre_compile2() is called, PCRE  automatically  inserts  callouts,  all
2788         before  each  item in the pattern. For example, if PCRE_AUTO_CALLOUT is         with  number  255,  before  each  item  in the pattern. For example, if
2789         used with the pattern         PCRE_AUTO_CALLOUT is used with the pattern
2790    
2791           A(\d{2}|--)           A(\d{2}|--)
2792    
# Line 2656  PCRE CALLOUTS Line 2805  PCRE CALLOUTS
2805  MISSING CALLOUTS  MISSING CALLOUTS
2806    
2807         You  should  be  aware  that,  because of optimizations in the way PCRE         You  should  be  aware  that,  because of optimizations in the way PCRE
2808         matches patterns, callouts sometimes do not happen. For example, if the         matches patterns by default, callouts  sometimes  do  not  happen.  For
2809         pattern is         example, if the pattern is
2810    
2811           ab(?C4)cd           ab(?C4)cd
2812    
# Line 2666  MISSING CALLOUTS Line 2815  MISSING CALLOUTS
2815         ever  start,  and  the  callout is never reached. However, with "abyd",         ever  start,  and  the  callout is never reached. However, with "abyd",
2816         though the result is still no match, the callout is obeyed.         though the result is still no match, the callout is obeyed.
2817    
2818           If the pattern is studied, PCRE knows the minimum length of a  matching
2819           string,  and will immediately give a "no match" return without actually
2820           running a match if the subject is not long enough, or,  for  unanchored
2821           patterns, if it has been scanned far enough.
2822    
2823           You  can disable these optimizations by passing the PCRE_NO_START_OPTI-
2824           MIZE option to pcre_exec() or  pcre_dfa_exec().  This  slows  down  the
2825           matching  process,  but  does  ensure that callouts such as the example
2826           above are obeyed.
2827    
2828    
2829  THE CALLOUT INTERFACE  THE CALLOUT INTERFACE
2830    
# Line 2693  THE CALLOUT INTERFACE Line 2852  THE CALLOUT INTERFACE
2852         version number will change again in future  if  additional  fields  are         version number will change again in future  if  additional  fields  are
2853         added, but the intention is never to remove any of the existing fields.         added, but the intention is never to remove any of the existing fields.
2854    
2855         The callout_number field contains the number of the  callout,  as  com-         The  callout_number  field  contains the number of the callout, as com-
2856         piled  into  the pattern (that is, the number after ?C for manual call-         piled into the pattern (that is, the number after ?C for  manual  call-
2857         outs, and 255 for automatically generated callouts).         outs, and 255 for automatically generated callouts).
2858    
2859         The offset_vector field is a pointer to the vector of offsets that  was         The  offset_vector field is a pointer to the vector of offsets that was
2860         passed   by   the   caller  to  pcre_exec()  or  pcre_dfa_exec().  When         passed  by  the  caller  to  pcre_exec()   or   pcre_dfa_exec().   When
2861         pcre_exec() is used, the contents can be inspected in order to  extract         pcre_exec()  is used, the contents can be inspected in order to extract
2862         substrings  that  have  been  matched  so  far,  in the same way as for         substrings that have been matched so  far,  in  the  same  way  as  for
2863         extracting substrings after a match has completed. For  pcre_dfa_exec()         extracting  substrings after a match has completed. For pcre_dfa_exec()
2864         this field is not useful.         this field is not useful.
2865    
2866         The subject and subject_length fields contain copies of the values that         The subject and subject_length fields contain copies of the values that
2867         were passed to pcre_exec().         were passed to pcre_exec().
2868    
2869         The start_match field normally contains the offset within  the  subject         The  start_match  field normally contains the offset within the subject
2870         at  which  the  current  match  attempt started. However, if the escape         at which the current match attempt  started.  However,  if  the  escape
2871         sequence \K has been encountered, this value is changed to reflect  the         sequence  \K has been encountered, this value is changed to reflect the
2872         modified  starting  point.  If the pattern is not anchored, the callout         modified starting point. If the pattern is not  anchored,  the  callout
2873         function may be called several times from the same point in the pattern         function may be called several times from the same point in the pattern
2874         for different starting points in the subject.         for different starting points in the subject.
2875    
2876         The  current_position  field  contains the offset within the subject of         The current_position field contains the offset within  the  subject  of
2877         the current match pointer.         the current match pointer.
2878    
2879         When the pcre_exec() function is used, the capture_top  field  contains         When  the  pcre_exec() function is used, the capture_top field contains
2880         one  more than the number of the highest numbered captured substring so         one more than the number of the highest numbered captured substring  so
2881         far. If no substrings have been captured, the value of  capture_top  is         far.  If  no substrings have been captured, the value of capture_top is
2882         one.  This  is always the case when pcre_dfa_exec() is used, because it         one. This is always the case when pcre_dfa_exec() is used,  because  it
2883         does not support captured substrings.         does not support captured substrings.
2884    
2885         The capture_last field contains the number of the  most  recently  cap-         The  capture_last  field  contains the number of the most recently cap-
2886         tured  substring. If no substrings have been captured, its value is -1.         tured substring. If no substrings have been captured, its value is  -1.
2887         This is always the case when pcre_dfa_exec() is used.         This is always the case when pcre_dfa_exec() is used.
2888    
2889         The callout_data field contains a value that is passed  to  pcre_exec()         The  callout_data  field contains a value that is passed to pcre_exec()
2890         or  pcre_dfa_exec() specifically so that it can be passed back in call-         or pcre_dfa_exec() specifically so that it can be passed back in  call-
2891         outs. It is passed in the pcre_callout field  of  the  pcre_extra  data         outs.  It  is  passed  in the pcre_callout field of the pcre_extra data
2892         structure.  If  no such data was passed, the value of callout_data in a         structure. If no such data was passed, the value of callout_data  in  a
2893         pcre_callout block is NULL. There is a description  of  the  pcre_extra         pcre_callout  block  is  NULL. There is a description of the pcre_extra
2894         structure in the pcreapi documentation.         structure in the pcreapi documentation.
2895    
2896         The  pattern_position field is present from version 1 of the pcre_call-         The pattern_position field is present from version 1 of the  pcre_call-
2897         out structure. It contains the offset to the next item to be matched in         out structure. It contains the offset to the next item to be matched in
2898         the pattern string.         the pattern string.
2899    
2900         The  next_item_length field is present from version 1 of the pcre_call-         The next_item_length field is present from version 1 of the  pcre_call-
2901         out structure. It contains the length of the next item to be matched in         out structure. It contains the length of the next item to be matched in
2902         the  pattern  string. When the callout immediately precedes an alterna-         the pattern string. When the callout immediately precedes  an  alterna-
2903         tion bar, a closing parenthesis, or the end of the pattern, the  length         tion  bar, a closing parenthesis, or the end of the pattern, the length
2904         is  zero.  When the callout precedes an opening parenthesis, the length         is zero. When the callout precedes an opening parenthesis,  the  length
2905         is that of the entire subpattern.         is that of the entire subpattern.
2906    
2907         The pattern_position and next_item_length fields are intended  to  help         The  pattern_position  and next_item_length fields are intended to help
2908         in  distinguishing between different automatic callouts, which all have         in distinguishing between different automatic callouts, which all  have
2909         the same callout number. However, they are set for all callouts.         the same callout number. However, they are set for all callouts.
2910    
2911    
2912  RETURN VALUES  RETURN VALUES
2913    
2914         The external callout function returns an integer to PCRE. If the  value         The  external callout function returns an integer to PCRE. If the value
2915         is  zero,  matching  proceeds  as  normal. If the value is greater than         is zero, matching proceeds as normal. If  the  value  is  greater  than
2916         zero, matching fails at the current point, but  the  testing  of  other         zero,  matching  fails  at  the current point, but the testing of other
2917         matching possibilities goes ahead, just as if a lookahead assertion had         matching possibilities goes ahead, just as if a lookahead assertion had
2918         failed. If the value is less than zero, the  match  is  abandoned,  and         failed.  If  the  value  is less than zero, the match is abandoned, and
2919         pcre_exec() (or pcre_dfa_exec()) returns the negative value.         pcre_exec() or pcre_dfa_exec() returns the negative value.
2920    
2921         Negative   values   should   normally   be   chosen  from  the  set  of         Negative  values  should  normally  be   chosen   from   the   set   of
2922         PCRE_ERROR_xxx values. In particular, PCRE_ERROR_NOMATCH forces a stan-         PCRE_ERROR_xxx values. In particular, PCRE_ERROR_NOMATCH forces a stan-
2923         dard  "no  match"  failure.   The  error  number  PCRE_ERROR_CALLOUT is         dard "no  match"  failure.   The  error  number  PCRE_ERROR_CALLOUT  is
2924         reserved for use by callout functions; it will never be  used  by  PCRE         reserved  for  use  by callout functions; it will never be used by PCRE
2925         itself.         itself.
2926    
2927    
# Line 2775  AUTHOR Line 2934  AUTHOR
2934    
2935  REVISION  REVISION
2936    
2937         Last updated: 29 May 2007         Last updated: 29 September 2009
2938         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
2939  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2940    
2941    
# Line 2790  NAME Line 2949  NAME
2949  DIFFERENCES BETWEEN PCRE AND PERL  DIFFERENCES BETWEEN PCRE AND PERL
2950    
2951         This  document describes the differences in the ways that PCRE and Perl         This  document describes the differences in the ways that PCRE and Perl
2952         handle regular expressions. The differences described here  are  mainly         handle regular expressions. The differences  described  here  are  with
2953         with  respect  to  Perl 5.8, though PCRE versions 7.0 and later contain         respect to Perl 5.10/5.11.
        some features that are expected to be in the forthcoming Perl 5.10.  
2954    
2955         1. PCRE has only a subset of Perl's UTF-8 and Unicode support.  Details         1.  PCRE has only a subset of Perl's UTF-8 and Unicode support. Details
2956         of  what  it does have are given in the section on UTF-8 support in the         of what it does have are given in the section on UTF-8 support  in  the
2957         main pcre page.         main pcre page.
2958    
2959         2. PCRE does not allow repeat quantifiers on lookahead assertions. Perl         2. PCRE does not allow repeat quantifiers on lookahead assertions. Perl
2960         permits  them,  but they do not mean what you might think. For example,         permits them, but they do not mean what you might think.  For  example,
2961         (?!a){3} does not assert that the next three characters are not "a". It         (?!a){3} does not assert that the next three characters are not "a". It
2962         just asserts that the next character is not "a" three times.         just asserts that the next character is not "a" three times.
2963    
2964         3.  Capturing  subpatterns  that occur inside negative lookahead asser-         3. Capturing subpatterns that occur inside  negative  lookahead  asser-
2965         tions are counted, but their entries in the offsets  vector  are  never         tions  are  counted,  but their entries in the offsets vector are never
2966         set.  Perl sets its numerical variables from any such patterns that are         set. Perl sets its numerical variables from any such patterns that  are
2967         matched before the assertion fails to match something (thereby succeed-         matched before the assertion fails to match something (thereby succeed-
2968         ing),  but  only  if the negative lookahead assertion contains just one         ing), but only if the negative lookahead assertion  contains  just  one
2969         branch.         branch.
2970    
2971         4. Though binary zero characters are supported in the  subject  string,         4.  Though  binary zero characters are supported in the subject string,
2972         they are not allowed in a pattern string because it is passed as a nor-         they are not allowed in a pattern string because it is passed as a nor-
2973         mal C string, terminated by zero. The escape sequence \0 can be used in         mal C string, terminated by zero. The escape sequence \0 can be used in
2974         the pattern to represent a binary zero.         the pattern to represent a binary zero.
2975    
2976         5.  The  following Perl escape sequences are not supported: \l, \u, \L,         5. The following Perl escape sequences are not supported: \l,  \u,  \L,
2977         \U, and \N. In fact these are implemented by Perl's general string-han-         \U, and \N. In fact these are implemented by Perl's general string-han-
2978         dling  and are not part of its pattern matching engine. If any of these         dling and are not part of its pattern matching engine. If any of  these
2979         are encountered by PCRE, an error is generated.         are encountered by PCRE, an error is generated.
2980    
2981         6. The Perl escape sequences \p, \P, and \X are supported only if  PCRE         6.  The Perl escape sequences \p, \P, and \X are supported only if PCRE
2982         is  built  with Unicode character property support. The properties that         is built with Unicode character property support. The  properties  that
2983         can be tested with \p and \P are limited to the general category  prop-         can  be tested with \p and \P are limited to the general category prop-
2984         erties  such  as  Lu and Nd, script names such as Greek or Han, and the         erties such as Lu and Nd, script names such as Greek or  Han,  and  the
2985         derived properties Any and L&.         derived  properties  Any  and  L&. PCRE does support the Cs (surrogate)
2986           property, which Perl does not; the  Perl  documentation  says  "Because
2987           Perl hides the need for the user to understand the internal representa-
2988           tion of Unicode characters, there is no need to implement the  somewhat
2989           messy concept of surrogates."
2990    
2991         7. PCRE does support the \Q...\E escape for quoting substrings. Charac-         7. PCRE does support the \Q...\E escape for quoting substrings. Charac-
2992         ters  in  between  are  treated as literals. This is slightly different         ters in between are treated as literals.  This  is  slightly  different
2993         from Perl in that $ and @ are  also  handled  as  literals  inside  the         from  Perl  in  that  $  and  @ are also handled as literals inside the
2994         quotes.  In Perl, they cause variable interpolation (but of course PCRE         quotes. In Perl, they cause variable interpolation (but of course  PCRE
2995         does not have variables). Note the following examples:         does not have variables). Note the following examples:
2996    
2997             Pattern            PCRE matches      Perl matches             Pattern            PCRE matches      Perl matches
# Line 2839  DIFFERENCES BETWEEN PCRE AND PERL Line 3001  DIFFERENCES BETWEEN PCRE AND PERL
3001             \Qabc\$xyz\E       abc\$xyz          abc\$xyz             \Qabc\$xyz\E       abc\$xyz          abc\$xyz
3002             \Qabc\E\$\Qxyz\E   abc$xyz           abc$xyz             \Qabc\E\$\Qxyz\E   abc$xyz           abc$xyz
3003    
3004         The \Q...\E sequence is recognized both inside  and  outside  character         The  \Q...\E  sequence  is recognized both inside and outside character
3005         classes.         classes.
3006    
3007         8. Fairly obviously, PCRE does not support the (?{code}) and (??{code})         8. Fairly obviously, PCRE does not support the (?{code}) and (??{code})
3008         constructions. However, there is support for recursive  patterns.  This         constructions.  However,  there is support for recursive patterns. This
3009         is  not available in Perl 5.8, but will be in Perl 5.10. Also, the PCRE         is not available in Perl 5.8, but it is in Perl 5.10.  Also,  the  PCRE
3010         "callout" feature allows an external function to be called during  pat-         "callout"  feature allows an external function to be called during pat-
3011         tern matching. See the pcrecallout documentation for details.         tern matching. See the pcrecallout documentation for details.
3012    
3013         9.  Subpatterns  that  are  called  recursively or as "subroutines" are         9. Subpatterns that are called  recursively  or  as  "subroutines"  are
3014         always treated as atomic groups in  PCRE.  This  is  like  Python,  but         always  treated  as  atomic  groups  in  PCRE. This is like Python, but
3015         unlike Perl.         unlike Perl. There is a discussion of an example that explains this  in
3016           more  detail  in  the section on recursion differences from Perl in the
3017         10.  There are some differences that are concerned with the settings of         pcrepattern page.
3018         captured strings when part of  a  pattern  is  repeated.  For  example,  
3019         matching  "aba"  against  the  pattern  /^(a(b)?)+$/  in Perl leaves $2         10. There are some differences that are concerned with the settings  of
3020           captured  strings  when  part  of  a  pattern is repeated. For example,
3021           matching "aba" against the  pattern  /^(a(b)?)+$/  in  Perl  leaves  $2
3022         unset, but in PCRE it is set to "b".         unset, but in PCRE it is set to "b".
3023    
3024         11.  PCRE  does  support  Perl  5.10's  backtracking  verbs  (*ACCEPT),         11.  PCRE's handling of duplicate subpattern numbers and duplicate sub-
3025         (*FAIL),  (*F),  (*COMMIT), (*PRUNE), (*SKIP), and (*THEN), but only in         pattern names is not as general as Perl's. This is a consequence of the
3026         the forms without an  argument.  PCRE  does  not  support  (*MARK).  If         fact the PCRE works internally just with numbers, using an external ta-
3027         (*ACCEPT)  is within capturing parentheses, PCRE does not set that cap-         ble to translate between numbers and names. In  particular,  a  pattern
3028         ture group; this is different to Perl.         such  as  (?|(?<a>A)|(?<b)B),  where the two capturing parentheses have
3029           the same number but different names, is not supported,  and  causes  an
3030           error  at compile time. If it were allowed, it would not be possible to
3031           distinguish which parentheses matched, because both names map  to  cap-
3032           turing subpattern number 1. To avoid this confusing situation, an error
3033           is given at compile time.
3034    
3035         12. PCRE provides some extensions to the Perl regular expression facil-         12. PCRE provides some extensions to the Perl regular expression facil-
3036         ities.   Perl  5.10  will  include new features that are not in earlier         ities.   Perl  5.10  includes new features that are not in earlier ver-
3037         versions, some of which (such as named parentheses) have been  in  PCRE         sions of Perl, some of which (such as named parentheses) have  been  in
3038         for some time. This list is with respect to Perl 5.10:         PCRE for some time. This list is with respect to Perl 5.10:
3039    
3040         (a)  Although  lookbehind  assertions  must match fixed length strings,         (a)  Although  lookbehind  assertions  in  PCRE must match fixed length
3041         each alternative branch of a lookbehind assertion can match a different         strings, each alternative branch of a lookbehind assertion can match  a
3042         length of string. Perl requires them all to have the same length.         different  length  of  string.  Perl requires them all to have the same
3043           length.
3044    
3045         (b)  If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $         (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the  $
3046         meta-character matches only at the very end of the string.         meta-character matches only at the very end of the string.
3047    
3048         (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-
3049         cial meaning is faulted. Otherwise, like Perl, the backslash is quietly         cial meaning is faulted. Otherwise, like Perl, the backslash is quietly
3050         ignored.  (Perl can be made to issue a warning.)         ignored.  (Perl can be made to issue a warning.)
3051    
3052         (d) If PCRE_UNGREEDY is set, the greediness of the  repetition  quanti-         (d)  If  PCRE_UNGREEDY is set, the greediness of the repetition quanti-
3053         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-
3054         lowed by a question mark they are.         lowed by a question mark they are.
3055    
3056         (e) PCRE_ANCHORED can be used at matching time to force a pattern to be         (e) PCRE_ANCHORED can be used at matching time to force a pattern to be
3057         tried only at the first matching position in the subject string.         tried only at the first matching position in the subject string.
3058    
3059         (f)  The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and PCRE_NO_AUTO_CAP-         (f) The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART,
3060         TURE options for pcre_exec() have no Perl equivalents.         and  PCRE_NO_AUTO_CAPTURE  options for pcre_exec() have no Perl equiva-
3061           lents.
3062    
3063         (g) The \R escape sequence can be restricted to match only CR,  LF,  or         (g) The \R escape sequence can be restricted to match only CR,  LF,  or
3064         CRLF by the PCRE_BSR_ANYCRLF option.         CRLF by the PCRE_BSR_ANYCRLF option.
# Line 2916  AUTHOR Line 3087  AUTHOR
3087    
3088  REVISION  REVISION
3089    
3090         Last updated: 11 September 2007         Last updated: 12 May 2010
3091         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2010 University of Cambridge.
3092  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
3093    
3094    
# Line 2947  PCRE REGULAR EXPRESSION DETAILS Line 3118  PCRE REGULAR EXPRESSION DETAILS
3118    
3119         The original operation of PCRE was on strings of  one-byte  characters.         The original operation of PCRE was on strings of  one-byte  characters.
3120         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
3121         this, you must build PCRE to  include  UTF-8  support,  and  then  call         this, PCRE must be built to include UTF-8 support, and  you  must  call
3122         pcre_compile()  with  the  PCRE_UTF8  option.  How this affects pattern         pcre_compile()  or  pcre_compile2() with the PCRE_UTF8 option. There is
3123         matching is mentioned in several places below. There is also a  summary         also a special sequence that can be given at the start of a pattern:
3124         of  UTF-8  features  in  the  section on UTF-8 support in the main pcre  
3125         page.           (*UTF8)
3126    
3127           Starting a pattern with this sequence  is  equivalent  to  setting  the
3128           PCRE_UTF8  option.  This  feature  is  not Perl-compatible. How setting
3129           UTF-8 mode affects pattern matching  is  mentioned  in  several  places
3130           below.  There  is  also  a  summary of UTF-8 features in the section on
3131           UTF-8 support in the main pcre page.
3132    
3133           Another special sequence that may appear at the start of a  pattern  or
3134           in combination with (*UTF8) is:
3135    
3136             (*UCP)
3137    
3138           This  has  the  same  effect  as setting the PCRE_UCP option: it causes
3139           sequences such as \d and \w to  use  Unicode  properties  to  determine
3140           character types, instead of recognizing only characters with codes less
3141           than 128 via a lookup table.
3142    
3143         The remainder of this document discusses the  patterns  that  are  sup-         The remainder of this document discusses the  patterns  that  are  sup-
3144         ported  by  PCRE when its main matching function, pcre_exec(), is used.         ported  by  PCRE when its main matching function, pcre_exec(), is used.
# Line 2981  NEWLINE CONVENTIONS Line 3168  NEWLINE CONVENTIONS
3168           (*ANYCRLF)   any of the three above           (*ANYCRLF)   any of the three above
3169           (*ANY)       all Unicode newline sequences           (*ANY)       all Unicode newline sequences
3170    
3171         These override the default and the options given to pcre_compile(). For         These  override  the default and the options given to pcre_compile() or
3172         example, on a Unix system where LF is the default newline sequence, the         pcre_compile2(). For example, on a Unix system where LF is the  default
3173         pattern         newline sequence, the pattern
3174    
3175           (*CR)a.b           (*CR)a.b
3176    
# Line 2993  NEWLINE CONVENTIONS Line 3180  NEWLINE CONVENTIONS
3180         and that they must be in upper case.  If  more  than  one  of  them  is         and that they must be in upper case.  If  more  than  one  of  them  is
3181         present, the last one is used.         present, the last one is used.
3182    
3183         The  newline  convention  does  not  affect what the \R escape sequence         The  newline convention affects the interpretation of the dot metachar-
3184         matches. By default, this is any Unicode  newline  sequence,  for  Perl         acter when PCRE_DOTALL is not set, and also the behaviour of  \N.  How-
3185         compatibility.  However, this can be changed; see the description of \R         ever,  it  does  not  affect  what  the  \R escape sequence matches. By
3186         in the section entitled "Newline sequences" below. A change of \R  set-         default, this is any Unicode newline sequence, for Perl  compatibility.
3187         ting can be combined with a change of newline convention.         However,  this can be changed; see the description of \R in the section
3188           entitled "Newline sequences" below. A change of \R setting can be  com-
3189           bined with a change of newline convention.
3190    
3191    
3192  CHARACTERS AND METACHARACTERS  CHARACTERS AND METACHARACTERS
# Line 3055  CHARACTERS AND METACHARACTERS Line 3244  CHARACTERS AND METACHARACTERS
3244                    syntax)                    syntax)
3245           ]      terminates the character class           ]      terminates the character class
3246    
3247         The  following sections describe the use of each of the metacharacters.         The following sections describe the use of each of the metacharacters.
3248    
3249    
3250  BACKSLASH  BACKSLASH
3251    
3252         The backslash character has several uses. Firstly, if it is followed by         The backslash character has several uses. Firstly, if it is followed by
3253         a  non-alphanumeric  character,  it takes away any special meaning that         a non-alphanumeric character, it takes away any  special  meaning  that
3254         character may have. This  use  of  backslash  as  an  escape  character         character  may  have.  This  use  of  backslash  as an escape character
3255         applies both inside and outside character classes.         applies both inside and outside character classes.
3256    
3257         For  example,  if  you want to match a * character, you write \* in the         For example, if you want to match a * character, you write  \*  in  the
3258         pattern.  This escaping action applies whether  or  not  the  following         pattern.   This  escaping  action  applies whether or not the following
3259         character  would  otherwise be interpreted as a metacharacter, so it is         character would otherwise be interpreted as a metacharacter, so  it  is
3260         always safe to precede a non-alphanumeric  with  backslash  to  specify         always  safe  to  precede  a non-alphanumeric with backslash to specify
3261         that  it stands for itself. In particular, if you want to match a back-         that it stands for itself. In particular, if you want to match a  back-
3262         slash, you write \\.         slash, you write \\.
3263    
3264         If a pattern is compiled with the PCRE_EXTENDED option,  whitespace  in         If  a  pattern is compiled with the PCRE_EXTENDED option, whitespace in
3265         the  pattern (other than in a character class) and characters between a         the pattern (other than in a character class) and characters between  a
3266         # outside a character class and the next newline are ignored. An escap-         # outside a character class and the next newline are ignored. An escap-
3267         ing  backslash  can  be  used to include a whitespace or # character as         ing backslash can be used to include a whitespace  or  #  character  as
3268         part of the pattern.         part of the pattern.
3269    
3270         If you want to remove the special meaning from a  sequence  of  charac-         If  you  want  to remove the special meaning from a sequence of charac-
3271         ters,  you can do so by putting them between \Q and \E. This is differ-         ters, you can do so by putting them between \Q and \E. This is  differ-
3272         ent from Perl in that $ and  @  are  handled  as  literals  in  \Q...\E         ent  from  Perl  in  that  $  and  @ are handled as literals in \Q...\E
3273         sequences  in  PCRE, whereas in Perl, $ and @ cause variable interpola-         sequences in PCRE, whereas in Perl, $ and @ cause  variable  interpola-
3274         tion. Note the following examples:         tion. Note the following examples:
3275    
3276           Pattern            PCRE matches   Perl matches           Pattern            PCRE matches   Perl matches
# Line 3091  BACKSLASH Line 3280  BACKSLASH
3280           \Qabc\$xyz\E       abc\$xyz       abc\$xyz           \Qabc\$xyz\E       abc\$xyz       abc\$xyz
3281           \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz           \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz
3282    
3283         The \Q...\E sequence is recognized both inside  and  outside  character         The  \Q...\E  sequence  is recognized both inside and outside character
3284         classes.         classes.
3285    
3286     Non-printing characters     Non-printing characters
3287    
3288         A second use of backslash provides a way of encoding non-printing char-         A second use of backslash provides a way of encoding non-printing char-
3289         acters in patterns in a visible manner. There is no restriction on  the         acters  in patterns in a visible manner. There is no restriction on the
3290         appearance  of non-printing characters, apart from the binary zero that         appearance of non-printing characters, apart from the binary zero  that
3291         terminates a pattern, but when a pattern  is  being  prepared  by  text         terminates  a  pattern,  but  when  a pattern is being prepared by text
3292         editing,  it  is  usually  easier  to  use  one of the following escape         editing, it is  often  easier  to  use  one  of  the  following  escape
3293         sequences than the binary character it represents:         sequences than the binary character it represents:
3294    
3295           \a        alarm, that is, the BEL character (hex 07)           \a        alarm, that is, the BEL character (hex 07)
# Line 3110  BACKSLASH Line 3299  BACKSLASH
3299           \n        linefeed (hex 0A)           \n        linefeed (hex 0A)
3300           \r        carriage return (hex 0D)           \r        carriage return (hex 0D)
3301           \t        tab (hex 09)           \t        tab (hex 09)
3302           \ddd      character with octal code ddd, or backreference           \ddd      character with octal code ddd, or back reference
3303           \xhh      character with hex code hh           \xhh      character with hex code hh
3304           \x{hhh..} character with hex code hhh..           \x{hhh..} character with hex code hhh..
3305    
3306         The precise effect of \cx is as follows: if x is a lower  case  letter,         The  precise  effect of \cx is as follows: if x is a lower case letter,
3307         it  is converted to upper case. Then bit 6 of the character (hex 40) is         it is converted to upper case. Then bit 6 of the character (hex 40)  is
3308         inverted.  Thus \cz becomes hex 1A, but \c{ becomes hex 3B,  while  \c;         inverted.   Thus  \cz becomes hex 1A, but \c{ becomes hex 3B, while \c;
3309         becomes hex 7B.         becomes hex 7B.
3310    
3311         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
3312         in upper or lower case). Any number of hexadecimal  digits  may  appear         in  upper  or  lower case). Any number of hexadecimal digits may appear
3313         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
3314         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,
3315         the  maximum value in hexadecimal is 7FFFFFFF. Note that this is bigger         the maximum value in hexadecimal is 7FFFFFFF. Note that this is  bigger
3316         than the largest Unicode code point, which is 10FFFF.         than the largest Unicode code point, which is 10FFFF.
3317    
3318         If characters other than hexadecimal digits appear between \x{  and  },         If  characters  other than hexadecimal digits appear between \x{ and },
3319         or if there is no terminating }, this form of escape is not recognized.         or if there is no terminating }, this form of escape is not recognized.
3320         Instead, the initial \x will be  interpreted  as  a  basic  hexadecimal         Instead,  the  initial  \x  will  be interpreted as a basic hexadecimal
3321         escape,  with  no  following  digits, giving a character whose value is         escape, with no following digits, giving a  character  whose  value  is
3322         zero.         zero.
3323    
3324         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
3325         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-
3326         dled. For example, \xdc is exactly the same as \x{dc}.         dled. For example, \xdc is exactly the same as \x{dc}.
3327    
3328         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
3329         than  two  digits,  just  those  that  are  present  are used. Thus the         than two digits, just  those  that  are  present  are  used.  Thus  the
3330         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
3331         (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
3332         if the pattern character that follows is itself an octal digit.         if the pattern character that follows is itself an octal digit.
3333    
3334         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-
3335         cated.  Outside a character class, PCRE reads it and any following dig-         cated.  Outside a character class, PCRE reads it and any following dig-
3336         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
3337         have been at least that many previous capturing left parentheses in the         have been at least that many previous capturing left parentheses in the
3338         expression, the entire  sequence  is  taken  as  a  back  reference.  A         expression,  the  entire  sequence  is  taken  as  a  back reference. A
3339         description  of how this works is given later, following the discussion         description of how this works is given later, following the  discussion
3340         of parenthesized subpatterns.         of parenthesized subpatterns.
3341    
3342         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
3343         and  there have not been that many capturing subpatterns, PCRE re-reads         and there have not been that many capturing subpatterns, PCRE  re-reads
3344         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-
3345         erate  a data character. Any subsequent digits stand for themselves. In         erate a data character. Any subsequent digits stand for themselves.  In
3346         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
3347         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
3348         example:         example:
3349    
3350           \040   is another way of writing a space           \040   is another way of writing a space
# Line 3173  BACKSLASH Line 3362  BACKSLASH
3362           \81    is either a back reference, or a binary zero           \81    is either a back reference, or a binary zero
3363                     followed by the two characters "8" and "1"                     followed by the two characters "8" and "1"
3364    
3365         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
3366         leading zero, because no more than three octal digits are ever read.         leading zero, because no more than three octal digits are ever read.
3367    
3368         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
3369         inside and outside character classes. In addition, inside  a  character         inside  and  outside character classes. In addition, inside a character
3370         class,  the  sequence \b is interpreted as the backspace character (hex         class, the sequence \b is interpreted as the backspace  character  (hex
3371         08), and the sequences \R and \X are interpreted as the characters  "R"         08).  The sequences \B, \N, \R, and \X are not special inside a charac-
3372         and  "X", respectively. Outside a character class, these sequences have         ter class. Like any  other  unrecognized  escape  sequences,  they  are
3373         different meanings (see below).         treated  as  the  literal characters "B", "N", "R", and "X" by default,
3374           but cause an error if the PCRE_EXTRA option is set. Outside a character
3375           class, these sequences have different meanings.
3376    
3377     Absolute and relative back references     Absolute and relative back references
3378    
3379         The sequence \g followed by an unsigned or a negative  number,  option-         The  sequence  \g followed by an unsigned or a negative number, option-
3380         ally  enclosed  in braces, is an absolute or relative back reference. A         ally enclosed in braces, is an absolute or relative back  reference.  A
3381         named back reference can be coded as \g{name}. Back references are dis-         named back reference can be coded as \g{name}. Back references are dis-
3382         cussed later, following the discussion of parenthesized subpatterns.         cussed later, following the discussion of parenthesized subpatterns.
3383    
3384     Absolute and relative subroutine calls     Absolute and relative subroutine calls
3385    
3386         For  compatibility with Oniguruma, the non-Perl syntax \g followed by a         For compatibility with Oniguruma, the non-Perl syntax \g followed by  a
3387         name or a number enclosed either in angle brackets or single quotes, is         name or a number enclosed either in angle brackets or single quotes, is
3388         an  alternative  syntax for referencing a subpattern as a "subroutine".         an alternative syntax for referencing a subpattern as  a  "subroutine".
3389         Details are discussed later.   Note  that  \g{...}  (Perl  syntax)  and         Details  are  discussed  later.   Note  that  \g{...} (Perl syntax) and
3390         \g<...>  (Oniguruma  syntax)  are  not synonymous. The former is a back         \g<...> (Oniguruma syntax) are not synonymous. The  former  is  a  back
3391         reference; the latter is a subroutine call.         reference; the latter is a subroutine call.
3392    
3393     Generic character types     Generic character types
3394    
3395         Another use of backslash is for specifying generic character types. The         Another use of backslash is for specifying generic character types:
        following are always recognized:  
3396    
3397           \d     any decimal digit           \d     any decimal digit
3398           \D     any character that is not a decimal digit           \D     any character that is not a decimal digit
# Line 3215  BACKSLASH Line 3405  BACKSLASH
3405           \w     any "word" character           \w     any "word" character
3406           \W     any "non-word" character           \W     any "non-word" character
3407    
3408         Each pair of escape sequences partitions the complete set of characters         There is also the single sequence \N, which matches a non-newline char-
3409         into two disjoint sets. Any given character matches one, and only  one,         acter.  This is the same as the "." metacharacter when  PCRE_DOTALL  is
3410         of each pair.         not set.
3411    
3412         These character type sequences can appear both inside and outside char-         Each  pair of lower and upper case escape sequences partitions the com-
3413         acter classes. They each match one character of the  appropriate  type.         plete set of characters into two disjoint  sets.  Any  given  character
3414         If  the current matching point is at the end of the subject string, all         matches  one, and only one, of each pair. The sequences can appear both
3415         of them fail, since there is no character to match.         inside and outside character classes. They each match one character  of
3416           the  appropriate  type.  If the current matching point is at the end of
3417         For compatibility with Perl, \s does not match the VT  character  (code         the subject string, all of them fail, because there is no character  to
3418         11).   This makes it different from the the POSIX "space" class. The \s         match.
3419         characters are HT (9), LF (10), FF (12), CR (13), and  space  (32).  If  
3420           For  compatibility  with Perl, \s does not match the VT character (code
3421           11).  This makes it different from the the POSIX "space" class. The  \s
3422           characters  are  HT  (9), LF (10), FF (12), CR (13), and space (32). If
3423         "use locale;" is included in a Perl script, \s may match the VT charac-         "use locale;" is included in a Perl script, \s may match the VT charac-
3424         ter. In PCRE, it never does.         ter. In PCRE, it never does.
3425    
3426         In UTF-8 mode, characters with values greater than 128 never match  \d,         A  "word"  character is an underscore or any character that is a letter
3427         \s, or \w, and always match \D, \S, and \W. This is true even when Uni-         or digit.  By default, the definition of letters  and  digits  is  con-
3428         code character property support is available.  These  sequences  retain         trolled  by PCRE's low-valued character tables, and may vary if locale-
3429         their original meanings from before UTF-8 support was available, mainly         specific matching is taking place (see "Locale support" in the  pcreapi
3430         for efficiency reasons.         page).  For  example,  in  a French locale such as "fr_FR" in Unix-like
3431           systems, or "french" in Windows, some character codes greater than  128
3432           are  used  for  accented letters, and these are then matched by \w. The
3433           use of locales with Unicode is discouraged.
3434    
3435           By default, in UTF-8 mode, characters  with  values  greater  than  128
3436           never  match  \d,  \s,  or  \w,  and always match \D, \S, and \W. These
3437           sequences retain their original meanings from before UTF-8 support  was
3438           available,  mainly for efficiency reasons. However, if PCRE is compiled
3439           with Unicode property support, and the PCRE_UCP option is set, the  be-
3440           haviour  is  changed  so  that Unicode properties are used to determine
3441           character types, as follows:
3442    
3443             \d  any character that \p{Nd} matches (decimal digit)
3444             \s  any character that \p{Z} matches, plus HT, LF, FF, CR
3445             \w  any character that \p{L} or \p{N} matches, plus underscore
3446    
3447           The upper case escapes match the inverse sets of characters. Note  that
3448           \d  matches  only decimal digits, whereas \w matches any Unicode digit,
3449           as well as any Unicode letter, and underscore. Note also that  PCRE_UCP
3450           affects  \b,  and  \B  because  they are defined in terms of \w and \W.
3451           Matching these sequences is noticeably slower when PCRE_UCP is set.
3452    
3453         The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to         The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to
3454         the  other  sequences, these do match certain high-valued codepoints in         the  other  sequences,  which  match  only ASCII characters by default,
3455         UTF-8 mode.  The horizontal space characters are:         these always  match  certain  high-valued  codepoints  in  UTF-8  mode,
3456           whether or not PCRE_UCP is set. The horizontal space characters are:
3457    
3458           U+0009     Horizontal tab           U+0009     Horizontal tab
3459           U+0020     Space           U+0020     Space
# Line 3270  BACKSLASH Line 3485  BACKSLASH
3485           U+2028     Line separator           U+2028     Line separator
3486           U+2029     Paragraph separator           U+2029     Paragraph separator
3487    
        A "word" character is an underscore or any character less than 256 that  
        is  a  letter  or  digit.  The definition of letters and digits is con-  
        trolled by PCRE's low-valued character tables, and may vary if  locale-  
        specific  matching is taking place (see "Locale support" in the pcreapi  
        page). For example, in a French locale such  as  "fr_FR"  in  Unix-like  
        systems,  or "french" in Windows, some character codes greater than 128  
        are used for accented letters, and these are matched by \w. The use  of  
        locales with Unicode is discouraged.  
   
3488     Newline sequences     Newline sequences
3489    
3490         Outside  a  character class, by default, the escape sequence \R matches         Outside  a  character class, by default, the escape sequence \R matches
# Line 3311  BACKSLASH Line 3517  BACKSLASH
3517           (*BSR_ANYCRLF)   CR, LF, or CRLF only           (*BSR_ANYCRLF)   CR, LF, or CRLF only
3518           (*BSR_UNICODE)   any Unicode newline sequence           (*BSR_UNICODE)   any Unicode newline sequence
3519    
3520         These override the default and the options given to pcre_compile(), but         These override the default and the options given to  pcre_compile()  or
3521         they can be overridden by options given to pcre_exec(). Note that these         pcre_compile2(),  but  they  can  be  overridden  by  options  given to
3522         special settings, which are not Perl-compatible, are recognized only at         pcre_exec() or pcre_dfa_exec(). Note that these special settings, which
3523         the  very  start  of a pattern, and that they must be in upper case. If         are  not  Perl-compatible,  are  recognized only at the very start of a
3524         more than one of them is present, the last one is  used.  They  can  be         pattern, and that they must be in upper case. If more than one of  them
3525         combined  with  a  change of newline convention, for example, a pattern         is present, the last one is used. They can be combined with a change of
3526         can start with:         newline convention; for example, a pattern can start with:
3527    
3528           (*ANY)(*BSR_ANYCRLF)           (*ANY)(*BSR_ANYCRLF)
3529    
3530         Inside a character class, \R matches the letter "R".         They can also be combined with the (*UTF8) or (*UCP) special sequences.
3531           Inside  a  character  class,  \R  is  treated as an unrecognized escape
3532           sequence, and so matches the letter "R" by default, but causes an error
3533           if PCRE_EXTRA is set.
3534    
3535     Unicode character properties     Unicode character properties
3536    
3537         When PCRE is built with Unicode character property support, three addi-         When PCRE is built with Unicode character property support, three addi-
3538         tional  escape sequences that match characters with specific properties         tional escape sequences that match characters with specific  properties
3539         are available.  When not in UTF-8 mode, these sequences are  of  course         are  available.   When not in UTF-8 mode, these sequences are of course
3540         limited  to  testing characters whose codepoints are less than 256, but         limited to testing characters whose codepoints are less than  256,  but
3541         they do work in this mode.  The extra escape sequences are:         they do work in this mode.  The extra escape sequences are:
3542    
3543           \p{xx}   a character with the xx property           \p{xx}   a character with the xx property
3544           \P{xx}   a character without the xx property           \P{xx}   a character without the xx property
3545           \X       an extended Unicode sequence           \X       an extended Unicode sequence
3546    
3547         The property names represented by xx above are limited to  the  Unicode         The  property  names represented by xx above are limited to the Unicode
3548         script names, the general category properties, and "Any", which matches         script names, the general category properties, "Any", which matches any
3549         any character (including newline). Other properties such as "InMusical-         character   (including  newline),  and  some  special  PCRE  properties
3550         Symbols"  are  not  currently supported by PCRE. Note that \P{Any} does         (described in the next section).  Other Perl properties such as  "InMu-
3551         not match any characters, so always causes a match failure.         sicalSymbols"  are  not  currently supported by PCRE. Note that \P{Any}
3552           does not match any characters, so always causes a match failure.
3553    
3554         Sets of Unicode characters are defined as belonging to certain scripts.         Sets of Unicode characters are defined as belonging to certain scripts.
3555         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.
# Line 3351  BACKSLASH Line 3561  BACKSLASH
3561         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
3562         "Common". The current list of scripts is:         "Common". The current list of scripts is:
3563    
3564         Arabic,  Armenian,  Balinese,  Bengali,  Bopomofo,  Braille,  Buginese,         Arabic, Armenian, Avestan, Balinese, Bamum, Bengali, Bopomofo, Braille,
3565         Buhid,  Canadian_Aboriginal,  Cherokee,  Common,   Coptic,   Cuneiform,         Buginese, Buhid, Canadian_Aboriginal, Carian, Cham,  Cherokee,  Common,
3566         Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic,         Coptic,   Cuneiform,  Cypriot,  Cyrillic,  Deseret,  Devanagari,  Egyp-
3567         Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew,  Hira-         tian_Hieroglyphs,  Ethiopic,  Georgian,  Glagolitic,   Gothic,   Greek,
3568         gana,  Inherited,  Kannada,  Katakana,  Kharoshthi,  Khmer, Lao, Latin,         Gujarati,  Gurmukhi,  Han,  Hangul,  Hanunoo,  Hebrew,  Hiragana, Impe-
3569         Limbu,  Linear_B,  Malayalam,  Mongolian,  Myanmar,  New_Tai_Lue,  Nko,         rial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscriptional_Parthian,
3570         Ogham,  Old_Italic,  Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician,         Javanese,  Kaithi, Kannada, Katakana, Kayah_Li, Kharoshthi, Khmer, Lao,
3571         Runic,  Shavian,  Sinhala,  Syloti_Nagri,  Syriac,  Tagalog,  Tagbanwa,         Latin,  Lepcha,  Limbu,  Linear_B,  Lisu,  Lycian,  Lydian,  Malayalam,
3572         Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi.         Meetei_Mayek,  Mongolian, Myanmar, New_Tai_Lue, Nko, Ogham, Old_Italic,
3573           Old_Persian, Old_South_Arabian, Old_Turkic, Ol_Chiki,  Oriya,  Osmanya,
3574         Each  character has exactly one general category property, specified by         Phags_Pa,  Phoenician,  Rejang,  Runic, Samaritan, Saurashtra, Shavian,
3575         a two-letter abbreviation. For compatibility with Perl, negation can be         Sinhala, Sundanese, Syloti_Nagri, Syriac,  Tagalog,  Tagbanwa,  Tai_Le,
3576         specified  by  including a circumflex between the opening brace and the         Tai_Tham,  Tai_Viet,  Tamil,  Telugu,  Thaana, Thai, Tibetan, Tifinagh,
3577         property name. For example, \p{^Lu} is the same as \P{Lu}.         Ugaritic, Vai, Yi.
3578    
3579           Each character has exactly one Unicode general category property, spec-
3580           ified  by a two-letter abbreviation. For compatibility with Perl, nega-
3581           tion can be specified by including a  circumflex  between  the  opening
3582           brace  and  the  property  name.  For  example,  \p{^Lu} is the same as
3583           \P{Lu}.
3584    
3585         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-
3586         eral  category properties that start with that letter. In this case, in         eral  category properties that start with that letter. In this case, in
# Line 3428  BACKSLASH Line 3644  BACKSLASH
3644         U+D800 to U+DFFF. Such characters are not valid in UTF-8  strings  (see         U+D800 to U+DFFF. Such characters are not valid in UTF-8  strings  (see
3645         RFC 3629) and so cannot be tested by PCRE, unless UTF-8 validity check-         RFC 3629) and so cannot be tested by PCRE, unless UTF-8 validity check-
3646         ing has been turned off (see the discussion  of  PCRE_NO_UTF8_CHECK  in         ing has been turned off (see the discussion  of  PCRE_NO_UTF8_CHECK  in
3647         the pcreapi page).         the pcreapi page). Perl does not support the Cs property.
3648    
3649         The  long  synonyms  for  these  properties that Perl supports (such as         The  long  synonyms  for  property  names  that  Perl supports (such as
3650         \p{Letter}) are not supported by PCRE, nor is it  permitted  to  prefix         \p{Letter}) are not supported by PCRE, nor is it  permitted  to  prefix
3651         any of these properties with "Is".         any of these properties with "Is".
3652    
# Line 3456  BACKSLASH Line 3672  BACKSLASH
3672         Matching characters by Unicode property is not fast, because  PCRE  has         Matching characters by Unicode property is not fast, because  PCRE  has
3673         to  search  a  structure  that  contains data for over fifteen thousand         to  search  a  structure  that  contains data for over fifteen thousand
3674         characters. That is why the traditional escape sequences such as \d and         characters. That is why the traditional escape sequences such as \d and
3675         \w do not use Unicode properties in PCRE.         \w  do  not  use  Unicode properties in PCRE by default, though you can
3676           make them do so by setting the PCRE_UCP option for pcre_compile() or by
3677           starting the pattern with (*UCP).
3678    
3679       PCRE's additional properties
3680    
3681           As  well  as  the standard Unicode properties described in the previous
3682           section, PCRE supports four more that make it possible to convert  tra-
3683           ditional escape sequences such as \w and \s and POSIX character classes
3684           to use Unicode properties. PCRE uses these non-standard, non-Perl prop-
3685           erties internally when PCRE_UCP is set. They are:
3686    
3687             Xan   Any alphanumeric character
3688             Xps   Any POSIX space character
3689             Xsp   Any Perl space character
3690             Xwd   Any Perl "word" character
3691    
3692           Xan  matches  characters that have either the L (letter) or the N (num-
3693           ber) property. Xps matches the characters tab, linefeed, vertical  tab,
3694           formfeed,  or  carriage  return, and any other character that has the Z
3695           (separator) property.  Xsp is the same as Xps, except that vertical tab
3696           is excluded. Xwd matches the same characters as Xan, plus underscore.
3697    
3698     Resetting the match start     Resetting the match start
3699    
# Line 3477  BACKSLASH Line 3714  BACKSLASH
3714    
3715         matches "foobar", the first substring is still set to "foo".         matches "foobar", the first substring is still set to "foo".
3716    
3717           Perl documents that the use  of  \K  within  assertions  is  "not  well
3718           defined".  In  PCRE,  \K  is  acted upon when it occurs inside positive
3719           assertions, but is ignored in negative assertions.
3720    
3721     Simple assertions     Simple assertions
3722    
3723         The final use of backslash is for certain simple assertions. An  asser-         The final use of backslash is for certain simple assertions. An  asser-
# Line 3493  BACKSLASH Line 3734  BACKSLASH
3734           \z     matches only at the end of the subject           \z     matches only at the end of the subject
3735           \G     matches at the first matching position in the subject           \G     matches at the first matching position in the subject
3736    
3737         These assertions may not appear in character classes (but note that  \b         Inside a character class, \b has a different meaning;  it  matches  the
3738         has a different meaning, namely the backspace character, inside a char-         backspace  character.  If  any  other  of these assertions appears in a
3739         acter class).         character class, by default it matches the corresponding literal  char-
3740           acter  (for  example,  \B  matches  the  letter  B).  However,  if  the
3741         A word boundary is a position in the subject string where  the  current         PCRE_EXTRA option is set, an "invalid escape sequence" error is  gener-
3742         character  and  the previous character do not both match \w or \W (i.e.         ated instead.
3743         one matches \w and the other matches \W), or the start or  end  of  the  
3744         string if the first or last character matches \w, respectively.         A  word  boundary is a position in the subject string where the current
3745           character and the previous character do not both match \w or  \W  (i.e.
3746           one  matches  \w  and the other matches \W), or the start or end of the
3747           string if the first or last  character  matches  \w,  respectively.  In
3748           UTF-8  mode,  the  meanings  of \w and \W can be changed by setting the
3749           PCRE_UCP option. When this is done, it also affects \b and \B.  Neither
3750           PCRE  nor  Perl has a separate "start of word" or "end of word" metase-
3751           quence. However, whatever follows \b normally determines which  it  is.
3752           For example, the fragment \ba matches "a" at the start of a word.
3753    
3754         The  \A,  \Z,  and \z assertions differ from the traditional circumflex         The  \A,  \Z,  and \z assertions differ from the traditional circumflex
3755         and dollar (described in the next section) in that they only ever match         and dollar (described in the next section) in that they only ever match
# Line 3583  CIRCUMFLEX AND DOLLAR Line 3832  CIRCUMFLEX AND DOLLAR
3832         set.         set.
3833    
3834    
3835  FULL STOP (PERIOD, DOT)  FULL STOP (PERIOD, DOT) AND \N
3836    
3837         Outside a character class, a dot in the pattern matches any one charac-         Outside a character class, a dot in the pattern matches any one charac-
3838         ter  in  the subject string except (by default) a character that signi-         ter  in  the subject string except (by default) a character that signi-
# Line 3606  FULL STOP (PERIOD, DOT) Line 3855  FULL STOP (PERIOD, DOT)
3855         flex and dollar, the only relationship being  that  they  both  involve         flex and dollar, the only relationship being  that  they  both  involve
3856         newlines. Dot has no special meaning in a character class.         newlines. Dot has no special meaning in a character class.
3857    
3858           The escape sequence \N always behaves as a dot does when PCRE_DOTALL is
3859           not set. In other words, it matches any one character except  one  that
3860           signifies the end of a line.
3861    
3862    
3863  MATCHING A SINGLE BYTE  MATCHING A SINGLE BYTE
3864    
# Line 3626  SQUARE BRACKETS AND CHARACTER CLASSES Line 3879  SQUARE BRACKETS AND CHARACTER CLASSES
3879    
3880         An opening square bracket introduces a character class, terminated by a         An opening square bracket introduces a character class, terminated by a
3881         closing square bracket. A closing square bracket on its own is not spe-         closing square bracket. A closing square bracket on its own is not spe-
3882         cial. If a closing square bracket is required as a member of the class,         cial by default.  However, if the PCRE_JAVASCRIPT_COMPAT option is set,
3883         it should be the first data character in the class  (after  an  initial         a lone closing square bracket causes a compile-time error. If a closing
3884         circumflex, if present) or escaped with a backslash.         square  bracket  is required as a member of the class, it should be the
3885           first data character in the class  (after  an  initial  circumflex,  if
3886           present) or escaped with a backslash.
3887    
3888         A  character  class matches a single character in the subject. In UTF-8         A  character  class matches a single character in the subject. In UTF-8
3889         mode, the character may occupy more than one byte. A matched  character         mode, the character may be more than one byte long. A matched character
3890         must be in the set of characters defined by the class, unless the first         must be in the set of characters defined by the class, unless the first
3891         character in the class definition is a circumflex, in  which  case  the         character in the class definition is a circumflex, in  which  case  the
3892         subject  character  must  not  be in the set defined by the class. If a         subject  character  must  not  be in the set defined by the class. If a
# Line 3642  SQUARE BRACKETS AND CHARACTER CLASSES Line 3897  SQUARE BRACKETS AND CHARACTER CLASSES
3897         while [^aeiou] matches any character that is not a  lower  case  vowel.         while [^aeiou] matches any character that is not a  lower  case  vowel.
3898         Note that a circumflex is just a convenient notation for specifying the         Note that a circumflex is just a convenient notation for specifying the
3899         characters that are in the class by enumerating those that are  not.  A         characters that are in the class by enumerating those that are  not.  A
3900         class  that starts with a circumflex is not an assertion: it still con-         class  that starts with a circumflex is not an assertion; it still con-
3901         sumes a character from the subject string, and therefore  it  fails  if         sumes a character from the subject string, and therefore  it  fails  if
3902         the current pointer is at the end of the string.         the current pointer is at the end of the string.
3903    
# Line 3658  SQUARE BRACKETS AND CHARACTER CLASSES Line 3913  SQUARE BRACKETS AND CHARACTER CLASSES
3913         than 128, so caseless matching is always possible. For characters  with         than 128, so caseless matching is always possible. For characters  with
3914         higher  values,  the  concept  of case is supported if PCRE is compiled         higher  values,  the  concept  of case is supported if PCRE is compiled
3915         with Unicode property support, but not otherwise.  If you want  to  use         with Unicode property support, but not otherwise.  If you want  to  use
3916         caseless  matching  for  characters 128 and above, you must ensure that         caseless  matching  in UTF8-mode for characters 128 and above, you must
3917         PCRE is compiled with Unicode property support as well  as  with  UTF-8         ensure that PCRE is compiled with Unicode property support as  well  as
3918         support.         with UTF-8 support.
3919    
3920         Characters  that  might  indicate  line breaks are never treated in any         Characters  that  might  indicate  line breaks are never treated in any
3921         special way  when  matching  character  classes,  whatever  line-ending         special way  when  matching  character  classes,  whatever  line-ending
# Line 3697  SQUARE BRACKETS AND CHARACTER CLASSES Line 3952  SQUARE BRACKETS AND CHARACTER CLASSES
3952         concept  of  case for characters with values greater than 128 only when         concept  of  case for characters with values greater than 128 only when
3953         it is compiled with Unicode property support.         it is compiled with Unicode property support.
3954    
3955         The character types \d, \D, \p, \P, \s, \S, \w, and \W may also  appear         The character types \d, \D, \h, \H, \p, \P, \s, \S, \v, \V, \w, and  \W
3956         in  a  character  class,  and add the characters that they match to the         may  also appear in a character class, and add the characters that they
3957         class. For example, [\dABCDEF] matches any hexadecimal digit. A circum-         match to the class. For example,  [\dABCDEF]  matches  any  hexadecimal
3958         flex  can  conveniently  be used with the upper case character types to         digit.  A circumflex can conveniently be used with the upper case char-
3959         specify a more restricted set of characters  than  the  matching  lower         acter types to specify a more restricted set  of  characters  than  the
3960         case  type.  For example, the class [^\W_] matches any letter or digit,         matching  lower  case  type.  For example, the class [^\W_] matches any
3961         but not underscore.         letter or digit, but not underscore.
3962    
3963         The only metacharacters that are recognized in  character  classes  are         The only metacharacters that are recognized in  character  classes  are
3964         backslash,  hyphen  (only  where  it can be interpreted as specifying a         backslash,  hyphen  (only  where  it can be interpreted as specifying a
# Line 3722  POSIX CHARACTER CLASSES Line 3977  POSIX CHARACTER CLASSES
3977           [01[:alpha:]%]           [01[:alpha:]%]
3978    
3979         matches "0", "1", any alphabetic character, or "%". The supported class         matches "0", "1", any alphabetic character, or "%". The supported class
3980         names are         names are:
3981    
3982           alnum    letters and digits           alnum    letters and digits
3983           alpha    letters           alpha    letters
# Line 3733  POSIX CHARACTER CLASSES Line 3988  POSIX CHARACTER CLASSES
3988           graph    printing characters, excluding space           graph    printing characters, excluding space
3989           lower    lower case letters           lower    lower case letters
3990           print    printing characters, including space           print    printing characters, including space
3991           punct    printing characters, excluding letters and digits           punct    printing characters, excluding letters and digits and space
3992           space    white space (not quite the same as \s)           space    white space (not quite the same as \s)
3993           upper    upper case letters           upper    upper case letters
3994           word     "word" characters (same as \w)           word     "word" characters (same as \w)
# Line 3754  POSIX CHARACTER CLASSES Line 4009  POSIX CHARACTER CLASSES
4009         POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but         POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but
4010         these are not supported, and an error is given if they are encountered.         these are not supported, and an error is given if they are encountered.
4011    
4012         In UTF-8 mode, characters with values greater than 128 do not match any         By  default,  in UTF-8 mode, characters with values greater than 128 do
4013         of the POSIX character classes.         not match any of the POSIX character classes. However, if the  PCRE_UCP
4014           option  is passed to pcre_compile(), some of the classes are changed so
4015           that Unicode character properties are used. This is achieved by replac-
4016           ing the POSIX classes by other sequences, as follows:
4017    
4018             [:alnum:]  becomes  \p{Xan}
4019             [:alpha:]  becomes  \p{L}
4020             [:blank:]  becomes  \h
4021             [:digit:]  becomes  \p{Nd}
4022             [:lower:]  becomes  \p{Ll}
4023             [:space:]  becomes  \p{Xps}
4024             [:upper:]  becomes  \p{Lu}
4025             [:word:]   becomes  \p{Xwd}
4026    
4027           Negated  versions,  such  as [:^alpha:] use \P instead of \p. The other
4028           POSIX classes are unchanged, and match only characters with code points
4029           less than 128.
4030    
4031    
4032  VERTICAL BAR  VERTICAL BAR
# Line 3770  VERTICAL BAR Line 4041  VERTICAL BAR
4041         string). The matching process tries each alternative in turn, from left         string). The matching process tries each alternative in turn, from left
4042         to  right, and the first one that succeeds is used. If the alternatives         to  right, and the first one that succeeds is used. If the alternatives
4043         are within a subpattern (defined below), "succeeds" means matching  the         are within a subpattern (defined below), "succeeds" means matching  the
4044         rest  of the main pattern as well as the alternative in the subpattern.         rest of the main pattern as well as the alternative in the subpattern.
4045    
4046    
4047  INTERNAL OPTION SETTING  INTERNAL OPTION SETTING
4048    
4049         The settings of the  PCRE_CASELESS,  PCRE_MULTILINE,  PCRE_DOTALL,  and         The  settings  of  the  PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
4050         PCRE_EXTENDED  options  (which are Perl-compatible) can be changed from         PCRE_EXTENDED options (which are Perl-compatible) can be  changed  from
4051         within the pattern by  a  sequence  of  Perl  option  letters  enclosed         within  the  pattern  by  a  sequence  of  Perl option letters enclosed
4052         between "(?" and ")".  The option letters are         between "(?" and ")".  The option letters are
4053    
4054           i  for PCRE_CASELESS           i  for PCRE_CASELESS
# Line 3787  INTERNAL OPTION SETTING Line 4058  INTERNAL OPTION SETTING
4058    
4059         For example, (?im) sets caseless, multiline matching. It is also possi-         For example, (?im) sets caseless, multiline matching. It is also possi-
4060         ble to unset these options by preceding the letter with a hyphen, and a         ble to unset these options by preceding the letter with a hyphen, and a
4061         combined  setting and unsetting such as (?im-sx), which sets PCRE_CASE-         combined setting and unsetting such as (?im-sx), which sets  PCRE_CASE-
4062         LESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and  PCRE_EXTENDED,         LESS  and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED,
4063         is  also  permitted.  If  a  letter  appears  both before and after the         is also permitted. If a  letter  appears  both  before  and  after  the
4064         hyphen, the option is unset.         hyphen, the option is unset.
4065    
4066         The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and  PCRE_EXTRA         The  PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA
4067         can  be changed in the same way as the Perl-compatible options by using         can be changed in the same way as the Perl-compatible options by  using
4068         the characters J, U and X respectively.         the characters J, U and X respectively.
4069    
4070         When an option change occurs at top level (that is, not inside  subpat-         When  one  of  these  option  changes occurs at top level (that is, not
4071         tern  parentheses),  the change applies to the remainder of the pattern         inside subpattern parentheses), the change applies to the remainder  of
4072         that follows.  If the change is placed right at the start of a pattern,         the pattern that follows. If the change is placed right at the start of
4073         PCRE extracts it into the global options (and it will therefore show up         a pattern, PCRE extracts it into the global options (and it will there-
4074         in data extracted by the pcre_fullinfo() function).         fore show up in data extracted by the pcre_fullinfo() function).
4075    
4076         An option change within a subpattern (see below for  a  description  of         An  option  change  within a subpattern (see below for a description of
4077         subpatterns) affects only that part of the current pattern that follows         subpatterns) affects only that part of the current pattern that follows
4078         it, so         it, so
4079    
4080           (a(?i)b)c           (a(?i)b)c
4081    
4082         matches abc and aBc and no other strings (assuming PCRE_CASELESS is not         matches abc and aBc and no other strings (assuming PCRE_CASELESS is not
4083         used).   By  this means, options can be made to have different settings         used).  By this means, options can be made to have  different  settings
4084         in different parts of the pattern. Any changes made in one  alternative         in  different parts of the pattern. Any changes made in one alternative
4085         do  carry  on  into subsequent branches within the same subpattern. For         do carry on into subsequent branches within the  same  subpattern.  For
4086         example,         example,
4087    
4088           (a(?i)b|c)           (a(?i)b|c)
4089    
4090         matches "ab", "aB", "c", and "C", even though  when  matching  "C"  the         matches  "ab",  "aB",  "c",  and "C", even though when matching "C" the
4091         first  branch  is  abandoned before the option setting. This is because         first branch is abandoned before the option setting.  This  is  because
4092         the effects of option settings happen at compile time. There  would  be         the  effects  of option settings happen at compile time. There would be
4093         some very weird behaviour otherwise.         some very weird behaviour otherwise.
4094    
4095         Note:  There  are  other  PCRE-specific  options that can be set by the         Note: There are other PCRE-specific options that  can  be  set  by  the
4096         application when the compile or match functions  are  called.  In  some         application  when  the  compile  or match functions are called. In some
4097         cases  the  pattern  can  contain special leading sequences to override         cases the pattern can contain special leading sequences such as (*CRLF)
4098         what the application has set or what has been  defaulted.  Details  are         to  override  what  the application has set or what has been defaulted.
4099         given in the section entitled "Newline sequences" above.         Details are given in the section entitled  "Newline  sequences"  above.
4100           There  are  also  the  (*UTF8) and (*UCP) leading sequences that can be
4101           used to set UTF-8 and Unicode property modes; they  are  equivalent  to
4102           setting the PCRE_UTF8 and the PCRE_UCP options, respectively.
4103    
4104    
4105  SUBPATTERNS  SUBPATTERNS
# Line 3907  DUPLICATE SUBPATTERN NUMBERS Line 4181  DUPLICATE SUBPATTERN NUMBERS
4181           / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x           / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
4182           # 1            2         2  3        2     3     4           # 1            2         2  3        2     3     4
4183    
4184         A backreference or a recursive call to  a  numbered  subpattern  always         A back reference to a numbered subpattern uses the  most  recent  value
4185         refers to the first one in the pattern with the given number.         that  is  set  for that number by any subpattern. The following pattern
4186           matches "abcabc" or "defdef":
4187    
4188             /(?|(abc)|(def))\1/
4189    
4190           In contrast, a recursive or "subroutine" call to a numbered  subpattern
4191           always  refers  to  the first one in the pattern with the given number.
4192           The following pattern matches "abcabc" or "defabc":
4193    
4194         An  alternative approach to using this "branch reset" feature is to use           /(?|(abc)|(def))(?1)/
4195    
4196           If a condition test for a subpattern's having matched refers to a  non-
4197           unique  number, the test is true if any of the subpatterns of that num-
4198           ber have matched.
4199    
4200           An alternative approach to using this "branch reset" feature is to  use
4201         duplicate named subpatterns, as described in the next section.         duplicate named subpatterns, as described in the next section.
4202    
4203    
4204  NAMED SUBPATTERNS  NAMED SUBPATTERNS
4205    
4206         Identifying capturing parentheses by number is simple, but  it  can  be         Identifying  capturing  parentheses  by number is simple, but it can be
4207         very  hard  to keep track of the numbers in complicated regular expres-         very hard to keep track of the numbers in complicated  regular  expres-
4208         sions. Furthermore, if an  expression  is  modified,  the  numbers  may         sions.  Furthermore,  if  an  expression  is  modified, the numbers may
4209         change.  To help with this difficulty, PCRE supports the naming of sub-         change. To help with this difficulty, PCRE supports the naming of  sub-
4210         patterns. This feature was not added to Perl until release 5.10. Python         patterns. This feature was not added to Perl until release 5.10. Python
4211         had  the  feature earlier, and PCRE introduced it at release 4.0, using         had the feature earlier, and PCRE introduced it at release  4.0,  using
4212         the Python syntax. PCRE now supports both the Perl and the Python  syn-         the  Python syntax. PCRE now supports both the Perl and the Python syn-
4213         tax.         tax. Perl allows identically numbered  subpatterns  to  have  different
4214           names, but PCRE does not.
4215    
4216         In  PCRE,  a subpattern can be named in one of three ways: (?<name>...)         In  PCRE,  a subpattern can be named in one of three ways: (?<name>...)
4217         or (?'name'...) as in Perl, or (?P<name>...) as in  Python.  References         or (?'name'...) as in Perl, or (?P<name>...) as in  Python.  References
4218         to capturing parentheses from other parts of the pattern, such as back-         to  capturing parentheses from other parts of the pattern, such as back
4219         references, recursion, and conditions, can be made by name as  well  as         references, recursion, and conditions, can be made by name as  well  as
4220         by number.         by number.
4221    
# Line 3940  NAMED SUBPATTERNS Line 4228  NAMED SUBPATTERNS
4228    
4229         By default, a name must be unique within a pattern, but it is  possible         By default, a name must be unique within a pattern, but it is  possible
4230         to relax this constraint by setting the PCRE_DUPNAMES option at compile         to relax this constraint by setting the PCRE_DUPNAMES option at compile
4231         time. This can be useful for patterns where only one  instance  of  the         time. (Duplicate names are also always permitted for  subpatterns  with
4232           the  same  number, set up as described in the previous section.) Dupli-
4233           cate names can be useful for patterns where only one  instance  of  the
4234         named  parentheses  can  match. Suppose you want to match the name of a         named  parentheses  can  match. Suppose you want to match the name of a
4235         weekday, either as a 3-letter abbreviation or as the full name, and  in         weekday, either as a 3-letter abbreviation or as the full name, and  in
4236         both cases you want to extract the abbreviation. This pattern (ignoring         both cases you want to extract the abbreviation. This pattern (ignoring
# Line 3959  NAMED SUBPATTERNS Line 4249  NAMED SUBPATTERNS
4249         The convenience function for extracting the data by  name  returns  the         The convenience function for extracting the data by  name  returns  the
4250         substring  for  the first (and in this example, the only) subpattern of         substring  for  the first (and in this example, the only) subpattern of
4251         that name that matched. This saves searching  to  find  which  numbered         that name that matched. This saves searching  to  find  which  numbered
4252         subpattern  it  was. If you make a reference to a non-unique named sub-         subpattern it was.
4253         pattern from elsewhere in the pattern, the one that corresponds to  the  
4254         lowest  number  is used. For further details of the interfaces for han-         If  you  make  a  back  reference to a non-unique named subpattern from
4255         dling named subpatterns, see the pcreapi documentation.         elsewhere in the pattern, the one that corresponds to the first  occur-
4256           rence of the name is used. In the absence of duplicate numbers (see the
4257           previous section) this is the one with the lowest number. If you use  a
4258           named  reference  in a condition test (see the section about conditions
4259           below), either to check whether a subpattern has matched, or  to  check
4260           for  recursion,  all  subpatterns with the same name are tested. If the
4261           condition is true for any one of them, the overall condition  is  true.
4262           This is the same behaviour as testing by number. For further details of
4263           the interfaces for handling named subpatterns, see the pcreapi documen-
4264           tation.
4265    
4266           Warning: You cannot use different names to distinguish between two sub-
4267           patterns with the same number because PCRE uses only the  numbers  when
4268           matching. For this reason, an error is given at compile time if differ-
4269           ent names are given to subpatterns with the same number.  However,  you
4270           can  give  the same name to subpatterns with the same number, even when
4271           PCRE_DUPNAMES is not set.
4272    
4273    
4274  REPETITION  REPETITION
# Line 3979  REPETITION Line 4285  REPETITION
4285           a character class           a character class
4286           a back reference (see next section)           a back reference (see next section)
4287           a parenthesized subpattern (unless it is an assertion)           a parenthesized subpattern (unless it is an assertion)
4288             a recursive or "subroutine" call to a subpattern
4289    
4290         The  general repetition quantifier specifies a minimum and maximum num-         The  general repetition quantifier specifies a minimum and maximum num-
4291         ber of permitted matches, by giving the two numbers in  curly  brackets         ber of permitted matches, by giving the two numbers in  curly  brackets
# Line 4004  REPETITION Line 4311  REPETITION
4311         the syntax of a quantifier, is taken as a literal character. For  exam-         the syntax of a quantifier, is taken as a literal character. For  exam-
4312         ple, {,6} is not a quantifier, but a literal string of four characters.         ple, {,6} is not a quantifier, but a literal string of four characters.
4313    
4314         In UTF-8 mode, quantifiers apply to UTF-8  characters  rather  than  to         In  UTF-8  mode,  quantifiers  apply to UTF-8 characters rather than to
4315         individual bytes. Thus, for example, \x{100}{2} matches two UTF-8 char-         individual bytes. Thus, for example, \x{100}{2} matches two UTF-8 char-
4316         acters, each of which is represented by a two-byte sequence. Similarly,         acters, each of which is represented by a two-byte sequence. Similarly,
4317         when Unicode property support is available, \X{3} matches three Unicode         when Unicode property support is available, \X{3} matches three Unicode
4318         extended sequences, each of which may be several bytes long  (and  they         extended  sequences,  each of which may be several bytes long (and they
4319         may be of different lengths).         may be of different lengths).
4320    
4321         The quantifier {0} is permitted, causing the expression to behave as if         The quantifier {0} is permitted, causing the expression to behave as if
4322         the previous item and the quantifier were not present. This may be use-         the previous item and the quantifier were not present. This may be use-
4323         ful  for  subpatterns that are referenced as subroutines from elsewhere         ful for subpatterns that are referenced as subroutines  from  elsewhere
4324         in the pattern. Items other than subpatterns that have a {0} quantifier         in the pattern. Items other than subpatterns that have a {0} quantifier
4325         are omitted from the compiled pattern.         are omitted from the compiled pattern.
4326    
4327         For  convenience, the three most common quantifiers have single-charac-         For convenience, the three most common quantifiers have  single-charac-
4328         ter abbreviations:         ter abbreviations:
4329    
4330           *    is equivalent to {0,}           *    is equivalent to {0,}
4331           +    is equivalent to {1,}           +    is equivalent to {1,}
4332           ?    is equivalent to {0,1}           ?    is equivalent to {0,1}
4333    
4334         It is possible to construct infinite loops by  following  a  subpattern         It  is  possible  to construct infinite loops by following a subpattern
4335         that can match no characters with a quantifier that has no upper limit,         that can match no characters with a quantifier that has no upper limit,
4336         for example:         for example:
4337    
4338           (a?)*           (a?)*
4339    
4340         Earlier versions of Perl and PCRE used to give an error at compile time         Earlier versions of Perl and PCRE used to give an error at compile time
4341         for  such  patterns. However, because there are cases where this can be         for such patterns. However, because there are cases where this  can  be
4342         useful, such patterns are now accepted, but if any  repetition  of  the         useful,  such  patterns  are now accepted, but if any repetition of the
4343         subpattern  does in fact match no characters, the loop is forcibly bro-         subpattern does in fact match no characters, the loop is forcibly  bro-
4344         ken.         ken.
4345    
4346         By default, the quantifiers are "greedy", that is, they match  as  much         By  default,  the quantifiers are "greedy", that is, they match as much
4347         as  possible  (up  to  the  maximum number of permitted times), without         as possible (up to the maximum  number  of  permitted  times),  without
4348         causing the rest of the pattern to fail. The classic example  of  where         causing  the  rest of the pattern to fail. The classic example of where
4349         this gives problems is in trying to match comments in C programs. These         this gives problems is in trying to match comments in C programs. These
4350         appear between /* and */ and within the comment,  individual  *  and  /         appear  between  /*  and  */ and within the comment, individual * and /
4351         characters  may  appear. An attempt to match C comments by applying the         characters may appear. An attempt to match C comments by  applying  the
4352         pattern         pattern
4353    
4354           /\*.*\*/           /\*.*\*/
# Line 4050  REPETITION Line 4357  REPETITION
4357    
4358           /* first comment */  not comment  /* second comment */           /* first comment */  not comment  /* second comment */
4359    
4360         fails, because it matches the entire string owing to the greediness  of         fails,  because it matches the entire string owing to the greediness of
4361         the .*  item.         the .*  item.
4362    
4363         However,  if  a quantifier is followed by a question mark, it ceases to         However, if a quantifier is followed by a question mark, it  ceases  to
4364         be greedy, and instead matches the minimum number of times possible, so         be greedy, and instead matches the minimum number of times possible, so
4365         the pattern         the pattern
4366    
4367           /\*.*?\*/           /\*.*?\*/
4368    
4369         does  the  right  thing with the C comments. The meaning of the various         does the right thing with the C comments. The meaning  of  the  various
4370         quantifiers is not otherwise changed,  just  the  preferred  number  of         quantifiers  is  not  otherwise  changed,  just the preferred number of
4371         matches.   Do  not  confuse this use of question mark with its use as a         matches.  Do not confuse this use of question mark with its  use  as  a
4372         quantifier in its own right. Because it has two uses, it can  sometimes         quantifier  in its own right. Because it has two uses, it can sometimes
4373         appear doubled, as in         appear doubled, as in
4374    
4375           \d??\d           \d??\d
# Line 4070  REPETITION Line 4377  REPETITION
4377         which matches one digit by preference, but can match two if that is the         which matches one digit by preference, but can match two if that is the
4378         only way the rest of the pattern matches.         only way the rest of the pattern matches.
4379    
4380         If the PCRE_UNGREEDY option is set (an option that is not available  in         If  the PCRE_UNGREEDY option is set (an option that is not available in
4381         Perl),  the  quantifiers are not greedy by default, but individual ones         Perl), the quantifiers are not greedy by default, but  individual  ones
4382         can be made greedy by following them with a  question  mark.  In  other         can  be  made  greedy  by following them with a question mark. In other
4383         words, it inverts the default behaviour.         words, it inverts the default behaviour.
4384    
4385         When  a  parenthesized  subpattern  is quantified with a minimum repeat         When a parenthesized subpattern is quantified  with  a  minimum  repeat
4386         count that is greater than 1 or with a limited maximum, more memory  is         count  that is greater than 1 or with a limited maximum, more memory is
4387         required  for  the  compiled  pattern, in proportion to the size of the         required for the compiled pattern, in proportion to  the  size  of  the
4388         minimum or maximum.         minimum or maximum.
4389    
4390         If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv-         If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv-
4391         alent  to  Perl's  /s) is set, thus allowing the dot to match newlines,         alent to Perl's /s) is set, thus allowing the dot  to  match  newlines,
4392         the pattern is implicitly anchored, because whatever  follows  will  be         the  pattern  is  implicitly anchored, because whatever follows will be
4393         tried  against every character position in the subject string, so there         tried against every character position in the subject string, so  there
4394         is no point in retrying the overall match at  any  position  after  the         is  no  point  in  retrying the overall match at any position after the
4395         first.  PCRE  normally treats such a pattern as though it were preceded         first. PCRE normally treats such a pattern as though it  were  preceded
4396         by \A.         by \A.
4397    
4398         In cases where it is known that the subject  string  contains  no  new-         In  cases  where  it  is known that the subject string contains no new-
4399         lines,  it  is  worth setting PCRE_DOTALL in order to obtain this opti-         lines, it is worth setting PCRE_DOTALL in order to  obtain  this  opti-
4400         mization, or alternatively using ^ to indicate anchoring explicitly.         mization, or alternatively using ^ to indicate anchoring explicitly.
4401    
4402         However, there is one situation where the optimization cannot be  used.         However,  there is one situation where the optimization cannot be used.
4403         When  .*   is  inside  capturing  parentheses that are the subject of a         When .*  is inside capturing parentheses that are the subject of a back
4404         backreference elsewhere in the pattern, a match at the start  may  fail         reference elsewhere in the pattern, a match at the start may fail where
4405         where a later one succeeds. Consider, for example:         a later one succeeds. Consider, for example:
4406    
4407           (.*)abc\1           (.*)abc\1
4408    
4409         If  the subject is "xyz123abc123" the match point is the fourth charac-         If the subject is "xyz123abc123" the match point is the fourth  charac-
4410         ter. For this reason, such a pattern is not implicitly anchored.         ter. For this reason, such a pattern is not implicitly anchored.
4411    
4412         When a capturing subpattern is repeated, the value captured is the sub-         When a capturing subpattern is repeated, the value captured is the sub-
# Line 4108  REPETITION Line 4415  REPETITION
4415           (tweedle[dume]{3}\s*)+           (tweedle[dume]{3}\s*)+
4416    
4417         has matched "tweedledum tweedledee" the value of the captured substring         has matched "tweedledum tweedledee" the value of the captured substring
4418         is "tweedledee". However, if there are  nested  capturing  subpatterns,         is  "tweedledee".  However,  if there are nested capturing subpatterns,
4419         the  corresponding captured values may have been set in previous itera-         the corresponding captured values may have been set in previous  itera-
4420         tions. For example, after         tions. For example, after
4421    
4422           /(a|(b))+/           /(a|(b))+/
# Line 4119  REPETITION Line 4426  REPETITION
4426    
4427  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS
4428    
4429         With both maximizing ("greedy") and minimizing ("ungreedy"  or  "lazy")         With  both  maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
4430         repetition,  failure  of what follows normally causes the repeated item         repetition, failure of what follows normally causes the  repeated  item
4431         to be re-evaluated to see if a different number of repeats  allows  the         to  be  re-evaluated to see if a different number of repeats allows the
4432         rest  of  the pattern to match. Sometimes it is useful to prevent this,         rest of the pattern to match. Sometimes it is useful to  prevent  this,
4433         either to change the nature of the match, or to cause it  fail  earlier         either  to  change the nature of the match, or to cause it fail earlier
4434         than  it otherwise might, when the author of the pattern knows there is         than it otherwise might, when the author of the pattern knows there  is
4435         no point in carrying on.         no point in carrying on.
4436    
4437         Consider, for example, the pattern \d+foo when applied to  the  subject         Consider,  for  example, the pattern \d+foo when applied to the subject
4438         line         line
4439    
4440           123456bar           123456bar
4441    
4442         After matching all 6 digits and then failing to match "foo", the normal         After matching all 6 digits and then failing to match "foo", the normal
4443         action of the matcher is to try again with only 5 digits  matching  the         action  of  the matcher is to try again with only 5 digits matching the
4444         \d+  item,  and  then  with  4,  and  so on, before ultimately failing.         \d+ item, and then with  4,  and  so  on,  before  ultimately  failing.
4445         "Atomic grouping" (a term taken from Jeffrey  Friedl's  book)  provides         "Atomic  grouping"  (a  term taken from Jeffrey Friedl's book) provides
4446         the  means for specifying that once a subpattern has matched, it is not         the means for specifying that once a subpattern has matched, it is  not
4447         to be re-evaluated in this way.         to be re-evaluated in this way.
4448    
4449         If we use atomic grouping for the previous example, the  matcher  gives         If  we  use atomic grouping for the previous example, the matcher gives
4450         up  immediately  on failing to match "foo" the first time. The notation         up immediately on failing to match "foo" the first time.  The  notation
4451         is a kind of special parenthesis, starting with (?> as in this example:         is a kind of special parenthesis, starting with (?> as in this example:
4452    
4453           (?>\d+)foo           (?>\d+)foo
# Line 4218  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE Line 4525  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE
4525    
4526           ((?>\D+)|<\d+>)*[!?]           ((?>\D+)|<\d+>)*[!?]
4527    
4528         sequences of non-digits cannot be broken, and failure happens  quickly.         sequences of non-digits cannot be broken, and failure happens quickly.
4529    
4530    
4531  BACK REFERENCES  BACK REFERENCES
4532    
4533         Outside a character class, a backslash followed by a digit greater than         Outside a character class, a backslash followed by a digit greater than
4534         0 (and possibly further digits) is a back reference to a capturing sub-         0 (and possibly further digits) is a back reference to a capturing sub-
4535         pattern  earlier  (that is, to its left) in the pattern, provided there         pattern earlier (that is, to its left) in the pattern,  provided  there
4536         have been that many previous capturing left parentheses.         have been that many previous capturing left parentheses.
4537    
4538         However, if the decimal number following the backslash is less than 10,         However, if the decimal number following the backslash is less than 10,
4539         it  is  always  taken  as a back reference, and causes an error only if         it is always taken as a back reference, and causes  an  error  only  if
4540         there are not that many capturing left parentheses in the  entire  pat-         there  are  not that many capturing left parentheses in the entire pat-
4541         tern.  In  other words, the parentheses that are referenced need not be         tern. In other words, the parentheses that are referenced need  not  be
4542         to the left of the reference for numbers less than 10. A "forward  back         to  the left of the reference for numbers less than 10. A "forward back
4543         reference"  of  this  type can make sense when a repetition is involved         reference" of this type can make sense when a  repetition  is  involved
4544         and the subpattern to the right has participated in an  earlier  itera-         and  the  subpattern to the right has participated in an earlier itera-
4545         tion.         tion.
4546    
4547         It  is  not  possible to have a numerical "forward back reference" to a         It is not possible to have a numerical "forward back  reference"  to  a
4548         subpattern whose number is 10 or  more  using  this  syntax  because  a         subpattern  whose  number  is  10  or  more using this syntax because a
4549         sequence  such  as  \50 is interpreted as a character defined in octal.         sequence such as \50 is interpreted as a character  defined  in  octal.
4550         See the subsection entitled "Non-printing characters" above for further         See the subsection entitled "Non-printing characters" above for further
4551         details  of  the  handling of digits following a backslash. There is no         details of the handling of digits following a backslash.  There  is  no
4552         such problem when named parentheses are used. A back reference  to  any         such  problem  when named parentheses are used. A back reference to any
4553         subpattern is possible using named parentheses (see below).         subpattern is possible using named parentheses (see below).
4554    
4555         Another  way  of  avoiding  the ambiguity inherent in the use of digits         Another way of avoiding the ambiguity inherent in  the  use  of  digits
4556         following a backslash is to use the \g escape sequence, which is a fea-         following a backslash is to use the \g escape sequence, which is a fea-
4557         ture  introduced  in  Perl  5.10.  This  escape  must be followed by an         ture introduced in Perl 5.10.  This  escape  must  be  followed  by  an
4558         unsigned number or a negative number, optionally  enclosed  in  braces.         unsigned  number  or  a negative number, optionally enclosed in braces.
4559         These examples are all identical:         These examples are all identical:
4560    
4561           (ring), \1           (ring), \1
4562           (ring), \g1           (ring), \g1
4563           (ring), \g{1}           (ring), \g{1}
4564    
4565         An  unsigned number specifies an absolute reference without the ambigu-         An unsigned number specifies an absolute reference without the  ambigu-
4566         ity that is present in the older syntax. It is also useful when literal         ity that is present in the older syntax. It is also useful when literal
4567         digits follow the reference. A negative number is a relative reference.         digits follow the reference. A negative number is a relative reference.
4568         Consider this example:         Consider this example:
# Line 4263  BACK REFERENCES Line 4570  BACK REFERENCES
4570           (abc(def)ghi)\g{-1}           (abc(def)ghi)\g{-1}
4571    
4572         The sequence \g{-1} is a reference to the most recently started captur-         The sequence \g{-1} is a reference to the most recently started captur-
4573         ing  subpattern  before \g, that is, is it equivalent to \2. Similarly,         ing subpattern before \g, that is, is it equivalent to  \2.  Similarly,
4574         \g{-2} would be equivalent to \1. The use of relative references can be         \g{-2} would be equivalent to \1. The use of relative references can be
4575         helpful  in  long  patterns,  and  also in patterns that are created by         helpful in long patterns, and also in  patterns  that  are  created  by
4576         joining together fragments that contain references within themselves.         joining together fragments that contain references within themselves.
4577    
4578         A back reference matches whatever actually matched the  capturing  sub-         A  back  reference matches whatever actually matched the capturing sub-
4579         pattern  in  the  current subject string, rather than anything matching         pattern in the current subject string, rather  than  anything  matching
4580         the subpattern itself (see "Subpatterns as subroutines" below for a way         the subpattern itself (see "Subpatterns as subroutines" below for a way
4581         of doing that). So the pattern         of doing that). So the pattern
4582    
4583           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
4584    
4585         matches  "sense and sensibility" and "response and responsibility", but         matches "sense and sensibility" and "response and responsibility",  but
4586         not "sense and responsibility". If caseful matching is in force at  the         not  "sense and responsibility". If caseful matching is in force at the
4587         time  of the back reference, the case of letters is relevant. For exam-         time of the back reference, the case of letters is relevant. For  exam-
4588         ple,         ple,
4589    
4590           ((?i)rah)\s+\1           ((?i)rah)\s+\1
4591    
4592         matches "rah rah" and "RAH RAH", but not "RAH  rah",  even  though  the         matches  "rah  rah"  and  "RAH RAH", but not "RAH rah", even though the
4593         original capturing subpattern is matched caselessly.         original capturing subpattern is matched caselessly.
4594    
4595         There  are  several  different ways of writing back references to named         There are several different ways of writing back  references  to  named
4596         subpatterns. The .NET syntax \k{name} and the Perl syntax  \k<name>  or         subpatterns.  The  .NET syntax \k{name} and the Perl syntax \k<name> or
4597         \k'name'  are supported, as is the Python syntax (?P=name). Perl 5.10's         \k'name' are supported, as is the Python syntax (?P=name). Perl  5.10's
4598         unified back reference syntax, in which \g can be used for both numeric         unified back reference syntax, in which \g can be used for both numeric
4599         and  named  references,  is  also supported. We could rewrite the above         and named references, is also supported. We  could  rewrite  the  above
4600         example in any of the following ways:         example in any of the following ways:
4601    
4602           (?<p1>(?i)rah)\s+\k<p1>           (?<p1>(?i)rah)\s+\k<p1>
# Line 4297  BACK REFERENCES Line 4604  BACK REFERENCES
4604           (?P<p1>(?i)rah)\s+(?P=p1)           (?P<p1>(?i)rah)\s+(?P=p1)
4605           (?<p1>(?i)rah)\s+\g{p1}           (?<p1>(?i)rah)\s+\g{p1}
4606    
4607         A subpattern that is referenced by  name  may  appear  in  the  pattern         A  subpattern  that  is  referenced  by  name may appear in the pattern
4608         before or after the reference.         before or after the reference.
4609    
4610         There  may be more than one back reference to the same subpattern. If a         There may be more than one back reference to the same subpattern. If  a
4611         subpattern has not actually been used in a particular match,  any  back         subpattern  has  not actually been used in a particular match, any back
4612         references to it always fail. For example, the pattern         references to it always fail by default. For example, the pattern
4613    
4614           (a|(bc))\2           (a|(bc))\2
4615    
4616         always  fails if it starts to match "a" rather than "bc". Because there         always fails if it starts to match "a" rather than  "bc".  However,  if
4617         may be many capturing parentheses in a pattern,  all  digits  following         the PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back refer-
4618         the  backslash  are taken as part of a potential back reference number.         ence to an unset value matches an empty string.
4619         If the pattern continues with a digit character, some delimiter must be  
4620         used  to  terminate  the back reference. If the PCRE_EXTENDED option is         Because there may be many capturing parentheses in a pattern, all  dig-
4621         set, this can be whitespace.  Otherwise an  empty  comment  (see  "Com-         its  following a backslash are taken as part of a potential back refer-
4622         ments" below) can be used.         ence number.  If the pattern continues with  a  digit  character,  some
4623           delimiter  must  be  used  to  terminate  the  back  reference.  If the
4624           PCRE_EXTENDED option is set, this can be whitespace. Otherwise, the \g{
4625           syntax or an empty comment (see "Comments" below) can be used.
4626    
4627       Recursive back references
4628    
4629         A  back reference that occurs inside the parentheses to which it refers         A  back reference that occurs inside the parentheses to which it refers
4630         fails when the subpattern is first used, so, for example,  (a\1)  never         fails when the subpattern is first used, so, for example,  (a\1)  never
# Line 4328  BACK REFERENCES Line 4640  BACK REFERENCES
4640         to match the back reference. This can be done using alternation, as  in         to match the back reference. This can be done using alternation, as  in
4641         the example above, or by a quantifier with a minimum of zero.         the example above, or by a quantifier with a minimum of zero.
4642    
4643           Back  references of this type cause the group that they reference to be
4644           treated as an atomic group.  Once the whole group has been  matched,  a
4645           subsequent  matching  failure cannot cause backtracking into the middle
4646           of the group.
4647    
4648    
4649  ASSERTIONS  ASSERTIONS
4650    
4651         An  assertion  is  a  test on the characters following or preceding the         An assertion is a test on the characters  following  or  preceding  the
4652         current matching point that does not actually consume  any  characters.         current  matching  point that does not actually consume any characters.
4653         The  simple  assertions  coded  as  \b, \B, \A, \G, \Z, \z, ^ and $ are         The simple assertions coded as \b, \B, \A, \G, \Z,  \z,  ^  and  $  are
4654         described above.         described above.
4655    
4656         More complicated assertions are coded as  subpatterns.  There  are  two         More  complicated  assertions  are  coded as subpatterns. There are two
4657         kinds:  those  that  look  ahead of the current position in the subject         kinds: those that look ahead of the current  position  in  the  subject
4658         string, and those that look  behind  it.  An  assertion  subpattern  is         string,  and  those  that  look  behind  it. An assertion subpattern is
4659         matched  in  the  normal way, except that it does not cause the current         matched in the normal way, except that it does not  cause  the  current
4660         matching position to be changed.         matching position to be changed.
4661    
4662         Assertion subpatterns are not capturing subpatterns,  and  may  not  be         Assertion  subpatterns  are  not  capturing subpatterns, and may not be
4663         repeated,  because  it  makes no sense to assert the same thing several         repeated, because it makes no sense to assert the  same  thing  several
4664         times. If any kind of assertion contains capturing  subpatterns  within         times.  If  any kind of assertion contains capturing subpatterns within
4665         it,  these are counted for the purposes of numbering the capturing sub-         it, these are counted for the purposes of numbering the capturing  sub-
4666         patterns in the whole pattern.  However, substring capturing is carried         patterns in the whole pattern.  However, substring capturing is carried
4667         out  only  for  positive assertions, because it does not make sense for         out only for positive assertions, because it does not  make  sense  for
4668         negative assertions.         negative assertions.
4669    
4670     Lookahead assertions     Lookahead assertions
# Line 4357  ASSERTIONS Line 4674  ASSERTIONS
4674    
4675           \w+(?=;)           \w+(?=;)
4676    
4677         matches  a word followed by a semicolon, but does not include the semi-         matches a word followed by a semicolon, but does not include the  semi-
4678         colon in the match, and         colon in the match, and
4679    
4680           foo(?!bar)           foo(?!bar)
4681    
4682         matches any occurrence of "foo" that is not  followed  by  "bar".  Note         matches  any  occurrence  of  "foo" that is not followed by "bar". Note
4683         that the apparently similar pattern         that the apparently similar pattern
4684    
4685           (?!foo)bar           (?!foo)bar
4686    
4687         does  not  find  an  occurrence  of "bar" that is preceded by something         does not find an occurrence of "bar"  that  is  preceded  by  something
4688         other than "foo"; it finds any occurrence of "bar" whatsoever,  because         other  than "foo"; it finds any occurrence of "bar" whatsoever, because
4689         the assertion (?!foo) is always true when the next three characters are         the assertion (?!foo) is always true when the next three characters are
4690         "bar". A lookbehind assertion is needed to achieve the other effect.         "bar". A lookbehind assertion is needed to achieve the other effect.
4691    
4692         If you want to force a matching failure at some point in a pattern, the         If you want to force a matching failure at some point in a pattern, the
4693         most  convenient  way  to  do  it  is with (?!) because an empty string         most convenient way to do it is  with  (?!)  because  an  empty  string
4694         always matches, so an assertion that requires there not to be an  empty         always  matches, so an assertion that requires there not to be an empty
4695         string must always fail.         string must always fail.   The  Perl  5.10  backtracking  control  verb
4696           (*FAIL) or (*F) is essentially a synonym for (?!).
4697    
4698     Lookbehind assertions     Lookbehind assertions
4699    
# Line 4398  ASSERTIONS Line 4716  ASSERTIONS
4716    
4717         causes an error at compile time. Branches that match  different  length         causes an error at compile time. Branches that match  different  length
4718         strings  are permitted only at the top level of a lookbehind assertion.         strings  are permitted only at the top level of a lookbehind assertion.
4719         This is an extension compared with  Perl  (at  least  for  5.8),  which         This is an extension compared with Perl (5.8 and 5.10), which  requires
4720         requires  all branches to match the same length of string. An assertion         all branches to match the same length of string. An assertion such as
        such as  
4721    
4722           (?<=ab(c|de))           (?<=ab(c|de))
4723    
4724         is not permitted, because its single top-level  branch  can  match  two         is  not  permitted,  because  its single top-level branch can match two
4725         different  lengths,  but  it is acceptable if rewritten to use two top-         different lengths, but it is acceptable to PCRE if rewritten to use two
4726         level branches:         top-level branches:
4727    
4728           (?<=abc|abde)           (?<=abc|abde)
4729    
4730         In some cases, the Perl 5.10 escape sequence \K (see above) can be used         In some cases, the Perl 5.10 escape sequence \K (see above) can be used
4731         instead  of  a lookbehind assertion; this is not restricted to a fixed-         instead of  a  lookbehind  assertion  to  get  round  the  fixed-length
4732         length.         restriction.
4733    
4734         The implementation of lookbehind assertions is, for  each  alternative,         The  implementation  of lookbehind assertions is, for each alternative,
4735         to  temporarily  move the current position back by the fixed length and         to temporarily move the current position back by the fixed  length  and
4736         then try to match. If there are insufficient characters before the cur-         then try to match. If there are insufficient characters before the cur-
4737         rent position, the assertion fails.         rent position, the assertion fails.
4738    
4739         PCRE does not allow the \C escape (which matches a single byte in UTF-8         PCRE does not allow the \C escape (which matches a single byte in UTF-8
4740         mode) to appear in lookbehind assertions, because it makes it  impossi-         mode)  to appear in lookbehind assertions, because it makes it impossi-
4741         ble  to  calculate the length of the lookbehind. The \X and \R escapes,         ble to calculate the length of the lookbehind. The \X and  \R  escapes,
4742         which can match different numbers of bytes, are also not permitted.         which can match different numbers of bytes, are also not permitted.
4743    
4744         Possessive quantifiers can  be  used  in  conjunction  with  lookbehind         "Subroutine"  calls  (see below) such as (?2) or (?&X) are permitted in
4745         assertions  to  specify  efficient  matching  at the end of the subject         lookbehinds, as long as the subpattern matches a  fixed-length  string.
4746         string. Consider a simple pattern such as         Recursion, however, is not supported.
4747    
4748           Possessive  quantifiers  can  be  used  in  conjunction with lookbehind
4749           assertions to specify efficient matching of fixed-length strings at the
4750           end of subject strings. Consider a simple pattern such as
4751    
4752           abcd$           abcd$
4753    
4754         when applied to a long string that does  not  match.  Because  matching         when  applied  to  a  long string that does not match. Because matching
4755         proceeds from left to right, PCRE will look for each "a" in the subject         proceeds from left to right, PCRE will look for each "a" in the subject
4756         and then see if what follows matches the rest of the  pattern.  If  the         and  then  see  if what follows matches the rest of the pattern. If the
4757         pattern is specified as         pattern is specified as
4758    
4759           ^.*abcd$           ^.*abcd$
4760    
4761         the  initial .* matches the entire string at first, but when this fails         the initial .* matches the entire string at first, but when this  fails
4762         (because there is no following "a"), it backtracks to match all but the         (because there is no following "a"), it backtracks to match all but the
4763         last  character,  then all but the last two characters, and so on. Once         last character, then all but the last two characters, and so  on.  Once
4764         again the search for "a" covers the entire string, from right to  left,         again  the search for "a" covers the entire string, from right to left,