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

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

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

revision 172 by ph10, Tue Jun 5 10:40:13 2007 UTC revision 392 by ph10, Tue Mar 17 21:30:30 2009 UTC
# Line 18  INTRODUCTION Line 18  INTRODUCTION
18    
19         The  PCRE  library is a set of functions that implement regular expres-         The  PCRE  library is a set of functions that implement regular expres-
20         sion pattern matching using the same syntax and semantics as Perl, with         sion pattern matching using the same syntax and semantics as Perl, with
21         just  a  few differences. (Certain features that appeared in Python and         just  a  few  differences. Certain features that appeared in Python and
22         PCRE before they appeared in Perl are also available using  the  Python         PCRE before they appeared in Perl are also available using  the  Python
23         syntax.)         syntax.  There is also some support for certain .NET and Oniguruma syn-
24           tax items, and there is an option for  requesting  some  minor  changes
25           that give better JavaScript compatibility.
26    
27         The  current  implementation of PCRE (release 7.x) corresponds approxi-         The  current  implementation of PCRE (release 7.x) corresponds approxi-
28         mately with Perl 5.10, including support for UTF-8 encoded strings  and         mately with Perl 5.10, including support for UTF-8 encoded strings  and
# Line 45  INTRODUCTION Line 47  INTRODUCTION
47    
48         Details of exactly which Perl regular expression features are  and  are         Details of exactly which Perl regular expression features are  and  are
49         not supported by PCRE are given in separate documents. See the pcrepat-         not supported by PCRE are given in separate documents. See the pcrepat-
50         tern and pcrecompat pages.         tern and pcrecompat pages. There is a syntax summary in the  pcresyntax
51           page.
52    
53         Some features of PCRE can be included, excluded, or  changed  when  the         Some  features  of  PCRE can be included, excluded, or changed when the
54         library  is  built.  The pcre_config() function makes it possible for a         library is built. The pcre_config() function makes it  possible  for  a
55         client to discover which features are  available.  The  features  them-         client  to  discover  which  features are available. The features them-
56         selves  are described in the pcrebuild page. Documentation about build-         selves are described in the pcrebuild page. Documentation about  build-
57         ing PCRE for various operating systems can be found in the README  file         ing  PCRE for various operating systems can be found in the README file
58         in the source distribution.         in the source distribution.
59    
60         The  library  contains  a number of undocumented internal functions and         The library contains a number of undocumented  internal  functions  and
61         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
62         functions,  but  which  are  not  intended for use by external callers.         functions, but which are not intended  for  use  by  external  callers.
63         Their names all begin with "_pcre_", which hopefully will  not  provoke         Their  names  all begin with "_pcre_", which hopefully will not provoke
64         any name clashes. In some environments, it is possible to control which         any name clashes. In some environments, it is possible to control which
65         external symbols are exported when a shared library is  built,  and  in         external  symbols  are  exported when a shared library is built, and in
66         these cases the undocumented symbols are not exported.         these cases the undocumented symbols are not exported.
67    
68    
69  USER DOCUMENTATION  USER DOCUMENTATION
70    
71         The  user  documentation  for PCRE comprises a number of different sec-         The user documentation for PCRE comprises a number  of  different  sec-
72         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
73         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.
74         In the plain text format, all the sections are concatenated,  for  ease         In  the  plain text format, all the sections are concatenated, for ease
75         of searching. The sections are as follows:         of searching. The sections are as follows:
76    
77           pcre              this document           pcre              this document
# Line 83  USER DOCUMENTATION Line 86  USER DOCUMENTATION
86           pcrepartial       details of the partial matching facility           pcrepartial       details of the partial matching facility
87           pcrepattern       syntax and semantics of supported           pcrepattern       syntax and semantics of supported
88                               regular expressions                               regular expressions
89             pcresyntax        quick syntax reference
90           pcreperform       discussion of performance issues           pcreperform       discussion of performance issues
91           pcreposix         the POSIX-compatible C API           pcreposix         the POSIX-compatible C API
92           pcreprecompile    details of saving and re-using precompiled patterns           pcreprecompile    details of saving and re-using precompiled patterns
# Line 107  LIMITATIONS Line 111  LIMITATIONS
111         In  these  cases the limit is substantially larger.  However, the speed         In  these  cases the limit is substantially larger.  However, the speed
112         of execution is slower.         of execution is slower.
113    
114         All values in repeating quantifiers must be less than 65536. The  maxi-         All values in repeating quantifiers must be less than 65536.
        mum  compiled  length  of  subpattern  with an explicit repeat count is  
        30000 bytes. The maximum number of capturing subpatterns is 65535.  
115    
116         There is no limit to the number of parenthesized subpatterns, but there         There is no limit to the number of parenthesized subpatterns, but there
117         can be no more than 65535 capturing subpatterns.         can be no more than 65535 capturing subpatterns.
# Line 155  UTF-8 AND UNICODE PROPERTY SUPPORT Line 157  UTF-8 AND UNICODE PROPERTY SUPPORT
157         optionally be prefixed by "Is", for compatibility with Perl  5.6.  PCRE         optionally be prefixed by "Is", for compatibility with Perl  5.6.  PCRE
158         does not support this.         does not support this.
159    
160         The following comments apply when PCRE is running in UTF-8 mode:     Validity of UTF-8 strings
161    
162           When  you  set  the  PCRE_UTF8 flag, the strings passed as patterns and
163           subjects are (by default) checked for validity on entry to the relevant
164           functions.  From  release 7.3 of PCRE, the check is according the rules
165           of RFC 3629, which are themselves derived from the  Unicode  specifica-
166           tion.  Earlier  releases  of PCRE followed the rules of RFC 2279, which
167           allows the full range of 31-bit values (0 to 0x7FFFFFFF).  The  current
168           check allows only values in the range U+0 to U+10FFFF, excluding U+D800
169           to U+DFFF.
170    
171           The excluded code points are the "Low Surrogate Area"  of  Unicode,  of
172           which  the Unicode Standard says this: "The Low Surrogate Area does not
173           contain any  character  assignments,  consequently  no  character  code
174           charts or namelists are provided for this area. Surrogates are reserved
175           for use with UTF-16 and then must be used in pairs."  The  code  points
176           that  are  encoded  by  UTF-16  pairs are available as independent code
177           points in the UTF-8 encoding. (In  other  words,  the  whole  surrogate
178           thing is a fudge for UTF-16 which unfortunately messes up UTF-8.)
179    
180           If  an  invalid  UTF-8  string  is  passed  to  PCRE,  an  error return
181           (PCRE_ERROR_BADUTF8) is given. In some situations, you may already know
182           that your strings are valid, and therefore want to skip these checks in
183           order to improve performance. If you set the PCRE_NO_UTF8_CHECK flag at
184           compile  time  or at run time, PCRE assumes that the pattern or subject
185           it is given (respectively) contains only valid  UTF-8  codes.  In  this
186           case, it does not diagnose an invalid UTF-8 string.
187    
188           If  you  pass  an  invalid UTF-8 string when PCRE_NO_UTF8_CHECK is set,
189           what happens depends on why the string is invalid. If the  string  con-
190           forms to the "old" definition of UTF-8 (RFC 2279), it is processed as a
191           string of characters in the range 0  to  0x7FFFFFFF.  In  other  words,
192           apart from the initial validity test, PCRE (when in UTF-8 mode) handles
193           strings according to the more liberal rules of RFC  2279.  However,  if
194           the  string does not even conform to RFC 2279, the result is undefined.
195           Your program may crash.
196    
197           If you want to process strings  of  values  in  the  full  range  0  to
198           0x7FFFFFFF,  encoded in a UTF-8-like manner as per the old RFC, you can
199           set PCRE_NO_UTF8_CHECK to bypass the more restrictive test. However, in
200           this situation, you will have to apply your own validity check.
201    
202         1.  When you set the PCRE_UTF8 flag, the strings passed as patterns and     General comments about UTF-8 mode
        subjects are checked for validity on entry to the  relevant  functions.  
        If an invalid UTF-8 string is passed, an error return is given. In some  
        situations, you may already know  that  your  strings  are  valid,  and  
        therefore want to skip these checks in order to improve performance. If  
        you set the PCRE_NO_UTF8_CHECK flag at compile time  or  at  run  time,  
        PCRE  assumes  that  the  pattern or subject it is given (respectively)  
        contains only valid UTF-8 codes. In this case, it does not diagnose  an  
        invalid  UTF-8 string. If you pass an invalid UTF-8 string to PCRE when  
        PCRE_NO_UTF8_CHECK is set, the results are undefined. Your program  may  
        crash.  
203    
204         2.  An  unbraced  hexadecimal  escape sequence (such as \xb3) matches a         1.  An  unbraced  hexadecimal  escape sequence (such as \xb3) matches a
205         two-byte UTF-8 character if the value is greater than 127.         two-byte UTF-8 character if the value is greater than 127.
206    
207         3. Octal numbers up to \777 are recognized, and  match  two-byte  UTF-8         2. Octal numbers up to \777 are recognized, and  match  two-byte  UTF-8
208         characters for values greater than \177.         characters for values greater than \177.
209    
210         4.  Repeat quantifiers apply to complete UTF-8 characters, not to indi-         3.  Repeat quantifiers apply to complete UTF-8 characters, not to indi-
211         vidual bytes, for example: \x{100}{3}.         vidual bytes, for example: \x{100}{3}.
212    
213         5. The dot metacharacter matches one UTF-8 character instead of a  sin-         4. The dot metacharacter matches one UTF-8 character instead of a  sin-
214         gle byte.         gle byte.
215    
216         6.  The  escape sequence \C can be used to match a single byte in UTF-8         5.  The  escape sequence \C can be used to match a single byte in UTF-8
217         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
218         not available in the alternative matching function, pcre_dfa_exec().         not available in the alternative matching function, pcre_dfa_exec().
219    
220         7.  The  character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly         6.  The  character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly
221         test characters of any code value, but the characters that PCRE  recog-         test characters of any code value, but the characters that PCRE  recog-
222         nizes  as  digits,  spaces,  or  word characters remain the same set as         nizes  as  digits,  spaces,  or  word characters remain the same set as
223         before, all with values less than 256. This remains true even when PCRE         before, all with values less than 256. This remains true even when PCRE
# Line 194  UTF-8 AND UNICODE PROPERTY SUPPORT Line 226  UTF-8 AND UNICODE PROPERTY SUPPORT
226         sense  of,  say,  "digit",  you must use Unicode property tests such as         sense  of,  say,  "digit",  you must use Unicode property tests such as
227         \p{Nd}.         \p{Nd}.
228    
229         8. Similarly, characters that match the POSIX named  character  classes         7. Similarly, characters that match the POSIX named  character  classes
230         are all low-valued characters.         are all low-valued characters.
231    
232           8.  However,  the Perl 5.10 horizontal and vertical whitespace matching
233           escapes (\h, \H, \v, and \V) do match all the appropriate Unicode char-
234           acters.
235    
236         9.  Case-insensitive  matching  applies only to characters whose values         9.  Case-insensitive  matching  applies only to characters whose values
237         are less than 128, unless PCRE is built with Unicode property  support.         are less than 128, unless PCRE is built with Unicode property  support.
238         Even  when  Unicode  property support is available, PCRE still uses its         Even  when  Unicode  property support is available, PCRE still uses its
# Line 222  AUTHOR Line 258  AUTHOR
258    
259  REVISION  REVISION
260    
261         Last updated: 18 April 2007         Last updated: 12 April 2008
262         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2008 University of Cambridge.
263  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
264    
265    
# Line 237  NAME Line 273  NAME
273  PCRE BUILD-TIME OPTIONS  PCRE BUILD-TIME OPTIONS
274    
275         This  document  describes  the  optional  features  of PCRE that can be         This  document  describes  the  optional  features  of PCRE that can be
276         selected when the library is compiled. They are all selected, or  dese-         selected when the library is compiled. It assumes use of the  configure
277         lected, by providing options to the configure script that is run before         script,  where the optional features are selected or deselected by pro-
278         the make command. The complete list of  options  for  configure  (which         viding options to configure before running the make  command.  However,
279         includes  the  standard  ones such as the selection of the installation         the  same  options  can be selected in both Unix-like and non-Unix-like
280         directory) can be obtained by running         environments using the GUI facility of  CMakeSetup  if  you  are  using
281           CMake instead of configure to build PCRE.
282    
283           The complete list of options for configure (which includes the standard
284           ones such as the  selection  of  the  installation  directory)  can  be
285           obtained by running
286    
287           ./configure --help           ./configure --help
288    
289         The following sections include  descriptions  of  options  whose  names         The  following  sections  include  descriptions  of options whose names
290         begin with --enable or --disable. These settings specify changes to the         begin with --enable or --disable. These settings specify changes to the
291         defaults for the configure command. Because of the way  that  configure         defaults  for  the configure command. Because of the way that configure
292         works,  --enable  and --disable always come in pairs, so the complemen-         works, --enable and --disable always come in pairs, so  the  complemen-
293         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
294         is not described.         is not described.
295    
296    
# Line 266  C++ SUPPORT Line 307  C++ SUPPORT
307    
308  UTF-8 SUPPORT  UTF-8 SUPPORT
309    
310         To build PCRE with support for UTF-8 character strings, add         To build PCRE with support for UTF-8 Unicode character strings, add
311    
312           --enable-utf8           --enable-utf8
313    
314         to  the  configure  command.  Of  itself, this does not make PCRE treat         to the configure command. Of itself, this  does  not  make  PCRE  treat
315         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
316         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()
317         function.         function.
318    
319           If  you set --enable-utf8 when compiling in an EBCDIC environment, PCRE
320           expects its input to be either ASCII or UTF-8 (depending on the runtime
321           option).  It  is not possible to support both EBCDIC and UTF-8 codes in
322           the same  version  of  the  library.  Consequently,  --enable-utf8  and
323           --enable-ebcdic are mutually exclusive.
324    
325    
326  UNICODE CHARACTER PROPERTY SUPPORT  UNICODE CHARACTER PROPERTY SUPPORT
327    
328         UTF-8 support allows PCRE to process character values greater than  255         UTF-8  support allows PCRE to process character values greater than 255
329         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-
330         vide any facilities for accessing the properties of such characters. If         vide any facilities for accessing the properties of such characters. If
331         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
332         refer to Unicode character properties, you must add         refer to Unicode character properties, you must add
333    
334           --enable-unicode-properties           --enable-unicode-properties
335    
336         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
337         not explicitly requested it.         not explicitly requested it.
338    
339         Including  Unicode  property  support  adds around 30K of tables to the         Including Unicode property support adds around 30K  of  tables  to  the
340         PCRE library. Only the general category properties such as  Lu  and  Nd         PCRE  library.  Only  the general category properties such as Lu and Nd
341         are supported. Details are given in the pcrepattern documentation.         are supported. Details are given in the pcrepattern documentation.
342    
343    
344  CODE VALUE OF NEWLINE  CODE VALUE OF NEWLINE
345    
346         By  default,  PCRE interprets character 10 (linefeed, LF) as indicating         By default, PCRE interprets the linefeed (LF) character  as  indicating
347         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
348         systems. You can compile PCRE to use character 13 (carriage return, CR)         systems. You can compile PCRE to use carriage return (CR)  instead,  by
349         instead, by adding         adding
350    
351           --enable-newline-is-cr           --enable-newline-is-cr
352    
353         to the  configure  command.  There  is  also  a  --enable-newline-is-lf         to  the  configure  command.  There  is  also  a --enable-newline-is-lf
354         option, which explicitly specifies linefeed as the newline character.         option, which explicitly specifies linefeed as the newline character.
355    
356         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 315  CODE VALUE OF NEWLINE Line 362  CODE VALUE OF NEWLINE
362    
363           --enable-newline-is-anycrlf           --enable-newline-is-anycrlf
364    
365         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
366         CRLF as indicating a line ending. Finally, a fifth option, specified by         CRLF as indicating a line ending. Finally, a fifth option, specified by
367    
368           --enable-newline-is-any           --enable-newline-is-any
# Line 327  CODE VALUE OF NEWLINE Line 374  CODE VALUE OF NEWLINE
374         conventional to use the standard for your operating system.         conventional to use the standard for your operating system.
375    
376    
377    WHAT \R MATCHES
378    
379           By default, the sequence \R in a pattern matches  any  Unicode  newline
380           sequence,  whatever  has  been selected as the line ending sequence. If
381           you specify
382    
383             --enable-bsr-anycrlf
384    
385           the default is changed so that \R matches only CR, LF, or  CRLF.  What-
386           ever  is selected when PCRE is built can be overridden when the library
387           functions are called.
388    
389    
390  BUILDING SHARED AND STATIC LIBRARIES  BUILDING SHARED AND STATIC LIBRARIES
391    
392         The PCRE building process uses libtool to build both shared and  static         The PCRE building process uses libtool to build both shared and  static
# Line 390  AVOIDING EXCESSIVE STACK USAGE Line 450  AVOIDING EXCESSIVE STACK USAGE
450    
451         to  the  configure  command. With this configuration, PCRE will use the         to  the  configure  command. With this configuration, PCRE will use the
452         pcre_stack_malloc and pcre_stack_free variables to call memory  manage-         pcre_stack_malloc and pcre_stack_free variables to call memory  manage-
453         ment  functions.  Separate  functions are provided because the usage is         ment  functions. By default these point to malloc() and free(), but you
454         very predictable: the block sizes requested are always  the  same,  and         can replace the pointers so that your own functions are used.
455         the  blocks  are always freed in reverse order. A calling program might  
456         be able to implement optimized functions that perform better  than  the         Separate functions are  provided  rather  than  using  pcre_malloc  and
457         standard  malloc()  and  free()  functions.  PCRE  runs noticeably more         pcre_free  because  the  usage  is  very  predictable:  the block sizes
458         slowly when built in this way. This option affects only the pcre_exec()         requested are always the same, and  the  blocks  are  always  freed  in
459         function; it is not relevant for the the pcre_dfa_exec() function.         reverse  order.  A calling program might be able to implement optimized
460           functions that perform better  than  malloc()  and  free().  PCRE  runs
461           noticeably more slowly when built in this way. This option affects only
462           the  pcre_exec()  function;  it   is   not   relevant   for   the   the
463           pcre_dfa_exec() function.
464    
465    
466  LIMITING PCRE RESOURCE USAGE  LIMITING PCRE RESOURCE USAGE
# Line 451  USING EBCDIC CODE Line 515  USING EBCDIC CODE
515    
516         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
517         character code is ASCII (or Unicode, which is  a  superset  of  ASCII).         character code is ASCII (or Unicode, which is  a  superset  of  ASCII).
518         PCRE  can,  however,  be  compiled  to  run in an EBCDIC environment by         This  is  the  case for most computer operating systems. PCRE can, how-
519         adding         ever, be compiled to run in an EBCDIC environment by adding
520    
521           --enable-ebcdic           --enable-ebcdic
522    
523         to the configure command. This setting implies --enable-rebuild-charta-         to the configure command. This setting implies --enable-rebuild-charta-
524         bles.         bles.  You  should  only  use  it if you know that you are in an EBCDIC
525           environment (for example,  an  IBM  mainframe  operating  system).  The
526           --enable-ebcdic option is incompatible with --enable-utf8.
527    
528    
529    PCREGREP OPTIONS FOR COMPRESSED FILE SUPPORT
530    
531           By default, pcregrep reads all files as plain text. You can build it so
532           that it recognizes files whose names end in .gz or .bz2, and reads them
533           with libz or libbz2, respectively, by adding one or both of
534    
535             --enable-pcregrep-libz
536             --enable-pcregrep-libbz2
537    
538           to the configure command. These options naturally require that the rel-
539           evant libraries are installed on your system. Configuration  will  fail
540           if they are not.
541    
542    
543    PCRETEST OPTION FOR LIBREADLINE SUPPORT
544    
545           If you add
546    
547             --enable-pcretest-libreadline
548    
549           to  the  configure  command,  pcretest  is  linked with the libreadline
550           library, and when its input is from a terminal, it reads it  using  the
551           readline() function. This provides line-editing and history facilities.
552           Note that libreadline is GPL-licenced, so if you distribute a binary of
553           pcretest linked in this way, there may be licensing issues.
554    
555           Setting  this  option  causes  the -lreadline option to be added to the
556           pcretest build. In many operating environments with  a  sytem-installed
557           libreadline this is sufficient. However, in some environments (e.g.  if
558           an unmodified distribution version of readline is in use),  some  extra
559           configuration  may  be necessary. The INSTALL file for libreadline says
560           this:
561    
562             "Readline uses the termcap functions, but does not link with the
563             termcap or curses library itself, allowing applications which link
564             with readline the to choose an appropriate library."
565    
566           If your environment has not been set up so that an appropriate  library
567           is automatically included, you may need to add something like
568    
569             LIBS="-ncurses"
570    
571           immediately before the configure command.
572    
573    
574  SEE ALSO  SEE ALSO
# Line 474  AUTHOR Line 585  AUTHOR
585    
586  REVISION  REVISION
587    
588         Last updated: 16 April 2007         Last updated: 17 March 2009
589         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
590  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
591    
592    
# Line 618  THE ALTERNATIVE MATCHING ALGORITHM Line 729  THE ALTERNATIVE MATCHING ALGORITHM
729         6.  Callouts  are  supported, but the value of the capture_top field is         6.  Callouts  are  supported, but the value of the capture_top field is
730         always 1, and the value of the capture_last field is always -1.         always 1, and the value of the capture_last field is always -1.
731    
732         7.  The \C escape sequence, which (in the standard algorithm) matches a         7. The \C escape sequence, which (in the standard algorithm) matches  a
733         single  byte, even in UTF-8 mode, is not supported because the alterna-         single  byte, even in UTF-8 mode, is not supported because the alterna-
734         tive algorithm moves through the subject  string  one  character  at  a         tive algorithm moves through the subject  string  one  character  at  a
735         time, for all active paths through the tree.         time, for all active paths through the tree.
736    
737           8.  Except for (*FAIL), the backtracking control verbs such as (*PRUNE)
738           are not supported. (*FAIL) is supported, and  behaves  like  a  failing
739           negative assertion.
740    
741    
742  ADVANTAGES OF THE ALTERNATIVE ALGORITHM  ADVANTAGES OF THE ALTERNATIVE ALGORITHM
743    
# Line 669  AUTHOR Line 784  AUTHOR
784    
785  REVISION  REVISION
786    
787         Last updated: 29 May 2007         Last updated: 19 April 2008
788         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2008 University of Cambridge.
789  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
790    
791    
# Line 782  PCRE API OVERVIEW Line 897  PCRE API OVERVIEW
897         a  Perl-compatible  manner. A sample program that demonstrates the sim-         a  Perl-compatible  manner. A sample program that demonstrates the sim-
898         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
899         the  source distribution. The pcresample documentation describes how to         the  source distribution. The pcresample documentation describes how to
900         run it.         compile and run it.
901    
902         A second matching function, pcre_dfa_exec(), which is not Perl-compati-         A second matching function, pcre_dfa_exec(), which is not Perl-compati-
903         ble,  is  also provided. This uses a different algorithm for the match-         ble,  is  also provided. This uses a different algorithm for the match-
# Line 866  NEWLINES Line 981  NEWLINES
981         dard. When PCRE is run, the default can be overridden,  either  when  a         dard. When PCRE is run, the default can be overridden,  either  when  a
982         pattern is compiled, or when it is matched.         pattern is compiled, or when it is matched.
983    
984           At compile time, the newline convention can be specified by the options
985           argument of pcre_compile(), or it can be specified by special  text  at
986           the start of the pattern itself; this overrides any other settings. See
987           the pcrepattern page for details of the special character sequences.
988    
989         In the PCRE documentation the word "newline" is used to mean "the char-         In the PCRE documentation the word "newline" is used to mean "the char-
990         acter or pair of characters that indicate a line break". The choice  of         acter  or pair of characters that indicate a line break". The choice of
991         newline  convention  affects  the  handling of the dot, circumflex, and         newline convention affects the handling of  the  dot,  circumflex,  and
992         dollar metacharacters, the handling of #-comments in /x mode, and, when         dollar metacharacters, the handling of #-comments in /x mode, and, when
993         CRLF  is a recognized line ending sequence, the match position advance-         CRLF is a recognized line ending sequence, the match position  advance-
994         ment for a non-anchored pattern. The choice of newline convention  does         ment for a non-anchored pattern. There is more detail about this in the
995         not affect the interpretation of the \n or \r escape sequences.         section on pcre_exec() options below.
996    
997           The choice of newline convention does not affect the interpretation  of
998           the  \n  or  \r  escape  sequences, nor does it affect what \R matches,
999           which is controlled in a similar way, but by separate options.
1000    
1001    
1002  MULTITHREADING  MULTITHREADING
1003    
1004         The  PCRE  functions  can be used in multi-threading applications, with         The PCRE functions can be used in  multi-threading  applications,  with
1005         the  proviso  that  the  memory  management  functions  pointed  to  by         the  proviso  that  the  memory  management  functions  pointed  to  by
1006         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
1007         callout function pointed to by pcre_callout, are shared by all threads.         callout function pointed to by pcre_callout, are shared by all threads.
# Line 926  CHECKING BUILD-TIME OPTIONS Line 1050  CHECKING BUILD-TIME OPTIONS
1050         The output is an integer whose value specifies  the  default  character         The output is an integer whose value specifies  the  default  character
1051         sequence  that is recognized as meaning "newline". The four values that         sequence  that is recognized as meaning "newline". The four values that
1052         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,
1053         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
1054         for your operating system.         are returned in EBCDIC environments. The default should normally corre-
1055           spond to the standard sequence for your operating system.
1056    
1057             PCRE_CONFIG_BSR
1058    
1059           The output is an integer whose value indicates what character sequences
1060           the \R escape sequence matches by default. A value of 0 means  that  \R
1061           matches  any  Unicode  line ending sequence; a value of 1 means that \R
1062           matches only CR, LF, or CRLF. The default can be overridden when a pat-
1063           tern is compiled or matched.
1064    
1065           PCRE_CONFIG_LINK_SIZE           PCRE_CONFIG_LINK_SIZE
1066    
1067         The output is an integer that contains the number  of  bytes  used  for         The  output  is  an  integer that contains the number of bytes used for
1068         internal linkage in compiled regular expressions. The value is 2, 3, or         internal linkage in compiled regular expressions. The value is 2, 3, or
1069         4. Larger values allow larger regular expressions to  be  compiled,  at         4.  Larger  values  allow larger regular expressions to be compiled, at
1070         the  expense  of  slower matching. The default value of 2 is sufficient         the expense of slower matching. The default value of  2  is  sufficient
1071         for all but the most massive patterns, since  it  allows  the  compiled         for  all  but  the  most massive patterns, since it allows the compiled
1072         pattern to be up to 64K in size.         pattern to be up to 64K in size.
1073    
1074           PCRE_CONFIG_POSIX_MALLOC_THRESHOLD           PCRE_CONFIG_POSIX_MALLOC_THRESHOLD
1075    
1076         The  output  is  an integer that contains the threshold above which the         The output is an integer that contains the threshold  above  which  the
1077         POSIX interface uses malloc() for output vectors. Further  details  are         POSIX  interface  uses malloc() for output vectors. Further details are
1078         given in the pcreposix documentation.         given in the pcreposix documentation.
1079    
1080           PCRE_CONFIG_MATCH_LIMIT           PCRE_CONFIG_MATCH_LIMIT
1081    
1082         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-
1083         internal matching function calls in a  pcre_exec()  execution.  Further         ber  of  internal  matching  function calls in a pcre_exec() execution.
1084         details are given with pcre_exec() below.         Further details are given with pcre_exec() below.
1085    
1086           PCRE_CONFIG_MATCH_LIMIT_RECURSION           PCRE_CONFIG_MATCH_LIMIT_RECURSION
1087    
1088         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
1089         recursion when calling the internal matching function in a  pcre_exec()         of   recursion  when  calling  the  internal  matching  function  in  a
1090         execution. Further details are given with pcre_exec() below.         pcre_exec() execution.  Further  details  are  given  with  pcre_exec()
1091           below.
1092    
1093           PCRE_CONFIG_STACKRECURSE           PCRE_CONFIG_STACKRECURSE
1094    
# Line 1058  COMPILING A PATTERN Line 1192  COMPILING A PATTERN
1192         all with number 255, before each pattern item. For  discussion  of  the         all with number 255, before each pattern item. For  discussion  of  the
1193         callout facility, see the pcrecallout documentation.         callout facility, see the pcrecallout documentation.
1194    
1195             PCRE_BSR_ANYCRLF
1196             PCRE_BSR_UNICODE
1197    
1198           These options (which are mutually exclusive) control what the \R escape
1199           sequence matches. The choice is either to match only CR, LF,  or  CRLF,
1200           or to match any Unicode newline sequence. The default is specified when
1201           PCRE is built. It can be overridden from within the pattern, or by set-
1202           ting an option when a compiled pattern is matched.
1203    
1204           PCRE_CASELESS           PCRE_CASELESS
1205    
1206         If  this  bit is set, letters in the pattern match both upper and lower         If  this  bit is set, letters in the pattern match both upper and lower
# Line 1131  COMPILING A PATTERN Line 1274  COMPILING A PATTERN
1274         before  or  at  the  first  newline  in  the subject string, though the         before  or  at  the  first  newline  in  the subject string, though the
1275         matched text may continue over the newline.         matched text may continue over the newline.
1276    
1277             PCRE_JAVASCRIPT_COMPAT
1278    
1279           If this option is set, PCRE's behaviour is changed in some ways so that
1280           it  is  compatible with JavaScript rather than Perl. The changes are as
1281           follows:
1282    
1283           (1) A lone closing square bracket in a pattern  causes  a  compile-time
1284           error,  because this is illegal in JavaScript (by default it is treated
1285           as a data character). Thus, the pattern AB]CD becomes illegal when this
1286           option is set.
1287    
1288           (2)  At run time, a back reference to an unset subpattern group matches
1289           an empty string (by default this causes the current  matching  alterna-
1290           tive  to  fail). A pattern such as (\1)(a) succeeds when this option is
1291           set (assuming it can find an "a" in the subject), whereas it  fails  by
1292           default, for Perl compatibility.
1293    
1294           PCRE_MULTILINE           PCRE_MULTILINE
1295    
1296         By default, PCRE treats the subject string as consisting  of  a  single         By  default,  PCRE  treats the subject string as consisting of a single
1297         line  of characters (even if it actually contains newlines). The "start         line of characters (even if it actually contains newlines). The  "start
1298         of line" metacharacter (^) matches only at the  start  of  the  string,         of  line"  metacharacter  (^)  matches only at the start of the string,
1299         while  the  "end  of line" metacharacter ($) matches only at the end of         while the "end of line" metacharacter ($) matches only at  the  end  of
1300         the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY         the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY
1301         is set). This is the same as Perl.         is set). This is the same as Perl.
1302    
1303         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"
1304         constructs match immediately following or immediately  before  internal         constructs  match  immediately following or immediately before internal
1305         newlines  in  the  subject string, respectively, as well as at the very         newlines in the subject string, respectively, as well as  at  the  very
1306         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
1307         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-
1308         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,
1309         setting PCRE_MULTILINE has no effect.         setting PCRE_MULTILINE has no effect.
1310    
1311           PCRE_NEWLINE_CR           PCRE_NEWLINE_CR
# Line 1154  COMPILING A PATTERN Line 1314  COMPILING A PATTERN
1314           PCRE_NEWLINE_ANYCRLF           PCRE_NEWLINE_ANYCRLF
1315           PCRE_NEWLINE_ANY           PCRE_NEWLINE_ANY
1316    
1317         These  options  override the default newline definition that was chosen         These options override the default newline definition that  was  chosen
1318         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
1319         newline  is  indicated  by a single character (CR or LF, respectively).         newline is indicated by a single character (CR  or  LF,  respectively).
1320         Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by  the         Setting  PCRE_NEWLINE_CRLF specifies that a newline is indicated by the
1321         two-character  CRLF  sequence.  Setting  PCRE_NEWLINE_ANYCRLF specifies         two-character CRLF  sequence.  Setting  PCRE_NEWLINE_ANYCRLF  specifies
1322         that any of the three preceding sequences should be recognized. Setting         that any of the three preceding sequences should be recognized. Setting
1323         PCRE_NEWLINE_ANY  specifies that any Unicode newline sequence should be         PCRE_NEWLINE_ANY specifies that any Unicode newline sequence should  be
1324         recognized. The Unicode newline sequences are the three just mentioned,         recognized. The Unicode newline sequences are the three just mentioned,
1325         plus  the  single  characters  VT (vertical tab, U+000B), FF (formfeed,         plus the single characters VT (vertical  tab,  U+000B),  FF  (formfeed,
1326         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
1327         (paragraph  separator,  U+2029).  The  last  two are recognized only in         (paragraph separator, U+2029). The last  two  are  recognized  only  in
1328         UTF-8 mode.         UTF-8 mode.
1329    
1330         The newline setting in the  options  word  uses  three  bits  that  are         The  newline  setting  in  the  options  word  uses three bits that are
1331         treated as a number, giving eight possibilities. Currently only six are         treated as a number, giving eight possibilities. Currently only six are
1332         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
1333         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-
1334         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
1335         PCRE_NEWLINE_CRLF,  but other combinations may yield unused numbers and         PCRE_NEWLINE_CRLF, but other combinations may yield unused numbers  and
1336         cause an error.         cause an error.
1337    
1338         The only time that a line break is specially recognized when  compiling         The  only time that a line break is specially recognized when compiling
1339         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
1340         character class is encountered. This indicates  a  comment  that  lasts         character  class  is  encountered.  This indicates a comment that lasts
1341         until  after the next line break sequence. In other circumstances, line         until after the next line break sequence. In other circumstances,  line
1342         break  sequences  are  treated  as  literal  data,   except   that   in         break   sequences   are   treated  as  literal  data,  except  that  in
1343         PCRE_EXTENDED mode, both CR and LF are treated as whitespace characters         PCRE_EXTENDED mode, both CR and LF are treated as whitespace characters
1344         and are therefore ignored.         and are therefore ignored.
1345    
1346         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
1347         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.
1348    
1349           PCRE_NO_AUTO_CAPTURE           PCRE_NO_AUTO_CAPTURE
1350    
# Line 1213  COMPILING A PATTERN Line 1373  COMPILING A PATTERN
1373           PCRE_NO_UTF8_CHECK           PCRE_NO_UTF8_CHECK
1374    
1375         When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is         When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is
1376         automatically  checked. If an invalid UTF-8 sequence of bytes is found,         automatically  checked.  There  is  a  discussion about the validity of
1377         pcre_compile() returns an error. If you already know that your  pattern         UTF-8 strings in the main pcre page. If an invalid  UTF-8  sequence  of
1378         is  valid, and you want to skip this check for performance reasons, you         bytes  is  found,  pcre_compile() returns an error. If you already know
1379         can set the PCRE_NO_UTF8_CHECK option. When it is set,  the  effect  of         that your pattern is valid, and you want to skip this check for perfor-
1380         passing an invalid UTF-8 string as a pattern is undefined. It may cause         mance  reasons,  you  can set the PCRE_NO_UTF8_CHECK option. When it is
1381         your program to crash.  Note that this option can  also  be  passed  to         set, the effect of passing an invalid UTF-8  string  as  a  pattern  is
1382         pcre_exec()  and pcre_dfa_exec(), to suppress the UTF-8 validity check-         undefined.  It  may  cause your program to crash. Note that this option
1383         ing of subject strings.         can also be passed to pcre_exec() and pcre_dfa_exec(), to suppress  the
1384           UTF-8 validity checking of subject strings.
1385    
1386    
1387  COMPILATION ERROR CODES  COMPILATION ERROR CODES
1388    
1389         The following table lists the error  codes  than  may  be  returned  by         The  following  table  lists  the  error  codes than may be returned by
1390         pcre_compile2(),  along with the error messages that may be returned by         pcre_compile2(), along with the error messages that may be returned  by
1391         both compiling functions. As PCRE has developed, some error codes  have         both  compiling functions. As PCRE has developed, some error codes have
1392         fallen out of use. To avoid confusion, they have not been re-used.         fallen out of use. To avoid confusion, they have not been re-used.
1393    
1394            0  no error            0  no error
# Line 1242  COMPILATION ERROR CODES Line 1403  COMPILATION ERROR CODES
1403            9  nothing to repeat            9  nothing to repeat
1404           10  [this code is not in use]           10  [this code is not in use]
1405           11  internal error: unexpected repeat           11  internal error: unexpected repeat
1406           12  unrecognized character after (?           12  unrecognized character after (? or (?-
1407           13  POSIX named classes are supported only within a class           13  POSIX named classes are supported only within a class
1408           14  missing )           14  missing )
1409           15  reference to non-existent subpattern           15  reference to non-existent subpattern
# Line 1250  COMPILATION ERROR CODES Line 1411  COMPILATION ERROR CODES
1411           17  unknown option bit(s) set           17  unknown option bit(s) set
1412           18  missing ) after comment           18  missing ) after comment
1413           19  [this code is not in use]           19  [this code is not in use]
1414           20  regular expression too large           20  regular expression is too large
1415           21  failed to get memory           21  failed to get memory
1416           22  unmatched parentheses           22  unmatched parentheses
1417           23  internal error: code overflow           23  internal error: code overflow
# Line 1259  COMPILATION ERROR CODES Line 1420  COMPILATION ERROR CODES
1420           26  malformed number or name after (?(           26  malformed number or name after (?(
1421           27  conditional group contains more than two branches           27  conditional group contains more than two branches
1422           28  assertion expected after (?(           28  assertion expected after (?(
1423           29  (?R or (?digits must be followed by )           29  (?R or (?[+-]digits must be followed by )
1424           30  unknown POSIX class name           30  unknown POSIX class name
1425           31  POSIX collating elements are not supported           31  POSIX collating elements are not supported
1426           32  this version of PCRE is not compiled with PCRE_UTF8 support           32  this version of PCRE is not compiled with PCRE_UTF8 support
# Line 1279  COMPILATION ERROR CODES Line 1440  COMPILATION ERROR CODES
1440           46  malformed \P or \p sequence           46  malformed \P or \p sequence
1441           47  unknown property name after \P or \p           47  unknown property name after \P or \p
1442           48  subpattern name is too long (maximum 32 characters)           48  subpattern name is too long (maximum 32 characters)
1443           49  too many named subpatterns (maximum 10,000)           49  too many named subpatterns (maximum 10000)
1444           50  repeated subpattern is too long           50  [this code is not in use]
1445           51  octal value is greater than \377 (not in UTF-8 mode)           51  octal value is greater than \377 (not in UTF-8 mode)
1446           52  internal error: overran compiling workspace           52  internal error: overran compiling workspace
1447           53   internal  error:  previously-checked  referenced  subpattern not           53  internal  error:  previously-checked  referenced  subpattern  not
1448         found         found
1449           54  DEFINE group contains more than one branch           54  DEFINE group contains more than one branch
1450           55  repeating a DEFINE group is not allowed           55  repeating a DEFINE group is not allowed
1451           56  inconsistent NEWLINE options"           56  inconsistent NEWLINE options
1452             57  \g is not followed by a braced, angle-bracketed, or quoted
1453                   name/number or by a plain number
1454             58  a numbered reference must not be zero
1455             59  (*VERB) with an argument is not supported
1456             60  (*VERB) not recognized
1457             61  number is too big
1458             62  subpattern name expected
1459             63  digit expected after (?+
1460             64  ] is an invalid data character in JavaScript compatibility mode
1461    
1462           The  numbers  32  and 10000 in errors 48 and 49 are defaults; different
1463           values may be used if the limits were changed when PCRE was built.
1464    
1465    
1466  STUDYING A PATTERN  STUDYING A PATTERN
# Line 1476  INFORMATION ABOUT A PATTERN Line 1649  INFORMATION ABOUT A PATTERN
1649         returned. The fourth argument should point to an unsigned char *  vari-         returned. The fourth argument should point to an unsigned char *  vari-
1650         able.         able.
1651    
1652             PCRE_INFO_HASCRORLF
1653    
1654           Return  1  if  the  pattern  contains any explicit matches for CR or LF
1655           characters, otherwise 0. The fourth argument should  point  to  an  int
1656           variable.  An explicit match is either a literal CR or LF character, or
1657           \r or \n.
1658    
1659           PCRE_INFO_JCHANGED           PCRE_INFO_JCHANGED
1660    
1661         Return  1  if the (?J) option setting is used in the pattern, otherwise         Return 1 if the (?J) or (?-J) option setting is used  in  the  pattern,
1662         0. The fourth argument should point to an int variable. The (?J) inter-         otherwise  0. The fourth argument should point to an int variable. (?J)
1663         nal option setting changes the local PCRE_DUPNAMES value.         and (?-J) set and unset the local PCRE_DUPNAMES option, respectively.
1664    
1665           PCRE_INFO_LASTLITERAL           PCRE_INFO_LASTLITERAL
1666    
1667         Return  the  value of the rightmost literal byte that must exist in any         Return the value of the rightmost literal byte that must exist  in  any
1668         matched string, other than at its  start,  if  such  a  byte  has  been         matched  string,  other  than  at  its  start,  if such a byte has been
1669         recorded. The fourth argument should point to an int variable. If there         recorded. The fourth argument should point to an int variable. If there
1670         is no such byte, -1 is returned. For anchored patterns, a last  literal         is  no such byte, -1 is returned. For anchored patterns, a last literal
1671         byte  is  recorded only if it follows something of variable length. For         byte is recorded only if it follows something of variable  length.  For
1672         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
1673         /^a\dz\d/ the returned value is -1.         /^a\dz\d/ the returned value is -1.
1674    
# Line 1496  INFORMATION ABOUT A PATTERN Line 1676  INFORMATION ABOUT A PATTERN
1676           PCRE_INFO_NAMEENTRYSIZE           PCRE_INFO_NAMEENTRYSIZE
1677           PCRE_INFO_NAMETABLE           PCRE_INFO_NAMETABLE
1678    
1679         PCRE  supports the use of named as well as numbered capturing parenthe-         PCRE supports the use of named as well as numbered capturing  parenthe-
1680         ses. The names are just an additional way of identifying the  parenthe-         ses.  The names are just an additional way of identifying the parenthe-
1681         ses, which still acquire numbers. Several convenience functions such as         ses, which still acquire numbers. Several convenience functions such as
1682         pcre_get_named_substring() are provided for  extracting  captured  sub-         pcre_get_named_substring()  are  provided  for extracting captured sub-
1683         strings  by  name. It is also possible to extract the data directly, by         strings by name. It is also possible to extract the data  directly,  by
1684         first converting the name to a number in order to  access  the  correct         first  converting  the  name to a number in order to access the correct
1685         pointers in the output vector (described with pcre_exec() below). To do         pointers in the output vector (described with pcre_exec() below). To do
1686         the conversion, you need  to  use  the  name-to-number  map,  which  is         the  conversion,  you  need  to  use  the  name-to-number map, which is
1687         described by these three values.         described by these three values.
1688    
1689         The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT         The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT
1690         gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size         gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size
1691         of  each  entry;  both  of  these  return  an int value. The entry size         of each entry; both of these  return  an  int  value.  The  entry  size
1692         depends on the length of the longest name. PCRE_INFO_NAMETABLE  returns         depends  on the length of the longest name. PCRE_INFO_NAMETABLE returns
1693         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
1694         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-
1695         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-
1696         sponding name, zero terminated. The names are  in  alphabetical  order.         sponding  name,  zero  terminated. The names are in alphabetical order.
1697         When PCRE_DUPNAMES is set, duplicate names are in order of their paren-         When PCRE_DUPNAMES is set, duplicate names are in order of their paren-
1698         theses numbers. For example, consider  the  following  pattern  (assume         theses  numbers.  For  example,  consider the following pattern (assume
1699         PCRE_EXTENDED  is  set,  so  white  space  -  including  newlines  - is         PCRE_EXTENDED is  set,  so  white  space  -  including  newlines  -  is
1700         ignored):         ignored):
1701    
1702           (?<date> (?<year>(\d\d)?\d\d) -           (?<date> (?<year>(\d\d)?\d\d) -
1703           (?<month>\d\d) - (?<day>\d\d) )           (?<month>\d\d) - (?<day>\d\d) )
1704    
1705         There are four named subpatterns, so the table has  four  entries,  and         There  are  four  named subpatterns, so the table has four entries, and
1706         each  entry  in the table is eight bytes long. The table is as follows,         each entry in the table is eight bytes long. The table is  as  follows,
1707         with non-printing bytes shows in hexadecimal, and undefined bytes shown         with non-printing bytes shows in hexadecimal, and undefined bytes shown
1708         as ??:         as ??:
1709    
# Line 1532  INFORMATION ABOUT A PATTERN Line 1712  INFORMATION ABOUT A PATTERN
1712           00 04 m  o  n  t  h  00           00 04 m  o  n  t  h  00
1713           00 02 y  e  a  r  00 ??           00 02 y  e  a  r  00 ??
1714    
1715         When  writing  code  to  extract  data from named subpatterns using the         When writing code to extract data  from  named  subpatterns  using  the
1716         name-to-number map, remember that the length of the entries  is  likely         name-to-number  map,  remember that the length of the entries is likely
1717         to be different for each compiled pattern.         to be different for each compiled pattern.
1718    
1719           PCRE_INFO_OKPARTIAL           PCRE_INFO_OKPARTIAL
1720    
1721         Return  1 if the pattern can be used for partial matching, otherwise 0.         Return 1 if the pattern can be used for partial matching, otherwise  0.
1722         The fourth argument should point to an int  variable.  The  pcrepartial         The  fourth  argument  should point to an int variable. The pcrepartial
1723         documentation  lists  the restrictions that apply to patterns when par-         documentation lists the restrictions that apply to patterns  when  par-
1724         tial matching is used.         tial matching is used.
1725    
1726           PCRE_INFO_OPTIONS           PCRE_INFO_OPTIONS
1727    
1728         Return a copy of the options with which the pattern was  compiled.  The         Return  a  copy of the options with which the pattern was compiled. The
1729         fourth  argument  should  point to an unsigned long int variable. These         fourth argument should point to an unsigned long  int  variable.  These
1730         option bits are those specified in the call to pcre_compile(), modified         option bits are those specified in the call to pcre_compile(), modified
1731         by any top-level option settings within the pattern itself.         by any top-level option settings at the start of the pattern itself. In
1732           other  words,  they are the options that will be in force when matching
1733           starts. For example, if the pattern /(?im)abc(?-i)d/ is  compiled  with
1734           the  PCRE_EXTENDED option, the result is PCRE_CASELESS, PCRE_MULTILINE,
1735           and PCRE_EXTENDED.
1736    
1737         A  pattern  is  automatically  anchored by PCRE if all of its top-level         A pattern is automatically anchored by PCRE if  all  of  its  top-level
1738         alternatives begin with one of the following:         alternatives begin with one of the following:
1739    
1740           ^     unless PCRE_MULTILINE is set           ^     unless PCRE_MULTILINE is set
# Line 1564  INFORMATION ABOUT A PATTERN Line 1748  INFORMATION ABOUT A PATTERN
1748    
1749           PCRE_INFO_SIZE           PCRE_INFO_SIZE
1750    
1751         Return  the  size  of the compiled pattern, that is, the value that was         Return the size of the compiled pattern, that is, the  value  that  was
1752         passed as the argument to pcre_malloc() when PCRE was getting memory in         passed as the argument to pcre_malloc() when PCRE was getting memory in
1753         which to place the compiled data. The fourth argument should point to a         which to place the compiled data. The fourth argument should point to a
1754         size_t variable.         size_t variable.
# Line 1572  INFORMATION ABOUT A PATTERN Line 1756  INFORMATION ABOUT A PATTERN
1756           PCRE_INFO_STUDYSIZE           PCRE_INFO_STUDYSIZE
1757    
1758         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
1759         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
1760         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
1761         created  by  pcre_study(). The fourth argument should point to a size_t         created by pcre_study(). The fourth argument should point to  a  size_t
1762         variable.         variable.
1763    
1764    
# Line 1582  OBSOLETE INFO FUNCTION Line 1766  OBSOLETE INFO FUNCTION
1766    
1767         int pcre_info(const pcre *code, int *optptr, int *firstcharptr);         int pcre_info(const pcre *code, int *optptr, int *firstcharptr);
1768    
1769         The pcre_info() function is now obsolete because its interface  is  too         The  pcre_info()  function is now obsolete because its interface is too
1770         restrictive  to return all the available data about a compiled pattern.         restrictive to return all the available data about a compiled  pattern.
1771         New  programs  should  use  pcre_fullinfo()  instead.  The   yield   of         New   programs   should  use  pcre_fullinfo()  instead.  The  yield  of
1772         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-
1773         lowing negative numbers:         lowing negative numbers:
1774    
1775           PCRE_ERROR_NULL       the argument code was NULL           PCRE_ERROR_NULL       the argument code was NULL
1776           PCRE_ERROR_BADMAGIC   the "magic number" was not found           PCRE_ERROR_BADMAGIC   the "magic number" was not found
1777    
1778         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
1779         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
1780         PCRE_INFO_OPTIONS above).         PCRE_INFO_OPTIONS above).
1781    
1782         If the pattern is not anchored and the  firstcharptr  argument  is  not         If  the  pattern  is  not anchored and the firstcharptr argument is not
1783         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
1784         any matched string (see PCRE_INFO_FIRSTBYTE above).         any matched string (see PCRE_INFO_FIRSTBYTE above).
1785    
1786    
# Line 1604  REFERENCE COUNTS Line 1788  REFERENCE COUNTS
1788    
1789         int pcre_refcount(pcre *code, int adjust);         int pcre_refcount(pcre *code, int adjust);
1790    
1791         The pcre_refcount() function is used to maintain a reference  count  in         The  pcre_refcount()  function is used to maintain a reference count in
1792         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
1793         benefit of applications that  operate  in  an  object-oriented  manner,         benefit  of  applications  that  operate  in an object-oriented manner,
1794         where different parts of the application may be using the same compiled         where different parts of the application may be using the same compiled
1795         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.
1796    
1797         When a pattern is compiled, the reference count field is initialized to         When a pattern is compiled, the reference count field is initialized to
1798         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
1799         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
1800         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
1801         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
1802         is outside these limits, it is forced to the appropriate limit value.         is outside these limits, it is forced to the appropriate limit value.
1803    
1804         Except  when it is zero, the reference count is not correctly preserved         Except when it is zero, the reference count is not correctly  preserved
1805         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
1806         whose byte-order is different. (This seems a highly unlikely scenario.)         whose byte-order is different. (This seems a highly unlikely scenario.)
1807    
1808    
# Line 1712  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1896  MATCHING A PATTERN: THE TRADITIONAL FUNC
1896         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-
1897         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.
1898    
1899         Limiting the recursion depth limits the amount of  stack  that  can  be         Limiting  the  recursion  depth  limits the amount of stack that can be
1900         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
1901         of the stack, the amount of heap memory that can be used.         of the stack, the amount of heap memory that can be used.
1902    
1903         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
1904         built;  the  default  default  is  the  same  value  as the default for         built; the default default  is  the  same  value  as  the  default  for
1905         match_limit. You can override the default by suppling pcre_exec()  with         match_limit.  You can override the default by suppling pcre_exec() with
1906         a   pcre_extra   block  in  which  match_limit_recursion  is  set,  and         a  pcre_extra  block  in  which  match_limit_recursion  is   set,   and
1907         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
1908         limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT.         limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT.
1909    
1910         The  pcre_callout  field is used in conjunction with the "callout" fea-         The pcre_callout field is used in conjunction with the  "callout"  fea-
1911         ture, which is described in the pcrecallout documentation.         ture, which is described in the pcrecallout documentation.
1912    
1913         The tables field  is  used  to  pass  a  character  tables  pointer  to         The  tables  field  is  used  to  pass  a  character  tables pointer to
1914         pcre_exec();  this overrides the value that is stored with the compiled         pcre_exec(); this overrides the value that is stored with the  compiled
1915         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
1916         custom  tables  were  supplied to pcre_compile() via its tableptr argu-         custom tables were supplied to pcre_compile() via  its  tableptr  argu-
1917         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
1918         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-
1919         using patterns that have been saved after compiling  with  an  external         using  patterns  that  have been saved after compiling with an external
1920         set  of  tables,  because  the  external tables might be at a different         set of tables, because the external tables  might  be  at  a  different
1921         address when pcre_exec() is called. See the  pcreprecompile  documenta-         address  when  pcre_exec() is called. See the pcreprecompile documenta-
1922         tion for a discussion of saving compiled patterns for later use.         tion for a discussion of saving compiled patterns for later use.
1923    
1924     Option bits for pcre_exec()     Option bits for pcre_exec()
1925    
1926         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.
1927         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,
1928         PCRE_NOTBOL,   PCRE_NOTEOL,   PCRE_NOTEMPTY,   PCRE_NO_UTF8_CHECK   and         PCRE_NOTBOL,   PCRE_NOTEOL,   PCRE_NOTEMPTY,    PCRE_NO_START_OPTIMIZE,
1929         PCRE_PARTIAL.         PCRE_NO_UTF8_CHECK and PCRE_PARTIAL.
1930    
1931           PCRE_ANCHORED           PCRE_ANCHORED
1932    
1933         The PCRE_ANCHORED option limits pcre_exec() to matching  at  the  first         The  PCRE_ANCHORED  option  limits pcre_exec() to matching at the first
1934         matching  position.  If  a  pattern was compiled with PCRE_ANCHORED, or         matching position. If a pattern was  compiled  with  PCRE_ANCHORED,  or
1935         turned out to be anchored by virtue of its contents, it cannot be  made         turned  out to be anchored by virtue of its contents, it cannot be made
1936         unachored at matching time.         unachored at matching time.
1937    
1938             PCRE_BSR_ANYCRLF
1939             PCRE_BSR_UNICODE
1940    
1941           These options (which are mutually exclusive) control what the \R escape
1942           sequence  matches.  The choice is either to match only CR, LF, or CRLF,
1943           or to match any Unicode newline sequence. These  options  override  the
1944           choice that was made or defaulted when the pattern was compiled.
1945    
1946           PCRE_NEWLINE_CR           PCRE_NEWLINE_CR
1947           PCRE_NEWLINE_LF           PCRE_NEWLINE_LF
1948           PCRE_NEWLINE_CRLF           PCRE_NEWLINE_CRLF
# Line 1762  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1954  MATCHING A PATTERN: THE TRADITIONAL FUNC
1954         tion  of  pcre_compile()  above.  During  matching,  the newline choice         tion  of  pcre_compile()  above.  During  matching,  the newline choice
1955         affects the behaviour of the dot, circumflex,  and  dollar  metacharac-         affects the behaviour of the dot, circumflex,  and  dollar  metacharac-
1956         ters.  It may also alter the way the match position is advanced after a         ters.  It may also alter the way the match position is advanced after a
1957         match  failure  for  an  unanchored  pattern.  When  PCRE_NEWLINE_CRLF,         match failure for an unanchored pattern.
1958         PCRE_NEWLINE_ANYCRLF,  or  PCRE_NEWLINE_ANY is set, and a match attempt  
1959         fails when the current position is at a CRLF sequence, the match  posi-         When PCRE_NEWLINE_CRLF, PCRE_NEWLINE_ANYCRLF,  or  PCRE_NEWLINE_ANY  is
1960         tion  is  advanced by two characters instead of one, in other words, to         set,  and a match attempt for an unanchored pattern fails when the cur-
1961         after the CRLF.         rent position is at a  CRLF  sequence,  and  the  pattern  contains  no
1962           explicit  matches  for  CR  or  LF  characters,  the  match position is
1963           advanced by two characters instead of one, in other words, to after the
1964           CRLF.
1965    
1966           The above rule is a compromise that makes the most common cases work as
1967           expected. For example, if the  pattern  is  .+A  (and  the  PCRE_DOTALL
1968           option is not set), it does not match the string "\r\nA" because, after
1969           failing at the start, it skips both the CR and the LF before  retrying.
1970           However,  the  pattern  [\r\n]A does match that string, because it con-
1971           tains an explicit CR or LF reference, and so advances only by one char-
1972           acter after the first failure.
1973    
1974           An explicit match for CR of LF is either a literal appearance of one of
1975           those characters, or one of the \r or  \n  escape  sequences.  Implicit
1976           matches  such  as [^X] do not count, nor does \s (which includes CR and
1977           LF in the characters that it matches).
1978    
1979           Notwithstanding the above, anomalous effects may still occur when  CRLF
1980           is a valid newline sequence and explicit \r or \n escapes appear in the
1981           pattern.
1982    
1983           PCRE_NOTBOL           PCRE_NOTBOL
1984    
# Line 1808  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2020  MATCHING A PATTERN: THE TRADITIONAL FUNC
2020         an ordinary match again. There is some code that demonstrates how to do         an ordinary match again. There is some code that demonstrates how to do
2021         this in the pcredemo.c sample program.         this in the pcredemo.c sample program.
2022    
2023             PCRE_NO_START_OPTIMIZE
2024    
2025           There are a number of optimizations that pcre_exec() uses at the  start
2026           of  a  match,  in  order to speed up the process. For example, if it is
2027           known that a match must start with a specific  character,  it  searches
2028           the subject for that character, and fails immediately if it cannot find
2029           it, without actually running the main matching function. When  callouts
2030           are  in  use,  these  optimizations  can cause them to be skipped. This
2031           option disables the "start-up" optimizations,  causing  performance  to
2032           suffer, but ensuring that the callouts do occur.
2033    
2034           PCRE_NO_UTF8_CHECK           PCRE_NO_UTF8_CHECK
2035    
2036         When PCRE_UTF8 is set at compile time, the validity of the subject as a         When PCRE_UTF8 is set at compile time, the validity of the subject as a
2037         UTF-8  string is automatically checked when pcre_exec() is subsequently         UTF-8 string is automatically checked when pcre_exec() is  subsequently
2038         called.  The value of startoffset is also checked  to  ensure  that  it         called.   The  value  of  startoffset is also checked to ensure that it
2039         points  to the start of a UTF-8 character. If an invalid UTF-8 sequence         points to the start of a UTF-8 character. There is a  discussion  about
2040         of bytes is found, pcre_exec() returns the error PCRE_ERROR_BADUTF8. If         the  validity  of  UTF-8 strings in the section on UTF-8 support in the
2041         startoffset  contains  an  invalid  value, PCRE_ERROR_BADUTF8_OFFSET is         main pcre page. If  an  invalid  UTF-8  sequence  of  bytes  is  found,
2042         returned.         pcre_exec()  returns  the error PCRE_ERROR_BADUTF8. If startoffset con-
2043           tains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is returned.
2044    
2045         If you already know that your subject is valid, and you  want  to  skip         If you already know that your subject is valid, and you  want  to  skip
2046         these    checks    for   performance   reasons,   you   can   set   the         these    checks    for   performance   reasons,   you   can   set   the
# Line 1843  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2067  MATCHING A PATTERN: THE TRADITIONAL FUNC
2067     The string to be matched by pcre_exec()     The string to be matched by pcre_exec()
2068    
2069         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
2070         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.
2071         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-
2072         Unlike  the  pattern string, the subject may contain binary zero bytes.         acter.  Unlike  the pattern string, the subject may contain binary zero
2073         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
2074         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
2075           case.
2076         A  non-zero  starting offset is useful when searching for another match  
2077         in the same subject by calling pcre_exec() again after a previous  suc-         A non-zero starting offset is useful when searching for  another  match
2078         cess.   Setting  startoffset differs from just passing over a shortened         in  the same subject by calling pcre_exec() again after a previous suc-
2079         string and setting PCRE_NOTBOL in the case of  a  pattern  that  begins         cess.  Setting startoffset differs from just passing over  a  shortened
2080           string  and  setting  PCRE_NOTBOL  in the case of a pattern that begins
2081         with any kind of lookbehind. For example, consider the pattern         with any kind of lookbehind. For example, consider the pattern
2082    
2083           \Biss\B           \Biss\B
2084    
2085         which  finds  occurrences  of "iss" in the middle of words. (\B matches         which finds occurrences of "iss" in the middle of  words.  (\B  matches
2086         only if the current position in the subject is not  a  word  boundary.)         only  if  the  current position in the subject is not a word boundary.)
2087         When  applied  to the string "Mississipi" the first call to pcre_exec()         When applied to the string "Mississipi" the first call  to  pcre_exec()
2088         finds the first occurrence. If pcre_exec() is called  again  with  just         finds  the  first  occurrence. If pcre_exec() is called again with just
2089         the  remainder  of  the  subject,  namely  "issipi", it does not match,         the remainder of the subject,  namely  "issipi",  it  does  not  match,
2090         because \B is always false at the start of the subject, which is deemed         because \B is always false at the start of the subject, which is deemed
2091         to  be  a  word  boundary. However, if pcre_exec() is passed the entire         to be a word boundary. However, if pcre_exec()  is  passed  the  entire
2092         string again, but with startoffset set to 4, it finds the second occur-         string again, but with startoffset set to 4, it finds the second occur-
2093         rence  of "iss" because it is able to look behind the starting point to         rence of "iss" because it is able to look behind the starting point  to
2094         discover that it is preceded by a letter.         discover that it is preceded by a letter.
2095    
2096         If a non-zero starting offset is passed when the pattern  is  anchored,         If  a  non-zero starting offset is passed when the pattern is anchored,
2097         one attempt to match at the given offset is made. This can only succeed         one attempt to match at the given offset is made. This can only succeed
2098         if the pattern does not require the match to be at  the  start  of  the         if  the  pattern  does  not require the match to be at the start of the
2099         subject.         subject.
2100    
2101     How pcre_exec() returns captured substrings     How pcre_exec() returns captured substrings
2102    
2103         In  general, a pattern matches a certain portion of the subject, and in         In general, a pattern matches a certain portion of the subject, and  in
2104         addition, further substrings from the subject  may  be  picked  out  by         addition,  further  substrings  from  the  subject may be picked out by
2105         parts  of  the  pattern.  Following the usage in Jeffrey Friedl's book,         parts of the pattern. Following the usage  in  Jeffrey  Friedl's  book,
2106         this is called "capturing" in what follows, and the  phrase  "capturing         this  is  called "capturing" in what follows, and the phrase "capturing
2107         subpattern"  is  used for a fragment of a pattern that picks out a sub-         subpattern" is used for a fragment of a pattern that picks out  a  sub-
2108         string. PCRE supports several other kinds of  parenthesized  subpattern         string.  PCRE  supports several other kinds of parenthesized subpattern
2109         that do not cause substrings to be captured.         that do not cause substrings to be captured.
2110    
2111         Captured  substrings are returned to the caller via a vector of integer         Captured substrings are returned to the caller via a vector of integers
2112         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-
2113         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:
2114         Note: this argument is NOT the size of ovector in bytes.         this argument is NOT the size of ovector in bytes.
2115    
2116         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-
2117         strings,  each  substring using a pair of integers. The remaining third         strings, each substring using a pair of integers. The  remaining  third
2118         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-
2119         turing  subpatterns, and is not available for passing back information.         turing subpatterns, and is not available for passing back  information.
2120         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
2121         it is not, it is rounded down.         it is not, it is rounded down.
2122    
2123         When  a  match  is successful, information about captured substrings is         When a match is successful, information about  captured  substrings  is
2124         returned in pairs of integers, starting at the  beginning  of  ovector,         returned  in  pairs  of integers, starting at the beginning of ovector,
2125         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
2126         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
2127         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
2128         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
2129         tor[1],  identify  the  portion  of  the  subject string matched by the         byte offsets, even in UTF-8 mode. They are not character counts.
2130         entire pattern. The next pair is used for the first  capturing  subpat-  
2131         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
2132         highest numbered pair that has been set. For example, if two substrings         portion  of  the subject string matched by the entire pattern. The next
2133         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
2134         subpatterns, the return value from a successful match is 1,  indicating         returned by pcre_exec() is one more than the highest numbered pair that
2135         that just the first pair of offsets has been set.         has been set.  For example, if two substrings have been  captured,  the
2136           returned  value is 3. If there are no capturing subpatterns, the return
2137           value from a successful match is 1, indicating that just the first pair
2138           of offsets has been set.
2139    
2140         If a capturing subpattern is matched repeatedly, it is the last portion         If a capturing subpattern is matched repeatedly, it is the last portion
2141         of the string that it matched that is returned.         of the string that it matched that is returned.
2142    
2143         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,
2144         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
2145         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
2146         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
2147         as NULL and ovecsize as zero. However, if  the  pattern  contains  back         ovecsize as zero. However, if the pattern contains back references  and
2148         references  and  the  ovector is not big enough to remember the related         the  ovector is not big enough to remember the related substrings, PCRE
2149         substrings, PCRE has to get additional memory for use during  matching.         has to get additional memory for use during matching. Thus it  is  usu-
2150         Thus it is usually advisable to supply an ovector.         ally advisable to supply an ovector.
2151    
2152         The  pcre_info()  function  can  be used to find out how many capturing         The  pcre_info()  function  can  be used to find out how many capturing
2153         subpatterns there are in a compiled  pattern.  The  smallest  size  for         subpatterns there are in a compiled  pattern.  The  smallest  size  for
# Line 2031  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2259  MATCHING A PATTERN: THE TRADITIONAL FUNC
2259    
2260           PCRE_ERROR_BADCOUNT       (-15)           PCRE_ERROR_BADCOUNT       (-15)
2261    
2262         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.
2263    
2264           PCRE_ERROR_RECURSIONLIMIT (-21)           PCRE_ERROR_RECURSIONLIMIT (-21)
2265    
2266         The internal recursion limit, as specified by the match_limit_recursion         The internal recursion limit, as specified by the match_limit_recursion
2267         field  in  a  pcre_extra  structure (or defaulted) was reached. See the         field in a pcre_extra structure (or defaulted)  was  reached.  See  the
2268         description above.         description above.
2269    
          PCRE_ERROR_NULLWSLIMIT    (-22)  
   
        When a group that can match an empty  substring  is  repeated  with  an  
        unbounded  upper  limit, the subject position at the start of the group  
        must be remembered, so that a test for an empty string can be made when  
        the  end  of the group is reached. Some workspace is required for this;  
        if it runs out, this error is given.  
   
2270           PCRE_ERROR_BADNEWLINE     (-23)           PCRE_ERROR_BADNEWLINE     (-23)
2271    
2272         An invalid combination of PCRE_NEWLINE_xxx options was given.         An invalid combination of PCRE_NEWLINE_xxx options was given.
2273    
2274         Error numbers -16 to -20 are not used by pcre_exec().         Error numbers -16 to -20 and -22 are not used by pcre_exec().
2275    
2276    
2277  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER
# Line 2067  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER Line 2287  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER
2287         int pcre_get_substring_list(const char *subject,         int pcre_get_substring_list(const char *subject,
2288              int *ovector, int stringcount, const char ***listptr);              int *ovector, int stringcount, const char ***listptr);
2289    
2290         Captured substrings can be  accessed  directly  by  using  the  offsets         Captured  substrings  can  be  accessed  directly  by using the offsets
2291         returned  by  pcre_exec()  in  ovector.  For convenience, the functions         returned by pcre_exec() in  ovector.  For  convenience,  the  functions
2292         pcre_copy_substring(),    pcre_get_substring(),    and    pcre_get_sub-         pcre_copy_substring(),    pcre_get_substring(),    and    pcre_get_sub-
2293         string_list()  are  provided for extracting captured substrings as new,         string_list() are provided for extracting captured substrings  as  new,
2294         separate, zero-terminated strings. These functions identify  substrings         separate,  zero-terminated strings. These functions identify substrings
2295         by  number.  The  next section describes functions for extracting named         by number. The next section describes functions  for  extracting  named
2296         substrings.         substrings.
2297    
2298         A substring that contains a binary zero is correctly extracted and  has         A  substring that contains a binary zero is correctly extracted and has
2299         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
2300         string.  However, you can process such a string  by  referring  to  the         string.   However,  you  can  process such a string by referring to the
2301         length  that  is  returned  by  pcre_copy_substring() and pcre_get_sub-         length that is  returned  by  pcre_copy_substring()  and  pcre_get_sub-
2302         string().  Unfortunately, the interface to pcre_get_substring_list() is         string().  Unfortunately, the interface to pcre_get_substring_list() is
2303         not  adequate for handling strings containing binary zeros, because the         not adequate for handling strings containing binary zeros, because  the
2304         end of the final string is not independently indicated.         end of the final string is not independently indicated.
2305    
2306         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-
2307         tions:  subject  is  the subject string that has just been successfully         tions: subject is the subject string that has  just  been  successfully
2308         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
2309         passed to pcre_exec(), and stringcount is the number of substrings that         passed to pcre_exec(), and stringcount is the number of substrings that
2310         were captured by the match, including the substring  that  matched  the         were  captured  by  the match, including the substring that matched the
2311         entire regular expression. This is the value returned by pcre_exec() if         entire regular expression. This is the value returned by pcre_exec() if
2312         it is greater than zero. If pcre_exec() returned zero, indicating  that         it  is greater than zero. If pcre_exec() returned zero, indicating that
2313         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
2314         be the number of elements in the vector divided by three.         be the number of elements in the vector divided by three.
2315    
2316         The functions pcre_copy_substring() and pcre_get_substring() extract  a         The  functions pcre_copy_substring() and pcre_get_substring() extract a
2317         single  substring,  whose  number  is given as stringnumber. A value of         single substring, whose number is given as  stringnumber.  A  value  of
2318         zero extracts the substring that matched the  entire  pattern,  whereas         zero  extracts  the  substring that matched the entire pattern, whereas
2319         higher  values  extract  the  captured  substrings.  For pcre_copy_sub-         higher values  extract  the  captured  substrings.  For  pcre_copy_sub-
2320         string(), the string is placed in buffer,  whose  length  is  given  by         string(),  the  string  is  placed  in buffer, whose length is given by
2321         buffersize,  while  for  pcre_get_substring()  a new block of memory is         buffersize, while for pcre_get_substring() a new  block  of  memory  is
2322         obtained via pcre_malloc, and its address is  returned  via  stringptr.         obtained  via  pcre_malloc,  and its address is returned via stringptr.
2323         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
2324         the terminating zero, or one of these error codes:         the terminating zero, or one of these error codes:
2325    
2326           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2327    
2328         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
2329         get memory failed for pcre_get_substring().         get memory failed for pcre_get_substring().
2330    
2331           PCRE_ERROR_NOSUBSTRING    (-7)           PCRE_ERROR_NOSUBSTRING    (-7)
2332    
2333         There is no substring whose number is stringnumber.         There is no substring whose number is stringnumber.
2334    
2335         The  pcre_get_substring_list()  function  extracts  all  available sub-         The pcre_get_substring_list()  function  extracts  all  available  sub-
2336         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
2337         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
2338         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
2339         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
2340         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
2341         error code         error code
2342    
2343           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2344    
2345         if the attempt to get the memory block failed.         if the attempt to get the memory block failed.
2346    
2347         When  any of these functions encounter a substring that is unset, which         When any of these functions encounter a substring that is unset,  which
2348         can happen when capturing subpattern number n+1 matches  some  part  of         can  happen  when  capturing subpattern number n+1 matches some part of
2349         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
2350         empty string. This can be distinguished from a genuine zero-length sub-         empty string. This can be distinguished from a genuine zero-length sub-
2351         string  by inspecting the appropriate offset in ovector, which is nega-         string by inspecting the appropriate offset in ovector, which is  nega-
2352         tive for unset substrings.         tive for unset substrings.
2353    
2354         The two convenience functions pcre_free_substring() and  pcre_free_sub-         The  two convenience functions pcre_free_substring() and pcre_free_sub-
2355         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
2356         call  of  pcre_get_substring()  or  pcre_get_substring_list(),  respec-         call  of  pcre_get_substring()  or  pcre_get_substring_list(),  respec-
2357         tively.  They  do  nothing  more  than  call the function pointed to by         tively. They do nothing more than  call  the  function  pointed  to  by
2358         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.
2359         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-
2360         cial  interface  to  another  programming  language  that  cannot   use         cial   interface  to  another  programming  language  that  cannot  use
2361         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-
2362         vided.         vided.
2363    
2364    
# Line 2157  EXTRACTING CAPTURED SUBSTRINGS BY NAME Line 2377  EXTRACTING CAPTURED SUBSTRINGS BY NAME
2377              int stringcount, const char *stringname,              int stringcount, const char *stringname,
2378              const char **stringptr);              const char **stringptr);
2379    
2380         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-
2381         ber.  For example, for this pattern         ber.  For example, for this pattern
2382    
2383           (a+)b(?<xxx>\d+)...           (a+)b(?<xxx>\d+)...
# Line 2166  EXTRACTING CAPTURED SUBSTRINGS BY NAME Line 2386  EXTRACTING CAPTURED SUBSTRINGS BY NAME
2386         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
2387         name by calling pcre_get_stringnumber(). The first argument is the com-         name by calling pcre_get_stringnumber(). The first argument is the com-
2388         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
2389         the  subpattern  number,  or PCRE_ERROR_NOSUBSTRING (-7) if there is no         the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if  there  is  no
2390         subpattern of that name.         subpattern of that name.
2391    
2392         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
2393         the functions described in the previous section. For convenience, there         the functions described in the previous section. For convenience, there
2394         are also two functions that do the whole job.         are also two functions that do the whole job.
2395    
2396         Most   of   the   arguments    of    pcre_copy_named_substring()    and         Most    of    the    arguments   of   pcre_copy_named_substring()   and
2397         pcre_get_named_substring()  are  the  same  as  those for the similarly         pcre_get_named_substring() are the same  as  those  for  the  similarly
2398         named functions that extract by number. As these are described  in  the         named  functions  that extract by number. As these are described in the
2399         previous  section,  they  are not re-described here. There are just two         previous section, they are not re-described here. There  are  just  two
2400         differences:         differences:
2401    
2402         First, instead of a substring number, a substring name is  given.  Sec-         First,  instead  of a substring number, a substring name is given. Sec-
2403         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
2404         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
2405         name-to-number translation table.         name-to-number translation table.
2406    
2407         These  functions call pcre_get_stringnumber(), and if it succeeds, they         These functions call pcre_get_stringnumber(), and if it succeeds,  they
2408         then call pcre_copy_substring() or pcre_get_substring(),  as  appropri-         then  call  pcre_copy_substring() or pcre_get_substring(), as appropri-
2409         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
2410         behaviour may not be what you want (see the next section).         behaviour may not be what you want (see the next section).
2411    
2412           Warning:  If the pattern uses the "(?|" feature to set up multiple sub-
2413           patterns with the same number, you  cannot  use  names  to  distinguish
2414           them, because names are not included in the compiled code. The matching
2415           process uses only numbers.
2416    
2417    
2418  DUPLICATE SUBPATTERN NAMES  DUPLICATE SUBPATTERN NAMES
2419    
# Line 2199  DUPLICATE SUBPATTERN NAMES Line 2424  DUPLICATE SUBPATTERN NAMES
2424         subpatterns  are  not  required  to  be unique. Normally, patterns with         subpatterns  are  not  required  to  be unique. Normally, patterns with
2425         duplicate names are such that in any one match, only one of  the  named         duplicate names are such that in any one match, only one of  the  named
2426         subpatterns  participates. An example is shown in the pcrepattern docu-         subpatterns  participates. An example is shown in the pcrepattern docu-
2427         mentation. When duplicates are present, pcre_copy_named_substring() and         mentation.
2428    
2429           When   duplicates   are   present,   pcre_copy_named_substring()    and
2430         pcre_get_named_substring()  return the first substring corresponding to         pcre_get_named_substring()  return the first substring corresponding to
2431         the given name that is set.  If  none  are  set,  an  empty  string  is         the given name that is set. If  none  are  set,  PCRE_ERROR_NOSUBSTRING
2432         returned.  The pcre_get_stringnumber() function returns one of the num-         (-7)  is  returned;  no  data  is returned. The pcre_get_stringnumber()
2433         bers that are associated with the name, but it is not defined which  it         function returns one of the numbers that are associated with the  name,
2434         is.         but it is not defined which it is.
2435    
2436         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
2437         name, you must use  the  pcre_get_stringtable_entries()  function.  The         name, you must use  the  pcre_get_stringtable_entries()  function.  The
# Line 2394  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2621  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2621  SEE ALSO  SEE ALSO
2622    
2623         pcrebuild(3),  pcrecallout(3), pcrecpp(3)(3), pcrematching(3), pcrepar-         pcrebuild(3),  pcrecallout(3), pcrecpp(3)(3), pcrematching(3), pcrepar-
2624         tial(3), pcreposix(3), pcreprecompile(3), pcresample(3),  pcrestack(3).         tial(3), pcreposix(3), pcreprecompile(3), pcresample(3), pcrestack(3).
2625    
2626    
2627  AUTHOR  AUTHOR
# Line 2406  AUTHOR Line 2633  AUTHOR
2633    
2634  REVISION  REVISION
2635    
2636         Last updated: 04 June 2007         Last updated: 17 March 2009
2637         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
2638  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2639    
2640    
# Line 2458  PCRE CALLOUTS Line 2685  PCRE CALLOUTS
2685  MISSING CALLOUTS  MISSING CALLOUTS
2686    
2687         You  should  be  aware  that,  because of optimizations in the way PCRE         You  should  be  aware  that,  because of optimizations in the way PCRE
2688         matches patterns, callouts sometimes do not happen. For example, if the         matches patterns by default, callouts  sometimes  do  not  happen.  For
2689         pattern is         example, if the pattern is
2690    
2691           ab(?C4)cd           ab(?C4)cd
2692    
# Line 2468  MISSING CALLOUTS Line 2695  MISSING CALLOUTS
2695         ever  start,  and  the  callout is never reached. However, with "abyd",         ever  start,  and  the  callout is never reached. However, with "abyd",
2696         though the result is still no match, the callout is obeyed.         though the result is still no match, the callout is obeyed.
2697    
2698           You can disable these optimizations by passing the  PCRE_NO_START_OPTI-
2699           MIZE  option  to  pcre_exec()  or  pcre_dfa_exec(). This slows down the
2700           matching process, but does ensure that callouts  such  as  the  example
2701           above are obeyed.
2702    
2703    
2704  THE CALLOUT INTERFACE  THE CALLOUT INTERFACE
2705    
2706         During matching, when PCRE reaches a callout point, the external  func-         During  matching, when PCRE reaches a callout point, the external func-
2707         tion  defined by pcre_callout is called (if it is set). This applies to         tion defined by pcre_callout is called (if it is set). This applies  to
2708         both the pcre_exec() and the pcre_dfa_exec()  matching  functions.  The         both  the  pcre_exec()  and the pcre_dfa_exec() matching functions. The
2709         only  argument  to  the callout function is a pointer to a pcre_callout         only argument to the callout function is a pointer  to  a  pcre_callout
2710         block. This structure contains the following fields:         block. This structure contains the following fields:
2711    
2712           int          version;           int          version;
# Line 2490  THE CALLOUT INTERFACE Line 2722  THE CALLOUT INTERFACE
2722           int          pattern_position;           int          pattern_position;
2723           int          next_item_length;           int          next_item_length;
2724    
2725         The version field is an integer containing the version  number  of  the         The  version  field  is an integer containing the version number of the
2726         block  format. The initial version was 0; the current version is 1. The         block format. The initial version was 0; the current version is 1.  The
2727         version number will change again in future  if  additional  fields  are         version  number  will  change  again in future if additional fields are
2728         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.
2729    
2730         The callout_number field contains the number of the  callout,  as  com-         The callout_number field contains the number of the  callout,  as  com-
# Line 2577  AUTHOR Line 2809  AUTHOR
2809    
2810  REVISION  REVISION
2811    
2812         Last updated: 29 May 2007         Last updated: 15 March 2009
2813         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
2814  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2815    
2816    
# Line 2593  DIFFERENCES BETWEEN PCRE AND PERL Line 2825  DIFFERENCES BETWEEN PCRE AND PERL
2825    
2826         This  document describes the differences in the ways that PCRE and Perl         This  document describes the differences in the ways that PCRE and Perl
2827         handle regular expressions. The differences described here  are  mainly         handle regular expressions. The differences described here  are  mainly
2828         with  respect  to  Perl 5.8, though PCRE version 7.0 contains some fea-         with  respect  to  Perl 5.8, though PCRE versions 7.0 and later contain
2829         tures that are expected to be in the forthcoming Perl 5.10.         some features that are expected to be in the forthcoming Perl 5.10.
2830    
2831         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
2832         of  what  it does have are given in the section on UTF-8 support in the         of  what  it does have are given in the section on UTF-8 support in the
# Line 2659  DIFFERENCES BETWEEN PCRE AND PERL Line 2891  DIFFERENCES BETWEEN PCRE AND PERL
2891         matching  "aba"  against  the  pattern  /^(a(b)?)+$/  in Perl leaves $2         matching  "aba"  against  the  pattern  /^(a(b)?)+$/  in Perl leaves $2
2892         unset, but in PCRE it is set to "b".         unset, but in PCRE it is set to "b".
2893    
2894         11. PCRE provides some extensions to the Perl regular expression facil-         11.  PCRE  does  support  Perl  5.10's  backtracking  verbs  (*ACCEPT),
2895           (*FAIL),  (*F),  (*COMMIT), (*PRUNE), (*SKIP), and (*THEN), but only in
2896           the forms without an  argument.  PCRE  does  not  support  (*MARK).  If
2897           (*ACCEPT)  is within capturing parentheses, PCRE does not set that cap-
2898           ture group; this is different to Perl.
2899    
2900           12. PCRE provides some extensions to the Perl regular expression facil-
2901         ities.   Perl  5.10  will  include new features that are not in earlier         ities.   Perl  5.10  will  include new features that are not in earlier
2902         versions, some of which (such as named parentheses) have been  in  PCRE         versions, some of which (such as named parentheses) have been  in  PCRE
2903         for some time. This list is with respect to Perl 5.10:         for some time. This list is with respect to Perl 5.10:
# Line 2672  DIFFERENCES BETWEEN PCRE AND PERL Line 2910  DIFFERENCES BETWEEN PCRE AND PERL
2910         meta-character matches only at the very end of the string.         meta-character matches only at the very end of the string.
2911    
2912         (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-
2913         cial  meaning  is  faulted.  Otherwise,  like  Perl,  the  backslash is         cial meaning is faulted. Otherwise, like Perl, the backslash is quietly
2914         ignored. (Perl can be made to issue a warning.)         ignored.  (Perl can be made to issue a warning.)
2915    
2916         (d) If PCRE_UNGREEDY is set, the greediness of the  repetition  quanti-         (d) If PCRE_UNGREEDY is set, the greediness of the  repetition  quanti-
2917         fiers is inverted, that is, by default they are not greedy, but if fol-         fiers is inverted, that is, by default they are not greedy, but if fol-
# Line 2685  DIFFERENCES BETWEEN PCRE AND PERL Line 2923  DIFFERENCES BETWEEN PCRE AND PERL
2923         (f)  The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and PCRE_NO_AUTO_CAP-         (f)  The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and PCRE_NO_AUTO_CAP-
2924         TURE options for pcre_exec() have no Perl equivalents.         TURE options for pcre_exec() have no Perl equivalents.
2925    
2926         (g) The callout facility is PCRE-specific.         (g) The \R escape sequence can be restricted to match only CR,  LF,  or
2927           CRLF by the PCRE_BSR_ANYCRLF option.
2928    
2929         (h) The partial matching facility is PCRE-specific.         (h) The callout facility is PCRE-specific.
2930    
2931         (i) Patterns compiled by PCRE can be saved and re-used at a later time,         (i) The partial matching facility is PCRE-specific.
2932    
2933           (j) Patterns compiled by PCRE can be saved and re-used at a later time,
2934         even on different hosts that have the other endianness.         even on different hosts that have the other endianness.
2935    
2936         (j)  The  alternative  matching function (pcre_dfa_exec()) matches in a         (k) The alternative matching function (pcre_dfa_exec())  matches  in  a
2937         different way and is not Perl-compatible.         different way and is not Perl-compatible.
2938    
2939           (l)  PCRE  recognizes some special sequences such as (*CR) at the start
2940           of a pattern that set overall options that cannot be changed within the
2941           pattern.
2942    
2943    
2944  AUTHOR  AUTHOR
2945    
# Line 2705  AUTHOR Line 2950  AUTHOR
2950    
2951  REVISION  REVISION
2952    
2953         Last updated: 06 March 2007         Last updated: 11 September 2007
2954         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
2955  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2956    
# Line 2719  NAME Line 2964  NAME
2964    
2965  PCRE REGULAR EXPRESSION DETAILS  PCRE REGULAR EXPRESSION DETAILS
2966    
2967         The  syntax  and semantics of the regular expressions supported by PCRE         The  syntax and semantics of the regular expressions that are supported
2968         are described below. Regular expressions are also described in the Perl         by PCRE are described in detail below. There is a quick-reference  syn-
2969         documentation  and  in  a  number  of books, some of which have copious         tax summary in the pcresyntax page. PCRE tries to match Perl syntax and
2970         examples.  Jeffrey Friedl's "Mastering Regular Expressions",  published         semantics as closely as it can. PCRE  also  supports  some  alternative
2971         by  O'Reilly, covers regular expressions in great detail. This descrip-         regular  expression  syntax (which does not conflict with the Perl syn-
2972         tion of PCRE's regular expressions is intended as reference material.         tax) in order to provide some compatibility with regular expressions in
2973           Python, .NET, and Oniguruma.
2974    
2975           Perl's  regular expressions are described in its own documentation, and
2976           regular expressions in general are covered in a number of  books,  some
2977           of  which  have  copious  examples. Jeffrey Friedl's "Mastering Regular
2978           Expressions", published by  O'Reilly,  covers  regular  expressions  in
2979           great  detail.  This  description  of  PCRE's  regular  expressions  is
2980           intended as reference material.
2981    
2982         The original operation of PCRE was on strings of  one-byte  characters.         The original operation of PCRE was on strings of  one-byte  characters.
2983         However,  there is now also support for UTF-8 character strings. To use         However,  there is now also support for UTF-8 character strings. To use
# Line 2744  PCRE REGULAR EXPRESSION DETAILS Line 2997  PCRE REGULAR EXPRESSION DETAILS
2997         discussed in the pcrematching page.         discussed in the pcrematching page.
2998    
2999    
3000    NEWLINE CONVENTIONS
3001    
3002           PCRE  supports five different conventions for indicating line breaks in
3003           strings: a single CR (carriage return) character, a  single  LF  (line-
3004           feed) character, the two-character sequence CRLF, any of the three pre-
3005           ceding, or any Unicode newline sequence. The pcreapi page  has  further
3006           discussion  about newlines, and shows how to set the newline convention
3007           in the options arguments for the compiling and matching functions.
3008    
3009           It is also possible to specify a newline convention by starting a  pat-
3010           tern string with one of the following five sequences:
3011    
3012             (*CR)        carriage return
3013             (*LF)        linefeed
3014             (*CRLF)      carriage return, followed by linefeed
3015             (*ANYCRLF)   any of the three above
3016             (*ANY)       all Unicode newline sequences
3017    
3018           These override the default and the options given to pcre_compile(). For
3019           example, on a Unix system where LF is the default newline sequence, the
3020           pattern
3021    
3022             (*CR)a.b
3023    
3024           changes the convention to CR. That pattern matches "a\nb" because LF is
3025           no longer a newline. Note that these special settings,  which  are  not
3026           Perl-compatible,  are  recognized  only at the very start of a pattern,
3027           and that they must be in upper case.  If  more  than  one  of  them  is
3028           present, the last one is used.
3029    
3030           The  newline  convention  does  not  affect what the \R escape sequence
3031           matches. By default, this is any Unicode  newline  sequence,  for  Perl
3032           compatibility.  However, this can be changed; see the description of \R
3033           in the section entitled "Newline sequences" below. A change of \R  set-
3034           ting can be combined with a change of newline convention.
3035    
3036    
3037  CHARACTERS AND METACHARACTERS  CHARACTERS AND METACHARACTERS
3038    
3039         A  regular  expression  is  a pattern that is matched against a subject         A  regular  expression  is  a pattern that is matched against a subject
# Line 2799  CHARACTERS AND METACHARACTERS Line 3089  CHARACTERS AND METACHARACTERS
3089                    syntax)                    syntax)
3090           ]      terminates the character class           ]      terminates the character class
3091    
3092         The  following sections describe the use of each of the metacharacters.         The following sections describe the use of each of the metacharacters.
3093    
3094    
3095  BACKSLASH  BACKSLASH
3096    
3097         The backslash character has several uses. Firstly, if it is followed by         The backslash character has several uses. Firstly, if it is followed by
3098         a  non-alphanumeric  character,  it takes away any special meaning that         a non-alphanumeric character, it takes away any  special  meaning  that
3099         character may have. This  use  of  backslash  as  an  escape  character         character  may  have.  This  use  of  backslash  as an escape character
3100         applies both inside and outside character classes.         applies both inside and outside character classes.
3101    
3102         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
3103         pattern.  This escaping action applies whether  or  not  the  following         pattern.   This  escaping  action  applies whether or not the following
3104         character  would  otherwise be interpreted as a metacharacter, so it is         character would otherwise be interpreted as a metacharacter, so  it  is
3105         always safe to precede a non-alphanumeric  with  backslash  to  specify         always  safe  to  precede  a non-alphanumeric with backslash to specify
3106         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-
3107         slash, you write \\.         slash, you write \\.
3108    
3109         If a pattern is compiled with the PCRE_EXTENDED option,  whitespace  in         If  a  pattern is compiled with the PCRE_EXTENDED option, whitespace in
3110         the  pattern (other than in a character class) and characters between a         the pattern (other than in a character class) and characters between  a
3111         # outside a character class and the next newline are ignored. An escap-         # outside a character class and the next newline are ignored. An escap-
3112         ing  backslash  can  be  used to include a whitespace or # character as         ing backslash can be used to include a whitespace  or  #  character  as
3113         part of the pattern.         part of the pattern.
3114    
3115         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-
3116         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-
3117         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
3118         sequences  in  PCRE, whereas in Perl, $ and @ cause variable interpola-         sequences in PCRE, whereas in Perl, $ and @ cause  variable  interpola-
3119         tion. Note the following examples:         tion. Note the following examples:
3120    
3121           Pattern            PCRE matches   Perl matches           Pattern            PCRE matches   Perl matches
# Line 2835  BACKSLASH Line 3125  BACKSLASH
3125           \Qabc\$xyz\E       abc\$xyz       abc\$xyz           \Qabc\$xyz\E       abc\$xyz       abc\$xyz
3126           \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz           \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz
3127    
3128         The \Q...\E sequence is recognized both inside  and  outside  character         The  \Q...\E  sequence  is recognized both inside and outside character
3129         classes.         classes.
3130    
3131     Non-printing characters     Non-printing characters
3132    
3133         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-
3134         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
3135         appearance  of non-printing characters, apart from the binary zero that         appearance of non-printing characters, apart from the binary zero  that
3136         terminates a pattern, but when a pattern  is  being  prepared  by  text         terminates  a  pattern,  but  when  a pattern is being prepared by text
3137         editing,  it  is  usually  easier  to  use  one of the following escape         editing, it is usually easier  to  use  one  of  the  following  escape
3138         sequences than the binary character it represents:         sequences than the binary character it represents:
3139    
3140           \a        alarm, that is, the BEL character (hex 07)           \a        alarm, that is, the BEL character (hex 07)
3141           \cx       "control-x", where x is any character           \cx       "control-x", where x is any character
3142           \e        escape (hex 1B)           \e        escape (hex 1B)
3143           \f        formfeed (hex 0C)           \f        formfeed (hex 0C)
3144           \n        newline (hex 0A)           \n        linefeed (hex 0A)
3145           \r        carriage return (hex 0D)           \r        carriage return (hex 0D)
3146           \t        tab (hex 09)           \t        tab (hex 09)
3147           \ddd      character with octal code ddd, or backreference           \ddd      character with octal code ddd, or backreference
3148           \xhh      character with hex code hh           \xhh      character with hex code hh
3149           \x{hhh..} character with hex code hhh..           \x{hhh..} character with hex code hhh..
3150    
3151         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,
3152         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
3153         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;
3154         becomes hex 7B.         becomes hex 7B.
3155    
3156         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
3157         in upper or lower case). Any number of hexadecimal  digits  may  appear         in  upper  or  lower case). Any number of hexadecimal digits may appear
3158         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
3159         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,
3160         the  maximum  hexadecimal  value is 7FFFFFFF). If characters other than         the maximum value in hexadecimal is 7FFFFFFF. Note that this is  bigger
3161         hexadecimal digits appear between \x{ and }, or if there is  no  termi-         than the largest Unicode code point, which is 10FFFF.
3162         nating  }, this form of escape is not recognized.  Instead, the initial  
3163         \x will be interpreted as a basic hexadecimal escape, with no following         If  characters  other than hexadecimal digits appear between \x{ and },
3164         digits, giving a character whose value is zero.         or if there is no terminating }, this form of escape is not recognized.
3165           Instead,  the  initial  \x  will  be interpreted as a basic hexadecimal
3166           escape, with no following digits, giving a  character  whose  value  is
3167           zero.
3168    
3169         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
3170         two syntaxes for \x. There is no difference in the way  they  are  han-         two syntaxes for \x. There is no difference in the way  they  are  han-
# Line 2926  BACKSLASH Line 3219  BACKSLASH
3219    
3220     Absolute and relative back references     Absolute and relative back references
3221    
3222         The  sequence  \g followed by a positive or negative number, optionally         The  sequence  \g followed by an unsigned or a negative number, option-
3223         enclosed in braces, is an absolute or relative back reference. A  named         ally enclosed in braces, is an absolute or relative back  reference.  A
3224         back  reference can be coded as \g{name}. Back references are discussed         named back reference can be coded as \g{name}. Back references are dis-
3225         later, following the discussion of parenthesized subpatterns.         cussed later, following the discussion of parenthesized subpatterns.
3226    
3227       Absolute and relative subroutine calls
3228    
3229           For compatibility with Oniguruma, the non-Perl syntax \g followed by  a
3230           name or a number enclosed either in angle brackets or single quotes, is
3231           an alternative syntax for referencing a subpattern as  a  "subroutine".
3232           Details  are  discussed  later.   Note  that  \g{...} (Perl syntax) and
3233           \g<...> (Oniguruma syntax) are not synonymous. The  former  is  a  back
3234           reference; the latter is a subroutine call.
3235    
3236     Generic character types     Generic character types
3237    
# Line 2938  BACKSLASH Line 3240  BACKSLASH
3240    
3241           \d     any decimal digit           \d     any decimal digit
3242           \D     any character that is not a decimal digit           \D     any character that is not a decimal digit
3243             \h     any horizontal whitespace character
3244             \H     any character that is not a horizontal whitespace character
3245           \s     any whitespace character           \s     any whitespace character
3246           \S     any character that is not a whitespace character           \S     any character that is not a whitespace character
3247             \v     any vertical whitespace character
3248             \V     any character that is not a vertical whitespace character
3249           \w     any "word" character           \w     any "word" character
3250           \W     any "non-word" character           \W     any "non-word" character
3251    
3252         Each pair of escape sequences partitions the complete set of characters         Each pair of escape sequences partitions the complete set of characters
3253         into two disjoint sets. Any given character matches one, and only  one,         into  two disjoint sets. Any given character matches one, and only one,
3254         of each pair.         of each pair.
3255    
3256         These character type sequences can appear both inside and outside char-         These character type sequences can appear both inside and outside char-
3257         acter classes. They each match one character of the  appropriate  type.         acter  classes.  They each match one character of the appropriate type.
3258         If  the current matching point is at the end of the subject string, all         If the current matching point is at the end of the subject string,  all
3259         of them fail, since there is no character to match.         of them fail, since there is no character to match.
3260    
3261         For compatibility with Perl, \s does not match the VT  character  (code         For  compatibility  with Perl, \s does not match the VT character (code
3262         11).   This makes it different from the the POSIX "space" class. The \s         11).  This makes it different from the the POSIX "space" class. The  \s
3263         characters are HT (9), LF (10), FF (12), CR (13), and space  (32).  (If         characters  are  HT  (9), LF (10), FF (12), CR (13), and space (32). If
3264         "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-
3265         ter. In PCRE, it never does.)         ter. In PCRE, it never does.
   
        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.  
3266    
3267         In UTF-8 mode, characters with values greater than 128 never match  \d,         In  UTF-8 mode, characters with values greater than 128 never match \d,
3268         \s, or \w, and always match \D, \S, and \W. This is true even when Uni-         \s, or \w, and always match \D, \S, and \W. This is true even when Uni-
3269         code character property support is available. The use of  locales  with         code  character  property  support is available. These sequences retain
3270         Unicode is discouraged.         their original meanings from before UTF-8 support was available, mainly
3271           for efficiency reasons.
3272    
3273           The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to
3274           the other sequences, these do match certain high-valued  codepoints  in
3275           UTF-8 mode.  The horizontal space characters are:
3276    
3277             U+0009     Horizontal tab
3278             U+0020     Space
3279             U+00A0     Non-break space
3280             U+1680     Ogham space mark
3281             U+180E     Mongolian vowel separator
3282             U+2000     En quad
3283             U+2001     Em quad
3284             U+2002     En space
3285             U+2003     Em space
3286             U+2004     Three-per-em space
3287             U+2005     Four-per-em space
3288             U+2006     Six-per-em space
3289             U+2007     Figure space
3290             U+2008     Punctuation space
3291             U+2009     Thin space
3292             U+200A     Hair space
3293             U+202F     Narrow no-break space
3294             U+205F     Medium mathematical space
3295             U+3000     Ideographic space
3296    
3297           The vertical space characters are:
3298    
3299             U+000A     Linefeed
3300             U+000B     Vertical tab
3301             U+000C     Formfeed
3302             U+000D     Carriage return
3303             U+0085     Next line
3304             U+2028     Line separator
3305             U+2029     Paragraph separator
3306    
3307           A "word" character is an underscore or any character less than 256 that
3308           is a letter or digit. The definition of  letters  and  digits  is  con-
3309           trolled  by PCRE's low-valued character tables, and may vary if locale-
3310           specific matching is taking place (see "Locale support" in the  pcreapi
3311           page).  For  example,  in  a French locale such as "fr_FR" in Unix-like
3312           systems, or "french" in Windows, some character codes greater than  128
3313           are  used for accented letters, and these are matched by \w. The use of
3314           locales with Unicode is discouraged.
3315    
3316     Newline sequences     Newline sequences
3317    
3318         Outside  a  character class, the escape sequence \R matches any Unicode         Outside a character class, by default, the escape sequence  \R  matches
3319         newline sequence. This is an extension to Perl. In non-UTF-8 mode \R is         any Unicode newline sequence. This is a Perl 5.10 feature. In non-UTF-8
3320         equivalent to the following:         mode \R is equivalent to the following:
3321    
3322           (?>\r\n|\n|\x0b|\f|\r|\x85)           (?>\r\n|\n|\x0b|\f|\r|\x85)
3323    
3324         This  is  an  example  of an "atomic group", details of which are given         This is an example of an "atomic group", details  of  which  are  given
3325         below.  This particular group matches either the two-character sequence         below.  This particular group matches either the two-character sequence
3326         CR  followed  by  LF,  or  one  of  the single characters LF (linefeed,         CR followed by LF, or  one  of  the  single  characters  LF  (linefeed,
3327         U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage         U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage
3328         return, U+000D), or NEL (next line, U+0085). The two-character sequence         return, U+000D), or NEL (next line, U+0085). The two-character sequence
3329         is treated as a single unit that cannot be split.         is treated as a single unit that cannot be split.
3330    
3331         In UTF-8 mode, two additional characters whose codepoints  are  greater         In  UTF-8  mode, two additional characters whose codepoints are greater
3332         than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa-         than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa-
3333         rator, U+2029).  Unicode character property support is not  needed  for         rator,  U+2029).   Unicode character property support is not needed for
3334         these characters to be recognized.         these characters to be recognized.
3335    
3336           It is possible to restrict \R to match only CR, LF, or CRLF (instead of
3337           the  complete  set  of  Unicode  line  endings)  by  setting the option
3338           PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched.
3339           (BSR is an abbrevation for "backslash R".) This can be made the default
3340           when PCRE is built; if this is the case, the  other  behaviour  can  be
3341           requested  via  the  PCRE_BSR_UNICODE  option.   It is also possible to
3342           specify these settings by starting a pattern string  with  one  of  the
3343           following sequences:
3344    
3345             (*BSR_ANYCRLF)   CR, LF, or CRLF only
3346             (*BSR_UNICODE)   any Unicode newline sequence
3347    
3348           These override the default and the options given to pcre_compile(), but
3349           they can be overridden by options given to pcre_exec(). Note that these
3350           special settings, which are not Perl-compatible, are recognized only at
3351           the very start of a pattern, and that they must be in  upper  case.  If
3352           more  than  one  of  them is present, the last one is used. They can be
3353           combined with a change of newline convention, for  example,  a  pattern
3354           can start with:
3355    
3356             (*ANY)(*BSR_ANYCRLF)
3357    
3358         Inside a character class, \R matches the letter "R".         Inside a character class, \R matches the letter "R".
3359    
3360     Unicode character properties     Unicode character properties
3361    
3362         When PCRE is built with Unicode character property support, three addi-         When PCRE is built with Unicode character property support, three addi-
3363         tional escape sequences to match  character  properties  are  available         tional escape sequences that match characters with specific  properties
3364         when UTF-8 mode is selected. They are:         are  available.   When not in UTF-8 mode, these sequences are of course
3365           limited to testing characters whose codepoints are less than  256,  but
3366           they do work in this mode.  The extra escape sequences are:
3367    
3368           \p{xx}   a character with the xx property           \p{xx}   a character with the xx property
3369           \P{xx}   a character without the xx property           \P{xx}   a character without the xx property
# Line 3092  BACKSLASH Line 3458  BACKSLASH
3458         has  the  Lu,  Ll, or Lt property, in other words, a letter that is not         has  the  Lu,  Ll, or Lt property, in other words, a letter that is not
3459         classified as a modifier or "other".         classified as a modifier or "other".
3460    
3461           The Cs (Surrogate) property applies only to  characters  in  the  range
3462           U+D800  to  U+DFFF. Such characters are not valid in UTF-8 strings (see
3463           RFC 3629) and so cannot be tested by PCRE, unless UTF-8 validity check-
3464           ing  has  been  turned off (see the discussion of PCRE_NO_UTF8_CHECK in
3465           the pcreapi page).
3466    
3467         The long synonyms for these properties  that  Perl  supports  (such  as         The long synonyms for these properties  that  Perl  supports  (such  as
3468         \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
3469         any of these properties with "Is".         any of these properties with "Is".
# Line 3111  BACKSLASH Line 3483  BACKSLASH
3483         That is, it matches a character without the "mark"  property,  followed         That is, it matches a character without the "mark"  property,  followed
3484         by  zero  or  more  characters with the "mark" property, and treats the         by  zero  or  more  characters with the "mark" property, and treats the
3485         sequence as an atomic group (see below).  Characters  with  the  "mark"         sequence as an atomic group (see below).  Characters  with  the  "mark"
3486         property are typically accents that affect the preceding character.         property  are  typically  accents  that affect the preceding character.
3487           None of them have codepoints less than 256, so  in  non-UTF-8  mode  \X
3488           matches any one character.
3489    
3490         Matching  characters  by Unicode property is not fast, because PCRE has         Matching  characters  by Unicode property is not fast, because PCRE has
3491         to search a structure that contains  data  for  over  fifteen  thousand         to search a structure that contains  data  for  over  fifteen  thousand
# Line 3420  POSIX CHARACTER CLASSES Line 3794  POSIX CHARACTER CLASSES
3794    
3795  VERTICAL BAR  VERTICAL BAR
3796    
3797         Vertical bar characters are used to separate alternative patterns.  For         Vertical  bar characters are used to separate alternative patterns. For
3798         example, the pattern         example, the pattern
3799    
3800           gilbert|sullivan           gilbert|sullivan
3801    
3802         matches  either "gilbert" or "sullivan". Any number of alternatives may         matches either "gilbert" or "sullivan". Any number of alternatives  may
3803         appear, and an empty  alternative  is  permitted  (matching  the  empty         appear,  and  an  empty  alternative  is  permitted (matching the empty
3804         string). The matching process tries each alternative in turn, from left         string). The matching process tries each alternative in turn, from left
3805         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
3806         are  within a subpattern (defined below), "succeeds" means matching the         are within a subpattern (defined below), "succeeds" means matching  the
3807         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.
3808    
3809    
3810  INTERNAL OPTION SETTING  INTERNAL OPTION SETTING
3811    
3812         The  settings  of  the  PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and         The  settings  of  the  PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
3813         PCRE_EXTENDED options can be changed  from  within  the  pattern  by  a         PCRE_EXTENDED options (which are Perl-compatible) can be  changed  from
3814         sequence  of  Perl  option  letters  enclosed between "(?" and ")". The         within  the  pattern  by  a  sequence  of  Perl option letters enclosed
3815         option letters are         between "(?" and ")".  The option letters are
3816    
3817           i  for PCRE_CASELESS           i  for PCRE_CASELESS
3818           m  for PCRE_MULTILINE           m  for PCRE_MULTILINE
# Line 3452  INTERNAL OPTION SETTING Line 3826  INTERNAL OPTION SETTING
3826         is also permitted. If a  letter  appears  both  before  and  after  the         is also permitted. If a  letter  appears  both  before  and  after  the
3827         hyphen, the option is unset.         hyphen, the option is unset.
3828    
3829           The  PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA
3830           can be changed in the same way as the Perl-compatible options by  using
3831           the characters J, U and X respectively.
3832    
3833         When  an option change occurs at top level (that is, not inside subpat-         When  an option change occurs at top level (that is, not inside subpat-
3834         tern parentheses), the change applies to the remainder of  the  pattern         tern parentheses), the change applies to the remainder of  the  pattern
3835         that follows.  If the change is placed right at the start of a pattern,         that follows.  If the change is placed right at the start of a pattern,
# Line 3477  INTERNAL OPTION SETTING Line 3855  INTERNAL OPTION SETTING
3855         the  effects  of option settings happen at compile time. There would be         the  effects  of option settings happen at compile time. There would be
3856         some very weird behaviour otherwise.         some very weird behaviour otherwise.
3857    
3858         The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and  PCRE_EXTRA         Note: There are other PCRE-specific options that  can  be  set  by  the
3859         can  be changed in the same way as the Perl-compatible options by using         application  when  the  compile  or match functions are called. In some
3860         the characters J, U and X respectively.         cases the pattern can contain special  leading  sequences  to  override
3861           what  the  application  has set or what has been defaulted. Details are
3862           given in the section entitled "Newline sequences" above.
3863    
3864    
3865  SUBPATTERNS  SUBPATTERNS
# Line 3537  SUBPATTERNS Line 3917  SUBPATTERNS
3917         "Saturday".         "Saturday".
3918    
3919    
3920    DUPLICATE SUBPATTERN NUMBERS
3921    
3922           Perl 5.10 introduced a feature whereby each alternative in a subpattern
3923           uses the same numbers for its capturing parentheses. Such a  subpattern
3924           starts  with (?| and is itself a non-capturing subpattern. For example,
3925           consider this pattern:
3926    
3927             (?|(Sat)ur|(Sun))day
3928    
3929           Because the two alternatives are inside a (?| group, both sets of  cap-
3930           turing  parentheses  are  numbered one. Thus, when the pattern matches,
3931           you can look at captured substring number  one,  whichever  alternative
3932           matched.  This  construct  is useful when you want to capture part, but
3933           not all, of one of a number of alternatives. Inside a (?| group, paren-
3934           theses  are  numbered as usual, but the number is reset at the start of
3935           each branch. The numbers of any capturing buffers that follow the  sub-
3936           pattern  start after the highest number used in any branch. The follow-
3937           ing example is taken from the Perl documentation.  The  numbers  under-
3938           neath show in which buffer the captured content will be stored.
3939    
3940             # before  ---------------branch-reset----------- after
3941             / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
3942             # 1            2         2  3        2     3     4
3943    
3944           A  backreference  or  a  recursive call to a numbered subpattern always
3945           refers to the first one in the pattern with the given number.
3946    
3947           An alternative approach to using this "branch reset" feature is to  use
3948           duplicate named subpatterns, as described in the next section.
3949    
3950    
3951  NAMED SUBPATTERNS  NAMED SUBPATTERNS
3952    
3953         Identifying  capturing  parentheses  by number is simple, but it can be         Identifying  capturing  parentheses  by number is simple, but it can be
# Line 3576  NAMED SUBPATTERNS Line 3987  NAMED SUBPATTERNS
3987           (?<DN>Sat)(?:urday)?           (?<DN>Sat)(?:urday)?
3988    
3989         There  are  five capturing substrings, but only one is ever set after a         There  are  five capturing substrings, but only one is ever set after a
3990         match.  The convenience  function  for  extracting  the  data  by  name         match.  (An alternative way of solving this problem is to use a "branch
3991         returns  the  substring  for  the first (and in this example, the only)         reset" subpattern, as described in the previous section.)
3992         subpattern of that name that matched.  This  saves  searching  to  find  
3993         which  numbered  subpattern  it  was. If you make a reference to a non-         The  convenience  function  for extracting the data by name returns the
3994         unique named subpattern from elsewhere in the  pattern,  the  one  that         substring for the first (and in this example, the only)  subpattern  of
3995         corresponds  to  the  lowest number is used. For further details of the         that  name  that  matched.  This saves searching to find which numbered
3996         interfaces for handling named subpatterns, see the  pcreapi  documenta-         subpattern it was. If you make a reference to a non-unique  named  sub-
3997         tion.         pattern  from elsewhere in the pattern, the one that corresponds to the
3998           lowest number is used. For further details of the interfaces  for  han-
3999           dling named subpatterns, see the pcreapi documentation.
4000    
4001           Warning: You cannot use different names to distinguish between two sub-
4002           patterns with the same number (see the previous section)  because  PCRE
4003           uses only the numbers when matching.
4004    
4005    
4006  REPETITION  REPETITION
# Line 3625  REPETITION Line 4042  REPETITION
4042         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-
4043         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.
4044    
4045         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
4046         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-
4047         acters, each of which is represented by a two-byte sequence. Similarly,         acters, each of which is represented by a two-byte sequence. Similarly,
4048         when Unicode property support is available, \X{3} matches three Unicode         when Unicode property support is available, \X{3} matches three Unicode
4049         extended  sequences,  each of which may be several bytes long (and they         extended sequences, each of which may be several bytes long  (and  they
4050         may be of different lengths).         may be of different lengths).
4051    
4052         The quantifier {0} is permitted, causing the expression to behave as if         The quantifier {0} is permitted, causing the expression to behave as if
4053         the previous item and the quantifier were not present.         the previous item and the quantifier were not present. This may be use-
4054           ful  for  subpatterns that are referenced as subroutines from elsewhere
4055           in the pattern. Items other than subpatterns that have a {0} quantifier
4056           are omitted from the compiled pattern.
4057    
4058         For  convenience, the three most common quantifiers have single-charac-         For  convenience, the three most common quantifiers have single-charac-
4059         ter abbreviations:         ter abbreviations:
# Line 3763  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE Line 4183  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE
4183    
4184           (?>\d+)foo           (?>\d+)foo
4185    
4186         This  kind  of  parenthesis "locks up" the  part of the pattern it con-         This kind of parenthesis "locks up" the  part of the  pattern  it  con-
4187         tains once it has matched, and a failure further into  the  pattern  is         tains  once  it  has matched, and a failure further into the pattern is
4188         prevented  from  backtracking into it. Backtracking past it to previous         prevented from backtracking into it. Backtracking past it  to  previous
4189         items, however, works as normal.         items, however, works as normal.
4190    
4191         An alternative description is that a subpattern of  this  type  matches         An  alternative  description  is that a subpattern of this type matches
4192         the  string  of  characters  that an identical standalone pattern would         the string of characters that an  identical  standalone  pattern  would
4193         match, if anchored at the current point in the subject string.         match, if anchored at the current point in the subject string.
4194    
4195         Atomic grouping subpatterns are not capturing subpatterns. Simple cases         Atomic grouping subpatterns are not capturing subpatterns. Simple cases
4196         such as the above example can be thought of as a maximizing repeat that         such as the above example can be thought of as a maximizing repeat that
4197         must swallow everything it can. So, while both \d+ and  \d+?  are  pre-         must  swallow  everything  it can. So, while both \d+ and \d+? are pre-
4198         pared  to  adjust  the number of digits they match in order to make the         pared to adjust the number of digits they match in order  to  make  the
4199         rest of the pattern match, (?>\d+) can only match an entire sequence of         rest of the pattern match, (?>\d+) can only match an entire sequence of
4200         digits.         digits.
4201    
4202         Atomic  groups in general can of course contain arbitrarily complicated         Atomic groups in general can of course contain arbitrarily  complicated
4203         subpatterns, and can be nested. However, when  the  subpattern  for  an         subpatterns,  and  can  be  nested. However, when the subpattern for an
4204         atomic group is just a single repeated item, as in the example above, a         atomic group is just a single repeated item, as in the example above, a
4205         simpler notation, called a "possessive quantifier" can  be  used.  This         simpler  notation,  called  a "possessive quantifier" can be used. This
4206         consists  of  an  additional  + character following a quantifier. Using         consists of an additional + character  following  a  quantifier.  Using
4207         this notation, the previous example can be rewritten as         this notation, the previous example can be rewritten as
4208    
4209           \d++foo           \d++foo
4210    
4211           Note that a possessive quantifier can be used with an entire group, for
4212           example:
4213    
4214             (abc|xyz){2,3}+
4215    
4216         Possessive  quantifiers  are  always  greedy;  the   setting   of   the         Possessive  quantifiers  are  always  greedy;  the   setting   of   the
4217         PCRE_UNGREEDY option is ignored. They are a convenient notation for the         PCRE_UNGREEDY option is ignored. They are a convenient notation for the
4218         simpler forms of atomic group. However, there is no difference  in  the         simpler forms of atomic group. However, there is no difference  in  the
# Line 3831  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE Line 4256  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE
4256    
4257           ((?>\D+)|<\d+>)*[!?]           ((?>\D+)|<\d+>)*[!?]
4258    
4259         sequences  of non-digits cannot be broken, and failure happens quickly.         sequences of non-digits cannot be broken, and failure happens quickly.
4260    
4261    
4262  BACK REFERENCES  BACK REFERENCES
4263    
4264         Outside a character class, a backslash followed by a digit greater than         Outside a character class, a backslash followed by a digit greater than
4265         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-
4266         pattern earlier (that is, to its left) in the pattern,  provided  there         pattern  earlier  (that is, to its left) in the pattern, provided there
4267         have been that many previous capturing left parentheses.         have been that many previous capturing left parentheses.
4268    
4269         However, if the decimal number following the backslash is less than 10,         However, if the decimal number following the backslash is less than 10,
4270         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
4271         there  are  not that many capturing left parentheses in the entire pat-         there are not that many capturing left parentheses in the  entire  pat-
4272         tern. In other words, the parentheses that are referenced need  not  be         tern.  In  other words, the parentheses that are referenced need not be
4273         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
4274         reference" of this type can make sense when a  repetition  is  involved         reference"  of  this  type can make sense when a repetition is involved
4275         and  the  subpattern to the right has participated in an earlier itera-         and the subpattern to the right has participated in an  earlier  itera-
4276         tion.         tion.
4277    
4278         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
4279         subpattern  whose  number  is  10  or  more using this syntax because a         subpattern whose number is 10 or  more  using  this  syntax  because  a
4280         sequence such as \50 is interpreted as a character  defined  in  octal.         sequence  such  as  \50 is interpreted as a character defined in octal.
4281         See the subsection entitled "Non-printing characters" above for further         See the subsection entitled "Non-printing characters" above for further
4282         details of the handling of digits following a backslash.  There  is  no         details  of  the  handling of digits following a backslash. There is no
4283         such  problem  when named parentheses are used. A back reference to any         such problem when named parentheses are used. A back reference  to  any
4284         subpattern is possible using named parentheses (see below).         subpattern is possible using named parentheses (see below).
4285    
4286         Another way of avoiding the ambiguity inherent in  the  use  of  digits         Another  way  of  avoiding  the ambiguity inherent in the use of digits
4287         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-
4288         ture introduced in Perl 5.10. This escape must be followed by  a  posi-         ture  introduced  in  Perl  5.10.  This  escape  must be followed by an
4289         tive  or  a negative number, optionally enclosed in braces. These exam-         unsigned number or a negative number, optionally  enclosed  in  braces.
4290         ples are all identical:         These examples are all identical:
4291    
4292           (ring), \1           (ring), \1
4293           (ring), \g1           (ring), \g1
4294           (ring), \g{1}           (ring), \g{1}
4295    
4296         A positive number specifies an absolute reference without the ambiguity         An  unsigned number specifies an absolute reference without the ambigu-
4297         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
4298         digits follow the reference. A negative number is a relative reference.         digits follow the reference. A negative number is a relative reference.
4299         Consider this example:         Consider this example:
4300    
4301           (abc(def)ghi)\g{-1}           (abc(def)ghi)\g{-1}
4302    
4303         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-
4304         ing subpattern before \g, that is, is it equivalent to  \2.  Similarly,         ing  subpattern  before \g, that is, is it equivalent to \2. Similarly,
4305         \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
4306         helpful in long patterns, and also in  patterns  that  are  created  by         helpful  in  long  patterns,  and  also in patterns that are created by
4307         joining together fragments that contain references within themselves.         joining together fragments that contain references within themselves.
4308    
4309         A  back  reference matches whatever actually matched the capturing sub-         A back reference matches whatever actually matched the  capturing  sub-
4310         pattern in the current subject string, rather  than  anything  matching         pattern  in  the  current subject string, rather than anything matching
4311         the subpattern itself (see "Subpatterns as subroutines" below for a way         the subpattern itself (see "Subpatterns as subroutines" below for a way
4312         of doing that). So the pattern         of doing that). So the pattern
4313    
4314           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
4315    
4316         matches "sense and sensibility" and "response and responsibility",  but         matches  "sense and sensibility" and "response and responsibility", but
4317         not  "sense and responsibility". If caseful matching is in force at the         not "sense and responsibility". If caseful matching is in force at  the
4318         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-
4319         ple,         ple,
4320    
4321           ((?i)rah)\s+\1           ((?i)rah)\s+\1
4322    
4323         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
4324         original capturing subpattern is matched caselessly.         original capturing subpattern is matched caselessly.
4325    
4326         There are several different ways of writing back  references  to  named         There  are  several  different ways of writing back references to named
4327         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
4328         \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
4329         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
4330         and named references, is also supported. We  could  rewrite  the  above         and  named  references,  is  also supported. We could rewrite the above
4331         example in any of the following ways:         example in any of the following ways:
4332    
4333           (?<p1>(?i)rah)\s+\k<p1>           (?<p1>(?i)rah)\s+\k<p1>
# Line 3910  BACK REFERENCES Line 4335  BACK REFERENCES
4335           (?P<p1>(?i)rah)\s+(?P=p1)           (?P<p1>(?i)rah)\s+(?P=p1)
4336           (?<p1>(?i)rah)\s+\g{p1}           (?<p1>(?i)rah)\s+\g{p1}
4337    
4338         A  subpattern  that  is  referenced  by  name may appear in the pattern         A subpattern that is referenced by  name  may  appear  in  the  pattern
4339         before or after the reference.         before or after the reference.
4340    
4341         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
4342         subpattern  has  not actually been used in a particular match, any back         subpattern has not actually been used in a particular match,  any  back
4343         references to it always fail. For example, the pattern         references to it always fail. For example, the pattern
4344    
4345           (a|(bc))\2           (a|(bc))\2
4346    
4347         always fails if it starts to match "a" rather than "bc". Because  there         always  fails if it starts to match "a" rather than "bc". Because there
4348         may  be  many  capturing parentheses in a pattern, all digits following         may be many capturing parentheses in a pattern,  all  digits  following
4349         the backslash are taken as part of a potential back  reference  number.         the  backslash  are taken as part of a potential back reference number.
4350         If the pattern continues with a digit character, some delimiter must be         If the pattern continues with a digit character, some delimiter must be
4351         used to terminate the back reference. If the  PCRE_EXTENDED  option  is         used  to  terminate  the back reference. If the PCRE_EXTENDED option is
4352         set,  this  can  be  whitespace.  Otherwise an empty comment (see "Com-         set, this can be whitespace.  Otherwise an  empty  comment  (see  "Com-
4353         ments" below) can be used.         ments" below) can be used.
4354    
4355         A back reference that occurs inside the parentheses to which it  refers         A  back reference that occurs inside the parentheses to which it refers
4356         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
4357         matches.  However, such references can be useful inside  repeated  sub-         matches.   However,  such references can be useful inside repeated sub-
4358         patterns. For example, the pattern         patterns. For example, the pattern
4359    
4360           (a|b\1)+           (a|b\1)+
4361    
4362         matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-         matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-
4363         ation of the subpattern,  the  back  reference  matches  the  character         ation  of  the  subpattern,  the  back  reference matches the character
4364         string  corresponding  to  the previous iteration. In order for this to         string corresponding to the previous iteration. In order  for  this  to
4365         work, the pattern must be such that the first iteration does  not  need         work,  the  pattern must be such that the first iteration does not need
4366         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
4367         the example above, or by a quantifier with a minimum of zero.         the example above, or by a quantifier with a minimum of zero.
4368    
4369    
4370  ASSERTIONS  ASSERTIONS
4371    
4372         An assertion is a test on the characters  following  or  preceding  the         An  assertion  is  a  test on the characters following or preceding the
4373         current  matching  point that does not actually consume any characters.         current matching point that does not actually consume  any  characters.
4374         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
4375         described above.         described above.
4376    
4377         More  complicated  assertions  are  coded as subpatterns. There are two         More complicated assertions are coded as  subpatterns.  There  are  two
4378         kinds: those that look ahead of the current  position  in  the  subject         kinds:  those  that  look  ahead of the current position in the subject
4379         string,  and  those  that  look  behind  it. An assertion subpattern is         string, and those that look  behind  it.  An  assertion  subpattern  is
4380         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
4381         matching position to be changed.         matching position to be changed.
4382    
4383         Assertion  subpatterns  are  not  capturing subpatterns, and may not be         Assertion subpatterns are not capturing subpatterns,  and  may  not  be
4384         repeated, because it makes no sense to assert the  same  thing  several         repeated,  because  it  makes no sense to assert the same thing several
4385         times.  If  any kind of assertion contains capturing subpatterns within         times. If any kind of assertion contains capturing  subpatterns  within
4386         it, these are counted for the purposes of numbering the capturing  sub-         it,  these are counted for the purposes of numbering the capturing sub-
4387         patterns in the whole pattern.  However, substring capturing is carried         patterns in the whole pattern.  However, substring capturing is carried
4388         out only for positive assertions, because it does not  make  sense  for         out  only  for  positive assertions, because it does not make sense for
4389         negative assertions.         negative assertions.
4390    
4391     Lookahead assertions     Lookahead assertions
# Line 3970  ASSERTIONS Line 4395  ASSERTIONS
4395    
4396           \w+(?=;)           \w+(?=;)
4397    
4398         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-
4399         colon in the match, and         colon in the match, and
4400    
4401           foo(?!bar)           foo(?!bar)
4402    
4403         matches  any  occurrence  of  "foo" that is not followed by "bar". Note         matches any occurrence of "foo" that is not  followed  by  "bar".  Note
4404         that the apparently similar pattern         that the apparently similar pattern
4405    
4406           (?!foo)bar           (?!foo)bar
4407    
4408         does not find an occurrence of "bar"  that  is  preceded  by  something         does  not  find  an  occurrence  of "bar" that is preceded by something
4409         other  than "foo"; it finds any occurrence of "bar" whatsoever, because         other than "foo"; it finds any occurrence of "bar" whatsoever,  because
4410         the assertion (?!foo) is always true when the next three characters are         the assertion (?!foo) is always true when the next three characters are
4411         "bar". A lookbehind assertion is needed to achieve the other effect.         "bar". A lookbehind assertion is needed to achieve the other effect.
4412    
4413         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
4414         most convenient way to do it is  with  (?!)  because  an  empty  string         most  convenient  way  to  do  it  is with (?!) because an empty string
4415         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
4416         string must always fail.         string must always fail.
4417    
4418     Lookbehind assertions     Lookbehind assertions
4419    
4420         Lookbehind assertions start with (?<= for positive assertions and  (?<!         Lookbehind  assertions start with (?<= for positive assertions and (?<!
4421         for negative assertions. For example,         for negative assertions. For example,
4422    
4423           (?<!foo)bar           (?<!foo)bar
4424    
4425         does  find  an  occurrence  of "bar" that is not preceded by "foo". The         does find an occurrence of "bar" that is not  preceded  by  "foo".  The
4426         contents of a lookbehind assertion are restricted  such  that  all  the         contents  of  a  lookbehind  assertion are restricted such that all the
4427         strings it matches must have a fixed length. However, if there are sev-         strings it matches must have a fixed length. However, if there are sev-
4428         eral top-level alternatives, they do not all  have  to  have  the  same         eral  top-level  alternatives,  they  do  not all have to have the same
4429         fixed length. Thus         fixed length. Thus
4430    
4431           (?<=bullock|donkey)           (?<=bullock|donkey)
# Line 4009  ASSERTIONS Line 4434  ASSERTIONS
4434    
4435           (?<!dogs?|cats?)           (?<!dogs?|cats?)
4436    
4437         causes  an  error at compile time. Branches that match different length         causes an error at compile time. Branches that match  different  length
4438         strings are permitted only at the top level of a lookbehind  assertion.         strings  are permitted only at the top level of a lookbehind assertion.
4439         This  is  an  extension  compared  with  Perl (at least for 5.8), which         This is an extension compared with  Perl  (at  least  for  5.8),  which
4440         requires all branches to match the same length of string. An  assertion         requires  all branches to match the same length of string. An assertion
4441         such as         such as
4442    
4443           (?<=ab(c|de))           (?<=ab(c|de))
4444    
4445         is  not  permitted,  because  its single top-level branch can match two         is not permitted, because its single top-level  branch  can  match  two
4446         different lengths, but it is acceptable if rewritten to  use  two  top-         different  lengths,  but  it is acceptable if rewritten to use two top-
4447         level branches:         level branches:
4448    
4449           (?<=abc|abde)           (?<=abc|abde)
4450    
4451         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
4452         instead of a lookbehind assertion; this is not restricted to  a  fixed-         instead  of  a lookbehind assertion; this is not restricted to a fixed-
4453         length.         length.
4454    
4455         The  implementation  of lookbehind assertions is, for each alternative,         The implementation of lookbehind assertions is, for  each  alternative,
4456         to temporarily move the current position back by the fixed  length  and         to  temporarily  move the current position back by the fixed length and
4457         then try to match. If there are insufficient characters before the cur-         then try to match. If there are insufficient characters before the cur-
4458         rent position, the assertion fails.         rent position, the assertion fails.
4459    
4460         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
4461         mode)  to appear in lookbehind assertions, because it makes it impossi-         mode) to appear in lookbehind assertions, because it makes it  impossi-
4462         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,
4463         which can match different numbers of bytes, are also not permitted.         which can match different numbers of bytes, are also not permitted.
4464    
4465         Possessive  quantifiers  can  be  used  in  conjunction with lookbehind         Possessive quantifiers can  be  used  in  conjunction  with  lookbehind
4466         assertions to specify efficient matching at  the  end  of  the  subject         assertions  to  specify  efficient  matching  at the end of the subject
4467         string. Consider a simple pattern such as         string. Consider a simple pattern such as
4468    
4469           abcd$           abcd$
4470    
4471         when  applied  to  a  long string that does not match. Because matching         when applied to a long string that does  not  match.  Because  matching
4472         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
4473         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
4474         pattern is specified as         pattern is specified as
4475    
4476           ^.*abcd$           ^.*abcd$
4477    
4478         the initial .* matches the entire string at first, but when this  fails         the  initial .* matches the entire string at first, but when this fails
4479         (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
4480         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
4481         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,
4482         so we are no better off. However, if the pattern is written as         so we are no better off. However, if the pattern is written as
4483    
4484           ^.*+(?<=abcd)           ^.*+(?<=abcd)
4485    
4486         there can be no backtracking for the .*+ item; it can  match  only  the         there  can  be  no backtracking for the .*+ item; it can match only the
4487         entire  string.  The subsequent lookbehind assertion does a single test         entire string. The subsequent lookbehind assertion does a  single  test
4488         on the last four characters. If it fails, the match fails  immediately.         on  the last four characters. If it fails, the match fails immediately.
4489         For  long  strings, this approach makes a significant difference to the         For long strings, this approach makes a significant difference  to  the
4490         processing time.         processing time.
4491    
4492     Using multiple assertions     Using multiple assertions
# Line 4070  ASSERTIONS Line 4495  ASSERTIONS
4495    
4496           (?<=\d{3})(?<!999)foo           (?<=\d{3})(?<!999)foo
4497    
4498         matches "foo" preceded by three digits that are not "999". Notice  that         matches  "foo" preceded by three digits that are not "999". Notice that
4499         each  of  the  assertions is applied independently at the same point in         each of the assertions is applied independently at the  same  point  in
4500         the subject string. First there is a  check  that  the  previous  three         the  subject  string.  First  there  is a check that the previous three
4501         characters  are  all  digits,  and  then there is a check that the same         characters are all digits, and then there is  a  check  that  the  same
4502         three characters are not "999".  This pattern does not match "foo" pre-         three characters are not "999".  This pattern does not match "foo" pre-
4503         ceded  by  six  characters,  the first of which are digits and the last         ceded by six characters, the first of which are  digits  and  the  last
4504         three of which are not "999". For example, it  doesn't  match  "123abc-         three  of  which  are not "999". For example, it doesn't match "123abc-
4505         foo". A pattern to do that is         foo". A pattern to do that is
4506    
4507           (?<=\d{3}...)(?<!999)foo           (?<=\d{3}...)(?<!999)foo
4508    
4509         This  time  the  first assertion looks at the preceding six characters,         This time the first assertion looks at the  preceding  six  characters,
4510         checking that the first three are digits, and then the second assertion         checking that the first three are digits, and then the second assertion
4511         checks that the preceding three characters are not "999".         checks that the preceding three characters are not "999".
4512    
# Line 4089  ASSERTIONS Line 4514  ASSERTIONS
4514    
4515           (?<=(?<!foo)bar)baz           (?<=(?<!foo)bar)baz
4516    
4517         matches  an occurrence of "baz" that is preceded by "bar" which in turn         matches an occurrence of "baz" that is preceded by "bar" which in  turn
4518         is not preceded by "foo", while         is not preceded by "foo", while
4519    
4520           (?<=\d{3}(?!999)...)foo           (?<=\d{3}(?!999)...)foo
4521    
4522         is another pattern that matches "foo" preceded by three digits and  any         is  another pattern that matches "foo" preceded by three digits and any
4523         three characters that are not "999".         three characters that are not "999".
4524    
4525    
4526  CONDITIONAL SUBPATTERNS  CONDITIONAL SUBPATTERNS
4527    
4528         It  is possible to cause the matching process to obey a subpattern con-         It is possible to cause the matching process to obey a subpattern  con-
4529         ditionally or to choose between two alternative subpatterns,  depending         ditionally  or to choose between two alternative subpatterns, depending
4530         on  the result of an assertion, or whether a previous capturing subpat-         on the result of an assertion, or whether a previous capturing  subpat-
4531         tern matched or not. The two possible forms of  conditional  subpattern         tern  matched  or not. The two possible forms of conditional subpattern
4532         are         are
4533    
4534           (?(condition)yes-pattern)           (?(condition)yes-pattern)
4535           (?(condition)yes-pattern|no-pattern)           (?(condition)yes-pattern|no-pattern)
4536    
4537         If  the  condition is satisfied, the yes-pattern is used; otherwise the         If the condition is satisfied, the yes-pattern is used;  otherwise  the
4538         no-pattern (if present) is used. If there are more  than  two  alterna-         no-pattern  (if  present)  is used. If there are more than two alterna-
4539         tives in the subpattern, a compile-time error occurs.         tives in the subpattern, a compile-time error occurs.
4540    
4541         There  are  four  kinds of condition: references to subpatterns, refer-         There are four kinds of condition: references  to  subpatterns,  refer-
4542         ences to recursion, a pseudo-condition called DEFINE, and assertions.         ences to recursion, a pseudo-condition called DEFINE, and assertions.
4543    
4544     Checking for a used subpattern by number     Checking for a used subpattern by number
4545    
4546         If the text between the parentheses consists of a sequence  of  digits,         If  the  text between the parentheses consists of a sequence of digits,
4547         the  condition  is  true if the capturing subpattern of that number has         the condition is true if the capturing subpattern of  that  number  has
4548         previously matched. An alternative notation is to  precede  the  digits         previously  matched.  An  alternative notation is to precede the digits
4549         with a plus or minus sign. In this case, the subpattern number is rela-         with a plus or minus sign. In this case, the subpattern number is rela-
4550         tive rather than absolute.  The most recently opened parentheses can be         tive rather than absolute.  The most recently opened parentheses can be
4551         referenced  by  (?(-1),  the  next most recent by (?(-2), and so on. In         referenced by (?(-1), the next most recent by (?(-2),  and  so  on.  In
4552         looping constructs it can also make sense to refer to subsequent groups         looping constructs it can also make sense to refer to subsequent groups
4553         with constructs such as (?(+2).         with constructs such as (?(+2).
4554    
4555         Consider  the  following  pattern, which contains non-significant white         Consider the following pattern, which  contains  non-significant  white
4556         space to make it more readable (assume the PCRE_EXTENDED option) and to         space to make it more readable (assume the PCRE_EXTENDED option) and to
4557         divide it into three parts for ease of discussion:         divide it into three parts for ease of discussion:
4558    
4559           ( \( )?    [^()]+    (?(1) \) )           ( \( )?    [^()]+    (?(1) \) )
4560    
4561         The  first  part  matches  an optional opening parenthesis, and if that         The first part matches an optional opening  parenthesis,  and  if  that
4562         character is present, sets it as the first captured substring. The sec-         character is present, sets it as the first captured substring. The sec-
4563         ond  part  matches one or more characters that are not parentheses. The         ond part matches one or more characters that are not  parentheses.  The
4564         third part is a conditional subpattern that tests whether the first set         third part is a conditional subpattern that tests whether the first set
4565         of parentheses matched or not. If they did, that is, if subject started         of parentheses matched or not. If they did, that is, if subject started
4566         with an opening parenthesis, the condition is true, and so the yes-pat-         with an opening parenthesis, the condition is true, and so the yes-pat-
4567         tern  is  executed  and  a  closing parenthesis is required. Otherwise,         tern is executed and a  closing  parenthesis  is  required.  Otherwise,
4568         since no-pattern is not present, the  subpattern  matches  nothing.  In         since  no-pattern  is  not  present, the subpattern matches nothing. In
4569         other  words,  this  pattern  matches  a  sequence  of non-parentheses,         other words,  this  pattern  matches  a  sequence  of  non-parentheses,
4570         optionally enclosed in parentheses.         optionally enclosed in parentheses.
4571    
4572         If you were embedding this pattern in a larger one,  you  could  use  a         If  you  were  embedding  this pattern in a larger one, you could use a
4573         relative reference:         relative reference:
4574    
4575           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...
4576    
4577         This  makes  the  fragment independent of the parentheses in the larger         This makes the fragment independent of the parentheses  in  the  larger
4578         pattern.         pattern.
4579    
4580     Checking for a used subpattern by name     Checking for a used subpattern by name
4581    
4582         Perl uses the syntax (?(<name>)...) or (?('name')...)  to  test  for  a         Perl  uses  the  syntax  (?(<name>)...) or (?('name')...) to test for a
4583         used  subpattern  by  name.  For compatibility with earlier versions of         used subpattern by name. For compatibility  with  earlier  versions  of
4584         PCRE, which had this facility before Perl, the syntax  (?(name)...)  is         PCRE,  which  had this facility before Perl, the syntax (?(name)...) is
4585         also  recognized. However, there is a possible ambiguity with this syn-         also recognized. However, there is a possible ambiguity with this  syn-
4586         tax, because subpattern names may  consist  entirely  of  digits.  PCRE         tax,  because  subpattern  names  may  consist entirely of digits. PCRE
4587         looks  first for a named subpattern; if it cannot find one and the name         looks first for a named subpattern; if it cannot find one and the  name
4588         consists entirely of digits, PCRE looks for a subpattern of  that  num-         consists  entirely  of digits, PCRE looks for a subpattern of that num-
4589         ber,  which must be greater than zero. Using subpattern names that con-         ber, which must be greater than zero. Using subpattern names that  con-
4590         sist entirely of digits is not recommended.         sist entirely of digits is not recommended.
4591    
4592         Rewriting the above example to use a named subpattern gives this:         Rewriting the above example to use a named subpattern gives this:
# Line 4172  CONDITIONAL SUBPATTERNS Line 4597  CONDITIONAL SUBPATTERNS
4597     Checking for pattern recursion     Checking for pattern recursion
4598    
4599         If the condition is the string (R), and there is no subpattern with the         If the condition is the string (R), and there is no subpattern with the
4600         name  R, the condition is true if a recursive call to the whole pattern         name R, the condition is true if a recursive call to the whole  pattern
4601         or any subpattern has been made. If digits or a name preceded by amper-         or any subpattern has been made. If digits or a name preceded by amper-
4602         sand follow the letter R, for example:         sand follow the letter R, for example:
4603    
4604           (?(R3)...) or (?(R&name)...)           (?(R3)...) or (?(R&name)...)
4605    
4606         the  condition is true if the most recent recursion is into the subpat-         the condition is true if the most recent recursion is into the  subpat-
4607         tern whose number or name is given. This condition does not  check  the         tern  whose  number or name is given. This condition does not check the
4608         entire recursion stack.         entire recursion stack.
4609    
4610         At  "top  level", all these recursion test conditions are false. Recur-         At "top level", all these recursion test conditions are  false.  Recur-
4611         sive patterns are described below.         sive patterns are described below.
4612    
4613     Defining subpatterns for use by reference only     Defining subpatterns for use by reference only
4614    
4615         If the condition is the string (DEFINE), and  there  is  no  subpattern         If  the  condition  is  the string (DEFINE), and there is no subpattern
4616         with  the  name  DEFINE,  the  condition is always false. In this case,         with the name DEFINE, the condition is  always  false.  In  this  case,
4617         there may be only one alternative  in  the  subpattern.  It  is  always         there  may  be  only  one  alternative  in the subpattern. It is always
4618         skipped  if  control  reaches  this  point  in the pattern; the idea of         skipped if control reaches this point  in  the  pattern;  the  idea  of
4619         DEFINE is that it can be used to define "subroutines" that can be  ref-         DEFINE  is that it can be used to define "subroutines" that can be ref-
4620         erenced  from elsewhere. (The use of "subroutines" is described below.)         erenced from elsewhere. (The use of "subroutines" is described  below.)
4621         For example, a pattern to match an IPv4 address could be  written  like         For  example,  a pattern to match an IPv4 address could be written like
4622         this (ignore whitespace and line breaks):         this (ignore whitespace and line breaks):
4623    
4624           (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )           (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
4625           \b (?&byte) (\.(?&byte)){3} \b           \b (?&byte) (\.(?&byte)){3} \b
4626    
4627         The  first part of the pattern is a DEFINE group inside which a another         The first part of the pattern is a DEFINE group inside which a  another
4628         group named "byte" is defined. This matches an individual component  of         group  named "byte" is defined. This matches an individual component of
4629         an  IPv4  address  (a number less than 256). When matching takes place,         an IPv4 address (a number less than 256). When  matching  takes  place,
4630         this part of the pattern is skipped because DEFINE acts  like  a  false         this  part  of  the pattern is skipped because DEFINE acts like a false
4631         condition.         condition.
4632    
4633         The rest of the pattern uses references to the named group to match the         The rest of the pattern uses references to the named group to match the
4634         four dot-separated components of an IPv4 address, insisting on  a  word         four  dot-separated  components of an IPv4 address, insisting on a word
4635         boundary at each end.         boundary at each end.
4636    
4637     Assertion conditions     Assertion conditions
4638    
4639         If  the  condition  is  not  in any of the above formats, it must be an         If the condition is not in any of the above  formats,  it  must  be  an
4640         assertion.  This may be a positive or negative lookahead or  lookbehind         assertion.   This may be a positive or negative lookahead or lookbehind
4641         assertion.  Consider  this  pattern,  again  containing non-significant         assertion. Consider  this  pattern,  again  containing  non-significant
4642         white space, and with the two alternatives on the second line:         white space, and with the two alternatives on the second line:
4643    
4644           (?(?=[^a-z]*[a-z])           (?(?=[^a-z]*[a-z])
4645           \d{2}-[a-z]{3}-\d{2}  |  \d{2}-\d{2}-\d{2} )           \d{2}-[a-z]{3}-\d{2}  |  \d{2}-\d{2}-\d{2} )
4646    
4647         The condition  is  a  positive  lookahead  assertion  that  matches  an         The  condition  is  a  positive  lookahead  assertion  that  matches an
4648         optional  sequence of non-letters followed by a letter. In other words,         optional sequence of non-letters followed by a letter. In other  words,
4649         it tests for the presence of at least one letter in the subject.  If  a         it  tests  for the presence of at least one letter in the subject. If a
4650         letter  is found, the subject is matched against the first alternative;         letter is found, the subject is matched against the first  alternative;
4651         otherwise it is  matched  against  the  second.  This  pattern  matches         otherwise  it  is  matched  against  the  second.  This pattern matches
4652         strings  in  one  of the two forms dd-aaa-dd or dd-dd-dd, where aaa are         strings in one of the two forms dd-aaa-dd or dd-dd-dd,  where  aaa  are
4653         letters and dd are digits.         letters and dd are digits.
4654    
4655    
4656  COMMENTS  COMMENTS
4657    
4658         The sequence (?# marks the start of a comment that continues up to  the         The  sequence (?# marks the start of a comment that continues up to the
4659         next  closing  parenthesis.  Nested  parentheses are not permitted. The         next closing parenthesis. Nested parentheses  are  not  permitted.  The
4660         characters that make up a comment play no part in the pattern  matching         characters  that make up a comment play no part in the pattern matching
4661         at all.         at all.
4662    
4663         If  the PCRE_EXTENDED option is set, an unescaped # character outside a         If the PCRE_EXTENDED option is set, an unescaped # character outside  a
4664         character class introduces a  comment  that  continues  to  immediately         character  class  introduces  a  comment  that continues to immediately
4665         after the next newline in the pattern.         after the next newline in the pattern.
4666    
4667    
4668  RECURSIVE PATTERNS  RECURSIVE PATTERNS
4669    
4670         Consider  the problem of matching a string in parentheses, allowing for         Consider the problem of matching a string in parentheses, allowing  for
4671         unlimited nested parentheses. Without the use of  recursion,  the  best         unlimited  nested  parentheses.  Without the use of recursion, the best
4672         that  can  be  done  is  to use a pattern that matches up to some fixed         that can be done is to use a pattern that  matches  up  to  some  fixed
4673         depth of nesting. It is not possible to  handle  an  arbitrary  nesting         depth  of  nesting.  It  is not possible to handle an arbitrary nesting
4674         depth.         depth.
4675    
4676         For some time, Perl has provided a facility that allows regular expres-         For some time, Perl has provided a facility that allows regular expres-
4677         sions to recurse (amongst other things). It does this by  interpolating         sions  to recurse (amongst other things). It does this by interpolating
4678         Perl  code in the expression at run time, and the code can refer to the         Perl code in the expression at run time, and the code can refer to  the
4679         expression itself. A Perl pattern using code interpolation to solve the         expression itself. A Perl pattern using code interpolation to solve the
4680         parentheses problem can be created like this:         parentheses problem can be created like this:
4681    
# Line 4260  RECURSIVE PATTERNS Line 4685  RECURSIVE PATTERNS
4685         refers recursively to the pattern in which it appears.         refers recursively to the pattern in which it appears.
4686    
4687         Obviously, PCRE cannot support the interpolation of Perl code. Instead,         Obviously, PCRE cannot support the interpolation of Perl code. Instead,
4688         it  supports  special  syntax  for recursion of the entire pattern, and         it supports special syntax for recursion of  the  entire  pattern,  and
4689         also for individual subpattern recursion.  After  its  introduction  in         also  for  individual  subpattern  recursion. After its introduction in
4690         PCRE  and  Python,  this  kind of recursion was introduced into Perl at         PCRE and Python, this kind of recursion was  introduced  into  Perl  at
4691         release 5.10.         release 5.10.
4692    
4693         A special item that consists of (? followed by a  number  greater  than         A  special  item  that consists of (? followed by a number greater than
4694         zero and a closing parenthesis is a recursive call of the subpattern of         zero and a closing parenthesis is a recursive call of the subpattern of
4695         the given number, provided that it occurs inside that  subpattern.  (If         the  given  number, provided that it occurs inside that subpattern. (If
4696         not,  it  is  a  "subroutine" call, which is described in the next sec-         not, it is a "subroutine" call, which is described  in  the  next  sec-
4697         tion.) The special item (?R) or (?0) is a recursive call of the  entire         tion.)  The special item (?R) or (?0) is a recursive call of the entire
4698         regular expression.         regular expression.
4699    
4700         In  PCRE (like Python, but unlike Perl), a recursive subpattern call is         In PCRE (like Python, but unlike Perl), a recursive subpattern call  is
4701         always treated as an atomic group. That is, once it has matched some of         always treated as an atomic group. That is, once it has matched some of
4702         the subject string, it is never re-entered, even if it contains untried         the subject string, it is never re-entered, even if it contains untried
4703         alternatives and there is a subsequent matching failure.         alternatives and there is a subsequent matching failure.
4704    
4705         This PCRE pattern solves the nested  parentheses  problem  (assume  the         This  PCRE  pattern  solves  the nested parentheses problem (assume the
4706         PCRE_EXTENDED option is set so that white space is ignored):         PCRE_EXTENDED option is set so that white space is ignored):
4707    
4708           \( ( (?>[^()]+) | (?R) )* \)           \( ( (?>[^()]+) | (?R) )* \)
4709    
4710         First  it matches an opening parenthesis. Then it matches any number of         First it matches an opening parenthesis. Then it matches any number  of
4711         substrings which can either be a  sequence  of  non-parentheses,  or  a         substrings  which  can  either  be  a sequence of non-parentheses, or a
4712         recursive  match  of the pattern itself (that is, a correctly parenthe-         recursive match of the pattern itself (that is, a  correctly  parenthe-
4713         sized substring).  Finally there is a closing parenthesis.         sized substring).  Finally there is a closing parenthesis.
4714    
4715         If this were part of a larger pattern, you would not  want  to  recurse         If  this  were  part of a larger pattern, you would not want to recurse
4716         the entire pattern, so instead you could use this:         the entire pattern, so instead you could use this:
4717    
4718           ( \( ( (?>[^()]+) | (?1) )* \) )           ( \( ( (?>[^()]+) | (?1) )* \) )
4719    
4720         We  have  put the pattern into parentheses, and caused the recursion to         We have put the pattern into parentheses, and caused the  recursion  to
4721         refer to them instead of the whole pattern.         refer to them instead of the whole pattern.
4722    
4723         In a larger pattern,  keeping  track  of  parenthesis  numbers  can  be         In  a  larger  pattern,  keeping  track  of  parenthesis numbers can be
4724         tricky.  This is made easier by the use of relative references. (A Perl         tricky. This is made easier by the use of relative references. (A  Perl
4725         5.10 feature.)  Instead of (?1) in the  pattern  above  you  can  write         5.10  feature.)   Instead  of  (?1)  in the pattern above you can write
4726         (?-2) to refer to the second most recently opened parentheses preceding         (?-2) to refer to the second most recently opened parentheses preceding
4727         the recursion. In other  words,  a  negative  number  counts  capturing         the  recursion.  In  other  words,  a  negative number counts capturing
4728         parentheses leftwards from the point at which it is encountered.         parentheses leftwards from the point at which it is encountered.
4729    
4730         It  is  also  possible  to refer to subsequently opened parentheses, by         It is also possible to refer to  subsequently  opened  parentheses,  by
4731         writing references such as (?+2). However, these  cannot  be  recursive         writing  references  such  as (?+2). However, these cannot be recursive
4732         because  the  reference  is  not inside the parentheses that are refer-         because the reference is not inside the  parentheses  that  are  refer-
4733         enced. They are always "subroutine" calls, as  described  in  the  next         enced.  They  are  always  "subroutine" calls, as described in the next
4734         section.         section.
4735    
4736         An  alternative  approach is to use named parentheses instead. The Perl         An alternative approach is to use named parentheses instead.  The  Perl
4737         syntax for this is (?&name); PCRE's earlier syntax  (?P>name)  is  also         syntax  for  this  is (?&name); PCRE's earlier syntax (?P>name) is also
4738         supported. We could rewrite the above example as follows:         supported. We could rewrite the above example as follows:
4739    
4740           (?<pn> \( ( (?>[^()]+) | (?&pn) )* \) )           (?<pn> \( ( (?>[^()]+) | (?&pn) )* \) )
4741    
4742         If  there  is more than one subpattern with the same name, the earliest         If there is more than one subpattern with the same name,  the  earliest
4743         one is used.         one is used.
4744    
4745         This particular example pattern that we have been looking  at  contains         This  particular  example pattern that we have been looking at contains
4746         nested  unlimited repeats, and so the use of atomic grouping for match-         nested unlimited repeats, and so the use of atomic grouping for  match-
4747         ing strings of non-parentheses is important when applying  the  pattern         ing  strings  of non-parentheses is important when applying the pattern
4748         to strings that do not match. For example, when this pattern is applied         to strings that do not match. For example, when this pattern is applied
4749         to         to
4750    
4751           (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()           (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
4752    
4753         it yields "no match" quickly. However, if atomic grouping is not  used,         it  yields "no match" quickly. However, if atomic grouping is not used,
4754         the  match  runs  for a very long time indeed because there are so many         the match runs for a very long time indeed because there  are  so  many
4755         different ways the + and * repeats can carve up the  subject,  and  all         different  ways  the  + and * repeats can carve up the subject, and all
4756         have to be tested before failure can be reported.         have to be tested before failure can be reported.
4757    
4758         At the end of a match, the values set for any capturing subpatterns are         At the end of a match, the values set for any capturing subpatterns are
4759         those from the outermost level of the recursion at which the subpattern         those from the outermost level of the recursion at which the subpattern
4760         value  is  set.   If  you want to obtain intermediate values, a callout         value is set.  If you want to obtain  intermediate  values,  a  callout
4761         function can be used (see below and the pcrecallout documentation).  If         function  can be used (see below and the pcrecallout documentation). If
4762         the pattern above is matched against         the pattern above is matched against
4763    
4764           (ab(cd)ef)           (ab(cd)ef)
4765    
4766         the  value  for  the  capturing  parentheses is "ef", which is the last         the value for the capturing parentheses is  "ef",  which  is  the  last
4767         value taken on at the top level. If additional parentheses  are  added,         value  taken  on at the top level. If additional parentheses are added,
4768         giving         giving
4769    
4770           \( ( ( (?>[^()]+) | (?R) )* ) \)           \( ( ( (?>[^()]+) | (?R) )* ) \)
4771              ^                        ^              ^                        ^
4772              ^                        ^              ^                        ^
4773    
4774         the  string  they  capture is "ab(cd)ef", the contents of the top level         the string they capture is "ab(cd)ef", the contents of  the  top  level
4775         parentheses. If there are more than 15 capturing parentheses in a  pat-         parentheses.  If there are more than 15 capturing parentheses in a pat-
4776         tern, PCRE has to obtain extra memory to store data during a recursion,         tern, PCRE has to obtain extra memory to store data during a recursion,
4777         which it does by using pcre_malloc, freeing  it  via  pcre_free  after-         which  it  does  by  using pcre_malloc, freeing it via pcre_free after-
4778         wards.  If  no  memory  can  be  obtained,  the  match  fails  with the         wards. If  no  memory  can  be  obtained,  the  match  fails  with  the
4779         PCRE_ERROR_NOMEMORY error.         PCRE_ERROR_NOMEMORY error.
4780    
4781         Do not confuse the (?R) item with the condition (R),  which  tests  for         Do  not  confuse  the (?R) item with the condition (R), which tests for
4782         recursion.   Consider  this pattern, which matches text in angle brack-         recursion.  Consider this pattern, which matches text in  angle  brack-
4783         ets, allowing for arbitrary nesting. Only digits are allowed in  nested         ets,  allowing for arbitrary nesting. Only digits are allowed in nested
4784         brackets  (that is, when recursing), whereas any characters are permit-         brackets (that is, when recursing), whereas any characters are  permit-
4785         ted at the outer level.         ted at the outer level.
4786    
4787           < (?: (?(R) \d++  | [^<>]*+) | (?R)) * >           < (?: (?(R) \d++  | [^<>]*+) | (?R)) * >
4788    
4789         In this pattern, (?(R) is the start of a conditional  subpattern,  with         In  this  pattern, (?(R) is the start of a conditional subpattern, with
4790         two  different  alternatives for the recursive and non-recursive cases.         two different alternatives for the recursive and  non-recursive  cases.
4791         The (?R) item is the actual recursive call.         The (?R) item is the actual recursive call.
4792    
4793    
4794  SUBPATTERNS AS SUBROUTINES  SUBPATTERNS AS SUBROUTINES
4795    
4796         If the syntax for a recursive subpattern reference (either by number or         If the syntax for a recursive subpattern reference (either by number or
4797         by  name)  is used outside the parentheses to which it refers, it oper-         by name) is used outside the parentheses to which it refers,  it  oper-
4798         ates like a subroutine in a programming language. The "called"  subpat-         ates  like a subroutine in a programming language. The "called" subpat-
4799         tern may be defined before or after the reference. A numbered reference         tern may be defined before or after the reference. A numbered reference
4800         can be absolute or relative, as in these examples:         can be absolute or relative, as in these examples:
4801    
# Line 4382  SUBPATTERNS AS SUBROUTINES Line 4807  SUBPATTERNS AS SUBROUTINES
4807    
4808           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
4809    
4810         matches "sense and sensibility" and "response and responsibility",  but         matches  "sense and sensibility" and "response and responsibility", but
4811         not "sense and responsibility". If instead the pattern         not "sense and responsibility". If instead the pattern
4812    
4813           (sens|respons)e and (?1)ibility           (sens|respons)e and (?1)ibility
4814    
4815         is  used, it does match "sense and responsibility" as well as the other         is used, it does match "sense and responsibility" as well as the  other
4816         two strings. Another example is  given  in  the  discussion  of  DEFINE         two  strings.  Another  example  is  given  in the discussion of DEFINE
4817         above.         above.
4818    
4819         Like recursive subpatterns, a "subroutine" call is always treated as an         Like recursive subpatterns, a "subroutine" call is always treated as an
4820         atomic group. That is, once it has matched some of the subject  string,         atomic  group. That is, once it has matched some of the subject string,
4821         it  is  never  re-entered, even if it contains untried alternatives and         it is never re-entered, even if it contains  untried  alternatives  and
4822         there is a subsequent matching failure.         there is a subsequent matching failure.
4823    
4824         When a subpattern is used as a subroutine, processing options  such  as         When  a  subpattern is used as a subroutine, processing options such as
4825         case-independence are fixed when the subpattern is defined. They cannot         case-independence are fixed when the subpattern is defined. They cannot
4826         be changed for different calls. For example, consider this pattern:         be changed for different calls. For example, consider this pattern:
4827    
4828           (abc)(?i:(?-1))           (abc)(?i:(?-1))
4829    
4830         It matches "abcabc". It does not match "abcABC" because the  change  of         It  matches  "abcabc". It does not match "abcABC" because the change of
4831         processing option does not affect the called subpattern.         processing option does not affect the called subpattern.
4832    
4833    
4834    ONIGURUMA SUBROUTINE SYNTAX
4835    
4836           For compatibility with Oniguruma, the non-Perl syntax \g followed by  a
4837           name or a number enclosed either in angle brackets or single quotes, is
4838           an alternative syntax for referencing a  subpattern  as  a  subroutine,
4839           possibly  recursively. Here are two of the examples used above, rewrit-
4840           ten using this syntax:
4841    
4842             (?<pn> \( ( (?>[^()]+) | \g<pn> )* \) )
4843             (sens|respons)e and \g'1'ibility
4844    
4845           PCRE supports an extension to Oniguruma: if a number is preceded  by  a
4846           plus or a minus sign it is taken as a relative reference. For example:
4847    
4848             (abc)(?i:\g<-1>)
4849    
4850           Note  that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are not
4851           synonymous. The former is a back reference; the latter is a  subroutine
4852           call.
4853    
4854    
4855  CALLOUTS  CALLOUTS
4856    
4857         Perl has a feature whereby using the sequence (?{...}) causes arbitrary         Perl has a feature whereby using the sequence (?{...}) causes arbitrary
# Line 4441  CALLOUTS Line 4887  CALLOUTS
4887         is given in the pcrecallout documentation.         is given in the pcrecallout documentation.
4888    
4889    
4890    BACKTRACKING CONTROL
4891    
4892           Perl  5.10 introduced a number of "Special Backtracking Control Verbs",
4893           which are described in the Perl documentation as "experimental and sub-
4894           ject  to  change or removal in a future version of Perl". It goes on to
4895           say: "Their usage in production code should be noted to avoid  problems
4896           during upgrades." The same remarks apply to the PCRE features described
4897           in this section.
4898    
4899           Since these verbs are specifically related  to  backtracking,  most  of
4900           them  can  be  used  only  when  the  pattern  is  to  be matched using
4901           pcre_exec(), which uses a backtracking algorithm. With the exception of
4902           (*FAIL), which behaves like a failing negative assertion, they cause an
4903           error if encountered by pcre_dfa_exec().
4904    
4905           The new verbs make use of what was previously invalid syntax: an  open-
4906           ing parenthesis followed by an asterisk. In Perl, they are generally of
4907           the form (*VERB:ARG) but PCRE does not support the use of arguments, so
4908           its  general  form is just (*VERB). Any number of these verbs may occur
4909           in a pattern. There are two kinds:
4910    
4911       Verbs that act immediately
4912    
4913           The following verbs act as soon as they are encountered:
4914    
4915              (*ACCEPT)
4916    
4917           This verb causes the match to end successfully, skipping the  remainder
4918           of  the pattern. When inside a recursion, only the innermost pattern is
4919           ended immediately. PCRE differs  from  Perl  in  what  happens  if  the
4920           (*ACCEPT)  is inside capturing parentheses. In Perl, the data so far is
4921           captured: in PCRE no data is captured. For example:
4922    
4923             A(A|B(*ACCEPT)|C)D
4924    
4925           This matches "AB", "AAD", or "ACD", but when it matches "AB",  no  data
4926           is captured.
4927    
4928             (*FAIL) or (*F)
4929    
4930           This  verb  causes the match to fail, forcing backtracking to occur. It
4931           is equivalent to (?!) but easier to read. The Perl documentation  notes
4932           that  it  is  probably  useful only when combined with (?{}) or (??{}).
4933           Those are, of course, Perl features that are not present in  PCRE.  The
4934           nearest  equivalent is the callout feature, as for example in this pat-
4935           tern:
4936    
4937             a+(?C)(*FAIL)
4938    
4939           A match with the string "aaaa" always fails, but the callout  is  taken
4940           before each backtrack happens (in this example, 10 times).
4941    
4942       Verbs that act after backtracking
4943    
4944           The following verbs do nothing when they are encountered. Matching con-
4945           tinues with what follows, but if there is no subsequent match, a  fail-
4946           ure  is  forced.   The  verbs  differ  in  exactly what kind of failure
4947           occurs.
4948    
4949             (*COMMIT)
4950    
4951           This verb causes the whole match to fail outright if the  rest  of  the
4952           pattern  does  not match. Even if the pattern is unanchored, no further
4953           attempts to find a match by advancing the start point take place.  Once
4954           (*COMMIT)  has been passed, pcre_exec() is committed to finding a match
4955           at the current starting point, or not at all. For example:
4956    
4957             a+(*COMMIT)b
4958    
4959           This matches "xxaab" but not "aacaab". It can be thought of as  a  kind
4960           of dynamic anchor, or "I've started, so I must finish."
4961    
4962             (*PRUNE)
4963    
4964           This  verb causes the match to fail at the current position if the rest
4965           of the pattern does not match. If the pattern is unanchored, the normal
4966           "bumpalong"  advance to the next starting character then happens. Back-
4967           tracking can occur as usual to the left of (*PRUNE), or  when  matching
4968           to  the right of (*PRUNE), but if there is no match to the right, back-
4969           tracking cannot cross (*PRUNE).  In simple cases, the use  of  (*PRUNE)
4970           is just an alternative to an atomic group or possessive quantifier, but
4971           there are some uses of (*PRUNE) that cannot be expressed in  any  other
4972           way.
4973    
4974             (*SKIP)
4975    
4976           This  verb  is like (*PRUNE), except that if the pattern is unanchored,
4977           the "bumpalong" advance is not to the next character, but to the  posi-
4978           tion  in  the  subject where (*SKIP) was encountered. (*SKIP) signifies
4979           that whatever text was matched leading up to it cannot  be  part  of  a
4980           successful match. Consider:
4981    
4982             a+(*SKIP)b
4983    
4984           If  the  subject  is  "aaaac...",  after  the first match attempt fails
4985           (starting at the first character in the  string),  the  starting  point
4986           skips on to start the next attempt at "c". Note that a possessive quan-
4987           tifer does not have the same effect in this example; although it  would
4988           suppress  backtracking  during  the  first  match  attempt,  the second
4989           attempt would start at the second character instead of skipping  on  to
4990           "c".
4991    
4992             (*THEN)
4993    
4994           This verb causes a skip to the next alternation if the rest of the pat-
4995           tern does not match. That is, it cancels pending backtracking, but only
4996           within  the  current  alternation.  Its name comes from the observation
4997           that it can be used for a pattern-based if-then-else block:
4998    
4999             ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
5000    
5001           If the COND1 pattern matches, FOO is tried (and possibly further  items
5002           after  the  end  of  the group if FOO succeeds); on failure the matcher
5003           skips to the second alternative and tries COND2,  without  backtracking
5004           into  COND1.  If  (*THEN)  is  used outside of any alternation, it acts
5005           exactly like (*PRUNE).
5006    
5007    
5008  SEE ALSO  SEE ALSO
5009    
5010         pcreapi(3), pcrecallout(3), pcrematching(3), pcre(3).         pcreapi(3), pcrecallout(3), pcrematching(3), pcre(3).
# Line 4455  AUTHOR Line 5019  AUTHOR
5019    
5020  REVISION  REVISION
5021    
5022         Last updated: 29 May 2007         Last updated: 08 March 2009
5023         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
5024    ------------------------------------------------------------------------------
5025    
5026    
5027    PCRESYNTAX(3)                                                    PCRESYNTAX(3)
5028    
5029    
5030    NAME
5031           PCRE - Perl-compatible regular expressions
5032    
5033    
5034    PCRE REGULAR EXPRESSION SYNTAX SUMMARY
5035    
5036           The  full syntax and semantics of the regular expressions that are sup-
5037           ported by PCRE are described in  the  pcrepattern  documentation.  This
5038           document contains just a quick-reference summary of the syntax.
5039    
5040    
5041    QUOTING
5042    
5043             \x         where x is non-alphanumeric is a literal x
5044             \Q...\E    treat enclosed characters as literal
5045    
5046    
5047    CHARACTERS
5048    
5049             \a         alarm, that is, the BEL character (hex 07)
5050             \cx        "control-x", where x is any character
5051             \e         escape (hex 1B)
5052             \f         formfeed (hex 0C)
5053             \n         newline (hex 0A)
5054             \r         carriage return (hex 0D)
5055             \t         tab (hex 09)
5056             \ddd       character with octal code ddd, or backreference
5057             \xhh       character with hex code hh
5058             \x{hhh..}  character with hex code hhh..
5059    
5060    
5061    CHARACTER TYPES
5062    
5063             .          any character except newline;
5064                          in dotall mode, any character whatsoever
5065             \C         one byte, even in UTF-8 mode (best avoided)
5066             \d         a decimal digit
5067             \D         a character that is not a decimal digit
5068             \h         a horizontal whitespace character
5069             \H         a character that is not a horizontal whitespace character
5070             \p{xx}     a character with the xx property
5071             \P{xx}     a character without the xx property
5072             \R         a newline sequence
5073             \s         a whitespace character
5074             \S         a character that is not a whitespace character
5075             \v         a vertical whitespace character
5076             \V         a character that is not a vertical whitespace character
5077             \w         a "word" character
5078             \W         a "non-word" character
5079             \X         an extended Unicode sequence
5080    
5081           In PCRE, \d, \D, \s, \S, \w, and \W recognize only ASCII characters.
5082    
5083    
5084    GENERAL CATEGORY PROPERTY CODES FOR \p and \P
5085    
5086             C          Other
5087             Cc         Control
5088             Cf         Format
5089             Cn         Unassigned
5090             Co         Private use
5091             Cs         Surrogate
5092    
5093             L          Letter
5094             Ll         Lower case letter
5095             Lm         Modifier letter
5096             Lo         Other letter
5097             Lt         Title case letter
5098             Lu         Upper case letter
5099             L&         Ll, Lu, or Lt
5100    
5101             M          Mark
5102             Mc         Spacing mark
5103             Me         Enclosing mark
5104             Mn         Non-spacing mark
5105    
5106             N          Number
5107             Nd         Decimal number
5108             Nl         Letter number
5109             No         Other number
5110    
5111             P          Punctuation
5112             Pc         Connector punctuation
5113             Pd         Dash punctuation
5114             Pe         Close punctuation
5115             Pf         Final punctuation
5116             Pi         Initial punctuation
5117             Po         Other punctuation
5118             Ps         Open punctuation
5119    
5120             S          Symbol
5121             Sc         Currency symbol
5122             Sk         Modifier symbol
5123             Sm         Mathematical symbol
5124             So         Other symbol
5125    
5126             Z          Separator
5127             Zl         Line separator
5128             Zp         Paragraph separator
5129             Zs         Space separator
5130    
5131    
5132    SCRIPT NAMES FOR \p AND \P
5133    
5134           Arabic,  Armenian,  Balinese,  Bengali,  Bopomofo,  Braille,  Buginese,
5135           Buhid,  Canadian_Aboriginal,  Cherokee,  Common,   Coptic,   Cuneiform,
5136           Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic,
5137           Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew,  Hira-
5138           gana,  Inherited,  Kannada,  Katakana,  Kharoshthi,  Khmer, Lao, Latin,
5139           Limbu,  Linear_B,  Malayalam,  Mongolian,  Myanmar,  New_Tai_Lue,  Nko,
5140           Ogham,  Old_Italic,  Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician,
5141           Runic,  Shavian,  Sinhala,  Syloti_Nagri,  Syriac,  Tagalog,  Tagbanwa,
5142           Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi.
5143    
5144    
5145    CHARACTER CLASSES
5146    
5147             [...]       positive character class
5148             [^...]      negative character class
5149             [x-y]       range (can be used for hex characters)
5150             [[:xxx:]]   positive POSIX named set
5151             [[:^xxx:]]  negative POSIX named set
5152    
5153             alnum       alphanumeric
5154             alpha       alphabetic
5155             ascii       0-127
5156             blank       space or tab
5157             cntrl       control character
5158             digit       decimal digit
5159             graph       printing, excluding space
5160             lower       lower case letter
5161             print       printing, including space
5162             punct       printing, excluding alphanumeric
5163             space       whitespace
5164             upper       upper case letter
5165             word        same as \w
5166             xdigit      hexadecimal digit
5167    
5168           In PCRE, POSIX character set names recognize only ASCII characters. You
5169           can use \Q...\E inside a character class.
5170    
5171    
5172    QUANTIFIERS
5173    
5174             ?           0 or 1, greedy
5175             ?+          0 or 1, possessive
5176             ??          0 or 1, lazy
5177             *           0 or more, greedy
5178             *+          0 or more, possessive
5179             *?          0 or more, lazy
5180             +           1 or more, greedy
5181             ++          1 or more, possessive
5182             +?          1 or more, lazy
5183             {n}         exactly n
5184             {n,m}       at least n, no more than m, greedy
5185             {n,m}+      at least n, no more than m, possessive
5186             {n,m}?      at least n, no more than m, lazy
5187             {n,}        n or more, greedy
5188             {n,}+       n or more, possessive
5189             {n,}?       n or more, lazy
5190    
5191    
5192    ANCHORS AND SIMPLE ASSERTIONS
5193    
5194             \b          word boundary
5195             \B          not a word boundary
5196             ^           start of subject
5197                          also after internal newline in multiline mode
5198             \A          start of subject
5199             $           end of subject
5200                          also before newline at end of subject
5201                          also before internal newline in multiline mode
5202             \Z          end of subject
5203                          also before newline at end of subject
5204             \z          end of subject
5205             \G          first matching position in subject
5206    
5207    
5208    MATCH POINT RESET
5209    
5210             \K          reset start of match
5211    
5212    
5213    ALTERNATION
5214    
5215             expr|expr|expr...
5216    
5217    
5218    CAPTURING
5219    
5220             (...)          capturing group
5221             (?<name>...)   named capturing group (Perl)
5222             (?'name'...)   named capturing group (Perl)
5223             (?P<name>...)  named capturing group (Python)
5224             (?:...)        non-capturing group
5225             (?|...)        non-capturing group; reset group numbers for
5226                             capturing groups in each alternative
5227    
5228    
5229    ATOMIC GROUPS
5230    
5231             (?>...)        atomic, non-capturing group
5232    
5233    
5234    COMMENT
5235    
5236             (?#....)       comment (not nestable)
5237    
5238    
5239    OPTION SETTING
5240    
5241             (?i)           caseless
5242             (?J)           allow duplicate names
5243             (?m)           multiline
5244             (?s)           single line (dotall)
5245             (?U)           default ungreedy (lazy)
5246             (?x)           extended (ignore white space)
5247             (?-...)        unset option(s)
5248    
5249    
5250    LOOKAHEAD AND LOOKBEHIND ASSERTIONS
5251    
5252             (?=...)        positive look ahead
5253             (?!...)        negative look ahead
5254             (?<=...)       positive look behind
5255             (?<!...)       negative look behind
5256    
5257           Each top-level branch of a look behind must be of a fixed length.
5258    
5259    
5260    BACKREFERENCES
5261    
5262             \n             reference by number (can be ambiguous)
5263             \gn            reference by number
5264             \g{n}          reference by number
5265             \g{-n}         relative reference by number
5266             \k<name>       reference by name (Perl)
5267             \k'name'       reference by name (Perl)
5268             \g{name}       reference by name (Perl)
5269             \k{name}       reference by name (.NET)
5270             (?P=name)      reference by name (Python)
5271    
5272    
5273    SUBROUTINE REFERENCES (POSSIBLY RECURSIVE)
5274    
5275             (?R)           recurse whole pattern
5276             (?n)           call subpattern by absolute number
5277             (?+n)          call subpattern by relative number
5278             (?-n)          call subpattern by relative number
5279             (?&name)       call subpattern by name (Perl)
5280             (?P>name)      call subpattern by name (Python)
5281             \g<name>       call subpattern by name (Oniguruma)
5282             \g'name'       call subpattern by name (Oniguruma)
5283             \g<n>          call subpattern by absolute number (Oniguruma)
5284             \g'n'          call subpattern by absolute number (Oniguruma)
5285             \g<+n>         call subpattern by relative number (PCRE extension)
5286             \g'+n'         call subpattern by relative number (PCRE extension)
5287             \g<-n>         call subpattern by relative number (PCRE extension)
5288             \g'-n'         call subpattern by relative number (PCRE extension)
5289    
5290    
5291    CONDITIONAL PATTERNS
5292    
5293             (?(condition)yes-pattern)
5294             (?(condition)yes-pattern|no-pattern)
5295    
5296             (?(n)...       absolute reference condition
5297             (?(+n)...      relative reference condition
5298             (?(-n)...      relative reference condition
5299             (?(<name>)...  named reference condition (Perl)
5300             (?('name')...  named reference condition (Perl)
5301             (?(name)...    named reference condition (PCRE)
5302             (?(R)...       overall recursion condition
5303             (?(Rn)...      specific group recursion condition
5304             (?(R&name)...  specific recursion condition
5305             (?(DEFINE)...  define subpattern for reference
5306             (?(assert)...  assertion condition
5307    
5308    
5309    BACKTRACKING CONTROL
5310    
5311           The following act immediately they are reached:
5312    
5313             (*ACCEPT)      force successful match
5314             (*FAIL)        force backtrack; synonym (*F)
5315    
5316           The following act only when a subsequent match failure causes  a  back-
5317           track to reach them. They all force a match failure, but they differ in
5318           what happens afterwards. Those that advance the start-of-match point do
5319           so only if the pattern is not anchored.
5320    
5321             (*COMMIT)      overall failure, no advance of starting point
5322             (*PRUNE)       advance to next starting character
5323             (*SKIP)        advance start to current matching position
5324             (*THEN)        local failure, backtrack to next alternation
5325    
5326    
5327    NEWLINE CONVENTIONS
5328    
5329           These  are  recognized only at the very start of the pattern or after a
5330           (*BSR_...) option.
5331    
5332             (*CR)
5333             (*LF)
5334             (*CRLF)
5335             (*ANYCRLF)
5336             (*ANY)
5337    
5338    
5339    WHAT \R MATCHES
5340    
5341           These are recognized only at the very start of the pattern or  after  a
5342           (*...) option that sets the newline convention.
5343    
5344             (*BSR_ANYCRLF)
5345             (*BSR_UNICODE)
5346    
5347    
5348    CALLOUTS
5349    
5350             (?C)      callout
5351             (?Cn)     callout with data n
5352    
5353    
5354    SEE ALSO
5355    
5356           pcrepattern(3), pcreapi(3), pcrecallout(3), pcrematching(3), pcre(3).
5357    
5358    
5359    AUTHOR
5360    
5361           Philip Hazel
5362           University Computing Service
5363           Cambridge CB2 3QH, England.
5364    
5365    
5366    REVISION
5367    
5368           Last updated: 09 April 2008
5369           Copyright (c) 1997-2008 University of Cambridge.
5370  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
5371    
5372    
# Line 4638  MULTI-SEGMENT MATCHING WITH pcre_dfa_exe Line 5548  MULTI-SEGMENT MATCHING WITH pcre_dfa_exe
5548            0: dogsbody            0: dogsbody
5549            1: dog            1: dog
5550    
5551         The  pattern matches the words "dog" or "dogsbody". When the subject is         The pattern matches the words "dog" or "dogsbody". When the subject  is
5552         presented in several parts ("do" and "gsb" being  the  first  two)  the         presented  in  several  parts  ("do" and "gsb" being the first two) the
5553         match  stops  when "dog" has been found, and it is not possible to con-         match stops when "dog" has been found, and it is not possible  to  con-
5554         tinue. On the other hand,  if  "dogsbody"  is  presented  as  a  single         tinue.  On  the  other  hand,  if  "dogsbody"  is presented as a single
5555         string, both matches are found.         string, both matches are found.
5556    
5557         Because  of  this  phenomenon,  it does not usually make sense to end a         Because of this phenomenon, it does not usually make  sense  to  end  a
5558         pattern that is going to be matched in this way with a variable repeat.         pattern that is going to be matched in this way with a variable repeat.
5559    
5560         4. Patterns that contain alternatives at the top level which do not all         4. Patterns that contain alternatives at the top level which do not all
# Line 4786  RE-USING A PRECOMPILED PATTERN Line 5696  RE-USING A PRECOMPILED PATTERN
5696    
5697  COMPATIBILITY WITH DIFFERENT PCRE RELEASES  COMPATIBILITY WITH DIFFERENT PCRE RELEASES
5698    
5699         The layout of the control block that is at the start of the  data  that         In general, it is safest to  recompile  all  saved  patterns  when  you
5700         makes  up  a  compiled pattern was changed for release 5.0. If you have         update  to  a new PCRE release, though not all updates actually require
5701         any saved patterns that were compiled with  previous  releases  (not  a         this. Recompiling is definitely needed for release 7.2.
        facility  that  was  previously advertised), you will have to recompile  
        them for release 5.0 and above.  
   
        If you have any saved patterns in UTF-8 mode that use  \p  or  \P  that  
        were  compiled  with any release up to and including 6.4, you will have  
        to recompile them for release 6.5 and above.  
   
        All saved patterns from earlier releases must be recompiled for release  
        7.0  or  higher,  because  there was an internal reorganization at that  
        release.  
5702    
5703    
5704  AUTHOR  AUTHOR
# Line 4810  AUTHOR Line 5710  AUTHOR
5710    
5711  REVISION  REVISION
5712    
5713         Last updated: 24 April 2007         Last updated: 13 June 2007
5714         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
5715  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
5716    
# Line 5001  DESCRIPTION Line 5901  DESCRIPTION
5901         command  for  linking  an application that uses them. Because the POSIX         command  for  linking  an application that uses them. Because the POSIX
5902         functions call the native ones, it is also necessary to add -lpcre.         functions call the native ones, it is also necessary to add -lpcre.
5903    
5904         I have implemented only those option bits that can be reasonably mapped         I have implemented only those POSIX option bits that can be  reasonably
5905         to PCRE native options. In addition, the option REG_EXTENDED is defined         mapped  to PCRE native options. In addition, the option REG_EXTENDED is
5906         with the value zero. This has no effect, but since  programs  that  are         defined with the value zero. This has no  effect,  but  since  programs
5907         written  to  the  POSIX interface often use it, this makes it easier to         that  are  written  to  the POSIX interface often use it, this makes it
5908         slot in PCRE as a replacement library. Other POSIX options are not even         easier to slot in PCRE as a replacement library.  Other  POSIX  options
5909         defined.         are not even defined.
5910    
5911         When  PCRE  is  called  via these functions, it is only the API that is         When  PCRE  is  called  via these functions, it is only the API that is
5912         POSIX-like in style. The syntax and semantics of  the  regular  expres-         POSIX-like in style. The syntax and semantics of  the  regular  expres-
# Line 5086  COMPILING A PATTERN Line 5986  COMPILING A PATTERN
5986  MATCHING NEWLINE CHARACTERS  MATCHING NEWLINE CHARACTERS
5987    
5988         This area is not simp