/[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 211 by ph10, Thu Aug 9 09:52:43 2007 UTC revision 371 by ph10, Mon Aug 25 18:28:05 2008 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 256  AUTHOR Line 258  AUTHOR
258    
259  REVISION  REVISION
260    
261         Last updated: 09 August 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 271  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 304  UTF-8 SUPPORT Line 311  UTF-8 SUPPORT
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    
320  UNICODE CHARACTER PROPERTY SUPPORT  UNICODE CHARACTER PROPERTY SUPPORT
321    
322         UTF-8 support allows PCRE to process character values greater than  255         UTF-8  support allows PCRE to process character values greater than 255
323         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-
324         vide any facilities for accessing the properties of such characters. If         vide any facilities for accessing the properties of such characters. If
325         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
326         refer to Unicode character properties, you must add         refer to Unicode character properties, you must add
327    
328           --enable-unicode-properties           --enable-unicode-properties
329    
330         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
331         not explicitly requested it.         not explicitly requested it.
332    
333         Including  Unicode  property  support  adds around 30K of tables to the         Including Unicode property support adds around 30K  of  tables  to  the
334         PCRE library. Only the general category properties such as  Lu  and  Nd         PCRE  library.  Only  the general category properties such as Lu and Nd
335         are supported. Details are given in the pcrepattern documentation.         are supported. Details are given in the pcrepattern documentation.
336    
337    
338  CODE VALUE OF NEWLINE  CODE VALUE OF NEWLINE
339    
340         By  default,  PCRE interprets character 10 (linefeed, LF) as indicating         By default, PCRE interprets character 10 (linefeed, LF)  as  indicating
341         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
342         systems. You can compile PCRE to use character 13 (carriage return, CR)         systems. You can compile PCRE to use character 13 (carriage return, CR)
343         instead, by adding         instead, by adding
344    
345           --enable-newline-is-cr           --enable-newline-is-cr
346    
347         to the  configure  command.  There  is  also  a  --enable-newline-is-lf         to  the  configure  command.  There  is  also  a --enable-newline-is-lf
348         option, which explicitly specifies linefeed as the newline character.         option, which explicitly specifies linefeed as the newline character.
349    
350         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 349  CODE VALUE OF NEWLINE Line 356  CODE VALUE OF NEWLINE
356    
357           --enable-newline-is-anycrlf           --enable-newline-is-anycrlf
358    
359         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
360         CRLF as indicating a line ending. Finally, a fifth option, specified by         CRLF as indicating a line ending. Finally, a fifth option, specified by
361    
362           --enable-newline-is-any           --enable-newline-is-any
363    
364         causes PCRE to recognize any Unicode newline sequence.         causes PCRE to recognize any Unicode newline sequence.
365    
366         Whatever line ending convention is selected when PCRE is built  can  be         Whatever  line  ending convention is selected when PCRE is built can be
367         overridden  when  the library functions are called. At build time it is         overridden when the library functions are called. At build time  it  is
368         conventional to use the standard for your operating system.         conventional to use the standard for your operating system.
369    
370    
371    WHAT \R MATCHES
372    
373           By  default,  the  sequence \R in a pattern matches any Unicode newline
374           sequence, whatever has been selected as the line  ending  sequence.  If
375           you specify
376    
377             --enable-bsr-anycrlf
378    
379           the  default  is changed so that \R matches only CR, LF, or CRLF. What-
380           ever is selected when PCRE is built can be overridden when the  library
381           functions are called.
382    
383    
384  BUILDING SHARED AND STATIC LIBRARIES  BUILDING SHARED AND STATIC LIBRARIES
385    
386         The PCRE building process uses libtool to build both shared and  static         The  PCRE building process uses libtool to build both shared and static
387         Unix  libraries by default. You can suppress one of these by adding one         Unix libraries by default. You can suppress one of these by adding  one
388         of         of
389    
390           --disable-shared           --disable-shared
# Line 376  BUILDING SHARED AND STATIC LIBRARIES Line 396  BUILDING SHARED AND STATIC LIBRARIES
396  POSIX MALLOC USAGE  POSIX MALLOC USAGE
397    
398         When PCRE is called through the POSIX interface (see the pcreposix doc-         When PCRE is called through the POSIX interface (see the pcreposix doc-
399         umentation),  additional  working  storage  is required for holding the         umentation), additional working storage is  required  for  holding  the
400         pointers to capturing substrings, because PCRE requires three  integers         pointers  to capturing substrings, because PCRE requires three integers
401         per  substring,  whereas  the POSIX interface provides only two. If the         per substring, whereas the POSIX interface provides only  two.  If  the
402         number of expected substrings is small, the wrapper function uses space         number of expected substrings is small, the wrapper function uses space
403         on the stack, because this is faster than using malloc() for each call.         on the stack, because this is faster than using malloc() for each call.
404         The default threshold above which the stack is no longer used is 10; it         The default threshold above which the stack is no longer used is 10; it
# Line 391  POSIX MALLOC USAGE Line 411  POSIX MALLOC USAGE
411    
412  HANDLING VERY LARGE PATTERNS  HANDLING VERY LARGE PATTERNS
413    
414         Within  a  compiled  pattern,  offset values are used to point from one         Within a compiled pattern, offset values are used  to  point  from  one
415         part to another (for example, from an opening parenthesis to an  alter-         part  to another (for example, from an opening parenthesis to an alter-
416         nation  metacharacter).  By default, two-byte values are used for these         nation metacharacter). By default, two-byte values are used  for  these
417         offsets, leading to a maximum size for a  compiled  pattern  of  around         offsets,  leading  to  a  maximum size for a compiled pattern of around
418         64K.  This  is sufficient to handle all but the most gigantic patterns.         64K. This is sufficient to handle all but the most  gigantic  patterns.
419         Nevertheless, some people do want to process enormous patterns,  so  it         Nevertheless,  some  people do want to process enormous patterns, so it
420         is  possible  to compile PCRE to use three-byte or four-byte offsets by         is possible to compile PCRE to use three-byte or four-byte  offsets  by
421         adding a setting such as         adding a setting such as
422    
423           --with-link-size=3           --with-link-size=3
424    
425         to the configure command. The value given must be 2,  3,  or  4.  Using         to  the  configure  command.  The value given must be 2, 3, or 4. Using
426         longer  offsets slows down the operation of PCRE because it has to load         longer offsets slows down the operation of PCRE because it has to  load
427         additional bytes when handling them.         additional bytes when handling them.
428    
429    
430  AVOIDING EXCESSIVE STACK USAGE  AVOIDING EXCESSIVE STACK USAGE
431    
432         When matching with the pcre_exec() function, PCRE implements backtrack-         When matching with the pcre_exec() function, PCRE implements backtrack-
433         ing  by  making recursive calls to an internal function called match().         ing by making recursive calls to an internal function  called  match().
434         In environments where the size of the stack is limited,  this  can  se-         In  environments  where  the size of the stack is limited, this can se-
435         verely  limit  PCRE's operation. (The Unix environment does not usually         verely limit PCRE's operation. (The Unix environment does  not  usually
436         suffer from this problem, but it may sometimes be necessary to increase         suffer from this problem, but it may sometimes be necessary to increase
437         the  maximum  stack size.  There is a discussion in the pcrestack docu-         the maximum stack size.  There is a discussion in the  pcrestack  docu-
438         mentation.) An alternative approach to recursion that uses memory  from         mentation.)  An alternative approach to recursion that uses memory from
439         the  heap  to remember data, instead of using recursive function calls,         the heap to remember data, instead of using recursive  function  calls,
440         has been implemented to work round the problem of limited  stack  size.         has  been  implemented to work round the problem of limited stack size.
441         If you want to build a version of PCRE that works this way, add         If you want to build a version of PCRE that works this way, add
442    
443           --disable-stack-for-recursion           --disable-stack-for-recursion
444    
445         to  the  configure  command. With this configuration, PCRE will use the         to the configure command. With this configuration, PCRE  will  use  the
446         pcre_stack_malloc and pcre_stack_free variables to call memory  manage-         pcre_stack_malloc  and pcre_stack_free variables to call memory manage-
447         ment  functions. By default these point to malloc() and free(), but you         ment functions. By default these point to malloc() and free(), but  you
448         can replace the pointers so that your own functions are used.         can replace the pointers so that your own functions are used.
449    
450         Separate functions are  provided  rather  than  using  pcre_malloc  and         Separate  functions  are  provided  rather  than  using pcre_malloc and
451         pcre_free  because  the  usage  is  very  predictable:  the block sizes         pcre_free because the  usage  is  very  predictable:  the  block  sizes
452         requested are always the same, and  the  blocks  are  always  freed  in         requested  are  always  the  same,  and  the blocks are always freed in
453         reverse  order.  A calling program might be able to implement optimized         reverse order. A calling program might be able to  implement  optimized
454         functions that perform better  than  malloc()  and  free().  PCRE  runs         functions  that  perform  better  than  malloc()  and free(). PCRE runs
455         noticeably more slowly when built in this way. This option affects only         noticeably more slowly when built in this way. This option affects only
456         the  pcre_exec()  function;  it   is   not   relevant   for   the   the         the   pcre_exec()   function;   it   is   not   relevant  for  the  the
457         pcre_dfa_exec() function.         pcre_dfa_exec() function.
458    
459    
460  LIMITING PCRE RESOURCE USAGE  LIMITING PCRE RESOURCE USAGE
461    
462         Internally,  PCRE has a function called match(), which it calls repeat-         Internally, PCRE has a function called match(), which it calls  repeat-
463         edly  (sometimes  recursively)  when  matching  a  pattern   with   the         edly   (sometimes   recursively)  when  matching  a  pattern  with  the
464         pcre_exec()  function.  By controlling the maximum number of times this         pcre_exec() function. By controlling the maximum number of  times  this
465         function may be called during a single matching operation, a limit  can         function  may be called during a single matching operation, a limit can
466         be  placed  on  the resources used by a single call to pcre_exec(). The         be placed on the resources used by a single call  to  pcre_exec().  The
467         limit can be changed at run time, as described in the pcreapi  documen-         limit  can be changed at run time, as described in the pcreapi documen-
468         tation.  The default is 10 million, but this can be changed by adding a         tation. The default is 10 million, but this can be changed by adding  a
469         setting such as         setting such as
470    
471           --with-match-limit=500000           --with-match-limit=500000
472    
473         to  the  configure  command.  This  setting  has  no  effect   on   the         to   the   configure  command.  This  setting  has  no  effect  on  the
474         pcre_dfa_exec() matching function.         pcre_dfa_exec() matching function.
475    
476         In  some  environments  it is desirable to limit the depth of recursive         In some environments it is desirable to limit the  depth  of  recursive
477         calls of match() more strictly than the total number of calls, in order         calls of match() more strictly than the total number of calls, in order
478         to  restrict  the maximum amount of stack (or heap, if --disable-stack-         to restrict the maximum amount of stack (or heap,  if  --disable-stack-
479         for-recursion is specified) that is used. A second limit controls this;         for-recursion is specified) that is used. A second limit controls this;
480         it  defaults  to  the  value  that is set for --with-match-limit, which         it defaults to the value that  is  set  for  --with-match-limit,  which
481         imposes no additional constraints. However, you can set a  lower  limit         imposes  no  additional constraints. However, you can set a lower limit
482         by adding, for example,         by adding, for example,
483    
484           --with-match-limit-recursion=10000           --with-match-limit-recursion=10000
485    
486         to  the  configure  command.  This  value can also be overridden at run         to the configure command. This value can  also  be  overridden  at  run
487         time.         time.
488    
489    
490  CREATING CHARACTER TABLES AT BUILD TIME  CREATING CHARACTER TABLES AT BUILD TIME
491    
492         PCRE uses fixed tables for processing characters whose code values  are         PCRE  uses fixed tables for processing characters whose code values are
493         less  than 256. By default, PCRE is built with a set of tables that are         less than 256. By default, PCRE is built with a set of tables that  are
494         distributed in the file pcre_chartables.c.dist. These  tables  are  for         distributed  in  the  file pcre_chartables.c.dist. These tables are for
495         ASCII codes only. If you add         ASCII codes only. If you add
496    
497           --enable-rebuild-chartables           --enable-rebuild-chartables
498    
499         to  the  configure  command, the distributed tables are no longer used.         to the configure command, the distributed tables are  no  longer  used.
500         Instead, a program called dftables is compiled and  run.  This  outputs         Instead,  a  program  called dftables is compiled and run. This outputs
501         the source for new set of tables, created in the default locale of your         the source for new set of tables, created in the default locale of your
502         C runtime system. (This method of replacing the tables does not work if         C runtime system. (This method of replacing the tables does not work if
503         you  are cross compiling, because dftables is run on the local host. If         you are cross compiling, because dftables is run on the local host.  If
504         you need to create alternative tables when cross  compiling,  you  will         you  need  to  create alternative tables when cross compiling, you will
505         have to do so "by hand".)         have to do so "by hand".)
506    
507    
508  USING EBCDIC CODE  USING EBCDIC CODE
509    
510         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
511         character code is ASCII (or Unicode, which is  a  superset  of  ASCII).         character  code  is  ASCII  (or Unicode, which is a superset of ASCII).
512         This  is  the  case for most computer operating systems. PCRE can, how-         This is the case for most computer operating systems.  PCRE  can,  how-
513         ever, be compiled to run in an EBCDIC environment by adding         ever, be compiled to run in an EBCDIC environment by adding
514    
515           --enable-ebcdic           --enable-ebcdic
516    
517         to the configure command. This setting implies --enable-rebuild-charta-         to the configure command. This setting implies --enable-rebuild-charta-
518         bles.  You  should  only  use  it if you know that you are in an EBCDIC         bles. You should only use it if you know that  you  are  in  an  EBCDIC
519         environment (for example, an IBM mainframe operating system).         environment (for example, an IBM mainframe operating system).
520    
521    
522    PCREGREP OPTIONS FOR COMPRESSED FILE SUPPORT
523    
524           By default, pcregrep reads all files as plain text. You can build it so
525           that it recognizes files whose names end in .gz or .bz2, and reads them
526           with libz or libbz2, respectively, by adding one or both of
527    
528             --enable-pcregrep-libz
529             --enable-pcregrep-libbz2
530    
531           to the configure command. These options naturally require that the rel-
532           evant libraries are installed on your system. Configuration  will  fail
533           if they are not.
534    
535    
536    PCRETEST OPTION FOR LIBREADLINE SUPPORT
537    
538           If you add
539    
540             --enable-pcretest-libreadline
541    
542           to  the  configure  command,  pcretest  is  linked with the libreadline
543           library, and when its input is from a terminal, it reads it  using  the
544           readline() function. This provides line-editing and history facilities.
545           Note that libreadline is GPL-licenced, so if you distribute a binary of
546           pcretest linked in this way, there may be licensing issues.
547    
548           Setting  this  option  causes  the -lreadline option to be added to the
549           pcretest build. In many operating environments with  a  sytem-installed
550           libreadline this is sufficient. However, in some environments (e.g.  if
551           an unmodified distribution version of readline is in use),  some  extra
552           configuration  may  be necessary. The INSTALL file for libreadline says
553           this:
554    
555             "Readline uses the termcap functions, but does not link with the
556             termcap or curses library itself, allowing applications which link
557             with readline the to choose an appropriate library."
558    
559           If your environment has not been set up so that an appropriate  library
560           is automatically included, you may need to add something like
561    
562             LIBS="-ncurses"
563    
564           immediately before the configure command.
565    
566    
567  SEE ALSO  SEE ALSO
568    
569         pcreapi(3), pcre_config(3).         pcreapi(3), pcre_config(3).
# Line 513  AUTHOR Line 578  AUTHOR
578    
579  REVISION  REVISION
580    
581         Last updated: 30 July 2007         Last updated: 13 April 2008
582         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2008 University of Cambridge.
583  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
584    
585    
# Line 662  THE ALTERNATIVE MATCHING ALGORITHM Line 727  THE ALTERNATIVE MATCHING ALGORITHM
727         tive algorithm moves through the subject  string  one  character  at  a         tive algorithm moves through the subject  string  one  character  at  a
728         time, for all active paths through the tree.         time, for all active paths through the tree.
729    
730         8.  None  of  the  backtracking control verbs such as (*PRUNE) are sup-         8.  Except for (*FAIL), the backtracking control verbs such as (*PRUNE)
731         ported.         are not supported. (*FAIL) is supported, and  behaves  like  a  failing
732           negative assertion.
733    
734    
735  ADVANTAGES OF THE ALTERNATIVE ALGORITHM  ADVANTAGES OF THE ALTERNATIVE ALGORITHM
736    
737         Using the alternative matching algorithm provides the following  advan-         Using  the alternative matching algorithm provides the following advan-
738         tages:         tages:
739    
740         1. All possible matches (at a single point in the subject) are automat-         1. All possible matches (at a single point in the subject) are automat-
741         ically found, and in particular, the longest match is  found.  To  find         ically  found,  and  in particular, the longest match is found. To find
742         more than one match using the standard algorithm, you have to do kludgy         more than one match using the standard algorithm, you have to do kludgy
743         things with callouts.         things with callouts.
744    
745         2. There is much better support for partial matching. The  restrictions         2.  There is much better support for partial matching. The restrictions
746         on  the content of the pattern that apply when using the standard algo-         on the content of the pattern that apply when using the standard  algo-
747         rithm for partial matching do not apply to the  alternative  algorithm.         rithm  for  partial matching do not apply to the alternative algorithm.
748         For  non-anchored patterns, the starting position of a partial match is         For non-anchored patterns, the starting position of a partial match  is
749         available.         available.
750    
751         3. Because the alternative algorithm  scans  the  subject  string  just         3.  Because  the  alternative  algorithm  scans the subject string just
752         once,  and  never  needs to backtrack, it is possible to pass very long         once, and never needs to backtrack, it is possible to  pass  very  long
753         subject strings to the matching function in  several  pieces,  checking         subject  strings  to  the matching function in several pieces, checking
754         for partial matching each time.         for partial matching each time.
755    
756    
# Line 692  DISADVANTAGES OF THE ALTERNATIVE ALGORIT Line 758  DISADVANTAGES OF THE ALTERNATIVE ALGORIT
758    
759         The alternative algorithm suffers from a number of disadvantages:         The alternative algorithm suffers from a number of disadvantages:
760    
761         1.  It  is  substantially  slower  than the standard algorithm. This is         1. It is substantially slower than  the  standard  algorithm.  This  is
762         partly because it has to search for all possible matches, but  is  also         partly  because  it has to search for all possible matches, but is also
763         because it is less susceptible to optimization.         because it is less susceptible to optimization.
764    
765         2. Capturing parentheses and back references are not supported.         2. Capturing parentheses and back references are not supported.
# Line 711  AUTHOR Line 777  AUTHOR
777    
778  REVISION  REVISION
779    
780         Last updated: 08 August 2007         Last updated: 19 April 2008
781         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2008 University of Cambridge.
782  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
783    
784    
# Line 824  PCRE API OVERVIEW Line 890  PCRE API OVERVIEW
890         a  Perl-compatible  manner. A sample program that demonstrates the sim-         a  Perl-compatible  manner. A sample program that demonstrates the sim-
891         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
892         the  source distribution. The pcresample documentation describes how to         the  source distribution. The pcresample documentation describes how to
893         run it.         compile and run it.
894    
895         A second matching function, pcre_dfa_exec(), which is not Perl-compati-         A second matching function, pcre_dfa_exec(), which is not Perl-compati-
896         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 908  NEWLINES Line 974  NEWLINES
974         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
975         pattern is compiled, or when it is matched.         pattern is compiled, or when it is matched.
976    
977           At compile time, the newline convention can be specified by the options
978           argument of pcre_compile(), or it can be specified by special  text  at
979           the start of the pattern itself; this overrides any other settings. See
980           the pcrepattern page for details of the special character sequences.
981    
982         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-
983         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
984         newline  convention  affects  the  handling of the dot, circumflex, and         newline convention affects the handling of  the  dot,  circumflex,  and
985         dollar metacharacters, the handling of #-comments in /x mode, and, when         dollar metacharacters, the handling of #-comments in /x mode, and, when
986         CRLF  is a recognized line ending sequence, the match position advance-         CRLF is a recognized line ending sequence, the match position  advance-
987         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
988         not affect the interpretation of the \n or \r escape sequences.         section on pcre_exec() options below.
989    
990           The choice of newline convention does not affect the interpretation  of
991           the  \n  or  \r  escape  sequences, nor does it affect what \R matches,
992           which is controlled in a similar way, but by separate options.
993    
994    
995  MULTITHREADING  MULTITHREADING
996    
997         The  PCRE  functions  can be used in multi-threading applications, with         The PCRE functions can be used in  multi-threading  applications,  with
998         the  proviso  that  the  memory  management  functions  pointed  to  by         the  proviso  that  the  memory  management  functions  pointed  to  by
999         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
1000         callout function pointed to by pcre_callout, are shared by all threads.         callout function pointed to by pcre_callout, are shared by all threads.
1001    
1002         The  compiled form of a regular expression is not altered during match-         The compiled form of a regular expression is not altered during  match-
1003         ing, so the same compiled pattern can safely be used by several threads         ing, so the same compiled pattern can safely be used by several threads
1004         at once.         at once.
1005    
# Line 932  MULTITHREADING Line 1007  MULTITHREADING
1007  SAVING PRECOMPILED PATTERNS FOR LATER USE  SAVING PRECOMPILED PATTERNS FOR LATER USE
1008    
1009         The compiled form of a regular expression can be saved and re-used at a         The compiled form of a regular expression can be saved and re-used at a
1010         later time, possibly by a different program, and even on a  host  other         later  time,  possibly by a different program, and even on a host other
1011         than  the  one  on  which  it  was  compiled.  Details are given in the         than the one on which  it  was  compiled.  Details  are  given  in  the
1012         pcreprecompile documentation. However, compiling a  regular  expression         pcreprecompile  documentation.  However, compiling a regular expression
1013         with  one version of PCRE for use with a different version is not guar-         with one version of PCRE for use with a different version is not  guar-
1014         anteed to work and may cause crashes.         anteed to work and may cause crashes.
1015    
1016    
# Line 943  CHECKING BUILD-TIME OPTIONS Line 1018  CHECKING BUILD-TIME OPTIONS
1018    
1019         int pcre_config(int what, void *where);         int pcre_config(int what, void *where);
1020    
1021         The function pcre_config() makes it possible for a PCRE client to  dis-         The  function pcre_config() makes it possible for a PCRE client to dis-
1022         cover which optional features have been compiled into the PCRE library.         cover which optional features have been compiled into the PCRE library.
1023         The pcrebuild documentation has more details about these optional  fea-         The  pcrebuild documentation has more details about these optional fea-
1024         tures.         tures.
1025    
1026         The  first  argument  for pcre_config() is an integer, specifying which         The first argument for pcre_config() is an  integer,  specifying  which
1027         information is required; the second argument is a pointer to a variable         information is required; the second argument is a pointer to a variable
1028         into  which  the  information  is  placed. The following information is         into which the information is  placed.  The  following  information  is
1029         available:         available:
1030    
1031           PCRE_CONFIG_UTF8           PCRE_CONFIG_UTF8
1032    
1033         The output is an integer that is set to one if UTF-8 support is  avail-         The  output is an integer that is set to one if UTF-8 support is avail-
1034         able; otherwise it is set to zero.         able; otherwise it is set to zero.
1035    
1036           PCRE_CONFIG_UNICODE_PROPERTIES           PCRE_CONFIG_UNICODE_PROPERTIES
1037    
1038         The  output  is  an  integer  that is set to one if support for Unicode         The output is an integer that is set to  one  if  support  for  Unicode
1039         character properties is available; otherwise it is set to zero.         character properties is available; otherwise it is set to zero.
1040    
1041           PCRE_CONFIG_NEWLINE           PCRE_CONFIG_NEWLINE
1042    
1043         The output is an integer whose value specifies  the  default  character         The  output  is  an integer whose value specifies the default character
1044         sequence  that is recognized as meaning "newline". The four values that         sequence that is recognized as meaning "newline". The four values  that
1045         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,
1046         and  -1  for  ANY. The default should normally be the standard sequence         and -1 for ANY. The default should normally be  the  standard  sequence
1047         for your operating system.         for your operating system.
1048    
1049             PCRE_CONFIG_BSR
1050    
1051           The output is an integer whose value indicates what character sequences
1052           the \R escape sequence matches by default. A value of 0 means  that  \R
1053           matches  any  Unicode  line ending sequence; a value of 1 means that \R
1054           matches only CR, LF, or CRLF. The default can be overridden when a pat-
1055           tern is compiled or matched.
1056    
1057           PCRE_CONFIG_LINK_SIZE           PCRE_CONFIG_LINK_SIZE
1058    
1059         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
1060         internal linkage in compiled regular expressions. The value is 2, 3, or         internal linkage in compiled regular expressions. The value is 2, 3, or
1061         4. Larger values allow larger regular expressions to  be  compiled,  at         4.  Larger  values  allow larger regular expressions to be compiled, at
1062         the  expense  of  slower matching. The default value of 2 is sufficient         the expense of slower matching. The default value of  2  is  sufficient
1063         for all but the most massive patterns, since  it  allows  the  compiled         for  all  but  the  most massive patterns, since it allows the compiled
1064         pattern to be up to 64K in size.         pattern to be up to 64K in size.
1065    
1066           PCRE_CONFIG_POSIX_MALLOC_THRESHOLD           PCRE_CONFIG_POSIX_MALLOC_THRESHOLD
1067    
1068         The  output  is  an integer that contains the threshold above which the         The output is an integer that contains the threshold  above  which  the
1069         POSIX interface uses malloc() for output vectors. Further  details  are         POSIX  interface  uses malloc() for output vectors. Further details are
1070         given in the pcreposix documentation.         given in the pcreposix documentation.
1071    
1072           PCRE_CONFIG_MATCH_LIMIT           PCRE_CONFIG_MATCH_LIMIT
1073    
1074         The output is an integer that gives the default limit for the number of         The output is an integer that gives the default limit for the number of
1075         internal matching function calls in a  pcre_exec()  execution.  Further         internal  matching  function  calls in a pcre_exec() execution. Further
1076         details are given with pcre_exec() below.         details are given with pcre_exec() below.
1077    
1078           PCRE_CONFIG_MATCH_LIMIT_RECURSION           PCRE_CONFIG_MATCH_LIMIT_RECURSION
1079    
1080         The  output is an integer that gives the default limit for the depth of         The output is an integer that gives the default limit for the depth  of
1081         recursion when calling the internal matching function in a  pcre_exec()         recursion  when calling the internal matching function in a pcre_exec()
1082         execution. Further details are given with pcre_exec() below.         execution. Further details are given with pcre_exec() below.
1083    
1084           PCRE_CONFIG_STACKRECURSE           PCRE_CONFIG_STACKRECURSE
1085    
1086         The  output is an integer that is set to one if internal recursion when         The output is an integer that is set to one if internal recursion  when
1087         running pcre_exec() is implemented by recursive function calls that use         running pcre_exec() is implemented by recursive function calls that use
1088         the  stack  to remember their state. This is the usual way that PCRE is         the stack to remember their state. This is the usual way that  PCRE  is
1089         compiled. The output is zero if PCRE was compiled to use blocks of data         compiled. The output is zero if PCRE was compiled to use blocks of data
1090         on  the  heap  instead  of  recursive  function  calls.  In  this case,         on the  heap  instead  of  recursive  function  calls.  In  this  case,
1091         pcre_stack_malloc and  pcre_stack_free  are  called  to  manage  memory         pcre_stack_malloc  and  pcre_stack_free  are  called  to  manage memory
1092         blocks on the heap, thus avoiding the use of the stack.         blocks on the heap, thus avoiding the use of the stack.
1093    
1094    
# Line 1022  COMPILING A PATTERN Line 1105  COMPILING A PATTERN
1105    
1106         Either of the functions pcre_compile() or pcre_compile2() can be called         Either of the functions pcre_compile() or pcre_compile2() can be called
1107         to compile a pattern into an internal form. The only difference between         to compile a pattern into an internal form. The only difference between
1108         the  two interfaces is that pcre_compile2() has an additional argument,         the two interfaces is that pcre_compile2() has an additional  argument,
1109         errorcodeptr, via which a numerical error code can be returned.         errorcodeptr, via which a numerical error code can be returned.
1110    
1111         The pattern is a C string terminated by a binary zero, and is passed in         The pattern is a C string terminated by a binary zero, and is passed in
1112         the  pattern  argument.  A  pointer to a single block of memory that is         the pattern argument. A pointer to a single block  of  memory  that  is
1113         obtained via pcre_malloc is returned. This contains the  compiled  code         obtained  via  pcre_malloc is returned. This contains the compiled code
1114         and related data. The pcre type is defined for the returned block; this         and related data. The pcre type is defined for the returned block; this
1115         is a typedef for a structure whose contents are not externally defined.         is a typedef for a structure whose contents are not externally defined.
1116         It is up to the caller to free the memory (via pcre_free) when it is no         It is up to the caller to free the memory (via pcre_free) when it is no
1117         longer required.         longer required.
1118    
1119         Although the compiled code of a PCRE regex is relocatable, that is,  it         Although  the compiled code of a PCRE regex is relocatable, that is, it
1120         does not depend on memory location, the complete pcre data block is not         does not depend on memory location, the complete pcre data block is not
1121         fully relocatable, because it may contain a copy of the tableptr  argu-         fully  relocatable, because it may contain a copy of the tableptr argu-
1122         ment, which is an address (see below).         ment, which is an address (see below).
1123    
1124         The options argument contains various bit settings that affect the com-         The options argument contains various bit settings that affect the com-
1125         pilation. It should be zero if no options are required.  The  available         pilation.  It  should be zero if no options are required. The available
1126         options  are  described  below. Some of them, in particular, those that         options are described below. Some of them, in  particular,  those  that
1127         are compatible with Perl, can also be set and  unset  from  within  the         are  compatible  with  Perl,  can also be set and unset from within the
1128         pattern  (see  the  detailed  description in the pcrepattern documenta-         pattern (see the detailed description  in  the  pcrepattern  documenta-
1129         tion). For these options, the contents of the options  argument  speci-         tion).  For  these options, the contents of the options argument speci-
1130         fies  their initial settings at the start of compilation and execution.         fies their initial settings at the start of compilation and  execution.
1131         The PCRE_ANCHORED and PCRE_NEWLINE_xxx options can be set at  the  time         The  PCRE_ANCHORED  and PCRE_NEWLINE_xxx options can be set at the time
1132         of matching as well as at compile time.         of matching as well as at compile time.
1133    
1134         If errptr is NULL, pcre_compile() returns NULL immediately.  Otherwise,         If errptr is NULL, pcre_compile() returns NULL immediately.  Otherwise,
1135         if compilation of a pattern fails,  pcre_compile()  returns  NULL,  and         if  compilation  of  a  pattern fails, pcre_compile() returns NULL, and
1136         sets the variable pointed to by errptr to point to a textual error mes-         sets the variable pointed to by errptr to point to a textual error mes-
1137         sage. This is a static string that is part of the library. You must not         sage. This is a static string that is part of the library. You must not
1138         try to free it. The offset from the start of the pattern to the charac-         try to free it. The offset from the start of the pattern to the charac-
1139         ter where the error was discovered is placed in the variable pointed to         ter where the error was discovered is placed in the variable pointed to
1140         by  erroffset,  which must not be NULL. If it is, an immediate error is         by erroffset, which must not be NULL. If it is, an immediate  error  is
1141         given.         given.
1142    
1143         If pcre_compile2() is used instead of pcre_compile(),  and  the  error-         If  pcre_compile2()  is  used instead of pcre_compile(), and the error-
1144         codeptr  argument is not NULL, a non-zero error code number is returned         codeptr argument is not NULL, a non-zero error code number is  returned
1145         via this argument in the event of an error. This is in addition to  the         via  this argument in the event of an error. This is in addition to the
1146         textual error message. Error codes and messages are listed below.         textual error message. Error codes and messages are listed below.
1147    
1148         If  the  final  argument, tableptr, is NULL, PCRE uses a default set of         If the final argument, tableptr, is NULL, PCRE uses a  default  set  of
1149         character tables that are  built  when  PCRE  is  compiled,  using  the         character  tables  that  are  built  when  PCRE  is compiled, using the
1150         default  C  locale.  Otherwise, tableptr must be an address that is the         default C locale. Otherwise, tableptr must be an address  that  is  the
1151         result of a call to pcre_maketables(). This value is  stored  with  the         result  of  a  call to pcre_maketables(). This value is stored with the
1152         compiled  pattern,  and used again by pcre_exec(), unless another table         compiled pattern, and used again by pcre_exec(), unless  another  table
1153         pointer is passed to it. For more discussion, see the section on locale         pointer is passed to it. For more discussion, see the section on locale
1154         support below.         support below.
1155    
1156         This  code  fragment  shows a typical straightforward call to pcre_com-         This code fragment shows a typical straightforward  call  to  pcre_com-
1157         pile():         pile():
1158    
1159           pcre *re;           pcre *re;
# Line 1083  COMPILING A PATTERN Line 1166  COMPILING A PATTERN
1166             &erroffset,       /* for error offset */             &erroffset,       /* for error offset */
1167             NULL);            /* use default character tables */             NULL);            /* use default character tables */
1168    
1169         The following names for option bits are defined in  the  pcre.h  header         The  following  names  for option bits are defined in the pcre.h header
1170         file:         file:
1171    
1172           PCRE_ANCHORED           PCRE_ANCHORED
1173    
1174         If this bit is set, the pattern is forced to be "anchored", that is, it         If this bit is set, the pattern is forced to be "anchored", that is, it
1175         is constrained to match only at the first matching point in the  string         is  constrained to match only at the first matching point in the string
1176         that  is being searched (the "subject string"). This effect can also be         that is being searched (the "subject string"). This effect can also  be
1177         achieved by appropriate constructs in the pattern itself, which is  the         achieved  by appropriate constructs in the pattern itself, which is the
1178         only way to do it in Perl.         only way to do it in Perl.
1179    
1180           PCRE_AUTO_CALLOUT           PCRE_AUTO_CALLOUT
1181    
1182         If this bit is set, pcre_compile() automatically inserts callout items,         If this bit is set, pcre_compile() automatically inserts callout items,
1183         all with number 255, before each pattern item. For  discussion  of  the         all  with  number  255, before each pattern item. For discussion of the
1184         callout facility, see the pcrecallout documentation.         callout facility, see the pcrecallout documentation.
1185    
1186             PCRE_BSR_ANYCRLF
1187             PCRE_BSR_UNICODE
1188    
1189           These options (which are mutually exclusive) control what the \R escape
1190           sequence  matches.  The choice is either to match only CR, LF, or CRLF,
1191           or to match any Unicode newline sequence. The default is specified when
1192           PCRE is built. It can be overridden from within the pattern, or by set-
1193           ting an option when a compiled pattern is matched.
1194    
1195           PCRE_CASELESS           PCRE_CASELESS
1196    
1197         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
1198         case letters. It is equivalent to Perl's  /i  option,  and  it  can  be         case  letters.  It  is  equivalent  to  Perl's /i option, and it can be
1199         changed  within a pattern by a (?i) option setting. In UTF-8 mode, PCRE         changed within a pattern by a (?i) option setting. In UTF-8 mode,  PCRE
1200         always understands the concept of case for characters whose values  are         always  understands the concept of case for characters whose values are
1201         less  than 128, so caseless matching is always possible. For characters         less than 128, so caseless matching is always possible. For  characters
1202         with higher values, the concept of case is supported if  PCRE  is  com-         with  higher  values,  the concept of case is supported if PCRE is com-
1203         piled  with Unicode property support, but not otherwise. If you want to         piled with Unicode property support, but not otherwise. If you want  to
1204         use caseless matching for characters 128 and  above,  you  must  ensure         use  caseless  matching  for  characters 128 and above, you must ensure
1205         that  PCRE  is  compiled  with Unicode property support as well as with         that PCRE is compiled with Unicode property support  as  well  as  with
1206         UTF-8 support.         UTF-8 support.
1207    
1208           PCRE_DOLLAR_ENDONLY           PCRE_DOLLAR_ENDONLY
1209    
1210         If this bit is set, a dollar metacharacter in the pattern matches  only         If  this bit is set, a dollar metacharacter in the pattern matches only
1211         at  the  end  of the subject string. Without this option, a dollar also         at the end of the subject string. Without this option,  a  dollar  also
1212         matches immediately before a newline at the end of the string (but  not         matches  immediately before a newline at the end of the string (but not
1213         before  any  other newlines). The PCRE_DOLLAR_ENDONLY option is ignored         before any other newlines). The PCRE_DOLLAR_ENDONLY option  is  ignored
1214         if PCRE_MULTILINE is set.  There is no equivalent  to  this  option  in         if  PCRE_MULTILINE  is  set.   There is no equivalent to this option in
1215         Perl, and no way to set it within a pattern.         Perl, and no way to set it within a pattern.
1216    
1217           PCRE_DOTALL           PCRE_DOTALL
1218    
1219         If this bit is set, a dot metacharater in the pattern matches all char-         If this bit is set, a dot metacharater in the pattern matches all char-
1220         acters, including those that indicate newline. Without it, a  dot  does         acters,  including  those that indicate newline. Without it, a dot does
1221         not  match  when  the  current position is at a newline. This option is         not match when the current position is at a  newline.  This  option  is
1222         equivalent to Perl's /s option, and it can be changed within a  pattern         equivalent  to Perl's /s option, and it can be changed within a pattern
1223         by  a (?s) option setting. A negative class such as [^a] always matches         by a (?s) option setting. A negative class such as [^a] always  matches
1224         newline characters, independent of the setting of this option.         newline characters, independent of the setting of this option.
1225    
1226           PCRE_DUPNAMES           PCRE_DUPNAMES
1227    
1228         If this bit is set, names used to identify capturing  subpatterns  need         If  this  bit is set, names used to identify capturing subpatterns need
1229         not be unique. This can be helpful for certain types of pattern when it         not be unique. This can be helpful for certain types of pattern when it
1230         is known that only one instance of the named  subpattern  can  ever  be         is  known  that  only  one instance of the named subpattern can ever be
1231         matched.  There  are  more details of named subpatterns below; see also         matched. There are more details of named subpatterns  below;  see  also
1232         the pcrepattern documentation.         the pcrepattern documentation.
1233    
1234           PCRE_EXTENDED           PCRE_EXTENDED
1235    
1236         If this bit is set, whitespace  data  characters  in  the  pattern  are         If  this  bit  is  set,  whitespace  data characters in the pattern are
1237         totally ignored except when escaped or inside a character class. White-         totally ignored except when escaped or inside a character class. White-
1238         space does not include the VT character (code 11). In addition, charac-         space does not include the VT character (code 11). In addition, charac-
1239         ters between an unescaped # outside a character class and the next new-         ters between an unescaped # outside a character class and the next new-
1240         line, inclusive, are also ignored. This  is  equivalent  to  Perl's  /x         line,  inclusive,  are  also  ignored.  This is equivalent to Perl's /x
1241         option,  and  it  can be changed within a pattern by a (?x) option set-         option, and it can be changed within a pattern by a  (?x)  option  set-
1242         ting.         ting.
1243    
1244         This option makes it possible to include  comments  inside  complicated         This  option  makes  it possible to include comments inside complicated
1245         patterns.   Note,  however,  that this applies only to data characters.         patterns.  Note, however, that this applies only  to  data  characters.
1246         Whitespace  characters  may  never  appear  within  special   character         Whitespace   characters  may  never  appear  within  special  character
1247         sequences  in  a  pattern,  for  example  within the sequence (?( which         sequences in a pattern, for  example  within  the  sequence  (?(  which
1248         introduces a conditional subpattern.         introduces a conditional subpattern.
1249    
1250           PCRE_EXTRA           PCRE_EXTRA
1251    
1252         This option was invented in order to turn on  additional  functionality         This  option  was invented in order to turn on additional functionality
1253         of  PCRE  that  is  incompatible with Perl, but it is currently of very         of PCRE that is incompatible with Perl, but it  is  currently  of  very
1254         little use. When set, any backslash in a pattern that is followed by  a         little  use. When set, any backslash in a pattern that is followed by a
1255         letter  that  has  no  special  meaning causes an error, thus reserving         letter that has no special meaning  causes  an  error,  thus  reserving
1256         these combinations for future expansion. By  default,  as  in  Perl,  a         these  combinations  for  future  expansion.  By default, as in Perl, a
1257         backslash  followed by a letter with no special meaning is treated as a         backslash followed by a letter with no special meaning is treated as  a
1258         literal. (Perl can, however, be persuaded to give a warning for  this.)         literal.  (Perl can, however, be persuaded to give a warning for this.)
1259         There  are  at  present no other features controlled by this option. It         There are at present no other features controlled by  this  option.  It
1260         can also be set by a (?X) option setting within a pattern.         can also be set by a (?X) option setting within a pattern.
1261    
1262           PCRE_FIRSTLINE           PCRE_FIRSTLINE
1263    
1264         If this option is set, an  unanchored  pattern  is  required  to  match         If  this  option  is  set,  an  unanchored pattern is required to match
1265         before  or  at  the  first  newline  in  the subject string, though the         before or at the first  newline  in  the  subject  string,  though  the
1266         matched text may continue over the newline.         matched text may continue over the newline.
1267    
1268             PCRE_JAVASCRIPT_COMPAT
1269    
1270           If this option is set, PCRE's behaviour is changed in some ways so that
1271           it is compatible with JavaScript rather than Perl. The changes  are  as
1272           follows:
1273    
1274           (1)  A  lone  closing square bracket in a pattern causes a compile-time
1275           error, because this is illegal in JavaScript (by default it is  treated
1276           as a data character). Thus, the pattern AB]CD becomes illegal when this
1277           option is set.
1278    
1279           (2) At run time, a back reference to an unset subpattern group  matches
1280           an  empty  string (by default this causes the current matching alterna-
1281           tive to fail). A pattern such as (\1)(a) succeeds when this  option  is
1282           set  (assuming  it can find an "a" in the subject), whereas it fails by
1283           default, for Perl compatibility.
1284    
1285           PCRE_MULTILINE           PCRE_MULTILINE
1286    
1287         By default, PCRE treats the subject string as consisting  of  a  single         By default, PCRE treats the subject string as consisting  of  a  single
# Line 1285  COMPILATION ERROR CODES Line 1394  COMPILATION ERROR CODES
1394            9  nothing to repeat            9  nothing to repeat
1395           10  [this code is not in use]           10  [this code is not in use]
1396           11  internal error: unexpected repeat           11  internal error: unexpected repeat
1397           12  unrecognized character after (?           12  unrecognized character after (? or (?-
1398           13  POSIX named classes are supported only within a class           13  POSIX named classes are supported only within a class
1399           14  missing )           14  missing )
1400           15  reference to non-existent subpattern           15  reference to non-existent subpattern
# Line 1293  COMPILATION ERROR CODES Line 1402  COMPILATION ERROR CODES
1402           17  unknown option bit(s) set           17  unknown option bit(s) set
1403           18  missing ) after comment           18  missing ) after comment
1404           19  [this code is not in use]           19  [this code is not in use]
1405           20  regular expression too large           20  regular expression is too large
1406           21  failed to get memory           21  failed to get memory
1407           22  unmatched parentheses           22  unmatched parentheses
1408           23  internal error: code overflow           23  internal error: code overflow
# Line 1322  COMPILATION ERROR CODES Line 1431  COMPILATION ERROR CODES
1431           46  malformed \P or \p sequence           46  malformed \P or \p sequence
1432           47  unknown property name after \P or \p           47  unknown property name after \P or \p
1433           48  subpattern name is too long (maximum 32 characters)           48  subpattern name is too long (maximum 32 characters)
1434           49  too many named subpatterns (maximum 10,000)           49  too many named subpatterns (maximum 10000)
1435           50  [this code is not in use]           50  [this code is not in use]
1436           51  octal value is greater than \377 (not in UTF-8 mode)           51  octal value is greater than \377 (not in UTF-8 mode)
1437           52  internal error: overran compiling workspace           52  internal error: overran compiling workspace
# Line 1330  COMPILATION ERROR CODES Line 1439  COMPILATION ERROR CODES
1439         found         found
1440           54  DEFINE group contains more than one branch           54  DEFINE group contains more than one branch
1441           55  repeating a DEFINE group is not allowed           55  repeating a DEFINE group is not allowed
1442           56  inconsistent NEWLINE options"           56  inconsistent NEWLINE options
1443           57  \g is not followed by a braced name or an optionally braced           57  \g is not followed by a braced, angle-bracketed, or quoted
1444                 non-zero number                 name/number or by a plain number
1445           58  (?+ or (?- or (?(+ or (?(- must be followed by a non-zero number           58  a numbered reference must not be zero
1446             59  (*VERB) with an argument is not supported
1447             60  (*VERB) not recognized
1448             61  number is too big
1449             62  subpattern name expected
1450             63  digit expected after (?+
1451             64  ] is an invalid data character in JavaScript compatibility mode
1452    
1453           The  numbers  32  and 10000 in errors 48 and 49 are defaults; different
1454           values may be used if the limits were changed when PCRE was built.
1455    
1456    
1457  STUDYING A PATTERN  STUDYING A PATTERN
# Line 1341  STUDYING A PATTERN Line 1459  STUDYING A PATTERN
1459         pcre_extra *pcre_study(const pcre *code, int options         pcre_extra *pcre_study(const pcre *code, int options
1460              const char **errptr);              const char **errptr);
1461    
1462         If  a  compiled  pattern is going to be used several times, it is worth         If a compiled pattern is going to be used several times,  it  is  worth
1463         spending more time analyzing it in order to speed up the time taken for         spending more time analyzing it in order to speed up the time taken for
1464         matching.  The function pcre_study() takes a pointer to a compiled pat-         matching. The function pcre_study() takes a pointer to a compiled  pat-
1465         tern as its first argument. If studying the pattern produces additional         tern as its first argument. If studying the pattern produces additional
1466         information  that  will  help speed up matching, pcre_study() returns a         information that will help speed up matching,  pcre_study()  returns  a
1467         pointer to a pcre_extra block, in which the study_data field points  to         pointer  to a pcre_extra block, in which the study_data field points to
1468         the results of the study.         the results of the study.
1469    
1470         The  returned  value  from  pcre_study()  can  be  passed  directly  to         The  returned  value  from  pcre_study()  can  be  passed  directly  to
1471         pcre_exec(). However, a pcre_extra block  also  contains  other  fields         pcre_exec().  However,  a  pcre_extra  block also contains other fields
1472         that  can  be  set  by the caller before the block is passed; these are         that can be set by the caller before the block  is  passed;  these  are
1473         described below in the section on matching a pattern.         described below in the section on matching a pattern.
1474    
1475         If studying the pattern does not  produce  any  additional  information         If  studying  the  pattern  does not produce any additional information
1476         pcre_study() returns NULL. In that circumstance, if the calling program         pcre_study() returns NULL. In that circumstance, if the calling program
1477         wants to pass any of the other fields to pcre_exec(), it  must  set  up         wants  to  pass  any of the other fields to pcre_exec(), it must set up
1478         its own pcre_extra block.         its own pcre_extra block.
1479    
1480         The  second  argument of pcre_study() contains option bits. At present,         The second argument of pcre_study() contains option bits.  At  present,
1481         no options are defined, and this argument should always be zero.         no options are defined, and this argument should always be zero.
1482    
1483         The third argument for pcre_study() is a pointer for an error  message.         The  third argument for pcre_study() is a pointer for an error message.
1484         If  studying  succeeds  (even  if no data is returned), the variable it         If studying succeeds (even if no data is  returned),  the  variable  it
1485         points to is set to NULL. Otherwise it is set to  point  to  a  textual         points  to  is  set  to NULL. Otherwise it is set to point to a textual
1486         error message. This is a static string that is part of the library. You         error message. This is a static string that is part of the library. You
1487         must not try to free it. You should test the  error  pointer  for  NULL         must  not  try  to  free it. You should test the error pointer for NULL
1488         after calling pcre_study(), to be sure that it has run successfully.         after calling pcre_study(), to be sure that it has run successfully.
1489    
1490         This is a typical call to pcre_study():         This is a typical call to pcre_study():
# Line 1378  STUDYING A PATTERN Line 1496  STUDYING A PATTERN
1496             &error);        /* set to NULL or points to a message */             &error);        /* set to NULL or points to a message */
1497    
1498         At present, studying a pattern is useful only for non-anchored patterns         At present, studying a pattern is useful only for non-anchored patterns
1499         that do not have a single fixed starting character. A bitmap of  possi-         that  do not have a single fixed starting character. A bitmap of possi-
1500         ble starting bytes is created.         ble starting bytes is created.
1501    
1502    
1503  LOCALE SUPPORT  LOCALE SUPPORT
1504    
1505         PCRE  handles  caseless matching, and determines whether characters are         PCRE handles caseless matching, and determines whether  characters  are
1506         letters, digits, or whatever, by reference to a set of tables,  indexed         letters,  digits, or whatever, by reference to a set of tables, indexed
1507         by  character  value.  When running in UTF-8 mode, this applies only to         by character value. When running in UTF-8 mode, this  applies  only  to
1508         characters with codes less than 128. Higher-valued  codes  never  match         characters  with  codes  less than 128. Higher-valued codes never match
1509         escapes  such  as  \w or \d, but can be tested with \p if PCRE is built         escapes such as \w or \d, but can be tested with \p if  PCRE  is  built
1510         with Unicode character property support. The use of locales  with  Uni-         with  Unicode  character property support. The use of locales with Uni-
1511         code  is discouraged. If you are handling characters with codes greater         code is discouraged. If you are handling characters with codes  greater
1512         than 128, you should either use UTF-8 and Unicode, or use locales,  but         than  128, you should either use UTF-8 and Unicode, or use locales, but
1513         not try to mix the two.         not try to mix the two.
1514    
1515         PCRE  contains  an  internal set of tables that are used when the final         PCRE contains an internal set of tables that are used  when  the  final
1516         argument of pcre_compile() is  NULL.  These  are  sufficient  for  many         argument  of  pcre_compile()  is  NULL.  These  are sufficient for many
1517         applications.  Normally, the internal tables recognize only ASCII char-         applications.  Normally, the internal tables recognize only ASCII char-
1518         acters. However, when PCRE is built, it is possible to cause the inter-         acters. However, when PCRE is built, it is possible to cause the inter-
1519         nal tables to be rebuilt in the default "C" locale of the local system,         nal tables to be rebuilt in the default "C" locale of the local system,
1520         which may cause them to be different.         which may cause them to be different.
1521    
1522         The internal tables can always be overridden by tables supplied by  the         The  internal tables can always be overridden by tables supplied by the
1523         application that calls PCRE. These may be created in a different locale         application that calls PCRE. These may be created in a different locale
1524         from the default. As more and more applications change  to  using  Uni-         from  the  default.  As more and more applications change to using Uni-
1525         code, the need for this locale support is expected to die away.         code, the need for this locale support is expected to die away.
1526    
1527         External  tables  are  built by calling the pcre_maketables() function,         External tables are built by calling  the  pcre_maketables()  function,
1528         which has no arguments, in the relevant locale. The result can then  be         which  has no arguments, in the relevant locale. The result can then be
1529         passed  to  pcre_compile()  or  pcre_exec()  as often as necessary. For         passed to pcre_compile() or pcre_exec()  as  often  as  necessary.  For
1530         example, to build and use tables that are appropriate  for  the  French         example,  to  build  and use tables that are appropriate for the French
1531         locale  (where  accented  characters  with  values greater than 128 are         locale (where accented characters with  values  greater  than  128  are
1532         treated as letters), the following code could be used:         treated as letters), the following code could be used:
1533    
1534           setlocale(LC_CTYPE, "fr_FR");           setlocale(LC_CTYPE, "fr_FR");
1535           tables = pcre_maketables();           tables = pcre_maketables();
1536           re = pcre_compile(..., tables);           re = pcre_compile(..., tables);
1537    
1538         The locale name "fr_FR" is used on Linux and other  Unix-like  systems;         The  locale  name "fr_FR" is used on Linux and other Unix-like systems;
1539         if you are using Windows, the name for the French locale is "french".         if you are using Windows, the name for the French locale is "french".
1540    
1541         When  pcre_maketables()  runs,  the  tables are built in memory that is         When pcre_maketables() runs, the tables are built  in  memory  that  is
1542         obtained via pcre_malloc. It is the caller's responsibility  to  ensure         obtained  via  pcre_malloc. It is the caller's responsibility to ensure
1543         that  the memory containing the tables remains available for as long as         that the memory containing the tables remains available for as long  as
1544         it is needed.         it is needed.
1545    
1546         The pointer that is passed to pcre_compile() is saved with the compiled         The pointer that is passed to pcre_compile() is saved with the compiled
1547         pattern,  and the same tables are used via this pointer by pcre_study()         pattern, and the same tables are used via this pointer by  pcre_study()
1548         and normally also by pcre_exec(). Thus, by default, for any single pat-         and normally also by pcre_exec(). Thus, by default, for any single pat-
1549         tern, compilation, studying and matching all happen in the same locale,         tern, compilation, studying and matching all happen in the same locale,
1550         but different patterns can be compiled in different locales.         but different patterns can be compiled in different locales.
1551    
1552         It is possible to pass a table pointer or NULL (indicating the  use  of         It  is  possible to pass a table pointer or NULL (indicating the use of
1553         the  internal  tables)  to  pcre_exec(). Although not intended for this         the internal tables) to pcre_exec(). Although  not  intended  for  this
1554         purpose, this facility could be used to match a pattern in a  different         purpose,  this facility could be used to match a pattern in a different
1555         locale from the one in which it was compiled. Passing table pointers at         locale from the one in which it was compiled. Passing table pointers at
1556         run time is discussed below in the section on matching a pattern.         run time is discussed below in the section on matching a pattern.
1557    
# Line 1443  INFORMATION ABOUT A PATTERN Line 1561  INFORMATION ABOUT A PATTERN
1561         int pcre_fullinfo(const pcre *code, const pcre_extra *extra,         int pcre_fullinfo(const pcre *code, const pcre_extra *extra,
1562              int what, void *where);              int what, void *where);
1563    
1564         The pcre_fullinfo() function returns information about a compiled  pat-         The  pcre_fullinfo() function returns information about a compiled pat-
1565         tern. It replaces the obsolete pcre_info() function, which is neverthe-         tern. It replaces the obsolete pcre_info() function, which is neverthe-
1566         less retained for backwards compability (and is documented below).         less retained for backwards compability (and is documented below).
1567    
1568         The first argument for pcre_fullinfo() is a  pointer  to  the  compiled         The  first  argument  for  pcre_fullinfo() is a pointer to the compiled
1569         pattern.  The second argument is the result of pcre_study(), or NULL if         pattern. The second argument is the result of pcre_study(), or NULL  if
1570         the pattern was not studied. The third argument specifies  which  piece         the  pattern  was not studied. The third argument specifies which piece
1571         of  information  is required, and the fourth argument is a pointer to a         of information is required, and the fourth argument is a pointer  to  a
1572         variable to receive the data. The yield of the  function  is  zero  for         variable  to  receive  the  data. The yield of the function is zero for
1573         success, or one of the following negative numbers:         success, or one of the following negative numbers:
1574    
1575           PCRE_ERROR_NULL       the argument code was NULL           PCRE_ERROR_NULL       the argument code was NULL
# Line 1459  INFORMATION ABOUT A PATTERN Line 1577  INFORMATION ABOUT A PATTERN
1577           PCRE_ERROR_BADMAGIC   the "magic number" was not found           PCRE_ERROR_BADMAGIC   the "magic number" was not found
1578           PCRE_ERROR_BADOPTION  the value of what was invalid           PCRE_ERROR_BADOPTION  the value of what was invalid
1579    
1580         The  "magic  number" is placed at the start of each compiled pattern as         The "magic number" is placed at the start of each compiled  pattern  as
1581         an simple check against passing an arbitrary memory pointer. Here is  a         an  simple check against passing an arbitrary memory pointer. Here is a
1582         typical  call  of pcre_fullinfo(), to obtain the length of the compiled         typical call of pcre_fullinfo(), to obtain the length of  the  compiled
1583         pattern:         pattern:
1584    
1585           int rc;           int rc;
# Line 1472  INFORMATION ABOUT A PATTERN Line 1590  INFORMATION ABOUT A PATTERN
1590             PCRE_INFO_SIZE,   /* what is required */             PCRE_INFO_SIZE,   /* what is required */
1591             &length);         /* where to put the data */             &length);         /* where to put the data */
1592    
1593         The possible values for the third argument are defined in  pcre.h,  and         The  possible  values for the third argument are defined in pcre.h, and
1594         are as follows:         are as follows:
1595    
1596           PCRE_INFO_BACKREFMAX           PCRE_INFO_BACKREFMAX
1597    
1598         Return  the  number  of  the highest back reference in the pattern. The         Return the number of the highest back reference  in  the  pattern.  The
1599         fourth argument should point to an int variable. Zero  is  returned  if         fourth  argument  should  point to an int variable. Zero is returned if
1600         there are no back references.         there are no back references.
1601    
1602           PCRE_INFO_CAPTURECOUNT           PCRE_INFO_CAPTURECOUNT
1603    
1604         Return  the  number of capturing subpatterns in the pattern. The fourth         Return the number of capturing subpatterns in the pattern.  The  fourth
1605         argument should point to an int variable.         argument should point to an int variable.
1606    
1607           PCRE_INFO_DEFAULT_TABLES           PCRE_INFO_DEFAULT_TABLES
1608    
1609         Return a pointer to the internal default character tables within  PCRE.         Return  a pointer to the internal default character tables within PCRE.
1610         The  fourth  argument should point to an unsigned char * variable. This         The fourth argument should point to an unsigned char *  variable.  This
1611         information call is provided for internal use by the pcre_study() func-         information call is provided for internal use by the pcre_study() func-
1612         tion.  External  callers  can  cause PCRE to use its internal tables by         tion. External callers can cause PCRE to use  its  internal  tables  by
1613         passing a NULL table pointer.         passing a NULL table pointer.
1614    
1615           PCRE_INFO_FIRSTBYTE           PCRE_INFO_FIRSTBYTE
1616    
1617         Return information about the first byte of any matched  string,  for  a         Return  information  about  the first byte of any matched string, for a
1618         non-anchored  pattern. The fourth argument should point to an int vari-         non-anchored pattern. The fourth argument should point to an int  vari-
1619         able. (This option used to be called PCRE_INFO_FIRSTCHAR; the old  name         able.  (This option used to be called PCRE_INFO_FIRSTCHAR; the old name
1620         is still recognized for backwards compatibility.)         is still recognized for backwards compatibility.)
1621    
1622         If  there  is  a  fixed first byte, for example, from a pattern such as         If there is a fixed first byte, for example, from  a  pattern  such  as
1623         (cat|cow|coyote), its value is returned. Otherwise, if either         (cat|cow|coyote), its value is returned. Otherwise, if either
1624    
1625         (a) the pattern was compiled with the PCRE_MULTILINE option, and  every         (a)  the pattern was compiled with the PCRE_MULTILINE option, and every
1626         branch starts with "^", or         branch starts with "^", or
1627    
1628         (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not         (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not
1629         set (if it were set, the pattern would be anchored),         set (if it were set, the pattern would be anchored),
1630    
1631         -1 is returned, indicating that the pattern matches only at  the  start         -1  is  returned, indicating that the pattern matches only at the start
1632         of  a  subject string or after any newline within the string. Otherwise         of a subject string or after any newline within the  string.  Otherwise
1633         -2 is returned. For anchored patterns, -2 is returned.         -2 is returned. For anchored patterns, -2 is returned.
1634    
1635           PCRE_INFO_FIRSTTABLE           PCRE_INFO_FIRSTTABLE
1636    
1637         If the pattern was studied, and this resulted in the construction of  a         If  the pattern was studied, and this resulted in the construction of a
1638         256-bit table indicating a fixed set of bytes for the first byte in any         256-bit table indicating a fixed set of bytes for the first byte in any
1639         matching string, a pointer to the table is returned. Otherwise NULL  is         matching  string, a pointer to the table is returned. Otherwise NULL is
1640         returned.  The fourth argument should point to an unsigned char * vari-         returned. The fourth argument should point to an unsigned char *  vari-
1641         able.         able.
1642    
1643             PCRE_INFO_HASCRORLF
1644    
1645           Return  1  if  the  pattern  contains any explicit matches for CR or LF
1646           characters, otherwise 0. The fourth argument should  point  to  an  int
1647           variable.  An explicit match is either a literal CR or LF character, or
1648           \r or \n.
1649    
1650           PCRE_INFO_JCHANGED           PCRE_INFO_JCHANGED
1651    
1652         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,
1653         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)
1654         nal option setting changes the local PCRE_DUPNAMES option.         and (?-J) set and unset the local PCRE_DUPNAMES option, respectively.
1655    
1656           PCRE_INFO_LASTLITERAL           PCRE_INFO_LASTLITERAL
1657    
# Line 1801  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1926  MATCHING A PATTERN: THE TRADITIONAL FUNC
1926         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
1927         unachored at matching time.         unachored at matching time.
1928    
1929             PCRE_BSR_ANYCRLF
1930             PCRE_BSR_UNICODE
1931    
1932           These options (which are mutually exclusive) control what the \R escape
1933           sequence  matches.  The choice is either to match only CR, LF, or CRLF,
1934           or to match any Unicode newline sequence. These  options  override  the
1935           choice that was made or defaulted when the pattern was compiled.
1936    
1937           PCRE_NEWLINE_CR           PCRE_NEWLINE_CR
1938           PCRE_NEWLINE_LF           PCRE_NEWLINE_LF
1939           PCRE_NEWLINE_CRLF           PCRE_NEWLINE_CRLF
1940           PCRE_NEWLINE_ANYCRLF           PCRE_NEWLINE_ANYCRLF
1941           PCRE_NEWLINE_ANY           PCRE_NEWLINE_ANY
1942    
1943         These options override  the  newline  definition  that  was  chosen  or         These  options  override  the  newline  definition  that  was chosen or
1944         defaulted  when the pattern was compiled. For details, see the descrip-         defaulted when the pattern was compiled. For details, see the  descrip-
1945         tion of pcre_compile()  above.  During  matching,  the  newline  choice         tion  of  pcre_compile()  above.  During  matching,  the newline choice
1946         affects  the  behaviour  of the dot, circumflex, and dollar metacharac-         affects the behaviour of the dot, circumflex,  and  dollar  metacharac-
1947         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
1948         match  failure  for  an  unanchored  pattern.  When  PCRE_NEWLINE_CRLF,         match failure for an unanchored pattern.
1949         PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is set, and a  match  attempt  
1950         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
1951         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-
1952         after the CRLF.         rent position is at a  CRLF  sequence,  and  the  pattern  contains  no
1953           explicit  matches  for  CR  or  LF  characters,  the  match position is
1954           advanced by two characters instead of one, in other words, to after the
1955           CRLF.
1956    
1957           The above rule is a compromise that makes the most common cases work as
1958           expected. For example, if the  pattern  is  .+A  (and  the  PCRE_DOTALL
1959           option is not set), it does not match the string "\r\nA" because, after
1960           failing at the start, it skips both the CR and the LF before  retrying.
1961           However,  the  pattern  [\r\n]A does match that string, because it con-
1962           tains an explicit CR or LF reference, and so advances only by one char-
1963           acter after the first failure.
1964    
1965           An explicit match for CR of LF is either a literal appearance of one of
1966           those characters, or one of the \r or  \n  escape  sequences.  Implicit
1967           matches  such  as [^X] do not count, nor does \s (which includes CR and
1968           LF in the characters that it matches).
1969    
1970           Notwithstanding the above, anomalous effects may still occur when  CRLF
1971           is a valid newline sequence and explicit \r or \n escapes appear in the
1972           pattern.
1973    
1974           PCRE_NOTBOL           PCRE_NOTBOL
1975    
1976         This option specifies that first character of the subject string is not         This option specifies that first character of the subject string is not
1977         the beginning of a line, so the  circumflex  metacharacter  should  not         the  beginning  of  a  line, so the circumflex metacharacter should not
1978         match  before it. Setting this without PCRE_MULTILINE (at compile time)         match before it. Setting this without PCRE_MULTILINE (at compile  time)
1979         causes circumflex never to match. This option affects only  the  behav-         causes  circumflex  never to match. This option affects only the behav-
1980         iour of the circumflex metacharacter. It does not affect \A.         iour of the circumflex metacharacter. It does not affect \A.
1981    
1982           PCRE_NOTEOL           PCRE_NOTEOL
1983    
1984         This option specifies that the end of the subject string is not the end         This option specifies that the end of the subject string is not the end
1985         of a line, so the dollar metacharacter should not match it nor  (except         of  a line, so the dollar metacharacter should not match it nor (except
1986         in  multiline mode) a newline immediately before it. Setting this with-         in multiline mode) a newline immediately before it. Setting this  with-
1987         out PCRE_MULTILINE (at compile time) causes dollar never to match. This         out PCRE_MULTILINE (at compile time) causes dollar never to match. This
1988         option  affects only the behaviour of the dollar metacharacter. It does         option affects only the behaviour of the dollar metacharacter. It  does
1989         not affect \Z or \z.         not affect \Z or \z.
1990    
1991           PCRE_NOTEMPTY           PCRE_NOTEMPTY
1992    
1993         An empty string is not considered to be a valid match if this option is         An empty string is not considered to be a valid match if this option is
1994         set.  If  there are alternatives in the pattern, they are tried. If all         set. If there are alternatives in the pattern, they are tried.  If  all
1995         the alternatives match the empty string, the entire  match  fails.  For         the  alternatives  match  the empty string, the entire match fails. For
1996         example, if the pattern         example, if the pattern
1997    
1998           a?b?           a?b?
1999    
2000         is  applied  to  a string not beginning with "a" or "b", it matches the         is applied to a string not beginning with "a" or "b",  it  matches  the
2001         empty string at the start of the subject. With PCRE_NOTEMPTY set,  this         empty  string at the start of the subject. With PCRE_NOTEMPTY set, this
2002         match is not valid, so PCRE searches further into the string for occur-         match is not valid, so PCRE searches further into the string for occur-
2003         rences of "a" or "b".         rences of "a" or "b".
2004    
2005         Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a spe-         Perl has no direct equivalent of PCRE_NOTEMPTY, but it does make a spe-
2006         cial  case  of  a  pattern match of the empty string within its split()         cial case of a pattern match of the empty  string  within  its  split()
2007         function, and when using the /g modifier. It  is  possible  to  emulate         function,  and  when  using  the /g modifier. It is possible to emulate
2008         Perl's behaviour after matching a null string by first trying the match         Perl's behaviour after matching a null string by first trying the match
2009         again at the same offset with PCRE_NOTEMPTY and PCRE_ANCHORED, and then         again at the same offset with PCRE_NOTEMPTY and PCRE_ANCHORED, and then
2010         if  that  fails by advancing the starting offset (see below) and trying         if that fails by advancing the starting offset (see below)  and  trying
2011         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
2012         this in the pcredemo.c sample program.         this in the pcredemo.c sample program.
2013    
2014           PCRE_NO_UTF8_CHECK           PCRE_NO_UTF8_CHECK
2015    
2016         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
2017         UTF-8 string is automatically checked when pcre_exec() is  subsequently         UTF-8  string is automatically checked when pcre_exec() is subsequently
2018         called.   The  value  of  startoffset is also checked to ensure that it         called.  The value of startoffset is also checked  to  ensure  that  it
2019         points to the start of a UTF-8 character. There is a  discussion  about         points  to  the start of a UTF-8 character. There is a discussion about
2020         the  validity  of  UTF-8 strings in the section on UTF-8 support in the         the validity of UTF-8 strings in the section on UTF-8  support  in  the
2021         main pcre page. If  an  invalid  UTF-8  sequence  of  bytes  is  found,         main  pcre  page.  If  an  invalid  UTF-8  sequence  of bytes is found,
2022         pcre_exec()  returns  the error PCRE_ERROR_BADUTF8. If startoffset con-         pcre_exec() returns the error PCRE_ERROR_BADUTF8. If  startoffset  con-
2023         tains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is returned.         tains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is returned.
2024    
2025         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
2026         these    checks    for   performance   reasons,   you   can   set   the         these   checks   for   performance   reasons,   you   can    set    the
2027         PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might  want  to         PCRE_NO_UTF8_CHECK  option  when calling pcre_exec(). You might want to
2028         do  this  for the second and subsequent calls to pcre_exec() if you are         do this for the second and subsequent calls to pcre_exec() if  you  are
2029         making repeated calls to find all  the  matches  in  a  single  subject         making  repeated  calls  to  find  all  the matches in a single subject
2030         string.  However,  you  should  be  sure  that the value of startoffset         string. However, you should be  sure  that  the  value  of  startoffset
2031         points to the start of a UTF-8 character.  When  PCRE_NO_UTF8_CHECK  is         points  to  the  start of a UTF-8 character. When PCRE_NO_UTF8_CHECK is
2032         set,  the  effect of passing an invalid UTF-8 string as a subject, or a         set, the effect of passing an invalid UTF-8 string as a subject,  or  a
2033         value of startoffset that does not point to the start of a UTF-8  char-         value  of startoffset that does not point to the start of a UTF-8 char-
2034         acter, is undefined. Your program may crash.         acter, is undefined. Your program may crash.
2035    
2036           PCRE_PARTIAL           PCRE_PARTIAL
2037    
2038         This  option  turns  on  the  partial  matching feature. If the subject         This option turns on the  partial  matching  feature.  If  the  subject
2039         string fails to match the pattern, but at some point during the  match-         string  fails to match the pattern, but at some point during the match-
2040         ing  process  the  end of the subject was reached (that is, the subject         ing process the end of the subject was reached (that  is,  the  subject
2041         partially matches the pattern and the failure to  match  occurred  only         partially  matches  the  pattern and the failure to match occurred only
2042         because  there were not enough subject characters), pcre_exec() returns         because there were not enough subject characters), pcre_exec()  returns
2043         PCRE_ERROR_PARTIAL instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL  is         PCRE_ERROR_PARTIAL  instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is
2044         used,  there  are restrictions on what may appear in the pattern. These         used, there are restrictions on what may appear in the  pattern.  These
2045         are discussed in the pcrepartial documentation.         are discussed in the pcrepartial documentation.
2046    
2047     The string to be matched by pcre_exec()     The string to be matched by pcre_exec()
2048    
2049         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
2050         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.
2051         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-
2052         Unlike  the  pattern string, the subject may contain binary zero bytes.         acter. Unlike the pattern string, the subject may contain  binary  zero
2053         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
2054         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
2055           case.
2056    
2057         A  non-zero  starting offset is useful when searching for another match         A  non-zero  starting offset is useful when searching for another match
2058         in the same subject by calling pcre_exec() again after a previous  suc-         in the same subject by calling pcre_exec() again after a previous  suc-
# Line 1934  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2088  MATCHING A PATTERN: THE TRADITIONAL FUNC
2088         string. PCRE supports several other kinds of  parenthesized  subpattern         string. PCRE supports several other kinds of  parenthesized  subpattern
2089         that do not cause substrings to be captured.         that do not cause substrings to be captured.
2090    
2091         Captured  substrings are returned to the caller via a vector of integer         Captured substrings are returned to the caller via a vector of integers
2092         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-
2093         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:
2094         Note: this argument is NOT the size of ovector in bytes.         this argument is NOT the size of ovector in bytes.
2095    
2096         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-
2097         strings,  each  substring using a pair of integers. The remaining third         strings,  each  substring using a pair of integers. The remaining third
2098         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-
2099         turing  subpatterns, and is not available for passing back information.         turing  subpatterns, and is not available for passing back information.
2100         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
2101         it is not, it is rounded down.         it is not, it is rounded down.
2102    
2103         When  a  match  is successful, information about captured substrings is         When  a  match  is successful, information about captured substrings is
2104         returned in pairs of integers, starting at the  beginning  of  ovector,         returned in pairs of integers, starting at the  beginning  of  ovector,
2105         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
2106         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
2107         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
2108         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
2109         tor[1],  identify  the  portion  of  the  subject string matched by the         byte offsets, even in UTF-8 mode. They are not character counts.
2110         entire pattern. The next pair is used for the first  capturing  subpat-  
2111         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
2112         highest numbered pair that has been set. For example, if two substrings         portion of the subject string matched by the entire pattern.  The  next
2113         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
2114         subpatterns, the return value from a successful match is 1,  indicating         returned by pcre_exec() is one more than the highest numbered pair that
2115         that just the first pair of offsets has been set.         has  been  set.  For example, if two substrings have been captured, the
2116           returned value is 3. If there are no capturing subpatterns, the  return
2117           value from a successful match is 1, indicating that just the first pair
2118           of offsets has been set.
2119    
2120         If a capturing subpattern is matched repeatedly, it is the last portion         If a capturing subpattern is matched repeatedly, it is the last portion
2121         of the string that it matched that is returned.         of the string that it matched that is returned.
2122    
2123         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,
2124         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
2125         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
2126         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
2127         as NULL and ovecsize as zero. However, if  the  pattern  contains  back         ovecsize  as zero. However, if the pattern contains back references and
2128         references  and  the  ovector is not big enough to remember the related         the ovector is not big enough to remember the related substrings,  PCRE
2129         substrings, PCRE has to get additional memory for use during  matching.         has  to  get additional memory for use during matching. Thus it is usu-
2130         Thus it is usually advisable to supply an ovector.         ally advisable to supply an ovector.
2131    
2132         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
2133         subpatterns there are in a compiled  pattern.  The  smallest  size  for         subpatterns  there  are  in  a  compiled pattern. The smallest size for
2134         ovector  that  will allow for n captured substrings, in addition to the         ovector that will allow for n captured substrings, in addition  to  the
2135         offsets of the substring matched by the whole pattern, is (n+1)*3.         offsets of the substring matched by the whole pattern, is (n+1)*3.
2136    
2137         It is possible for capturing subpattern number n+1 to match  some  part         It  is  possible for capturing subpattern number n+1 to match some part
2138         of the subject when subpattern n has not been used at all. For example,         of the subject when subpattern n has not been used at all. For example,
2139         if the string "abc" is matched  against  the  pattern  (a|(z))(bc)  the         if  the  string  "abc"  is  matched against the pattern (a|(z))(bc) the
2140         return from the function is 4, and subpatterns 1 and 3 are matched, but         return from the function is 4, and subpatterns 1 and 3 are matched, but
2141         2 is not. When this happens, both values in  the  offset  pairs  corre-         2  is  not.  When  this happens, both values in the offset pairs corre-
2142         sponding to unused subpatterns are set to -1.         sponding to unused subpatterns are set to -1.
2143    
2144         Offset  values  that correspond to unused subpatterns at the end of the         Offset values that correspond to unused subpatterns at the end  of  the
2145         expression are also set to -1. For example,  if  the  string  "abc"  is         expression  are  also  set  to  -1. For example, if the string "abc" is
2146         matched  against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not         matched against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are  not
2147         matched. The return from the function is 2, because  the  highest  used         matched.  The  return  from the function is 2, because the highest used
2148         capturing subpattern number is 1. However, you can refer to the offsets         capturing subpattern number is 1. However, you can refer to the offsets
2149         for the second and third capturing subpatterns if  you  wish  (assuming         for  the  second  and third capturing subpatterns if you wish (assuming
2150         the vector is large enough, of course).         the vector is large enough, of course).
2151    
2152         Some  convenience  functions  are  provided for extracting the captured         Some convenience functions are provided  for  extracting  the  captured
2153         substrings as separate strings. These are described below.         substrings as separate strings. These are described below.
2154    
2155     Error return values from pcre_exec()     Error return values from pcre_exec()
2156    
2157         If pcre_exec() fails, it returns a negative number. The  following  are         If  pcre_exec()  fails, it returns a negative number. The following are
2158         defined in the header file:         defined in the header file:
2159    
2160           PCRE_ERROR_NOMATCH        (-1)           PCRE_ERROR_NOMATCH        (-1)
# Line 2006  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2163  MATCHING A PATTERN: THE TRADITIONAL FUNC
2163    
2164           PCRE_ERROR_NULL           (-2)           PCRE_ERROR_NULL           (-2)
2165    
2166         Either  code  or  subject  was  passed as NULL, or ovector was NULL and         Either code or subject was passed as NULL,  or  ovector  was  NULL  and
2167         ovecsize was not zero.         ovecsize was not zero.
2168    
2169           PCRE_ERROR_BADOPTION      (-3)           PCRE_ERROR_BADOPTION      (-3)
# Line 2015  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2172  MATCHING A PATTERN: THE TRADITIONAL FUNC
2172    
2173           PCRE_ERROR_BADMAGIC       (-4)           PCRE_ERROR_BADMAGIC       (-4)
2174    
2175         PCRE stores a 4-byte "magic number" at the start of the compiled  code,         PCRE  stores a 4-byte "magic number" at the start of the compiled code,
2176         to catch the case when it is passed a junk pointer and to detect when a         to catch the case when it is passed a junk pointer and to detect when a
2177         pattern that was compiled in an environment of one endianness is run in         pattern that was compiled in an environment of one endianness is run in
2178         an  environment  with the other endianness. This is the error that PCRE         an environment with the other endianness. This is the error  that  PCRE
2179         gives when the magic number is not present.         gives when the magic number is not present.
2180    
2181           PCRE_ERROR_UNKNOWN_OPCODE (-5)           PCRE_ERROR_UNKNOWN_OPCODE (-5)
2182    
2183         While running the pattern match, an unknown item was encountered in the         While running the pattern match, an unknown item was encountered in the
2184         compiled  pattern.  This  error  could be caused by a bug in PCRE or by         compiled pattern. This error could be caused by a bug  in  PCRE  or  by
2185         overwriting of the compiled pattern.         overwriting of the compiled pattern.
2186    
2187           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2188    
2189         If a pattern contains back references, but the ovector that  is  passed         If  a  pattern contains back references, but the ovector that is passed
2190         to pcre_exec() is not big enough to remember the referenced substrings,         to pcre_exec() is not big enough to remember the referenced substrings,
2191         PCRE gets a block of memory at the start of matching to  use  for  this         PCRE  gets  a  block of memory at the start of matching to use for this
2192         purpose.  If the call via pcre_malloc() fails, this error is given. The         purpose. If the call via pcre_malloc() fails, this error is given.  The
2193         memory is automatically freed at the end of matching.         memory is automatically freed at the end of matching.
2194    
2195           PCRE_ERROR_NOSUBSTRING    (-7)           PCRE_ERROR_NOSUBSTRING    (-7)
2196    
2197         This error is used by the pcre_copy_substring(),  pcre_get_substring(),         This  error is used by the pcre_copy_substring(), pcre_get_substring(),
2198         and  pcre_get_substring_list()  functions  (see  below).  It  is  never         and  pcre_get_substring_list()  functions  (see  below).  It  is  never
2199         returned by pcre_exec().         returned by pcre_exec().
2200    
2201           PCRE_ERROR_MATCHLIMIT     (-8)           PCRE_ERROR_MATCHLIMIT     (-8)
2202    
2203         The backtracking limit, as specified by  the  match_limit  field  in  a         The  backtracking  limit,  as  specified  by the match_limit field in a
2204         pcre_extra  structure  (or  defaulted) was reached. See the description         pcre_extra structure (or defaulted) was reached.  See  the  description
2205         above.         above.
2206    
2207           PCRE_ERROR_CALLOUT        (-9)           PCRE_ERROR_CALLOUT        (-9)
2208    
2209         This error is never generated by pcre_exec() itself. It is provided for         This error is never generated by pcre_exec() itself. It is provided for
2210         use  by  callout functions that want to yield a distinctive error code.         use by callout functions that want to yield a distinctive  error  code.
2211         See the pcrecallout documentation for details.         See the pcrecallout documentation for details.
2212    
2213           PCRE_ERROR_BADUTF8        (-10)           PCRE_ERROR_BADUTF8        (-10)
2214    
2215         A string that contains an invalid UTF-8 byte sequence was passed  as  a         A  string  that contains an invalid UTF-8 byte sequence was passed as a
2216         subject.         subject.
2217    
2218           PCRE_ERROR_BADUTF8_OFFSET (-11)           PCRE_ERROR_BADUTF8_OFFSET (-11)
2219    
2220         The UTF-8 byte sequence that was passed as a subject was valid, but the         The UTF-8 byte sequence that was passed as a subject was valid, but the
2221         value of startoffset did not point to the beginning of a UTF-8  charac-         value  of startoffset did not point to the beginning of a UTF-8 charac-
2222         ter.         ter.
2223    
2224           PCRE_ERROR_PARTIAL        (-12)           PCRE_ERROR_PARTIAL        (-12)
2225    
2226         The  subject  string did not match, but it did match partially. See the         The subject string did not match, but it did match partially.  See  the
2227         pcrepartial documentation for details of partial matching.         pcrepartial documentation for details of partial matching.
2228    
2229           PCRE_ERROR_BADPARTIAL     (-13)           PCRE_ERROR_BADPARTIAL     (-13)
2230    
2231         The PCRE_PARTIAL option was used with  a  compiled  pattern  containing         The  PCRE_PARTIAL  option  was  used with a compiled pattern containing
2232         items  that are not supported for partial matching. See the pcrepartial         items that are not supported for partial matching. See the  pcrepartial
2233         documentation for details of partial matching.         documentation for details of partial matching.
2234    
2235           PCRE_ERROR_INTERNAL       (-14)           PCRE_ERROR_INTERNAL       (-14)
2236    
2237         An unexpected internal error has occurred. This error could  be  caused         An  unexpected  internal error has occurred. This error could be caused
2238         by a bug in PCRE or by overwriting of the compiled pattern.         by a bug in PCRE or by overwriting of the compiled pattern.
2239    
2240           PCRE_ERROR_BADCOUNT       (-15)           PCRE_ERROR_BADCOUNT       (-15)
2241    
2242         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.
2243    
2244           PCRE_ERROR_RECURSIONLIMIT (-21)           PCRE_ERROR_RECURSIONLIMIT (-21)
2245    
2246         The internal recursion limit, as specified by the match_limit_recursion         The internal recursion limit, as specified by the match_limit_recursion
2247         field  in  a  pcre_extra  structure (or defaulted) was reached. See the         field in a pcre_extra structure (or defaulted)  was  reached.  See  the
2248         description above.         description above.
2249    
2250           PCRE_ERROR_BADNEWLINE     (-23)           PCRE_ERROR_BADNEWLINE     (-23)
# Line 2110  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER Line 2267  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER
2267         int pcre_get_substring_list(const char *subject,         int pcre_get_substring_list(const char *subject,
2268              int *ovector, int stringcount, const char ***listptr);              int *ovector, int stringcount, const char ***listptr);
2269    
2270         Captured substrings can be  accessed  directly  by  using  the  offsets         Captured  substrings  can  be  accessed  directly  by using the offsets
2271         returned  by  pcre_exec()  in  ovector.  For convenience, the functions         returned by pcre_exec() in  ovector.  For  convenience,  the  functions
2272         pcre_copy_substring(),    pcre_get_substring(),    and    pcre_get_sub-         pcre_copy_substring(),    pcre_get_substring(),    and    pcre_get_sub-
2273         string_list()  are  provided for extracting captured substrings as new,         string_list() are provided for extracting captured substrings  as  new,
2274         separate, zero-terminated strings. These functions identify  substrings         separate,  zero-terminated strings. These functions identify substrings
2275         by  number.  The  next section describes functions for extracting named         by number. The next section describes functions  for  extracting  named
2276         substrings.         substrings.
2277    
2278         A substring that contains a binary zero is correctly extracted and  has         A  substring that contains a binary zero is correctly extracted and has
2279         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
2280         string.  However, you can process such a string  by  referring  to  the         string.   However,  you  can  process such a string by referring to the
2281         length  that  is  returned  by  pcre_copy_substring() and pcre_get_sub-         length that is  returned  by  pcre_copy_substring()  and  pcre_get_sub-
2282         string().  Unfortunately, the interface to pcre_get_substring_list() is         string().  Unfortunately, the interface to pcre_get_substring_list() is
2283         not  adequate for handling strings containing binary zeros, because the         not adequate for handling strings containing binary zeros, because  the
2284         end of the final string is not independently indicated.         end of the final string is not independently indicated.
2285    
2286         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-
2287         tions:  subject  is  the subject string that has just been successfully         tions: subject is the subject string that has  just  been  successfully
2288         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
2289         passed to pcre_exec(), and stringcount is the number of substrings that         passed to pcre_exec(), and stringcount is the number of substrings that
2290         were captured by the match, including the substring  that  matched  the         were  captured  by  the match, including the substring that matched the
2291         entire regular expression. This is the value returned by pcre_exec() if         entire regular expression. This is the value returned by pcre_exec() if
2292         it is greater than zero. If pcre_exec() returned zero, indicating  that         it  is greater than zero. If pcre_exec() returned zero, indicating that
2293         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
2294         be the number of elements in the vector divided by three.         be the number of elements in the vector divided by three.
2295    
2296         The functions pcre_copy_substring() and pcre_get_substring() extract  a         The  functions pcre_copy_substring() and pcre_get_substring() extract a
2297         single  substring,  whose  number  is given as stringnumber. A value of         single substring, whose number is given as  stringnumber.  A  value  of
2298         zero extracts the substring that matched the  entire  pattern,  whereas         zero  extracts  the  substring that matched the entire pattern, whereas
2299         higher  values  extract  the  captured  substrings.  For pcre_copy_sub-         higher values  extract  the  captured  substrings.  For  pcre_copy_sub-
2300         string(), the string is placed in buffer,  whose  length  is  given  by         string(),  the  string  is  placed  in buffer, whose length is given by
2301         buffersize,  while  for  pcre_get_substring()  a new block of memory is         buffersize, while for pcre_get_substring() a new  block  of  memory  is
2302         obtained via pcre_malloc, and its address is  returned  via  stringptr.         obtained  via  pcre_malloc,  and its address is returned via stringptr.
2303         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
2304         the terminating zero, or one of these error codes:         the terminating zero, or one of these error codes:
2305    
2306           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2307    
2308         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
2309         get memory failed for pcre_get_substring().         get memory failed for pcre_get_substring().
2310    
2311           PCRE_ERROR_NOSUBSTRING    (-7)           PCRE_ERROR_NOSUBSTRING    (-7)
2312    
2313         There is no substring whose number is stringnumber.         There is no substring whose number is stringnumber.
2314    
2315         The  pcre_get_substring_list()  function  extracts  all  available sub-         The pcre_get_substring_list()  function  extracts  all  available  sub-
2316         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
2317         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
2318         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
2319         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
2320         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
2321         error code         error code
2322    
2323           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2324    
2325         if the attempt to get the memory block failed.         if the attempt to get the memory block failed.
2326    
2327         When  any of these functions encounter a substring that is unset, which         When any of these functions encounter a substring that is unset,  which
2328         can happen when capturing subpattern number n+1 matches  some  part  of         can  happen  when  capturing subpattern number n+1 matches some part of
2329         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
2330         empty string. This can be distinguished from a genuine zero-length sub-         empty string. This can be distinguished from a genuine zero-length sub-
2331         string  by inspecting the appropriate offset in ovector, which is nega-         string by inspecting the appropriate offset in ovector, which is  nega-
2332         tive for unset substrings.         tive for unset substrings.
2333    
2334         The two convenience functions pcre_free_substring() and  pcre_free_sub-         The  two convenience functions pcre_free_substring() and pcre_free_sub-
2335         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
2336         call  of  pcre_get_substring()  or  pcre_get_substring_list(),  respec-         call  of  pcre_get_substring()  or  pcre_get_substring_list(),  respec-
2337         tively.  They  do  nothing  more  than  call the function pointed to by         tively. They do nothing more than  call  the  function  pointed  to  by
2338         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.
2339         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-
2340         cial  interface  to  another  programming  language  that  cannot   use         cial   interface  to  another  programming  language  that  cannot  use
2341         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-
2342         vided.         vided.
2343    
2344    
# Line 2200  EXTRACTING CAPTURED SUBSTRINGS BY NAME Line 2357  EXTRACTING CAPTURED SUBSTRINGS BY NAME
2357              int stringcount, const char *stringname,              int stringcount, const char *stringname,
2358              const char **stringptr);              const char **stringptr);
2359    
2360         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-
2361         ber.  For example, for this pattern         ber.  For example, for this pattern
2362    
2363           (a+)b(?<xxx>\d+)...           (a+)b(?<xxx>\d+)...
# Line 2209  EXTRACTING CAPTURED SUBSTRINGS BY NAME Line 2366  EXTRACTING CAPTURED SUBSTRINGS BY NAME
2366         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
2367         name by calling pcre_get_stringnumber(). The first argument is the com-         name by calling pcre_get_stringnumber(). The first argument is the com-
2368         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
2369         the  subpattern  number,  or PCRE_ERROR_NOSUBSTRING (-7) if there is no         the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if  there  is  no
2370         subpattern of that name.         subpattern of that name.
2371    
2372         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
2373         the functions described in the previous section. For convenience, there         the functions described in the previous section. For convenience, there
2374         are also two functions that do the whole job.         are also two functions that do the whole job.
2375    
2376         Most   of   the   arguments    of    pcre_copy_named_substring()    and         Most    of    the    arguments   of   pcre_copy_named_substring()   and
2377         pcre_get_named_substring()  are  the  same  as  those for the similarly         pcre_get_named_substring() are the same  as  those  for  the  similarly
2378         named functions that extract by number. As these are described  in  the         named  functions  that extract by number. As these are described in the
2379         previous  section,  they  are not re-described here. There are just two         previous section, they are not re-described here. There  are  just  two
2380         differences:         differences:
2381    
2382         First, instead of a substring number, a substring name is  given.  Sec-         First,  instead  of a substring number, a substring name is given. Sec-
2383         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
2384         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
2385         name-to-number translation table.         name-to-number translation table.
2386    
2387         These  functions call pcre_get_stringnumber(), and if it succeeds, they         These functions call pcre_get_stringnumber(), and if it succeeds,  they
2388         then call pcre_copy_substring() or pcre_get_substring(),  as  appropri-         then  call  pcre_copy_substring() or pcre_get_substring(), as appropri-
2389         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
2390         behaviour may not be what you want (see the next section).         behaviour may not be what you want (see the next section).
2391    
2392    
# Line 2238  DUPLICATE SUBPATTERN NAMES Line 2395  DUPLICATE SUBPATTERN NAMES
2395         int pcre_get_stringtable_entries(const pcre *code,         int pcre_get_stringtable_entries(const pcre *code,
2396              const char *name, char **first, char **last);              const char *name, char **first, char **last);
2397    
2398         When a pattern is compiled with the  PCRE_DUPNAMES  option,  names  for         When  a  pattern  is  compiled with the PCRE_DUPNAMES option, names for
2399         subpatterns  are  not  required  to  be unique. Normally, patterns with         subpatterns are not required to  be  unique.  Normally,  patterns  with
2400         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
2401         subpatterns  participates. An example is shown in the pcrepattern docu-         subpatterns participates. An example is shown in the pcrepattern  docu-
2402         mentation.         mentation.
2403    
2404         When   duplicates   are   present,   pcre_copy_named_substring()    and         When    duplicates   are   present,   pcre_copy_named_substring()   and
2405         pcre_get_named_substring()  return the first substring corresponding to         pcre_get_named_substring() return the first substring corresponding  to
2406         the given name that is set. If  none  are  set,  PCRE_ERROR_NOSUBSTRING         the  given  name  that  is set. If none are set, PCRE_ERROR_NOSUBSTRING
2407         (-7)  is  returned;  no  data  is returned. The pcre_get_stringnumber()         (-7) is returned; no  data  is  returned.  The  pcre_get_stringnumber()
2408         function returns one of the numbers that are associated with the  name,         function  returns one of the numbers that are associated with the name,
2409         but it is not defined which it is.         but it is not defined which it is.
2410    
2411         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
2412         name, you must use  the  pcre_get_stringtable_entries()  function.  The         name,  you  must  use  the pcre_get_stringtable_entries() function. The
2413         first argument is the compiled pattern, and the second is the name. The         first argument is the compiled pattern, and the second is the name. The
2414         third and fourth are pointers to variables which  are  updated  by  the         third  and  fourth  are  pointers to variables which are updated by the
2415         function. After it has run, they point to the first and last entries in         function. After it has run, they point to the first and last entries in
2416         the name-to-number table  for  the  given  name.  The  function  itself         the  name-to-number  table  for  the  given  name.  The function itself
2417         returns  the  length  of  each entry, or PCRE_ERROR_NOSUBSTRING (-7) if         returns the length of each entry,  or  PCRE_ERROR_NOSUBSTRING  (-7)  if
2418         there are none. The format of the table is described above in the  sec-         there  are none. The format of the table is described above in the sec-
2419         tion  entitled  Information  about  a  pattern.  Given all the relevant         tion entitled Information about a  pattern.   Given  all  the  relevant
2420         entries for the name, you can extract each of their numbers, and  hence         entries  for the name, you can extract each of their numbers, and hence
2421         the captured data, if any.         the captured data, if any.
2422    
2423    
2424  FINDING ALL POSSIBLE MATCHES  FINDING ALL POSSIBLE MATCHES
2425    
2426         The  traditional  matching  function  uses a similar algorithm to Perl,         The traditional matching function uses a  similar  algorithm  to  Perl,
2427         which stops when it finds the first match, starting at a given point in         which stops when it finds the first match, starting at a given point in
2428         the  subject.  If you want to find all possible matches, or the longest         the subject. If you want to find all possible matches, or  the  longest
2429         possible match, consider using the alternative matching  function  (see         possible  match,  consider using the alternative matching function (see
2430         below)  instead.  If you cannot use the alternative function, but still         below) instead. If you cannot use the alternative function,  but  still
2431         need to find all possible matches, you can kludge it up by  making  use         need  to  find all possible matches, you can kludge it up by making use
2432         of the callout facility, which is described in the pcrecallout documen-         of the callout facility, which is described in the pcrecallout documen-
2433         tation.         tation.
2434    
2435         What you have to do is to insert a callout right at the end of the pat-         What you have to do is to insert a callout right at the end of the pat-
2436         tern.   When your callout function is called, extract and save the cur-         tern.  When your callout function is called, extract and save the  cur-
2437         rent matched substring. Then return  1,  which  forces  pcre_exec()  to         rent  matched  substring.  Then  return  1, which forces pcre_exec() to
2438         backtrack  and  try other alternatives. Ultimately, when it runs out of         backtrack and try other alternatives. Ultimately, when it runs  out  of
2439         matches, pcre_exec() will yield PCRE_ERROR_NOMATCH.         matches, pcre_exec() will yield PCRE_ERROR_NOMATCH.
2440    
2441    
# Line 2289  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2446  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2446              int options, int *ovector, int ovecsize,              int options, int *ovector, int ovecsize,
2447              int *workspace, int wscount);              int *workspace, int wscount);
2448    
2449         The function pcre_dfa_exec()  is  called  to  match  a  subject  string         The  function  pcre_dfa_exec()  is  called  to  match  a subject string
2450         against  a  compiled pattern, using a matching algorithm that scans the         against a compiled pattern, using a matching algorithm that  scans  the
2451         subject string just once, and does not backtrack.  This  has  different         subject  string  just  once, and does not backtrack. This has different
2452         characteristics  to  the  normal  algorithm, and is not compatible with         characteristics to the normal algorithm, and  is  not  compatible  with
2453         Perl. Some of the features of PCRE patterns are not  supported.  Never-         Perl.  Some  of the features of PCRE patterns are not supported. Never-
2454         theless,  there are times when this kind of matching can be useful. For         theless, there are times when this kind of matching can be useful.  For
2455         a discussion of the two matching algorithms, see the pcrematching docu-         a discussion of the two matching algorithms, see the pcrematching docu-
2456         mentation.         mentation.
2457    
2458         The  arguments  for  the  pcre_dfa_exec()  function are the same as for         The arguments for the pcre_dfa_exec() function  are  the  same  as  for
2459         pcre_exec(), plus two extras. The ovector argument is used in a differ-         pcre_exec(), plus two extras. The ovector argument is used in a differ-
2460         ent  way,  and  this is described below. The other common arguments are         ent way, and this is described below. The other  common  arguments  are
2461         used in the same way as for pcre_exec(), so their  description  is  not         used  in  the  same way as for pcre_exec(), so their description is not
2462         repeated here.         repeated here.
2463    
2464         The  two  additional  arguments provide workspace for the function. The         The two additional arguments provide workspace for  the  function.  The
2465         workspace vector should contain at least 20 elements. It  is  used  for         workspace  vector  should  contain at least 20 elements. It is used for
2466         keeping  track  of  multiple  paths  through  the  pattern  tree.  More         keeping  track  of  multiple  paths  through  the  pattern  tree.  More
2467         workspace will be needed for patterns and subjects where  there  are  a         workspace  will  be  needed for patterns and subjects where there are a
2468         lot of potential matches.         lot of potential matches.
2469    
2470         Here is an example of a simple call to pcre_dfa_exec():         Here is an example of a simple call to pcre_dfa_exec():
# Line 2329  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2486  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2486    
2487     Option bits for pcre_dfa_exec()     Option bits for pcre_dfa_exec()
2488    
2489         The  unused  bits  of  the options argument for pcre_dfa_exec() must be         The unused bits of the options argument  for  pcre_dfa_exec()  must  be
2490         zero. The only bits  that  may  be  set  are  PCRE_ANCHORED,  PCRE_NEW-         zero.  The  only  bits  that  may  be  set are PCRE_ANCHORED, PCRE_NEW-
2491         LINE_xxx,  PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK,         LINE_xxx, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY,  PCRE_NO_UTF8_CHECK,
2492         PCRE_PARTIAL, PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last         PCRE_PARTIAL, PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last
2493         three of these are the same as for pcre_exec(), so their description is         three of these are the same as for pcre_exec(), so their description is
2494         not repeated here.         not repeated here.
2495    
2496           PCRE_PARTIAL           PCRE_PARTIAL
2497    
2498         This has the same general effect as it does for  pcre_exec(),  but  the         This  has  the  same general effect as it does for pcre_exec(), but the
2499         details   are   slightly   different.  When  PCRE_PARTIAL  is  set  for         details  are  slightly  different.  When  PCRE_PARTIAL   is   set   for
2500         pcre_dfa_exec(), the return code PCRE_ERROR_NOMATCH is  converted  into         pcre_dfa_exec(),  the  return code PCRE_ERROR_NOMATCH is converted into
2501         PCRE_ERROR_PARTIAL  if  the  end  of the subject is reached, there have         PCRE_ERROR_PARTIAL if the end of the subject  is  reached,  there  have
2502         been no complete matches, but there is still at least one matching pos-         been no complete matches, but there is still at least one matching pos-
2503         sibility.  The portion of the string that provided the partial match is         sibility. The portion of the string that provided the partial match  is
2504         set as the first matching string.         set as the first matching string.
2505    
2506           PCRE_DFA_SHORTEST           PCRE_DFA_SHORTEST
2507    
2508         Setting the PCRE_DFA_SHORTEST option causes the matching  algorithm  to         Setting  the  PCRE_DFA_SHORTEST option causes the matching algorithm to
2509         stop as soon as it has found one match. Because of the way the alterna-         stop as soon as it has found one match. Because of the way the alterna-
2510         tive algorithm works, this is necessarily the shortest  possible  match         tive  algorithm  works, this is necessarily the shortest possible match
2511         at the first possible matching point in the subject string.         at the first possible matching point in the subject string.
2512    
2513           PCRE_DFA_RESTART           PCRE_DFA_RESTART
2514    
2515         When  pcre_dfa_exec()  is  called  with  the  PCRE_PARTIAL  option, and         When pcre_dfa_exec()  is  called  with  the  PCRE_PARTIAL  option,  and
2516         returns a partial match, it is possible to call it  again,  with  addi-         returns  a  partial  match, it is possible to call it again, with addi-
2517         tional  subject  characters,  and have it continue with the same match.         tional subject characters, and have it continue with  the  same  match.
2518         The PCRE_DFA_RESTART option requests this action; when it is  set,  the         The  PCRE_DFA_RESTART  option requests this action; when it is set, the
2519         workspace  and wscount options must reference the same vector as before         workspace and wscount options must reference the same vector as  before
2520         because data about the match so far is left in  them  after  a  partial         because  data  about  the  match so far is left in them after a partial
2521         match.  There  is  more  discussion of this facility in the pcrepartial         match. There is more discussion of this  facility  in  the  pcrepartial
2522         documentation.         documentation.
2523    
2524     Successful returns from pcre_dfa_exec()     Successful returns from pcre_dfa_exec()
2525    
2526         When pcre_dfa_exec() succeeds, it may have matched more than  one  sub-         When  pcre_dfa_exec()  succeeds, it may have matched more than one sub-
2527         string in the subject. Note, however, that all the matches from one run         string in the subject. Note, however, that all the matches from one run
2528         of the function start at the same point in  the  subject.  The  shorter         of  the  function  start  at the same point in the subject. The shorter
2529         matches  are all initial substrings of the longer matches. For example,         matches are all initial substrings of the longer matches. For  example,
2530         if the pattern         if the pattern
2531    
2532           <.*>           <.*>
# Line 2384  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2541  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2541           <something> <something else>           <something> <something else>
2542           <something> <something else> <something further>           <something> <something else> <something further>
2543    
2544         On success, the yield of the function is a number  greater  than  zero,         On  success,  the  yield of the function is a number greater than zero,
2545         which  is  the  number of matched substrings. The substrings themselves         which is the number of matched substrings.  The  substrings  themselves
2546         are returned in ovector. Each string uses two elements;  the  first  is         are  returned  in  ovector. Each string uses two elements; the first is
2547         the  offset  to  the start, and the second is the offset to the end. In         the offset to the start, and the second is the offset to  the  end.  In
2548         fact, all the strings have the same start  offset.  (Space  could  have         fact,  all  the  strings  have the same start offset. (Space could have
2549         been  saved by giving this only once, but it was decided to retain some         been saved by giving this only once, but it was decided to retain  some
2550         compatibility with the way pcre_exec() returns data,  even  though  the         compatibility  with  the  way pcre_exec() returns data, even though the
2551         meaning of the strings is different.)         meaning of the strings is different.)
2552    
2553         The strings are returned in reverse order of length; that is, the long-         The strings are returned in reverse order of length; that is, the long-
2554         est matching string is given first. If there were too many  matches  to         est  matching  string is given first. If there were too many matches to
2555         fit  into ovector, the yield of the function is zero, and the vector is         fit into ovector, the yield of the function is zero, and the vector  is
2556         filled with the longest matches.         filled with the longest matches.
2557    
2558     Error returns from pcre_dfa_exec()     Error returns from pcre_dfa_exec()
2559    
2560         The pcre_dfa_exec() function returns a negative number when  it  fails.         The  pcre_dfa_exec()  function returns a negative number when it fails.
2561         Many  of  the  errors  are  the  same as for pcre_exec(), and these are         Many of the errors are the same  as  for  pcre_exec(),  and  these  are
2562         described above.  There are in addition the following errors  that  are         described  above.   There are in addition the following errors that are
2563         specific to pcre_dfa_exec():         specific to pcre_dfa_exec():
2564    
2565           PCRE_ERROR_DFA_UITEM      (-16)           PCRE_ERROR_DFA_UITEM      (-16)
2566    
2567         This  return is given if pcre_dfa_exec() encounters an item in the pat-         This return is given if pcre_dfa_exec() encounters an item in the  pat-
2568         tern that it does not support, for instance, the use of \C  or  a  back         tern  that  it  does not support, for instance, the use of \C or a back
2569         reference.         reference.
2570    
2571           PCRE_ERROR_DFA_UCOND      (-17)           PCRE_ERROR_DFA_UCOND      (-17)
2572    
2573         This  return  is  given  if pcre_dfa_exec() encounters a condition item         This return is given if pcre_dfa_exec()  encounters  a  condition  item
2574         that uses a back reference for the condition, or a test  for  recursion         that  uses  a back reference for the condition, or a test for recursion
2575         in a specific group. These are not supported.         in a specific group. These are not supported.
2576    
2577           PCRE_ERROR_DFA_UMLIMIT    (-18)           PCRE_ERROR_DFA_UMLIMIT    (-18)
2578    
2579         This  return  is given if pcre_dfa_exec() is called with an extra block         This return is given if pcre_dfa_exec() is called with an  extra  block
2580         that contains a setting of the match_limit field. This is not supported         that contains a setting of the match_limit field. This is not supported
2581         (it is meaningless).         (it is meaningless).
2582    
2583           PCRE_ERROR_DFA_WSSIZE     (-19)           PCRE_ERROR_DFA_WSSIZE     (-19)
2584    
2585         This  return  is  given  if  pcre_dfa_exec()  runs  out of space in the         This return is given if  pcre_dfa_exec()  runs  out  of  space  in  the
2586         workspace vector.         workspace vector.
2587    
2588           PCRE_ERROR_DFA_RECURSE    (-20)           PCRE_ERROR_DFA_RECURSE    (-20)
2589    
2590         When a recursive subpattern is processed, the matching  function  calls         When  a  recursive subpattern is processed, the matching function calls
2591         itself  recursively,  using  private vectors for ovector and workspace.         itself recursively, using private vectors for  ovector  and  workspace.
2592         This error is given if the output vector  is  not  large  enough.  This         This  error  is  given  if  the output vector is not large enough. This
2593         should be extremely rare, as a vector of size 1000 is used.         should be extremely rare, as a vector of size 1000 is used.
2594    
2595    
2596  SEE ALSO  SEE ALSO
2597    
2598         pcrebuild(3),  pcrecallout(3), pcrecpp(3)(3), pcrematching(3), pcrepar-         pcrebuild(3), pcrecallout(3), pcrecpp(3)(3), pcrematching(3),  pcrepar-
2599         tial(3), pcreposix(3), pcreprecompile(3), pcresample(3),  pcrestack(3).         tial(3),  pcreposix(3), pcreprecompile(3), pcresample(3), pcrestack(3).
2600    
2601    
2602  AUTHOR  AUTHOR
# Line 2451  AUTHOR Line 2608  AUTHOR
2608    
2609  REVISION  REVISION
2610    
2611         Last updated: 09 August 2007         Last updated: 24 August 2008
2612         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2008 University of Cambridge.
2613  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2614    
2615    
# Line 2736  DIFFERENCES BETWEEN PCRE AND PERL Line 2893  DIFFERENCES BETWEEN PCRE AND PERL
2893         (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-
2894         TURE options for pcre_exec() have no Perl equivalents.         TURE options for pcre_exec() have no Perl equivalents.
2895    
2896         (g) The callout facility is PCRE-specific.         (g) The \R escape sequence can be restricted to match only CR,  LF,  or
2897           CRLF by the PCRE_BSR_ANYCRLF option.
2898    
2899         (h) The partial matching facility is PCRE-specific.         (h) The callout facility is PCRE-specific.
2900    
2901         (i) Patterns compiled by PCRE can be saved and re-used at a later time,         (i) The partial matching facility is PCRE-specific.
2902    
2903           (j) Patterns compiled by PCRE can be saved and re-used at a later time,
2904         even on different hosts that have the other endianness.         even on different hosts that have the other endianness.
2905    
2906         (j)  The  alternative  matching function (pcre_dfa_exec()) matches in a         (k) The alternative matching function (pcre_dfa_exec())  matches  in  a
2907         different way and is not Perl-compatible.         different way and is not Perl-compatible.
2908    
2909           (l)  PCRE  recognizes some special sequences such as (*CR) at the start
2910           of a pattern that set overall options that cannot be changed within the
2911           pattern.
2912    
2913    
2914  AUTHOR  AUTHOR
2915    
# Line 2756  AUTHOR Line 2920  AUTHOR
2920    
2921  REVISION  REVISION
2922    
2923         Last updated: 08 August 2007         Last updated: 11 September 2007
2924         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
2925  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2926    
# Line 2772  PCRE REGULAR EXPRESSION DETAILS Line 2936  PCRE REGULAR EXPRESSION DETAILS
2936    
2937         The  syntax and semantics of the regular expressions that are supported         The  syntax and semantics of the regular expressions that are supported
2938         by PCRE are described in detail below. There is a quick-reference  syn-         by PCRE are described in detail below. There is a quick-reference  syn-
2939         tax  summary  in  the  pcresyntax  page. Perl's regular expressions are         tax summary in the pcresyntax page. PCRE tries to match Perl syntax and
2940         described in its own documentation, and regular expressions in  general         semantics as closely as it can. PCRE  also  supports  some  alternative
2941         are  covered in a number of books, some of which have copious examples.         regular  expression  syntax (which does not conflict with the Perl syn-
2942         Jeffrey  Friedl's  "Mastering  Regular   Expressions",   published   by         tax) in order to provide some compatibility with regular expressions in
2943         O'Reilly,  covers regular expressions in great detail. This description         Python, .NET, and Oniguruma.
2944         of PCRE's regular expressions is intended as reference material.  
2945           Perl's  regular expressions are described in its own documentation, and
2946           regular expressions in general are covered in a number of  books,  some
2947           of  which  have  copious  examples. Jeffrey Friedl's "Mastering Regular
2948           Expressions", published by  O'Reilly,  covers  regular  expressions  in
2949           great  detail.  This  description  of  PCRE's  regular  expressions  is
2950           intended as reference material.
2951    
2952         The original operation of PCRE was on strings of  one-byte  characters.         The original operation of PCRE was on strings of  one-byte  characters.
2953         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 2797  PCRE REGULAR EXPRESSION DETAILS Line 2967  PCRE REGULAR EXPRESSION DETAILS
2967         discussed in the pcrematching page.         discussed in the pcrematching page.
2968    
2969    
2970    NEWLINE CONVENTIONS
2971    
2972           PCRE  supports five different conventions for indicating line breaks in
2973           strings: a single CR (carriage return) character, a  single  LF  (line-
2974           feed) character, the two-character sequence CRLF, any of the three pre-
2975           ceding, or any Unicode newline sequence. The pcreapi page  has  further
2976           discussion  about newlines, and shows how to set the newline convention
2977           in the options arguments for the compiling and matching functions.
2978    
2979           It is also possible to specify a newline convention by starting a  pat-
2980           tern string with one of the following five sequences:
2981    
2982             (*CR)        carriage return
2983             (*LF)        linefeed
2984             (*CRLF)      carriage return, followed by linefeed
2985             (*ANYCRLF)   any of the three above
2986             (*ANY)       all Unicode newline sequences
2987    
2988           These override the default and the options given to pcre_compile(). For
2989           example, on a Unix system where LF is the default newline sequence, the
2990           pattern
2991    
2992             (*CR)a.b
2993    
2994           changes the convention to CR. That pattern matches "a\nb" because LF is
2995           no longer a newline. Note that these special settings,  which  are  not
2996           Perl-compatible,  are  recognized  only at the very start of a pattern,
2997           and that they must be in upper case.  If  more  than  one  of  them  is
2998           present, the last one is used.
2999    
3000           The  newline  convention  does  not  affect what the \R escape sequence
3001           matches. By default, this is any Unicode  newline  sequence,  for  Perl
3002           compatibility.  However, this can be changed; see the description of \R
3003           in the section entitled "Newline sequences" below. A change of \R  set-
3004           ting can be combined with a change of newline convention.
3005    
3006    
3007  CHARACTERS AND METACHARACTERS  CHARACTERS AND METACHARACTERS
3008    
3009         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 2904  BACKSLASH Line 3111  BACKSLASH
3111           \cx       "control-x", where x is any character           \cx       "control-x", where x is any character
3112           \e        escape (hex 1B)           \e        escape (hex 1B)
3113           \f        formfeed (hex 0C)           \f        formfeed (hex 0C)
3114           \n        newline (hex 0A)           \n        linefeed (hex 0A)
3115           \r        carriage return (hex 0D)           \r        carriage return (hex 0D)
3116           \t        tab (hex 09)           \t        tab (hex 09)
3117           \ddd      character with octal code ddd, or backreference           \ddd      character with octal code ddd, or backreference
# Line 2987  BACKSLASH Line 3194  BACKSLASH
3194         named back reference can be coded as \g{name}. Back references are dis-         named back reference can be coded as \g{name}. Back references are dis-
3195         cussed later, following the discussion of parenthesized subpatterns.         cussed later, following the discussion of parenthesized subpatterns.
3196    
3197       Absolute and relative subroutine calls
3198    
3199           For  compatibility with Oniguruma, the non-Perl syntax \g followed by a
3200           name or a number enclosed either in angle brackets or single quotes, is
3201           an  alternative  syntax for referencing a subpattern as a "subroutine".
3202           Details are discussed later.   Note  that  \g{...}  (Perl  syntax)  and
3203           \g<...>  (Oniguruma  syntax)  are  not synonymous. The former is a back
3204           reference; the latter is a subroutine call.
3205    
3206     Generic character types     Generic character types
3207    
3208         Another use of backslash is for specifying generic character types. The         Another use of backslash is for specifying generic character types. The
# Line 3004  BACKSLASH Line 3220  BACKSLASH
3220           \W     any "non-word" character           \W     any "non-word" character
3221    
3222         Each pair of escape sequences partitions the complete set of characters         Each pair of escape sequences partitions the complete set of characters
3223         into  two disjoint sets. Any given character matches one, and only one,         into two disjoint sets. Any given character matches one, and only  one,
3224         of each pair.         of each pair.
3225    
3226         These character type sequences can appear both inside and outside char-         These character type sequences can appear both inside and outside char-
3227         acter  classes.  They each match one character of the appropriate type.         acter classes. They each match one character of the  appropriate  type.
3228         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
3229         of them fail, since there is no character to match.         of them fail, since there is no character to match.
3230    
3231         For  compatibility  with Perl, \s does not match the VT character (code         For compatibility with Perl, \s does not match the VT  character  (code
3232         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
3233         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
3234         "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-
3235         ter. In PCRE, it never does.         ter. In PCRE, it never does.
3236    
3237         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,
3238         \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-
3239         code  character  property  support is available. These sequences retain         code character property support is available.  These  sequences  retain
3240         their original meanings from before UTF-8 support was available, mainly         their original meanings from before UTF-8 support was available, mainly
3241         for efficiency reasons.         for efficiency reasons.
3242    
3243         The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to         The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to
3244         the other sequences, these do match certain high-valued  codepoints  in         the  other  sequences, these do match certain high-valued codepoints in
3245         UTF-8 mode.  The horizontal space characters are:         UTF-8 mode.  The horizontal space characters are:
3246    
3247           U+0009     Horizontal tab           U+0009     Horizontal tab
# Line 3059  BACKSLASH Line 3275  BACKSLASH
3275           U+2029     Paragraph separator           U+2029     Paragraph separator
3276    
3277         A "word" character is an underscore or any character less than 256 that         A "word" character is an underscore or any character less than 256 that
3278         is a letter or digit. The definition of  letters  and  digits  is  con-         is  a  letter  or  digit.  The definition of letters and digits is con-
3279         trolled  by PCRE's low-valued character tables, and may vary if locale-         trolled by PCRE's low-valued character tables, and may vary if  locale-
3280         specific matching is taking place (see "Locale support" in the  pcreapi         specific  matching is taking place (see "Locale support" in the pcreapi
3281         page).  For  example,  in  a French locale such as "fr_FR" in Unix-like         page). For example, in a French locale such  as  "fr_FR"  in  Unix-like
3282         systems, or "french" in Windows, some character codes greater than  128         systems,  or "french" in Windows, some character codes greater than 128
3283         are  used for accented letters, and these are matched by \w. The use of         are used for accented letters, and these are matched by \w. The use  of
3284         locales with Unicode is discouraged.         locales with Unicode is discouraged.
3285    
3286     Newline sequences     Newline sequences
3287    
3288         Outside a character class, the escape sequence \R matches  any  Unicode         Outside  a  character class, by default, the escape sequence \R matches
3289         newline  sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \R is         any Unicode newline sequence. This is a Perl 5.10 feature. In non-UTF-8
3290         equivalent to the following:         mode \R is equivalent to the following:
3291    
3292           (?>\r\n|\n|\x0b|\f|\r|\x85)           (?>\r\n|\n|\x0b|\f|\r|\x85)
3293    
3294         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
3295         below.  This particular group matches either the two-character sequence         below.  This particular group matches either the two-character sequence
3296         CR followed by LF, or  one  of  the  single  characters  LF  (linefeed,         CR  followed  by  LF,  or  one  of  the single characters LF (linefeed,
3297         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
3298         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
3299         is treated as a single unit that cannot be split.         is treated as a single unit that cannot be split.
3300    
3301         In  UTF-8  mode, two additional characters whose codepoints are greater         In UTF-8 mode, two additional characters whose codepoints  are  greater
3302         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-
3303         rator,  U+2029).   Unicode character property support is not needed for         rator, U+2029).  Unicode character property support is not  needed  for
3304         these characters to be recognized.         these characters to be recognized.
3305    
3306           It is possible to restrict \R to match only CR, LF, or CRLF (instead of
3307           the complete set  of  Unicode  line  endings)  by  setting  the  option
3308           PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched.
3309           (BSR is an abbrevation for "backslash R".) This can be made the default
3310           when  PCRE  is  built;  if this is the case, the other behaviour can be
3311           requested via the PCRE_BSR_UNICODE option.   It  is  also  possible  to
3312           specify  these  settings  by  starting a pattern string with one of the
3313           following sequences:
3314    
3315             (*BSR_ANYCRLF)   CR, LF, or CRLF only
3316             (*BSR_UNICODE)   any Unicode newline sequence
3317    
3318           These override the default and the options given to pcre_compile(), but
3319           they can be overridden by options given to pcre_exec(). Note that these
3320           special settings, which are not Perl-compatible, are recognized only at
3321           the  very  start  of a pattern, and that they must be in upper case. If
3322           more than one of them is present, the last one is  used.  They  can  be
3323           combined  with  a  change of newline convention, for example, a pattern
3324           can start with:
3325    
3326             (*ANY)(*BSR_ANYCRLF)
3327    
3328         Inside a character class, \R matches the letter "R".         Inside a character class, \R matches the letter "R".
3329    
3330     Unicode character properties     Unicode character properties
# Line 3542  VERTICAL BAR Line 3780  VERTICAL BAR
3780  INTERNAL OPTION SETTING  INTERNAL OPTION SETTING
3781    
3782         The settings of the  PCRE_CASELESS,  PCRE_MULTILINE,  PCRE_DOTALL,  and         The settings of the  PCRE_CASELESS,  PCRE_MULTILINE,  PCRE_DOTALL,  and
3783         PCRE_EXTENDED  options  can  be  changed  from  within the pattern by a         PCRE_EXTENDED  options  (which are Perl-compatible) can be changed from
3784         sequence of Perl option letters enclosed  between  "(?"  and  ")".  The         within the pattern by  a  sequence  of  Perl  option  letters  enclosed
3785         option letters are         between "(?" and ")".  The option letters are
3786    
3787           i  for PCRE_CASELESS           i  for PCRE_CASELESS
3788           m  for PCRE_MULTILINE           m  for PCRE_MULTILINE
# Line 3558  INTERNAL OPTION SETTING Line 3796  INTERNAL OPTION SETTING
3796         is  also  permitted.  If  a  letter  appears  both before and after the         is  also  permitted.  If  a  letter  appears  both before and after the
3797         hyphen, the option is unset.         hyphen, the option is unset.
3798    
3799           The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and  PCRE_EXTRA
3800           can  be changed in the same way as the Perl-compatible options by using
3801           the characters J, U and X respectively.
3802    
3803         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-
3804         tern  parentheses),  the change applies to the remainder of the pattern         tern  parentheses),  the change applies to the remainder of the pattern
3805         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 3583  INTERNAL OPTION SETTING Line 3825  INTERNAL OPTION SETTING
3825         the effects of option settings happen at compile time. There  would  be         the effects of option settings happen at compile time. There  would  be
3826         some very weird behaviour otherwise.         some very weird behaviour otherwise.
3827    
3828         The  PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA         Note:  There  are  other  PCRE-specific  options that can be set by the
3829         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
3830         the characters J, U and X respectively.         cases  the  pattern  can  contain special leading sequences to override
3831           what the application has set or what has been  defaulted.  Details  are
3832           given in the section entitled "Newline sequences" above.
3833    
3834    
3835  SUBPATTERNS  SUBPATTERNS
# Line 3772  REPETITION Line 4016  REPETITION
4016         may be of different lengths).         may be of different lengths).
4017    
4018         The quantifier {0} is permitted, causing the expression to behave as if         The quantifier {0} is permitted, causing the expression to behave as if
4019         the previous item and the quantifier were not present.         the previous item and the quantifier were not present. This may be use-
4020           ful  for  subpatterns that are referenced as subroutines from elsewhere
4021           in the pattern. Items other than subpatterns that have a {0} quantifier
4022           are omitted from the compiled pattern.
4023    
4024         For convenience, the three most common quantifiers have  single-charac-         For  convenience, the three most common quantifiers have single-charac-
4025         ter abbreviations:         ter abbreviations:
4026    
4027           *    is equivalent to {0,}           *    is equivalent to {0,}
4028           +    is equivalent to {1,}           +    is equivalent to {1,}
4029           ?    is equivalent to {0,1}           ?    is equivalent to {0,1}
4030    
4031         It  is  possible  to construct infinite loops by following a subpattern         It is possible to construct infinite loops by  following  a  subpattern
4032         that can match no characters with a quantifier that has no upper limit,         that can match no characters with a quantifier that has no upper limit,
4033         for example:         for example:
4034    
4035           (a?)*           (a?)*
4036    
4037         Earlier versions of Perl and PCRE used to give an error at compile time         Earlier versions of Perl and PCRE used to give an error at compile time
4038         for such patterns. However, because there are cases where this  can  be         for  such  patterns. However, because there are cases where this can be
4039         useful,  such  patterns  are now accepted, but if any repetition of the         useful, such patterns are now accepted, but if any  repetition  of  the
4040         subpattern does in fact match no characters, the loop is forcibly  bro-         subpattern  does in fact match no characters, the loop is forcibly bro-
4041         ken.         ken.
4042    
4043         By  default,  the quantifiers are "greedy", that is, they match as much         By default, the quantifiers are "greedy", that is, they match  as  much
4044         as possible (up to the maximum  number  of  permitted  times),  without         as  possible  (up  to  the  maximum number of permitted times), without
4045         causing  the  rest of the pattern to fail. The classic example of where         causing the rest of the pattern to fail. The classic example  of  where
4046         this gives problems is in trying to match comments in C programs. These         this gives problems is in trying to match comments in C programs. These
4047         appear  between  /*  and  */ and within the comment, individual * and /         appear between /* and */ and within the comment,  individual  *  and  /
4048         characters may appear. An attempt to match C comments by  applying  the         characters  may  appear. An attempt to match C comments by applying the
4049         pattern         pattern
4050    
4051           /\*.*\*/           /\*.*\*/
# Line 3807  REPETITION Line 4054  REPETITION
4054    
4055           /* first comment */  not comment  /* second comment */           /* first comment */  not comment  /* second comment */
4056    
4057         fails,  because it matches the entire string owing to the greediness of         fails, because it matches the entire string owing to the greediness  of
4058         the .*  item.         the .*  item.
4059    
4060         However, if a quantifier is followed by a question mark, it  ceases  to         However,  if  a quantifier is followed by a question mark, it ceases to
4061         be greedy, and instead matches the minimum number of times possible, so         be greedy, and instead matches the minimum number of times possible, so
4062         the pattern         the pattern
4063    
4064           /\*.*?\*/           /\*.*?\*/
4065    
4066         does the right thing with the C comments. The meaning  of  the  various         does  the  right  thing with the C comments. The meaning of the various
4067         quantifiers  is  not  otherwise  changed,  just the preferred number of         quantifiers is not otherwise changed,  just  the  preferred  number  of
4068         matches.  Do not confuse this use of question mark with its  use  as  a         matches.   Do  not  confuse this use of question mark with its use as a
4069         quantifier  in its own right. Because it has two uses, it can sometimes         quantifier in its own right. Because it has two uses, it can  sometimes
4070         appear doubled, as in         appear doubled, as in
4071    
4072           \d??\d           \d??\d
# Line 3827  REPETITION Line 4074  REPETITION
4074         which matches one digit by preference, but can match two if that is the         which matches one digit by preference, but can match two if that is the
4075         only way the rest of the pattern matches.         only way the rest of the pattern matches.
4076    
4077         If  the PCRE_UNGREEDY option is set (an option that is not available in         If the PCRE_UNGREEDY option is set (an option that is not available  in
4078         Perl), the quantifiers are not greedy by default, but  individual  ones         Perl),  the  quantifiers are not greedy by default, but individual ones
4079         can  be  made  greedy  by following them with a question mark. In other         can be made greedy by following them with a  question  mark.  In  other
4080         words, it inverts the default behaviour.         words, it inverts the default behaviour.
4081    
4082         When a parenthesized subpattern is quantified  with  a  minimum  repeat         When  a  parenthesized  subpattern  is quantified with a minimum repeat
4083         count  that is greater than 1 or with a limited maximum, more memory is         count that is greater than 1 or with a limited maximum, more memory  is
4084         required for the compiled pattern, in proportion to  the  size  of  the         required  for  the  compiled  pattern, in proportion to the size of the
4085         minimum or maximum.         minimum or maximum.
4086    
4087         If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv-         If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv-
4088         alent to Perl's /s) is set, thus allowing the dot  to  match  newlines,         alent  to  Perl's  /s) is set, thus allowing the dot to match newlines,
4089         the  pattern  is  implicitly anchored, because whatever follows will be         the pattern is implicitly anchored, because whatever  follows  will  be
4090         tried against every character position in the subject string, so  there         tried  against every character position in the subject string, so there
4091         is  no  point  in  retrying the overall match at any position after the         is no point in retrying the overall match at  any  position  after  the
4092         first. PCRE normally treats such a pattern as though it  were  preceded         first.  PCRE  normally treats such a pattern as though it were preceded
4093         by \A.         by \A.
4094    
4095         In  cases  where  it  is known that the subject string contains no new-         In cases where it is known that the subject  string  contains  no  new-
4096         lines, it is worth setting PCRE_DOTALL in order to  obtain  this  opti-         lines,  it  is  worth setting PCRE_DOTALL in order to obtain this opti-
4097         mization, or alternatively using ^ to indicate anchoring explicitly.         mization, or alternatively using ^ to indicate anchoring explicitly.
4098    
4099         However,  there is one situation where the optimization cannot be used.         However, there is one situation where the optimization cannot be  used.
4100         When .*  is inside capturing parentheses that  are  the  subject  of  a         When  .*   is  inside  capturing  parentheses that are the subject of a
4101         backreference  elsewhere  in the pattern, a match at the start may fail         backreference elsewhere in the pattern, a match at the start  may  fail
4102         where a later one succeeds. Consider, for example:         where a later one succeeds. Consider, for example:
4103    
4104           (.*)abc\1           (.*)abc\1
4105    
4106         If the subject is "xyz123abc123" the match point is the fourth  charac-         If  the subject is "xyz123abc123" the match point is the fourth charac-
4107         ter. For this reason, such a pattern is not implicitly anchored.         ter. For this reason, such a pattern is not implicitly anchored.
4108    
4109         When a capturing subpattern is repeated, the value captured is the sub-         When a capturing subpattern is repeated, the value captured is the sub-
# Line 3865  REPETITION Line 4112  REPETITION
4112           (tweedle[dume]{3}\s*)+           (tweedle[dume]{3}\s*)+
4113    
4114         has matched "tweedledum tweedledee" the value of the captured substring         has matched "tweedledum tweedledee" the value of the captured substring
4115         is  "tweedledee".  However,  if there are nested capturing subpatterns,         is "tweedledee". However, if there are  nested  capturing  subpatterns,
4116         the corresponding captured values may have been set in previous  itera-         the  corresponding captured values may have been set in previous itera-
4117         tions. For example, after         tions. For example, after
4118    
4119           /(a|(b))+/           /(a|(b))+/
# Line 3876  REPETITION Line 4123  REPETITION
4123    
4124  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS
4125    
4126         With  both  maximizing ("greedy") and minimizing ("ungreedy" or "lazy")         With both maximizing ("greedy") and minimizing ("ungreedy"  or  "lazy")
4127         repetition, failure of what follows normally causes the  repeated  item         repetition,  failure  of what follows normally causes the repeated item
4128         to  be  re-evaluated to see if a different number of repeats allows the         to be re-evaluated to see if a different number of repeats  allows  the
4129         rest of the pattern to match. Sometimes it is useful to  prevent  this,         rest  of  the pattern to match. Sometimes it is useful to prevent this,
4130         either  to  change the nature of the match, or to cause it fail earlier         either to change the nature of the match, or to cause it  fail  earlier
4131         than it otherwise might, when the author of the pattern knows there  is         than  it otherwise might, when the author of the pattern knows there is
4132         no point in carrying on.         no point in carrying on.
4133    
4134         Consider,  for  example, the pattern \d+foo when applied to the subject         Consider, for example, the pattern \d+foo when applied to  the  subject
4135         line         line
4136    
4137           123456bar           123456bar
4138    
4139         After matching all 6 digits and then failing to match "foo", the normal         After matching all 6 digits and then failing to match "foo", the normal
4140         action  of  the matcher is to try again with only 5 digits matching the         action of the matcher is to try again with only 5 digits  matching  the
4141         \d+ item, and then with  4,  and  so  on,  before  ultimately  failing.         \d+  item,  and  then  with  4,  and  so on, before ultimately failing.
4142         "Atomic  grouping"  (a  term taken from Jeffrey Friedl's book) provides         "Atomic grouping" (a term taken from Jeffrey  Friedl's  book)  provides
4143         the means for specifying that once a subpattern has matched, it is  not         the  means for specifying that once a subpattern has matched, it is not
4144         to be re-evaluated in this way.         to be re-evaluated in this way.
4145    
4146         If  we  use atomic grouping for the previous example, the matcher gives         If we use atomic grouping for the previous example, the  matcher  gives
4147         up immediately on failing to match "foo" the first time.  The  notation         up  immediately  on failing to match "foo" the first time. The notation
4148         is a kind of special parenthesis, starting with (?> as in this example:         is a kind of special parenthesis, starting with (?> as in this example:
4149    
4150           (?>\d+)foo           (?>\d+)foo
4151    
4152         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-
4153         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
4154         prevented from backtracking into it. Backtracking past it  to  previous         prevented  from  backtracking into it. Backtracking past it to previous
4155         items, however, works as normal.         items, however, works as normal.
4156    
4157         An  alternative  description  is that a subpattern of this type matches         An alternative description is that a subpattern of  this  type  matches
4158         the string of characters that an  identical  standalone  pattern  would         the  string  of  characters  that an identical standalone pattern would
4159         match, if anchored at the current point in the subject string.         match, if anchored at the current point in the subject string.
4160    
4161         Atomic grouping subpatterns are not capturing subpatterns. Simple cases         Atomic grouping subpatterns are not capturing subpatterns. Simple cases
4162         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
4163         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-
4164         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
4165         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
4166         digits.         digits.
4167    
4168         Atomic groups in general can of course contain arbitrarily  complicated         Atomic  groups in general can of course contain arbitrarily complicated
4169         subpatterns,  and  can  be  nested. However, when the subpattern for an         subpatterns, and can be nested. However, when  the  subpattern  for  an
4170         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
4171         simpler  notation,  called  a "possessive quantifier" can be used. This         simpler notation, called a "possessive quantifier" can  be  used.  This
4172         consists of an additional + character  following  a  quantifier.  Using         consists  of  an  additional  + character following a quantifier. Using
4173         this notation, the previous example can be rewritten as         this notation, the previous example can be rewritten as
4174    
4175           \d++foo           \d++foo
# Line 3932  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE Line 4179  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE
4179    
4180           (abc|xyz){2,3}+           (abc|xyz){2,3}+
4181    
4182         Possessive  quantifiers  are  always  greedy;  the   setting   of   the         Possessive   quantifiers   are   always  greedy;  the  setting  of  the
4183         PCRE_UNGREEDY option is ignored. They are a convenient notation for the         PCRE_UNGREEDY option is ignored. They are a convenient notation for the
4184         simpler forms of atomic group. However, there is no difference  in  the         simpler  forms  of atomic group. However, there is no difference in the
4185         meaning  of  a  possessive  quantifier and the equivalent atomic group,         meaning of a possessive quantifier and  the  equivalent  atomic  group,
4186         though there may be a performance  difference;  possessive  quantifiers         though  there  may  be a performance difference; possessive quantifiers
4187         should be slightly faster.         should be slightly faster.
4188    
4189         The  possessive  quantifier syntax is an extension to the Perl 5.8 syn-         The possessive quantifier syntax is an extension to the Perl  5.8  syn-
4190         tax.  Jeffrey Friedl originated the idea (and the name)  in  the  first         tax.   Jeffrey  Friedl  originated the idea (and the name) in the first
4191         edition of his book. Mike McCloskey liked it, so implemented it when he         edition of his book. Mike McCloskey liked it, so implemented it when he
4192         built Sun's Java package, and PCRE copied it from there. It  ultimately         built  Sun's Java package, and PCRE copied it from there. It ultimately
4193         found its way into Perl at release 5.10.         found its way into Perl at release 5.10.
4194    
4195         PCRE has an optimization that automatically "possessifies" certain sim-         PCRE has an optimization that automatically "possessifies" certain sim-
4196         ple pattern constructs. For example, the sequence  A+B  is  treated  as         ple  pattern  constructs.  For  example, the sequence A+B is treated as
4197         A++B  because  there is no point in backtracking into a sequence of A's         A++B because there is no point in backtracking into a sequence  of  A's
4198         when B must follow.         when B must follow.
4199    
4200         When a pattern contains an unlimited repeat inside  a  subpattern  that         When  a  pattern  contains an unlimited repeat inside a subpattern that
4201         can  itself  be  repeated  an  unlimited number of times, the use of an         can itself be repeated an unlimited number of  times,  the  use  of  an
4202         atomic group is the only way to avoid some  failing  matches  taking  a         atomic  group  is  the  only way to avoid some failing matches taking a
4203         very long time indeed. The pattern         very long time indeed. The pattern
4204    
4205           (\D+|<\d+>)*[!?]           (\D+|<\d+>)*[!?]
4206    
4207         matches  an  unlimited number of substrings that either consist of non-         matches an unlimited number of substrings that either consist  of  non-
4208         digits, or digits enclosed in <>, followed by either ! or  ?.  When  it         digits,  or  digits  enclosed in <>, followed by either ! or ?. When it
4209         matches, it runs quickly. However, if it is applied to         matches, it runs quickly. However, if it is applied to
4210    
4211           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
4212    
4213         it  takes  a  long  time  before reporting failure. This is because the         it takes a long time before reporting  failure.  This  is  because  the
4214         string can be divided between the internal \D+ repeat and the  external         string  can be divided between the internal \D+ repeat and the external
4215         *  repeat  in  a  large  number of ways, and all have to be tried. (The         * repeat in a large number of ways, and all  have  to  be  tried.  (The
4216         example uses [!?] rather than a single character at  the  end,  because         example  uses  [!?]  rather than a single character at the end, because
4217         both  PCRE  and  Perl have an optimization that allows for fast failure         both PCRE and Perl have an optimization that allows  for  fast  failure
4218         when a single character is used. They remember the last single  charac-         when  a single character is used. They remember the last single charac-
4219         ter  that  is required for a match, and fail early if it is not present         ter that is required for a match, and fail early if it is  not  present
4220         in the string.) If the pattern is changed so that  it  uses  an  atomic         in  the  string.)  If  the pattern is changed so that it uses an atomic
4221         group, like this:         group, like this:
4222    
4223           ((?>\D+)|<\d+>)*[!?]           ((?>\D+)|<\d+>)*[!?]
4224    
4225         sequences  of non-digits cannot be broken, and failure happens quickly.         sequences of non-digits cannot be broken, and failure happens  quickly.
4226    
4227    
4228  BACK REFERENCES  BACK REFERENCES
4229    
4230         Outside a character class, a backslash followed by a digit greater than         Outside a character class, a backslash followed by a digit greater than
4231         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-
4232         pattern earlier (that is, to its left) in the pattern,  provided  there         pattern  earlier  (that is, to its left) in the pattern, provided there
4233         have been that many previous capturing left parentheses.         have been that many previous capturing left parentheses.
4234    
4235         However, if the decimal number following the backslash is less than 10,         However, if the decimal number following the backslash is less than 10,
4236         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
4237         there  are  not that many capturing left parentheses in the entire pat-         there are not that many capturing left parentheses in the  entire  pat-
4238         tern. In other words, the parentheses that are referenced need  not  be         tern.  In  other words, the parentheses that are referenced need not be
4239         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
4240         reference" of this type can make sense when a  repetition  is  involved         reference"  of  this  type can make sense when a repetition is involved
4241         and  the  subpattern to the right has participated in an earlier itera-         and the subpattern to the right has participated in an  earlier  itera-
4242         tion.         tion.
4243    
4244         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
4245         subpattern  whose  number  is  10  or  more using this syntax because a         subpattern whose number is 10 or  more  using  this  syntax  because  a
4246         sequence such as \50 is interpreted as a character  defined  in  octal.         sequence  such  as  \50 is interpreted as a character defined in octal.
4247         See the subsection entitled "Non-printing characters" above for further         See the subsection entitled "Non-printing characters" above for further
4248         details of the handling of digits following a backslash.  There  is  no         details  of  the  handling of digits following a backslash. There is no
4249         such  problem  when named parentheses are used. A back reference to any         such problem when named parentheses are used. A back reference  to  any
4250         subpattern is possible using named parentheses (see below).         subpattern is possible using named parentheses (see below).
4251    
4252         Another way of avoiding the ambiguity inherent in  the  use  of  digits         Another  way  of  avoiding  the ambiguity inherent in the use of digits
4253         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-
4254         ture introduced in Perl 5.10.  This  escape  must  be  followed  by  an         ture  introduced  in  Perl  5.10.  This  escape  must be followed by an
4255         unsigned  number  or  a negative number, optionally enclosed in braces.         unsigned number or a negative number, optionally  enclosed  in  braces.
4256         These examples are all identical:         These examples are all identical:
4257    
4258           (ring), \1           (ring), \1
4259           (ring), \g1           (ring), \g1
4260           (ring), \g{1}           (ring), \g{1}
4261    
4262         An unsigned number specifies an absolute reference without the  ambigu-         An  unsigned number specifies an absolute reference without the ambigu-
4263         ity that is present in the older syntax. It is also useful when literal         ity that is present in the older syntax. It is also useful when literal
4264         digits follow the reference. A negative number is a relative reference.         digits follow the reference. A negative number is a relative reference.
4265         Consider this example:         Consider this example:
# Line 4020  BACK REFERENCES Line 4267  BACK REFERENCES
4267           (abc(def)ghi)\g{-1}           (abc(def)ghi)\g{-1}
4268    
4269         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-
4270         ing subpattern before \g, that is, is it equivalent to  \2.  Similarly,         ing  subpattern  before \g, that is, is it equivalent to \2. Similarly,
4271         \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
4272         helpful in long patterns, and also in  patterns  that  are  created  by         helpful  in  long  patterns,  and  also in patterns that are created by
4273         joining together fragments that contain references within themselves.         joining together fragments that contain references within themselves.
4274    
4275         A  back  reference matches whatever actually matched the capturing sub-         A back reference matches whatever actually matched the  capturing  sub-
4276         pattern in the current subject string, rather  than  anything  matching         pattern  in  the  current subject string, rather than anything matching
4277         the subpattern itself (see "Subpatterns as subroutines" below for a way         the subpattern itself (see "Subpatterns as subroutines" below for a way
4278         of doing that). So the pattern         of doing that). So the pattern
4279    
4280           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
4281    
4282         matches "sense and sensibility" and "response and responsibility",  but         matches  "sense and sensibility" and "response and responsibility", but
4283         not  "sense and responsibility". If caseful matching is in force at the         not "sense and responsibility". If caseful matching is in force at  the
4284         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-
4285         ple,         ple,
4286    
4287           ((?i)rah)\s+\1           ((?i)rah)\s+\1
4288    
4289         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
4290         original capturing subpattern is matched caselessly.         original capturing subpattern is matched caselessly.
4291    
4292         There are several different ways of writing back  references  to  named         There  are  several  different ways of writing back references to named
4293         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
4294         \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
4295         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
4296         and named references, is also supported. We  could  rewrite  the  above         and  named  references,  is  also supported. We could rewrite the above
4297         example in any of the following ways:         example in any of the following ways:
4298    
4299           (?<p1>(?i)rah)\s+\k<p1>           (?<p1>(?i)rah)\s+\k<p1>
# Line 4054  BACK REFERENCES Line 4301  BACK REFERENCES
4301           (?P<p1>(?i)rah)\s+(?P=p1)           (?P<p1>(?i)rah)\s+(?P=p1)
4302           (?<p1>(?i)rah)\s+\g{p1}           (?<p1>(?i)rah)\s+\g{p1}
4303    
4304         A  subpattern  that  is  referenced  by  name may appear in the pattern         A subpattern that is referenced by  name  may  appear  in  the  pattern
4305         before or after the reference.         before or after the reference.
4306    
4307         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
4308         subpattern  has  not actually been used in a particular match, any back         subpattern has not actually been used in a particular match,  any  back
4309         references to it always fail. For example, the pattern         references to it always fail. For example, the pattern
4310    
4311           (a|(bc))\2           (a|(bc))\2
4312    
4313         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
4314         may  be  many  capturing parentheses in a pattern, all digits following         may be many capturing parentheses in a pattern,  all  digits  following
4315         the backslash are taken as part of a potential back  reference  number.         the  backslash  are taken as part of a potential back reference number.
4316         If the pattern continues with a digit character, some delimiter must be         If the pattern continues with a digit character, some delimiter must be
4317         used to terminate the back reference. If the  PCRE_EXTENDED  option  is         used  to  terminate  the back reference. If the PCRE_EXTENDED option is
4318         set,  this  can  be  whitespace.  Otherwise an empty comment (see "Com-         set, this can be whitespace.  Otherwise an  empty  comment  (see  "Com-
4319         ments" below) can be used.         ments" below) can be used.
4320    
4321         A back reference that occurs inside the parentheses to which it  refers         A  back reference that occurs inside the parentheses to which it refers
4322         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
4323         matches.  However, such references can be useful inside  repeated  sub-         matches.   However,  such references can be useful inside repeated sub-
4324         patterns. For example, the pattern         patterns. For example, the pattern
4325    
4326           (a|b\1)+           (a|b\1)+
4327    
4328         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-
4329         ation of the subpattern,  the  back  reference  matches  the  character         ation  of  the  subpattern,  the  back  reference matches the character
4330         string  corresponding  to  the previous iteration. In order for this to         string corresponding to the previous iteration. In order  for  this  to
4331         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
4332         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
4333         the example above, or by a quantifier with a minimum of zero.         the example above, or by a quantifier with a minimum of zero.
4334    
4335    
4336  ASSERTIONS  ASSERTIONS
4337    
4338         An assertion is a test on the characters  following  or  preceding  the         An  assertion  is  a  test on the characters following or preceding the
4339         current  matching  point that does not actually consume any characters.         current matching point that does not actually consume  any  characters.
4340         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
4341         described above.         described above.
4342    
4343         More  complicated  assertions  are  coded as subpatterns. There are two         More complicated assertions are coded as  subpatterns.  There  are  two
4344         kinds: those that look ahead of the current  position  in  the  subject         kinds:  those  that  look  ahead of the current position in the subject
4345         string,  and  those  that  look  behind  it. An assertion subpattern is         string, and those that look  behind  it.  An  assertion  subpattern  is
4346         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
4347         matching position to be changed.         matching position to be changed.
4348    
4349         Assertion  subpatterns  are  not  capturing subpatterns, and may not be         Assertion subpatterns are not capturing subpatterns,  and  may  not  be
4350         repeated, because it makes no sense to assert the  same  thing  several         repeated,  because  it  makes no sense to assert the same thing several
4351         times.  If  any kind of assertion contains capturing subpatterns within         times. If any kind of assertion contains capturing  subpatterns  within
4352         it, these are counted for the purposes of numbering the capturing  sub-         it,  these are counted for the purposes of numbering the capturing sub-
4353         patterns in the whole pattern.  However, substring capturing is carried         patterns in the whole pattern.  However, substring capturing is carried
4354         out only for positive assertions, because it does not  make  sense  for         out  only  for  positive assertions, because it does not make sense for
4355         negative assertions.         negative assertions.
4356    
4357     Lookahead assertions     Lookahead assertions
# Line 4114  ASSERTIONS Line 4361  ASSERTIONS
4361    
4362           \w+(?=;)           \w+(?=;)
4363    
4364         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-
4365         colon in the match, and         colon in the match, and
4366    
4367           foo(?!bar)           foo(?!bar)
4368    
4369         matches  any  occurrence  of  "foo" that is not followed by "bar". Note         matches any occurrence of "foo" that is not  followed  by  "bar".  Note
4370         that the apparently similar pattern         that the apparently similar pattern
4371    
4372           (?!foo)bar           (?!foo)bar
4373    
4374         does not find an occurrence of "bar"  that  is  preceded  by  something         does  not  find  an  occurrence  of "bar" that is preceded by something
4375         other  than "foo"; it finds any occurrence of "bar" whatsoever, because         other than "foo"; it finds any occurrence of "bar" whatsoever,  because
4376         the assertion (?!foo) is always true when the next three characters are         the assertion (?!foo) is always true when the next three characters are
4377         "bar". A lookbehind assertion is needed to achieve the other effect.         "bar". A lookbehind assertion is needed to achieve the other effect.
4378    
4379         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
4380         most convenient way to do it is  with  (?!)  because  an  empty  string         most  convenient  way  to  do  it  is with (?!) because an empty string
4381         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
4382         string must always fail.         string must always fail.
4383    
4384     Lookbehind assertions     Lookbehind assertions
4385    
4386         Lookbehind assertions start with (?<= for positive assertions and  (?<!         Lookbehind  assertions start with (?<= for positive assertions and (?<!
4387         for negative assertions. For example,         for negative assertions. For example,
4388    
4389           (?<!foo)bar           (?<!foo)bar
4390    
4391         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
4392         contents of a lookbehind assertion are restricted  such  that  all  the         contents  of  a  lookbehind  assertion are restricted such that all the
4393         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-
4394         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
4395         fixed length. Thus         fixed length. Thus
4396    
4397           (?<=bullock|donkey)           (?<=bullock|donkey)
# Line 4153  ASSERTIONS Line 4400  ASSERTIONS
4400    
4401           (?<!dogs?|cats?)           (?<!dogs?|cats?)
4402    
4403         causes  an  error at compile time. Branches that match different length         causes an error at compile time. Branches that match  different  length
4404         strings are permitted only at the top level of a lookbehind  assertion.         strings  are permitted only at the top level of a lookbehind assertion.
4405         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
4406         requires all branches to match the same length of string. An  assertion         requires  all branches to match the same length of string. An assertion
4407         such as         such as
4408    
4409           (?<=ab(c|de))           (?<=ab(c|de))
4410    
4411         is  not  permitted,  because  its single top-level branch can match two         is not permitted, because its single top-level  branch  can  match  two
4412         different lengths, but it is acceptable if rewritten to  use  two  top-         different  lengths,  but  it is acceptable if rewritten to use two top-
4413         level branches:         level branches:
4414    
4415           (?<=abc|abde)           (?<=abc|abde)
4416    
4417         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
4418         instead of a lookbehind assertion; this is not restricted to  a  fixed-         instead  of  a lookbehind assertion; this is not restricted to a fixed-
4419         length.         length.
4420    
4421         The  implementation  of lookbehind assertions is, for each alternative,         The implementation of lookbehind assertions is, for  each  alternative,
4422         to temporarily move the current position back by the fixed  length  and         to  temporarily  move the current position back by the fixed length and
4423         then try to match. If there are insufficient characters before the cur-         then try to match. If there are insufficient characters before the cur-
4424         rent position, the assertion fails.         rent position, the assertion fails.
4425    
4426         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
4427         mode)  to appear in lookbehind assertions, because it makes it impossi-         mode) to appear in lookbehind assertions, because it makes it  impossi-
4428         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,
4429         which can match different numbers of bytes, are also not permitted.         which can match different numbers of bytes, are also not permitted.
4430    
4431         Possessive  quantifiers  can  be  used  in  conjunction with lookbehind         Possessive quantifiers can  be  used  in  conjunction  with  lookbehind
4432         assertions to specify efficient matching at  the  end  of  the  subject         assertions  to  specify  efficient  matching  at the end of the subject
4433         string. Consider a simple pattern such as         string. Consider a simple pattern such as
4434    
4435           abcd$           abcd$
4436    
4437         when  applied  to  a  long string that does not match. Because matching         when applied to a long string that does  not  match.  Because  matching
4438         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
4439         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
4440         pattern is specified as         pattern is specified as
4441    
4442           ^.*abcd$           ^.*abcd$
4443    
4444         the initial .* matches the entire string at first, but when this  fails         the  initial .* matches the entire string at first, but when this fails
4445         (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
4446         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
4447         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,
4448         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
4449    
4450           ^.*+(?<=abcd)           ^.*+(?<=abcd)
4451    
4452         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
4453         entire  string.  The subsequent lookbehind assertion does a single test         entire string. The subsequent lookbehind assertion does a  single  test
4454         on the last four characters. If it fails, the match fails  immediately.         on  the last four characters. If it fails, the match fails immediately.
4455         For  long  strings, this approach makes a significant difference to the         For long strings, this approach makes a significant difference  to  the
4456         processing time.         processing time.
4457    
4458     Using multiple assertions     Using multiple assertions
# Line 4214  ASSERTIONS Line 4461  ASSERTIONS
4461    
4462           (?<=\d{3})(?<!999)foo           (?<=\d{3})(?<!999)foo
4463    
4464         matches "foo" preceded by three digits that are not "999". Notice  that         matches  "foo" preceded by three digits that are not "999". Notice that
4465         each  of  the  assertions is applied independently at the same point in         each of the assertions is applied independently at the  same  point  in
4466         the subject string. First there is a  check  that  the  previous  three         the  subject  string.  First  there  is a check that the previous three
4467         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
4468         three characters are not "999".  This pattern does not match "foo" pre-         three characters are not "999".  This pattern does not match "foo" pre-
4469         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
4470         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-
4471         foo". A pattern to do that is         foo". A pattern to do that is
4472    
4473           (?<=\d{3}...)(?<!999)foo           (?<=\d{3}...)(?<!999)foo
4474    
4475         This  time  the  first assertion looks at the preceding six characters,         This time the first assertion looks at the  preceding  six  characters,
4476         checking that the first three are digits, and then the second assertion         checking that the first three are digits, and then the second assertion
4477         checks that the preceding three characters are not "999".         checks that the preceding three characters are not "999".
4478    
# Line 4233  ASSERTIONS Line 4480  ASSERTIONS
4480    
4481           (?<=(?<!foo)bar)baz           (?<=(?<!foo)bar)baz
4482    
4483         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
4484         is not preceded by "foo", while         is not preceded by "foo", while
4485    
4486           (?<=\d{3}(?!999)...)foo           (?<=\d{3}(?!999)...)foo
4487    
4488         is another pattern that matches "foo" preceded by three digits and  any         is  another pattern that matches "foo" preceded by three digits and any
4489         three characters that are not "999".         three characters that are not "999".
4490    
4491    
4492  CONDITIONAL SUBPATTERNS  CONDITIONAL SUBPATTERNS
4493    
4494         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-
4495         ditionally or to choose between two alternative subpatterns,  depending         ditionally  or to choose between two alternative subpatterns, depending
4496         on  the result of an assertion, or whether a previous capturing subpat-         on the result of an assertion, or whether a previous capturing  subpat-
4497         tern matched or not. The two possible forms of  conditional  subpattern         tern  matched  or not. The two possible forms of conditional subpattern
4498         are         are
4499    
4500           (?(condition)yes-pattern)           (?(condition)yes-pattern)
4501           (?(condition)yes-pattern|no-pattern)           (?(condition)yes-pattern|no-pattern)
4502    
4503         If  the  condition is satisfied, the yes-pattern is used; otherwise the         If the condition is satisfied, the yes-pattern is used;  otherwise  the
4504         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-
4505         tives in the subpattern, a compile-time error occurs.         tives in the subpattern, a compile-time error occurs.
4506    
4507         There  are  four  kinds of condition: references to subpatterns, refer-         There are four kinds of condition: references  to  subpatterns,  refer-
4508         ences to recursion, a pseudo-condition called DEFINE, and assertions.         ences to recursion, a pseudo-condition called DEFINE, and assertions.
4509    
4510     Checking for a used subpattern by number     Checking for a used subpattern by number
4511    
4512         If the text between the parentheses consists of a sequence  of  digits,         If  the  text between the parentheses consists of a sequence of digits,
4513         the  condition  is  true if the capturing subpattern of that number has         the condition is true if the capturing subpattern of  that  number  has
4514         previously matched. An alternative notation is to  precede  the  digits         previously  matched.  An  alternative notation is to precede the digits
4515         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-
4516         tive rather than absolute.  The most recently opened parentheses can be         tive rather than absolute.  The most recently opened parentheses can be
4517         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
4518         looping constructs it can also make sense to refer to subsequent groups         looping constructs it can also make sense to refer to subsequent groups
4519         with constructs such as (?(+2).         with constructs such as (?(+2).
4520    
4521         Consider  the  following  pattern, which contains non-significant white         Consider the following pattern, which  contains  non-significant  white
4522         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
4523         divide it into three parts for ease of discussion:         divide it into three parts for ease of discussion:
4524    
4525           ( \( )?    [^()]+    (?(1) \) )           ( \( )?    [^()]+    (?(1) \) )
4526    
4527         The  first  part  matches  an optional opening parenthesis, and if that         The first part matches an optional opening  parenthesis,  and  if  that
4528         character is present, sets it as the first captured substring. The sec-         character is present, sets it as the first captured substring. The sec-
4529         ond  part  matches one or more characters that are not parentheses. The         ond part matches one or more characters that are not  parentheses.  The
4530         third part is a conditional subpattern that tests whether the first set         third part is a conditional subpattern that tests whether the first set
4531         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
4532         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-
4533         tern  is  executed  and  a  closing parenthesis is required. Otherwise,         tern is executed and a  closing  parenthesis  is  required.  Otherwise,
4534         since no-pattern is not present, the  subpattern  matches  nothing.  In         since  no-pattern  is  not  present, the subpattern matches nothing. In
4535         other  words,  this  pattern  matches  a  sequence  of non-parentheses,         other words,  this  pattern  matches  a  sequence  of  non-parentheses,
4536         optionally enclosed in parentheses.         optionally enclosed in parentheses.
4537    
4538         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
4539         relative reference:         relative reference:
4540    
4541           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...
4542    
4543         This  makes  the  fragment independent of the parentheses in the larger         This makes the fragment independent of the parentheses  in  the  larger
4544         pattern.         pattern.
4545    
4546     Checking for a used subpattern by name     Checking for a used subpattern by name
4547    
4548         Perl uses the syntax (?(<name>)...) or (?('name')...)  to  test  for  a         Perl  uses  the  syntax  (?(<name>)...) or (?('name')...) to test for a
4549         used  subpattern  by  name.  For compatibility with earlier versions of         used subpattern by name. For compatibility  with  earlier  versions  of
4550         PCRE, which had this facility before Perl, the syntax  (?(name)...)  is         PCRE,  which  had this facility before Perl, the syntax (?(name)...) is
4551         also  recognized. However, there is a possible ambiguity with this syn-         also recognized. However, there is a possible ambiguity with this  syn-
4552         tax, because subpattern names may  consist  entirely  of  digits.  PCRE         tax,  because  subpattern  names  may  consist entirely of digits. PCRE
4553         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
4554         consists entirely of digits, PCRE looks for a subpattern of  that  num-         consists  entirely  of digits, PCRE looks for a subpattern of that num-
4555         ber,  which must be greater than zero. Using subpattern names that con-         ber, which must be greater than zero. Using subpattern names that  con-
4556         sist entirely of digits is not recommended.         sist entirely of digits is not recommended.
4557    
4558         Rewriting the above example to use a named subpattern gives this:         Rewriting the above example to use a named subpattern gives this:
# Line 4316  CONDITIONAL SUBPATTERNS Line 4563  CONDITIONAL SUBPATTERNS
4563     Checking for pattern recursion     Checking for pattern recursion
4564    
4565         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
4566         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
4567         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-
4568         sand follow the letter R, for example:         sand follow the letter R, for example:
4569    
4570           (?(R3)...) or (?(R&name)...)           (?(R3)...) or (?(R&name)...)
4571    
4572         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-
4573         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
4574         entire recursion stack.         entire recursion stack.
4575    
4576         At  "top  level", all these recursion test conditions are false. Recur-         At "top level", all these recursion test conditions are  false.  Recur-
4577         sive patterns are described below.         sive patterns are described below.
4578    
4579     Defining subpatterns for use by reference only     Defining subpatterns for use by reference only
4580    
4581         If the condition is the string (DEFINE), and  there  is  no  subpattern         If  the  condition  is  the string (DEFINE), and there is no subpattern
4582         with  the  name  DEFINE,  the  condition is always false. In this case,         with the name DEFINE, the condition is  always  false.  In  this  case,
4583         there may be only one alternative  in  the  subpattern.  It  is  always         there  may  be  only  one  alternative  in the subpattern. It is always
4584         skipped  if  control  reaches  this  point  in the pattern; the idea of         skipped if control reaches this point  in  the  pattern;  the  idea  of
4585         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-
4586         erenced  from elsewhere. (The use of "subroutines" is described below.)         erenced from elsewhere. (The use of "subroutines" is described  below.)
4587         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
4588         this (ignore whitespace and line breaks):         this (ignore whitespace and line breaks):
4589    
4590           (?(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) )
4591           \b (?&byte) (\.(?&byte)){3} \b           \b (?&byte) (\.(?&byte)){3} \b
4592    
4593         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
4594         group named "byte" is defined. This matches an individual component  of         group  named "byte" is defined. This matches an individual component of
4595         an  IPv4  address  (a number less than 256). When matching takes place,         an IPv4 address (a number less than 256). When  matching  takes  place,
4596         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
4597         condition.         condition.
4598    
4599         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
4600         four dot-separated components of an IPv4 address, insisting on  a  word         four  dot-separated  components of an IPv4 address, insisting on a word
4601         boundary at each end.         boundary at each end.
4602    
4603     Assertion conditions     Assertion conditions
4604    
4605         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
4606         assertion.  This may be a positive or negative lookahead or  lookbehind         assertion.   This may be a positive or negative lookahead or lookbehind
4607         assertion.  Consider  this  pattern,  again  containing non-significant         assertion. Consider  this  pattern,  again  containing  non-significant
4608         white space, and with the two alternatives on the second line:         white space, and with the two alternatives on the second line:
4609    
4610           (?(?=[^a-z]*[a-z])           (?(?=[^a-z]*[a-z])
4611           \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} )
4612    
4613         The condition  is  a  positive  lookahead  assertion  that  matches  an         The  condition  is  a  positive  lookahead  assertion  that  matches an
4614         optional  sequence of non-letters followed by a letter. In other words,         optional sequence of non-letters followed by a letter. In other  words,
4615         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
4616         letter  is found, the subject is matched against the first alternative;         letter is found, the subject is matched against the first  alternative;
4617         otherwise it is  matched  against  the  second.  This  pattern  matches         otherwise  it  is  matched  against  the  second.  This pattern matches
4618         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
4619         letters and dd are digits.         letters and dd are digits.
4620    
4621    
4622  COMMENTS  COMMENTS
4623    
4624         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
4625         next  closing  parenthesis.  Nested  parentheses are not permitted. The         next closing parenthesis. Nested parentheses  are  not  permitted.  The
4626         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
4627         at all.         at all.
4628    
4629         If  the PCRE_EXTENDED option is set, an unescaped # character outside a         If the PCRE_EXTENDED option is set, an unescaped # character outside  a
4630         character class introduces a  comment  that  continues  to  immediately         character  class  introduces  a  comment  that continues to immediately
4631         after the next newline in the pattern.         after the next newline in the pattern.
4632    
4633    
4634  RECURSIVE PATTERNS  RECURSIVE PATTERNS
4635    
4636         Consider  the problem of matching a string in parentheses, allowing for         Consider the problem of matching a string in parentheses, allowing  for
4637         unlimited nested parentheses. Without the use of  recursion,  the  best         unlimited  nested  parentheses.  Without the use of recursion, the best
4638         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
4639         depth of nesting. It is not possible to  handle  an  arbitrary  nesting         depth  of  nesting.  It  is not possible to handle an arbitrary nesting
4640         depth.         depth.
4641    
4642         For some time, Perl has provided a facility that allows regular expres-         For some time, Perl has provided a facility that allows regular expres-
4643         sions to recurse (amongst other things). It does this by  interpolating         sions  to recurse (amongst other things). It does this by interpolating
4644         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
4645         expression itself. A Perl pattern using code interpolation to solve the         expression itself. A Perl pattern using code interpolation to solve the
4646         parentheses problem can be created like this:         parentheses problem can be created like this:
4647    
# Line 4404  RECURSIVE PATTERNS Line 4651  RECURSIVE PATTERNS
4651         refers recursively to the pattern in which it appears.         refers recursively to the pattern in which it appears.
4652    
4653         Obviously, PCRE cannot support the interpolation of Perl code. Instead,         Obviously, PCRE cannot support the interpolation of Perl code. Instead,
4654         it  supports  special  syntax  for recursion of the entire pattern, and         it supports special syntax for recursion of  the  entire  pattern,  and
4655         also for individual subpattern recursion.  After  its  introduction  in         also  for  individual  subpattern  recursion. After its introduction in
4656         PCRE  and  Python,  this  kind of recursion was introduced into Perl at         PCRE and Python, this kind of recursion was  introduced  into  Perl  at
4657         release 5.10.         release 5.10.
4658    
4659         A special item that consists of (? followed by a  number  greater  than         A  special  item  that consists of (? followed by a number greater than
4660         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
4661         the given number, provided that it occurs inside that  subpattern.  (If         the  given  number, provided that it occurs inside that subpattern. (If
4662         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-
4663         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
4664         regular expression.         regular expression.
4665    
4666         In  PCRE (like Python, but unlike Perl), a recursive subpattern call is         In PCRE (like Python, but unlike Perl), a recursive subpattern call  is
4667         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
4668         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
4669         alternatives and there is a subsequent matching failure.         alternatives and there is a subsequent matching failure.
4670    
4671         This PCRE pattern solves the nested  parentheses  problem  (assume  the         This  PCRE  pattern  solves  the nested parentheses problem (assume the
4672         PCRE_EXTENDED option is set so that white space is ignored):         PCRE_EXTENDED option is set so that white space is ignored):
4673    
4674           \( ( (?>[^()]+) | (?R) )* \)           \( ( (?>[^()]+) | (?R) )* \)
4675    
4676         First  it matches an opening parenthesis. Then it matches any number of         First it matches an opening parenthesis. Then it matches any number  of
4677         substrings which can either be a  sequence  of  non-parentheses,  or  a         substrings  which  can  either  be  a sequence of non-parentheses, or a
4678         recursive  match  of the pattern itself (that is, a correctly parenthe-         recursive match of the pattern itself (that is, a  correctly  parenthe-
4679         sized substring).  Finally there is a closing parenthesis.         sized substring).  Finally there is a closing parenthesis.
4680    
4681         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
4682         the entire pattern, so instead you could use this:         the entire pattern, so instead you could use this:
4683    
4684           ( \( ( (?>[^()]+) | (?1) )* \) )           ( \( ( (?>[^()]+) | (?1) )* \) )
4685    
4686         We  have  put the pattern into parentheses, and caused the recursion to         We have put the pattern into parentheses, and caused the  recursion  to
4687         refer to them instead of the whole pattern.         refer to them instead of the whole pattern.
4688    
4689         In a larger pattern,  keeping  track  of  parenthesis  numbers  can  be         In  a  larger  pattern,  keeping  track  of  parenthesis numbers can be
4690         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
4691         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
4692         (?-2) to refer to the second most recently opened parentheses preceding         (?-2) to refer to the second most recently opened parentheses preceding
4693         the recursion. In other  words,  a  negative  number  counts  capturing         the  recursion.  In  other  words,  a  negative number counts capturing
4694         parentheses leftwards from the point at which it is encountered.         parentheses leftwards from the point at which it is encountered.
4695    
4696         It  is  also  possible  to refer to subsequently opened parentheses, by         It is also possible to refer to  subsequently  opened  parentheses,  by
4697         writing references such as (?+2). However, these  cannot  be  recursive         writing  references  such  as (?+2). However, these cannot be recursive
4698         because  the  reference  is  not inside the parentheses that are refer-         because the reference is not inside the  parentheses  that  are  refer-
4699         enced. They are always "subroutine" calls, as  described  in  the  next         enced.  They  are  always  "subroutine" calls, as described in the next
4700         section.         section.
4701    
4702         An  alternative  approach is to use named parentheses instead. The Perl         An alternative approach is to use named parentheses instead.  The  Perl
4703         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
4704         supported. We could rewrite the above example as follows:         supported. We could rewrite the above example as follows:
4705    
4706           (?<pn> \( ( (?>[^()]+) | (?&pn) )* \) )           (?<pn> \( ( (?>[^()]+) | (?&pn) )* \) )
4707    
4708         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
4709         one is used.         one is used.
4710    
4711         This particular example pattern that we have been looking  at  contains         This  particular  example pattern that we have been looking at contains
4712         nested  unlimited repeats, and so the use of atomic grouping for match-         nested unlimited repeats, and so the use of atomic grouping for  match-
4713         ing strings of non-parentheses is important when applying  the  pattern         ing  strings  of non-parentheses is important when applying the pattern
4714         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
4715         to         to
4716    
4717           (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()           (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
4718    
4719         it yields "no match" quickly. However, if atomic grouping is not  used,         it  yields "no match" quickly. However, if atomic grouping is not used,
4720         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
4721         different ways the + and * repeats can carve up the  subject,  and  all         different  ways  the  + and * repeats can carve up the subject, and all
4722         have to be tested before failure can be reported.         have to be tested before failure can be reported.
4723    
4724         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
4725         those from the outermost level of the recursion at which the subpattern         those from the outermost level of the recursion at which the subpattern
4726         value  is  set.   If  you want to obtain intermediate values, a callout         value is set.  If you want to obtain  intermediate  values,  a  callout
4727         function can be used (see below and the pcrecallout documentation).  If         function  can be used (see below and the pcrecallout documentation). If
4728         the pattern above is matched against         the pattern above is matched against
4729    
4730           (ab(cd)ef)           (ab(cd)ef)
4731    
4732         the  value  for  the  capturing  parentheses is "ef", which is the last         the value for the capturing parentheses is  "ef",  which  is  the  last
4733         value taken on at the top level. If additional parentheses  are  added,         value  taken  on at the top level. If additional parentheses are added,
4734         giving         giving
4735    
4736           \( ( ( (?>[^()]+) | (?R) )* ) \)           \( ( ( (?>[^()]+) | (?R) )* ) \)
4737              ^                        ^              ^                        ^
4738              ^                        ^              ^                        ^
4739    
4740         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
4741         parentheses. If there are more than 15 capturing parentheses in a  pat-         parentheses.  If there are more than 15 capturing parentheses in a pat-
4742         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,
4743         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-
4744         wards.  If  no  memory  can  be  obtained,  the  match  fails  with the         wards. If  no  memory  can  be  obtained,  the  match  fails  with  the
4745         PCRE_ERROR_NOMEMORY error.         PCRE_ERROR_NOMEMORY error.
4746    
4747         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
4748         recursion.   Consider  this pattern, which matches text in angle brack-         recursion.  Consider this pattern, which matches text in  angle  brack-
4749         ets, allowing for arbitrary nesting. Only digits are allowed in  nested         ets,  allowing for arbitrary nesting. Only digits are allowed in nested
4750         brackets  (that is, when recursing), whereas any characters are permit-         brackets (that is, when recursing), whereas any characters are  permit-
4751         ted at the outer level.         ted at the outer level.
4752    
4753           < (?: (?(R) \d++  | [^<>]*+) | (?R)) * >           < (?: (?(R) \d++  | [^<>]*+) | (?R)) * >
4754    
4755         In this pattern, (?(R) is the start of a conditional  subpattern,  with         In  this  pattern, (?(R) is the start of a conditional subpattern, with
4756         two  different  alternatives for the recursive and non-recursive cases.         two different alternatives for the recursive and  non-recursive  cases.
4757         The (?R) item is the actual recursive call.         The (?R) item is the actual recursive call.
4758    
4759    
4760  SUBPATTERNS AS SUBROUTINES  SUBPATTERNS AS SUBROUTINES
4761    
4762         If the syntax for a recursive subpattern reference (either by number or         If the syntax for a recursive subpattern reference (either by number or
4763         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-
4764         ates like a subroutine in a programming language. The "called"  subpat-         ates  like a subroutine in a programming language. The "called" subpat-
4765         tern may be defined before or after the reference. A numbered reference         tern may be defined before or after the reference. A numbered reference
4766         can be absolute or relative, as in these examples:         can be absolute or relative, as in these examples:
4767    
# Line 4526  SUBPATTERNS AS SUBROUTINES Line 4773  SUBPATTERNS AS SUBROUTINES
4773    
4774           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
4775    
4776         matches "sense and sensibility" and "response and responsibility",  but         matches  "sense and sensibility" and "response and responsibility", but
4777         not "sense and responsibility". If instead the pattern         not "sense and responsibility". If instead the pattern
4778    
4779           (sens|respons)e and (?1)ibility           (sens|respons)e and (?1)ibility
4780    
4781         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
4782         two strings. Another example is  given  in  the  discussion  of  DEFINE         two  strings.  Another  example  is  given  in the discussion of DEFINE
4783         above.         above.
4784    
4785         Like recursive subpatterns, a "subroutine" call is always treated as an         Like recursive subpatterns, a "subroutine" call is always treated as an
4786         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,
4787         it  is  never  re-entered, even if it contains untried alternatives and         it is never re-entered, even if it contains  untried  alternatives  and
4788         there is a subsequent matching failure.         there is a subsequent matching failure.
4789    
4790         When a subpattern is used as a subroutine, processing options  such  as         When  a  subpattern is used as a subroutine, processing options such as
4791         case-independence are fixed when the subpattern is defined. They cannot         case-independence are fixed when the subpattern is defined. They cannot
4792         be changed for different calls. For example, consider this pattern:         be changed for different calls. For example, consider this pattern:
4793    
4794           (abc)(?i:(?-1))           (abc)(?i:(?-1))
4795    
4796         It matches "abcabc". It does not match "abcABC" because the  change  of         It  matches  "abcabc". It does not match "abcABC" because the change of
4797         processing option does not affect the called subpattern.         processing option does not affect the called subpattern.
4798    
4799    
4800    ONIGURUMA SUBROUTINE SYNTAX
4801    
4802           For compatibility with Oniguruma, the non-Perl syntax \g followed by  a
4803           name or a number enclosed either in angle brackets or single quotes, is
4804           an alternative syntax for referencing a  subpattern  as  a  subroutine,
4805           possibly  recursively. Here are two of the examples used above, rewrit-
4806           ten using this syntax:
4807    
4808             (?<pn> \( ( (?>[^()]+) | \g<pn> )* \) )
4809             (sens|respons)e and \g'1'ibility
4810    
4811           PCRE supports an extension to Oniguruma: if a number is preceded  by  a
4812           plus or a minus sign it is taken as a relative reference. For example:
4813    
4814             (abc)(?i:\g<-1>)
4815    
4816           Note  that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are not
4817           synonymous. The former is a back reference; the latter is a  subroutine
4818           call.
4819    
4820    
4821  CALLOUTS  CALLOUTS
4822    
4823         Perl has a feature whereby using the sequence (?{...}) causes arbitrary         Perl has a feature whereby using the sequence (?{...}) causes arbitrary
# Line 4585  CALLOUTS Line 4853  CALLOUTS
4853         is given in the pcrecallout documentation.         is given in the pcrecallout documentation.
4854    
4855    
4856  BACTRACKING CONTROL  BACKTRACKING CONTROL
4857    
4858         Perl  5.10 introduced a number of "Special Backtracking Control Verbs",         Perl  5.10 introduced a number of "Special Backtracking Control Verbs",
4859         which are described in the Perl documentation as "experimental and sub-         which are described in the Perl documentation as "experimental and sub-
# Line 4594  BACTRACKING CONTROL Line 4862  BACTRACKING CONTROL
4862         during upgrades." The same remarks apply to the PCRE features described         during upgrades." The same remarks apply to the PCRE features described
4863         in this section.         in this section.
4864    
4865         Since these verbs are specifically related to backtracking, they can be         Since these verbs are specifically related  to  backtracking,  most  of
4866         used  only  when  the pattern is to be matched using pcre_exec(), which         them  can  be  used  only  when  the  pattern  is  to  be matched using
4867         uses a backtracking algorithm. They cause an error  if  encountered  by         pcre_exec(), which uses a backtracking algorithm. With the exception of
4868         pcre_dfa_exec().         (*FAIL), which behaves like a failing negative assertion, they cause an
4869           error if encountered by pcre_dfa_exec().
4870    
4871         The  new verbs make use of what was previously invalid syntax: an open-         The new verbs make use of what was previously invalid syntax: an  open-
4872         ing parenthesis followed by an asterisk. In Perl, they are generally of         ing parenthesis followed by an asterisk. In Perl, they are generally of
4873         the form (*VERB:ARG) but PCRE does not support the use of arguments, so         the form (*VERB:ARG) but PCRE does not support the use of arguments, so
4874         its general form is just (*VERB). Any number of these verbs  may  occur         its  general  form is just (*VERB). Any number of these verbs may occur
4875         in a pattern. There are two kinds:         in a pattern. There are two kinds:
4876    
4877     Verbs that act immediately     Verbs that act immediately
# Line 4611  BACTRACKING CONTROL Line 4880  BACTRACKING CONTROL
4880    
4881            (*ACCEPT)            (*ACCEPT)
4882    
4883         This  verb causes the match to end successfully, skipping the remainder         This verb causes the match to end successfully, skipping the  remainder
4884         of the pattern. When inside a recursion, only the innermost pattern  is         of  the pattern. When inside a recursion, only the innermost pattern is
4885         ended  immediately.  PCRE  differs  from  Perl  in  what happens if the         ended immediately. PCRE differs  from  Perl  in  what  happens  if  the
4886         (*ACCEPT) is inside capturing parentheses. In Perl, the data so far  is         (*ACCEPT)  is inside capturing parentheses. In Perl, the data so far is
4887         captured: in PCRE no data is captured. For example:         captured: in PCRE no data is captured. For example:
4888    
4889           A(A|B(*ACCEPT)|C)D           A(A|B(*ACCEPT)|C)D
4890    
4891         This  matches  "AB", "AAD", or "ACD", but when it matches "AB", no data         This matches "AB", "AAD", or "ACD", but when it matches "AB",  no  data
4892         is captured.         is captured.
4893    
4894           (*FAIL) or (*F)           (*FAIL) or (*F)
4895    
4896         This verb causes the match to fail, forcing backtracking to  occur.  It         This  verb  causes the match to fail, forcing backtracking to occur. It
4897         is  equivalent to (?!) but easier to read. The Perl documentation notes         is equivalent to (?!) but easier to read. The Perl documentation  notes
4898         that it is probably useful only when combined  with  (?{})  or  (??{}).         that  it  is  probably  useful only when combined with (?{}) or (??{}).
4899         Those  are,  of course, Perl features that are not present in PCRE. The         Those are, of course, Perl features that are not present in  PCRE.  The
4900         nearest equivalent is the callout feature, as for example in this  pat-         nearest  equivalent is the callout feature, as for example in this pat-
4901         tern:         tern:
4902    
4903           a+(?C)(*FAIL)           a+(?C)(*FAIL)
4904    
4905         A  match  with the string "aaaa" always fails, but the callout is taken         A match with the string "aaaa" always fails, but the callout  is  taken
4906         before each backtrack happens (in this example, 10 times).         before each backtrack happens (in this example, 10 times).
4907    
4908     Verbs that act after backtracking     Verbs that act after backtracking
4909    
4910         The following verbs do nothing when they are encountered. Matching con-         The following verbs do nothing when they are encountered. Matching con-
4911         tinues  with what follows, but if there is no subsequent match, a fail-         tinues with what follows, but if there is no subsequent match, a  fail-
4912         ure is forced.  The verbs  differ  in  exactly  what  kind  of  failure         ure  is  forced.   The  verbs  differ  in  exactly what kind of failure
4913         occurs.         occurs.
4914    
4915           (*COMMIT)           (*COMMIT)
4916    
4917         This  verb  causes  the whole match to fail outright if the rest of the         This verb causes the whole match to fail outright if the  rest  of  the
4918         pattern does not match. Even if the pattern is unanchored,  no  further         pattern  does  not match. Even if the pattern is unanchored, no further
4919         attempts  to find a match by advancing the start point take place. Once         attempts to find a match by advancing the start point take place.  Once
4920         (*COMMIT) has been passed, pcre_exec() is committed to finding a  match         (*COMMIT)  has been passed, pcre_exec() is committed to finding a match
4921         at the current starting point, or not at all. For example:         at the current starting point, or not at all. For example:
4922    
4923           a+(*COMMIT)b           a+(*COMMIT)b
4924    
4925         This  matches  "xxaab" but not "aacaab". It can be thought of as a kind         This matches "xxaab" but not "aacaab". It can be thought of as  a  kind
4926         of dynamic anchor, or "I've started, so I must finish."         of dynamic anchor, or "I've started, so I must finish."
4927    
4928           (*PRUNE)           (*PRUNE)
4929    
4930         This verb causes the match to fail at the current position if the  rest         This  verb causes the match to fail at the current position if the rest
4931         of the pattern does not match. If the pattern is unanchored, the normal         of the pattern does not match. If the pattern is unanchored, the normal
4932         "bumpalong" advance to the next starting character then happens.  Back-         "bumpalong"  advance to the next starting character then happens. Back-
4933         tracking  can  occur as usual to the left of (*PRUNE), or when matching         tracking can occur as usual to the left of (*PRUNE), or  when  matching
4934         to the right of (*PRUNE), but if there is no match to the right,  back-         to  the right of (*PRUNE), but if there is no match to the right, back-
4935         tracking  cannot  cross (*PRUNE).  In simple cases, the use of (*PRUNE)         tracking cannot cross (*PRUNE).  In simple cases, the use  of  (*PRUNE)
4936         is just an alternative to an atomic group or possessive quantifier, but         is just an alternative to an atomic group or possessive quantifier, but
4937         there  are  some uses of (*PRUNE) that cannot be expressed in any other         there are some uses of (*PRUNE) that cannot be expressed in  any  other
4938         way.         way.
4939    
4940           (*SKIP)           (*SKIP)
4941    
4942         This verb is like (*PRUNE), except that if the pattern  is  unanchored,         This  verb  is like (*PRUNE), except that if the pattern is unanchored,
4943         the  "bumpalong" advance is not to the next character, but to the posi-         the "bumpalong" advance is not to the next character, but to the  posi-
4944         tion in the subject where (*SKIP) was  encountered.  (*SKIP)  signifies         tion  in  the  subject where (*SKIP) was encountered. (*SKIP) signifies
4945         that  whatever  text  was  matched leading up to it cannot be part of a         that whatever text was matched leading up to it cannot  be  part  of  a
4946         successful match. Consider:         successful match. Consider:
4947    
4948           a+(*SKIP)b           a+(*SKIP)b
4949    
4950         If the subject is "aaaac...",  after  the  first  match  attempt  fails         If  the  subject  is  "aaaac...",  after  the first match attempt fails
4951         (starting  at  the  first  character in the string), the starting point         (starting at the first character in the  string),  the  starting  point
4952         skips on to start the next attempt at "c". Note that a possessive quan-         skips on to start the next attempt at "c". Note that a possessive quan-
4953         tifer  does not have the same effect in this example; although it would         tifer does not have the same effect in this example; although it  would
4954         suppress backtracking  during  the  first  match  attempt,  the  second         suppress  backtracking  during  the  first  match  attempt,  the second
4955         attempt  would  start at the second character instead of skipping on to         attempt would start at the second character instead of skipping  on  to
4956         "c".         "c".
4957    
4958           (*THEN)           (*THEN)
4959    
4960         This verb causes a skip to the next alternation if the rest of the pat-         This verb causes a skip to the next alternation if the rest of the pat-
4961         tern does not match. That is, it cancels pending backtracking, but only         tern does not match. That is, it cancels pending backtracking, but only
4962         within the current alternation. Its name  comes  from  the  observation         within  the  current  alternation.  Its name comes from the observation
4963         that it can be used for a pattern-based if-then-else block:         that it can be used for a pattern-based if-then-else block:
4964    
4965           ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...           ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
4966    
4967         If  the COND1 pattern matches, FOO is tried (and possibly further items         If the COND1 pattern matches, FOO is tried (and possibly further  items
4968         after the end of the group if FOO succeeds);  on  failure  the  matcher         after  the  end  of  the group if FOO succeeds); on failure the matcher
4969         skips  to  the second alternative and tries COND2, without backtracking         skips to the second alternative and tries COND2,  without  backtracking
4970         into COND1. If (*THEN) is used outside  of  any  alternation,  it  acts         into  COND1.  If  (*THEN)  is  used outside of any alternation, it acts
4971         exactly like (*PRUNE).         exactly like (*PRUNE).
4972    
4973    
# Line 4716  AUTHOR Line 4985  AUTHOR
4985    
4986  REVISION  REVISION
4987    
4988         Last updated: 09 August 2007         Last updated: 19 April 2008
4989         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2008 University of Cambridge.
4990  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
4991    
4992    
# Line 4845  CHARACTER CLASSES Line 5114  CHARACTER CLASSES
5114           [^...]      negative character class           [^...]      negative character class
5115           [x-y]       range (can be used for hex characters)           [x-y]       range (can be used for hex characters)
5116           [[:xxx:]]   positive POSIX named set           [[:xxx:]]   positive POSIX named set
5117           [[^:xxx:]]  negative POSIX named set           [[:^xxx:]]  negative POSIX named set
5118    
5119           alnum       alphanumeric           alnum       alphanumeric
5120           alpha       alphabetic           alpha       alphabetic
# Line 4975  SUBROUTINE REFERENCES (POSSIBLY RECURSIV Line 5244  SUBROUTINE REFERENCES (POSSIBLY RECURSIV
5244           (?-n)          call subpattern by relative number           (?-n)          call subpattern by relative number
5245           (?&name)       call subpattern by name (Perl)           (?&name)       call subpattern by name (Perl)
5246           (?P>name)      call subpattern by name (Python)           (?P>name)      call subpattern by name (Python)
5247             \g<name>       call subpattern by name (Oniguruma)
5248             \g'name'       call subpattern by name (Oniguruma)
5249             \g<n>          call subpattern by absolute number (Oniguruma)
5250             \g'n'          call subpattern by absolute number (Oniguruma)
5251             \g<+n>         call subpattern by relative number (PCRE extension)
5252             \g'+n'         call subpattern by relative number (PCRE extension)
5253             \g<-n>         call subpattern by relative number (PCRE extension)
5254             \g'-n'         call subpattern by relative number (PCRE extension)
5255    
5256    
5257  CONDITIONAL PATTERNS  CONDITIONAL PATTERNS
# Line 5013  BACKTRACKING CONTROL Line 5290  BACKTRACKING CONTROL
5290           (*THEN)        local failure, backtrack to next alternation           (*THEN)        local failure, backtrack to next alternation
5291    
5292    
5293    NEWLINE CONVENTIONS
5294    
5295           These  are  recognized only at the very start of the pattern or after a
5296           (*BSR_...) option.
5297    
5298             (*CR)
5299             (*LF)
5300             (*CRLF)
5301             (*ANYCRLF)
5302             (*ANY)
5303    
5304    
5305    WHAT \R MATCHES
5306    
5307           These are recognized only at the very start of the pattern or  after  a
5308           (*...) option that sets the newline convention.
5309    
5310             (*BSR_ANYCRLF)
5311             (*BSR_UNICODE)
5312    
5313    
5314  CALLOUTS  CALLOUTS
5315    
5316           (?C)      callout           (?C)      callout
# Line 5033  AUTHOR Line 5331  AUTHOR
5331    
5332  REVISION  REVISION
5333    
5334         Last updated: 08 August 2007         Last updated: 09 April 2008
5335         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2008 University of Cambridge.
5336  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
5337    
5338    
# Line 5689  MATCHING NEWLINE CHARACTERS Line 5987  MATCHING NEWLINE CHARACTERS
5987  MATCHING A PATTERN  MATCHING A PATTERN
5988    
5989         The  function  regexec()  is  called  to  match a compiled pattern preg         The  function  regexec()  is  called  to  match a compiled pattern preg
5990         against a given string, which is terminated by a zero byte, subject  to         against a given string, which is by default terminated by a  zero  byte
5991         the options in eflags. These can be:         (but  see  REG_STARTEND below), subject to the options in eflags. These
5992           can be:
5993    
5994           REG_NOTBOL           REG_NOTBOL
5995    
# Line 5702  MATCHING A PATTERN Line 6001  MATCHING A PATTERN
6001         The PCRE_NOTEOL option is set when calling the underlying PCRE matching         The PCRE_NOTEOL option is set when calling the underlying PCRE matching
6002         function.         function.
6003    
6004             REG_STARTEND
6005    
6006           The string is considered to start at string +  pmatch[0].rm_so  and  to
6007           have  a terminating NUL located at string + pmatch[0].rm_eo (there need
6008           not actually be a NUL at that location), regardless  of  the  value  of
6009           nmatch.  This  is a BSD extension, compatible with but not specified by
6010           IEEE Standard 1003.2 (POSIX.2), and should  be  used  with  caution  in
6011           software intended to be portable to other systems. Note that a non-zero
6012           rm_so does not imply REG_NOTBOL; REG_STARTEND affects only the location
6013           of the string, not how it is matched.
6014    
6015         If  the pattern was compiled with the REG_NOSUB flag, no data about any         If  the pattern was compiled with the REG_NOSUB flag, no data about any
6016         matched strings  is  returned.  The  nmatch  and  pmatch  arguments  of         matched strings  is  returned.  The  nmatch  and  pmatch  arguments  of
6017         regexec() are ignored.         regexec() are ignored.
# Line 5748  AUTHOR Line 6058  AUTHOR
6058    
6059  REVISION  REVISION
6060    
6061         Last updated: 06 March 2007         Last updated: 05 April 2008
6062         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2008 University of Cambridge.
6063  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
6064    
6065    
# Line 5837  MATCHING INTERFACE Line 6147  MATCHING INTERFACE
6147    
6148           c. The "i"th argument has a suitable type for holding the           c. The "i"th argument has a suitable type for holding the
6149              string captured as the "i"th sub-pattern. If you pass in              string captured as the "i"th sub-pattern. If you pass in
6150              NULL for the "i"th argument, or pass fewer arguments than              void * NULL for the "i"th argument, or a non-void * NULL
6151                of the correct type, or pass fewer arguments than the
6152              number of sub-patterns, "i"th captured sub-pattern is              number of sub-patterns, "i"th captured sub-pattern is
6153              ignored.              ignored.
6154    
# Line 6085  AUTHOR Line 6396  AUTHOR
6396    
6397  REVISION  REVISION
6398    
6399         Last updated: 06 March 2007         Last updated: 12 November 2007
6400  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
6401    
6402    
# Line 6113  PCRE SAMPLE PROGRAM Line 6424  PCRE SAMPLE PROGRAM
6424         bility  of  matching an empty string. Comments in the code explain what         bility  of  matching an empty string. Comments in the code explain what
6425         is going on.         is going on.
6426    
6427         The demonstration program is automatically built if you use  "./config-         If PCRE is installed in the standard include  and  library  di