/[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 247 by ph10, Mon Sep 17 09:38:32 2007 UTC
# Line 361  CODE VALUE OF NEWLINE Line 361  CODE VALUE OF NEWLINE
361         conventional to use the standard for your operating system.         conventional to use the standard for your operating system.
362    
363    
364    WHAT \R MATCHES
365    
366           By default, the sequence \R in a pattern matches  any  Unicode  newline
367           sequence,  whatever  has  been selected as the line ending sequence. If
368           you specify
369    
370             --enable-bsr-anycrlf
371    
372           the default is changed so that \R matches only CR, LF, or  CRLF.  What-
373           ever  is selected when PCRE is built can be overridden when the library
374           functions are called.
375    
376    
377  BUILDING SHARED AND STATIC LIBRARIES  BUILDING SHARED AND STATIC LIBRARIES
378    
379         The PCRE building process uses libtool to build both shared and  static         The PCRE building process uses libtool to build both shared and  static
# Line 513  AUTHOR Line 526  AUTHOR
526    
527  REVISION  REVISION
528    
529         Last updated: 30 July 2007         Last updated: 11 September 2007
530         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
531  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
532    
# Line 908  NEWLINES Line 921  NEWLINES
921         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
922         pattern is compiled, or when it is matched.         pattern is compiled, or when it is matched.
923    
924           At compile time, the newline convention can be specified by the options
925           argument of pcre_compile(), or it can be specified by special  text  at
926           the start of the pattern itself; this overrides any other settings. See
927           the pcrepattern page for details of the special character sequences.
928    
929         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-
930         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
931         newline  convention  affects  the  handling of the dot, circumflex, and         newline convention affects the handling of  the  dot,  circumflex,  and
932         dollar metacharacters, the handling of #-comments in /x mode, and, when         dollar metacharacters, the handling of #-comments in /x mode, and, when
933         CRLF  is a recognized line ending sequence, the match position advance-         CRLF is a recognized line ending sequence, the match position  advance-
934         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
935         not affect the interpretation of the \n or \r escape sequences.         section on pcre_exec() options below.
936    
937           The choice of newline convention does not affect the interpretation  of
938           the  \n  or  \r  escape  sequences, nor does it affect what \R matches,
939           which is controlled in a similar way, but by separate options.
940    
941    
942  MULTITHREADING  MULTITHREADING
943    
944         The  PCRE  functions  can be used in multi-threading applications, with         The PCRE functions can be used in  multi-threading  applications,  with
945         the  proviso  that  the  memory  management  functions  pointed  to  by         the  proviso  that  the  memory  management  functions  pointed  to  by
946         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
947         callout function pointed to by pcre_callout, are shared by all threads.         callout function pointed to by pcre_callout, are shared by all threads.
948    
949         The  compiled form of a regular expression is not altered during match-         The compiled form of a regular expression is not altered during  match-
950         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
951         at once.         at once.
952    
# Line 932  MULTITHREADING Line 954  MULTITHREADING
954  SAVING PRECOMPILED PATTERNS FOR LATER USE  SAVING PRECOMPILED PATTERNS FOR LATER USE
955    
956         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
957         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
958         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
959         pcreprecompile documentation. However, compiling a  regular  expression         pcreprecompile  documentation.  However, compiling a regular expression
960         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-
961         anteed to work and may cause crashes.         anteed to work and may cause crashes.
962    
963    
# Line 943  CHECKING BUILD-TIME OPTIONS Line 965  CHECKING BUILD-TIME OPTIONS
965    
966         int pcre_config(int what, void *where);         int pcre_config(int what, void *where);
967    
968         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-
969         cover which optional features have been compiled into the PCRE library.         cover which optional features have been compiled into the PCRE library.
970         The pcrebuild documentation has more details about these optional  fea-         The  pcrebuild documentation has more details about these optional fea-
971         tures.         tures.
972    
973         The  first  argument  for pcre_config() is an integer, specifying which         The first argument for pcre_config() is an  integer,  specifying  which
974         information is required; the second argument is a pointer to a variable         information is required; the second argument is a pointer to a variable
975         into  which  the  information  is  placed. The following information is         into which the information is  placed.  The  following  information  is
976         available:         available:
977    
978           PCRE_CONFIG_UTF8           PCRE_CONFIG_UTF8
979    
980         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-
981         able; otherwise it is set to zero.         able; otherwise it is set to zero.
982    
983           PCRE_CONFIG_UNICODE_PROPERTIES           PCRE_CONFIG_UNICODE_PROPERTIES
984    
985         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
986         character properties is available; otherwise it is set to zero.         character properties is available; otherwise it is set to zero.
987    
988           PCRE_CONFIG_NEWLINE           PCRE_CONFIG_NEWLINE
989    
990         The output is an integer whose value specifies  the  default  character         The  output  is  an integer whose value specifies the default character
991         sequence  that is recognized as meaning "newline". The four values that         sequence that is recognized as meaning "newline". The four values  that
992         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,
993         and  -1  for  ANY. The default should normally be the standard sequence         and -1 for ANY. The default should normally be  the  standard  sequence
994         for your operating system.         for your operating system.
995    
996             PCRE_CONFIG_BSR
997    
998           The output is an integer whose value indicates what character sequences
999           the \R escape sequence matches by default. A value of 0 means  that  \R
1000           matches  any  Unicode  line ending sequence; a value of 1 means that \R
1001           matches only CR, LF, or CRLF. The default can be overridden when a pat-
1002           tern is compiled or matched.
1003    
1004           PCRE_CONFIG_LINK_SIZE           PCRE_CONFIG_LINK_SIZE
1005    
1006         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
1007         internal linkage in compiled regular expressions. The value is 2, 3, or         internal linkage in compiled regular expressions. The value is 2, 3, or
1008         4. Larger values allow larger regular expressions to  be  compiled,  at         4.  Larger  values  allow larger regular expressions to be compiled, at
1009         the  expense  of  slower matching. The default value of 2 is sufficient         the expense of slower matching. The default value of  2  is  sufficient
1010         for all but the most massive patterns, since  it  allows  the  compiled         for  all  but  the  most massive patterns, since it allows the compiled
1011         pattern to be up to 64K in size.         pattern to be up to 64K in size.
1012    
1013           PCRE_CONFIG_POSIX_MALLOC_THRESHOLD           PCRE_CONFIG_POSIX_MALLOC_THRESHOLD
1014    
1015         The  output  is  an integer that contains the threshold above which the         The output is an integer that contains the threshold  above  which  the
1016         POSIX interface uses malloc() for output vectors. Further  details  are         POSIX  interface  uses malloc() for output vectors. Further details are
1017         given in the pcreposix documentation.         given in the pcreposix documentation.
1018    
1019           PCRE_CONFIG_MATCH_LIMIT           PCRE_CONFIG_MATCH_LIMIT
1020    
1021         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
1022         internal matching function calls in a  pcre_exec()  execution.  Further         internal  matching  function  calls in a pcre_exec() execution. Further
1023         details are given with pcre_exec() below.         details are given with pcre_exec() below.
1024    
1025           PCRE_CONFIG_MATCH_LIMIT_RECURSION           PCRE_CONFIG_MATCH_LIMIT_RECURSION
1026    
1027         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
1028         recursion when calling the internal matching function in a  pcre_exec()         recursion  when calling the internal matching function in a pcre_exec()
1029         execution. Further details are given with pcre_exec() below.         execution. Further details are given with pcre_exec() below.
1030    
1031           PCRE_CONFIG_STACKRECURSE           PCRE_CONFIG_STACKRECURSE
1032    
1033         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
1034         running pcre_exec() is implemented by recursive function calls that use         running pcre_exec() is implemented by recursive function calls that use
1035         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
1036         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
1037         on  the  heap  instead  of  recursive  function  calls.  In  this case,         on the  heap  instead  of  recursive  function  calls.  In  this  case,
1038         pcre_stack_malloc and  pcre_stack_free  are  called  to  manage  memory         pcre_stack_malloc  and  pcre_stack_free  are  called  to  manage memory
1039         blocks on the heap, thus avoiding the use of the stack.         blocks on the heap, thus avoiding the use of the stack.
1040    
1041    
# Line 1022  COMPILING A PATTERN Line 1052  COMPILING A PATTERN
1052    
1053         Either of the functions pcre_compile() or pcre_compile2() can be called         Either of the functions pcre_compile() or pcre_compile2() can be called
1054         to compile a pattern into an internal form. The only difference between         to compile a pattern into an internal form. The only difference between
1055         the  two interfaces is that pcre_compile2() has an additional argument,         the two interfaces is that pcre_compile2() has an additional  argument,
1056         errorcodeptr, via which a numerical error code can be returned.         errorcodeptr, via which a numerical error code can be returned.
1057    
1058         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
1059         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
1060         obtained via pcre_malloc is returned. This contains the  compiled  code         obtained  via  pcre_malloc is returned. This contains the compiled code
1061         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
1062         is a typedef for a structure whose contents are not externally defined.         is a typedef for a structure whose contents are not externally defined.
1063         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
1064         longer required.         longer required.
1065    
1066         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
1067         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
1068         fully relocatable, because it may contain a copy of the tableptr  argu-         fully  relocatable, because it may contain a copy of the tableptr argu-
1069         ment, which is an address (see below).         ment, which is an address (see below).
1070    
1071         The options argument contains various bit settings that affect the com-         The options argument contains various bit settings that affect the com-
1072         pilation. It should be zero if no options are required.  The  available         pilation.  It  should be zero if no options are required. The available
1073         options  are  described  below. Some of them, in particular, those that         options are described below. Some of them, in  particular,  those  that
1074         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
1075         pattern  (see  the  detailed  description in the pcrepattern documenta-         pattern (see the detailed description  in  the  pcrepattern  documenta-
1076         tion). For these options, the contents of the options  argument  speci-         tion).  For  these options, the contents of the options argument speci-
1077         fies  their initial settings at the start of compilation and execution.         fies their initial settings at the start of compilation and  execution.
1078         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
1079         of matching as well as at compile time.         of matching as well as at compile time.
1080    
1081         If errptr is NULL, pcre_compile() returns NULL immediately.  Otherwise,         If errptr is NULL, pcre_compile() returns NULL immediately.  Otherwise,
1082         if compilation of a pattern fails,  pcre_compile()  returns  NULL,  and         if  compilation  of  a  pattern fails, pcre_compile() returns NULL, and
1083         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-
1084         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
1085         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-
1086         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
1087         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
1088         given.         given.
1089    
1090         If pcre_compile2() is used instead of pcre_compile(),  and  the  error-         If  pcre_compile2()  is  used instead of pcre_compile(), and the error-
1091         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
1092         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
1093         textual error message. Error codes and messages are listed below.         textual error message. Error codes and messages are listed below.
1094    
1095         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
1096         character tables that are  built  when  PCRE  is  compiled,  using  the         character  tables  that  are  built  when  PCRE  is compiled, using the
1097         default  C  locale.  Otherwise, tableptr must be an address that is the         default C locale. Otherwise, tableptr must be an address  that  is  the
1098         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
1099         compiled  pattern,  and used again by pcre_exec(), unless another table         compiled pattern, and used again by pcre_exec(), unless  another  table
1100         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
1101         support below.         support below.
1102    
1103         This  code  fragment  shows a typical straightforward call to pcre_com-         This code fragment shows a typical straightforward  call  to  pcre_com-
1104         pile():         pile():
1105    
1106           pcre *re;           pcre *re;
# Line 1083  COMPILING A PATTERN Line 1113  COMPILING A PATTERN
1113             &erroffset,       /* for error offset */             &erroffset,       /* for error offset */
1114             NULL);            /* use default character tables */             NULL);            /* use default character tables */
1115    
1116         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
1117         file:         file:
1118    
1119           PCRE_ANCHORED           PCRE_ANCHORED
1120    
1121         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
1122         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
1123         that  is being searched (the "subject string"). This effect can also be         that is being searched (the "subject string"). This effect can also  be
1124         achieved by appropriate constructs in the pattern itself, which is  the         achieved  by appropriate constructs in the pattern itself, which is the
1125         only way to do it in Perl.         only way to do it in Perl.
1126    
1127           PCRE_AUTO_CALLOUT           PCRE_AUTO_CALLOUT
1128    
1129         If this bit is set, pcre_compile() automatically inserts callout items,         If this bit is set, pcre_compile() automatically inserts callout items,
1130         all with number 255, before each pattern item. For  discussion  of  the         all  with  number  255, before each pattern item. For discussion of the
1131         callout facility, see the pcrecallout documentation.         callout facility, see the pcrecallout documentation.
1132    
1133             PCRE_BSR_ANYCRLF
1134             PCRE_BSR_UNICODE
1135    
1136           These options (which are mutually exclusive) control what the \R escape
1137           sequence  matches.  The choice is either to match only CR, LF, or CRLF,
1138           or to match any Unicode newline sequence. The default is specified when
1139           PCRE is built. It can be overridden from within the pattern, or by set-
1140           ting an option when a compiled pattern is matched.
1141    
1142           PCRE_CASELESS           PCRE_CASELESS
1143    
1144         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
1145         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
1146         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
1147         always understands the concept of case for characters whose values  are         always  understands the concept of case for characters whose values are
1148         less  than 128, so caseless matching is always possible. For characters         less than 128, so caseless matching is always possible. For  characters
1149         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-
1150         piled  with Unicode property support, but not otherwise. If you want to         piled with Unicode property support, but not otherwise. If you want  to
1151         use caseless matching for characters 128 and  above,  you  must  ensure         use  caseless  matching  for  characters 128 and above, you must ensure
1152         that  PCRE  is  compiled  with Unicode property support as well as with         that PCRE is compiled with Unicode property support  as  well  as  with
1153         UTF-8 support.         UTF-8 support.
1154    
1155           PCRE_DOLLAR_ENDONLY           PCRE_DOLLAR_ENDONLY
1156    
1157         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
1158         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
1159         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
1160         before  any  other newlines). The PCRE_DOLLAR_ENDONLY option is ignored         before any other newlines). The PCRE_DOLLAR_ENDONLY option  is  ignored
1161         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
1162         Perl, and no way to set it within a pattern.         Perl, and no way to set it within a pattern.
1163    
1164           PCRE_DOTALL           PCRE_DOTALL
1165    
1166         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-
1167         acters, including those that indicate newline. Without it, a  dot  does         acters,  including  those that indicate newline. Without it, a dot does
1168         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
1169         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
1170         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
1171         newline characters, independent of the setting of this option.         newline characters, independent of the setting of this option.
1172    
1173           PCRE_DUPNAMES           PCRE_DUPNAMES
1174    
1175         If this bit is set, names used to identify capturing  subpatterns  need         If  this  bit is set, names used to identify capturing subpatterns need
1176         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
1177         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
1178         matched.  There  are  more details of named subpatterns below; see also         matched. There are more details of named subpatterns  below;  see  also
1179         the pcrepattern documentation.         the pcrepattern documentation.
1180    
1181           PCRE_EXTENDED           PCRE_EXTENDED
1182    
1183         If this bit is set, whitespace  data  characters  in  the  pattern  are         If  this  bit  is  set,  whitespace  data characters in the pattern are
1184         totally ignored except when escaped or inside a character class. White-         totally ignored except when escaped or inside a character class. White-
1185         space does not include the VT character (code 11). In addition, charac-         space does not include the VT character (code 11). In addition, charac-
1186         ters between an unescaped # outside a character class and the next new-         ters between an unescaped # outside a character class and the next new-
1187         line, inclusive, are also ignored. This  is  equivalent  to  Perl's  /x         line,  inclusive,  are  also  ignored.  This is equivalent to Perl's /x
1188         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-
1189         ting.         ting.
1190    
1191         This option makes it possible to include  comments  inside  complicated         This  option  makes  it possible to include comments inside complicated
1192         patterns.   Note,  however,  that this applies only to data characters.         patterns.  Note, however, that this applies only  to  data  characters.
1193         Whitespace  characters  may  never  appear  within  special   character         Whitespace   characters  may  never  appear  within  special  character
1194         sequences  in  a  pattern,  for  example  within the sequence (?( which         sequences in a pattern, for  example  within  the  sequence  (?(  which
1195         introduces a conditional subpattern.         introduces a conditional subpattern.
1196    
1197           PCRE_EXTRA           PCRE_EXTRA
1198    
1199         This option was invented in order to turn on  additional  functionality         This  option  was invented in order to turn on additional functionality
1200         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
1201         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
1202         letter  that  has  no  special  meaning causes an error, thus reserving         letter that has no special meaning  causes  an  error,  thus  reserving
1203         these combinations for future expansion. By  default,  as  in  Perl,  a         these  combinations  for  future  expansion.  By default, as in Perl, a
1204         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
1205         literal. (Perl can, however, be persuaded to give a warning for  this.)         literal.  (Perl can, however, be persuaded to give a warning for this.)
1206         There  are  at  present no other features controlled by this option. It         There are at present no other features controlled by  this  option.  It
1207         can also be set by a (?X) option setting within a pattern.         can also be set by a (?X) option setting within a pattern.
1208    
1209           PCRE_FIRSTLINE           PCRE_FIRSTLINE
1210    
1211         If this option is set, an  unanchored  pattern  is  required  to  match         If  this  option  is  set,  an  unanchored pattern is required to match
1212         before  or  at  the  first  newline  in  the subject string, though the         before or at the first  newline  in  the  subject  string,  though  the
1213         matched text may continue over the newline.         matched text may continue over the newline.
1214    
1215           PCRE_MULTILINE           PCRE_MULTILINE
1216    
1217         By default, PCRE treats the subject string as consisting  of  a  single         By  default,  PCRE  treats the subject string as consisting of a single
1218         line  of characters (even if it actually contains newlines). The "start         line of characters (even if it actually contains newlines). The  "start
1219         of line" metacharacter (^) matches only at the  start  of  the  string,         of  line"  metacharacter  (^)  matches only at the start of the string,
1220         while  the  "end  of line" metacharacter ($) matches only at the end of         while the "end of line" metacharacter ($) matches only at  the  end  of
1221         the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY         the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY
1222         is set). This is the same as Perl.         is set). This is the same as Perl.
1223    
1224         When  PCRE_MULTILINE  it  is set, the "start of line" and "end of line"         When PCRE_MULTILINE it is set, the "start of line" and  "end  of  line"
1225         constructs match immediately following or immediately  before  internal         constructs  match  immediately following or immediately before internal
1226         newlines  in  the  subject string, respectively, as well as at the very         newlines in the subject string, respectively, as well as  at  the  very
1227         start and end. This is equivalent to Perl's /m option, and  it  can  be         start  and  end.  This is equivalent to Perl's /m option, and it can be
1228         changed within a pattern by a (?m) option setting. If there are no new-         changed within a pattern by a (?m) option setting. If there are no new-
1229         lines in a subject string, or no occurrences of ^ or $  in  a  pattern,         lines  in  a  subject string, or no occurrences of ^ or $ in a pattern,
1230         setting PCRE_MULTILINE has no effect.         setting PCRE_MULTILINE has no effect.
1231    
1232           PCRE_NEWLINE_CR           PCRE_NEWLINE_CR
# Line 1196  COMPILING A PATTERN Line 1235  COMPILING A PATTERN
1235           PCRE_NEWLINE_ANYCRLF           PCRE_NEWLINE_ANYCRLF
1236           PCRE_NEWLINE_ANY           PCRE_NEWLINE_ANY
1237    
1238         These  options  override the default newline definition that was chosen         These options override the default newline definition that  was  chosen
1239         when PCRE was built. Setting the first or the second specifies  that  a         when  PCRE  was built. Setting the first or the second specifies that a
1240         newline  is  indicated  by a single character (CR or LF, respectively).         newline is indicated by a single character (CR  or  LF,  respectively).
1241         Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by  the         Setting  PCRE_NEWLINE_CRLF specifies that a newline is indicated by the
1242         two-character  CRLF  sequence.  Setting  PCRE_NEWLINE_ANYCRLF specifies         two-character CRLF  sequence.  Setting  PCRE_NEWLINE_ANYCRLF  specifies
1243         that any of the three preceding sequences should be recognized. Setting         that any of the three preceding sequences should be recognized. Setting
1244         PCRE_NEWLINE_ANY  specifies that any Unicode newline sequence should be         PCRE_NEWLINE_ANY specifies that any Unicode newline sequence should  be
1245         recognized. The Unicode newline sequences are the three just mentioned,         recognized. The Unicode newline sequences are the three just mentioned,
1246         plus  the  single  characters  VT (vertical tab, U+000B), FF (formfeed,         plus the single characters VT (vertical  tab,  U+000B),  FF  (formfeed,
1247         U+000C), NEL (next line, U+0085), LS (line separator, U+2028),  and  PS         U+000C),  NEL  (next line, U+0085), LS (line separator, U+2028), and PS
1248         (paragraph  separator,  U+2029).  The  last  two are recognized only in         (paragraph separator, U+2029). The last  two  are  recognized  only  in
1249         UTF-8 mode.         UTF-8 mode.
1250    
1251         The newline setting in the  options  word  uses  three  bits  that  are         The  newline  setting  in  the  options  word  uses three bits that are
1252         treated as a number, giving eight possibilities. Currently only six are         treated as a number, giving eight possibilities. Currently only six are
1253         used (default plus the five values above). This means that if  you  set         used  (default  plus the five values above). This means that if you set
1254         more  than one newline option, the combination may or may not be sensi-         more than one newline option, the combination may or may not be  sensi-
1255         ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to         ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to
1256         PCRE_NEWLINE_CRLF,  but other combinations may yield unused numbers and         PCRE_NEWLINE_CRLF, but other combinations may yield unused numbers  and
1257         cause an error.         cause an error.
1258    
1259         The only time that a line break is specially recognized when  compiling         The  only time that a line break is specially recognized when compiling
1260         a  pattern  is  if  PCRE_EXTENDED  is set, and an unescaped # outside a         a pattern is if PCRE_EXTENDED is set, and  an  unescaped  #  outside  a
1261         character class is encountered. This indicates  a  comment  that  lasts         character  class  is  encountered.  This indicates a comment that lasts
1262         until  after the next line break sequence. In other circumstances, line         until after the next line break sequence. In other circumstances,  line
1263         break  sequences  are  treated  as  literal  data,   except   that   in         break   sequences   are   treated  as  literal  data,  except  that  in
1264         PCRE_EXTENDED mode, both CR and LF are treated as whitespace characters         PCRE_EXTENDED mode, both CR and LF are treated as whitespace characters
1265         and are therefore ignored.         and are therefore ignored.
1266    
1267         The newline option that is set at compile time becomes the default that         The newline option that is set at compile time becomes the default that
1268         is  used for pcre_exec() and pcre_dfa_exec(), but it can be overridden.         is used for pcre_exec() and pcre_dfa_exec(), but it can be  overridden.
1269    
1270           PCRE_NO_AUTO_CAPTURE           PCRE_NO_AUTO_CAPTURE
1271    
1272         If this option is set, it disables the use of numbered capturing paren-         If this option is set, it disables the use of numbered capturing paren-
1273         theses  in the pattern. Any opening parenthesis that is not followed by         theses in the pattern. Any opening parenthesis that is not followed  by
1274         ? behaves as if it were followed by ?: but named parentheses can  still         ?  behaves as if it were followed by ?: but named parentheses can still
1275         be  used  for  capturing  (and  they acquire numbers in the usual way).         be used for capturing (and they acquire  numbers  in  the  usual  way).
1276         There is no equivalent of this option in Perl.         There is no equivalent of this option in Perl.
1277    
1278           PCRE_UNGREEDY           PCRE_UNGREEDY
1279    
1280         This option inverts the "greediness" of the quantifiers  so  that  they         This  option  inverts  the "greediness" of the quantifiers so that they
1281         are  not greedy by default, but become greedy if followed by "?". It is         are not greedy by default, but become greedy if followed by "?". It  is
1282         not compatible with Perl. It can also be set by a (?U)  option  setting         not  compatible  with Perl. It can also be set by a (?U) option setting
1283         within the pattern.         within the pattern.
1284    
1285           PCRE_UTF8           PCRE_UTF8
1286    
1287         This  option  causes PCRE to regard both the pattern and the subject as         This option causes PCRE to regard both the pattern and the  subject  as
1288         strings of UTF-8 characters instead of single-byte  character  strings.         strings  of  UTF-8 characters instead of single-byte character strings.
1289         However,  it is available only when PCRE is built to include UTF-8 sup-         However, it is available only when PCRE is built to include UTF-8  sup-
1290         port. If not, the use of this option provokes an error. Details of  how         port.  If not, the use of this option provokes an error. Details of how
1291         this  option  changes the behaviour of PCRE are given in the section on         this option changes the behaviour of PCRE are given in the  section  on
1292         UTF-8 support in the main pcre page.         UTF-8 support in the main pcre page.
1293    
1294           PCRE_NO_UTF8_CHECK           PCRE_NO_UTF8_CHECK
1295    
1296         When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is         When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is
1297         automatically  checked.  There  is  a  discussion about the validity of         automatically checked. There is a  discussion  about  the  validity  of
1298         UTF-8 strings in the main pcre page. If an invalid  UTF-8  sequence  of         UTF-8  strings  in  the main pcre page. If an invalid UTF-8 sequence of
1299         bytes  is  found,  pcre_compile() returns an error. If you already know         bytes is found, pcre_compile() returns an error. If  you  already  know
1300         that your pattern is valid, and you want to skip this check for perfor-         that your pattern is valid, and you want to skip this check for perfor-
1301         mance  reasons,  you  can set the PCRE_NO_UTF8_CHECK option. When it is         mance reasons, you can set the PCRE_NO_UTF8_CHECK option.  When  it  is
1302         set, the effect of passing an invalid UTF-8  string  as  a  pattern  is         set,  the  effect  of  passing  an invalid UTF-8 string as a pattern is
1303         undefined.  It  may  cause your program to crash. Note that this option         undefined. It may cause your program to crash. Note  that  this  option
1304         can also be passed to pcre_exec() and pcre_dfa_exec(), to suppress  the         can  also be passed to pcre_exec() and pcre_dfa_exec(), to suppress the
1305         UTF-8 validity checking of subject strings.         UTF-8 validity checking of subject strings.
1306    
1307    
1308  COMPILATION ERROR CODES  COMPILATION ERROR CODES
1309    
1310         The  following  table  lists  the  error  codes than may be returned by         The following table lists the error  codes  than  may  be  returned  by
1311         pcre_compile2(), along with the error messages that may be returned  by         pcre_compile2(),  along with the error messages that may be returned by
1312         both  compiling functions. As PCRE has developed, some error codes have         both compiling functions. As PCRE has developed, some error codes  have
1313         fallen out of use. To avoid confusion, they have not been re-used.         fallen out of use. To avoid confusion, they have not been re-used.
1314    
1315            0  no error            0  no error
# Line 1326  COMPILATION ERROR CODES Line 1365  COMPILATION ERROR CODES
1365           50  [this code is not in use]           50  [this code is not in use]
1366           51  octal value is greater than \377 (not in UTF-8 mode)           51  octal value is greater than \377 (not in UTF-8 mode)
1367           52  internal error: overran compiling workspace           52  internal error: overran compiling workspace
1368           53  internal  error:  previously-checked  referenced  subpattern  not           53   internal  error:  previously-checked  referenced  subpattern not
1369         found         found
1370           54  DEFINE group contains more than one branch           54  DEFINE group contains more than one branch
1371           55  repeating a DEFINE group is not allowed           55  repeating a DEFINE group is not allowed
1372           56  inconsistent NEWLINE options"           56  inconsistent NEWLINE options
1373           57  \g is not followed by a braced name or an optionally braced           57  \g is not followed by a braced name or an optionally braced
1374                 non-zero number                 non-zero number
1375           58  (?+ or (?- or (?(+ or (?(- must be followed by a non-zero number           58  (?+ or (?- or (?(+ or (?(- must be followed by a non-zero number
# Line 1341  STUDYING A PATTERN Line 1380  STUDYING A PATTERN
1380         pcre_extra *pcre_study(const pcre *code, int options         pcre_extra *pcre_study(const pcre *code, int options
1381              const char **errptr);              const char **errptr);
1382    
1383         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
1384         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
1385         matching.  The function pcre_study() takes a pointer to a compiled pat-         matching. The function pcre_study() takes a pointer to a compiled  pat-
1386         tern as its first argument. If studying the pattern produces additional         tern as its first argument. If studying the pattern produces additional
1387         information  that  will  help speed up matching, pcre_study() returns a         information that will help speed up matching,  pcre_study()  returns  a
1388         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
1389         the results of the study.         the results of the study.
1390    
1391         The  returned  value  from  pcre_study()  can  be  passed  directly  to         The  returned  value  from  pcre_study()  can  be  passed  directly  to
1392         pcre_exec(). However, a pcre_extra block  also  contains  other  fields         pcre_exec().  However,  a  pcre_extra  block also contains other fields
1393         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
1394         described below in the section on matching a pattern.         described below in the section on matching a pattern.
1395    
1396         If studying the pattern does not  produce  any  additional  information         If  studying  the  pattern  does not produce any additional information
1397         pcre_study() returns NULL. In that circumstance, if the calling program         pcre_study() returns NULL. In that circumstance, if the calling program
1398         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
1399         its own pcre_extra block.         its own pcre_extra block.
1400    
1401         The  second  argument of pcre_study() contains option bits. At present,         The second argument of pcre_study() contains option bits.  At  present,
1402         no options are defined, and this argument should always be zero.         no options are defined, and this argument should always be zero.
1403    
1404         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.
1405         If  studying  succeeds  (even  if no data is returned), the variable it         If studying succeeds (even if no data is  returned),  the  variable  it
1406         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
1407         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
1408         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
1409         after calling pcre_study(), to be sure that it has run successfully.         after calling pcre_study(), to be sure that it has run successfully.
1410    
1411         This is a typical call to pcre_study():         This is a typical call to pcre_study():
# Line 1378  STUDYING A PATTERN Line 1417  STUDYING A PATTERN
1417             &error);        /* set to NULL or points to a message */             &error);        /* set to NULL or points to a message */
1418    
1419         At present, studying a pattern is useful only for non-anchored patterns         At present, studying a pattern is useful only for non-anchored patterns
1420         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-
1421         ble starting bytes is created.         ble starting bytes is created.
1422    
1423    
1424  LOCALE SUPPORT  LOCALE SUPPORT
1425    
1426         PCRE  handles  caseless matching, and determines whether characters are         PCRE handles caseless matching, and determines whether  characters  are
1427         letters, digits, or whatever, by reference to a set of tables,  indexed         letters,  digits, or whatever, by reference to a set of tables, indexed
1428         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
1429         characters with codes less than 128. Higher-valued  codes  never  match         characters  with  codes  less than 128. Higher-valued codes never match
1430         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
1431         with Unicode character property support. The use of locales  with  Uni-         with  Unicode  character property support. The use of locales with Uni-
1432         code  is discouraged. If you are handling characters with codes greater         code is discouraged. If you are handling characters with codes  greater
1433         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
1434         not try to mix the two.         not try to mix the two.
1435    
1436         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
1437         argument of pcre_compile() is  NULL.  These  are  sufficient  for  many         argument  of  pcre_compile()  is  NULL.  These  are sufficient for many
1438         applications.  Normally, the internal tables recognize only ASCII char-         applications.  Normally, the internal tables recognize only ASCII char-
1439         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-
1440         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,
1441         which may cause them to be different.         which may cause them to be different.
1442    
1443         The internal tables can always be overridden by tables supplied by  the         The  internal tables can always be overridden by tables supplied by the
1444         application that calls PCRE. These may be created in a different locale         application that calls PCRE. These may be created in a different locale
1445         from the default. As more and more applications change  to  using  Uni-         from  the  default.  As more and more applications change to using Uni-
1446         code, the need for this locale support is expected to die away.         code, the need for this locale support is expected to die away.
1447    
1448         External  tables  are  built by calling the pcre_maketables() function,         External tables are built by calling  the  pcre_maketables()  function,
1449         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
1450         passed  to  pcre_compile()  or  pcre_exec()  as often as necessary. For         passed to pcre_compile() or pcre_exec()  as  often  as  necessary.  For
1451         example, to build and use tables that are appropriate  for  the  French         example,  to  build  and use tables that are appropriate for the French
1452         locale  (where  accented  characters  with  values greater than 128 are         locale (where accented characters with  values  greater  than  128  are
1453         treated as letters), the following code could be used:         treated as letters), the following code could be used:
1454    
1455           setlocale(LC_CTYPE, "fr_FR");           setlocale(LC_CTYPE, "fr_FR");
1456           tables = pcre_maketables();           tables = pcre_maketables();
1457           re = pcre_compile(..., tables);           re = pcre_compile(..., tables);
1458    
1459         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;
1460         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".
1461    
1462         When  pcre_maketables()  runs,  the  tables are built in memory that is         When pcre_maketables() runs, the tables are built  in  memory  that  is
1463         obtained via pcre_malloc. It is the caller's responsibility  to  ensure         obtained  via  pcre_malloc. It is the caller's responsibility to ensure
1464         that  the memory containing the tables remains available for as long as         that the memory containing the tables remains available for as long  as
1465         it is needed.         it is needed.
1466    
1467         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
1468         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()
1469         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-
1470         tern, compilation, studying and matching all happen in the same locale,         tern, compilation, studying and matching all happen in the same locale,
1471         but different patterns can be compiled in different locales.         but different patterns can be compiled in different locales.
1472    
1473         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
1474         the  internal  tables)  to  pcre_exec(). Although not intended for this         the internal tables) to pcre_exec(). Although  not  intended  for  this
1475         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
1476         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
1477         run time is discussed below in the section on matching a pattern.         run time is discussed below in the section on matching a pattern.
1478    
# Line 1443  INFORMATION ABOUT A PATTERN Line 1482  INFORMATION ABOUT A PATTERN
1482         int pcre_fullinfo(const pcre *code, const pcre_extra *extra,         int pcre_fullinfo(const pcre *code, const pcre_extra *extra,
1483              int what, void *where);              int what, void *where);
1484    
1485         The pcre_fullinfo() function returns information about a compiled  pat-         The  pcre_fullinfo() function returns information about a compiled pat-
1486         tern. It replaces the obsolete pcre_info() function, which is neverthe-         tern. It replaces the obsolete pcre_info() function, which is neverthe-
1487         less retained for backwards compability (and is documented below).         less retained for backwards compability (and is documented below).
1488    
1489         The first argument for pcre_fullinfo() is a  pointer  to  the  compiled         The  first  argument  for  pcre_fullinfo() is a pointer to the compiled
1490         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
1491         the pattern was not studied. The third argument specifies  which  piece         the  pattern  was not studied. The third argument specifies which piece
1492         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
1493         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
1494         success, or one of the following negative numbers:         success, or one of the following negative numbers:
1495    
1496           PCRE_ERROR_NULL       the argument code was NULL           PCRE_ERROR_NULL       the argument code was NULL
# Line 1459  INFORMATION ABOUT A PATTERN Line 1498  INFORMATION ABOUT A PATTERN
1498           PCRE_ERROR_BADMAGIC   the "magic number" was not found           PCRE_ERROR_BADMAGIC   the "magic number" was not found
1499           PCRE_ERROR_BADOPTION  the value of what was invalid           PCRE_ERROR_BADOPTION  the value of what was invalid
1500    
1501         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
1502         an simple check against passing an arbitrary memory pointer. Here is  a         an  simple check against passing an arbitrary memory pointer. Here is a
1503         typical  call  of pcre_fullinfo(), to obtain the length of the compiled         typical call of pcre_fullinfo(), to obtain the length of  the  compiled
1504         pattern:         pattern:
1505    
1506           int rc;           int rc;
# Line 1472  INFORMATION ABOUT A PATTERN Line 1511  INFORMATION ABOUT A PATTERN
1511             PCRE_INFO_SIZE,   /* what is required */             PCRE_INFO_SIZE,   /* what is required */
1512             &length);         /* where to put the data */             &length);         /* where to put the data */
1513    
1514         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
1515         are as follows:         are as follows:
1516    
1517           PCRE_INFO_BACKREFMAX           PCRE_INFO_BACKREFMAX
1518    
1519         Return  the  number  of  the highest back reference in the pattern. The         Return the number of the highest back reference  in  the  pattern.  The
1520         fourth argument should point to an int variable. Zero  is  returned  if         fourth  argument  should  point to an int variable. Zero is returned if
1521         there are no back references.         there are no back references.
1522    
1523           PCRE_INFO_CAPTURECOUNT           PCRE_INFO_CAPTURECOUNT
1524    
1525         Return  the  number of capturing subpatterns in the pattern. The fourth         Return the number of capturing subpatterns in the pattern.  The  fourth
1526         argument should point to an int variable.         argument should point to an int variable.
1527    
1528           PCRE_INFO_DEFAULT_TABLES           PCRE_INFO_DEFAULT_TABLES
1529    
1530         Return a pointer to the internal default character tables within  PCRE.         Return  a pointer to the internal default character tables within PCRE.
1531         The  fourth  argument should point to an unsigned char * variable. This         The fourth argument should point to an unsigned char *  variable.  This
1532         information call is provided for internal use by the pcre_study() func-         information call is provided for internal use by the pcre_study() func-
1533         tion.  External  callers  can  cause PCRE to use its internal tables by         tion. External callers can cause PCRE to use  its  internal  tables  by
1534         passing a NULL table pointer.         passing a NULL table pointer.
1535    
1536           PCRE_INFO_FIRSTBYTE           PCRE_INFO_FIRSTBYTE
1537    
1538         Return information about the first byte of any matched  string,  for  a         Return  information  about  the first byte of any matched string, for a
1539         non-anchored  pattern. The fourth argument should point to an int vari-         non-anchored pattern. The fourth argument should point to an int  vari-
1540         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
1541         is still recognized for backwards compatibility.)         is still recognized for backwards compatibility.)
1542    
1543         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
1544         (cat|cow|coyote), its value is returned. Otherwise, if either         (cat|cow|coyote), its value is returned. Otherwise, if either
1545    
1546         (a) the pattern was compiled with the PCRE_MULTILINE option, and  every         (a)  the pattern was compiled with the PCRE_MULTILINE option, and every
1547         branch starts with "^", or         branch starts with "^", or
1548    
1549         (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
1550         set (if it were set, the pattern would be anchored),         set (if it were set, the pattern would be anchored),
1551    
1552         -1 is returned, indicating that the pattern matches only at  the  start         -1  is  returned, indicating that the pattern matches only at the start
1553         of  a  subject string or after any newline within the string. Otherwise         of a subject string or after any newline within the  string.  Otherwise
1554         -2 is returned. For anchored patterns, -2 is returned.         -2 is returned. For anchored patterns, -2 is returned.
1555    
1556           PCRE_INFO_FIRSTTABLE           PCRE_INFO_FIRSTTABLE
1557    
1558         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
1559         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
1560         matching string, a pointer to the table is returned. Otherwise NULL  is         matching  string, a pointer to the table is returned. Otherwise NULL is
1561         returned.  The fourth argument should point to an unsigned char * vari-         returned. The fourth argument should point to an unsigned char *  vari-
1562         able.         able.
1563    
1564             PCRE_INFO_HASCRORLF
1565    
1566           Return  1  if  the  pattern  contains any explicit matches for CR or LF
1567           characters, otherwise 0. The fourth argument should  point  to  an  int
1568           variable.  An explicit match is either a literal CR or LF character, or
1569           \r or \n.
1570    
1571           PCRE_INFO_JCHANGED           PCRE_INFO_JCHANGED
1572    
1573         Return 1 if the (?J) option setting is used in the  pattern,  otherwise         Return 1 if the (?J) option setting is used in the  pattern,  otherwise
# Line 1801  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1847  MATCHING A PATTERN: THE TRADITIONAL FUNC
1847         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
1848         unachored at matching time.         unachored at matching time.
1849    
1850             PCRE_BSR_ANYCRLF
1851             PCRE_BSR_UNICODE
1852    
1853           These options (which are mutually exclusive) control what the \R escape
1854           sequence  matches.  The choice is either to match only CR, LF, or CRLF,
1855           or to match any Unicode newline sequence. These  options  override  the
1856           choice that was made or defaulted when the pattern was compiled.
1857    
1858           PCRE_NEWLINE_CR           PCRE_NEWLINE_CR
1859           PCRE_NEWLINE_LF           PCRE_NEWLINE_LF
1860           PCRE_NEWLINE_CRLF           PCRE_NEWLINE_CRLF
1861           PCRE_NEWLINE_ANYCRLF           PCRE_NEWLINE_ANYCRLF
1862           PCRE_NEWLINE_ANY           PCRE_NEWLINE_ANY
1863    
1864         These options override  the  newline  definition  that  was  chosen  or         These  options  override  the  newline  definition  that  was chosen or
1865         defaulted  when the pattern was compiled. For details, see the descrip-         defaulted when the pattern was compiled. For details, see the  descrip-
1866         tion of pcre_compile()  above.  During  matching,  the  newline  choice         tion  of  pcre_compile()  above.  During  matching,  the newline choice
1867         affects  the  behaviour  of the dot, circumflex, and dollar metacharac-         affects the behaviour of the dot, circumflex,  and  dollar  metacharac-
1868         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
1869         match  failure  for  an  unanchored  pattern.  When  PCRE_NEWLINE_CRLF,         match failure for an unanchored pattern.
1870         PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is set, and a  match  attempt  
1871         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
1872         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-
1873         after the CRLF.         rent position is at a  CRLF  sequence,  and  the  pattern  contains  no
1874           explicit  matches  for  CR  or  LF  characters,  the  match position is
1875           advanced by two characters instead of one, in other words, to after the
1876           CRLF.
1877    
1878           The above rule is a compromise that makes the most common cases work as
1879           expected. For example, if the  pattern  is  .+A  (and  the  PCRE_DOTALL
1880           option is not set), it does not match the string "\r\nA" because, after
1881           failing at the start, it skips both the CR and the LF before  retrying.
1882           However,  the  pattern  [\r\n]A does match that string, because it con-
1883           tains an explicit CR or LF reference, and so advances only by one char-
1884           acter after the first failure.
1885    
1886           An explicit match for CR of LF is either a literal appearance of one of
1887           those characters, or one of the \r or  \n  escape  sequences.  Implicit
1888           matches  such  as [^X] do not count, nor does \s (which includes CR and
1889           LF in the characters that it matches).
1890    
1891           Notwithstanding the above, anomalous effects may still occur when  CRLF
1892           is a valid newline sequence and explicit \r or \n escapes appear in the
1893           pattern.
1894    
1895           PCRE_NOTBOL           PCRE_NOTBOL
1896    
1897         This option specifies that first character of the subject string is not         This option specifies that first character of the subject string is not
1898         the beginning of a line, so the  circumflex  metacharacter  should  not         the  beginning  of  a  line, so the circumflex metacharacter should not
1899         match  before it. Setting this without PCRE_MULTILINE (at compile time)         match before it. Setting this without PCRE_MULTILINE (at compile  time)
1900         causes circumflex never to match. This option affects only  the  behav-         causes  circumflex  never to match. This option affects only the behav-
1901         iour of the circumflex metacharacter. It does not affect \A.         iour of the circumflex metacharacter. It does not affect \A.
1902    
1903           PCRE_NOTEOL           PCRE_NOTEOL
1904    
1905         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
1906         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
1907         in  multiline mode) a newline immediately before it. Setting this with-         in multiline mode) a newline immediately before it. Setting this  with-
1908         out PCRE_MULTILINE (at compile time) causes dollar never to match. This         out PCRE_MULTILINE (at compile time) causes dollar never to match. This
1909         option  affects only the behaviour of the dollar metacharacter. It does         option affects only the behaviour of the dollar metacharacter. It  does
1910         not affect \Z or \z.         not affect \Z or \z.
1911    
1912           PCRE_NOTEMPTY           PCRE_NOTEMPTY
1913    
1914         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
1915         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
1916         the alternatives match the empty string, the entire  match  fails.  For         the  alternatives  match  the empty string, the entire match fails. For
1917         example, if the pattern         example, if the pattern
1918    
1919           a?b?           a?b?
1920    
1921         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
1922         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
1923         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-
1924         rences of "a" or "b".         rences of "a" or "b".
1925    
1926         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-
1927         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()
1928         function, and when using the /g modifier. It  is  possible  to  emulate         function,  and  when  using  the /g modifier. It is possible to emulate
1929         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
1930         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
1931         if  that  fails by advancing the starting offset (see below) and trying         if that fails by advancing the starting offset (see below)  and  trying
1932         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
1933         this in the pcredemo.c sample program.         this in the pcredemo.c sample program.
1934    
1935           PCRE_NO_UTF8_CHECK           PCRE_NO_UTF8_CHECK
1936    
1937         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
1938         UTF-8 string is automatically checked when pcre_exec() is  subsequently         UTF-8  string is automatically checked when pcre_exec() is subsequently
1939         called.   The  value  of  startoffset is also checked to ensure that it         called.  The value of startoffset is also checked  to  ensure  that  it
1940         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
1941         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
1942         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,
1943         pcre_exec()  returns  the error PCRE_ERROR_BADUTF8. If startoffset con-         pcre_exec() returns the error PCRE_ERROR_BADUTF8. If  startoffset  con-
1944         tains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is returned.         tains an invalid value, PCRE_ERROR_BADUTF8_OFFSET is returned.
1945    
1946         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
1947         these    checks    for   performance   reasons,   you   can   set   the         these   checks   for   performance   reasons,   you   can    set    the
1948         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
1949         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
1950         making repeated calls to find all  the  matches  in  a  single  subject         making  repeated  calls  to  find  all  the matches in a single subject
1951         string.  However,  you  should  be  sure  that the value of startoffset         string. However, you should be  sure  that  the  value  of  startoffset
1952         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
1953         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
1954         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-
1955         acter, is undefined. Your program may crash.         acter, is undefined. Your program may crash.
1956    
1957           PCRE_PARTIAL           PCRE_PARTIAL
1958    
1959         This  option  turns  on  the  partial  matching feature. If the subject         This option turns on the  partial  matching  feature.  If  the  subject
1960         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-
1961         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
1962         partially matches the pattern and the failure to  match  occurred  only         partially  matches  the  pattern and the failure to match occurred only
1963         because  there were not enough subject characters), pcre_exec() returns         because there were not enough subject characters), pcre_exec()  returns
1964         PCRE_ERROR_PARTIAL instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL  is         PCRE_ERROR_PARTIAL  instead of PCRE_ERROR_NOMATCH. When PCRE_PARTIAL is
1965         used,  there  are restrictions on what may appear in the pattern. These         used, there are restrictions on what may appear in the  pattern.  These
1966         are discussed in the pcrepartial documentation.         are discussed in the pcrepartial documentation.
1967    
1968     The string to be matched by pcre_exec()     The string to be matched by pcre_exec()
1969    
1970         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
1971         length  in  length, and a starting byte offset in startoffset. In UTF-8         length in length, and a starting byte offset in startoffset.  In  UTF-8
1972         mode, the byte offset must point to the start  of  a  UTF-8  character.         mode,  the  byte  offset  must point to the start of a UTF-8 character.
1973         Unlike  the  pattern string, the subject may contain binary zero bytes.         Unlike the pattern string, the subject may contain binary  zero  bytes.
1974         When the starting offset is zero, the search for a match starts at  the         When  the starting offset is zero, the search for a match starts at the
1975         beginning of the subject, and this is by far the most common case.         beginning of the subject, and this is by far the most common case.
1976    
1977         A  non-zero  starting offset is useful when searching for another match         A non-zero starting offset is useful when searching for  another  match
1978         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-
1979         cess.   Setting  startoffset differs from just passing over a shortened         cess.  Setting startoffset differs from just passing over  a  shortened
1980         string and setting PCRE_NOTBOL in the case of  a  pattern  that  begins         string  and  setting  PCRE_NOTBOL  in the case of a pattern that begins
1981         with any kind of lookbehind. For example, consider the pattern         with any kind of lookbehind. For example, consider the pattern
1982    
1983           \Biss\B           \Biss\B
1984    
1985         which  finds  occurrences  of "iss" in the middle of words. (\B matches         which finds occurrences of "iss" in the middle of  words.  (\B  matches
1986         only if the current position in the subject is not  a  word  boundary.)         only  if  the  current position in the subject is not a word boundary.)
1987         When  applied  to the string "Mississipi" the first call to pcre_exec()         When applied to the string "Mississipi" the first call  to  pcre_exec()
1988         finds the first occurrence. If pcre_exec() is called  again  with  just         finds  the  first  occurrence. If pcre_exec() is called again with just
1989         the  remainder  of  the  subject,  namely  "issipi", it does not match,         the remainder of the subject,  namely  "issipi",  it  does  not  match,
1990         because \B is always false at the start of the subject, which is deemed         because \B is always false at the start of the subject, which is deemed
1991         to  be  a  word  boundary. However, if pcre_exec() is passed the entire         to be a word boundary. However, if pcre_exec()  is  passed  the  entire
1992         string again, but with startoffset set to 4, it finds the second occur-         string again, but with startoffset set to 4, it finds the second occur-
1993         rence  of "iss" because it is able to look behind the starting point to         rence of "iss" because it is able to look behind the starting point  to
1994         discover that it is preceded by a letter.         discover that it is preceded by a letter.
1995    
1996         If a non-zero starting offset is passed when the pattern  is  anchored,         If  a  non-zero starting offset is passed when the pattern is anchored,
1997         one attempt to match at the given offset is made. This can only succeed         one attempt to match at the given offset is made. This can only succeed
1998         if the pattern does not require the match to be at  the  start  of  the         if  the  pattern  does  not require the match to be at the start of the
1999         subject.         subject.
2000    
2001     How pcre_exec() returns captured substrings     How pcre_exec() returns captured substrings
2002    
2003         In  general, a pattern matches a certain portion of the subject, and in         In general, a pattern matches a certain portion of the subject, and  in
2004         addition, further substrings from the subject  may  be  picked  out  by         addition,  further  substrings  from  the  subject may be picked out by
2005         parts  of  the  pattern.  Following the usage in Jeffrey Friedl's book,         parts of the pattern. Following the usage  in  Jeffrey  Friedl's  book,
2006         this is called "capturing" in what follows, and the  phrase  "capturing         this  is  called "capturing" in what follows, and the phrase "capturing
2007         subpattern"  is  used for a fragment of a pattern that picks out a sub-         subpattern" is used for a fragment of a pattern that picks out  a  sub-
2008         string. PCRE supports several other kinds of  parenthesized  subpattern         string.  PCRE  supports several other kinds of parenthesized subpattern
2009         that do not cause substrings to be captured.         that do not cause substrings to be captured.
2010    
2011         Captured  substrings are returned to the caller via a vector of integer         Captured substrings are returned to the caller via a vector of  integer
2012         offsets whose address is passed in ovector. The number of  elements  in         offsets  whose  address is passed in ovector. The number of elements in
2013         the  vector is passed in ovecsize, which must be a non-negative number.         the vector is passed in ovecsize, which must be a non-negative  number.
2014         Note: this argument is NOT the size of ovector in bytes.         Note: this argument is NOT the size of ovector in bytes.
2015    
2016         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-
2017         strings,  each  substring using a pair of integers. The remaining third         strings, each substring using a pair of integers. The  remaining  third
2018         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-
2019         turing  subpatterns, and is not available for passing back information.         turing subpatterns, and is not available for passing back  information.
2020         The length passed in ovecsize should always be a multiple of three.  If         The  length passed in ovecsize should always be a multiple of three. If
2021         it is not, it is rounded down.         it is not, it is rounded down.
2022    
2023         When  a  match  is successful, information about captured substrings is         When a match is successful, information about  captured  substrings  is
2024         returned in pairs of integers, starting at the  beginning  of  ovector,         returned  in  pairs  of integers, starting at the beginning of ovector,
2025         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
2026         element of a pair is set to the offset of the first character in a sub-         element of a pair is set to the offset of the first character in a sub-
2027         string,  and  the  second  is  set to the offset of the first character         string, and the second is set to the  offset  of  the  first  character
2028         after the end of a substring. The  first  pair,  ovector[0]  and  ovec-         after  the  end  of  a  substring. The first pair, ovector[0] and ovec-
2029         tor[1],  identify  the  portion  of  the  subject string matched by the         tor[1], identify the portion of  the  subject  string  matched  by  the
2030         entire pattern. The next pair is used for the first  capturing  subpat-         entire  pattern.  The next pair is used for the first capturing subpat-
2031         tern, and so on. The value returned by pcre_exec() is one more than the         tern, and so on. The value returned by pcre_exec() is one more than the
2032         highest numbered pair that has been set. For example, if two substrings         highest numbered pair that has been set. For example, if two substrings
2033         have  been captured, the returned value is 3. If there are no capturing         have been captured, the returned value is 3. If there are no  capturing
2034         subpatterns, the return value from a successful match is 1,  indicating         subpatterns,  the return value from a successful match is 1, indicating
2035         that just the first pair of offsets has been set.         that just the first pair of offsets has been set.
2036    
2037         If a capturing subpattern is matched repeatedly, it is the last portion         If a capturing subpattern is matched repeatedly, it is the last portion
2038         of the string that it matched that is returned.         of the string that it matched that is returned.
2039    
2040         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,
2041         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
2042         function returns a value of zero. In particular, if the substring  off-         function  returns a value of zero. In particular, if the substring off-
2043         sets are not of interest, pcre_exec() may be called with ovector passed         sets are not of interest, pcre_exec() may be called with ovector passed
2044         as NULL and ovecsize as zero. However, if  the  pattern  contains  back         as  NULL  and  ovecsize  as zero. However, if the pattern contains back
2045         references  and  the  ovector is not big enough to remember the related         references and the ovector is not big enough to  remember  the  related
2046         substrings, PCRE has to get additional memory for use during  matching.         substrings,  PCRE has to get additional memory for use during matching.
2047         Thus it is usually advisable to supply an ovector.         Thus it is usually advisable to supply an ovector.
2048    
2049         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
2050         subpatterns there are in a compiled  pattern.  The  smallest  size  for         subpatterns  there  are  in  a  compiled pattern. The smallest size for
2051         ovector  that  will allow for n captured substrings, in addition to the         ovector that will allow for n captured substrings, in addition  to  the
2052         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.
2053    
2054         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
2055         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,
2056         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
2057         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
2058         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-
2059         sponding to unused subpatterns are set to -1.         sponding to unused subpatterns are set to -1.
2060    
2061         Offset  values  that correspond to unused subpatterns at the end of the         Offset values that correspond to unused subpatterns at the end  of  the
2062         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
2063         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
2064         matched. The return from the function is 2, because  the  highest  used         matched.  The  return  from the function is 2, because the highest used
2065         capturing subpattern number is 1. However, you can refer to the offsets         capturing subpattern number is 1. However, you can refer to the offsets
2066         for the second and third capturing subpatterns if  you  wish  (assuming         for  the  second  and third capturing subpatterns if you wish (assuming
2067         the vector is large enough, of course).         the vector is large enough, of course).
2068    
2069         Some  convenience  functions  are  provided for extracting the captured         Some convenience functions are provided  for  extracting  the  captured
2070         substrings as separate strings. These are described below.         substrings as separate strings. These are described below.
2071    
2072     Error return values from pcre_exec()     Error return values from pcre_exec()
2073    
2074         If pcre_exec() fails, it returns a negative number. The  following  are         If  pcre_exec()  fails, it returns a negative number. The following are
2075         defined in the header file:         defined in the header file:
2076    
2077           PCRE_ERROR_NOMATCH        (-1)           PCRE_ERROR_NOMATCH        (-1)
# Line 2006  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2080  MATCHING A PATTERN: THE TRADITIONAL FUNC
2080    
2081           PCRE_ERROR_NULL           (-2)           PCRE_ERROR_NULL           (-2)
2082    
2083         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
2084         ovecsize was not zero.         ovecsize was not zero.
2085    
2086           PCRE_ERROR_BADOPTION      (-3)           PCRE_ERROR_BADOPTION      (-3)
# Line 2015  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2089  MATCHING A PATTERN: THE TRADITIONAL FUNC
2089    
2090           PCRE_ERROR_BADMAGIC       (-4)           PCRE_ERROR_BADMAGIC       (-4)
2091    
2092         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,
2093         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
2094         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
2095         an  environment  with the other endianness. This is the error that PCRE         an environment with the other endianness. This is the error  that  PCRE
2096         gives when the magic number is not present.         gives when the magic number is not present.
2097    
2098           PCRE_ERROR_UNKNOWN_OPCODE (-5)           PCRE_ERROR_UNKNOWN_OPCODE (-5)
2099    
2100         While running the pattern match, an unknown item was encountered in the         While running the pattern match, an unknown item was encountered in the
2101         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
2102         overwriting of the compiled pattern.         overwriting of the compiled pattern.
2103    
2104           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2105    
2106         If a pattern contains back references, but the ovector that  is  passed         If  a  pattern contains back references, but the ovector that is passed
2107         to pcre_exec() is not big enough to remember the referenced substrings,         to pcre_exec() is not big enough to remember the referenced substrings,
2108         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
2109         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
2110         memory is automatically freed at the end of matching.         memory is automatically freed at the end of matching.
2111    
2112           PCRE_ERROR_NOSUBSTRING    (-7)           PCRE_ERROR_NOSUBSTRING    (-7)
2113    
2114         This error is used by the pcre_copy_substring(),  pcre_get_substring(),         This  error is used by the pcre_copy_substring(), pcre_get_substring(),
2115         and  pcre_get_substring_list()  functions  (see  below).  It  is  never         and  pcre_get_substring_list()  functions  (see  below).  It  is  never
2116         returned by pcre_exec().         returned by pcre_exec().
2117    
2118           PCRE_ERROR_MATCHLIMIT     (-8)           PCRE_ERROR_MATCHLIMIT     (-8)
2119    
2120         The backtracking limit, as specified by  the  match_limit  field  in  a         The  backtracking  limit,  as  specified  by the match_limit field in a
2121         pcre_extra  structure  (or  defaulted) was reached. See the description         pcre_extra structure (or defaulted) was reached.  See  the  description
2122         above.         above.
2123    
2124           PCRE_ERROR_CALLOUT        (-9)           PCRE_ERROR_CALLOUT        (-9)
2125    
2126         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
2127         use  by  callout functions that want to yield a distinctive error code.         use by callout functions that want to yield a distinctive  error  code.
2128         See the pcrecallout documentation for details.         See the pcrecallout documentation for details.
2129    
2130           PCRE_ERROR_BADUTF8        (-10)           PCRE_ERROR_BADUTF8        (-10)
2131    
2132         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
2133         subject.         subject.
2134    
2135           PCRE_ERROR_BADUTF8_OFFSET (-11)           PCRE_ERROR_BADUTF8_OFFSET (-11)
2136    
2137         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
2138         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-
2139         ter.         ter.
2140    
2141           PCRE_ERROR_PARTIAL        (-12)           PCRE_ERROR_PARTIAL        (-12)
2142    
2143         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
2144         pcrepartial documentation for details of partial matching.         pcrepartial documentation for details of partial matching.
2145    
2146           PCRE_ERROR_BADPARTIAL     (-13)           PCRE_ERROR_BADPARTIAL     (-13)
2147    
2148         The PCRE_PARTIAL option was used with  a  compiled  pattern  containing         The  PCRE_PARTIAL  option  was  used with a compiled pattern containing
2149         items  that are not supported for partial matching. See the pcrepartial         items that are not supported for partial matching. See the  pcrepartial
2150         documentation for details of partial matching.         documentation for details of partial matching.
2151    
2152           PCRE_ERROR_INTERNAL       (-14)           PCRE_ERROR_INTERNAL       (-14)
2153    
2154         An unexpected internal error has occurred. This error could  be  caused         An  unexpected  internal error has occurred. This error could be caused
2155         by a bug in PCRE or by overwriting of the compiled pattern.         by a bug in PCRE or by overwriting of the compiled pattern.
2156    
2157           PCRE_ERROR_BADCOUNT       (-15)           PCRE_ERROR_BADCOUNT       (-15)
2158    
2159         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.
2160    
2161           PCRE_ERROR_RECURSIONLIMIT (-21)           PCRE_ERROR_RECURSIONLIMIT (-21)
2162    
2163         The internal recursion limit, as specified by the match_limit_recursion         The internal recursion limit, as specified by the match_limit_recursion
2164         field  in  a  pcre_extra  structure (or defaulted) was reached. See the         field in a pcre_extra structure (or defaulted)  was  reached.  See  the
2165         description above.         description above.
2166    
2167           PCRE_ERROR_BADNEWLINE     (-23)           PCRE_ERROR_BADNEWLINE     (-23)
# Line 2110  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER Line 2184  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER
2184         int pcre_get_substring_list(const char *subject,         int pcre_get_substring_list(const char *subject,
2185              int *ovector, int stringcount, const char ***listptr);              int *ovector, int stringcount, const char ***listptr);
2186    
2187         Captured substrings can be  accessed  directly  by  using  the  offsets         Captured  substrings  can  be  accessed  directly  by using the offsets
2188         returned  by  pcre_exec()  in  ovector.  For convenience, the functions         returned by pcre_exec() in  ovector.  For  convenience,  the  functions
2189         pcre_copy_substring(),    pcre_get_substring(),    and    pcre_get_sub-         pcre_copy_substring(),    pcre_get_substring(),    and    pcre_get_sub-
2190         string_list()  are  provided for extracting captured substrings as new,         string_list() are provided for extracting captured substrings  as  new,
2191         separate, zero-terminated strings. These functions identify  substrings         separate,  zero-terminated strings. These functions identify substrings
2192         by  number.  The  next section describes functions for extracting named         by number. The next section describes functions  for  extracting  named
2193         substrings.         substrings.
2194    
2195         A substring that contains a binary zero is correctly extracted and  has         A  substring that contains a binary zero is correctly extracted and has
2196         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
2197         string.  However, you can process such a string  by  referring  to  the         string.   However,  you  can  process such a string by referring to the
2198         length  that  is  returned  by  pcre_copy_substring() and pcre_get_sub-         length that is  returned  by  pcre_copy_substring()  and  pcre_get_sub-
2199         string().  Unfortunately, the interface to pcre_get_substring_list() is         string().  Unfortunately, the interface to pcre_get_substring_list() is
2200         not  adequate for handling strings containing binary zeros, because the         not adequate for handling strings containing binary zeros, because  the
2201         end of the final string is not independently indicated.         end of the final string is not independently indicated.
2202    
2203         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-
2204         tions:  subject  is  the subject string that has just been successfully         tions: subject is the subject string that has  just  been  successfully
2205         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
2206         passed to pcre_exec(), and stringcount is the number of substrings that         passed to pcre_exec(), and stringcount is the number of substrings that
2207         were captured by the match, including the substring  that  matched  the         were  captured  by  the match, including the substring that matched the
2208         entire regular expression. This is the value returned by pcre_exec() if         entire regular expression. This is the value returned by pcre_exec() if
2209         it is greater than zero. If pcre_exec() returned zero, indicating  that         it  is greater than zero. If pcre_exec() returned zero, indicating that
2210         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
2211         be the number of elements in the vector divided by three.         be the number of elements in the vector divided by three.
2212    
2213         The functions pcre_copy_substring() and pcre_get_substring() extract  a         The  functions pcre_copy_substring() and pcre_get_substring() extract a
2214         single  substring,  whose  number  is given as stringnumber. A value of         single substring, whose number is given as  stringnumber.  A  value  of
2215         zero extracts the substring that matched the  entire  pattern,  whereas         zero  extracts  the  substring that matched the entire pattern, whereas
2216         higher  values  extract  the  captured  substrings.  For pcre_copy_sub-         higher values  extract  the  captured  substrings.  For  pcre_copy_sub-
2217         string(), the string is placed in buffer,  whose  length  is  given  by         string(),  the  string  is  placed  in buffer, whose length is given by
2218         buffersize,  while  for  pcre_get_substring()  a new block of memory is         buffersize, while for pcre_get_substring() a new  block  of  memory  is
2219         obtained via pcre_malloc, and its address is  returned  via  stringptr.         obtained  via  pcre_malloc,  and its address is returned via stringptr.
2220         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
2221         the terminating zero, or one of these error codes:         the terminating zero, or one of these error codes:
2222    
2223           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2224    
2225         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
2226         get memory failed for pcre_get_substring().         get memory failed for pcre_get_substring().
2227    
2228           PCRE_ERROR_NOSUBSTRING    (-7)           PCRE_ERROR_NOSUBSTRING    (-7)
2229    
2230         There is no substring whose number is stringnumber.         There is no substring whose number is stringnumber.
2231    
2232         The  pcre_get_substring_list()  function  extracts  all  available sub-         The pcre_get_substring_list()  function  extracts  all  available  sub-
2233         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
2234         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
2235         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
2236         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
2237         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
2238         error code         error code
2239    
2240           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2241    
2242         if the attempt to get the memory block failed.         if the attempt to get the memory block failed.
2243    
2244         When  any of these functions encounter a substring that is unset, which         When any of these functions encounter a substring that is unset,  which
2245         can happen when capturing subpattern number n+1 matches  some  part  of         can  happen  when  capturing subpattern number n+1 matches some part of
2246         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
2247         empty string. This can be distinguished from a genuine zero-length sub-         empty string. This can be distinguished from a genuine zero-length sub-
2248         string  by inspecting the appropriate offset in ovector, which is nega-         string by inspecting the appropriate offset in ovector, which is  nega-
2249         tive for unset substrings.         tive for unset substrings.
2250    
2251         The two convenience functions pcre_free_substring() and  pcre_free_sub-         The  two convenience functions pcre_free_substring() and pcre_free_sub-
2252         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
2253         call  of  pcre_get_substring()  or  pcre_get_substring_list(),  respec-         call  of  pcre_get_substring()  or  pcre_get_substring_list(),  respec-
2254         tively.  They  do  nothing  more  than  call the function pointed to by         tively. They do nothing more than  call  the  function  pointed  to  by
2255         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.
2256         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-
2257         cial  interface  to  another  programming  language  that  cannot   use         cial   interface  to  another  programming  language  that  cannot  use
2258         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-
2259         vided.         vided.
2260    
2261    
# Line 2200  EXTRACTING CAPTURED SUBSTRINGS BY NAME Line 2274  EXTRACTING CAPTURED SUBSTRINGS BY NAME
2274              int stringcount, const char *stringname,              int stringcount, const char *stringname,
2275              const char **stringptr);              const char **stringptr);
2276    
2277         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-
2278         ber.  For example, for this pattern         ber.  For example, for this pattern
2279    
2280           (a+)b(?<xxx>\d+)...           (a+)b(?<xxx>\d+)...
# Line 2209  EXTRACTING CAPTURED SUBSTRINGS BY NAME Line 2283  EXTRACTING CAPTURED SUBSTRINGS BY NAME
2283         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
2284         name by calling pcre_get_stringnumber(). The first argument is the com-         name by calling pcre_get_stringnumber(). The first argument is the com-
2285         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
2286         the  subpattern  number,  or PCRE_ERROR_NOSUBSTRING (-7) if there is no         the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if  there  is  no
2287         subpattern of that name.         subpattern of that name.
2288    
2289         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
2290         the functions described in the previous section. For convenience, there         the functions described in the previous section. For convenience, there
2291         are also two functions that do the whole job.         are also two functions that do the whole job.
2292    
2293         Most   of   the   arguments    of    pcre_copy_named_substring()    and         Most    of    the    arguments   of   pcre_copy_named_substring()   and
2294         pcre_get_named_substring()  are  the  same  as  those for the similarly         pcre_get_named_substring() are the same  as  those  for  the  similarly
2295         named functions that extract by number. As these are described  in  the         named  functions  that extract by number. As these are described in the
2296         previous  section,  they  are not re-described here. There are just two         previous section, they are not re-described here. There  are  just  two
2297         differences:         differences:
2298    
2299         First, instead of a substring number, a substring name is  given.  Sec-         First,  instead  of a substring number, a substring name is given. Sec-
2300         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
2301         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
2302         name-to-number translation table.         name-to-number translation table.
2303    
2304         These  functions call pcre_get_stringnumber(), and if it succeeds, they         These functions call pcre_get_stringnumber(), and if it succeeds,  they
2305         then call pcre_copy_substring() or pcre_get_substring(),  as  appropri-         then  call  pcre_copy_substring() or pcre_get_substring(), as appropri-
2306         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
2307         behaviour may not be what you want (see the next section).         behaviour may not be what you want (see the next section).
2308    
2309    
# Line 2238  DUPLICATE SUBPATTERN NAMES Line 2312  DUPLICATE SUBPATTERN NAMES
2312         int pcre_get_stringtable_entries(const pcre *code,         int pcre_get_stringtable_entries(const pcre *code,
2313              const char *name, char **first, char **last);              const char *name, char **first, char **last);
2314    
2315         When a pattern is compiled with the  PCRE_DUPNAMES  option,  names  for         When  a  pattern  is  compiled with the PCRE_DUPNAMES option, names for
2316         subpatterns  are  not  required  to  be unique. Normally, patterns with         subpatterns are not required to  be  unique.  Normally,  patterns  with
2317         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
2318         subpatterns  participates. An example is shown in the pcrepattern docu-         subpatterns participates. An example is shown in the pcrepattern  docu-
2319         mentation.         mentation.
2320    
2321         When   duplicates   are   present,   pcre_copy_named_substring()    and         When    duplicates   are   present,   pcre_copy_named_substring()   and
2322         pcre_get_named_substring()  return the first substring corresponding to         pcre_get_named_substring() return the first substring corresponding  to
2323         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
2324         (-7)  is  returned;  no  data  is returned. The pcre_get_stringnumber()         (-7) is returned; no  data  is  returned.  The  pcre_get_stringnumber()
2325         function returns one of the numbers that are associated with the  name,         function  returns one of the numbers that are associated with the name,
2326         but it is not defined which it is.         but it is not defined which it is.
2327    
2328         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
2329         name, you must use  the  pcre_get_stringtable_entries()  function.  The         name,  you  must  use  the pcre_get_stringtable_entries() function. The
2330         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
2331         third and fourth are pointers to variables which  are  updated  by  the         third  and  fourth  are  pointers to variables which are updated by the
2332         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
2333         the name-to-number table  for  the  given  name.  The  function  itself         the  name-to-number  table  for  the  given  name.  The function itself
2334         returns  the  length  of  each entry, or PCRE_ERROR_NOSUBSTRING (-7) if         returns the length of each entry,  or  PCRE_ERROR_NOSUBSTRING  (-7)  if
2335         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-
2336         tion  entitled  Information  about  a  pattern.  Given all the relevant         tion entitled Information about a  pattern.   Given  all  the  relevant
2337         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
2338         the captured data, if any.         the captured data, if any.
2339    
2340    
2341  FINDING ALL POSSIBLE MATCHES  FINDING ALL POSSIBLE MATCHES
2342    
2343         The  traditional  matching  function  uses a similar algorithm to Perl,         The traditional matching function uses a  similar  algorithm  to  Perl,
2344         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
2345         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
2346         possible match, consider using the alternative matching  function  (see         possible  match,  consider using the alternative matching function (see
2347         below)  instead.  If you cannot use the alternative function, but still         below) instead. If you cannot use the alternative function,  but  still
2348         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
2349         of the callout facility, which is described in the pcrecallout documen-         of the callout facility, which is described in the pcrecallout documen-
2350         tation.         tation.
2351    
2352         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-
2353         tern.   When your callout function is called, extract and save the cur-         tern.  When your callout function is called, extract and save the  cur-
2354         rent matched substring. Then return  1,  which  forces  pcre_exec()  to         rent  matched  substring.  Then  return  1, which forces pcre_exec() to
2355         backtrack  and  try other alternatives. Ultimately, when it runs out of         backtrack and try other alternatives. Ultimately, when it runs  out  of
2356         matches, pcre_exec() will yield PCRE_ERROR_NOMATCH.         matches, pcre_exec() will yield PCRE_ERROR_NOMATCH.
2357    
2358    
# Line 2289  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2363  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2363              int options, int *ovector, int ovecsize,              int options, int *ovector, int ovecsize,
2364              int *workspace, int wscount);              int *workspace, int wscount);
2365    
2366         The function pcre_dfa_exec()  is  called  to  match  a  subject  string         The  function  pcre_dfa_exec()  is  called  to  match  a subject string
2367         against  a  compiled pattern, using a matching algorithm that scans the         against a compiled pattern, using a matching algorithm that  scans  the
2368         subject string just once, and does not backtrack.  This  has  different         subject  string  just  once, and does not backtrack. This has different
2369         characteristics  to  the  normal  algorithm, and is not compatible with         characteristics to the normal algorithm, and  is  not  compatible  with
2370         Perl. Some of the features of PCRE patterns are not  supported.  Never-         Perl.  Some  of the features of PCRE patterns are not supported. Never-
2371         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
2372         a discussion of the two matching algorithms, see the pcrematching docu-         a discussion of the two matching algorithms, see the pcrematching docu-
2373         mentation.         mentation.
2374    
2375         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
2376         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-
2377         ent  way,  and  this is described below. The other common arguments are         ent way, and this is described below. The other  common  arguments  are
2378         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
2379         repeated here.         repeated here.
2380    
2381         The  two  additional  arguments provide workspace for the function. The         The two additional arguments provide workspace for  the  function.  The
2382         workspace vector should contain at least 20 elements. It  is  used  for         workspace  vector  should  contain at least 20 elements. It is used for
2383         keeping  track  of  multiple  paths  through  the  pattern  tree.  More         keeping  track  of  multiple  paths  through  the  pattern  tree.  More
2384         workspace will be needed for patterns and subjects where  there  are  a         workspace  will  be  needed for patterns and subjects where there are a
2385         lot of potential matches.         lot of potential matches.
2386    
2387         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 2403  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2403    
2404     Option bits for pcre_dfa_exec()     Option bits for pcre_dfa_exec()
2405    
2406         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
2407         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-
2408         LINE_xxx,  PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NO_UTF8_CHECK,         LINE_xxx, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY,  PCRE_NO_UTF8_CHECK,
2409         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
2410         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
2411         not repeated here.         not repeated here.
2412    
2413           PCRE_PARTIAL           PCRE_PARTIAL
2414    
2415         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
2416         details   are   slightly   different.  When  PCRE_PARTIAL  is  set  for         details  are  slightly  different.  When  PCRE_PARTIAL   is   set   for
2417         pcre_dfa_exec(), the return code PCRE_ERROR_NOMATCH is  converted  into         pcre_dfa_exec(),  the  return code PCRE_ERROR_NOMATCH is converted into
2418         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
2419         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-
2420         sibility.  The portion of the string that provided the partial match is         sibility. The portion of the string that provided the partial match  is
2421         set as the first matching string.         set as the first matching string.
2422    
2423           PCRE_DFA_SHORTEST           PCRE_DFA_SHORTEST
2424    
2425         Setting the PCRE_DFA_SHORTEST option causes the matching  algorithm  to         Setting  the  PCRE_DFA_SHORTEST option causes the matching algorithm to
2426         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-
2427         tive algorithm works, this is necessarily the shortest  possible  match         tive  algorithm  works, this is necessarily the shortest possible match
2428         at the first possible matching point in the subject string.         at the first possible matching point in the subject string.
2429    
2430           PCRE_DFA_RESTART           PCRE_DFA_RESTART
2431    
2432         When  pcre_dfa_exec()  is  called  with  the  PCRE_PARTIAL  option, and         When pcre_dfa_exec()  is  called  with  the  PCRE_PARTIAL  option,  and
2433         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-
2434         tional  subject  characters,  and have it continue with the same match.         tional subject characters, and have it continue with  the  same  match.
2435         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
2436         workspace  and wscount options must reference the same vector as before         workspace and wscount options must reference the same vector as  before
2437         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
2438         match.  There  is  more  discussion of this facility in the pcrepartial         match. There is more discussion of this  facility  in  the  pcrepartial
2439         documentation.         documentation.
2440    
2441     Successful returns from pcre_dfa_exec()     Successful returns from pcre_dfa_exec()
2442    
2443         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-
2444         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
2445         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
2446         matches  are all initial substrings of the longer matches. For example,         matches are all initial substrings of the longer matches. For  example,
2447         if the pattern         if the pattern
2448    
2449           <.*>           <.*>
# Line 2384  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2458  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2458           <something> <something else>           <something> <something else>
2459           <something> <something else> <something further>           <something> <something else> <something further>
2460    
2461         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,
2462         which  is  the  number of matched substrings. The substrings themselves         which is the number of matched substrings.  The  substrings  themselves
2463         are returned in ovector. Each string uses two elements;  the  first  is         are  returned  in  ovector. Each string uses two elements; the first is
2464         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
2465         fact, all the strings have the same start  offset.  (Space  could  have         fact,  all  the  strings  have the same start offset. (Space could have
2466         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
2467         compatibility with the way pcre_exec() returns data,  even  though  the         compatibility  with  the  way pcre_exec() returns data, even though the
2468         meaning of the strings is different.)         meaning of the strings is different.)
2469    
2470         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-
2471         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
2472         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
2473         filled with the longest matches.         filled with the longest matches.
2474    
2475     Error returns from pcre_dfa_exec()     Error returns from pcre_dfa_exec()
2476    
2477         The pcre_dfa_exec() function returns a negative number when  it  fails.         The  pcre_dfa_exec()  function returns a negative number when it fails.
2478         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
2479         described above.  There are in addition the following errors  that  are         described  above.   There are in addition the following errors that are
2480         specific to pcre_dfa_exec():         specific to pcre_dfa_exec():
2481    
2482           PCRE_ERROR_DFA_UITEM      (-16)           PCRE_ERROR_DFA_UITEM      (-16)
2483    
2484         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-
2485         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
2486         reference.         reference.
2487    
2488           PCRE_ERROR_DFA_UCOND      (-17)           PCRE_ERROR_DFA_UCOND      (-17)
2489    
2490         This  return  is  given  if pcre_dfa_exec() encounters a condition item         This return is given if pcre_dfa_exec()  encounters  a  condition  item
2491         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
2492         in a specific group. These are not supported.         in a specific group. These are not supported.
2493    
2494           PCRE_ERROR_DFA_UMLIMIT    (-18)           PCRE_ERROR_DFA_UMLIMIT    (-18)
2495    
2496         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
2497         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
2498         (it is meaningless).         (it is meaningless).
2499    
2500           PCRE_ERROR_DFA_WSSIZE     (-19)           PCRE_ERROR_DFA_WSSIZE     (-19)
2501    
2502         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
2503         workspace vector.         workspace vector.
2504    
2505           PCRE_ERROR_DFA_RECURSE    (-20)           PCRE_ERROR_DFA_RECURSE    (-20)
2506    
2507         When a recursive subpattern is processed, the matching  function  calls         When  a  recursive subpattern is processed, the matching function calls
2508         itself  recursively,  using  private vectors for ovector and workspace.         itself recursively, using private vectors for  ovector  and  workspace.
2509         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
2510         should be extremely rare, as a vector of size 1000 is used.         should be extremely rare, as a vector of size 1000 is used.
2511    
2512    
2513  SEE ALSO  SEE ALSO
2514    
2515         pcrebuild(3),  pcrecallout(3), pcrecpp(3)(3), pcrematching(3), pcrepar-         pcrebuild(3), pcrecallout(3), pcrecpp(3)(3), pcrematching(3),  pcrepar-
2516         tial(3), pcreposix(3), pcreprecompile(3), pcresample(3),  pcrestack(3).         tial(3),  pcreposix(3), pcreprecompile(3), pcresample(3), pcrestack(3).
2517    
2518    
2519  AUTHOR  AUTHOR
# Line 2451  AUTHOR Line 2525  AUTHOR
2525    
2526  REVISION  REVISION
2527    
2528         Last updated: 09 August 2007         Last updated: 11 September 2007
2529         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
2530  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2531    
# Line 2736  DIFFERENCES BETWEEN PCRE AND PERL Line 2810  DIFFERENCES BETWEEN PCRE AND PERL
2810         (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-
2811         TURE options for pcre_exec() have no Perl equivalents.         TURE options for pcre_exec() have no Perl equivalents.
2812    
2813         (g) The callout facility is PCRE-specific.         (g) The \R escape sequence can be restricted to match only CR,  LF,  or
2814           CRLF by the PCRE_BSR_ANYCRLF option.
2815    
2816         (h) The partial matching facility is PCRE-specific.         (h) The callout facility is PCRE-specific.
2817    
2818         (i) Patterns compiled by PCRE can be saved and re-used at a later time,         (i) The partial matching facility is PCRE-specific.
2819    
2820           (j) Patterns compiled by PCRE can be saved and re-used at a later time,
2821         even on different hosts that have the other endianness.         even on different hosts that have the other endianness.
2822    
2823         (j)  The  alternative  matching function (pcre_dfa_exec()) matches in a         (k) The alternative matching function (pcre_dfa_exec())  matches  in  a
2824         different way and is not Perl-compatible.         different way and is not Perl-compatible.
2825    
2826           (l)  PCRE  recognizes some special sequences such as (*CR) at the start
2827           of a pattern that set overall options that cannot be changed within the
2828           pattern.
2829    
2830    
2831  AUTHOR  AUTHOR
2832    
# Line 2756  AUTHOR Line 2837  AUTHOR
2837    
2838  REVISION  REVISION
2839    
2840         Last updated: 08 August 2007         Last updated: 11 September 2007
2841         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
2842  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2843    
# Line 2797  PCRE REGULAR EXPRESSION DETAILS Line 2878  PCRE REGULAR EXPRESSION DETAILS
2878         discussed in the pcrematching page.         discussed in the pcrematching page.
2879    
2880    
2881    NEWLINE CONVENTIONS
2882    
2883           PCRE  supports five different conventions for indicating line breaks in
2884           strings: a single CR (carriage return) character, a  single  LF  (line-
2885           feed) character, the two-character sequence CRLF, any of the three pre-
2886           ceding, or any Unicode newline sequence. The pcreapi page  has  further
2887           discussion  about newlines, and shows how to set the newline convention
2888           in the options arguments for the compiling and matching functions.
2889    
2890           It is also possible to specify a newline convention by starting a  pat-
2891           tern string with one of the following five sequences:
2892    
2893             (*CR)        carriage return
2894             (*LF)        linefeed
2895             (*CRLF)      carriage return, followed by linefeed
2896             (*ANYCRLF)   any of the three above
2897             (*ANY)       all Unicode newline sequences
2898    
2899           These override the default and the options given to pcre_compile(). For
2900           example, on a Unix system where LF is the default newline sequence, the
2901           pattern
2902    
2903             (*CR)a.b
2904    
2905           changes the convention to CR. That pattern matches "a\nb" because LF is
2906           no longer a newline. Note that these special settings,  which  are  not
2907           Perl-compatible,  are  recognized  only at the very start of a pattern,
2908           and that they must be in upper case.  If  more  than  one  of  them  is
2909           present, the last one is used.
2910    
2911           The  newline  convention  does  not  affect what the \R escape sequence
2912           matches. By default, this is any Unicode  newline  sequence,  for  Perl
2913           compatibility.  However, this can be changed; see the description of \R
2914           in the section entitled "Newline sequences" below. A change of \R  set-
2915           ting can be combined with a change of newline convention.
2916    
2917    
2918  CHARACTERS AND METACHARACTERS  CHARACTERS AND METACHARACTERS
2919    
2920         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 3022  BACKSLASH
3022           \cx       "control-x", where x is any character           \cx       "control-x", where x is any character
3023           \e        escape (hex 1B)           \e        escape (hex 1B)
3024           \f        formfeed (hex 0C)           \f        formfeed (hex 0C)
3025           \n        newline (hex 0A)           \n        linefeed (hex 0A)
3026           \r        carriage return (hex 0D)           \r        carriage return (hex 0D)
3027           \t        tab (hex 09)           \t        tab (hex 09)
3028           \ddd      character with octal code ddd, or backreference           \ddd      character with octal code ddd, or backreference
# Line 3069  BACKSLASH Line 3187  BACKSLASH
3187    
3188     Newline sequences     Newline sequences
3189    
3190         Outside a character class, the escape sequence \R matches  any  Unicode         Outside a character class, by default, the escape sequence  \R  matches
3191         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
3192         equivalent to the following:         mode \R is equivalent to the following:
3193    
3194           (?>\r\n|\n|\x0b|\f|\r|\x85)           (?>\r\n|\n|\x0b|\f|\r|\x85)
3195    
# Line 3087  BACKSLASH Line 3205  BACKSLASH
3205         rator,  U+2029).   Unicode character property support is not needed for         rator,  U+2029).   Unicode character property support is not needed for
3206         these characters to be recognized.         these characters to be recognized.
3207    
3208           It is possible to restrict \R to match only CR, LF, or CRLF (instead of
3209           the  complete  set  of  Unicode  line  endings)  by  setting the option
3210           PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched.
3211           (BSR is an abbrevation for "backslash R".) This can be made the default
3212           when PCRE is built; if this is the case, the  other  behaviour  can  be
3213           requested  via  the  PCRE_BSR_UNICODE  option.   It is also possible to
3214           specify these settings by starting a pattern string  with  one  of  the
3215           following sequences:
3216    
3217             (*BSR_ANYCRLF)   CR, LF, or CRLF only
3218             (*BSR_UNICODE)   any Unicode newline sequence
3219    
3220           These override the default and the options given to pcre_compile(), but
3221           they can be overridden by options given to pcre_exec(). Note that these
3222           special settings, which are not Perl-compatible, are recognized only at
3223           the very start of a pattern, and that they must be in  upper  case.  If
3224           more  than  one  of  them is present, the last one is used. They can be
3225           combined with a change of newline convention, for  example,  a  pattern
3226           can start with:
3227    
3228             (*ANY)(*BSR_ANYCRLF)
3229    
3230         Inside a character class, \R matches the letter "R".         Inside a character class, \R matches the letter "R".
3231    
3232     Unicode character properties     Unicode character properties
3233    
3234         When PCRE is built with Unicode character property support, three addi-         When PCRE is built with Unicode character property support, three addi-
3235         tional  escape sequences that match characters with specific properties         tional escape sequences that match characters with specific  properties
3236         are available.  When not in UTF-8 mode, these sequences are  of  course         are  available.   When not in UTF-8 mode, these sequences are of course
3237         limited  to  testing characters whose codepoints are less than 256, but         limited to testing characters whose codepoints are less than  256,  but
3238         they do work in this mode.  The extra escape sequences are:         they do work in this mode.  The extra escape sequences are:
3239    
3240           \p{xx}   a character with the xx property           \p{xx}   a character with the xx property
3241           \P{xx}   a character without the xx property           \P{xx}   a character without the xx property
3242           \X       an extended Unicode sequence           \X       an extended Unicode sequence
3243    
3244         The property names represented by xx above are limited to  the  Unicode         The  property  names represented by xx above are limited to the Unicode
3245         script names, the general category properties, and "Any", which matches         script names, the general category properties, and "Any", which matches
3246         any character (including newline). Other properties such as "InMusical-         any character (including newline). Other properties such as "InMusical-
3247         Symbols"  are  not  currently supported by PCRE. Note that \P{Any} does         Symbols" are not currently supported by PCRE. Note  that  \P{Any}  does
3248         not match any characters, so always causes a match failure.         not match any characters, so always causes a match failure.
3249    
3250         Sets of Unicode characters are defined as belonging to certain scripts.         Sets of Unicode characters are defined as belonging to certain scripts.
3251         A  character from one of these sets can be matched using a script name.         A character from one of these sets can be matched using a script  name.
3252         For example:         For example:
3253    
3254           \p{Greek}           \p{Greek}
3255           \P{Han}           \P{Han}
3256    
3257         Those that are not part of an identified script are lumped together  as         Those  that are not part of an identified script are lumped together as
3258         "Common". The current list of scripts is:         "Common". The current list of scripts is:
3259    
3260         Arabic,  Armenian,  Balinese,  Bengali,  Bopomofo,  Braille,  Buginese,         Arabic,  Armenian,  Balinese,  Bengali,  Bopomofo,  Braille,  Buginese,
3261         Buhid,  Canadian_Aboriginal,  Cherokee,  Common,   Coptic,   Cuneiform,         Buhid,   Canadian_Aboriginal,   Cherokee,  Common,  Coptic,  Cuneiform,
3262         Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic,         Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic,
3263         Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew,  Hira-         Gothic,  Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hira-
3264         gana,  Inherited,  Kannada,  Katakana,  Kharoshthi,  Khmer, Lao, Latin,         gana, Inherited, Kannada,  Katakana,  Kharoshthi,  Khmer,  Lao,  Latin,
3265         Limbu,  Linear_B,  Malayalam,  Mongolian,  Myanmar,  New_Tai_Lue,  Nko,         Limbu,  Linear_B,  Malayalam,  Mongolian,  Myanmar,  New_Tai_Lue,  Nko,
3266         Ogham,  Old_Italic,  Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician,         Ogham, Old_Italic, Old_Persian, Oriya, Osmanya,  Phags_Pa,  Phoenician,
3267         Runic,  Shavian,  Sinhala,  Syloti_Nagri,  Syriac,  Tagalog,  Tagbanwa,         Runic,  Shavian,  Sinhala,  Syloti_Nagri,  Syriac,  Tagalog,  Tagbanwa,
3268         Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi.         Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi.
3269    
3270         Each  character has exactly one general category property, specified by         Each character has exactly one general category property, specified  by
3271         a two-letter abbreviation. For compatibility with Perl, negation can be         a two-letter abbreviation. For compatibility with Perl, negation can be
3272         specified  by  including a circumflex between the opening brace and the         specified by including a circumflex between the opening brace  and  the
3273         property name. For example, \p{^Lu} is the same as \P{Lu}.         property name. For example, \p{^Lu} is the same as \P{Lu}.
3274    
3275         If only one letter is specified with \p or \P, it includes all the gen-         If only one letter is specified with \p or \P, it includes all the gen-
3276         eral  category properties that start with that letter. In this case, in         eral category properties that start with that letter. In this case,  in
3277         the absence of negation, the curly brackets in the escape sequence  are         the  absence of negation, the curly brackets in the escape sequence are
3278         optional; these two examples have the same effect:         optional; these two examples have the same effect:
3279    
3280           \p{L}           \p{L}
# Line 3186  BACKSLASH Line 3326  BACKSLASH
3326           Zp    Paragraph separator           Zp    Paragraph separator
3327           Zs    Space separator           Zs    Space separator
3328    
3329         The  special property L& is also supported: it matches a character that         The special property L& is also supported: it matches a character  that
3330         has the Lu, Ll, or Lt property, in other words, a letter  that  is  not         has  the  Lu,  Ll, or Lt property, in other words, a letter that is not
3331         classified as a modifier or "other".         classified as a modifier or "other".
3332    
3333         The  Cs  (Surrogate)  property  applies only to characters in the range         The Cs (Surrogate) property applies only to  characters  in  the  range
3334         U+D800 to U+DFFF. Such characters are not valid in UTF-8  strings  (see         U+D800  to  U+DFFF. Such characters are not valid in UTF-8 strings (see
3335         RFC 3629) and so cannot be tested by PCRE, unless UTF-8 validity check-         RFC 3629) and so cannot be tested by PCRE, unless UTF-8 validity check-
3336         ing has been turned off (see the discussion  of  PCRE_NO_UTF8_CHECK  in         ing  has  been  turned off (see the discussion of PCRE_NO_UTF8_CHECK in
3337         the pcreapi page).         the pcreapi page).
3338    
3339         The  long  synonyms  for  these  properties that Perl supports (such as         The long synonyms for these properties  that  Perl  supports  (such  as
3340         \p{Letter}) are not supported by PCRE, nor is it  permitted  to  prefix         \p{Letter})  are  not  supported by PCRE, nor is it permitted to prefix
3341         any of these properties with "Is".         any of these properties with "Is".
3342    
3343         No character that is in the Unicode table has the Cn (unassigned) prop-         No character that is in the Unicode table has the Cn (unassigned) prop-
3344         erty.  Instead, this property is assumed for any code point that is not         erty.  Instead, this property is assumed for any code point that is not
3345         in the Unicode table.         in the Unicode table.
3346    
3347         Specifying  caseless  matching  does not affect these escape sequences.         Specifying caseless matching does not affect  these  escape  sequences.
3348         For example, \p{Lu} always matches only upper case letters.         For example, \p{Lu} always matches only upper case letters.
3349    
3350         The \X escape matches any number of Unicode  characters  that  form  an         The  \X  escape  matches  any number of Unicode characters that form an
3351         extended Unicode sequence. \X is equivalent to         extended Unicode sequence. \X is equivalent to
3352    
3353           (?>\PM\pM*)           (?>\PM\pM*)
3354    
3355         That  is,  it matches a character without the "mark" property, followed         That is, it matches a character without the "mark"  property,  followed
3356         by zero or more characters with the "mark"  property,  and  treats  the         by  zero  or  more  characters with the "mark" property, and treats the
3357         sequence  as  an  atomic group (see below).  Characters with the "mark"         sequence as an atomic group (see below).  Characters  with  the  "mark"
3358         property are typically accents that  affect  the  preceding  character.         property  are  typically  accents  that affect the preceding character.
3359         None  of  them  have  codepoints less than 256, so in non-UTF-8 mode \X         None of them have codepoints less than 256, so  in  non-UTF-8  mode  \X
3360         matches any one character.         matches any one character.
3361    
3362         Matching characters by Unicode property is not fast, because  PCRE  has         Matching  characters  by Unicode property is not fast, because PCRE has
3363         to  search  a  structure  that  contains data for over fifteen thousand         to search a structure that contains  data  for  over  fifteen  thousand
3364         characters. That is why the traditional escape sequences such as \d and         characters. That is why the traditional escape sequences such as \d and
3365         \w do not use Unicode properties in PCRE.         \w do not use Unicode properties in PCRE.
3366    
3367     Resetting the match start     Resetting the match start
3368    
3369         The escape sequence \K, which is a Perl 5.10 feature, causes any previ-         The escape sequence \K, which is a Perl 5.10 feature, causes any previ-
3370         ously matched characters not  to  be  included  in  the  final  matched         ously  matched  characters  not  to  be  included  in the final matched
3371         sequence. For example, the pattern:         sequence. For example, the pattern:
3372    
3373           foo\Kbar           foo\Kbar
3374    
3375         matches  "foobar",  but reports that it has matched "bar". This feature         matches "foobar", but reports that it has matched "bar".  This  feature
3376         is similar to a lookbehind assertion (described  below).   However,  in         is  similar  to  a lookbehind assertion (described below).  However, in
3377         this  case, the part of the subject before the real match does not have         this case, the part of the subject before the real match does not  have
3378         to be of fixed length, as lookbehind assertions do. The use of \K  does         to  be of fixed length, as lookbehind assertions do. The use of \K does
3379         not  interfere  with  the setting of captured substrings.  For example,         not interfere with the setting of captured  substrings.   For  example,
3380         when the pattern         when the pattern
3381    
3382           (foo)\Kbar           (foo)\Kbar
# Line 3245  BACKSLASH Line 3385  BACKSLASH
3385    
3386     Simple assertions     Simple assertions
3387    
3388         The final use of backslash is for certain simple assertions. An  asser-         The  final use of backslash is for certain simple assertions. An asser-
3389         tion  specifies a condition that has to be met at a particular point in         tion specifies a condition that has to be met at a particular point  in
3390         a match, without consuming any characters from the subject string.  The         a  match, without consuming any characters from the subject string. The
3391         use  of subpatterns for more complicated assertions is described below.         use of subpatterns for more complicated assertions is described  below.
3392         The backslashed assertions are:         The backslashed assertions are:
3393    
3394           \b     matches at a word boundary           \b     matches at a word boundary
# Line 3259  BACKSLASH Line 3399  BACKSLASH
3399           \z     matches only at the end of the subject           \z     matches only at the end of the subject
3400           \G     matches at the first matching position in the subject           \G     matches at the first matching position in the subject
3401    
3402         These assertions may not appear in character classes (but note that  \b         These  assertions may not appear in character classes (but note that \b
3403         has a different meaning, namely the backspace character, inside a char-         has a different meaning, namely the backspace character, inside a char-
3404         acter class).         acter class).
3405    
3406         A word boundary is a position in the subject string where  the  current         A  word  boundary is a position in the subject string where the current
3407         character  and  the previous character do not both match \w or \W (i.e.         character and the previous character do not both match \w or  \W  (i.e.
3408         one matches \w and the other matches \W), or the start or  end  of  the         one  matches  \w  and the other matches \W), or the start or end of the
3409         string if the first or last character matches \w, respectively.         string if the first or last character matches \w, respectively.
3410    
3411         The  \A,  \Z,  and \z assertions differ from the traditional circumflex         The \A, \Z, and \z assertions differ from  the  traditional  circumflex
3412         and dollar (described in the next section) in that they only ever match         and dollar (described in the next section) in that they only ever match
3413         at  the  very start and end of the subject string, whatever options are         at the very start and end of the subject string, whatever  options  are
3414         set. Thus, they are independent of multiline mode. These  three  asser-         set.  Thus,  they are independent of multiline mode. These three asser-
3415         tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which         tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which
3416         affect only the behaviour of the circumflex and dollar  metacharacters.         affect  only the behaviour of the circumflex and dollar metacharacters.
3417         However,  if the startoffset argument of pcre_exec() is non-zero, indi-         However, if the startoffset argument of pcre_exec() is non-zero,  indi-
3418         cating that matching is to start at a point other than the beginning of         cating that matching is to start at a point other than the beginning of
3419         the  subject,  \A  can never match. The difference between \Z and \z is         the subject, \A can never match. The difference between \Z  and  \z  is
3420         that \Z matches before a newline at the end of the string as well as at         that \Z matches before a newline at the end of the string as well as at
3421         the very end, whereas \z matches only at the end.         the very end, whereas \z matches only at the end.
3422    
3423         The  \G assertion is true only when the current matching position is at         The \G assertion is true only when the current matching position is  at
3424         the start point of the match, as specified by the startoffset  argument         the  start point of the match, as specified by the startoffset argument
3425         of  pcre_exec().  It  differs  from \A when the value of startoffset is         of pcre_exec(). It differs from \A when the  value  of  startoffset  is
3426         non-zero. By calling pcre_exec() multiple times with appropriate  argu-         non-zero.  By calling pcre_exec() multiple times with appropriate argu-
3427         ments, you can mimic Perl's /g option, and it is in this kind of imple-         ments, you can mimic Perl's /g option, and it is in this kind of imple-
3428         mentation where \G can be useful.         mentation where \G can be useful.
3429    
3430         Note, however, that PCRE's interpretation of \G, as the  start  of  the         Note,  however,  that  PCRE's interpretation of \G, as the start of the
3431         current match, is subtly different from Perl's, which defines it as the         current match, is subtly different from Perl's, which defines it as the
3432         end of the previous match. In Perl, these can  be  different  when  the         end  of  the  previous  match. In Perl, these can be different when the
3433         previously  matched  string was empty. Because PCRE does just one match         previously matched string was empty. Because PCRE does just  one  match
3434         at a time, it cannot reproduce this behaviour.         at a time, it cannot reproduce this behaviour.
3435    
3436         If all the alternatives of a pattern begin with \G, the  expression  is         If  all  the alternatives of a pattern begin with \G, the expression is
3437         anchored to the starting match position, and the "anchored" flag is set         anchored to the starting match position, and the "anchored" flag is set
3438         in the compiled regular expression.         in the compiled regular expression.
3439    
# Line 3301  BACKSLASH Line 3441  BACKSLASH
3441  CIRCUMFLEX AND DOLLAR  CIRCUMFLEX AND DOLLAR
3442    
3443         Outside a character class, in the default matching mode, the circumflex         Outside a character class, in the default matching mode, the circumflex
3444         character  is  an  assertion  that is true only if the current matching         character is an assertion that is true only  if  the  current  matching
3445         point is at the start of the subject string. If the  startoffset  argu-         point  is  at the start of the subject string. If the startoffset argu-
3446         ment  of  pcre_exec()  is  non-zero,  circumflex can never match if the         ment of pcre_exec() is non-zero, circumflex  can  never  match  if  the
3447         PCRE_MULTILINE option is unset. Inside a  character  class,  circumflex         PCRE_MULTILINE  option  is  unset. Inside a character class, circumflex
3448         has an entirely different meaning (see below).         has an entirely different meaning (see below).
3449    
3450         Circumflex  need  not be the first character of the pattern if a number         Circumflex need not be the first character of the pattern if  a  number
3451         of alternatives are involved, but it should be the first thing in  each         of  alternatives are involved, but it should be the first thing in each
3452         alternative  in  which  it appears if the pattern is ever to match that         alternative in which it appears if the pattern is ever  to  match  that
3453         branch. If all possible alternatives start with a circumflex, that  is,         branch.  If all possible alternatives start with a circumflex, that is,
3454         if  the  pattern  is constrained to match only at the start of the sub-         if the pattern is constrained to match only at the start  of  the  sub-
3455         ject, it is said to be an "anchored" pattern.  (There  are  also  other         ject,  it  is  said  to be an "anchored" pattern. (There are also other
3456         constructs that can cause a pattern to be anchored.)         constructs that can cause a pattern to be anchored.)
3457    
3458         A  dollar  character  is  an assertion that is true only if the current         A dollar character is an assertion that is true  only  if  the  current
3459         matching point is at the end of  the  subject  string,  or  immediately         matching  point  is  at  the  end of the subject string, or immediately
3460         before a newline at the end of the string (by default). Dollar need not         before a newline at the end of the string (by default). Dollar need not
3461         be the last character of the pattern if a number  of  alternatives  are         be  the  last  character of the pattern if a number of alternatives are
3462         involved,  but  it  should  be  the last item in any branch in which it         involved, but it should be the last item in  any  branch  in  which  it
3463         appears. Dollar has no special meaning in a character class.         appears. Dollar has no special meaning in a character class.
3464    
3465         The meaning of dollar can be changed so that it  matches  only  at  the         The  meaning  of  dollar  can be changed so that it matches only at the
3466         very  end  of  the string, by setting the PCRE_DOLLAR_ENDONLY option at         very end of the string, by setting the  PCRE_DOLLAR_ENDONLY  option  at
3467         compile time. This does not affect the \Z assertion.         compile time. This does not affect the \Z assertion.
3468    
3469         The meanings of the circumflex and dollar characters are changed if the         The meanings of the circumflex and dollar characters are changed if the
3470         PCRE_MULTILINE  option  is  set.  When  this  is the case, a circumflex         PCRE_MULTILINE option is set. When  this  is  the  case,  a  circumflex
3471         matches immediately after internal newlines as well as at the start  of         matches  immediately after internal newlines as well as at the start of
3472         the  subject  string.  It  does not match after a newline that ends the         the subject string. It does not match after a  newline  that  ends  the
3473         string. A dollar matches before any newlines in the string, as well  as         string.  A dollar matches before any newlines in the string, as well as
3474         at  the very end, when PCRE_MULTILINE is set. When newline is specified         at the very end, when PCRE_MULTILINE is set. When newline is  specified
3475         as the two-character sequence CRLF, isolated CR and  LF  characters  do         as  the  two-character  sequence CRLF, isolated CR and LF characters do
3476         not indicate newlines.         not indicate newlines.
3477    
3478         For  example, the pattern /^abc$/ matches the subject string "def\nabc"         For example, the pattern /^abc$/ matches the subject string  "def\nabc"
3479         (where \n represents a newline) in multiline mode, but  not  otherwise.         (where  \n  represents a newline) in multiline mode, but not otherwise.
3480         Consequently,  patterns  that  are anchored in single line mode because         Consequently, patterns that are anchored in single  line  mode  because
3481         all branches start with ^ are not anchored in  multiline  mode,  and  a         all  branches  start  with  ^ are not anchored in multiline mode, and a
3482         match  for  circumflex  is  possible  when  the startoffset argument of         match for circumflex is  possible  when  the  startoffset  argument  of
3483         pcre_exec() is non-zero. The PCRE_DOLLAR_ENDONLY option is  ignored  if         pcre_exec()  is  non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if
3484         PCRE_MULTILINE is set.         PCRE_MULTILINE is set.
3485    
3486         Note  that  the sequences \A, \Z, and \z can be used to match the start         Note that the sequences \A, \Z, and \z can be used to match  the  start
3487         and end of the subject in both modes, and if all branches of a  pattern         and  end of the subject in both modes, and if all branches of a pattern
3488         start  with  \A it is always anchored, whether or not PCRE_MULTILINE is         start with \A it is always anchored, whether or not  PCRE_MULTILINE  is
3489         set.         set.
3490    
3491    
3492  FULL STOP (PERIOD, DOT)  FULL STOP (PERIOD, DOT)
3493    
3494         Outside a character class, a dot in the pattern matches any one charac-         Outside a character class, a dot in the pattern matches any one charac-
3495         ter  in  the subject string except (by default) a character that signi-         ter in the subject string except (by default) a character  that  signi-
3496         fies the end of a line. In UTF-8 mode, the  matched  character  may  be         fies  the  end  of  a line. In UTF-8 mode, the matched character may be
3497         more than one byte long.         more than one byte long.
3498    
3499         When  a line ending is defined as a single character, dot never matches         When a line ending is defined as a single character, dot never  matches
3500         that character; when the two-character sequence CRLF is used, dot  does         that  character; when the two-character sequence CRLF is used, dot does
3501         not  match  CR  if  it  is immediately followed by LF, but otherwise it         not match CR if it is immediately followed  by  LF,  but  otherwise  it
3502         matches all characters (including isolated CRs and LFs). When any  Uni-         matches  all characters (including isolated CRs and LFs). When any Uni-
3503         code  line endings are being recognized, dot does not match CR or LF or         code line endings are being recognized, dot does not match CR or LF  or
3504         any of the other line ending characters.         any of the other line ending characters.
3505    
3506         The behaviour of dot with regard to newlines can  be  changed.  If  the         The  behaviour  of  dot  with regard to newlines can be changed. If the
3507         PCRE_DOTALL  option  is  set,  a dot matches any one character, without         PCRE_DOTALL option is set, a dot matches  any  one  character,  without
3508         exception. If the two-character sequence CRLF is present in the subject         exception. If the two-character sequence CRLF is present in the subject
3509         string, it takes two dots to match it.         string, it takes two dots to match it.
3510    
3511         The  handling of dot is entirely independent of the handling of circum-         The handling of dot is entirely independent of the handling of  circum-
3512         flex and dollar, the only relationship being  that  they  both  involve         flex  and  dollar,  the  only relationship being that they both involve
3513         newlines. Dot has no special meaning in a character class.         newlines. Dot has no special meaning in a character class.
3514    
3515    
3516  MATCHING A SINGLE BYTE  MATCHING A SINGLE BYTE
3517    
3518         Outside a character class, the escape sequence \C matches any one byte,         Outside a character class, the escape sequence \C matches any one byte,
3519         both in and out of UTF-8 mode. Unlike a  dot,  it  always  matches  any         both  in  and  out  of  UTF-8 mode. Unlike a dot, it always matches any
3520         line-ending  characters.  The  feature  is provided in Perl in order to         line-ending characters. The feature is provided in  Perl  in  order  to
3521         match individual bytes in UTF-8 mode. Because it breaks up UTF-8  char-         match  individual bytes in UTF-8 mode. Because it breaks up UTF-8 char-
3522         acters  into individual bytes, what remains in the string may be a mal-         acters into individual bytes, what remains in the string may be a  mal-
3523         formed UTF-8 string. For this reason, the \C escape  sequence  is  best         formed  UTF-8  string.  For this reason, the \C escape sequence is best
3524         avoided.         avoided.
3525    
3526         PCRE  does  not  allow \C to appear in lookbehind assertions (described         PCRE does not allow \C to appear in  lookbehind  assertions  (described
3527         below), because in UTF-8 mode this would make it impossible  to  calcu-         below),  because  in UTF-8 mode this would make it impossible to calcu-
3528         late the length of the lookbehind.         late the length of the lookbehind.
3529    
3530    
# Line 3393  SQUARE BRACKETS AND CHARACTER CLASSES Line 3533  SQUARE BRACKETS AND CHARACTER CLASSES
3533         An opening square bracket introduces a character class, terminated by a         An opening square bracket introduces a character class, terminated by a
3534         closing square bracket. A closing square bracket on its own is not spe-         closing square bracket. A closing square bracket on its own is not spe-
3535         cial. If a closing square bracket is required as a member of the class,         cial. If a closing square bracket is required as a member of the class,
3536         it should be the first data character in the class  (after  an  initial         it  should  be  the first data character in the class (after an initial
3537         circumflex, if present) or escaped with a backslash.         circumflex, if present) or escaped with a backslash.
3538    
3539         A  character  class matches a single character in the subject. In UTF-8         A character class matches a single character in the subject.  In  UTF-8
3540         mode, the character may occupy more than one byte. A matched  character         mode,  the character may occupy more than one byte. A matched character
3541         must be in the set of characters defined by the class, unless the first         must be in the set of characters defined by the class, unless the first
3542         character in the class definition is a circumflex, in  which  case  the         character  in  the  class definition is a circumflex, in which case the
3543         subject  character  must  not  be in the set defined by the class. If a         subject character must not be in the set defined by  the  class.  If  a
3544         circumflex is actually required as a member of the class, ensure it  is         circumflex  is actually required as a member of the class, ensure it is
3545         not the first character, or escape it with a backslash.         not the first character, or escape it with a backslash.
3546    
3547         For  example, the character class [aeiou] matches any lower case vowel,         For example, the character class [aeiou] matches any lower case  vowel,
3548         while [^aeiou] matches any character that is not a  lower  case  vowel.         while  [^aeiou]  matches  any character that is not a lower case vowel.
3549         Note that a circumflex is just a convenient notation for specifying the         Note that a circumflex is just a convenient notation for specifying the
3550         characters that are in the class by enumerating those that are  not.  A         characters  that  are in the class by enumerating those that are not. A
3551         class  that starts with a circumflex is not an assertion: it still con-         class that starts with a circumflex is not an assertion: it still  con-
3552         sumes a character from the subject string, and therefore  it  fails  if         sumes  a  character  from the subject string, and therefore it fails if
3553         the current pointer is at the end of the string.         the current pointer is at the end of the string.
3554    
3555         In  UTF-8 mode, characters with values greater than 255 can be included         In UTF-8 mode, characters with values greater than 255 can be  included
3556         in a class as a literal string of bytes, or by using the  \x{  escaping         in  a  class as a literal string of bytes, or by using the \x{ escaping
3557         mechanism.         mechanism.
3558    
3559         When  caseless  matching  is set, any letters in a class represent both         When caseless matching is set, any letters in a  class  represent  both
3560         their upper case and lower case versions, so for  example,  a  caseless         their  upper  case  and lower case versions, so for example, a caseless
3561         [aeiou]  matches  "A"  as well as "a", and a caseless [^aeiou] does not         [aeiou] matches "A" as well as "a", and a caseless  [^aeiou]  does  not
3562         match "A", whereas a caseful version would. In UTF-8 mode, PCRE  always         match  "A", whereas a caseful version would. In UTF-8 mode, PCRE always
3563         understands  the  concept  of case for characters whose values are less         understands the concept of case for characters whose  values  are  less
3564         than 128, so caseless matching is always possible. For characters  with         than  128, so caseless matching is always possible. For characters with
3565         higher  values,  the  concept  of case is supported if PCRE is compiled         higher values, the concept of case is supported  if  PCRE  is  compiled
3566         with Unicode property support, but not otherwise.  If you want  to  use         with  Unicode  property support, but not otherwise.  If you want to use
3567         caseless  matching  for  characters 128 and above, you must ensure that         caseless matching for characters 128 and above, you  must  ensure  that
3568         PCRE is compiled with Unicode property support as well  as  with  UTF-8         PCRE  is  compiled  with Unicode property support as well as with UTF-8
3569         support.         support.
3570    
3571         Characters  that  might  indicate  line breaks are never treated in any         Characters that might indicate line breaks are  never  treated  in  any
3572         special way  when  matching  character  classes,  whatever  line-ending         special  way  when  matching  character  classes,  whatever line-ending
3573         sequence  is  in  use,  and  whatever  setting  of  the PCRE_DOTALL and         sequence is in  use,  and  whatever  setting  of  the  PCRE_DOTALL  and
3574         PCRE_MULTILINE options is used. A class such as [^a] always matches one         PCRE_MULTILINE options is used. A class such as [^a] always matches one
3575         of these characters.         of these characters.
3576    
3577         The  minus (hyphen) character can be used to specify a range of charac-         The minus (hyphen) character can be used to specify a range of  charac-
3578         ters in a character  class.  For  example,  [d-m]  matches  any  letter         ters  in  a  character  class.  For  example,  [d-m] matches any letter
3579         between  d  and  m,  inclusive.  If  a minus character is required in a         between d and m, inclusive. If a  minus  character  is  required  in  a
3580         class, it must be escaped with a backslash  or  appear  in  a  position         class,  it  must  be  escaped  with a backslash or appear in a position
3581         where  it cannot be interpreted as indicating a range, typically as the         where it cannot be interpreted as indicating a range, typically as  the
3582         first or last character in the class.         first or last character in the class.
3583    
3584         It is not possible to have the literal character "]" as the end charac-         It is not possible to have the literal character "]" as the end charac-
3585         ter  of a range. A pattern such as [W-]46] is interpreted as a class of         ter of a range. A pattern such as [W-]46] is interpreted as a class  of
3586         two characters ("W" and "-") followed by a literal string "46]", so  it         two  characters ("W" and "-") followed by a literal string "46]", so it
3587         would  match  "W46]"  or  "-46]". However, if the "]" is escaped with a         would match "W46]" or "-46]". However, if the "]"  is  escaped  with  a
3588         backslash it is interpreted as the end of range, so [W-\]46] is  inter-         backslash  it is interpreted as the end of range, so [W-\]46] is inter-
3589         preted  as a class containing a range followed by two other characters.         preted as a class containing a range followed by two other  characters.
3590         The octal or hexadecimal representation of "]" can also be used to  end         The  octal or hexadecimal representation of "]" can also be used to end
3591         a range.         a range.
3592    
3593         Ranges  operate in the collating sequence of character values. They can         Ranges operate in the collating sequence of character values. They  can
3594         also  be  used  for  characters  specified  numerically,  for   example         also   be  used  for  characters  specified  numerically,  for  example
3595         [\000-\037].  In UTF-8 mode, ranges can include characters whose values         [\000-\037]. In UTF-8 mode, ranges can include characters whose  values
3596         are greater than 255, for example [\x{100}-\x{2ff}].         are greater than 255, for example [\x{100}-\x{2ff}].
3597    
3598         If a range that includes letters is used when caseless matching is set,         If a range that includes letters is used when caseless matching is set,
3599         it matches the letters in either case. For example, [W-c] is equivalent         it matches the letters in either case. For example, [W-c] is equivalent
3600         to [][\\^_`wxyzabc], matched caselessly,  and  in  non-UTF-8  mode,  if         to  [][\\^_`wxyzabc],  matched  caselessly,  and  in non-UTF-8 mode, if
3601         character  tables  for  a French locale are in use, [\xc8-\xcb] matches         character tables for a French locale are in  use,  [\xc8-\xcb]  matches
3602         accented E characters in both cases. In UTF-8 mode, PCRE  supports  the         accented  E  characters in both cases. In UTF-8 mode, PCRE supports the
3603         concept  of  case for characters with values greater than 128 only when         concept of case for characters with values greater than 128  only  when
3604         it is compiled with Unicode property support.         it is compiled with Unicode property support.
3605    
3606         The character types \d, \D, \p, \P, \s, \S, \w, and \W may also  appear         The  character types \d, \D, \p, \P, \s, \S, \w, and \W may also appear
3607         in  a  character  class,  and add the characters that they match to the         in a character class, and add the characters that  they  match  to  the
3608         class. For example, [\dABCDEF] matches any hexadecimal digit. A circum-         class. For example, [\dABCDEF] matches any hexadecimal digit. A circum-
3609         flex  can  conveniently  be used with the upper case character types to         flex can conveniently be used with the upper case  character  types  to
3610         specify a more restricted set of characters  than  the  matching  lower         specify  a  more  restricted  set of characters than the matching lower
3611         case  type.  For example, the class [^\W_] matches any letter or digit,         case type. For example, the class [^\W_] matches any letter  or  digit,
3612         but not underscore.         but not underscore.
3613    
3614         The only metacharacters that are recognized in  character  classes  are         The  only  metacharacters  that are recognized in character classes are
3615         backslash,  hyphen  (only  where  it can be interpreted as specifying a         backslash, hyphen (only where it can be  interpreted  as  specifying  a
3616         range), circumflex (only at the start), opening  square  bracket  (only         range),  circumflex  (only  at the start), opening square bracket (only
3617         when  it can be interpreted as introducing a POSIX class name - see the         when it can be interpreted as introducing a POSIX class name - see  the
3618         next section), and the terminating  closing  square  bracket.  However,         next  section),  and  the  terminating closing square bracket. However,
3619         escaping other non-alphanumeric characters does no harm.         escaping other non-alphanumeric characters does no harm.
3620    
3621    
3622  POSIX CHARACTER CLASSES  POSIX CHARACTER CLASSES
3623    
3624         Perl supports the POSIX notation for character classes. This uses names         Perl supports the POSIX notation for character classes. This uses names
3625         enclosed by [: and :] within the enclosing square brackets.  PCRE  also         enclosed  by  [: and :] within the enclosing square brackets. PCRE also
3626         supports this notation. For example,         supports this notation. For example,
3627    
3628           [01[:alpha:]%]           [01[:alpha:]%]
# Line 3505  POSIX CHARACTER CLASSES Line 3645  POSIX CHARACTER CLASSES
3645           word     "word" characters (same as \w)           word     "word" characters (same as \w)
3646           xdigit   hexadecimal digits           xdigit   hexadecimal digits
3647    
3648         The "space" characters are HT (9), LF (10), VT (11), FF (12), CR  (13),         The  "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13),
3649         and  space  (32). Notice that this list includes the VT character (code         and space (32). Notice that this list includes the VT  character  (code
3650         11). This makes "space" different to \s, which does not include VT (for         11). This makes "space" different to \s, which does not include VT (for
3651         Perl compatibility).         Perl compatibility).
3652    
3653         The  name  "word"  is  a Perl extension, and "blank" is a GNU extension         The name "word" is a Perl extension, and "blank"  is  a  GNU  extension
3654         from Perl 5.8. Another Perl extension is negation, which  is  indicated         from  Perl  5.8. Another Perl extension is negation, which is indicated
3655         by a ^ character after the colon. For example,         by a ^ character after the colon. For example,
3656    
3657           [12[:^digit:]]           [12[:^digit:]]
3658    
3659         matches  "1", "2", or any non-digit. PCRE (and Perl) also recognize the         matches "1", "2", or any non-digit. PCRE (and Perl) also recognize  the
3660         POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but         POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but
3661         these are not supported, and an error is given if they are encountered.         these are not supported, and an error is given if they are encountered.
3662    
# Line 3526  POSIX CHARACTER CLASSES Line 3666  POSIX CHARACTER CLASSES
3666    
3667  VERTICAL BAR  VERTICAL BAR
3668    
3669         Vertical  bar characters are used to separate alternative patterns. For         Vertical bar characters are used to separate alternative patterns.  For
3670         example, the pattern         example, the pattern
3671    
3672           gilbert|sullivan           gilbert|sullivan
3673    
3674         matches either "gilbert" or "sullivan". Any number of alternatives  may         matches  either "gilbert" or "sullivan". Any number of alternatives may
3675         appear,  and  an  empty  alternative  is  permitted (matching the empty         appear, and an empty  alternative  is  permitted  (matching  the  empty
3676         string). The matching process tries each alternative in turn, from left         string). The matching process tries each alternative in turn, from left
3677         to  right, and the first one that succeeds is used. If the alternatives         to right, and the first one that succeeds is used. If the  alternatives
3678         are within a subpattern (defined below), "succeeds" means matching  the         are  within a subpattern (defined below), "succeeds" means matching the
3679         rest  of the main pattern as well as the alternative in the subpattern.         rest of the main pattern as well as the alternative in the  subpattern.
3680    
3681    
3682  INTERNAL OPTION SETTING  INTERNAL OPTION SETTING
3683    
3684         The settings of the  PCRE_CASELESS,  PCRE_MULTILINE,  PCRE_DOTALL,  and         The  settings  of  the  PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
3685         PCRE_EXTENDED  options  can  be  changed  from  within the pattern by a         PCRE_EXTENDED options (which are Perl-compatible) can be  changed  from
3686         sequence of Perl option letters enclosed  between  "(?"  and  ")".  The         within  the  pattern  by  a  sequence  of  Perl option letters enclosed
3687         option letters are         between "(?" and ")".  The option letters are
3688    
3689           i  for PCRE_CASELESS           i  for PCRE_CASELESS
3690           m  for PCRE_MULTILINE           m  for PCRE_MULTILINE
# Line 3553  INTERNAL OPTION SETTING Line 3693  INTERNAL OPTION SETTING
3693    
3694         For example, (?im) sets caseless, multiline matching. It is also possi-         For example, (?im) sets caseless, multiline matching. It is also possi-
3695         ble to unset these options by preceding the letter with a hyphen, and a         ble to unset these options by preceding the letter with a hyphen, and a
3696         combined  setting and unsetting such as (?im-sx), which sets PCRE_CASE-         combined setting and unsetting such as (?im-sx), which sets  PCRE_CASE-
3697         LESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and  PCRE_EXTENDED,         LESS  and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED,
3698         is  also  permitted.  If  a  letter  appears  both before and after the         is also permitted. If a  letter  appears  both  before  and  after  the
3699         hyphen, the option is unset.         hyphen, the option is unset.
3700    
3701         When an option change occurs at top level (that is, not inside  subpat-         The  PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA
3702         tern  parentheses),  the change applies to the remainder of the pattern         can be changed in the same way as the Perl-compatible options by  using
3703           the characters J, U and X respectively.
3704    
3705           When  an option change occurs at top level (that is, not inside subpat-
3706           tern parentheses), the change applies to the remainder of  the  pattern
3707         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,
3708         PCRE extracts it into the global options (and it will therefore show up         PCRE extracts it into the global options (and it will therefore show up
3709         in data extracted by the pcre_fullinfo() function).         in data extracted by the pcre_fullinfo() function).
3710    
3711         An option change within a subpattern (see below for  a  description  of         An  option  change  within a subpattern (see below for a description of
3712         subpatterns) affects only that part of the current pattern that follows         subpatterns) affects only that part of the current pattern that follows
3713         it, so         it, so
3714    
3715           (a(?i)b)c           (a(?i)b)c
3716    
3717         matches abc and aBc and no other strings (assuming PCRE_CASELESS is not         matches abc and aBc and no other strings (assuming PCRE_CASELESS is not
3718         used).   By  this means, options can be made to have different settings         used).  By this means, options can be made to have  different  settings
3719         in different parts of the pattern. Any changes made in one  alternative         in  different parts of the pattern. Any changes made in one alternative
3720         do  carry  on  into subsequent branches within the same subpattern. For         do carry on into subsequent branches within the  same  subpattern.  For
3721         example,         example,
3722    
3723           (a(?i)b|c)           (a(?i)b|c)
3724    
3725         matches "ab", "aB", "c", and "C", even though  when  matching  "C"  the         matches  "ab",  "aB",  "c",  and "C", even though when matching "C" the
3726         first  branch  is  abandoned before the option setting. This is because         first branch is abandoned before the option setting.  This  is  because
3727         the effects of option settings happen at compile time. There  would  be         the  effects  of option settings happen at compile time. There would be
3728         some very weird behaviour otherwise.         some very weird behaviour otherwise.
3729    
        The  PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA  
        can be changed in the same way as the Perl-compatible options by  using  
        the characters J, U and X respectively.  
   
3730    
3731  SUBPATTERNS  SUBPATTERNS
3732    
# Line 3597  SUBPATTERNS Line 3737  SUBPATTERNS
3737    
3738           cat(aract|erpillar|)           cat(aract|erpillar|)
3739    
3740         matches one of the words "cat", "cataract", or  "caterpillar".  Without         matches  one  of the words "cat", "cataract", or "caterpillar". Without
3741         the  parentheses,  it  would  match  "cataract", "erpillar" or an empty         the parentheses, it would match  "cataract",  "erpillar"  or  an  empty
3742         string.         string.
3743    
3744         2. It sets up the subpattern as  a  capturing  subpattern.  This  means         2.  It  sets  up  the  subpattern as a capturing subpattern. This means
3745         that,  when  the  whole  pattern  matches,  that portion of the subject         that, when the whole pattern  matches,  that  portion  of  the  subject
3746         string that matched the subpattern is passed back to the caller via the         string that matched the subpattern is passed back to the caller via the
3747         ovector  argument  of pcre_exec(). Opening parentheses are counted from         ovector argument of pcre_exec(). Opening parentheses are  counted  from
3748         left to right (starting from 1) to obtain  numbers  for  the  capturing         left  to  right  (starting  from 1) to obtain numbers for the capturing
3749         subpatterns.         subpatterns.
3750    
3751         For  example,  if the string "the red king" is matched against the pat-         For example, if the string "the red king" is matched against  the  pat-
3752         tern         tern
3753    
3754           the ((red|white) (king|queen))           the ((red|white) (king|queen))
# Line 3616  SUBPATTERNS Line 3756  SUBPATTERNS
3756         the captured substrings are "red king", "red", and "king", and are num-         the captured substrings are "red king", "red", and "king", and are num-
3757         bered 1, 2, and 3, respectively.         bered 1, 2, and 3, respectively.
3758    
3759         The  fact  that  plain  parentheses  fulfil two functions is not always         The fact that plain parentheses fulfil  two  functions  is  not  always
3760         helpful.  There are often times when a grouping subpattern is  required         helpful.   There are often times when a grouping subpattern is required
3761         without  a capturing requirement. If an opening parenthesis is followed         without a capturing requirement. If an opening parenthesis is  followed
3762         by a question mark and a colon, the subpattern does not do any  captur-         by  a question mark and a colon, the subpattern does not do any captur-
3763         ing,  and  is  not  counted when computing the number of any subsequent         ing, and is not counted when computing the  number  of  any  subsequent
3764         capturing subpatterns. For example, if the string "the white queen"  is         capturing  subpatterns. For example, if the string "the white queen" is
3765         matched against the pattern         matched against the pattern
3766    
3767           the ((?:red|white) (king|queen))           the ((?:red|white) (king|queen))
# Line 3629  SUBPATTERNS Line 3769  SUBPATTERNS
3769         the captured substrings are "white queen" and "queen", and are numbered         the captured substrings are "white queen" and "queen", and are numbered
3770         1 and 2. The maximum number of capturing subpatterns is 65535.         1 and 2. The maximum number of capturing subpatterns is 65535.
3771    
3772         As a convenient shorthand, if any option settings are required  at  the         As  a  convenient shorthand, if any option settings are required at the
3773         start  of  a  non-capturing  subpattern,  the option letters may appear         start of a non-capturing subpattern,  the  option  letters  may  appear
3774         between the "?" and the ":". Thus the two patterns         between the "?" and the ":". Thus the two patterns
3775    
3776           (?i:saturday|sunday)           (?i:saturday|sunday)
3777           (?:(?i)saturday|sunday)           (?:(?i)saturday|sunday)
3778    
3779         match exactly the same set of strings. Because alternative branches are         match exactly the same set of strings. Because alternative branches are
3780         tried  from  left  to right, and options are not reset until the end of         tried from left to right, and options are not reset until  the  end  of
3781         the subpattern is reached, an option setting in one branch does  affect         the  subpattern is reached, an option setting in one branch does affect
3782         subsequent  branches,  so  the above patterns match "SUNDAY" as well as         subsequent branches, so the above patterns match "SUNDAY"  as  well  as
3783         "Saturday".         "Saturday".
3784    
3785    
3786  DUPLICATE SUBPATTERN NUMBERS  DUPLICATE SUBPATTERN NUMBERS
3787    
3788         Perl 5.10 introduced a feature whereby each alternative in a subpattern         Perl 5.10 introduced a feature whereby each alternative in a subpattern
3789         uses  the same numbers for its capturing parentheses. Such a subpattern         uses the same numbers for its capturing parentheses. Such a  subpattern
3790         starts with (?| and is itself a non-capturing subpattern. For  example,         starts  with (?| and is itself a non-capturing subpattern. For example,
3791         consider this pattern:         consider this pattern:
3792    
3793           (?|(Sat)ur|(Sun))day           (?|(Sat)ur|(Sun))day
3794    
3795         Because  the two alternatives are inside a (?| group, both sets of cap-         Because the two alternatives are inside a (?| group, both sets of  cap-
3796         turing parentheses are numbered one. Thus, when  the  pattern  matches,         turing  parentheses  are  numbered one. Thus, when the pattern matches,
3797         you  can  look  at captured substring number one, whichever alternative         you can look at captured substring number  one,  whichever  alternative
3798         matched. This construct is useful when you want to  capture  part,  but         matched.  This  construct  is useful when you want to capture part, but
3799         not all, of one of a number of alternatives. Inside a (?| group, paren-         not all, of one of a number of alternatives. Inside a (?| group, paren-
3800         theses are numbered as usual, but the number is reset at the  start  of         theses  are  numbered as usual, but the number is reset at the start of
3801         each  branch. The numbers of any capturing buffers that follow the sub-         each branch. The numbers of any capturing buffers that follow the  sub-
3802         pattern start after the highest number used in any branch. The  follow-         pattern  start after the highest number used in any branch. The follow-
3803         ing  example  is taken from the Perl documentation.  The numbers under-         ing example is taken from the Perl documentation.  The  numbers  under-
3804         neath show in which buffer the captured content will be stored.         neath show in which buffer the captured content will be stored.
3805    
3806           # before  ---------------branch-reset----------- after           # before  ---------------branch-reset----------- after
3807           / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x           / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
3808           # 1            2         2  3        2     3     4           # 1            2         2  3        2     3     4
3809    
3810         A backreference or a recursive call to  a  numbered  subpattern  always         A  backreference  or  a  recursive call to a numbered subpattern always
3811         refers to the first one in the pattern with the given number.         refers to the first one in the pattern with the given number.
3812    
3813         An  alternative approach to using this "branch reset" feature is to use         An alternative approach to using this "branch reset" feature is to  use
3814         duplicate named subpatterns, as described in the next section.         duplicate named subpatterns, as described in the next section.
3815    
3816    
3817  NAMED SUBPATTERNS  NAMED SUBPATTERNS
3818    
3819         Identifying capturing parentheses by number is simple, but  it  can  be         Identifying  capturing  parentheses  by number is simple, but it can be
3820         very  hard  to keep track of the numbers in complicated regular expres-         very hard to keep track of the numbers in complicated  regular  expres-
3821         sions. Furthermore, if an  expression  is  modified,  the  numbers  may         sions.  Furthermore,  if  an  expression  is  modified, the numbers may
3822         change.  To help with this difficulty, PCRE supports the naming of sub-         change. To help with this difficulty, PCRE supports the naming of  sub-
3823         patterns. This feature was not added to Perl until release 5.10. Python         patterns. This feature was not added to Perl until release 5.10. Python
3824         had  the  feature earlier, and PCRE introduced it at release 4.0, using         had the feature earlier, and PCRE introduced it at release  4.0,  using
3825         the Python syntax. PCRE now supports both the Perl and the Python  syn-         the  Python syntax. PCRE now supports both the Perl and the Python syn-
3826         tax.         tax.
3827    
3828         In  PCRE,  a subpattern can be named in one of three ways: (?<name>...)         In PCRE, a subpattern can be named in one of three  ways:  (?<name>...)
3829         or (?'name'...) as in Perl, or (?P<name>...) as in  Python.  References         or  (?'name'...)  as in Perl, or (?P<name>...) as in Python. References
3830         to capturing parentheses from other parts of the pattern, such as back-         to capturing parentheses from other parts of the pattern, such as back-
3831         references, recursion, and conditions, can be made by name as  well  as         references,  recursion,  and conditions, can be made by name as well as
3832         by number.         by number.
3833    
3834         Names  consist  of  up  to  32 alphanumeric characters and underscores.         Names consist of up to  32  alphanumeric  characters  and  underscores.
3835         Named capturing parentheses are still  allocated  numbers  as  well  as         Named  capturing  parentheses  are  still  allocated numbers as well as
3836         names,  exactly as if the names were not present. The PCRE API provides         names, exactly as if the names were not present. The PCRE API  provides
3837         function calls for extracting the name-to-number translation table from         function calls for extracting the name-to-number translation table from
3838         a compiled pattern. There is also a convenience function for extracting         a compiled pattern. There is also a convenience function for extracting
3839         a captured substring by name.         a captured substring by name.
3840    
3841         By default, a name must be unique within a pattern, but it is  possible         By  default, a name must be unique within a pattern, but it is possible
3842         to relax this constraint by setting the PCRE_DUPNAMES option at compile         to relax this constraint by setting the PCRE_DUPNAMES option at compile
3843         time. This can be useful for patterns where only one  instance  of  the         time.  This  can  be useful for patterns where only one instance of the
3844         named  parentheses  can  match. Suppose you want to match the name of a         named parentheses can match. Suppose you want to match the  name  of  a
3845         weekday, either as a 3-letter abbreviation or as the full name, and  in         weekday,  either as a 3-letter abbreviation or as the full name, and in
3846         both cases you want to extract the abbreviation. This pattern (ignoring         both cases you want to extract the abbreviation. This pattern (ignoring
3847         the line breaks) does the job:         the line breaks) does the job:
3848    
# Line 3712  NAMED SUBPATTERNS Line 3852  NAMED SUBPATTERNS
3852           (?<DN>Thu)(?:rsday)?|           (?<DN>Thu)(?:rsday)?|
3853           (?<DN>Sat)(?:urday)?           (?<DN>Sat)(?:urday)?
3854    
3855         There are five capturing substrings, but only one is ever set  after  a         There  are  five capturing substrings, but only one is ever set after a
3856         match.  (An alternative way of solving this problem is to use a "branch         match.  (An alternative way of solving this problem is to use a "branch
3857         reset" subpattern, as described in the previous section.)         reset" subpattern, as described in the previous section.)
3858    
3859         The convenience function for extracting the data by  name  returns  the         The  convenience  function  for extracting the data by name returns the
3860         substring  for  the first (and in this example, the only) subpattern of         substring for the first (and in this example, the only)  subpattern  of
3861         that name that matched. This saves searching  to  find  which  numbered         that  name  that  matched.  This saves searching to find which numbered
3862         subpattern  it  was. If you make a reference to a non-unique named sub-         subpattern it was. If you make a reference to a non-unique  named  sub-
3863         pattern from elsewhere in the pattern, the one that corresponds to  the         pattern  from elsewhere in the pattern, the one that corresponds to the
3864         lowest  number  is used. For further details of the interfaces for han-         lowest number is used. For further details of the interfaces  for  han-
3865         dling named subpatterns, see the pcreapi documentation.         dling named subpatterns, see the pcreapi documentation.
3866    
3867    
3868  REPETITION  REPETITION
3869    
3870         Repetition is specified by quantifiers, which can  follow  any  of  the         Repetition  is  specified  by  quantifiers, which can follow any of the
3871         following items:         following items:
3872    
3873           a literal data character           a literal data character
# Line 3740  REPETITION Line 3880  REPETITION
3880           a back reference (see next section)           a back reference (see next section)
3881           a parenthesized subpattern (unless it is an assertion)           a parenthesized subpattern (unless it is an assertion)
3882    
3883         The  general repetition quantifier specifies a minimum and maximum num-         The general repetition quantifier specifies a minimum and maximum  num-
3884         ber of permitted matches, by giving the two numbers in  curly  brackets         ber  of  permitted matches, by giving the two numbers in curly brackets
3885         (braces),  separated  by  a comma. The numbers must be less than 65536,         (braces), separated by a comma. The numbers must be  less  than  65536,
3886         and the first must be less than or equal to the second. For example:         and the first must be less than or equal to the second. For example:
3887    
3888           z{2,4}           z{2,4}
3889    
3890         matches "zz", "zzz", or "zzzz". A closing brace on its  own  is  not  a         matches  "zz",  "zzz",  or  "zzzz". A closing brace on its own is not a
3891         special  character.  If  the second number is omitted, but the comma is         special character. If the second number is omitted, but  the  comma  is
3892         present, there is no upper limit; if the second number  and  the  comma         present,  there  is  no upper limit; if the second number and the comma
3893         are  both omitted, the quantifier specifies an exact number of required         are both omitted, the quantifier specifies an exact number of  required
3894         matches. Thus         matches. Thus
3895    
3896           [aeiou]{3,}           [aeiou]{3,}
# Line 3759  REPETITION Line 3899  REPETITION
3899    
3900           \d{8}           \d{8}
3901    
3902         matches exactly 8 digits. An opening curly bracket that  appears  in  a         matches  exactly  8  digits. An opening curly bracket that appears in a
3903         position  where a quantifier is not allowed, or one that does not match         position where a quantifier is not allowed, or one that does not  match
3904         the syntax of a quantifier, is taken as a literal character. For  exam-         the  syntax of a quantifier, is taken as a literal character. For exam-
3905         ple, {,6} is not a quantifier, but a literal string of four characters.         ple, {,6} is not a quantifier, but a literal string of four characters.
3906    
3907         In UTF-8 mode, quantifiers apply to UTF-8  characters  rather  than  to         In  UTF-8  mode,  quantifiers  apply to UTF-8 characters rather than to
3908         individual bytes. Thus, for example, \x{100}{2} matches two UTF-8 char-         individual bytes. Thus, for example, \x{100}{2} matches two UTF-8 char-
3909         acters, each of which is represented by a two-byte sequence. Similarly,         acters, each of which is represented by a two-byte sequence. Similarly,
3910         when Unicode property support is available, \X{3} matches three Unicode         when Unicode property support is available, \X{3} matches three Unicode
3911         extended sequences, each of which may be several bytes long  (and  they         extended  sequences,  each of which may be several bytes long (and they
3912         may be of different lengths).         may be of different lengths).
3913    
3914         The quantifier {0} is permitted, causing the expression to behave as if         The quantifier {0} is permitted, causing the expression to behave as if
3915         the previous item and the quantifier were not present.         the previous item and the quantifier were not present.
3916    
3917         For convenience, the three most common quantifiers have  single-charac-         For  convenience, the three most common quantifiers have single-charac-
3918         ter abbreviations:         ter abbreviations:
3919    
3920           *    is equivalent to {0,}           *    is equivalent to {0,}
3921           +    is equivalent to {1,}           +    is equivalent to {1,}
3922           ?    is equivalent to {0,1}           ?    is equivalent to {0,1}
3923    
3924         It  is  possible  to construct infinite loops by following a subpattern         It is possible to construct infinite loops by  following  a  subpattern
3925         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,
3926         for example:         for example:
3927    
3928           (a?)*           (a?)*
3929    
3930         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
3931         for such patterns. However, because there are cases where this  can  be         for  such  patterns. However, because there are cases where this can be
3932         useful,  such  patterns  are now accepted, but if any repetition of the         useful, such patterns are now accepted, but if any  repetition  of  the
3933         subpattern does in fact match no characters, the loop is forcibly  bro-         subpattern  does in fact match no characters, the loop is forcibly bro-
3934         ken.         ken.
3935    
3936         By  default,  the quantifiers are "greedy", that is, they match as much         By default, the quantifiers are "greedy", that is, they match  as  much
3937         as possible (up to the maximum  number  of  permitted  times),  without         as  possible  (up  to  the  maximum number of permitted times), without
3938         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
3939         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
3940         appear  between  /*  and  */ and within the comment, individual * and /         appear between /* and */ and within the comment,  individual  *  and  /
3941         characters may appear. An attempt to match C comments by  applying  the         characters  may  appear. An attempt to match C comments by applying the
3942         pattern         pattern
3943    
3944           /\*.*\*/           /\*.*\*/
# Line 3807  REPETITION Line 3947  REPETITION
3947    
3948           /* first comment */  not comment  /* second comment */           /* first comment */  not comment  /* second comment */
3949    
3950         fails,  because it matches the entire string owing to the greediness of         fails, because it matches the entire string owing to the greediness  of
3951         the .*  item.         the .*  item.
3952    
3953         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
3954         be greedy, and instead matches the minimum number of times possible, so         be greedy, and instead matches the minimum number of times possible, so
3955         the pattern         the pattern
3956    
3957           /\*.*?\*/           /\*.*?\*/
3958    
3959         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
3960         quantifiers  is  not  otherwise  changed,  just the preferred number of         quantifiers is not otherwise changed,  just  the  preferred  number  of
3961         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
3962         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
3963         appear doubled, as in         appear doubled, as in
3964    
3965           \d??\d           \d??\d
# Line 3827  REPETITION Line 3967  REPETITION
3967         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
3968         only way the rest of the pattern matches.         only way the rest of the pattern matches.
3969    
3970         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
3971         Perl), the quantifiers are not greedy by default, but  individual  ones         Perl),  the  quantifiers are not greedy by default, but individual ones
3972         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
3973         words, it inverts the default behaviour.         words, it inverts the default behaviour.
3974    
3975         When a parenthesized subpattern is quantified  with  a  minimum  repeat         When  a  parenthesized  subpattern  is quantified with a minimum repeat
3976         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
3977         required for the compiled pattern, in proportion to  the  size  of  the         required  for  the  compiled  pattern, in proportion to the size of the
3978         minimum or maximum.         minimum or maximum.
3979    
3980         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-
3981         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,
3982         the  pattern  is  implicitly anchored, because whatever follows will be         the pattern is implicitly anchored, because whatever  follows  will  be
3983         tried against every character position in the subject string, so  there         tried  against every character position in the subject string, so there
3984         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
3985         first. PCRE normally treats such a pattern as though it  were  preceded         first.  PCRE  normally treats such a pattern as though it were preceded
3986         by \A.         by \A.
3987    
3988         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-
3989         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-
3990         mization, or alternatively using ^ to indicate anchoring explicitly.         mization, or alternatively using ^ to indicate anchoring explicitly.
3991    
3992         However,  there is one situation where the optimization cannot be used.         However, there is one situation where the optimization cannot be  used.
3993         When .*  is inside capturing parentheses that  are  the  subject  of  a         When  .*   is  inside  capturing  parentheses that are the subject of a
3994         backreference  elsewhere  in the pattern, a match at the start may fail         backreference elsewhere in the pattern, a match at the start  may  fail
3995         where a later one succeeds. Consider, for example:         where a later one succeeds. Consider, for example:
3996    
3997           (.*)abc\1           (.*)abc\1
3998    
3999         If the subject is "xyz123abc123" the match point is the fourth  charac-         If  the subject is "xyz123abc123" the match point is the fourth charac-
4000         ter. For this reason, such a pattern is not implicitly anchored.         ter. For this reason, such a pattern is not implicitly anchored.
4001    
4002         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 4005  REPETITION
4005           (tweedle[dume]{3}\s*)+           (tweedle[dume]{3}\s*)+
4006    
4007         has matched "tweedledum tweedledee" the value of the captured substring         has matched "tweedledum tweedledee" the value of the captured substring
4008         is  "tweedledee".  However,  if there are nested capturing subpatterns,         is "tweedledee". However, if there are  nested  capturing  subpatterns,
4009         the corresponding captured values may have been set in previous  itera-         the  corresponding captured values may have been set in previous itera-
4010         tions. For example, after         tions. For example, after
4011    
4012           /(a|(b))+/           /(a|(b))+/
# Line 3876  REPETITION Line 4016  REPETITION
4016    
4017  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS
4018    
4019         With  both  maximizing ("greedy") and minimizing ("ungreedy" or "lazy")         With both maximizing ("greedy") and minimizing ("ungreedy"  or  "lazy")
4020         repetition, failure of what follows normally causes the  repeated  item         repetition,  failure  of what follows normally causes the repeated item
4021         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
4022         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,
4023         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
4024         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
4025         no point in carrying on.         no point in carrying on.
4026    
4027         Consider,  for  example, the pattern \d+foo when applied to the subject         Consider, for example, the pattern \d+foo when applied to  the  subject
4028         line         line
4029    
4030           123456bar           123456bar
4031    
4032         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
4033         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
4034         \d+ item, and then with  4,  and  so  on,  before  ultimately  failing.         \d+  item,  and  then  with  4,  and  so on, before ultimately failing.
4035         "Atomic  grouping"  (a  term taken from Jeffrey Friedl's book) provides         "Atomic grouping" (a term taken from Jeffrey  Friedl's  book)  provides
4036         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
4037         to be re-evaluated in this way.         to be re-evaluated in this way.
4038    
4039         If  we  use atomic grouping for the previous example, the matcher gives         If we use atomic grouping for the previous example, the  matcher  gives
4040         up immediately on failing to match "foo" the first time.  The  notation         up  immediately  on failing to match "foo" the first time. The notation
4041         is a kind of special parenthesis, starting with (?> as in this example:         is a kind of special parenthesis, starting with (?> as in this example:
4042    
4043           (?>\d+)foo           (?>\d+)foo
4044    
4045         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-
4046         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
4047         prevented from backtracking into it. Backtracking past it  to  previous         prevented  from  backtracking into it. Backtracking past it to previous
4048         items, however, works as normal.         items, however, works as normal.
4049    
4050         An  alternative  description  is that a subpattern of this type matches         An alternative description is that a subpattern of  this  type  matches
4051         the string of characters that an  identical  standalone  pattern  would         the  string  of  characters  that an identical standalone pattern would
4052         match, if anchored at the current point in the subject string.         match, if anchored at the current point in the subject string.
4053    
4054         Atomic grouping subpatterns are not capturing subpatterns. Simple cases         Atomic grouping subpatterns are not capturing subpatterns. Simple cases
4055         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
4056         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-
4057         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
4058         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
4059         digits.         digits.
4060    
4061         Atomic groups in general can of course contain arbitrarily  complicated         Atomic  groups in general can of course contain arbitrarily complicated
4062         subpatterns,  and  can  be  nested. However, when the subpattern for an         subpatterns, and can be nested. However, when  the  subpattern  for  an
4063         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
4064         simpler  notation,  called  a "possessive quantifier" can be used. This         simpler notation, called a "possessive quantifier" can  be  used.  This
4065         consists of an additional + character  following  a  quantifier.  Using         consists  of  an  additional  + character following a quantifier. Using
4066         this notation, the previous example can be rewritten as         this notation, the previous example can be rewritten as
4067    
4068           \d++foo           \d++foo
# Line 3932  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE Line 4072  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE
4072    
4073           (abc|xyz){2,3}+           (abc|xyz){2,3}+
4074    
4075         Possessive  quantifiers  are  always  greedy;  the   setting   of   the         Possessive   quantifiers   are   always  greedy;  the  setting  of  the
4076         PCRE_UNGREEDY option is ignored. They are a convenient notation for the         PCRE_UNGREEDY option is ignored. They are a convenient notation for the
4077         simpler forms of atomic group. However, there is no difference  in  the         simpler  forms  of atomic group. However, there is no difference in the
4078         meaning  of  a  possessive  quantifier and the equivalent atomic group,         meaning of a possessive quantifier and  the  equivalent  atomic  group,
4079         though there may be a performance  difference;  possessive  quantifiers         though  there  may  be a performance difference; possessive quantifiers
4080         should be slightly faster.         should be slightly faster.
4081    
4082         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-
4083         tax.  Jeffrey Friedl originated the idea (and the name)  in  the  first         tax.   Jeffrey  Friedl  originated the idea (and the name) in the first
4084         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
4085         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
4086         found its way into Perl at release 5.10.         found its way into Perl at release 5.10.
4087    
4088         PCRE has an optimization that automatically "possessifies" certain sim-         PCRE has an optimization that automatically "possessifies" certain sim-
4089         ple pattern constructs. For example, the sequence  A+B  is  treated  as         ple  pattern  constructs.  For  example, the sequence A+B is treated as
4090         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
4091         when B must follow.         when B must follow.
4092    
4093         When a pattern contains an unlimited repeat inside  a  subpattern  that         When  a  pattern  contains an unlimited repeat inside a subpattern that
4094         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
4095         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
4096         very long time indeed. The pattern         very long time indeed. The pattern
4097    
4098           (\D+|<\d+>)*[!?]           (\D+|<\d+>)*[!?]
4099    
4100         matches  an  unlimited number of substrings that either consist of non-         matches an unlimited number of substrings that either consist  of  non-
4101         digits, or digits enclosed in <>, followed by either ! or  ?.  When  it         digits,  or  digits  enclosed in <>, followed by either ! or ?. When it
4102         matches, it runs quickly. However, if it is applied to         matches, it runs quickly. However, if it is applied to
4103    
4104           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
4105    
4106         it  takes  a  long  time  before reporting failure. This is because the         it takes a long time before reporting  failure.  This  is  because  the
4107         string can be divided between the internal \D+ repeat and the  external         string  can be divided between the internal \D+ repeat and the external
4108         *  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
4109         example uses [!?] rather than a single character at  the  end,  because         example  uses  [!?]  rather than a single character at the end, because
4110         both  PCRE  and  Perl have an optimization that allows for fast failure         both PCRE and Perl have an optimization that allows  for  fast  failure
4111         when a single character is used. They remember the last single  charac-         when  a single character is used. They remember the last single charac-
4112         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
4113         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
4114         group, like this:         group, like this:
4115    
4116           ((?>\D+)|<\d+>)*[!?]           ((?>\D+)|<\d+>)*[!?]
4117    
4118         sequences  of non-digits cannot be broken, and failure happens quickly.         sequences of non-digits cannot be broken, and failure happens  quickly.
4119    
4120    
4121  BACK REFERENCES  BACK REFERENCES
4122    
4123         Outside a character class, a backslash followed by a digit greater than         Outside a character class, a backslash followed by a digit greater than
4124         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-
4125         pattern earlier (that is, to its left) in the pattern,  provided  there         pattern  earlier  (that is, to its left) in the pattern, provided there
4126         have been that many previous capturing left parentheses.         have been that many previous capturing left parentheses.
4127    
4128         However, if the decimal number following the backslash is less than 10,         However, if the decimal number following the backslash is less than 10,
4129         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
4130         there  are  not that many capturing left parentheses in the entire pat-         there are not that many capturing left parentheses in the  entire  pat-
4131         tern. In other words, the parentheses that are referenced need  not  be         tern.  In  other words, the parentheses that are referenced need not be
4132         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
4133         reference" of this type can make sense when a  repetition  is  involved         reference"  of  this  type can make sense when a repetition is involved
4134         and  the  subpattern to the right has participated in an earlier itera-         and the subpattern to the right has participated in an  earlier  itera-
4135         tion.         tion.
4136    
4137         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
4138         subpattern  whose  number  is  10  or  more using this syntax because a         subpattern whose number is 10 or  more  using  this  syntax  because  a
4139         sequence such as \50 is interpreted as a character  defined  in  octal.         sequence  such  as  \50 is interpreted as a character defined in octal.
4140         See the subsection entitled "Non-printing characters" above for further         See the subsection entitled "Non-printing characters" above for further
4141         details of the handling of digits following a backslash.  There  is  no         details  of  the  handling of digits following a backslash. There is no
4142         such  problem  when named parentheses are used. A back reference to any         such problem when named parentheses are used. A back reference  to  any
4143         subpattern is possible using named parentheses (see below).         subpattern is possible using named parentheses (see below).
4144    
4145         Another way of avoiding the ambiguity inherent in  the  use  of  digits         Another  way  of  avoiding  the ambiguity inherent in the use of digits
4146         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-
4147         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
4148         unsigned  number  or  a negative number, optionally enclosed in braces.         unsigned number or a negative number, optionally  enclosed  in  braces.
4149         These examples are all identical:         These examples are all identical:
4150    
4151           (ring), \1           (ring), \1
4152           (ring), \g1           (ring), \g1
4153           (ring), \g{1}           (ring), \g{1}
4154    
4155         An unsigned number specifies an absolute reference without the  ambigu-         An  unsigned number specifies an absolute reference without the ambigu-
4156         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
4157         digits follow the reference. A negative number is a relative reference.         digits follow the reference. A negative number is a relative reference.
4158         Consider this example:         Consider this example:
# Line 4020  BACK REFERENCES Line 4160  BACK REFERENCES
4160           (abc(def)ghi)\g{-1}           (abc(def)ghi)\g{-1}
4161    
4162         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-
4163         ing subpattern before \g, that is, is it equivalent to  \2.  Similarly,         ing  subpattern  before \g, that is, is it equivalent to \2. Similarly,
4164         \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
4165         helpful in long patterns, and also in  patterns  that  are  created  by         helpful  in  long  patterns,  and  also in patterns that are created by
4166         joining together fragments that contain references within themselves.         joining together fragments that contain references within themselves.
4167    
4168         A  back  reference matches whatever actually matched the capturing sub-         A back reference matches whatever actually matched the  capturing  sub-
4169         pattern in the current subject string, rather  than  anything  matching         pattern  in  the  current subject string, rather than anything matching
4170         the subpattern itself (see "Subpatterns as subroutines" below for a way         the subpattern itself (see "Subpatterns as subroutines" below for a way
4171         of doing that). So the pattern         of doing that). So the pattern
4172    
4173           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
4174    
4175         matches "sense and sensibility" and "response and responsibility",  but         matches  "sense and sensibility" and "response and responsibility", but
4176         not  "sense and responsibility". If caseful matching is in force at the         not "sense and responsibility". If caseful matching is in force at  the
4177         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-
4178         ple,         ple,
4179    
4180           ((?i)rah)\s+\1           ((?i)rah)\s+\1
4181    
4182         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
4183         original capturing subpattern is matched caselessly.         original capturing subpattern is matched caselessly.
4184    
4185         There are several different ways of writing back  references  to  named         There  are  several  different ways of writing back references to named
4186         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
4187         \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
4188         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
4189         and named references, is also supported. We  could  rewrite  the  above         and  named  references,  is  also supported. We could rewrite the above
4190         example in any of the following ways:         example in any of the following ways:
4191    
4192           (?<p1>(?i)rah)\s+\k<p1>           (?<p1>(?i)rah)\s+\k<p1>
# Line 4054  BACK REFERENCES Line 4194  BACK REFERENCES
4194           (?P<p1>(?i)rah)\s+(?P=p1)           (?P<p1>(?i)rah)\s+(?P=p1)
4195           (?<p1>(?i)rah)\s+\g{p1}           (?<p1>(?i)rah)\s+\g{p1}
4196    
4197         A  subpattern  that  is  referenced  by  name may appear in the pattern         A subpattern that is referenced by  name  may  appear  in  the  pattern
4198         before or after the reference.         before or after the reference.
4199    
4200         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
4201         subpattern  has  not actually been used in a particular match, any back         subpattern has not actually been used in a particular match,  any  back
4202         references to it always fail. For example, the pattern         references to it always fail. For example, the pattern
4203    
4204           (a|(bc))\2           (a|(bc))\2
4205    
4206         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
4207         may  be  many  capturing parentheses in a pattern, all digits following         may be many capturing parentheses in a pattern,  all  digits  following
4208         the backslash are taken as part of a potential back  reference  number.         the  backslash  are taken as part of a potential back reference number.
4209         If the pattern continues with a digit character, some delimiter must be         If the pattern continues with a digit character, some delimiter must be
4210         used to terminate the back reference. If the  PCRE_EXTENDED  option  is         used  to  terminate  the back reference. If the PCRE_EXTENDED option is
4211         set,  this  can  be  whitespace.  Otherwise an empty comment (see "Com-         set, this can be whitespace.  Otherwise an  empty  comment  (see  "Com-
4212         ments" below) can be used.         ments" below) can be used.
4213    
4214         A back reference that occurs inside the parentheses to which it  refers         A  back reference that occurs inside the parentheses to which it refers
4215         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
4216         matches.  However, such references can be useful inside  repeated  sub-         matches.   However,  such references can be useful inside repeated sub-
4217         patterns. For example, the pattern         patterns. For example, the pattern
4218    
4219           (a|b\1)+           (a|b\1)+
4220    
4221         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-
4222         ation of the subpattern,  the  back  reference  matches  the  character         ation  of  the  subpattern,  the  back  reference matches the character
4223         string  corresponding  to  the previous iteration. In order for this to         string corresponding to the previous iteration. In order  for  this  to
4224         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
4225         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
4226         the example above, or by a quantifier with a minimum of zero.         the example above, or by a quantifier with a minimum of zero.
4227    
4228    
4229  ASSERTIONS  ASSERTIONS
4230    
4231         An assertion is a test on the characters  following  or  preceding  the         An  assertion  is  a  test on the characters following or preceding the
4232         current  matching  point that does not actually consume any characters.         current matching point that does not actually consume  any  characters.
4233         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
4234         described above.         described above.
4235    
4236         More  complicated  assertions  are  coded as subpatterns. There are two         More complicated assertions are coded as  subpatterns.  There  are  two
4237         kinds: those that look ahead of the current  position  in  the  subject         kinds:  those  that  look  ahead of the current position in the subject
4238         string,  and  those  that  look  behind  it. An assertion subpattern is         string, and those that look  behind  it.  An  assertion  subpattern  is
4239         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
4240         matching position to be changed.         matching position to be changed.
4241    
4242         Assertion  subpatterns  are  not  capturing subpatterns, and may not be         Assertion subpatterns are not capturing subpatterns,  and  may  not  be
4243         repeated, because it makes no sense to assert the  same  thing  several         repeated,  because  it  makes no sense to assert the same thing several
4244         times.  If  any kind of assertion contains capturing subpatterns within         times. If any kind of assertion contains capturing  subpatterns  within
4245         it, these are counted for the purposes of numbering the capturing  sub-         it,  these are counted for the purposes of numbering the capturing sub-
4246         patterns in the whole pattern.  However, substring capturing is carried         patterns in the whole pattern.  However, substring capturing is carried
4247         out only for positive assertions, because it does not  make  sense  for         out  only  for  positive assertions, because it does not make sense for
4248         negative assertions.         negative assertions.
4249    
4250     Lookahead assertions     Lookahead assertions
# Line 4114  ASSERTIONS Line 4254  ASSERTIONS
4254    
4255           \w+(?=;)           \w+(?=;)
4256    
4257         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-
4258         colon in the match, and         colon in the match, and
4259    
4260           foo(?!bar)           foo(?!bar)
4261    
4262         matches  any  occurrence  of  "foo" that is not followed by "bar". Note         matches any occurrence of "foo" that is not  followed  by  "bar".  Note
4263         that the apparently similar pattern         that the apparently similar pattern
4264    
4265           (?!foo)bar           (?!foo)bar
4266    
4267         does not find an occurrence of "bar"  that  is  preceded  by  something         does  not  find  an  occurrence  of "bar" that is preceded by something
4268         other  than "foo"; it finds any occurrence of "bar" whatsoever, because         other than "foo"; it finds any occurrence of "bar" whatsoever,  because
4269         the assertion (?!foo) is always true when the next three characters are         the assertion (?!foo) is always true when the next three characters are
4270         "bar". A lookbehind assertion is needed to achieve the other effect.         "bar". A lookbehind assertion is needed to achieve the other effect.
4271    
4272         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
4273         most convenient way to do it is  with  (?!)  because  an  empty  string         most  convenient  way  to  do  it  is with (?!) because an empty string
4274         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
4275         string must always fail.         string must always fail.
4276    
4277     Lookbehind assertions     Lookbehind assertions
4278    
4279         Lookbehind assertions start with (?<= for positive assertions and  (?<!         Lookbehind  assertions start with (?<= for positive assertions and (?<!
4280         for negative assertions. For example,         for negative assertions. For example,
4281    
4282           (?<!foo)bar           (?<!foo)bar
4283    
4284         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
4285         contents of a lookbehind assertion are restricted  such  that  all  the         contents  of  a  lookbehind  assertion are restricted such that all the
4286         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-
4287         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
4288         fixed length. Thus         fixed length. Thus
4289    
4290           (?<=bullock|donkey)           (?<=bullock|donkey)
# Line 4153  ASSERTIONS Line 4293  ASSERTIONS
4293    
4294           (?<!dogs?|cats?)           (?<!dogs?|cats?)
4295    
4296         causes  an  error at compile time. Branches that match different length         causes an error at compile time. Branches that match  different  length
4297         strings are permitted only at the top level of a lookbehind  assertion.         strings  are permitted only at the top level of a lookbehind assertion.
4298         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
4299         requires all branches to match the same length of string. An  assertion         requires  all branches to match the same length of string. An assertion
4300         such as         such as
4301    
4302           (?<=ab(c|de))           (?<=ab(c|de))
4303    
4304         is  not  permitted,  because  its single top-level branch can match two         is not permitted, because its single top-level  branch  can  match  two
4305         different lengths, but it is acceptable if rewritten to  use  two  top-         different  lengths,  but  it is acceptable if rewritten to use two top-
4306         level branches:         level branches:
4307    
4308           (?<=abc|abde)           (?<=abc|abde)
4309    
4310         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
4311         instead of a lookbehind assertion; this is not restricted to  a  fixed-         instead  of  a lookbehind assertion; this is not restricted to a fixed-
4312         length.         length.
4313    
4314         The  implementation  of lookbehind assertions is, for each alternative,         The implementation of lookbehind assertions is, for  each  alternative,
4315         to temporarily move the current position back by the fixed  length  and         to  temporarily  move the current position back by the fixed length and
4316         then try to match. If there are insufficient characters before the cur-         then try to match. If there are insufficient characters before the cur-
4317         rent position, the assertion fails.         rent position, the assertion fails.
4318    
4319         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
4320         mode)  to appear in lookbehind assertions, because it makes it impossi-         mode) to appear in lookbehind assertions, because it makes it  impossi-
4321         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,
4322         which can match different numbers of bytes, are also not permitted.         which can match different numbers of bytes, are also not permitted.
4323    
4324         Possessive  quantifiers  can  be  used  in  conjunction with lookbehind         Possessive quantifiers can  be  used  in  conjunction  with  lookbehind
4325         assertions to specify efficient matching at  the  end  of  the  subject         assertions  to  specify  efficient  matching  at the end of the subject
4326         string. Consider a simple pattern such as         string. Consider a simple pattern such as
4327    
4328           abcd$           abcd$
4329    
4330         when  applied  to  a  long string that does not match. Because matching         when applied to a long string that does  not  match.  Because  matching
4331         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
4332         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
4333         pattern is specified as         pattern is specified as
4334    
4335           ^.*abcd$           ^.*abcd$
4336    
4337         the initial .* matches the entire string at first, but when this  fails         the  initial .* matches the entire string at first, but when this fails
4338         (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
4339         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
4340         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,
4341         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
4342    
4343           ^.*+(?<=abcd)           ^.*+(?<=abcd)
4344    
4345         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
4346         entire  string.  The subsequent lookbehind assertion does a single test         entire string. The subsequent lookbehind assertion does a  single  test
4347         on the last four characters. If it fails, the match fails  immediately.         on  the last four characters. If it fails, the match fails immediately.
4348         For  long  strings, this approach makes a significant difference to the         For long strings, this approach makes a significant difference  to  the
4349         processing time.         processing time.
4350    
4351     Using multiple assertions     Using multiple assertions
# Line 4214  ASSERTIONS Line 4354  ASSERTIONS
4354    
4355           (?<=\d{3})(?<!999)foo           (?<=\d{3})(?<!999)foo
4356    
4357         matches "foo" preceded by three digits that are not "999". Notice  that         matches  "foo" preceded by three digits that are not "999". Notice that
4358         each  of  the  assertions is applied independently at the same point in         each of the assertions is applied independently at the  same  point  in
4359         the subject string. First there is a  check  that  the  previous  three         the  subject  string.  First  there  is a check that the previous three
4360         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
4361         three characters are not "999".  This pattern does not match "foo" pre-         three characters are not "999".  This pattern does not match "foo" pre-
4362         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
4363         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-
4364         foo". A pattern to do that is         foo". A pattern to do that is
4365    
4366           (?<=\d{3}...)(?<!999)foo           (?<=\d{3}...)(?<!999)foo
4367    
4368         This  time  the  first assertion looks at the preceding six characters,         This time the first assertion looks at the  preceding  six  characters,
4369         checking that the first three are digits, and then the second assertion         checking that the first three are digits, and then the second assertion
4370         checks that the preceding three characters are not "999".         checks that the preceding three characters are not "999".
4371    
# Line 4233  ASSERTIONS Line 4373  ASSERTIONS
4373    
4374           (?<=(?<!foo)bar)baz           (?<=(?<!foo)bar)baz
4375    
4376         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
4377         is not preceded by "foo", while         is not preceded by "foo", while
4378    
4379           (?<=\d{3}(?!999)...)foo           (?<=\d{3}(?!999)...)foo
4380    
4381         is another pattern that matches "foo" preceded by three digits and  any         is  another pattern that matches "foo" preceded by three digits and any
4382         three characters that are not "999".         three characters that are not "999".
4383    
4384    
4385  CONDITIONAL SUBPATTERNS  CONDITIONAL SUBPATTERNS
4386    
4387         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-
4388         ditionally or to choose between two alternative subpatterns,  depending         ditionally  or to choose between two alternative subpatterns, depending
4389         on  the result of an assertion, or whether a previous capturing subpat-         on the result of an assertion, or whether a previous capturing  subpat-
4390         tern matched or not. The two possible forms of  conditional  subpattern         tern  matched  or not. The two possible forms of conditional subpattern
4391         are         are
4392    
4393           (?(condition)yes-pattern)           (?(condition)yes-pattern)
4394           (?(condition)yes-pattern|no-pattern)           (?(condition)yes-pattern|no-pattern)
4395    
4396         If  the  condition is satisfied, the yes-pattern is used; otherwise the         If the condition is satisfied, the yes-pattern is used;  otherwise  the
4397         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-
4398         tives in the subpattern, a compile-time error occurs.         tives in the subpattern, a compile-time error occurs.
4399    
4400         There  are  four  kinds of condition: references to subpatterns, refer-         There are four kinds of condition: references  to  subpatterns,  refer-
4401         ences to recursion, a pseudo-condition called DEFINE, and assertions.         ences to recursion, a pseudo-condition called DEFINE, and assertions.
4402    
4403     Checking for a used subpattern by number     Checking for a used subpattern by number
4404    
4405         If the text between the parentheses consists of a sequence  of  digits,         If  the  text between the parentheses consists of a sequence of digits,
4406         the  condition  is  true if the capturing subpattern of that number has         the condition is true if the capturing subpattern of  that  number  has
4407         previously matched. An alternative notation is to  precede  the  digits         previously  matched.  An  alternative notation is to precede the digits
4408         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-
4409         tive rather than absolute.  The most recently opened parentheses can be         tive rather than absolute.  The most recently opened parentheses can be
4410         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
4411         looping constructs it can also make sense to refer to subsequent groups         looping constructs it can also make sense to refer to subsequent groups
4412         with constructs such as (?(+2).         with constructs such as (?(+2).
4413    
4414         Consider  the  following  pattern, which contains non-significant white         Consider the following pattern, which  contains  non-significant  white
4415         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
4416         divide it into three parts for ease of discussion:         divide it into three parts for ease of discussion:
4417    
4418           ( \( )?    [^()]+    (?(1) \) )           ( \( )?    [^()]+    (?(1) \) )
4419    
4420         The  first  part  matches  an optional opening parenthesis, and if that         The first part matches an optional opening  parenthesis,  and  if  that
4421         character is present, sets it as the first captured substring. The sec-         character is present, sets it as the first captured substring. The sec-
4422         ond  part  matches one or more characters that are not parentheses. The         ond part matches one or more characters that are not  parentheses.  The
4423         third part is a conditional subpattern that tests whether the first set         third part is a conditional subpattern that tests whether the first set
4424         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
4425         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-
4426         tern  is  executed  and  a  closing parenthesis is required. Otherwise,         tern is executed and a  closing  parenthesis  is  required.  Otherwise,
4427         since no-pattern is not present, the  subpattern  matches  nothing.  In         since  no-pattern  is  not  present, the subpattern matches nothing. In
4428         other  words,  this  pattern  matches  a  sequence  of non-parentheses,         other words,  this  pattern  matches  a  sequence  of  non-parentheses,
4429         optionally enclosed in parentheses.         optionally enclosed in parentheses.
4430    
4431         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
4432         relative reference:         relative reference:
4433    
4434           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...
4435    
4436         This  makes  the  fragment independent of the parentheses in the larger         This makes the fragment independent of the parentheses  in  the  larger
4437         pattern.         pattern.
4438    
4439     Checking for a used subpattern by name     Checking for a used subpattern by name
4440    
4441         Perl uses the syntax (?(<name>)...) or (?('name')...)  to  test  for  a         Perl  uses  the  syntax  (?(<name>)...) or (?('name')...) to test for a
4442         used  subpattern  by  name.  For compatibility with earlier versions of         used subpattern by name. For compatibility  with  earlier  versions  of
4443         PCRE, which had this facility before Perl, the syntax  (?(name)...)  is         PCRE,  which  had this facility before Perl, the syntax (?(name)...) is
4444         also  recognized. However, there is a possible ambiguity with this syn-         also recognized. However, there is a possible ambiguity with this  syn-
4445         tax, because subpattern names may  consist  entirely  of  digits.  PCRE         tax,  because  subpattern  names  may  consist entirely of digits. PCRE
4446         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
4447         consists entirely of digits, PCRE looks for a subpattern of  that  num-         consists  entirely  of digits, PCRE looks for a subpattern of that num-
4448         ber,  which must be greater than zero. Using subpattern names that con-         ber, which must be greater than zero. Using subpattern names that  con-
4449         sist entirely of digits is not recommended.         sist entirely of digits is not recommended.
4450    
4451         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 4456  CONDITIONAL SUBPATTERNS
4456     Checking for pattern recursion     Checking for pattern recursion
4457    
4458         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
4459         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
4460         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-
4461         sand follow the letter R, for example:         sand follow the letter R, for example:
4462    
4463           (?(R3)...) or (?(R&name)...)           (?(R3)...) or (?(R&name)...)
4464    
4465         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-
4466         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
4467         entire recursion stack.         entire recursion stack.
4468    
4469         At  "top  level", all these recursion test conditions are false. Recur-         At "top level", all these recursion test conditions are  false.  Recur-
4470         sive patterns are described below.         sive patterns are described below.
4471    
4472     Defining subpatterns for use by reference only     Defining subpatterns for use by reference only
4473    
4474         If the condition is the string (DEFINE), and  there  is  no  subpattern         If  the  condition  is  the string (DEFINE), and there is no subpattern
4475         with  the  name  DEFINE,  the  condition is always false. In this case,         with the name DEFINE, the condition is  always  false.  In  this  case,
4476         there may be only one alternative  in  the  subpattern.  It  is  always         there  may  be  only  one  alternative  in the subpattern. It is always
4477         skipped  if  control  reaches  this  point  in the pattern; the idea of         skipped if control reaches this point  in  the  pattern;  the  idea  of
4478         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-
4479         erenced  from elsewhere. (The use of "subroutines" is described below.)         erenced from elsewhere. (The use of "subroutines" is described  below.)
4480         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
4481         this (ignore whitespace and line breaks):         this (ignore whitespace and line breaks):
4482    
4483           (?(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) )
4484           \b (?&byte) (\.(?&byte)){3} \b           \b (?&byte) (\.(?&byte)){3} \b
4485    
4486         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
4487         group named "byte" is defined. This matches an individual component  of         group  named "byte" is defined. This matches an individual component of
4488         an  IPv4  address  (a number less than 256). When matching takes place,         an IPv4 address (a number less than 256). When  matching  takes  place,
4489         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
4490         condition.         condition.
4491    
4492         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
4493         four dot-separated components of an IPv4 address, insisting on  a  word         four  dot-separated  components of an IPv4 address, insisting on a word
4494         boundary at each end.         boundary at each end.
4495    
4496     Assertion conditions     Assertion conditions
4497    
4498         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
4499         assertion.  This may be a positive or negative lookahead or  lookbehind         assertion.   This may be a positive or negative lookahead or lookbehind
4500         assertion.  Consider  this  pattern,  again  containing non-significant         assertion. Consider  this  pattern,  again  containing  non-significant
4501         white space, and with the two alternatives on the second line:         white space, and with the two alternatives on the second line:
4502    
4503           (?(?=[^a-z]*[a-z])           (?(?=[^a-z]*[a-z])
4504           \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} )
4505    
4506         The condition  is  a  positive  lookahead  assertion  that  matches  an         The  condition  is  a  positive  lookahead  assertion  that  matches an
4507         optional  sequence of non-letters followed by a letter. In other words,         optional sequence of non-letters followed by a letter. In other  words,
4508         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
4509         letter  is found, the subject is matched against the first alternative;         letter is found, the subject is matched against the first  alternative;
4510         otherwise it is  matched  against  the  second.  This  pattern  matches         otherwise  it  is  matched  against  the  second.  This pattern matches
4511         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
4512         letters and dd are digits.         letters and dd are digits.
4513    
4514    
4515  COMMENTS  COMMENTS
4516    
4517         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
4518         next  closing  parenthesis.  Nested  parentheses are not permitted. The         next closing parenthesis. Nested parentheses  are  not  permitted.  The
4519         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
4520         at all.         at all.
4521    
4522         If  the PCRE_EXTENDED option is set, an unescaped # character outside a         If the PCRE_EXTENDED option is set, an unescaped # character outside  a
4523         character class introduces a  comment  that  continues  to  immediately         character  class  introduces  a  comment  that continues to immediately
4524         after the next newline in the pattern.         after the next newline in the pattern.
4525    
4526    
4527  RECURSIVE PATTERNS  RECURSIVE PATTERNS
4528    
4529         Consider  the problem of matching a string in parentheses, allowing for         Consider the problem of matching a string in parentheses, allowing  for
4530         unlimited nested parentheses. Without the use of  recursion,  the  best         unlimited  nested  parentheses.  Without the use of recursion, the best
4531         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
4532         depth of nesting. It is not possible to  handle  an  arbitrary  nesting         depth  of  nesting.  It  is not possible to handle an arbitrary nesting
4533         depth.         depth.
4534    
4535         For some time, Perl has provided a facility that allows regular expres-         For some time, Perl has provided a facility that allows regular expres-
4536         sions to recurse (amongst other things). It does this by  interpolating         sions  to recurse (amongst other things). It does this by interpolating
4537         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
4538         expression itself. A Perl pattern using code interpolation to solve the         expression itself. A Perl pattern using code interpolation to solve the
4539         parentheses problem can be created like this:         parentheses problem can be created like this:
4540    
# Line 4404  RECURSIVE PATTERNS Line 4544  RECURSIVE PATTERNS
4544         refers recursively to the pattern in which it appears.         refers recursively to the pattern in which it appears.
4545    
4546         Obviously, PCRE cannot support the interpolation of Perl code. Instead,         Obviously, PCRE cannot support the interpolation of Perl code. Instead,
4547         it  supports  special  syntax  for recursion of the entire pattern, and         it supports special syntax for recursion of  the  entire  pattern,  and
4548         also for individual subpattern recursion.  After  its  introduction  in         also  for  individual  subpattern  recursion. After its introduction in
4549         PCRE  and  Python,  this  kind of recursion was introduced into Perl at         PCRE and Python, this kind of recursion was  introduced  into  Perl  at
4550         release 5.10.         release 5.10.
4551    
4552         A special item that consists of (? followed by a  number  greater  than         A  special  item  that consists of (? followed by a number greater than
4553         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
4554         the given number, provided that it occurs inside that  subpattern.  (If         the  given  number, provided that it occurs inside that subpattern. (If
4555         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-
4556         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
4557         regular expression.         regular expression.
4558    
4559         In  PCRE (like Python, but unlike Perl), a recursive subpattern call is         In PCRE (like Python, but unlike Perl), a recursive subpattern call  is
4560         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
4561         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
4562         alternatives and there is a subsequent matching failure.         alternatives and there is a subsequent matching failure.
4563    
4564         This PCRE pattern solves the nested  parentheses  problem  (assume  the         This  PCRE  pattern  solves  the nested parentheses problem (assume the
4565         PCRE_EXTENDED option is set so that white space is ignored):         PCRE_EXTENDED option is set so that white space is ignored):
4566    
4567           \( ( (?>[^()]+) | (?R) )* \)           \( ( (?>[^()]+) | (?R) )* \)
4568    
4569         First  it matches an opening parenthesis. Then it matches any number of         First it matches an opening parenthesis. Then it matches any number  of
4570         substrings which can either be a  sequence  of  non-parentheses,  or  a         substrings  which  can  either  be  a sequence of non-parentheses, or a
4571         recursive  match  of the pattern itself (that is, a correctly parenthe-         recursive match of the pattern itself (that is, a  correctly  parenthe-
4572         sized substring).  Finally there is a closing parenthesis.         sized substring).  Finally there is a closing parenthesis.
4573    
4574         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
4575         the entire pattern, so instead you could use this:         the entire pattern, so instead you could use this:
4576    
4577           ( \( ( (?>[^()]+) | (?1) )* \) )           ( \( ( (?>[^()]+) | (?1) )* \) )
4578    
4579         We  have  put the pattern into parentheses, and caused the recursion to         We have put the pattern into parentheses, and caused the  recursion  to
4580         refer to them instead of the whole pattern.         refer to them instead of the whole pattern.
4581    
4582         In a larger pattern,  keeping  track  of  parenthesis  numbers  can  be         In  a  larger  pattern,  keeping  track  of  parenthesis numbers can be
4583         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
4584         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
4585         (?-2) to refer to the second most recently opened parentheses preceding         (?-2) to refer to the second most recently opened parentheses preceding
4586         the recursion. In other  words,  a  negative  number  counts  capturing         the  recursion.  In  other  words,  a  negative number counts capturing
4587         parentheses leftwards from the point at which it is encountered.         parentheses leftwards from the point at which it is encountered.
4588    
4589         It  is  also  possible  to refer to subsequently opened parentheses, by         It is also possible to refer to  subsequently  opened  parentheses,  by
4590         writing references such as (?+2). However, these  cannot  be  recursive         writing  references  such  as (?+2). However, these cannot be recursive
4591         because  the  reference  is  not inside the parentheses that are refer-         because the reference is not inside the  parentheses  that  are  refer-
4592         enced. They are always "subroutine" calls, as  described  in  the  next         enced.  They  are  always  "subroutine" calls, as described in the next
4593         section.         section.
4594    
4595         An  alternative  approach is to use named parentheses instead. The Perl         An alternative approach is to use named parentheses instead.  The  Perl
4596         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
4597         supported. We could rewrite the above example as follows:         supported. We could rewrite the above example as follows:
4598    
4599           (?<pn> \( ( (?>[^()]+) | (?&pn) )* \) )           (?<pn> \( ( (?>[^()]+) | (?&pn) )* \) )
4600    
4601         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
4602         one is used.         one is used.
4603    
4604         This particular example pattern that we have been looking  at  contains         This  particular  example pattern that we have been looking at contains
4605         nested  unlimited repeats, and so the use of atomic grouping for match-         nested unlimited repeats, and so the use of atomic grouping for  match-
4606         ing strings of non-parentheses is important when applying  the  pattern         ing  strings  of non-parentheses is important when applying the pattern
4607         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
4608         to         to
4609    
4610           (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()           (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
4611    
4612         it yields "no match" quickly. However, if atomic grouping is not  used,         it  yields "no match" quickly. However, if atomic grouping is not used,
4613         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
4614         different ways the + and * repeats can carve up the  subject,  and  all         different  ways  the  + and * repeats can carve up the subject, and all
4615         have to be tested before failure can be reported.         have to be tested before failure can be reported.
4616    
4617         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
4618         those from the outermost level of the recursion at which the subpattern         those from the outermost level of the recursion at which the subpattern
4619         value  is  set.   If  you want to obtain intermediate values, a callout         value is set.  If you want to obtain  intermediate  values,  a  callout
4620         function can be used (see below and the pcrecallout documentation).  If         function  can be used (see below and the pcrecallout documentation). If
4621         the pattern above is matched against         the pattern above is matched against
4622    
4623           (ab(cd)ef)           (ab(cd)ef)
4624    
4625         the  value  for  the  capturing  parentheses is "ef", which is the last         the value for the capturing parentheses is  "ef",  which  is  the  last
4626         value taken on at the top level. If additional parentheses  are  added,         value  taken  on at the top level. If additional parentheses are added,
4627         giving         giving
4628    
4629           \( ( ( (?>[^()]+) | (?R) )* ) \)           \( ( ( (?>[^()]+) | (?R) )* ) \)
4630              ^                        ^              ^                        ^
4631              ^                        ^              ^                        ^
4632    
4633         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
4634         parentheses. If there are more than 15 capturing parentheses in a  pat-         parentheses.  If there are more than 15 capturing parentheses in a pat-
4635         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,
4636         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-
4637         wards.  If  no  memory  can  be  obtained,  the  match  fails  with the         wards. If  no  memory  can  be  obtained,  the  match  fails  with  the
4638         PCRE_ERROR_NOMEMORY error.         PCRE_ERROR_NOMEMORY error.
4639    
4640         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
4641         recursion.   Consider  this pattern, which matches text in angle brack-         recursion.  Consider this pattern, which matches text in  angle  brack-
4642         ets, allowing for arbitrary nesting. Only digits are allowed in  nested         ets,  allowing for arbitrary nesting. Only digits are allowed in nested
4643         brackets  (that is, when recursing), whereas any characters are permit-         brackets (that is, when recursing), whereas any characters are  permit-
4644         ted at the outer level.         ted at the outer level.
4645    
4646           < (?: (?(R) \d++  | [^<>]*+) | (?R)) * >           < (?: (?(R) \d++  | [^<>]*+) | (?R)) * >
4647    
4648         In this pattern, (?(R) is the start of a conditional  subpattern,  with         In  this  pattern, (?(R) is the start of a conditional subpattern, with
4649         two  different  alternatives for the recursive and non-recursive cases.         two different alternatives for the recursive and  non-recursive  cases.
4650         The (?R) item is the actual recursive call.         The (?R) item is the actual recursive call.
4651    
4652    
4653  SUBPATTERNS AS SUBROUTINES  SUBPATTERNS AS SUBROUTINES
4654    
4655         If the syntax for a recursive subpattern reference (either by number or         If the syntax for a recursive subpattern reference (either by number or
4656         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-
4657         ates like a subroutine in a programming language. The "called"  subpat-         ates  like a subroutine in a programming language. The "called" subpat-
4658         tern may be defined before or after the reference. A numbered reference         tern may be defined before or after the reference. A numbered reference
4659         can be absolute or relative, as in these examples:         can be absolute or relative, as in these examples: