/[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 392 by ph10, Tue Mar 17 21:30:30 2009 UTC
# Line 18  INTRODUCTION Line 18  INTRODUCTION
18    
19         The  PCRE  library is a set of functions that implement regular expres-         The  PCRE  library is a set of functions that implement regular expres-
20         sion pattern matching using the same syntax and semantics as Perl, with         sion pattern matching using the same syntax and semantics as Perl, with
21         just  a  few differences. (Certain features that appeared in Python and         just  a  few  differences. Certain features that appeared in Python and
22         PCRE before they appeared in Perl are also available using  the  Python         PCRE before they appeared in Perl are also available using  the  Python
23         syntax.)         syntax.  There is also some support for certain .NET and Oniguruma syn-
24           tax items, and there is an option for  requesting  some  minor  changes
25           that give better JavaScript compatibility.
26    
27         The  current  implementation of PCRE (release 7.x) corresponds approxi-         The  current  implementation of PCRE (release 7.x) corresponds approxi-
28         mately with Perl 5.10, including support for UTF-8 encoded strings  and         mately with Perl 5.10, including support for UTF-8 encoded strings  and
# Line 92  USER DOCUMENTATION Line 94  USER DOCUMENTATION
94           pcrestack         discussion of stack usage           pcrestack         discussion of stack usage
95           pcretest          description of the pcretest testing command           pcretest          description of the pcretest testing command
96    
97         In  addition,  in the "man" and HTML formats, there is a short page for         In addition, in the "man" and HTML formats, there is a short  page  for
98         each C library function, listing its arguments and results.         each C library function, listing its arguments and results.
99    
100    
101  LIMITATIONS  LIMITATIONS
102    
103         There are some size limitations in PCRE but it is hoped that they  will         There  are some size limitations in PCRE but it is hoped that they will
104         never in practice be relevant.         never in practice be relevant.
105    
106         The  maximum  length of a compiled pattern is 65539 (sic) bytes if PCRE         The maximum length of a compiled pattern is 65539 (sic) bytes  if  PCRE
107         is compiled with the default internal linkage size of 2. If you want to         is compiled with the default internal linkage size of 2. If you want to
108         process  regular  expressions  that are truly enormous, you can compile         process regular expressions that are truly enormous,  you  can  compile
109         PCRE with an internal linkage size of 3 or 4 (see the  README  file  in         PCRE  with  an  internal linkage size of 3 or 4 (see the README file in
110         the  source  distribution and the pcrebuild documentation for details).         the source distribution and the pcrebuild documentation  for  details).
111         In these cases the limit is substantially larger.  However,  the  speed         In  these  cases the limit is substantially larger.  However, the speed
112         of execution is slower.         of execution is slower.
113    
114         All values in repeating quantifiers must be less than 65536.         All values in repeating quantifiers must be less than 65536.
# Line 117  LIMITATIONS Line 119  LIMITATIONS
119         The maximum length of name for a named subpattern is 32 characters, and         The maximum length of name for a named subpattern is 32 characters, and
120         the maximum number of named subpatterns is 10000.         the maximum number of named subpatterns is 10000.
121    
122         The  maximum  length of a subject string is the largest positive number         The maximum length of a subject string is the largest  positive  number
123         that an integer variable can hold. However, when using the  traditional         that  an integer variable can hold. However, when using the traditional
124         matching function, PCRE uses recursion to handle subpatterns and indef-         matching function, PCRE uses recursion to handle subpatterns and indef-
125         inite repetition.  This means that the available stack space may  limit         inite  repetition.  This means that the available stack space may limit
126         the size of a subject string that can be processed by certain patterns.         the size of a subject string that can be processed by certain patterns.
127         For a discussion of stack issues, see the pcrestack documentation.         For a discussion of stack issues, see the pcrestack documentation.
128    
129    
130  UTF-8 AND UNICODE PROPERTY SUPPORT  UTF-8 AND UNICODE PROPERTY SUPPORT
131    
132         From release 3.3, PCRE has  had  some  support  for  character  strings         From  release  3.3,  PCRE  has  had  some support for character strings
133         encoded  in the UTF-8 format. For release 4.0 this was greatly extended         encoded in the UTF-8 format. For release 4.0 this was greatly  extended
134         to cover most common requirements, and in release 5.0  additional  sup-         to  cover  most common requirements, and in release 5.0 additional sup-
135         port for Unicode general category properties was added.         port for Unicode general category properties was added.
136    
137         In  order  process  UTF-8 strings, you must build PCRE to include UTF-8         In order process UTF-8 strings, you must build PCRE  to  include  UTF-8
138         support in the code, and, in addition,  you  must  call  pcre_compile()         support  in  the  code,  and, in addition, you must call pcre_compile()
139         with  the PCRE_UTF8 option flag. When you do this, both the pattern and         with the PCRE_UTF8 option flag. When you do this, both the pattern  and
140         any subject strings that are matched against it are  treated  as  UTF-8         any  subject  strings  that are matched against it are treated as UTF-8
141         strings instead of just strings of bytes.         strings instead of just strings of bytes.
142    
143         If  you compile PCRE with UTF-8 support, but do not use it at run time,         If you compile PCRE with UTF-8 support, but do not use it at run  time,
144         the library will be a bit bigger, but the additional run time  overhead         the  library will be a bit bigger, but the additional run time overhead
145         is limited to testing the PCRE_UTF8 flag occasionally, so should not be         is limited to testing the PCRE_UTF8 flag occasionally, so should not be
146         very big.         very big.
147    
148         If PCRE is built with Unicode character property support (which implies         If PCRE is built with Unicode character property support (which implies
149         UTF-8  support),  the  escape sequences \p{..}, \P{..}, and \X are sup-         UTF-8 support), the escape sequences \p{..}, \P{..}, and  \X  are  sup-
150         ported.  The available properties that can be tested are limited to the         ported.  The available properties that can be tested are limited to the
151         general  category  properties such as Lu for an upper case letter or Nd         general category properties such as Lu for an upper case letter  or  Nd
152         for a decimal number, the Unicode script names such as Arabic  or  Han,         for  a  decimal number, the Unicode script names such as Arabic or Han,
153         and  the  derived  properties  Any  and L&. A full list is given in the         and the derived properties Any and L&. A full  list  is  given  in  the
154         pcrepattern documentation. Only the short names for properties are sup-         pcrepattern documentation. Only the short names for properties are sup-
155         ported.  For example, \p{L} matches a letter. Its Perl synonym, \p{Let-         ported. For example, \p{L} matches a letter. Its Perl synonym,  \p{Let-
156         ter}, is not supported.  Furthermore,  in  Perl,  many  properties  may         ter},  is  not  supported.   Furthermore,  in Perl, many properties may
157         optionally  be  prefixed by "Is", for compatibility with Perl 5.6. PCRE         optionally be prefixed by "Is", for compatibility with Perl  5.6.  PCRE
158         does not support this.         does not support this.
159    
160     Validity of UTF-8 strings     Validity of UTF-8 strings
161    
162         When you set the PCRE_UTF8 flag, the strings  passed  as  patterns  and         When  you  set  the  PCRE_UTF8 flag, the strings passed as patterns and
163         subjects are (by default) checked for validity on entry to the relevant         subjects are (by default) checked for validity on entry to the relevant
164         functions. From release 7.3 of PCRE, the check is according  the  rules         functions.  From  release 7.3 of PCRE, the check is according the rules
165         of  RFC  3629, which are themselves derived from the Unicode specifica-         of RFC 3629, which are themselves derived from the  Unicode  specifica-
166         tion. Earlier releases of PCRE followed the rules of  RFC  2279,  which         tion.  Earlier  releases  of PCRE followed the rules of RFC 2279, which
167         allows  the  full range of 31-bit values (0 to 0x7FFFFFFF). The current         allows the full range of 31-bit values (0 to 0x7FFFFFFF).  The  current
168         check allows only values in the range U+0 to U+10FFFF, excluding U+D800         check allows only values in the range U+0 to U+10FFFF, excluding U+D800
169         to U+DFFF.         to U+DFFF.
170    
171         The  excluded  code  points are the "Low Surrogate Area" of Unicode, of         The excluded code points are the "Low Surrogate Area"  of  Unicode,  of
172         which the Unicode Standard says this: "The Low Surrogate Area does  not         which  the Unicode Standard says this: "The Low Surrogate Area does not
173         contain  any  character  assignments,  consequently  no  character code         contain any  character  assignments,  consequently  no  character  code
174         charts or namelists are provided for this area. Surrogates are reserved         charts or namelists are provided for this area. Surrogates are reserved
175         for  use  with  UTF-16 and then must be used in pairs." The code points         for use with UTF-16 and then must be used in pairs."  The  code  points
176         that are encoded by UTF-16 pairs  are  available  as  independent  code         that  are  encoded  by  UTF-16  pairs are available as independent code
177         points  in  the  UTF-8  encoding.  (In other words, the whole surrogate         points in the UTF-8 encoding. (In  other  words,  the  whole  surrogate
178         thing is a fudge for UTF-16 which unfortunately messes up UTF-8.)         thing is a fudge for UTF-16 which unfortunately messes up UTF-8.)
179    
180         If an  invalid  UTF-8  string  is  passed  to  PCRE,  an  error  return         If  an  invalid  UTF-8  string  is  passed  to  PCRE,  an  error return
181         (PCRE_ERROR_BADUTF8) is given. In some situations, you may already know         (PCRE_ERROR_BADUTF8) is given. In some situations, you may already know
182         that your strings are valid, and therefore want to skip these checks in         that your strings are valid, and therefore want to skip these checks in
183         order to improve performance. If you set the PCRE_NO_UTF8_CHECK flag at         order to improve performance. If you set the PCRE_NO_UTF8_CHECK flag at
184         compile time or at run time, PCRE assumes that the pattern  or  subject         compile  time  or at run time, PCRE assumes that the pattern or subject
185         it  is  given  (respectively)  contains only valid UTF-8 codes. In this         it is given (respectively) contains only valid  UTF-8  codes.  In  this
186         case, it does not diagnose an invalid UTF-8 string.         case, it does not diagnose an invalid UTF-8 string.
187    
188         If you pass an invalid UTF-8 string  when  PCRE_NO_UTF8_CHECK  is  set,         If  you  pass  an  invalid UTF-8 string when PCRE_NO_UTF8_CHECK is set,
189         what  happens  depends on why the string is invalid. If the string con-         what happens depends on why the string is invalid. If the  string  con-
190         forms to the "old" definition of UTF-8 (RFC 2279), it is processed as a         forms to the "old" definition of UTF-8 (RFC 2279), it is processed as a
191         string  of  characters  in  the  range 0 to 0x7FFFFFFF. In other words,         string of characters in the range 0  to  0x7FFFFFFF.  In  other  words,
192         apart from the initial validity test, PCRE (when in UTF-8 mode) handles         apart from the initial validity test, PCRE (when in UTF-8 mode) handles
193         strings  according  to  the more liberal rules of RFC 2279. However, if         strings according to the more liberal rules of RFC  2279.  However,  if
194         the string does not even conform to RFC 2279, the result is  undefined.         the  string does not even conform to RFC 2279, the result is undefined.
195         Your program may crash.         Your program may crash.
196    
197         If  you  want  to  process  strings  of  values  in the full range 0 to         If you want to process strings  of  values  in  the  full  range  0  to
198         0x7FFFFFFF, encoded in a UTF-8-like manner as per the old RFC, you  can         0x7FFFFFFF,  encoded in a UTF-8-like manner as per the old RFC, you can
199         set PCRE_NO_UTF8_CHECK to bypass the more restrictive test. However, in         set PCRE_NO_UTF8_CHECK to bypass the more restrictive test. However, in
200         this situation, you will have to apply your own validity check.         this situation, you will have to apply your own validity check.
201    
202     General comments about UTF-8 mode     General comments about UTF-8 mode
203    
204         1. An unbraced hexadecimal escape sequence (such  as  \xb3)  matches  a         1.  An  unbraced  hexadecimal  escape sequence (such as \xb3) matches a
205         two-byte UTF-8 character if the value is greater than 127.         two-byte UTF-8 character if the value is greater than 127.
206    
207         2.  Octal  numbers  up to \777 are recognized, and match two-byte UTF-8         2. Octal numbers up to \777 are recognized, and  match  two-byte  UTF-8
208         characters for values greater than \177.         characters for values greater than \177.
209    
210         3. Repeat quantifiers apply to complete UTF-8 characters, not to  indi-         3.  Repeat quantifiers apply to complete UTF-8 characters, not to indi-
211         vidual bytes, for example: \x{100}{3}.         vidual bytes, for example: \x{100}{3}.
212    
213         4.  The dot metacharacter matches one UTF-8 character instead of a sin-         4. The dot metacharacter matches one UTF-8 character instead of a  sin-
214         gle byte.         gle byte.
215    
216         5. The escape sequence \C can be used to match a single byte  in  UTF-8         5.  The  escape sequence \C can be used to match a single byte in UTF-8
217         mode,  but  its  use can lead to some strange effects. This facility is         mode, but its use can lead to some strange effects.  This  facility  is
218         not available in the alternative matching function, pcre_dfa_exec().         not available in the alternative matching function, pcre_dfa_exec().
219    
220         6. The character escapes \b, \B, \d, \D, \s, \S, \w, and  \W  correctly         6.  The  character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly
221         test  characters of any code value, but the characters that PCRE recog-         test characters of any code value, but the characters that PCRE  recog-
222         nizes as digits, spaces, or word characters  remain  the  same  set  as         nizes  as  digits,  spaces,  or  word characters remain the same set as
223         before, all with values less than 256. This remains true even when PCRE         before, all with values less than 256. This remains true even when PCRE
224         includes Unicode property support, because to do otherwise  would  slow         includes  Unicode  property support, because to do otherwise would slow
225         down  PCRE in many common cases. If you really want to test for a wider         down PCRE in many common cases. If you really want to test for a  wider
226         sense of, say, "digit", you must use Unicode  property  tests  such  as         sense  of,  say,  "digit",  you must use Unicode property tests such as
227         \p{Nd}.         \p{Nd}.
228    
229         7.  Similarly,  characters that match the POSIX named character classes         7. Similarly, characters that match the POSIX named  character  classes
230         are all low-valued characters.         are all low-valued characters.
231    
232         8. However, the Perl 5.10 horizontal and vertical  whitespace  matching         8.  However,  the Perl 5.10 horizontal and vertical whitespace matching
233         escapes (\h, \H, \v, and \V) do match all the appropriate Unicode char-         escapes (\h, \H, \v, and \V) do match all the appropriate Unicode char-
234         acters.         acters.
235    
236         9. Case-insensitive matching applies only to  characters  whose  values         9.  Case-insensitive  matching  applies only to characters whose values
237         are  less than 128, unless PCRE is built with Unicode property support.         are less than 128, unless PCRE is built with Unicode property  support.
238         Even when Unicode property support is available, PCRE  still  uses  its         Even  when  Unicode  property support is available, PCRE still uses its
239         own  character  tables when checking the case of low-valued characters,         own character tables when checking the case of  low-valued  characters,
240         so as not to degrade performance.  The Unicode property information  is         so  as not to degrade performance.  The Unicode property information is
241         used only for characters with higher values. Even when Unicode property         used only for characters with higher values. Even when Unicode property
242         support is available, PCRE supports case-insensitive matching only when         support is available, PCRE supports case-insensitive matching only when
243         there  is  a  one-to-one  mapping between a letter's cases. There are a         there is a one-to-one mapping between a letter's  cases.  There  are  a
244         small number of many-to-one mappings in Unicode;  these  are  not  sup-         small  number  of  many-to-one  mappings in Unicode; these are not sup-
245         ported by PCRE.         ported by PCRE.
246    
247    
# Line 249  AUTHOR Line 251  AUTHOR
251         University Computing Service         University Computing Service
252         Cambridge CB2 3QH, England.         Cambridge CB2 3QH, England.
253    
254         Putting  an actual email address here seems to have been a spam magnet,         Putting an actual email address here seems to have been a spam  magnet,
255         so I've taken it away. If you want to email me, use  my  two  initials,         so  I've  taken  it away. If you want to email me, use my two initials,
256         followed by the two digits 10, at the domain cam.ac.uk.         followed by the two digits 10, at the domain cam.ac.uk.
257    
258    
259  REVISION  REVISION
260    
261         Last updated: 09 August 2007         Last updated: 12 April 2008
262         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2008 University of Cambridge.
263  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
264    
265    
# Line 271  NAME Line 273  NAME
273  PCRE BUILD-TIME OPTIONS  PCRE BUILD-TIME OPTIONS
274    
275         This  document  describes  the  optional  features  of PCRE that can be         This  document  describes  the  optional  features  of PCRE that can be
276         selected when the library is compiled. They are all selected, or  dese-         selected when the library is compiled. It assumes use of the  configure
277         lected, by providing options to the configure script that is run before         script,  where the optional features are selected or deselected by pro-
278         the make command. The complete list of  options  for  configure  (which         viding options to configure before running the make  command.  However,
279         includes  the  standard  ones such as the selection of the installation         the  same  options  can be selected in both Unix-like and non-Unix-like
280         directory) can be obtained by running         environments using the GUI facility of  CMakeSetup  if  you  are  using
281           CMake instead of configure to build PCRE.
282    
283           The complete list of options for configure (which includes the standard
284           ones such as the  selection  of  the  installation  directory)  can  be
285           obtained by running
286    
287           ./configure --help           ./configure --help
288    
289         The following sections include  descriptions  of  options  whose  names         The  following  sections  include  descriptions  of options whose names
290         begin with --enable or --disable. These settings specify changes to the         begin with --enable or --disable. These settings specify changes to the
291         defaults for the configure command. Because of the way  that  configure         defaults  for  the configure command. Because of the way that configure
292         works,  --enable  and --disable always come in pairs, so the complemen-         works, --enable and --disable always come in pairs, so  the  complemen-
293         tary option always exists as well, but as it specifies the default,  it         tary  option always exists as well, but as it specifies the default, it
294         is not described.         is not described.
295    
296    
# Line 300  C++ SUPPORT Line 307  C++ SUPPORT
307    
308  UTF-8 SUPPORT  UTF-8 SUPPORT
309    
310         To build PCRE with support for UTF-8 character strings, add         To build PCRE with support for UTF-8 Unicode character strings, add
311    
312           --enable-utf8           --enable-utf8
313    
314         to  the  configure  command.  Of  itself, this does not make PCRE treat         to the configure command. Of itself, this  does  not  make  PCRE  treat
315         strings as UTF-8. As well as compiling PCRE with this option, you  also         strings  as UTF-8. As well as compiling PCRE with this option, you also
316         have  have to set the PCRE_UTF8 option when you call the pcre_compile()         have have to set the PCRE_UTF8 option when you call the  pcre_compile()
317         function.         function.
318    
319           If  you set --enable-utf8 when compiling in an EBCDIC environment, PCRE
320           expects its input to be either ASCII or UTF-8 (depending on the runtime
321           option).  It  is not possible to support both EBCDIC and UTF-8 codes in
322           the same  version  of  the  library.  Consequently,  --enable-utf8  and
323           --enable-ebcdic are mutually exclusive.
324    
325    
326  UNICODE CHARACTER PROPERTY SUPPORT  UNICODE CHARACTER PROPERTY SUPPORT
327    
328         UTF-8 support allows PCRE to process character values greater than  255         UTF-8  support allows PCRE to process character values greater than 255
329         in  the  strings that it handles. On its own, however, it does not pro-         in the strings that it handles. On its own, however, it does  not  pro-
330         vide any facilities for accessing the properties of such characters. If         vide any facilities for accessing the properties of such characters. If
331         you  want  to  be able to use the pattern escapes \P, \p, and \X, which         you want to be able to use the pattern escapes \P, \p,  and  \X,  which
332         refer to Unicode character properties, you must add         refer to Unicode character properties, you must add
333    
334           --enable-unicode-properties           --enable-unicode-properties
335    
336         to the configure command. This implies UTF-8 support, even if you  have         to  the configure command. This implies UTF-8 support, even if you have
337         not explicitly requested it.         not explicitly requested it.
338    
339         Including  Unicode  property  support  adds around 30K of tables to the         Including Unicode property support adds around 30K  of  tables  to  the
340         PCRE library. Only the general category properties such as  Lu  and  Nd         PCRE  library.  Only  the general category properties such as Lu and Nd
341         are supported. Details are given in the pcrepattern documentation.         are supported. Details are given in the pcrepattern documentation.
342    
343    
344  CODE VALUE OF NEWLINE  CODE VALUE OF NEWLINE
345    
346         By  default,  PCRE interprets character 10 (linefeed, LF) as indicating         By default, PCRE interprets the linefeed (LF) character  as  indicating
347         the end of a line. This is the normal newline  character  on  Unix-like         the  end  of  a line. This is the normal newline character on Unix-like
348         systems. You can compile PCRE to use character 13 (carriage return, CR)         systems. You can compile PCRE to use carriage return (CR)  instead,  by
349         instead, by adding         adding
350    
351           --enable-newline-is-cr           --enable-newline-is-cr
352    
353         to the  configure  command.  There  is  also  a  --enable-newline-is-lf         to  the  configure  command.  There  is  also  a --enable-newline-is-lf
354         option, which explicitly specifies linefeed as the newline character.         option, which explicitly specifies linefeed as the newline character.
355    
356         Alternatively, you can specify that line endings are to be indicated by         Alternatively, you can specify that line endings are to be indicated by
# Line 349  CODE VALUE OF NEWLINE Line 362  CODE VALUE OF NEWLINE
362    
363           --enable-newline-is-anycrlf           --enable-newline-is-anycrlf
364    
365         which causes PCRE to recognize any of the three sequences  CR,  LF,  or         which  causes  PCRE  to recognize any of the three sequences CR, LF, or
366         CRLF as indicating a line ending. Finally, a fifth option, specified by         CRLF as indicating a line ending. Finally, a fifth option, specified by
367    
368           --enable-newline-is-any           --enable-newline-is-any
# Line 361  CODE VALUE OF NEWLINE Line 374  CODE VALUE OF NEWLINE
374         conventional to use the standard for your operating system.         conventional to use the standard for your operating system.
375    
376    
377    WHAT \R MATCHES
378    
379           By default, the sequence \R in a pattern matches  any  Unicode  newline
380           sequence,  whatever  has  been selected as the line ending sequence. If
381           you specify
382    
383             --enable-bsr-anycrlf
384    
385           the default is changed so that \R matches only CR, LF, or  CRLF.  What-
386           ever  is selected when PCRE is built can be overridden when the library
387           functions are called.
388    
389    
390  BUILDING SHARED AND STATIC LIBRARIES  BUILDING SHARED AND STATIC LIBRARIES
391    
392         The PCRE building process uses libtool to build both shared and  static         The PCRE building process uses libtool to build both shared and  static
# Line 496  USING EBCDIC CODE Line 522  USING EBCDIC CODE
522    
523         to the configure command. This setting implies --enable-rebuild-charta-         to the configure command. This setting implies --enable-rebuild-charta-
524         bles.  You  should  only  use  it if you know that you are in an EBCDIC         bles.  You  should  only  use  it if you know that you are in an EBCDIC
525         environment (for example, an IBM mainframe operating system).         environment (for example,  an  IBM  mainframe  operating  system).  The
526           --enable-ebcdic option is incompatible with --enable-utf8.
527    
528    
529    PCREGREP OPTIONS FOR COMPRESSED FILE SUPPORT
530    
531           By default, pcregrep reads all files as plain text. You can build it so
532           that it recognizes files whose names end in .gz or .bz2, and reads them
533           with libz or libbz2, respectively, by adding one or both of
534    
535             --enable-pcregrep-libz
536             --enable-pcregrep-libbz2
537    
538           to the configure command. These options naturally require that the rel-
539           evant libraries are installed on your system. Configuration  will  fail
540           if they are not.
541    
542    
543    PCRETEST OPTION FOR LIBREADLINE SUPPORT
544    
545           If you add
546    
547             --enable-pcretest-libreadline
548    
549           to  the  configure  command,  pcretest  is  linked with the libreadline
550           library, and when its input is from a terminal, it reads it  using  the
551           readline() function. This provides line-editing and history facilities.
552           Note that libreadline is GPL-licenced, so if you distribute a binary of
553           pcretest linked in this way, there may be licensing issues.
554    
555           Setting  this  option  causes  the -lreadline option to be added to the
556           pcretest build. In many operating environments with  a  sytem-installed
557           libreadline this is sufficient. However, in some environments (e.g.  if
558           an unmodified distribution version of readline is in use),  some  extra
559           configuration  may  be necessary. The INSTALL file for libreadline says
560           this:
561    
562             "Readline uses the termcap functions, but does not link with the
563             termcap or curses library itself, allowing applications which link
564             with readline the to choose an appropriate library."
565    
566           If your environment has not been set up so that an appropriate  library
567           is automatically included, you may need to add something like
568    
569             LIBS="-ncurses"
570    
571           immediately before the configure command.
572    
573    
574  SEE ALSO  SEE ALSO
# Line 513  AUTHOR Line 585  AUTHOR
585    
586  REVISION  REVISION
587    
588         Last updated: 30 July 2007         Last updated: 17 March 2009
589         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
590  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
591    
592    
# Line 662  THE ALTERNATIVE MATCHING ALGORITHM Line 734  THE ALTERNATIVE MATCHING ALGORITHM
734         tive algorithm moves through the subject  string  one  character  at  a         tive algorithm moves through the subject  string  one  character  at  a
735         time, for all active paths through the tree.         time, for all active paths through the tree.
736    
737         8.  None  of  the  backtracking control verbs such as (*PRUNE) are sup-         8.  Except for (*FAIL), the backtracking control verbs such as (*PRUNE)
738         ported.         are not supported. (*FAIL) is supported, and  behaves  like  a  failing
739           negative assertion.
740    
741    
742  ADVANTAGES OF THE ALTERNATIVE ALGORITHM  ADVANTAGES OF THE ALTERNATIVE ALGORITHM
743    
744         Using the alternative matching algorithm provides the following  advan-         Using  the alternative matching algorithm provides the following advan-
745         tages:         tages:
746    
747         1. All possible matches (at a single point in the subject) are automat-         1. All possible matches (at a single point in the subject) are automat-
748         ically found, and in particular, the longest match is  found.  To  find         ically  found,  and  in particular, the longest match is found. To find
749         more than one match using the standard algorithm, you have to do kludgy         more than one match using the standard algorithm, you have to do kludgy
750         things with callouts.         things with callouts.
751    
752         2. There is much better support for partial matching. The  restrictions         2.  There is much better support for partial matching. The restrictions
753         on  the content of the pattern that apply when using the standard algo-         on the content of the pattern that apply when using the standard  algo-
754         rithm for partial matching do not apply to the  alternative  algorithm.         rithm  for  partial matching do not apply to the alternative algorithm.
755         For  non-anchored patterns, the starting position of a partial match is         For non-anchored patterns, the starting position of a partial match  is
756         available.         available.
757    
758         3. Because the alternative algorithm  scans  the  subject  string  just         3.  Because  the  alternative  algorithm  scans the subject string just
759         once,  and  never  needs to backtrack, it is possible to pass very long         once, and never needs to backtrack, it is possible to  pass  very  long
760         subject strings to the matching function in  several  pieces,  checking         subject  strings  to  the matching function in several pieces, checking
761         for partial matching each time.         for partial matching each time.
762    
763    
# Line 692  DISADVANTAGES OF THE ALTERNATIVE ALGORIT Line 765  DISADVANTAGES OF THE ALTERNATIVE ALGORIT
765    
766         The alternative algorithm suffers from a number of disadvantages:         The alternative algorithm suffers from a number of disadvantages:
767    
768         1.  It  is  substantially  slower  than the standard algorithm. This is         1. It is substantially slower than  the  standard  algorithm.  This  is
769         partly because it has to search for all possible matches, but  is  also         partly  because  it has to search for all possible matches, but is also
770         because it is less susceptible to optimization.         because it is less susceptible to optimization.
771    
772         2. Capturing parentheses and back references are not supported.         2. Capturing parentheses and back references are not supported.
# Line 711  AUTHOR Line 784  AUTHOR
784    
785  REVISION  REVISION
786    
787         Last updated: 08 August 2007         Last updated: 19 April 2008
788         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2008 University of Cambridge.
789  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
790    
791    
# Line 824  PCRE API OVERVIEW Line 897  PCRE API OVERVIEW
897         a  Perl-compatible  manner. A sample program that demonstrates the sim-         a  Perl-compatible  manner. A sample program that demonstrates the sim-
898         plest way of using them is provided in the file  called  pcredemo.c  in         plest way of using them is provided in the file  called  pcredemo.c  in
899         the  source distribution. The pcresample documentation describes how to         the  source distribution. The pcresample documentation describes how to
900         run it.         compile and run it.
901    
902         A second matching function, pcre_dfa_exec(), which is not Perl-compati-         A second matching function, pcre_dfa_exec(), which is not Perl-compati-
903         ble,  is  also provided. This uses a different algorithm for the match-         ble,  is  also provided. This uses a different algorithm for the match-
# Line 908  NEWLINES Line 981  NEWLINES
981         dard. When PCRE is run, the default can be overridden,  either  when  a         dard. When PCRE is run, the default can be overridden,  either  when  a
982         pattern is compiled, or when it is matched.         pattern is compiled, or when it is matched.
983    
984           At compile time, the newline convention can be specified by the options
985           argument of pcre_compile(), or it can be specified by special  text  at
986           the start of the pattern itself; this overrides any other settings. See
987           the pcrepattern page for details of the special character sequences.
988    
989         In the PCRE documentation the word "newline" is used to mean "the char-         In the PCRE documentation the word "newline" is used to mean "the char-
990         acter or pair of characters that indicate a line break". The choice  of         acter  or pair of characters that indicate a line break". The choice of
991         newline  convention  affects  the  handling of the dot, circumflex, and         newline convention affects the handling of  the  dot,  circumflex,  and
992         dollar metacharacters, the handling of #-comments in /x mode, and, when         dollar metacharacters, the handling of #-comments in /x mode, and, when
993         CRLF  is a recognized line ending sequence, the match position advance-         CRLF is a recognized line ending sequence, the match position  advance-
994         ment for a non-anchored pattern. The choice of newline convention  does         ment for a non-anchored pattern. There is more detail about this in the
995         not affect the interpretation of the \n or \r escape sequences.         section on pcre_exec() options below.
996    
997           The choice of newline convention does not affect the interpretation  of
998           the  \n  or  \r  escape  sequences, nor does it affect what \R matches,
999           which is controlled in a similar way, but by separate options.
1000    
1001    
1002  MULTITHREADING  MULTITHREADING
1003    
1004         The  PCRE  functions  can be used in multi-threading applications, with         The PCRE functions can be used in  multi-threading  applications,  with
1005         the  proviso  that  the  memory  management  functions  pointed  to  by         the  proviso  that  the  memory  management  functions  pointed  to  by
1006         pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the         pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the
1007         callout function pointed to by pcre_callout, are shared by all threads.         callout function pointed to by pcre_callout, are shared by all threads.
# Line 968  CHECKING BUILD-TIME OPTIONS Line 1050  CHECKING BUILD-TIME OPTIONS
1050         The output is an integer whose value specifies  the  default  character         The output is an integer whose value specifies  the  default  character
1051         sequence  that is recognized as meaning "newline". The four values that         sequence  that is recognized as meaning "newline". The four values that
1052         are supported are: 10 for LF, 13 for CR, 3338 for CRLF, -2 for ANYCRLF,         are supported are: 10 for LF, 13 for CR, 3338 for CRLF, -2 for ANYCRLF,
1053         and  -1  for  ANY. The default should normally be the standard sequence         and  -1  for  ANY.  Though they are derived from ASCII, the same values
1054         for your operating system.         are returned in EBCDIC environments. The default should normally corre-
1055           spond to the standard sequence for your operating system.
1056    
1057             PCRE_CONFIG_BSR
1058    
1059           The output is an integer whose value indicates what character sequences
1060           the \R escape sequence matches by default. A value of 0 means  that  \R
1061           matches  any  Unicode  line ending sequence; a value of 1 means that \R
1062           matches only CR, LF, or CRLF. The default can be overridden when a pat-
1063           tern is compiled or matched.
1064    
1065           PCRE_CONFIG_LINK_SIZE           PCRE_CONFIG_LINK_SIZE
1066    
1067         The output is an integer that contains the number  of  bytes  used  for         The  output  is  an  integer that contains the number of bytes used for
1068         internal linkage in compiled regular expressions. The value is 2, 3, or         internal linkage in compiled regular expressions. The value is 2, 3, or
1069         4. Larger values allow larger regular expressions to  be  compiled,  at         4.  Larger  values  allow larger regular expressions to be compiled, at
1070         the  expense  of  slower matching. The default value of 2 is sufficient         the expense of slower matching. The default value of  2  is  sufficient
1071         for all but the most massive patterns, since  it  allows  the  compiled         for  all  but  the  most massive patterns, since it allows the compiled
1072         pattern to be up to 64K in size.         pattern to be up to 64K in size.
1073    
1074           PCRE_CONFIG_POSIX_MALLOC_THRESHOLD           PCRE_CONFIG_POSIX_MALLOC_THRESHOLD
1075    
1076         The  output  is  an integer that contains the threshold above which the         The output is an integer that contains the threshold  above  which  the
1077         POSIX interface uses malloc() for output vectors. Further  details  are         POSIX  interface  uses malloc() for output vectors. Further details are
1078         given in the pcreposix documentation.         given in the pcreposix documentation.
1079    
1080           PCRE_CONFIG_MATCH_LIMIT           PCRE_CONFIG_MATCH_LIMIT
1081    
1082         The output is an integer that gives the default limit for the number of         The output is a long integer that gives the default limit for the  num-
1083         internal matching function calls in a  pcre_exec()  execution.  Further         ber  of  internal  matching  function calls in a pcre_exec() execution.
1084         details are given with pcre_exec() below.         Further details are given with pcre_exec() below.
1085    
1086           PCRE_CONFIG_MATCH_LIMIT_RECURSION           PCRE_CONFIG_MATCH_LIMIT_RECURSION
1087    
1088         The  output is an integer that gives the default limit for the depth of         The output is a long integer that gives the default limit for the depth
1089         recursion when calling the internal matching function in a  pcre_exec()         of   recursion  when  calling  the  internal  matching  function  in  a
1090         execution. Further details are given with pcre_exec() below.         pcre_exec() execution.  Further  details  are  given  with  pcre_exec()
1091           below.
1092    
1093           PCRE_CONFIG_STACKRECURSE           PCRE_CONFIG_STACKRECURSE
1094    
# Line 1100  COMPILING A PATTERN Line 1192  COMPILING A PATTERN
1192         all with number 255, before each pattern item. For  discussion  of  the         all with number 255, before each pattern item. For  discussion  of  the
1193         callout facility, see the pcrecallout documentation.         callout facility, see the pcrecallout documentation.
1194    
1195             PCRE_BSR_ANYCRLF
1196             PCRE_BSR_UNICODE
1197    
1198           These options (which are mutually exclusive) control what the \R escape
1199           sequence matches. The choice is either to match only CR, LF,  or  CRLF,
1200           or to match any Unicode newline sequence. The default is specified when
1201           PCRE is built. It can be overridden from within the pattern, or by set-
1202           ting an option when a compiled pattern is matched.
1203    
1204           PCRE_CASELESS           PCRE_CASELESS
1205    
1206         If  this  bit is set, letters in the pattern match both upper and lower         If  this  bit is set, letters in the pattern match both upper and lower
# Line 1173  COMPILING A PATTERN Line 1274  COMPILING A PATTERN
1274         before  or  at  the  first  newline  in  the subject string, though the         before  or  at  the  first  newline  in  the subject string, though the
1275         matched text may continue over the newline.         matched text may continue over the newline.
1276    
1277             PCRE_JAVASCRIPT_COMPAT
1278    
1279           If this option is set, PCRE's behaviour is changed in some ways so that
1280           it  is  compatible with JavaScript rather than Perl. The changes are as
1281           follows:
1282    
1283           (1) A lone closing square bracket in a pattern  causes  a  compile-time
1284           error,  because this is illegal in JavaScript (by default it is treated
1285           as a data character). Thus, the pattern AB]CD becomes illegal when this
1286           option is set.
1287    
1288           (2)  At run time, a back reference to an unset subpattern group matches
1289           an empty string (by default this causes the current  matching  alterna-
1290           tive  to  fail). A pattern such as (\1)(a) succeeds when this option is
1291           set (assuming it can find an "a" in the subject), whereas it  fails  by
1292           default, for Perl compatibility.
1293    
1294           PCRE_MULTILINE           PCRE_MULTILINE
1295    
1296         By default, PCRE treats the subject string as consisting  of  a  single         By  default,  PCRE  treats the subject string as consisting of a single
1297         line  of characters (even if it actually contains newlines). The "start         line of characters (even if it actually contains newlines). The  "start
1298         of line" metacharacter (^) matches only at the  start  of  the  string,         of  line"  metacharacter  (^)  matches only at the start of the string,
1299         while  the  "end  of line" metacharacter ($) matches only at the end of         while the "end of line" metacharacter ($) matches only at  the  end  of
1300         the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY         the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY
1301         is set). This is the same as Perl.         is set). This is the same as Perl.
1302    
1303         When  PCRE_MULTILINE  it  is set, the "start of line" and "end of line"         When PCRE_MULTILINE it is set, the "start of line" and  "end  of  line"
1304         constructs match immediately following or immediately  before  internal         constructs  match  immediately following or immediately before internal
1305         newlines  in  the  subject string, respectively, as well as at the very         newlines in the subject string, respectively, as well as  at  the  very
1306         start and end. This is equivalent to Perl's /m option, and  it  can  be         start  and  end.  This is equivalent to Perl's /m option, and it can be
1307         changed within a pattern by a (?m) option setting. If there are no new-         changed within a pattern by a (?m) option setting. If there are no new-
1308         lines in a subject string, or no occurrences of ^ or $  in  a  pattern,         lines  in  a  subject string, or no occurrences of ^ or $ in a pattern,
1309         setting PCRE_MULTILINE has no effect.         setting PCRE_MULTILINE has no effect.
1310    
1311           PCRE_NEWLINE_CR           PCRE_NEWLINE_CR
# Line 1196  COMPILING A PATTERN Line 1314  COMPILING A PATTERN
1314           PCRE_NEWLINE_ANYCRLF           PCRE_NEWLINE_ANYCRLF
1315           PCRE_NEWLINE_ANY           PCRE_NEWLINE_ANY
1316    
1317         These  options  override the default newline definition that was chosen         These options override the default newline definition that  was  chosen
1318         when PCRE was built. Setting the first or the second specifies  that  a         when  PCRE  was built. Setting the first or the second specifies that a
1319         newline  is  indicated  by a single character (CR or LF, respectively).         newline is indicated by a single character (CR  or  LF,  respectively).
1320         Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by  the         Setting  PCRE_NEWLINE_CRLF specifies that a newline is indicated by the
1321         two-character  CRLF  sequence.  Setting  PCRE_NEWLINE_ANYCRLF specifies         two-character CRLF  sequence.  Setting  PCRE_NEWLINE_ANYCRLF  specifies
1322         that any of the three preceding sequences should be recognized. Setting         that any of the three preceding sequences should be recognized. Setting
1323         PCRE_NEWLINE_ANY  specifies that any Unicode newline sequence should be         PCRE_NEWLINE_ANY specifies that any Unicode newline sequence should  be
1324         recognized. The Unicode newline sequences are the three just mentioned,         recognized. The Unicode newline sequences are the three just mentioned,
1325         plus  the  single  characters  VT (vertical tab, U+000B), FF (formfeed,         plus the single characters VT (vertical  tab,  U+000B),  FF  (formfeed,
1326         U+000C), NEL (next line, U+0085), LS (line separator, U+2028),  and  PS         U+000C),  NEL  (next line, U+0085), LS (line separator, U+2028), and PS
1327         (paragraph  separator,  U+2029).  The  last  two are recognized only in         (paragraph separator, U+2029). The last  two  are  recognized  only  in
1328         UTF-8 mode.         UTF-8 mode.
1329    
1330         The newline setting in the  options  word  uses  three  bits  that  are         The  newline  setting  in  the  options  word  uses three bits that are
1331         treated as a number, giving eight possibilities. Currently only six are         treated as a number, giving eight possibilities. Currently only six are
1332         used (default plus the five values above). This means that if  you  set         used  (default  plus the five values above). This means that if you set
1333         more  than one newline option, the combination may or may not be sensi-         more than one newline option, the combination may or may not be  sensi-
1334         ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to         ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to
1335         PCRE_NEWLINE_CRLF,  but other combinations may yield unused numbers and         PCRE_NEWLINE_CRLF, but other combinations may yield unused numbers  and
1336         cause an error.         cause an error.
1337    
1338         The only time that a line break is specially recognized when  compiling         The  only time that a line break is specially recognized when compiling
1339         a  pattern  is  if  PCRE_EXTENDED  is set, and an unescaped # outside a         a pattern is if PCRE_EXTENDED is set, and  an  unescaped  #  outside  a
1340         character class is encountered. This indicates  a  comment  that  lasts         character  class  is  encountered.  This indicates a comment that lasts
1341         until  after the next line break sequence. In other circumstances, line         until after the next line break sequence. In other circumstances,  line
1342         break  sequences  are  treated  as  literal  data,   except   that   in         break   sequences   are   treated  as  literal  data,  except  that  in
1343         PCRE_EXTENDED mode, both CR and LF are treated as whitespace characters         PCRE_EXTENDED mode, both CR and LF are treated as whitespace characters
1344         and are therefore ignored.         and are therefore ignored.
1345    
1346         The newline option that is set at compile time becomes the default that         The newline option that is set at compile time becomes the default that
1347         is  used for pcre_exec() and pcre_dfa_exec(), but it can be overridden.         is used for pcre_exec() and pcre_dfa_exec(), but it can be overridden.
1348    
1349           PCRE_NO_AUTO_CAPTURE           PCRE_NO_AUTO_CAPTURE
1350    
# Line 1285  COMPILATION ERROR CODES Line 1403  COMPILATION ERROR CODES
1403            9  nothing to repeat            9  nothing to repeat
1404           10  [this code is not in use]           10  [this code is not in use]
1405           11  internal error: unexpected repeat           11  internal error: unexpected repeat
1406           12  unrecognized character after (?           12  unrecognized character after (? or (?-
1407           13  POSIX named classes are supported only within a class           13  POSIX named classes are supported only within a class
1408           14  missing )           14  missing )
1409           15  reference to non-existent subpattern           15  reference to non-existent subpattern
# Line 1293  COMPILATION ERROR CODES Line 1411  COMPILATION ERROR CODES
1411           17  unknown option bit(s) set           17  unknown option bit(s) set
1412           18  missing ) after comment           18  missing ) after comment
1413           19  [this code is not in use]           19  [this code is not in use]
1414           20  regular expression too large           20  regular expression is too large
1415           21  failed to get memory           21  failed to get memory
1416           22  unmatched parentheses           22  unmatched parentheses
1417           23  internal error: code overflow           23  internal error: code overflow
# Line 1322  COMPILATION ERROR CODES Line 1440  COMPILATION ERROR CODES
1440           46  malformed \P or \p sequence           46  malformed \P or \p sequence
1441           47  unknown property name after \P or \p           47  unknown property name after \P or \p
1442           48  subpattern name is too long (maximum 32 characters)           48  subpattern name is too long (maximum 32 characters)
1443           49  too many named subpatterns (maximum 10,000)           49  too many named subpatterns (maximum 10000)
1444           50  [this code is not in use]           50  [this code is not in use]
1445           51  octal value is greater than \377 (not in UTF-8 mode)           51  octal value is greater than \377 (not in UTF-8 mode)
1446           52  internal error: overran compiling workspace           52  internal error: overran compiling workspace
# Line 1330  COMPILATION ERROR CODES Line 1448  COMPILATION ERROR CODES
1448         found         found
1449           54  DEFINE group contains more than one branch           54  DEFINE group contains more than one branch
1450           55  repeating a DEFINE group is not allowed           55  repeating a DEFINE group is not allowed
1451           56  inconsistent NEWLINE options"           56  inconsistent NEWLINE options
1452           57  \g is not followed by a braced name or an optionally braced           57  \g is not followed by a braced, angle-bracketed, or quoted
1453                 non-zero number                 name/number or by a plain number
1454           58  (?+ or (?- or (?(+ or (?(- must be followed by a non-zero number           58  a numbered reference must not be zero
1455             59  (*VERB) with an argument is not supported
1456             60  (*VERB) not recognized
1457             61  number is too big
1458             62  subpattern name expected
1459             63  digit expected after (?+
1460             64  ] is an invalid data character in JavaScript compatibility mode
1461    
1462           The  numbers  32  and 10000 in errors 48 and 49 are defaults; different
1463           values may be used if the limits were changed when PCRE was built.
1464    
1465    
1466  STUDYING A PATTERN  STUDYING A PATTERN
# Line 1341  STUDYING A PATTERN Line 1468  STUDYING A PATTERN
1468         pcre_extra *pcre_study(const pcre *code, int options         pcre_extra *pcre_study(const pcre *code, int options
1469              const char **errptr);              const char **errptr);
1470    
1471         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
1472         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
1473         matching.  The function pcre_study() takes a pointer to a compiled pat-         matching. The function pcre_study() takes a pointer to a compiled  pat-
1474         tern as its first argument. If studying the pattern produces additional         tern as its first argument. If studying the pattern produces additional
1475         information  that  will  help speed up matching, pcre_study() returns a         information that will help speed up matching,  pcre_study()  returns  a
1476         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
1477         the results of the study.         the results of the study.
1478    
1479         The  returned  value  from  pcre_study()  can  be  passed  directly  to         The  returned  value  from  pcre_study()  can  be  passed  directly  to
1480         pcre_exec(). However, a pcre_extra block  also  contains  other  fields         pcre_exec().  However,  a  pcre_extra  block also contains other fields
1481         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
1482         described below in the section on matching a pattern.         described below in the section on matching a pattern.
1483    
1484         If studying the pattern does not  produce  any  additional  information         If  studying  the  pattern  does not produce any additional information
1485         pcre_study() returns NULL. In that circumstance, if the calling program         pcre_study() returns NULL. In that circumstance, if the calling program
1486         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
1487         its own pcre_extra block.         its own pcre_extra block.
1488    
1489         The  second  argument of pcre_study() contains option bits. At present,         The second argument of pcre_study() contains option bits.  At  present,
1490         no options are defined, and this argument should always be zero.         no options are defined, and this argument should always be zero.
1491    
1492         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.
1493         If  studying  succeeds  (even  if no data is returned), the variable it         If studying succeeds (even if no data is  returned),  the  variable  it
1494         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
1495         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
1496         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
1497         after calling pcre_study(), to be sure that it has run successfully.         after calling pcre_study(), to be sure that it has run successfully.
1498    
1499         This is a typical call to pcre_study():         This is a typical call to pcre_study():
# Line 1378  STUDYING A PATTERN Line 1505  STUDYING A PATTERN
1505             &error);        /* set to NULL or points to a message */             &error);        /* set to NULL or points to a message */
1506    
1507         At present, studying a pattern is useful only for non-anchored patterns         At present, studying a pattern is useful only for non-anchored patterns
1508         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-
1509         ble starting bytes is created.         ble starting bytes is created.
1510    
1511    
1512  LOCALE SUPPORT  LOCALE SUPPORT
1513    
1514         PCRE  handles  caseless matching, and determines whether characters are         PCRE handles caseless matching, and determines whether  characters  are
1515         letters, digits, or whatever, by reference to a set of tables,  indexed         letters,  digits, or whatever, by reference to a set of tables, indexed
1516         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
1517         characters with codes less than 128. Higher-valued  codes  never  match         characters  with  codes  less than 128. Higher-valued codes never match
1518         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
1519         with Unicode character property support. The use of locales  with  Uni-         with  Unicode  character property support. The use of locales with Uni-
1520         code  is discouraged. If you are handling characters with codes greater         code is discouraged. If you are handling characters with codes  greater
1521         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
1522         not try to mix the two.         not try to mix the two.
1523    
1524         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
1525         argument of pcre_compile() is  NULL.  These  are  sufficient  for  many         argument  of  pcre_compile()  is  NULL.  These  are sufficient for many
1526         applications.  Normally, the internal tables recognize only ASCII char-         applications.  Normally, the internal tables recognize only ASCII char-
1527         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-
1528         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,
1529         which may cause them to be different.         which may cause them to be different.
1530    
1531         The internal tables can always be overridden by tables supplied by  the         The  internal tables can always be overridden by tables supplied by the
1532         application that calls PCRE. These may be created in a different locale         application that calls PCRE. These may be created in a different locale
1533         from the default. As more and more applications change  to  using  Uni-         from  the  default.  As more and more applications change to using Uni-
1534         code, the need for this locale support is expected to die away.         code, the need for this locale support is expected to die away.
1535    
1536         External  tables  are  built by calling the pcre_maketables() function,         External tables are built by calling  the  pcre_maketables()  function,
1537         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
1538         passed  to  pcre_compile()  or  pcre_exec()  as often as necessary. For         passed to pcre_compile() or pcre_exec()  as  often  as  necessary.  For
1539         example, to build and use tables that are appropriate  for  the  French         example,  to  build  and use tables that are appropriate for the French
1540         locale  (where  accented  characters  with  values greater than 128 are         locale (where accented characters with  values  greater  than  128  are
1541         treated as letters), the following code could be used:         treated as letters), the following code could be used:
1542    
1543           setlocale(LC_CTYPE, "fr_FR");           setlocale(LC_CTYPE, "fr_FR");
1544           tables = pcre_maketables();           tables = pcre_maketables();
1545           re = pcre_compile(..., tables);           re = pcre_compile(..., tables);
1546    
1547         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;
1548         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".
1549    
1550         When  pcre_maketables()  runs,  the  tables are built in memory that is         When pcre_maketables() runs, the tables are built  in  memory  that  is
1551         obtained via pcre_malloc. It is the caller's responsibility  to  ensure         obtained  via  pcre_malloc. It is the caller's responsibility to ensure
1552         that  the memory containing the tables remains available for as long as         that the memory containing the tables remains available for as long  as
1553         it is needed.         it is needed.
1554    
1555         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
1556         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()
1557         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-
1558         tern, compilation, studying and matching all happen in the same locale,         tern, compilation, studying and matching all happen in the same locale,
1559         but different patterns can be compiled in different locales.         but different patterns can be compiled in different locales.
1560    
1561         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
1562         the  internal  tables)  to  pcre_exec(). Although not intended for this         the internal tables) to pcre_exec(). Although  not  intended  for  this
1563         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
1564         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
1565         run time is discussed below in the section on matching a pattern.         run time is discussed below in the section on matching a pattern.
1566    
# Line 1443  INFORMATION ABOUT A PATTERN Line 1570  INFORMATION ABOUT A PATTERN
1570         int pcre_fullinfo(const pcre *code, const pcre_extra *extra,         int pcre_fullinfo(const pcre *code, const pcre_extra *extra,
1571              int what, void *where);              int what, void *where);
1572    
1573         The pcre_fullinfo() function returns information about a compiled  pat-         The  pcre_fullinfo() function returns information about a compiled pat-
1574         tern. It replaces the obsolete pcre_info() function, which is neverthe-         tern. It replaces the obsolete pcre_info() function, which is neverthe-
1575         less retained for backwards compability (and is documented below).         less retained for backwards compability (and is documented below).
1576    
1577         The first argument for pcre_fullinfo() is a  pointer  to  the  compiled         The  first  argument  for  pcre_fullinfo() is a pointer to the compiled
1578         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
1579         the pattern was not studied. The third argument specifies  which  piece         the  pattern  was not studied. The third argument specifies which piece
1580         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
1581         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
1582         success, or one of the following negative numbers:         success, or one of the following negative numbers:
1583    
1584           PCRE_ERROR_NULL       the argument code was NULL           PCRE_ERROR_NULL       the argument code was NULL
# Line 1459  INFORMATION ABOUT A PATTERN Line 1586  INFORMATION ABOUT A PATTERN
1586           PCRE_ERROR_BADMAGIC   the "magic number" was not found           PCRE_ERROR_BADMAGIC   the "magic number" was not found
1587           PCRE_ERROR_BADOPTION  the value of what was invalid           PCRE_ERROR_BADOPTION  the value of what was invalid
1588    
1589         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
1590         an simple check against passing an arbitrary memory pointer. Here is  a         an  simple check against passing an arbitrary memory pointer. Here is a
1591         typical  call  of pcre_fullinfo(), to obtain the length of the compiled         typical call of pcre_fullinfo(), to obtain the length of  the  compiled
1592         pattern:         pattern:
1593    
1594           int rc;           int rc;
# Line 1472  INFORMATION ABOUT A PATTERN Line 1599  INFORMATION ABOUT A PATTERN
1599             PCRE_INFO_SIZE,   /* what is required */             PCRE_INFO_SIZE,   /* what is required */
1600             &length);         /* where to put the data */             &length);         /* where to put the data */
1601    
1602         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
1603         are as follows:         are as follows:
1604    
1605           PCRE_INFO_BACKREFMAX           PCRE_INFO_BACKREFMAX
1606    
1607         Return  the  number  of  the highest back reference in the pattern. The         Return the number of the highest back reference  in  the  pattern.  The
1608         fourth argument should point to an int variable. Zero  is  returned  if         fourth  argument  should  point to an int variable. Zero is returned if
1609         there are no back references.         there are no back references.
1610    
1611           PCRE_INFO_CAPTURECOUNT           PCRE_INFO_CAPTURECOUNT
1612    
1613         Return  the  number of capturing subpatterns in the pattern. The fourth         Return the number of capturing subpatterns in the pattern.  The  fourth
1614         argument should point to an int variable.         argument should point to an int variable.
1615    
1616           PCRE_INFO_DEFAULT_TABLES           PCRE_INFO_DEFAULT_TABLES
1617    
1618         Return a pointer to the internal default character tables within  PCRE.         Return  a pointer to the internal default character tables within PCRE.
1619         The  fourth  argument should point to an unsigned char * variable. This         The fourth argument should point to an unsigned char *  variable.  This
1620         information call is provided for internal use by the pcre_study() func-         information call is provided for internal use by the pcre_study() func-
1621         tion.  External  callers  can  cause PCRE to use its internal tables by         tion. External callers can cause PCRE to use  its  internal  tables  by
1622         passing a NULL table pointer.         passing a NULL table pointer.
1623    
1624           PCRE_INFO_FIRSTBYTE           PCRE_INFO_FIRSTBYTE
1625    
1626         Return information about the first byte of any matched  string,  for  a         Return  information  about  the first byte of any matched string, for a
1627         non-anchored  pattern. The fourth argument should point to an int vari-         non-anchored pattern. The fourth argument should point to an int  vari-
1628         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
1629         is still recognized for backwards compatibility.)         is still recognized for backwards compatibility.)
1630    
1631         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
1632         (cat|cow|coyote), its value is returned. Otherwise, if either         (cat|cow|coyote), its value is returned. Otherwise, if either
1633    
1634         (a) the pattern was compiled with the PCRE_MULTILINE option, and  every         (a)  the pattern was compiled with the PCRE_MULTILINE option, and every
1635         branch starts with "^", or         branch starts with "^", or
1636    
1637         (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
1638         set (if it were set, the pattern would be anchored),         set (if it were set, the pattern would be anchored),
1639    
1640         -1 is returned, indicating that the pattern matches only at  the  start         -1  is  returned, indicating that the pattern matches only at the start
1641         of  a  subject string or after any newline within the string. Otherwise         of a subject string or after any newline within the  string.  Otherwise
1642         -2 is returned. For anchored patterns, -2 is returned.         -2 is returned. For anchored patterns, -2 is returned.
1643    
1644           PCRE_INFO_FIRSTTABLE           PCRE_INFO_FIRSTTABLE
1645    
1646         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
1647         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
1648         matching string, a pointer to the table is returned. Otherwise NULL  is         matching  string, a pointer to the table is returned. Otherwise NULL is
1649         returned.  The fourth argument should point to an unsigned char * vari-         returned. The fourth argument should point to an unsigned char *  vari-
1650         able.         able.
1651    
1652             PCRE_INFO_HASCRORLF
1653    
1654           Return  1  if  the  pattern  contains any explicit matches for CR or LF
1655           characters, otherwise 0. The fourth argument should  point  to  an  int
1656           variable.  An explicit match is either a literal CR or LF character, or
1657           \r or \n.
1658    
1659           PCRE_INFO_JCHANGED           PCRE_INFO_JCHANGED
1660    
1661         Return 1 if the (?J) option setting is used in the  pattern,  otherwise         Return 1 if the (?J) or (?-J) option setting is used  in  the  pattern,
1662         0. The fourth argument should point to an int variable. The (?J) inter-         otherwise  0. The fourth argument should point to an int variable. (?J)
1663         nal option setting changes the local PCRE_DUPNAMES option.         and (?-J) set and unset the local PCRE_DUPNAMES option, respectively.
1664    
1665           PCRE_INFO_LASTLITERAL           PCRE_INFO_LASTLITERAL
1666    
# Line 1678  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1812  MATCHING A PATTERN: THE TRADITIONAL FUNC
1812              const char *subject, int length, int startoffset,              const char *subject, int length, int startoffset,
1813              int options, int *ovector, int ovecsize);              int options, int *ovector, int ovecsize);
1814    
1815         The  function pcre_exec() is called to match a subject string against a         The function pcre_exec() is called to match a subject string against  a
1816         compiled pattern, which is passed in the code argument. If the  pattern         compiled  pattern, which is passed in the code argument. If the pattern
1817         has been studied, the result of the study should be passed in the extra         has been studied, the result of the study should be passed in the extra
1818         argument. This function is the main matching facility of  the  library,         argument.  This  function is the main matching facility of the library,
1819         and it operates in a Perl-like manner. For specialist use there is also         and it operates in a Perl-like manner. For specialist use there is also
1820         an alternative matching function, which is described below in the  sec-         an  alternative matching function, which is described below in the sec-
1821         tion about the pcre_dfa_exec() function.         tion about the pcre_dfa_exec() function.
1822    
1823         In  most applications, the pattern will have been compiled (and option-         In most applications, the pattern will have been compiled (and  option-
1824         ally studied) in the same process that calls pcre_exec().  However,  it         ally  studied)  in the same process that calls pcre_exec(). However, it
1825         is possible to save compiled patterns and study data, and then use them         is possible to save compiled patterns and study data, and then use them
1826         later in different processes, possibly even on different hosts.  For  a         later  in  different processes, possibly even on different hosts. For a
1827         discussion about this, see the pcreprecompile documentation.         discussion about this, see the pcreprecompile documentation.
1828    
1829         Here is an example of a simple call to pcre_exec():         Here is an example of a simple call to pcre_exec():
# Line 1708  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1842  MATCHING A PATTERN: THE TRADITIONAL FUNC
1842    
1843     Extra data for pcre_exec()     Extra data for pcre_exec()
1844    
1845         If  the  extra argument is not NULL, it must point to a pcre_extra data         If the extra argument is not NULL, it must point to a  pcre_extra  data
1846         block. The pcre_study() function returns such a block (when it  doesn't         block.  The pcre_study() function returns such a block (when it doesn't
1847         return  NULL), but you can also create one for yourself, and pass addi-         return NULL), but you can also create one for yourself, and pass  addi-
1848         tional information in it. The pcre_extra block contains  the  following         tional  information  in it. The pcre_extra block contains the following
1849         fields (not necessarily in this order):         fields (not necessarily in this order):
1850    
1851           unsigned long int flags;           unsigned long int flags;
# Line 1721  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1855  MATCHING A PATTERN: THE TRADITIONAL FUNC
1855           void *callout_data;           void *callout_data;
1856           const unsigned char *tables;           const unsigned char *tables;
1857    
1858         The  flags  field  is a bitmap that specifies which of the other fields         The flags field is a bitmap that specifies which of  the  other  fields
1859         are set. The flag bits are:         are set. The flag bits are:
1860    
1861           PCRE_EXTRA_STUDY_DATA           PCRE_EXTRA_STUDY_DATA
# Line 1730  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1864  MATCHING A PATTERN: THE TRADITIONAL FUNC
1864           PCRE_EXTRA_CALLOUT_DATA           PCRE_EXTRA_CALLOUT_DATA
1865           PCRE_EXTRA_TABLES           PCRE_EXTRA_TABLES
1866    
1867         Other flag bits should be set to zero. The study_data field is  set  in         Other  flag  bits should be set to zero. The study_data field is set in
1868         the  pcre_extra  block  that is returned by pcre_study(), together with         the pcre_extra block that is returned by  pcre_study(),  together  with
1869         the appropriate flag bit. You should not set this yourself, but you may         the appropriate flag bit. You should not set this yourself, but you may
1870         add  to  the  block by setting the other fields and their corresponding         add to the block by setting the other fields  and  their  corresponding
1871         flag bits.         flag bits.
1872    
1873         The match_limit field provides a means of preventing PCRE from using up         The match_limit field provides a means of preventing PCRE from using up
1874         a  vast amount of resources when running patterns that are not going to         a vast amount of resources when running patterns that are not going  to
1875         match, but which have a very large number  of  possibilities  in  their         match,  but  which  have  a very large number of possibilities in their
1876         search  trees.  The  classic  example  is  the  use of nested unlimited         search trees. The classic  example  is  the  use  of  nested  unlimited
1877         repeats.         repeats.
1878    
1879         Internally, PCRE uses a function called match() which it calls  repeat-         Internally,  PCRE uses a function called match() which it calls repeat-
1880         edly  (sometimes  recursively). The limit set by match_limit is imposed         edly (sometimes recursively). The limit set by match_limit  is  imposed
1881         on the number of times this function is called during  a  match,  which         on  the  number  of times this function is called during a match, which
1882         has  the  effect  of  limiting the amount of backtracking that can take         has the effect of limiting the amount of  backtracking  that  can  take
1883         place. For patterns that are not anchored, the count restarts from zero         place. For patterns that are not anchored, the count restarts from zero
1884         for each position in the subject string.         for each position in the subject string.
1885    
1886         The  default  value  for  the  limit can be set when PCRE is built; the         The default value for the limit can be set  when  PCRE  is  built;  the
1887         default default is 10 million, which handles all but the  most  extreme         default  default  is 10 million, which handles all but the most extreme
1888         cases.  You  can  override  the  default by suppling pcre_exec() with a         cases. You can override the default  by  suppling  pcre_exec()  with  a
1889         pcre_extra    block    in    which    match_limit    is    set,     and         pcre_extra     block    in    which    match_limit    is    set,    and
1890         PCRE_EXTRA_MATCH_LIMIT  is  set  in  the  flags  field. If the limit is         PCRE_EXTRA_MATCH_LIMIT is set in the  flags  field.  If  the  limit  is
1891         exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT.         exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT.
1892    
1893         The match_limit_recursion field is similar to match_limit, but  instead         The  match_limit_recursion field is similar to match_limit, but instead
1894         of limiting the total number of times that match() is called, it limits         of limiting the total number of times that match() is called, it limits
1895         the depth of recursion. The recursion depth is a  smaller  number  than         the  depth  of  recursion. The recursion depth is a smaller number than
1896         the  total number of calls, because not all calls to match() are recur-         the total number of calls, because not all calls to match() are  recur-
1897         sive.  This limit is of use only if it is set smaller than match_limit.         sive.  This limit is of use only if it is set smaller than match_limit.
1898    
1899         Limiting  the  recursion  depth  limits the amount of stack that can be         Limiting  the  recursion  depth  limits the amount of stack that can be
# Line 1791  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1925  MATCHING A PATTERN: THE TRADITIONAL FUNC
1925    
1926         The unused bits of the options argument for pcre_exec() must  be  zero.         The unused bits of the options argument for pcre_exec() must  be  zero.
1927         The  only  bits  that  may  be set are PCRE_ANCHORED, PCRE_NEWLINE_xxx,         The  only  bits  that  may  be set are PCRE_ANCHORED, PCRE_NEWLINE_xxx,
1928         PCRE_NOTBOL,   PCRE_NOTEOL,   PCRE_NOTEMPTY,   PCRE_NO_UTF8_CHECK   and         PCRE_NOTBOL,   PCRE_NOTEOL,   PCRE_NOTEMPTY,    PCRE_NO_START_OPTIMIZE,
1929         PCRE_PARTIAL.         PCRE_NO_UTF8_CHECK and PCRE_PARTIAL.
1930    
1931           PCRE_ANCHORED           PCRE_ANCHORED
1932    
# Line 1801  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 1935  MATCHING A PATTERN: THE TRADITIONAL FUNC
1935         turned  out to be anchored by virtue of its contents, it cannot be made         turned  out to be anchored by virtue of its contents, it cannot be made
1936         unachored at matching time.         unachored at matching time.
1937    
1938             PCRE_BSR_ANYCRLF
1939             PCRE_BSR_UNICODE
1940    
1941           These options (which are mutually exclusive) control what the \R escape
1942           sequence  matches.  The choice is either to match only CR, LF, or CRLF,
1943           or to match any Unicode newline sequence. These  options  override  the
1944           choice that was made or defaulted when the pattern was compiled.
1945    
1946           PCRE_NEWLINE_CR           PCRE_NEWLINE_CR
1947           PCRE_NEWLINE_LF           PCRE_NEWLINE_LF
1948           PCRE_NEWLINE_CRLF           PCRE_NEWLINE_CRLF
1949           PCRE_NEWLINE_ANYCRLF           PCRE_NEWLINE_ANYCRLF
1950           PCRE_NEWLINE_ANY           PCRE_NEWLINE_ANY
1951    
1952         These options override  the  newline  definition  that  was  chosen  or         These  options  override  the  newline  definition  that  was chosen or
1953         defaulted  when the pattern was compiled. For details, see the descrip-         defaulted when the pattern was compiled. For details, see the  descrip-
1954         tion of pcre_compile()  above.  During  matching,  the  newline  choice         tion  of  pcre_compile()  above.  During  matching,  the newline choice
1955         affects  the  behaviour  of the dot, circumflex, and dollar metacharac-         affects the behaviour of the dot, circumflex,  and  dollar  metacharac-
1956         ters. It may also alter the way the match position is advanced after  a         ters.  It may also alter the way the match position is advanced after a
1957         match  failure  for  an  unanchored  pattern.  When  PCRE_NEWLINE_CRLF,         match failure for an unanchored pattern.
1958         PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is set, and a  match  attempt  
1959         fails  when the current position is at a CRLF sequence, the match posi-         When PCRE_NEWLINE_CRLF, PCRE_NEWLINE_ANYCRLF,  or  PCRE_NEWLINE_ANY  is
1960         tion is advanced by two characters instead of one, in other  words,  to         set,  and a match attempt for an unanchored pattern fails when the cur-
1961         after the CRLF.         rent position is at a  CRLF  sequence,  and  the  pattern  contains  no
1962           explicit  matches  for  CR  or  LF  characters,  the  match position is
1963           advanced by two characters instead of one, in other words, to after the
1964           CRLF.
1965    
1966           The above rule is a compromise that makes the most common cases work as
1967           expected. For example, if the  pattern  is  .+A  (and  the  PCRE_DOTALL
1968           option is not set), it does not match the string "\r\nA" because, after
1969           failing at the start, it skips both the CR and the LF before  retrying.
1970           However,  the  pattern  [\r\n]A does match that string, because it con-
1971           tains an explicit CR or LF reference, and so advances only by one char-
1972           acter after the first failure.
1973    
1974           An explicit match for CR of LF is either a literal appearance of one of
1975           those characters, or one of the \r or  \n  escape  sequences.  Implicit
1976           matches  such  as [^X] do not count, nor does \s (which includes CR and
1977           LF in the characters that it matches).
1978    
1979           Notwithstanding the above, anomalous effects may still occur when  CRLF
1980           is a valid newline sequence and explicit \r or \n escapes appear in the
1981           pattern.
1982    
1983           PCRE_NOTBOL           PCRE_NOTBOL
1984    
1985         This option specifies that first character of the subject string is not         This option specifies that first character of the subject string is not
1986         the beginning of a line, so the  circumflex  metacharacter  should  not         the  beginning  of  a  line, so the circumflex metacharacter should not
1987         match  before it. Setting this without PCRE_MULTILINE (at compile time)         match before it. Setting this without PCRE_MULTILINE (at compile  time)
1988         causes circumflex never to match. This option affects only  the  behav-         causes  circumflex  never to match. This option affects only the behav-
1989         iour of the circumflex metacharacter. It does not affect \A.         iour of the circumflex metacharacter. It does not affect \A.
1990    
1991           PCRE_NOTEOL           PCRE_NOTEOL
1992    
1993         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
1994         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
1995         in  multiline mode) a newline immediately before it. Setting this with-         in multiline mode) a newline immediately before it. Setting this  with-
1996         out PCRE_MULTILINE (at compile time) causes dollar never to match. This         out PCRE_MULTILINE (at compile time) causes dollar never to match. This
1997         option  affects only the behaviour of the dollar metacharacter. It does         option affects only the behaviour of the dollar metacharacter. It  does
1998         not affect \Z or \z.         not affect \Z or \z.
1999    
2000           PCRE_NOTEMPTY           PCRE_NOTEMPTY
2001    
2002         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
2003         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
2004         the alternatives match the empty string, the entire  match  fails.  For         the  alternatives  match  the empty string, the entire match fails. For
2005         example, if the pattern         example, if the pattern
2006    
2007           a?b?           a?b?
2008    
2009         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
2010         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
2011         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-
2012         rences of "a" or "b".         rences of "a" or "b".
2013    
2014         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-
2015         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()
2016         function, and when using the /g modifier. It  is  possible  to  emulate         function,  and  when  using  the /g modifier. It is possible to emulate
2017         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
2018         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
2019         if  that  fails by advancing the starting offset (see below) and trying         if that fails by advancing the starting offset (see below)  and  trying
2020         an ordinary match again. There is some code that demonstrates how to do         an ordinary match again. There is some code that demonstrates how to do
2021         this in the pcredemo.c sample program.         this in the pcredemo.c sample program.
2022    
2023             PCRE_NO_START_OPTIMIZE
2024    
2025           There are a number of optimizations that pcre_exec() uses at the  start
2026           of  a  match,  in  order to speed up the process. For example, if it is
2027           known that a match must start with a specific  character,  it  searches
2028           the subject for that character, and fails immediately if it cannot find
2029           it, without actually running the main matching function. When  callouts
2030           are  in  use,  these  optimizations  can cause them to be skipped. This
2031           option disables the "start-up" optimizations,  causing  performance  to
2032           suffer, but ensuring that the callouts do occur.
2033    
2034           PCRE_NO_UTF8_CHECK           PCRE_NO_UTF8_CHECK
2035    
2036         When PCRE_UTF8 is set at compile time, the validity of the subject as a         When PCRE_UTF8 is set at compile time, the validity of the subject as a
# Line 1894  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2067  MATCHING A PATTERN: THE TRADITIONAL FUNC
2067     The string to be matched by pcre_exec()     The string to be matched by pcre_exec()
2068    
2069         The subject string is passed to pcre_exec() as a pointer in subject,  a         The subject string is passed to pcre_exec() as a pointer in subject,  a
2070         length  in  length, and a starting byte offset in startoffset. In UTF-8         length (in bytes) in length, and a starting byte offset in startoffset.
2071         mode, the byte offset must point to the start  of  a  UTF-8  character.         In UTF-8 mode, the byte offset must point to the start of a UTF-8 char-
2072         Unlike  the  pattern string, the subject may contain binary zero bytes.         acter.  Unlike  the pattern string, the subject may contain binary zero
2073         When the starting offset is zero, the search for a match starts at  the         bytes. When the starting offset is zero, the search for a match  starts
2074         beginning of the subject, and this is by far the most common case.         at  the  beginning  of  the subject, and this is by far the most common
2075           case.
2076         A  non-zero  starting offset is useful when searching for another match  
2077         in the same subject by calling pcre_exec() again after a previous  suc-         A non-zero starting offset is useful when searching for  another  match
2078         cess.   Setting  startoffset differs from just passing over a shortened         in  the same subject by calling pcre_exec() again after a previous suc-
2079         string and setting PCRE_NOTBOL in the case of  a  pattern  that  begins         cess.  Setting startoffset differs from just passing over  a  shortened
2080           string  and  setting  PCRE_NOTBOL  in the case of a pattern that begins
2081         with any kind of lookbehind. For example, consider the pattern         with any kind of lookbehind. For example, consider the pattern
2082    
2083           \Biss\B           \Biss\B
2084    
2085         which  finds  occurrences  of "iss" in the middle of words. (\B matches         which finds occurrences of "iss" in the middle of  words.  (\B  matches
2086         only if the current position in the subject is not  a  word  boundary.)         only  if  the  current position in the subject is not a word boundary.)
2087         When  applied  to the string "Mississipi" the first call to pcre_exec()         When applied to the string "Mississipi" the first call  to  pcre_exec()
2088         finds the first occurrence. If pcre_exec() is called  again  with  just         finds  the  first  occurrence. If pcre_exec() is called again with just
2089         the  remainder  of  the  subject,  namely  "issipi", it does not match,         the remainder of the subject,  namely  "issipi",  it  does  not  match,
2090         because \B is always false at the start of the subject, which is deemed         because \B is always false at the start of the subject, which is deemed
2091         to  be  a  word  boundary. However, if pcre_exec() is passed the entire         to be a word boundary. However, if pcre_exec()  is  passed  the  entire
2092         string again, but with startoffset set to 4, it finds the second occur-         string again, but with startoffset set to 4, it finds the second occur-
2093         rence  of "iss" because it is able to look behind the starting point to         rence of "iss" because it is able to look behind the starting point  to
2094         discover that it is preceded by a letter.         discover that it is preceded by a letter.
2095    
2096         If a non-zero starting offset is passed when the pattern  is  anchored,         If  a  non-zero starting offset is passed when the pattern is anchored,
2097         one attempt to match at the given offset is made. This can only succeed         one attempt to match at the given offset is made. This can only succeed
2098         if the pattern does not require the match to be at  the  start  of  the         if  the  pattern  does  not require the match to be at the start of the
2099         subject.         subject.
2100    
2101     How pcre_exec() returns captured substrings     How pcre_exec() returns captured substrings
2102    
2103         In  general, a pattern matches a certain portion of the subject, and in         In general, a pattern matches a certain portion of the subject, and  in
2104         addition, further substrings from the subject  may  be  picked  out  by         addition,  further  substrings  from  the  subject may be picked out by
2105         parts  of  the  pattern.  Following the usage in Jeffrey Friedl's book,         parts of the pattern. Following the usage  in  Jeffrey  Friedl's  book,
2106         this is called "capturing" in what follows, and the  phrase  "capturing         this  is  called "capturing" in what follows, and the phrase "capturing
2107         subpattern"  is  used for a fragment of a pattern that picks out a sub-         subpattern" is used for a fragment of a pattern that picks out  a  sub-
2108         string. PCRE supports several other kinds of  parenthesized  subpattern         string.  PCRE  supports several other kinds of parenthesized subpattern
2109         that do not cause substrings to be captured.         that do not cause substrings to be captured.
2110    
2111         Captured  substrings are returned to the caller via a vector of integer         Captured substrings are returned to the caller via a vector of integers
2112         offsets whose address is passed in ovector. The number of  elements  in         whose  address is passed in ovector. The number of elements in the vec-
2113         the  vector is passed in ovecsize, which must be a non-negative number.         tor is passed in ovecsize, which must be a non-negative  number.  Note:
2114         Note: this argument is NOT the size of ovector in bytes.         this argument is NOT the size of ovector in bytes.
2115    
2116         The first two-thirds of the vector is used to pass back  captured  sub-         The  first  two-thirds of the vector is used to pass back captured sub-
2117         strings,  each  substring using a pair of integers. The remaining third         strings, each substring using a pair of integers. The  remaining  third
2118         of the vector is used as workspace by pcre_exec() while  matching  cap-         of  the  vector is used as workspace by pcre_exec() while matching cap-
2119         turing  subpatterns, and is not available for passing back information.         turing subpatterns, and is not available for passing back  information.
2120         The length passed in ovecsize should always be a multiple of three.  If         The  number passed in ovecsize should always be a multiple of three. If
2121         it is not, it is rounded down.         it is not, it is rounded down.
2122    
2123         When  a  match  is successful, information about captured substrings is         When a match is successful, information about  captured  substrings  is
2124         returned in pairs of integers, starting at the  beginning  of  ovector,         returned  in  pairs  of integers, starting at the beginning of ovector,
2125         and  continuing  up  to two-thirds of its length at the most. The first         and continuing up to two-thirds of its length at the  most.  The  first
2126         element of a pair is set to the offset of the first character in a sub-         element  of  each pair is set to the byte offset of the first character
2127         string,  and  the  second  is  set to the offset of the first character         in a substring, and the second is set to the byte offset of  the  first
2128         after the end of a substring. The  first  pair,  ovector[0]  and  ovec-         character  after  the end of a substring. Note: these values are always
2129         tor[1],  identify  the  portion  of  the  subject string matched by the         byte offsets, even in UTF-8 mode. They are not character counts.
2130         entire pattern. The next pair is used for the first  capturing  subpat-  
2131         tern, and so on. The value returned by pcre_exec() is one more than the         The first pair of integers, ovector[0]  and  ovector[1],  identify  the
2132         highest numbered pair that has been set. For example, if two substrings         portion  of  the subject string matched by the entire pattern. The next
2133         have  been captured, the returned value is 3. If there are no capturing         pair is used for the first capturing subpattern, and so on.  The  value
2134         subpatterns, the return value from a successful match is 1,  indicating         returned by pcre_exec() is one more than the highest numbered pair that
2135         that just the first pair of offsets has been set.         has been set.  For example, if two substrings have been  captured,  the
2136           returned  value is 3. If there are no capturing subpatterns, the return
2137           value from a successful match is 1, indicating that just the first pair
2138           of offsets has been set.
2139    
2140         If a capturing subpattern is matched repeatedly, it is the last portion         If a capturing subpattern is matched repeatedly, it is the last portion
2141         of the string that it matched that is returned.         of the string that it matched that is returned.
2142    
2143         If the vector is too small to hold all the captured substring  offsets,         If the vector is too small to hold all the captured substring  offsets,
2144         it is used as far as possible (up to two-thirds of its length), and the         it is used as far as possible (up to two-thirds of its length), and the
2145         function returns a value of zero. In particular, if the substring  off-         function returns a value of zero. If the substring offsets are  not  of
2146         sets are not of interest, pcre_exec() may be called with ovector passed         interest,  pcre_exec()  may  be  called with ovector passed as NULL and
2147         as NULL and ovecsize as zero. However, if  the  pattern  contains  back         ovecsize as zero. However, if the pattern contains back references  and
2148         references  and  the  ovector is not big enough to remember the related         the  ovector is not big enough to remember the related substrings, PCRE
2149         substrings, PCRE has to get additional memory for use during  matching.         has to get additional memory for use during matching. Thus it  is  usu-
2150         Thus it is usually advisable to supply an ovector.         ally advisable to supply an ovector.
2151    
2152         The  pcre_info()  function  can  be used to find out how many capturing         The  pcre_info()  function  can  be used to find out how many capturing
2153         subpatterns there are in a compiled  pattern.  The  smallest  size  for         subpatterns there are in a compiled  pattern.  The  smallest  size  for
# Line 2082  MATCHING A PATTERN: THE TRADITIONAL FUNC Line 2259  MATCHING A PATTERN: THE TRADITIONAL FUNC
2259    
2260           PCRE_ERROR_BADCOUNT       (-15)           PCRE_ERROR_BADCOUNT       (-15)
2261    
2262         This  error is given if the value of the ovecsize argument is negative.         This error is given if the value of the ovecsize argument is negative.
2263    
2264           PCRE_ERROR_RECURSIONLIMIT (-21)           PCRE_ERROR_RECURSIONLIMIT (-21)
2265    
2266         The internal recursion limit, as specified by the match_limit_recursion         The internal recursion limit, as specified by the match_limit_recursion
2267         field  in  a  pcre_extra  structure (or defaulted) was reached. See the         field in a pcre_extra structure (or defaulted)  was  reached.  See  the
2268         description above.         description above.
2269    
2270           PCRE_ERROR_BADNEWLINE     (-23)           PCRE_ERROR_BADNEWLINE     (-23)
# Line 2110  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER Line 2287  EXTRACTING CAPTURED SUBSTRINGS BY NUMBER
2287         int pcre_get_substring_list(const char *subject,         int pcre_get_substring_list(const char *subject,
2288              int *ovector, int stringcount, const char ***listptr);              int *ovector, int stringcount, const char ***listptr);
2289    
2290         Captured substrings can be  accessed  directly  by  using  the  offsets         Captured  substrings  can  be  accessed  directly  by using the offsets
2291         returned  by  pcre_exec()  in  ovector.  For convenience, the functions         returned by pcre_exec() in  ovector.  For  convenience,  the  functions
2292         pcre_copy_substring(),    pcre_get_substring(),    and    pcre_get_sub-         pcre_copy_substring(),    pcre_get_substring(),    and    pcre_get_sub-
2293         string_list()  are  provided for extracting captured substrings as new,         string_list() are provided for extracting captured substrings  as  new,
2294         separate, zero-terminated strings. These functions identify  substrings         separate,  zero-terminated strings. These functions identify substrings
2295         by  number.  The  next section describes functions for extracting named         by number. The next section describes functions  for  extracting  named
2296         substrings.         substrings.
2297    
2298         A substring that contains a binary zero is correctly extracted and  has         A  substring that contains a binary zero is correctly extracted and has
2299         a  further zero added on the end, but the result is not, of course, a C         a further zero added on the end, but the result is not, of course, a  C
2300         string.  However, you can process such a string  by  referring  to  the         string.   However,  you  can  process such a string by referring to the
2301         length  that  is  returned  by  pcre_copy_substring() and pcre_get_sub-         length that is  returned  by  pcre_copy_substring()  and  pcre_get_sub-
2302         string().  Unfortunately, the interface to pcre_get_substring_list() is         string().  Unfortunately, the interface to pcre_get_substring_list() is
2303         not  adequate for handling strings containing binary zeros, because the         not adequate for handling strings containing binary zeros, because  the
2304         end of the final string is not independently indicated.         end of the final string is not independently indicated.
2305    
2306         The first three arguments are the same for all  three  of  these  func-         The  first  three  arguments  are the same for all three of these func-
2307         tions:  subject  is  the subject string that has just been successfully         tions: subject is the subject string that has  just  been  successfully
2308         matched, ovector is a pointer to the vector of integer offsets that was         matched, ovector is a pointer to the vector of integer offsets that was
2309         passed to pcre_exec(), and stringcount is the number of substrings that         passed to pcre_exec(), and stringcount is the number of substrings that
2310         were captured by the match, including the substring  that  matched  the         were  captured  by  the match, including the substring that matched the
2311         entire regular expression. This is the value returned by pcre_exec() if         entire regular expression. This is the value returned by pcre_exec() if
2312         it is greater than zero. If pcre_exec() returned zero, indicating  that         it  is greater than zero. If pcre_exec() returned zero, indicating that
2313         it  ran out of space in ovector, the value passed as stringcount should         it ran out of space in ovector, the value passed as stringcount  should
2314         be the number of elements in the vector divided by three.         be the number of elements in the vector divided by three.
2315    
2316         The functions pcre_copy_substring() and pcre_get_substring() extract  a         The  functions pcre_copy_substring() and pcre_get_substring() extract a
2317         single  substring,  whose  number  is given as stringnumber. A value of         single substring, whose number is given as  stringnumber.  A  value  of
2318         zero extracts the substring that matched the  entire  pattern,  whereas         zero  extracts  the  substring that matched the entire pattern, whereas
2319         higher  values  extract  the  captured  substrings.  For pcre_copy_sub-         higher values  extract  the  captured  substrings.  For  pcre_copy_sub-
2320         string(), the string is placed in buffer,  whose  length  is  given  by         string(),  the  string  is  placed  in buffer, whose length is given by
2321         buffersize,  while  for  pcre_get_substring()  a new block of memory is         buffersize, while for pcre_get_substring() a new  block  of  memory  is
2322         obtained via pcre_malloc, and its address is  returned  via  stringptr.         obtained  via  pcre_malloc,  and its address is returned via stringptr.
2323         The  yield  of  the function is the length of the string, not including         The yield of the function is the length of the  string,  not  including
2324         the terminating zero, or one of these error codes:         the terminating zero, or one of these error codes:
2325    
2326           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2327    
2328         The buffer was too small for pcre_copy_substring(), or the  attempt  to         The  buffer  was too small for pcre_copy_substring(), or the attempt to
2329         get memory failed for pcre_get_substring().         get memory failed for pcre_get_substring().
2330    
2331           PCRE_ERROR_NOSUBSTRING    (-7)           PCRE_ERROR_NOSUBSTRING    (-7)
2332    
2333         There is no substring whose number is stringnumber.         There is no substring whose number is stringnumber.
2334    
2335         The  pcre_get_substring_list()  function  extracts  all  available sub-         The pcre_get_substring_list()  function  extracts  all  available  sub-
2336         strings and builds a list of pointers to them. All this is  done  in  a         strings  and  builds  a list of pointers to them. All this is done in a
2337         single block of memory that is obtained via pcre_malloc. The address of         single block of memory that is obtained via pcre_malloc. The address of
2338         the memory block is returned via listptr, which is also  the  start  of         the  memory  block  is returned via listptr, which is also the start of
2339         the  list  of  string pointers. The end of the list is marked by a NULL         the list of string pointers. The end of the list is marked  by  a  NULL
2340         pointer. The yield of the function is zero if all  went  well,  or  the         pointer.  The  yield  of  the function is zero if all went well, or the
2341         error code         error code
2342    
2343           PCRE_ERROR_NOMEMORY       (-6)           PCRE_ERROR_NOMEMORY       (-6)
2344    
2345         if the attempt to get the memory block failed.         if the attempt to get the memory block failed.
2346    
2347         When  any of these functions encounter a substring that is unset, which         When any of these functions encounter a substring that is unset,  which
2348         can happen when capturing subpattern number n+1 matches  some  part  of         can  happen  when  capturing subpattern number n+1 matches some part of
2349         the  subject, but subpattern n has not been used at all, they return an         the subject, but subpattern n has not been used at all, they return  an
2350         empty string. This can be distinguished from a genuine zero-length sub-         empty string. This can be distinguished from a genuine zero-length sub-
2351         string  by inspecting the appropriate offset in ovector, which is nega-         string by inspecting the appropriate offset in ovector, which is  nega-
2352         tive for unset substrings.         tive for unset substrings.
2353    
2354         The two convenience functions pcre_free_substring() and  pcre_free_sub-         The  two convenience functions pcre_free_substring() and pcre_free_sub-
2355         string_list()  can  be  used  to free the memory returned by a previous         string_list() can be used to free the memory  returned  by  a  previous
2356         call  of  pcre_get_substring()  or  pcre_get_substring_list(),  respec-         call  of  pcre_get_substring()  or  pcre_get_substring_list(),  respec-
2357         tively.  They  do  nothing  more  than  call the function pointed to by         tively. They do nothing more than  call  the  function  pointed  to  by
2358         pcre_free, which of course could be called directly from a  C  program.         pcre_free,  which  of course could be called directly from a C program.
2359         However,  PCRE is used in some situations where it is linked via a spe-         However, PCRE is used in some situations where it is linked via a  spe-
2360         cial  interface  to  another  programming  language  that  cannot   use         cial   interface  to  another  programming  language  that  cannot  use
2361         pcre_free  directly;  it is for these cases that the functions are pro-         pcre_free directly; it is for these cases that the functions  are  pro-
2362         vided.         vided.
2363    
2364    
# Line 2200  EXTRACTING CAPTURED SUBSTRINGS BY NAME Line 2377  EXTRACTING CAPTURED SUBSTRINGS BY NAME
2377              int stringcount, const char *stringname,              int stringcount, const char *stringname,
2378              const char **stringptr);              const char **stringptr);
2379    
2380         To extract a substring by name, you first have to find associated  num-         To  extract a substring by name, you first have to find associated num-
2381         ber.  For example, for this pattern         ber.  For example, for this pattern
2382    
2383           (a+)b(?<xxx>\d+)...           (a+)b(?<xxx>\d+)...
# Line 2209  EXTRACTING CAPTURED SUBSTRINGS BY NAME Line 2386  EXTRACTING CAPTURED SUBSTRINGS BY NAME
2386         be unique (PCRE_DUPNAMES was not set), you can find the number from the         be unique (PCRE_DUPNAMES was not set), you can find the number from the
2387         name by calling pcre_get_stringnumber(). The first argument is the com-         name by calling pcre_get_stringnumber(). The first argument is the com-
2388         piled pattern, and the second is the name. The yield of the function is         piled pattern, and the second is the name. The yield of the function is
2389         the  subpattern  number,  or PCRE_ERROR_NOSUBSTRING (-7) if there is no         the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if  there  is  no
2390         subpattern of that name.         subpattern of that name.
2391    
2392         Given the number, you can extract the substring directly, or use one of         Given the number, you can extract the substring directly, or use one of
2393         the functions described in the previous section. For convenience, there         the functions described in the previous section. For convenience, there
2394         are also two functions that do the whole job.         are also two functions that do the whole job.
2395    
2396         Most   of   the   arguments    of    pcre_copy_named_substring()    and         Most    of    the    arguments   of   pcre_copy_named_substring()   and
2397         pcre_get_named_substring()  are  the  same  as  those for the similarly         pcre_get_named_substring() are the same  as  those  for  the  similarly
2398         named functions that extract by number. As these are described  in  the         named  functions  that extract by number. As these are described in the
2399         previous  section,  they  are not re-described here. There are just two         previous section, they are not re-described here. There  are  just  two
2400         differences:         differences:
2401    
2402         First, instead of a substring number, a substring name is  given.  Sec-         First,  instead  of a substring number, a substring name is given. Sec-
2403         ond, there is an extra argument, given at the start, which is a pointer         ond, there is an extra argument, given at the start, which is a pointer
2404         to the compiled pattern. This is needed in order to gain access to  the         to  the compiled pattern. This is needed in order to gain access to the
2405         name-to-number translation table.         name-to-number translation table.
2406    
2407         These  functions call pcre_get_stringnumber(), and if it succeeds, they         These functions call pcre_get_stringnumber(), and if it succeeds,  they
2408         then call pcre_copy_substring() or pcre_get_substring(),  as  appropri-         then  call  pcre_copy_substring() or pcre_get_substring(), as appropri-
2409         ate.  NOTE:  If PCRE_DUPNAMES is set and there are duplicate names, the         ate. NOTE: If PCRE_DUPNAMES is set and there are duplicate  names,  the
2410         behaviour may not be what you want (see the next section).         behaviour may not be what you want (see the next section).
2411    
2412           Warning:  If the pattern uses the "(?|" feature to set up multiple sub-
2413           patterns with the same number, you  cannot  use  names  to  distinguish
2414           them, because names are not included in the compiled code. The matching
2415           process uses only numbers.
2416    
2417    
2418  DUPLICATE SUBPATTERN NAMES  DUPLICATE SUBPATTERN NAMES
2419    
# Line 2439  MATCHING A PATTERN: THE ALTERNATIVE FUNC Line 2621  MATCHING A PATTERN: THE ALTERNATIVE FUNC
2621  SEE ALSO  SEE ALSO
2622    
2623         pcrebuild(3),  pcrecallout(3), pcrecpp(3)(3), pcrematching(3), pcrepar-         pcrebuild(3),  pcrecallout(3), pcrecpp(3)(3), pcrematching(3), pcrepar-
2624         tial(3), pcreposix(3), pcreprecompile(3), pcresample(3),  pcrestack(3).         tial(3), pcreposix(3), pcreprecompile(3), pcresample(3), pcrestack(3).
2625    
2626    
2627  AUTHOR  AUTHOR
# Line 2451  AUTHOR Line 2633  AUTHOR
2633    
2634  REVISION  REVISION
2635    
2636         Last updated: 09 August 2007         Last updated: 17 March 2009
2637         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
2638  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2639    
2640    
# Line 2503  PCRE CALLOUTS Line 2685  PCRE CALLOUTS
2685  MISSING CALLOUTS  MISSING CALLOUTS
2686    
2687         You  should  be  aware  that,  because of optimizations in the way PCRE         You  should  be  aware  that,  because of optimizations in the way PCRE
2688         matches patterns, callouts sometimes do not happen. For example, if the         matches patterns by default, callouts  sometimes  do  not  happen.  For
2689         pattern is         example, if the pattern is
2690    
2691           ab(?C4)cd           ab(?C4)cd
2692    
# Line 2513  MISSING CALLOUTS Line 2695  MISSING CALLOUTS
2695         ever  start,  and  the  callout is never reached. However, with "abyd",         ever  start,  and  the  callout is never reached. However, with "abyd",
2696         though the result is still no match, the callout is obeyed.         though the result is still no match, the callout is obeyed.
2697    
2698           You can disable these optimizations by passing the  PCRE_NO_START_OPTI-
2699           MIZE  option  to  pcre_exec()  or  pcre_dfa_exec(). This slows down the
2700           matching process, but does ensure that callouts  such  as  the  example
2701           above are obeyed.
2702    
2703    
2704  THE CALLOUT INTERFACE  THE CALLOUT INTERFACE
2705    
2706         During matching, when PCRE reaches a callout point, the external  func-         During  matching, when PCRE reaches a callout point, the external func-
2707         tion  defined by pcre_callout is called (if it is set). This applies to         tion defined by pcre_callout is called (if it is set). This applies  to
2708         both the pcre_exec() and the pcre_dfa_exec()  matching  functions.  The         both  the  pcre_exec()  and the pcre_dfa_exec() matching functions. The
2709         only  argument  to  the callout function is a pointer to a pcre_callout         only argument to the callout function is a pointer  to  a  pcre_callout
2710         block. This structure contains the following fields:         block. This structure contains the following fields:
2711    
2712           int          version;           int          version;
# Line 2535  THE CALLOUT INTERFACE Line 2722  THE CALLOUT INTERFACE
2722           int          pattern_position;           int          pattern_position;
2723           int          next_item_length;           int          next_item_length;
2724    
2725         The version field is an integer containing the version  number  of  the         The  version  field  is an integer containing the version number of the
2726         block  format. The initial version was 0; the current version is 1. The         block format. The initial version was 0; the current version is 1.  The
2727         version number will change again in future  if  additional  fields  are         version  number  will  change  again in future if additional fields are
2728         added, but the intention is never to remove any of the existing fields.         added, but the intention is never to remove any of the existing fields.
2729    
2730         The callout_number field contains the number of the  callout,  as  com-         The callout_number field contains the number of the  callout,  as  com-
# Line 2622  AUTHOR Line 2809  AUTHOR
2809    
2810  REVISION  REVISION
2811    
2812         Last updated: 29 May 2007         Last updated: 15 March 2009
2813         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2009 University of Cambridge.
2814  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2815    
2816    
# Line 2736  DIFFERENCES BETWEEN PCRE AND PERL Line 2923  DIFFERENCES BETWEEN PCRE AND PERL
2923         (f)  The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and PCRE_NO_AUTO_CAP-         (f)  The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and PCRE_NO_AUTO_CAP-
2924         TURE options for pcre_exec() have no Perl equivalents.         TURE options for pcre_exec() have no Perl equivalents.
2925    
2926         (g) The callout facility is PCRE-specific.         (g) The \R escape sequence can be restricted to match only CR,  LF,  or
2927           CRLF by the PCRE_BSR_ANYCRLF option.
2928    
2929           (h) The callout facility is PCRE-specific.
2930    
2931         (h) The partial matching facility is PCRE-specific.         (i) The partial matching facility is PCRE-specific.
2932    
2933         (i) Patterns compiled by PCRE can be saved and re-used at a later time,         (j) Patterns compiled by PCRE can be saved and re-used at a later time,
2934         even on different hosts that have the other endianness.         even on different hosts that have the other endianness.
2935    
2936         (j)  The  alternative  matching function (pcre_dfa_exec()) matches in a         (k) The alternative matching function (pcre_dfa_exec())  matches  in  a
2937         different way and is not Perl-compatible.         different way and is not Perl-compatible.
2938    
2939           (l)  PCRE  recognizes some special sequences such as (*CR) at the start
2940           of a pattern that set overall options that cannot be changed within the
2941           pattern.
2942    
2943    
2944  AUTHOR  AUTHOR
2945    
# Line 2756  AUTHOR Line 2950  AUTHOR
2950    
2951  REVISION  REVISION
2952    
2953         Last updated: 08 August 2007         Last updated: 11 September 2007
2954         Copyright (c) 1997-2007 University of Cambridge.         Copyright (c) 1997-2007 University of Cambridge.
2955  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
2956    
# Line 2772  PCRE REGULAR EXPRESSION DETAILS Line 2966  PCRE REGULAR EXPRESSION DETAILS
2966    
2967         The  syntax and semantics of the regular expressions that are supported         The  syntax and semantics of the regular expressions that are supported
2968         by PCRE are described in detail below. There is a quick-reference  syn-         by PCRE are described in detail below. There is a quick-reference  syn-
2969         tax  summary  in  the  pcresyntax  page. Perl's regular expressions are         tax summary in the pcresyntax page. PCRE tries to match Perl syntax and
2970         described in its own documentation, and regular expressions in  general         semantics as closely as it can. PCRE  also  supports  some  alternative
2971         are  covered in a number of books, some of which have copious examples.         regular  expression  syntax (which does not conflict with the Perl syn-
2972         Jeffrey  Friedl's  "Mastering  Regular   Expressions",   published   by         tax) in order to provide some compatibility with regular expressions in
2973         O'Reilly,  covers regular expressions in great detail. This description         Python, .NET, and Oniguruma.
2974         of PCRE's regular expressions is intended as reference material.  
2975           Perl's  regular expressions are described in its own documentation, and
2976           regular expressions in general are covered in a number of  books,  some
2977           of  which  have  copious  examples. Jeffrey Friedl's "Mastering Regular
2978           Expressions", published by  O'Reilly,  covers  regular  expressions  in
2979           great  detail.  This  description  of  PCRE's  regular  expressions  is
2980           intended as reference material.
2981    
2982         The original operation of PCRE was on strings of  one-byte  characters.         The original operation of PCRE was on strings of  one-byte  characters.
2983         However,  there is now also support for UTF-8 character strings. To use         However,  there is now also support for UTF-8 character strings. To use
# Line 2797  PCRE REGULAR EXPRESSION DETAILS Line 2997  PCRE REGULAR EXPRESSION DETAILS
2997         discussed in the pcrematching page.         discussed in the pcrematching page.
2998    
2999    
3000    NEWLINE CONVENTIONS
3001    
3002           PCRE  supports five different conventions for indicating line breaks in
3003           strings: a single CR (carriage return) character, a  single  LF  (line-
3004           feed) character, the two-character sequence CRLF, any of the three pre-
3005           ceding, or any Unicode newline sequence. The pcreapi page  has  further
3006           discussion  about newlines, and shows how to set the newline convention
3007           in the options arguments for the compiling and matching functions.
3008    
3009           It is also possible to specify a newline convention by starting a  pat-
3010           tern string with one of the following five sequences:
3011    
3012             (*CR)        carriage return
3013             (*LF)        linefeed
3014             (*CRLF)      carriage return, followed by linefeed
3015             (*ANYCRLF)   any of the three above
3016             (*ANY)       all Unicode newline sequences
3017    
3018           These override the default and the options given to pcre_compile(). For
3019           example, on a Unix system where LF is the default newline sequence, the
3020           pattern
3021    
3022             (*CR)a.b
3023    
3024           changes the convention to CR. That pattern matches "a\nb" because LF is
3025           no longer a newline. Note that these special settings,  which  are  not
3026           Perl-compatible,  are  recognized  only at the very start of a pattern,
3027           and that they must be in upper case.  If  more  than  one  of  them  is
3028           present, the last one is used.
3029    
3030           The  newline  convention  does  not  affect what the \R escape sequence
3031           matches. By default, this is any Unicode  newline  sequence,  for  Perl
3032           compatibility.  However, this can be changed; see the description of \R
3033           in the section entitled "Newline sequences" below. A change of \R  set-
3034           ting can be combined with a change of newline convention.
3035    
3036    
3037  CHARACTERS AND METACHARACTERS  CHARACTERS AND METACHARACTERS
3038    
3039         A  regular  expression  is  a pattern that is matched against a subject         A  regular  expression  is  a pattern that is matched against a subject
# Line 2852  CHARACTERS AND METACHARACTERS Line 3089  CHARACTERS AND METACHARACTERS
3089                    syntax)                    syntax)
3090           ]      terminates the character class           ]      terminates the character class
3091    
3092         The  following sections describe the use of each of the metacharacters.         The following sections describe the use of each of the metacharacters.
3093    
3094    
3095  BACKSLASH  BACKSLASH
3096    
3097         The backslash character has several uses. Firstly, if it is followed by         The backslash character has several uses. Firstly, if it is followed by
3098         a  non-alphanumeric  character,  it takes away any special meaning that         a non-alphanumeric character, it takes away any  special  meaning  that
3099         character may have. This  use  of  backslash  as  an  escape  character         character  may  have.  This  use  of  backslash  as an escape character
3100         applies both inside and outside character classes.         applies both inside and outside character classes.
3101    
3102         For  example,  if  you want to match a * character, you write \* in the         For example, if you want to match a * character, you write  \*  in  the
3103         pattern.  This escaping action applies whether  or  not  the  following         pattern.   This  escaping  action  applies whether or not the following
3104         character  would  otherwise be interpreted as a metacharacter, so it is         character would otherwise be interpreted as a metacharacter, so  it  is
3105         always safe to precede a non-alphanumeric  with  backslash  to  specify         always  safe  to  precede  a non-alphanumeric with backslash to specify
3106         that  it stands for itself. In particular, if you want to match a back-         that it stands for itself. In particular, if you want to match a  back-
3107         slash, you write \\.         slash, you write \\.
3108    
3109         If a pattern is compiled with the PCRE_EXTENDED option,  whitespace  in         If  a  pattern is compiled with the PCRE_EXTENDED option, whitespace in
3110         the  pattern (other than in a character class) and characters between a         the pattern (other than in a character class) and characters between  a
3111         # outside a character class and the next newline are ignored. An escap-         # outside a character class and the next newline are ignored. An escap-
3112         ing  backslash  can  be  used to include a whitespace or # character as         ing backslash can be used to include a whitespace  or  #  character  as
3113         part of the pattern.         part of the pattern.
3114    
3115         If you want to remove the special meaning from a  sequence  of  charac-         If  you  want  to remove the special meaning from a sequence of charac-
3116         ters,  you can do so by putting them between \Q and \E. This is differ-         ters, you can do so by putting them between \Q and \E. This is  differ-
3117         ent from Perl in that $ and  @  are  handled  as  literals  in  \Q...\E         ent  from  Perl  in  that  $  and  @ are handled as literals in \Q...\E
3118         sequences  in  PCRE, whereas in Perl, $ and @ cause variable interpola-         sequences in PCRE, whereas in Perl, $ and @ cause  variable  interpola-
3119         tion. Note the following examples:         tion. Note the following examples:
3120    
3121           Pattern            PCRE matches   Perl matches           Pattern            PCRE matches   Perl matches
# Line 2888  BACKSLASH Line 3125  BACKSLASH
3125           \Qabc\$xyz\E       abc\$xyz       abc\$xyz           \Qabc\$xyz\E       abc\$xyz       abc\$xyz
3126           \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz           \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz
3127    
3128         The \Q...\E sequence is recognized both inside  and  outside  character         The  \Q...\E  sequence  is recognized both inside and outside character
3129         classes.         classes.
3130    
3131     Non-printing characters     Non-printing characters
3132    
3133         A second use of backslash provides a way of encoding non-printing char-         A second use of backslash provides a way of encoding non-printing char-
3134         acters in patterns in a visible manner. There is no restriction on  the         acters  in patterns in a visible manner. There is no restriction on the
3135         appearance  of non-printing characters, apart from the binary zero that         appearance of non-printing characters, apart from the binary zero  that
3136         terminates a pattern, but when a pattern  is  being  prepared  by  text         terminates  a  pattern,  but  when  a pattern is being prepared by text
3137         editing,  it  is  usually  easier  to  use  one of the following escape         editing, it is usually easier  to  use  one  of  the  following  escape
3138         sequences than the binary character it represents:         sequences than the binary character it represents:
3139    
3140           \a        alarm, that is, the BEL character (hex 07)           \a        alarm, that is, the BEL character (hex 07)
3141           \cx       "control-x", where x is any character           \cx       "control-x", where x is any character
3142           \e        escape (hex 1B)           \e        escape (hex 1B)
3143           \f        formfeed (hex 0C)           \f        formfeed (hex 0C)
3144           \n        newline (hex 0A)           \n        linefeed (hex 0A)
3145           \r        carriage return (hex 0D)           \r        carriage return (hex 0D)
3146           \t        tab (hex 09)           \t        tab (hex 09)
3147           \ddd      character with octal code ddd, or backreference           \ddd      character with octal code ddd, or backreference
3148           \xhh      character with hex code hh           \xhh      character with hex code hh
3149           \x{hhh..} character with hex code hhh..           \x{hhh..} character with hex code hhh..
3150    
3151         The precise effect of \cx is as follows: if x is a lower  case  letter,         The  precise  effect of \cx is as follows: if x is a lower case letter,
3152         it  is converted to upper case. Then bit 6 of the character (hex 40) is         it is converted to upper case. Then bit 6 of the character (hex 40)  is
3153         inverted.  Thus \cz becomes hex 1A, but \c{ becomes hex 3B,  while  \c;         inverted.   Thus  \cz becomes hex 1A, but \c{ becomes hex 3B, while \c;
3154         becomes hex 7B.         becomes hex 7B.
3155    
3156         After  \x, from zero to two hexadecimal digits are read (letters can be         After \x, from zero to two hexadecimal digits are read (letters can  be
3157         in upper or lower case). Any number of hexadecimal  digits  may  appear         in  upper  or  lower case). Any number of hexadecimal digits may appear
3158         between  \x{  and  },  but the value of the character code must be less         between \x{ and }, but the value of the character  code  must  be  less
3159         than 256 in non-UTF-8 mode, and less than 2**31 in UTF-8 mode. That is,         than 256 in non-UTF-8 mode, and less than 2**31 in UTF-8 mode. That is,
3160         the  maximum value in hexadecimal is 7FFFFFFF. Note that this is bigger         the maximum value in hexadecimal is 7FFFFFFF. Note that this is  bigger
3161         than the largest Unicode code point, which is 10FFFF.         than the largest Unicode code point, which is 10FFFF.
3162    
3163         If characters other than hexadecimal digits appear between \x{  and  },         If  characters  other than hexadecimal digits appear between \x{ and },
3164         or if there is no terminating }, this form of escape is not recognized.         or if there is no terminating }, this form of escape is not recognized.
3165         Instead, the initial \x will be  interpreted  as  a  basic  hexadecimal         Instead,  the  initial  \x  will  be interpreted as a basic hexadecimal
3166         escape,  with  no  following  digits, giving a character whose value is         escape, with no following digits, giving a  character  whose  value  is
3167         zero.         zero.
3168    
3169         Characters whose value is less than 256 can be defined by either of the         Characters whose value is less than 256 can be defined by either of the
3170         two  syntaxes  for  \x. There is no difference in the way they are han-         two syntaxes for \x. There is no difference in the way  they  are  han-
3171         dled. For example, \xdc is exactly the same as \x{dc}.         dled. For example, \xdc is exactly the same as \x{dc}.
3172    
3173         After \0 up to two further octal digits are read. If  there  are  fewer         After  \0  up  to two further octal digits are read. If there are fewer
3174         than  two  digits,  just  those  that  are  present  are used. Thus the         than two digits, just  those  that  are  present  are  used.  Thus  the
3175         sequence \0\x\07 specifies two binary zeros followed by a BEL character         sequence \0\x\07 specifies two binary zeros followed by a BEL character
3176         (code  value 7). Make sure you supply two digits after the initial zero         (code value 7). Make sure you supply two digits after the initial  zero
3177         if the pattern character that follows is itself an octal digit.         if the pattern character that follows is itself an octal digit.
3178    
3179         The handling of a backslash followed by a digit other than 0 is compli-         The handling of a backslash followed by a digit other than 0 is compli-
3180         cated.  Outside a character class, PCRE reads it and any following dig-         cated.  Outside a character class, PCRE reads it and any following dig-
3181         its as a decimal number. If the number is less than  10,  or  if  there         its  as  a  decimal  number. If the number is less than 10, or if there
3182         have been at least that many previous capturing left parentheses in the         have been at least that many previous capturing left parentheses in the
3183         expression, the entire  sequence  is  taken  as  a  back  reference.  A         expression,  the  entire  sequence  is  taken  as  a  back reference. A
3184         description  of how this works is given later, following the discussion         description of how this works is given later, following the  discussion
3185         of parenthesized subpatterns.         of parenthesized subpatterns.
3186    
3187         Inside a character class, or if the decimal number is  greater  than  9         Inside  a  character  class, or if the decimal number is greater than 9
3188         and  there have not been that many capturing subpatterns, PCRE re-reads         and there have not been that many capturing subpatterns, PCRE  re-reads
3189         up to three octal digits following the backslash, and uses them to gen-         up to three octal digits following the backslash, and uses them to gen-
3190         erate  a data character. Any subsequent digits stand for themselves. In         erate a data character. Any subsequent digits stand for themselves.  In
3191         non-UTF-8 mode, the value of a character specified  in  octal  must  be         non-UTF-8  mode,  the  value  of a character specified in octal must be
3192         less  than  \400.  In  UTF-8 mode, values up to \777 are permitted. For         less than \400. In UTF-8 mode, values up to  \777  are  permitted.  For
3193         example:         example:
3194    
3195           \040   is another way of writing a space           \040   is another way of writing a space
# Line 2970  BACKSLASH Line 3207  BACKSLASH
3207           \81    is either a back reference, or a binary zero           \81    is either a back reference, or a binary zero
3208                     followed by the two characters "8" and "1"                     followed by the two characters "8" and "1"
3209    
3210         Note that octal values of 100 or greater must not be  introduced  by  a         Note  that  octal  values of 100 or greater must not be introduced by a
3211         leading zero, because no more than three octal digits are ever read.         leading zero, because no more than three octal digits are ever read.
3212    
3213         All the sequences that define a single character value can be used both         All the sequences that define a single character value can be used both
3214         inside and outside character classes. In addition, inside  a  character         inside  and  outside character classes. In addition, inside a character
3215         class,  the  sequence \b is interpreted as the backspace character (hex         class, the sequence \b is interpreted as the backspace  character  (hex
3216         08), and the sequences \R and \X are interpreted as the characters  "R"         08),  and the sequences \R and \X are interpreted as the characters "R"
3217         and  "X", respectively. Outside a character class, these sequences have         and "X", respectively. Outside a character class, these sequences  have
3218         different meanings (see below).         different meanings (see below).
3219    
3220     Absolute and relative back references     Absolute and relative back references
3221    
3222         The sequence \g followed by an unsigned or a negative  number,  option-         The  sequence  \g followed by an unsigned or a negative number, option-
3223         ally  enclosed  in braces, is an absolute or relative back reference. A         ally enclosed in braces, is an absolute or relative back  reference.  A
3224         named back reference can be coded as \g{name}. Back references are dis-         named back reference can be coded as \g{name}. Back references are dis-
3225         cussed later, following the discussion of parenthesized subpatterns.         cussed later, following the discussion of parenthesized subpatterns.
3226    
3227       Absolute and relative subroutine calls
3228    
3229           For compatibility with Oniguruma, the non-Perl syntax \g followed by  a
3230           name or a number enclosed either in angle brackets or single quotes, is
3231           an alternative syntax for referencing a subpattern as  a  "subroutine".
3232           Details  are  discussed  later.   Note  that  \g{...} (Perl syntax) and
3233           \g<...> (Oniguruma syntax) are not synonymous. The  former  is  a  back
3234           reference; the latter is a subroutine call.
3235    
3236     Generic character types     Generic character types
3237    
3238         Another use of backslash is for specifying generic character types. The         Another use of backslash is for specifying generic character types. The
# Line 3069  BACKSLASH Line 3315  BACKSLASH
3315    
3316     Newline sequences     Newline sequences
3317    
3318         Outside a character class, the escape sequence \R matches  any  Unicode         Outside a character class, by default, the escape sequence  \R  matches
3319         newline  sequence. This is 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
3320         equivalent to the following:         mode \R is equivalent to the following:
3321    
3322           (?>\r\n|\n|\x0b|\f|\r|\x85)           (?>\r\n|\n|\x0b|\f|\r|\x85)
3323    
# Line 3087  BACKSLASH Line 3333  BACKSLASH
3333         rator,  U+2029).   Unicode character property support is not needed for         rator,  U+2029).   Unicode character property support is not needed for
3334         these characters to be recognized.         these characters to be recognized.
3335    
3336           It is possible to restrict \R to match only CR, LF, or CRLF (instead of
3337           the  complete  set  of  Unicode  line  endings)  by  setting the option
3338           PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched.
3339           (BSR is an abbrevation for "backslash R".) This can be made the default
3340           when PCRE is built; if this is the case, the  other  behaviour  can  be
3341           requested  via  the  PCRE_BSR_UNICODE  option.   It is also possible to
3342           specify these settings by starting a pattern string  with  one  of  the
3343           following sequences:
3344    
3345             (*BSR_ANYCRLF)   CR, LF, or CRLF only
3346             (*BSR_UNICODE)   any Unicode newline sequence
3347    
3348           These override the default and the options given to pcre_compile(), but
3349           they can be overridden by options given to pcre_exec(). Note that these
3350           special settings, which are not Perl-compatible, are recognized only at
3351           the very start of a pattern, and that they must be in  upper  case.  If
3352           more  than  one  of  them is present, the last one is used. They can be
3353           combined with a change of newline convention, for  example,  a  pattern
3354           can start with:
3355    
3356             (*ANY)(*BSR_ANYCRLF)
3357    
3358         Inside a character class, \R matches the letter "R".         Inside a character class, \R matches the letter "R".
3359    
3360     Unicode character properties     Unicode character properties
3361    
3362         When PCRE is built with Unicode character property support, three addi-         When PCRE is built with Unicode character property support, three addi-
3363         tional  escape sequences that match characters with specific properties         tional escape sequences that match characters with specific  properties
3364         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
3365         limited  to  testing characters whose codepoints are less than 256, but         limited to testing characters whose codepoints are less than  256,  but
3366         they do work in this mode.  The extra escape sequences are:         they do work in this mode.  The extra escape sequences are:
3367    
3368           \p{xx}   a character with the xx property           \p{xx}   a character with the xx property
3369           \P{xx}   a character without the xx property           \P{xx}   a character without the xx property
3370           \X       an extended Unicode sequence           \X       an extended Unicode sequence
3371    
3372         The property names represented by xx above are limited to  the  Unicode         The  property  names represented by xx above are limited to the Unicode
3373         script names, the general category properties, and "Any", which matches         script names, the general category properties, and "Any", which matches
3374         any character (including newline). Other properties such as "InMusical-         any character (including newline). Other properties such as "InMusical-
3375         Symbols"  are  not  currently supported by PCRE. Note that \P{Any} does         Symbols" are not currently supported by PCRE. Note  that  \P{Any}  does
3376         not match any characters, so always causes a match failure.         not match any characters, so always causes a match failure.
3377    
3378         Sets of Unicode characters are defined as belonging to certain scripts.         Sets of Unicode characters are defined as belonging to certain scripts.
3379         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.
3380         For example:         For example:
3381    
3382           \p{Greek}           \p{Greek}
3383           \P{Han}           \P{Han}
3384    
3385         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
3386         "Common". The current list of scripts is:         "Common". The current list of scripts is:
3387    
3388         Arabic,  Armenian,  Balinese,  Bengali,  Bopomofo,  Braille,  Buginese,         Arabic,  Armenian,  Balinese,  Bengali,  Bopomofo,  Braille,  Buginese,
3389         Buhid,  Canadian_Aboriginal,  Cherokee,  Common,   Coptic,   Cuneiform,         Buhid,   Canadian_Aboriginal,   Cherokee,  Common,  Coptic,  Cuneiform,
3390         Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic,         Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic,
3391         Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew,  Hira-         Gothic,  Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hira-
3392         gana,  Inherited,  Kannada,  Katakana,  Kharoshthi,  Khmer, Lao, Latin,         gana, Inherited, Kannada,  Katakana,  Kharoshthi,  Khmer,  Lao,  Latin,
3393         Limbu,  Linear_B,  Malayalam,  Mongolian,  Myanmar,  New_Tai_Lue,  Nko,         Limbu,  Linear_B,  Malayalam,  Mongolian,  Myanmar,  New_Tai_Lue,  Nko,
3394         Ogham,  Old_Italic,  Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician,         Ogham, Old_Italic, Old_Persian, Oriya, Osmanya,  Phags_Pa,  Phoenician,
3395         Runic,  Shavian,  Sinhala,  Syloti_Nagri,  Syriac,  Tagalog,  Tagbanwa,         Runic,  Shavian,  Sinhala,  Syloti_Nagri,  Syriac,  Tagalog,  Tagbanwa,
3396         Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi.         Tai_Le, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Yi.
3397    
3398         Each  character has exactly one general category property, specified by         Each character has exactly one general category property, specified  by
3399         a two-letter abbreviation. For compatibility with Perl, negation can be         a two-letter abbreviation. For compatibility with Perl, negation can be
3400         specified  by  including a circumflex between the opening brace and the         specified by including a circumflex between the opening brace  and  the
3401         property name. For example, \p{^Lu} is the same as \P{Lu}.         property name. For example, \p{^Lu} is the same as \P{Lu}.
3402    
3403         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-
3404         eral  category properties that start with that letter. In this case, in         eral category properties that start with that letter. In this case,  in
3405         the absence of negation, the curly brackets in the escape sequence  are         the  absence of negation, the curly brackets in the escape sequence are
3406         optional; these two examples have the same effect:         optional; these two examples have the same effect:
3407    
3408           \p{L}           \p{L}
# Line 3186  BACKSLASH Line 3454  BACKSLASH
3454           Zp    Paragraph separator           Zp    Paragraph separator
3455           Zs    Space separator           Zs    Space separator
3456    
3457         The  special property L& is also supported: it matches a character that         The special property L& is also supported: it matches a character  that
3458         has the Lu, Ll, or Lt property, in other words, a letter  that  is  not         has  the  Lu,  Ll, or Lt property, in other words, a letter that is not
3459         classified as a modifier or "other".         classified as a modifier or "other".
3460    
3461         The  Cs  (Surrogate)  property  applies only to characters in the range         The Cs (Surrogate) property applies only to  characters  in  the  range
3462         U+D800 to U+DFFF. Such characters are not valid in UTF-8  strings  (see         U+D800  to  U+DFFF. Such characters are not valid in UTF-8 strings (see
3463         RFC 3629) and so cannot be tested by PCRE, unless UTF-8 validity check-         RFC 3629) and so cannot be tested by PCRE, unless UTF-8 validity check-
3464         ing has been turned off (see the discussion  of  PCRE_NO_UTF8_CHECK  in         ing  has  been  turned off (see the discussion of PCRE_NO_UTF8_CHECK in
3465         the pcreapi page).         the pcreapi page).
3466    
3467         The  long  synonyms  for  these  properties that Perl supports (such as         The long synonyms for these properties  that  Perl  supports  (such  as
3468         \p{Letter}) are not supported by PCRE, nor is it  permitted  to  prefix         \p{Letter})  are  not  supported by PCRE, nor is it permitted to prefix
3469         any of these properties with "Is".         any of these properties with "Is".
3470    
3471         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-
3472         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
3473         in the Unicode table.         in the Unicode table.
3474    
3475         Specifying  caseless  matching  does not affect these escape sequences.         Specifying caseless matching does not affect  these  escape  sequences.
3476         For example, \p{Lu} always matches only upper case letters.         For example, \p{Lu} always matches only upper case letters.
3477    
3478         The \X escape matches any number of Unicode  characters  that  form  an         The  \X  escape  matches  any number of Unicode characters that form an
3479         extended Unicode sequence. \X is equivalent to         extended Unicode sequence. \X is equivalent to
3480    
3481           (?>\PM\pM*)           (?>\PM\pM*)
3482    
3483         That  is,  it matches a character without the "mark" property, followed         That is, it matches a character without the "mark"  property,  followed
3484         by zero or more characters with the "mark"  property,  and  treats  the         by  zero  or  more  characters with the "mark" property, and treats the
3485         sequence  as  an  atomic group (see below).  Characters with the "mark"         sequence as an atomic group (see below).  Characters  with  the  "mark"
3486         property are typically accents that  affect  the  preceding  character.         property  are  typically  accents  that affect the preceding character.
3487         None  of  them  have  codepoints less than 256, so in non-UTF-8 mode \X         None of them have codepoints less than 256, so  in  non-UTF-8  mode  \X
3488         matches any one character.         matches any one character.
3489    
3490         Matching characters by Unicode property is not fast, because  PCRE  has         Matching  characters  by Unicode property is not fast, because PCRE has
3491         to  search  a  structure  that  contains data for over fifteen thousand         to search a structure that contains  data  for  over  fifteen  thousand
3492         characters. That is why the traditional escape sequences such as \d and         characters. That is why the traditional escape sequences such as \d and
3493         \w do not use Unicode properties in PCRE.         \w do not use Unicode properties in PCRE.
3494    
3495     Resetting the match start     Resetting the match start
3496    
3497         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-
3498         ously matched characters not  to  be  included  in  the  final  matched         ously  matched  characters  not  to  be  included  in the final matched
3499         sequence. For example, the pattern:         sequence. For example, the pattern:
3500    
3501           foo\Kbar           foo\Kbar
3502    
3503         matches  "foobar",  but reports that it has matched "bar". This feature         matches "foobar", but reports that it has matched "bar".  This  feature
3504         is similar to a lookbehind assertion (described  below).   However,  in         is  similar  to  a lookbehind assertion (described below).  However, in
3505         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
3506         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
3507         not  interfere  with  the setting of captured substrings.  For example,         not interfere with the setting of captured  substrings.   For  example,
3508         when the pattern         when the pattern
3509    
3510           (foo)\Kbar           (foo)\Kbar
# Line 3245  BACKSLASH Line 3513  BACKSLASH
3513    
3514     Simple assertions     Simple assertions
3515    
3516         The final use of backslash is for certain simple assertions. An  asser-         The  final use of backslash is for certain simple assertions. An asser-
3517         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
3518         a match, without consuming any characters from the subject string.  The         a  match, without consuming any characters from the subject string. The
3519         use  of subpatterns for more complicated assertions is described below.         use of subpatterns for more complicated assertions is described  below.
3520         The backslashed assertions are:         The backslashed assertions are:
3521    
3522           \b     matches at a word boundary           \b     matches at a word boundary
# Line 3259  BACKSLASH Line 3527  BACKSLASH
3527           \z     matches only at the end of the subject           \z     matches only at the end of the subject
3528           \G     matches at the first matching position in the subject           \G     matches at the first matching position in the subject
3529    
3530         These assertions may not appear in character classes (but note that  \b         These  assertions may not appear in character classes (but note that \b
3531         has a different meaning, namely the backspace character, inside a char-         has a different meaning, namely the backspace character, inside a char-
3532         acter class).         acter class).
3533    
3534         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
3535         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.
3536         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
3537         string if the first or last character matches \w, respectively.         string if the first or last character matches \w, respectively.
3538    
3539         The  \A,  \Z,  and \z assertions differ from the traditional circumflex         The \A, \Z, and \z assertions differ from  the  traditional  circumflex
3540         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
3541         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
3542         set. Thus, they are independent of multiline mode. These  three  asser-         set.  Thus,  they are independent of multiline mode. These three asser-
3543         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
3544         affect only the behaviour of the circumflex and dollar  metacharacters.         affect  only the behaviour of the circumflex and dollar metacharacters.
3545         However,  if the startoffset argument of pcre_exec() is non-zero, indi-         However, if the startoffset argument of pcre_exec() is non-zero,  indi-
3546         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
3547         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
3548         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
3549         the very end, whereas \z matches only at the end.         the very end, whereas \z matches only at the end.
3550    
3551         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
3552         the start point of the match, as specified by the startoffset  argument         the  start point of the match, as specified by the startoffset argument
3553         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
3554         non-zero. By calling pcre_exec() multiple times with appropriate  argu-         non-zero.  By calling pcre_exec() multiple times with appropriate argu-
3555         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-
3556         mentation where \G can be useful.         mentation where \G can be useful.
3557    
3558         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
3559         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
3560         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
3561         previously  matched  string was empty. Because PCRE does just one match         previously matched string was empty. Because PCRE does just  one  match
3562         at a time, it cannot reproduce this behaviour.         at a time, it cannot reproduce this behaviour.
3563    
3564         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
3565         anchored to the starting match position, and the "anchored" flag is set         anchored to the starting match position, and the "anchored" flag is set
3566         in the compiled regular expression.         in the compiled regular expression.
3567    
# Line 3301  BACKSLASH Line 3569  BACKSLASH
3569  CIRCUMFLEX AND DOLLAR  CIRCUMFLEX AND DOLLAR
3570    
3571         Outside a character class, in the default matching mode, the circumflex         Outside a character class, in the default matching mode, the circumflex
3572         character  is  an  assertion  that is true only if the current matching         character is an assertion that is true only  if  the  current  matching
3573         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-
3574         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
3575         PCRE_MULTILINE option is unset. Inside a  character  class,  circumflex         PCRE_MULTILINE  option  is  unset. Inside a character class, circumflex
3576         has an entirely different meaning (see below).         has an entirely different meaning (see below).
3577    
3578         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
3579         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
3580         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
3581         branch. If all possible alternatives start with a circumflex, that  is,         branch.  If all possible alternatives start with a circumflex, that is,
3582         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-
3583         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
3584         constructs that can cause a pattern to be anchored.)         constructs that can cause a pattern to be anchored.)
3585    
3586         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
3587         matching point is at the end of  the  subject  string,  or  immediately         matching  point  is  at  the  end of the subject string, or immediately
3588         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
3589         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
3590         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
3591         appears. Dollar has no special meaning in a character class.         appears. Dollar has no special meaning in a character class.
3592    
3593         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
3594         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
3595         compile time. This does not affect the \Z assertion.         compile time. This does not affect the \Z assertion.
3596    
3597         The meanings of the circumflex and dollar characters are changed if the         The meanings of the circumflex and dollar characters are changed if the
3598         PCRE_MULTILINE  option  is  set.  When  this  is the case, a circumflex         PCRE_MULTILINE option is set. When  this  is  the  case,  a  circumflex
3599         matches immediately after internal newlines as well as at the start  of         matches  immediately after internal newlines as well as at the start of
3600         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
3601         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
3602         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
3603         as the two-character sequence CRLF, isolated CR and  LF  characters  do         as  the  two-character  sequence CRLF, isolated CR and LF characters do
3604         not indicate newlines.         not indicate newlines.
3605    
3606         For  example, the pattern /^abc$/ matches the subject string "def\nabc"         For example, the pattern /^abc$/ matches the subject string  "def\nabc"
3607         (where \n represents a newline) in multiline mode, but  not  otherwise.         (where  \n  represents a newline) in multiline mode, but not otherwise.
3608         Consequently,  patterns  that  are anchored in single line mode because         Consequently, patterns that are anchored in single  line  mode  because
3609         all branches start with ^ are not anchored in  multiline  mode,  and  a         all  branches  start  with  ^ are not anchored in multiline mode, and a
3610         match  for  circumflex  is  possible  when  the startoffset argument of         match for circumflex is  possible  when  the  startoffset  argument  of
3611         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
3612         PCRE_MULTILINE is set.         PCRE_MULTILINE is set.
3613    
3614         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
3615         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
3616         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
3617         set.         set.
3618    
3619    
3620  FULL STOP (PERIOD, DOT)  FULL STOP (PERIOD, DOT)
3621    
3622         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-
3623         ter  in  the subject string except (by default) a character that signi-         ter in the subject string except (by default) a character  that  signi-
3624         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
3625         more than one byte long.         more than one byte long.
3626    
3627         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
3628         that character; when the two-character sequence CRLF is used, dot  does         that  character; when the two-character sequence CRLF is used, dot does
3629         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
3630         matches all characters (including isolated CRs and LFs). When any  Uni-         matches  all characters (including isolated CRs and LFs). When any Uni-
3631         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
3632         any of the other line ending characters.         any of the other line ending characters.
3633    
3634         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
3635         PCRE_DOTALL  option  is  set,  a dot matches any one character, without         PCRE_DOTALL option is set, a dot matches  any  one  character,  without
3636         exception. If the two-character sequence CRLF is present in the subject         exception. If the two-character sequence CRLF is present in the subject
3637         string, it takes two dots to match it.         string, it takes two dots to match it.
3638    
3639         The  handling of dot is entirely independent of the handling of circum-         The handling of dot is entirely independent of the handling of  circum-
3640         flex and dollar, the only relationship being  that  they  both  involve         flex  and  dollar,  the  only relationship being that they both involve
3641         newlines. Dot has no special meaning in a character class.         newlines. Dot has no special meaning in a character class.
3642    
3643    
3644  MATCHING A SINGLE BYTE  MATCHING A SINGLE BYTE
3645    
3646         Outside a character class, the escape sequence \C matches any one byte,         Outside a character class, the escape sequence \C matches any one byte,
3647         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
3648         line-ending  characters.  The  feature  is provided in Perl in order to         line-ending characters. The feature is provided in  Perl  in  order  to
3649         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-
3650         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-
3651         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
3652         avoided.         avoided.
3653    
3654         PCRE  does  not  allow \C to appear in lookbehind assertions (described         PCRE does not allow \C to appear in  lookbehind  assertions  (described
3655         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-
3656         late the length of the lookbehind.         late the length of the lookbehind.
3657    
3658    
# Line 3393  SQUARE BRACKETS AND CHARACTER CLASSES Line 3661  SQUARE BRACKETS AND CHARACTER CLASSES
3661         An opening square bracket introduces a character class, terminated by a         An opening square bracket introduces a character class, terminated by a
3662         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-
3663         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,
3664         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
3665         circumflex, if present) or escaped with a backslash.         circumflex, if present) or escaped with a backslash.
3666    
3667         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
3668         mode, the character may occupy more than one byte. A matched  character         mode,  the character may occupy more than one byte. A matched character
3669         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
3670         character in the class definition is a circumflex, in  which  case  the         character  in  the  class definition is a circumflex, in which case the
3671         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
3672         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
3673         not the first character, or escape it with a backslash.         not the first character, or escape it with a backslash.
3674    
3675         For  example, the character class [aeiou] matches any lower case vowel,         For example, the character class [aeiou] matches any lower case  vowel,
3676         while [^aeiou] matches any character that is not a  lower  case  vowel.         while  [^aeiou]  matches  any character that is not a lower case vowel.
3677         Note that a circumflex is just a convenient notation for specifying the         Note that a circumflex is just a convenient notation for specifying the
3678         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
3679         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-
3680         sumes a character from the subject string, and therefore  it  fails  if         sumes  a  character  from the subject string, and therefore it fails if
3681         the current pointer is at the end of the string.         the current pointer is at the end of the string.
3682    
3683         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
3684         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
3685         mechanism.         mechanism.
3686    
3687         When  caseless  matching  is set, any letters in a class represent both         When caseless matching is set, any letters in a  class  represent  both
3688         their upper case and lower case versions, so for  example,  a  caseless         their  upper  case  and lower case versions, so for example, a caseless
3689         [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
3690         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
3691         understands  the  concept  of case for characters whose values are less         understands the concept of case for characters whose  values  are  less
3692         than 128, so caseless matching is always possible. For characters  with         than  128, so caseless matching is always possible. For characters with
3693         higher  values,  the  concept  of case is supported if PCRE is compiled         higher values, the concept of case is supported  if  PCRE  is  compiled
3694         with Unicode property support, but not otherwise.  If you want  to  use         with  Unicode  property support, but not otherwise.  If you want to use
3695         caseless  matching  for  characters 128 and above, you must ensure that         caseless matching for characters 128 and above, you  must  ensure  that
3696         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
3697         support.         support.
3698    
3699         Characters  that  might  indicate  line breaks are never treated in any         Characters that might indicate line breaks are  never  treated  in  any
3700         special way  when  matching  character  classes,  whatever  line-ending         special  way  when  matching  character  classes,  whatever line-ending
3701         sequence  is  in  use,  and  whatever  setting  of  the PCRE_DOTALL and         sequence is in  use,  and  whatever  setting  of  the  PCRE_DOTALL  and
3702         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
3703         of these characters.         of these characters.
3704    
3705         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-
3706         ters in a character  class.  For  example,  [d-m]  matches  any  letter         ters  in  a  character  class.  For  example,  [d-m] matches any letter
3707         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
3708         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
3709         where  it cannot be interpreted as indicating a range, typically as the         where it cannot be interpreted as indicating a range, typically as  the
3710         first or last character in the class.         first or last character in the class.
3711    
3712         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-
3713         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
3714         two characters ("W" and "-") followed by a literal string "46]", so  it         two  characters ("W" and "-") followed by a literal string "46]", so it
3715         would  match  "W46]"  or  "-46]". However, if the "]" is escaped with a         would match "W46]" or "-46]". However, if the "]"  is  escaped  with  a
3716         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-
3717         preted  as a class containing a range followed by two other characters.         preted as a class containing a range followed by two other  characters.
3718         The octal or hexadecimal representation of "]" can also be used to  end         The  octal or hexadecimal representation of "]" can also be used to end
3719         a range.         a range.
3720    
3721         Ranges  operate in the collating sequence of character values. They can         Ranges operate in the collating sequence of character values. They  can
3722         also  be  used  for  characters  specified  numerically,  for   example         also   be  used  for  characters  specified  numerically,  for  example
3723         [\000-\037].  In UTF-8 mode, ranges can include characters whose values         [\000-\037]. In UTF-8 mode, ranges can include characters whose  values
3724         are greater than 255, for example [\x{100}-\x{2ff}].         are greater than 255, for example [\x{100}-\x{2ff}].
3725    
3726         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,
3727         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
3728         to [][\\^_`wxyzabc], matched caselessly,  and  in  non-UTF-8  mode,  if         to  [][\\^_`wxyzabc],  matched  caselessly,  and  in non-UTF-8 mode, if
3729         character  tables  for  a French locale are in use, [\xc8-\xcb] matches         character tables for a French locale are in  use,  [\xc8-\xcb]  matches
3730         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
3731         concept  of  case for characters with values greater than 128 only when         concept of case for characters with values greater than 128  only  when
3732         it is compiled with Unicode property support.         it is compiled with Unicode property support.
3733    
3734         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
3735         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
3736         class. For example, [\dABCDEF] matches any hexadecimal digit. A circum-         class. For example, [\dABCDEF] matches any hexadecimal digit. A circum-
3737         flex  can  conveniently  be used with the upper case character types to         flex can conveniently be used with the upper case  character  types  to
3738         specify a more restricted set of characters  than  the  matching  lower         specify  a  more  restricted  set of characters than the matching lower
3739         case  type.  For example, the class [^\W_] matches any letter or digit,         case type. For example, the class [^\W_] matches any letter  or  digit,
3740         but not underscore.         but not underscore.
3741    
3742         The only metacharacters that are recognized in  character  classes  are         The  only  metacharacters  that are recognized in character classes are
3743         backslash,  hyphen  (only  where  it can be interpreted as specifying a         backslash, hyphen (only where it can be  interpreted  as  specifying  a
3744         range), circumflex (only at the start), opening  square  bracket  (only         range),  circumflex  (only  at the start), opening square bracket (only
3745         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
3746         next section), and the terminating  closing  square  bracket.  However,         next  section),  and  the  terminating closing square bracket. However,
3747         escaping other non-alphanumeric characters does no harm.         escaping other non-alphanumeric characters does no harm.
3748    
3749    
3750  POSIX CHARACTER CLASSES  POSIX CHARACTER CLASSES
3751    
3752         Perl supports the POSIX notation for character classes. This uses names         Perl supports the POSIX notation for character classes. This uses names
3753         enclosed by [: and :] within the enclosing square brackets.  PCRE  also         enclosed  by  [: and :] within the enclosing square brackets. PCRE also
3754         supports this notation. For example,         supports this notation. For example,
3755    
3756           [01[:alpha:]%]           [01[:alpha:]%]
# Line 3505  POSIX CHARACTER CLASSES Line 3773  POSIX CHARACTER CLASSES
3773           word     "word" characters (same as \w)           word     "word" characters (same as \w)
3774           xdigit   hexadecimal digits           xdigit   hexadecimal digits
3775    
3776         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),
3777         and  space  (32). Notice that this list includes the VT character (code         and space (32). Notice that this list includes the VT  character  (code
3778         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
3779         Perl compatibility).         Perl compatibility).
3780    
3781         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
3782         from Perl 5.8. Another Perl extension is negation, which  is  indicated         from  Perl  5.8. Another Perl extension is negation, which is indicated
3783         by a ^ character after the colon. For example,         by a ^ character after the colon. For example,
3784    
3785           [12[:^digit:]]           [12[:^digit:]]
3786    
3787         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
3788         POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but         POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but
3789         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.
3790    
# Line 3536  VERTICAL BAR Line 3804  VERTICAL BAR
3804         string). The matching process tries each alternative in turn, from left         string). The matching process tries each alternative in turn, from left
3805         to  right, and the first one that succeeds is used. If the alternatives         to  right, and the first one that succeeds is used. If the alternatives
3806         are within a subpattern (defined below), "succeeds" means matching  the         are within a subpattern (defined below), "succeeds" means matching  the
3807         rest  of the main pattern as well as the alternative in the subpattern.         rest of the main pattern as well as the alternative in the subpattern.
3808    
3809    
3810  INTERNAL OPTION SETTING  INTERNAL OPTION SETTING
3811    
3812         The settings of the  PCRE_CASELESS,  PCRE_MULTILINE,  PCRE_DOTALL,  and         The  settings  of  the  PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
3813         PCRE_EXTENDED  options  can  be  changed  from  within the pattern by a         PCRE_EXTENDED options (which are Perl-compatible) can be  changed  from
3814         sequence of Perl option letters enclosed  between  "(?"  and  ")".  The         within  the  pattern  by  a  sequence  of  Perl option letters enclosed
3815         option letters are         between "(?" and ")".  The option letters are
3816    
3817           i  for PCRE_CASELESS           i  for PCRE_CASELESS
3818           m  for PCRE_MULTILINE           m  for PCRE_MULTILINE
# Line 3553  INTERNAL OPTION SETTING Line 3821  INTERNAL OPTION SETTING
3821    
3822         For example, (?im) sets caseless, multiline matching. It is also possi-         For example, (?im) sets caseless, multiline matching. It is also possi-
3823         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
3824         combined  setting and unsetting such as (?im-sx), which sets PCRE_CASE-         combined setting and unsetting such as (?im-sx), which sets  PCRE_CASE-
3825         LESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and  PCRE_EXTENDED,         LESS  and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED,
3826         is  also  permitted.  If  a  letter  appears  both before and after the         is also permitted. If a  letter  appears  both  before  and  after  the
3827         hyphen, the option is unset.         hyphen, the option is unset.
3828    
3829         When an option change occurs at top level (that is, not inside  subpat-         The  PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA
3830         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
3831           the characters J, U and X respectively.
3832    
3833           When  an option change occurs at top level (that is, not inside subpat-
3834           tern parentheses), the change applies to the remainder of  the  pattern
3835         that follows.  If the change is placed right at the start of a pattern,         that follows.  If the change is placed right at the start of a pattern,
3836         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
3837         in data extracted by the pcre_fullinfo() function).         in data extracted by the pcre_fullinfo() function).
3838    
3839         An option change within a subpattern (see below for  a  description  of         An  option  change  within a subpattern (see below for a description of
3840         subpatterns) affects only that part of the current pattern that follows         subpatterns) affects only that part of the current pattern that follows
3841         it, so         it, so
3842    
3843           (a(?i)b)c           (a(?i)b)c
3844    
3845         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
3846         used).   By  this means, options can be made to have different settings         used).  By this means, options can be made to have  different  settings
3847         in different parts of the pattern. Any changes made in one  alternative         in  different parts of the pattern. Any changes made in one alternative
3848         do  carry  on  into subsequent branches within the same subpattern. For         do carry on into subsequent branches within the  same  subpattern.  For
3849         example,         example,
3850    
3851           (a(?i)b|c)           (a(?i)b|c)
3852    
3853         matches "ab", "aB", "c", and "C", even though  when  matching  "C"  the         matches  "ab",  "aB",  "c",  and "C", even though when matching "C" the
3854         first  branch  is  abandoned before the option setting. This is because         first branch is abandoned before the option setting.  This  is  because
3855         the effects of option settings happen at compile time. There  would  be         the  effects  of option settings happen at compile time. There would be
3856         some very weird behaviour otherwise.         some very weird behaviour otherwise.
3857    
3858         The  PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA         Note: There are other PCRE-specific options that  can  be  set  by  the
3859         can be changed in the same way as the Perl-compatible options by  using         application  when  the  compile  or match functions are called. In some
3860         the characters J, U and X respectively.         cases the pattern can contain special  leading  sequences  to  override
3861           what  the  application  has set or what has been defaulted. Details are
3862           given in the section entitled "Newline sequences" above.
3863    
3864    
3865  SUBPATTERNS  SUBPATTERNS
# Line 3597  SUBPATTERNS Line 3871  SUBPATTERNS
3871    
3872           cat(aract|erpillar|)           cat(aract|erpillar|)
3873    
3874         matches one of the words "cat", "cataract", or  "caterpillar".  Without         matches  one  of the words "cat", "cataract", or "caterpillar". Without
3875         the  parentheses,  it  would  match  "cataract", "erpillar" or an empty         the parentheses, it would match  "cataract",  "erpillar"  or  an  empty
3876         string.         string.
3877    
3878         2. It sets up the subpattern as  a  capturing  subpattern.  This  means         2.  It  sets  up  the  subpattern as a capturing subpattern. This means
3879         that,  when  the  whole  pattern  matches,  that portion of the subject         that, when the whole pattern  matches,  that  portion  of  the  subject
3880         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
3881         ovector  argument  of pcre_exec(). Opening parentheses are counted from         ovector argument of pcre_exec(). Opening parentheses are  counted  from
3882         left to right (starting from 1) to obtain  numbers  for  the  capturing         left  to  right  (starting  from 1) to obtain numbers for the capturing
3883         subpatterns.         subpatterns.
3884    
3885         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-
3886         tern         tern
3887    
3888           the ((red|white) (king|queen))           the ((red|white) (king|queen))
# Line 3616  SUBPATTERNS Line 3890  SUBPATTERNS
3890         the captured substrings are "red king", "red", and "king", and are num-         the captured substrings are "red king", "red", and "king", and are num-
3891         bered 1, 2, and 3, respectively.         bered 1, 2, and 3, respectively.
3892    
3893         The  fact  that  plain  parentheses  fulfil two functions is not always         The fact that plain parentheses fulfil  two  functions  is  not  always
3894         helpful.  There are often times when a grouping subpattern is  required         helpful.   There are often times when a grouping subpattern is required
3895         without  a capturing requirement. If an opening parenthesis is followed         without a capturing requirement. If an opening parenthesis is  followed
3896         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-
3897         ing,  and  is  not  counted when computing the number of any subsequent         ing, and is not counted when computing the  number  of  any  subsequent
3898         capturing subpatterns. For example, if the string "the white queen"  is         capturing  subpatterns. For example, if the string "the white queen" is
3899         matched against the pattern         matched against the pattern
3900    
3901           the ((?:red|white) (king|queen))           the ((?:red|white) (king|queen))
# Line 3629  SUBPATTERNS Line 3903  SUBPATTERNS
3903         the captured substrings are "white queen" and "queen", and are numbered         the captured substrings are "white queen" and "queen", and are numbered
3904         1 and 2. The maximum number of capturing subpatterns is 65535.         1 and 2. The maximum number of capturing subpatterns is 65535.
3905    
3906         As a convenient shorthand, if any option settings are required  at  the         As  a  convenient shorthand, if any option settings are required at the
3907         start  of  a  non-capturing  subpattern,  the option letters may appear         start of a non-capturing subpattern,  the  option  letters  may  appear
3908         between the "?" and the ":". Thus the two patterns         between the "?" and the ":". Thus the two patterns
3909    
3910           (?i:saturday|sunday)           (?i:saturday|sunday)
3911           (?:(?i)saturday|sunday)           (?:(?i)saturday|sunday)
3912    
3913         match exactly the same set of strings. Because alternative branches are         match exactly the same set of strings. Because alternative branches are
3914         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
3915         the subpattern is reached, an option setting in one branch does  affect         the  subpattern is reached, an option setting in one branch does affect
3916         subsequent  branches,  so  the above patterns match "SUNDAY" as well as         subsequent branches, so the above patterns match "SUNDAY"  as  well  as
3917         "Saturday".         "Saturday".
3918    
3919    
3920  DUPLICATE SUBPATTERN NUMBERS  DUPLICATE SUBPATTERN NUMBERS
3921    
3922         Perl 5.10 introduced a feature whereby each alternative in a subpattern         Perl 5.10 introduced a feature whereby each alternative in a subpattern
3923         uses  the same numbers for its capturing parentheses. Such a subpattern         uses the same numbers for its capturing parentheses. Such a  subpattern
3924         starts with (?| and is itself a non-capturing subpattern. For  example,         starts  with (?| and is itself a non-capturing subpattern. For example,
3925         consider this pattern:         consider this pattern:
3926    
3927           (?|(Sat)ur|(Sun))day           (?|(Sat)ur|(Sun))day
3928    
3929         Because  the two alternatives are inside a (?| group, both sets of cap-         Because the two alternatives are inside a (?| group, both sets of  cap-
3930         turing parentheses are numbered one. Thus, when  the  pattern  matches,         turing  parentheses  are  numbered one. Thus, when the pattern matches,
3931         you  can  look  at captured substring number one, whichever alternative         you can look at captured substring number  one,  whichever  alternative
3932         matched. This construct is useful when you want to  capture  part,  but         matched.  This  construct  is useful when you want to capture part, but
3933         not all, of one of a number of alternatives. Inside a (?| group, paren-         not all, of one of a number of alternatives. Inside a (?| group, paren-
3934         theses are numbered as usual, but the number is reset at the  start  of         theses  are  numbered as usual, but the number is reset at the start of
3935         each  branch. The numbers of any capturing buffers that follow the sub-         each branch. The numbers of any capturing buffers that follow the  sub-
3936         pattern start after the highest number used in any branch. The  follow-         pattern  start after the highest number used in any branch. The follow-
3937         ing  example  is taken from the Perl documentation.  The numbers under-         ing example is taken from the Perl documentation.  The  numbers  under-
3938         neath show in which buffer the captured content will be stored.         neath show in which buffer the captured content will be stored.
3939    
3940           # before  ---------------branch-reset----------- after           # before  ---------------branch-reset----------- after
3941           / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x           / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
3942           # 1            2         2  3        2     3     4           # 1            2         2  3        2     3     4
3943    
3944         A backreference or a recursive call to  a  numbered  subpattern  always         A  backreference  or  a  recursive call to a numbered subpattern always
3945         refers to the first one in the pattern with the given number.         refers to the first one in the pattern with the given number.
3946    
3947         An  alternative approach to using this "branch reset" feature is to use         An alternative approach to using this "branch reset" feature is to  use
3948         duplicate named subpatterns, as described in the next section.         duplicate named subpatterns, as described in the next section.
3949    
3950    
3951  NAMED SUBPATTERNS  NAMED SUBPATTERNS
3952    
3953         Identifying capturing parentheses by number is simple, but  it  can  be         Identifying  capturing  parentheses  by number is simple, but it can be
3954         very  hard  to keep track of the numbers in complicated regular expres-         very hard to keep track of the numbers in complicated  regular  expres-
3955         sions. Furthermore, if an  expression  is  modified,  the  numbers  may         sions.  Furthermore,  if  an  expression  is  modified, the numbers may
3956         change.  To help with this difficulty, PCRE supports the naming of sub-         change. To help with this difficulty, PCRE supports the naming of  sub-
3957         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
3958         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
3959         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-
3960         tax.         tax.
3961    
3962         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>...)
3963         or (?'name'...) as in Perl, or (?P<name>...) as in  Python.  References         or  (?'name'...)  as in Perl, or (?P<name>...) as in Python. References
3964         to capturing parentheses from other parts of the pattern, such as back-         to capturing parentheses from other parts of the pattern, such as back-
3965         references, recursion, and conditions, can be made by name as  well  as         references,  recursion,  and conditions, can be made by name as well as
3966         by number.         by number.
3967    
3968         Names  consist  of  up  to  32 alphanumeric characters and underscores.         Names consist of up to  32  alphanumeric  characters  and  underscores.
3969         Named capturing parentheses are still  allocated  numbers  as  well  as         Named  capturing  parentheses  are  still  allocated numbers as well as
3970         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
3971         function calls for extracting the name-to-number translation table from         function calls for extracting the name-to-number translation table from
3972         a compiled pattern. There is also a convenience function for extracting         a compiled pattern. There is also a convenience function for extracting
3973         a captured substring by name.         a captured substring by name.
3974    
3975         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
3976         to relax this constraint by setting the PCRE_DUPNAMES option at compile         to relax this constraint by setting the PCRE_DUPNAMES option at compile
3977         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
3978         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
3979         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
3980         both cases you want to extract the abbreviation. This pattern (ignoring         both cases you want to extract the abbreviation. This pattern (ignoring
3981         the line breaks) does the job:         the line breaks) does the job:
3982    
# Line 3712  NAMED SUBPATTERNS Line 3986  NAMED SUBPATTERNS
3986           (?<DN>Thu)(?:rsday)?|           (?<DN>Thu)(?:rsday)?|
3987           (?<DN>Sat)(?:urday)?           (?<DN>Sat)(?:urday)?
3988    
3989         There are five capturing substrings, but only one is ever set  after  a         There  are  five capturing substrings, but only one is ever set after a
3990         match.  (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
3991         reset" subpattern, as described in the previous section.)         reset" subpattern, as described in the previous section.)
3992    
3993         The convenience function for extracting the data by  name  returns  the         The  convenience  function  for extracting the data by name returns the
3994         substring  for  the first (and in this example, the only) subpattern of         substring for the first (and in this example, the only)  subpattern  of
3995         that name that matched. This saves searching  to  find  which  numbered         that  name  that  matched.  This saves searching to find which numbered
3996         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-
3997         pattern from elsewhere in the pattern, the one that corresponds to  the         pattern  from elsewhere in the pattern, the one that corresponds to the
3998         lowest  number  is used. For further details of the interfaces for han-         lowest number is used. For further details of the interfaces  for  han-
3999         dling named subpatterns, see the pcreapi documentation.         dling named subpatterns, see the pcreapi documentation.
4000    
4001           Warning: You cannot use different names to distinguish between two sub-
4002           patterns with the same number (see the previous section)  because  PCRE
4003           uses only the numbers when matching.
4004    
4005    
4006  REPETITION  REPETITION
4007    
4008         Repetition is specified by quantifiers, which can  follow  any  of  the         Repetition  is  specified  by  quantifiers, which can follow any of the
4009         following items:         following items:
4010    
4011           a literal data character           a literal data character
# Line 3740  REPETITION Line 4018  REPETITION
4018           a back reference (see next section)           a back reference (see next section)
4019           a parenthesized subpattern (unless it is an assertion)           a parenthesized subpattern (unless it is an assertion)
4020    
4021         The  general repetition quantifier specifies a minimum and maximum num-         The general repetition quantifier specifies a minimum and maximum  num-
4022         ber of permitted matches, by giving the two numbers in  curly  brackets         ber  of  permitted matches, by giving the two numbers in curly brackets
4023         (braces),  separated  by  a comma. The numbers must be less than 65536,         (braces), separated by a comma. The numbers must be  less  than  65536,
4024         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:
4025    
4026           z{2,4}           z{2,4}
4027    
4028         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
4029         special  character.  If  the second number is omitted, but the comma is         special character. If the second number is omitted, but  the  comma  is
4030         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
4031         are  both omitted, the quantifier specifies an exact number of required         are both omitted, the quantifier specifies an exact number of  required
4032         matches. Thus         matches. Thus
4033    
4034           [aeiou]{3,}           [aeiou]{3,}
# Line 3759  REPETITION Line 4037  REPETITION
4037    
4038           \d{8}           \d{8}
4039    
4040         matches exactly 8 digits. An opening curly bracket that  appears  in  a         matches  exactly  8  digits. An opening curly bracket that appears in a
4041         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
4042         the syntax of a quantifier, is taken as a literal character. For  exam-         the  syntax of a quantifier, is taken as a literal character. For exam-
4043         ple, {,6} is not a quantifier, but a literal string of four characters.         ple, {,6} is not a quantifier, but a literal string of four characters.
4044    
4045         In UTF-8 mode, quantifiers apply to UTF-8  characters  rather  than  to         In UTF-8 mode, quantifiers apply to UTF-8  characters  rather  than  to
# Line 3772  REPETITION Line 4050  REPETITION
4050         may be of different lengths).         may be of different lengths).
4051    
4052         The quantifier {0} is permitted, causing the expression to behave as if         The quantifier {0} is permitted, causing the expression to behave as if
4053         the previous item and the quantifier were not present.         the previous item and the quantifier were not present. This may be use-
4054           ful  for  subpatterns that are referenced as subroutines from elsewhere
4055           in the pattern. Items other than subpatterns that have a {0} quantifier
4056           are omitted from the compiled pattern.
4057    
4058         For convenience, the three most common quantifiers have  single-charac-         For  convenience, the three most common quantifiers have single-charac-
4059         ter abbreviations:         ter abbreviations:
4060    
4061           *    is equivalent to {0,}           *    is equivalent to {0,}
4062           +    is equivalent to {1,}           +    is equivalent to {1,}
4063           ?    is equivalent to {0,1}           ?    is equivalent to {0,1}
4064    
4065         It  is  possible  to construct infinite loops by following a subpattern         It is possible to construct infinite loops by  following  a  subpattern
4066         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,
4067         for example:         for example:
4068    
4069           (a?)*           (a?)*
4070    
4071         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
4072         for such patterns. However, because there are cases where this  can  be         for  such  patterns. However, because there are cases where this can be
4073         useful,  such  patterns  are now accepted, but if any repetition of the         useful, such patterns are now accepted, but if any  repetition  of  the
4074         subpattern does in fact match no characters, the loop is forcibly  bro-         subpattern  does in fact match no characters, the loop is forcibly bro-
4075         ken.         ken.
4076    
4077         By  default,  the quantifiers are "greedy", that is, they match as much         By default, the quantifiers are "greedy", that is, they match  as  much
4078         as possible (up to the maximum  number  of  permitted  times),  without         as  possible  (up  to  the  maximum number of permitted times), without
4079         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
4080         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
4081         appear  between  /*  and  */ and within the comment, individual * and /         appear between /* and */ and within the comment,  individual  *  and  /
4082         characters may appear. An attempt to match C comments by  applying  the         characters  may  appear. An attempt to match C comments by applying the
4083         pattern         pattern
4084    
4085           /\*.*\*/           /\*.*\*/
# Line 3807  REPETITION Line 4088  REPETITION
4088    
4089           /* first comment */  not comment  /* second comment */           /* first comment */  not comment  /* second comment */
4090    
4091         fails,  because it matches the entire string owing to the greediness of         fails, because it matches the entire string owing to the greediness  of
4092         the .*  item.         the .*  item.
4093    
4094         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
4095         be greedy, and instead matches the minimum number of times possible, so         be greedy, and instead matches the minimum number of times possible, so
4096         the pattern         the pattern
4097    
4098           /\*.*?\*/           /\*.*?\*/
4099    
4100         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
4101         quantifiers  is  not  otherwise  changed,  just the preferred number of         quantifiers is not otherwise changed,  just  the  preferred  number  of
4102         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
4103         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
4104         appear doubled, as in         appear doubled, as in
4105    
4106           \d??\d           \d??\d
# Line 3827  REPETITION Line 4108  REPETITION
4108         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
4109         only way the rest of the pattern matches.         only way the rest of the pattern matches.
4110    
4111         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
4112         Perl), the quantifiers are not greedy by default, but  individual  ones         Perl),  the  quantifiers are not greedy by default, but individual ones
4113         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
4114         words, it inverts the default behaviour.         words, it inverts the default behaviour.
4115    
4116         When a parenthesized subpattern is quantified  with  a  minimum  repeat         When  a  parenthesized  subpattern  is quantified with a minimum repeat
4117         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
4118         required for the compiled pattern, in proportion to  the  size  of  the         required  for  the  compiled  pattern, in proportion to the size of the
4119         minimum or maximum.         minimum or maximum.
4120    
4121         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-
4122         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,
4123         the  pattern  is  implicitly anchored, because whatever follows will be         the pattern is implicitly anchored, because whatever  follows  will  be
4124         tried against every character position in the subject string, so  there         tried  against every character position in the subject string, so there
4125         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
4126         first. PCRE normally treats such a pattern as though it  were  preceded         first.  PCRE  normally treats such a pattern as though it were preceded
4127         by \A.         by \A.
4128    
4129         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-
4130         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-
4131         mization, or alternatively using ^ to indicate anchoring explicitly.         mization, or alternatively using ^ to indicate anchoring explicitly.
4132    
4133         However,  there is one situation where the optimization cannot be used.         However, there is one situation where the optimization cannot be  used.
4134         When .*  is inside capturing parentheses that  are  the  subject  of  a         When  .*   is  inside  capturing  parentheses that are the subject of a
4135         backreference  elsewhere  in the pattern, a match at the start may fail         backreference elsewhere in the pattern, a match at the start  may  fail
4136         where a later one succeeds. Consider, for example:         where a later one succeeds. Consider, for example:
4137    
4138           (.*)abc\1           (.*)abc\1
4139    
4140         If the subject is "xyz123abc123" the match point is the fourth  charac-         If  the subject is "xyz123abc123" the match point is the fourth charac-
4141         ter. For this reason, such a pattern is not implicitly anchored.         ter. For this reason, such a pattern is not implicitly anchored.
4142    
4143         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 4146  REPETITION
4146           (tweedle[dume]{3}\s*)+           (tweedle[dume]{3}\s*)+
4147    
4148         has matched "tweedledum tweedledee" the value of the captured substring         has matched "tweedledum tweedledee" the value of the captured substring
4149         is  "tweedledee".  However,  if there are nested capturing subpatterns,         is "tweedledee". However, if there are  nested  capturing  subpatterns,
4150         the corresponding captured values may have been set in previous  itera-         the  corresponding captured values may have been set in previous itera-
4151         tions. For example, after         tions. For example, after
4152    
4153           /(a|(b))+/           /(a|(b))+/
# Line 3876  REPETITION Line 4157  REPETITION
4157    
4158  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS
4159    
4160         With  both  maximizing ("greedy") and minimizing ("ungreedy" or "lazy")         With both maximizing ("greedy") and minimizing ("ungreedy"  or  "lazy")
4161         repetition, failure of what follows normally causes the  repeated  item         repetition,  failure  of what follows normally causes the repeated item
4162         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
4163         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,
4164         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
4165         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
4166         no point in carrying on.         no point in carrying on.
4167    
4168         Consider,  for  example, the pattern \d+foo when applied to the subject         Consider, for example, the pattern \d+foo when applied to  the  subject
4169         line         line
4170    
4171           123456bar           123456bar
4172    
4173         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
4174         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
4175         \d+ item, and then with  4,  and  so  on,  before  ultimately  failing.         \d+  item,  and  then  with  4,  and  so on, before ultimately failing.
4176         "Atomic  grouping"  (a  term taken from Jeffrey Friedl's book) provides         "Atomic grouping" (a term taken from Jeffrey  Friedl's  book)  provides
4177         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
4178         to be re-evaluated in this way.         to be re-evaluated in this way.
4179    
4180         If  we  use atomic grouping for the previous example, the matcher gives         If we use atomic grouping for the previous example, the  matcher  gives
4181         up immediately on failing to match "foo" the first time.  The  notation         up  immediately  on failing to match "foo" the first time. The notation
4182         is a kind of special parenthesis, starting with (?> as in this example:         is a kind of special parenthesis, starting with (?> as in this example:
4183    
4184           (?>\d+)foo           (?>\d+)foo
# Line 3975  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE Line 4256  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE
4256    
4257           ((?>\D+)|<\d+>)*[!?]           ((?>\D+)|<\d+>)*[!?]
4258    
4259         sequences  of non-digits cannot be broken, and failure happens quickly.         sequences of non-digits cannot be broken, and failure happens quickly.
4260    
4261    
4262  BACK REFERENCES  BACK REFERENCES
4263    
4264         Outside a character class, a backslash followed by a digit greater than         Outside a character class, a backslash followed by a digit greater than
4265         0 (and possibly further digits) is a back reference to a capturing sub-         0 (and possibly further digits) is a back reference to a capturing sub-
4266         pattern earlier (that is, to its left) in the pattern,  provided  there         pattern  earlier  (that is, to its left) in the pattern, provided there
4267         have been that many previous capturing left parentheses.         have been that many previous capturing left parentheses.
4268    
4269         However, if the decimal number following the backslash is less than 10,         However, if the decimal number following the backslash is less than 10,
4270         it is always taken as a back reference, and causes  an  error  only  if         it  is  always  taken  as a back reference, and causes an error only if
4271         there  are  not that many capturing left parentheses in the entire pat-         there are not that many capturing left parentheses in the  entire  pat-
4272         tern. In other words, the parentheses that are referenced need  not  be         tern.  In  other words, the parentheses that are referenced need not be
4273         to  the left of the reference for numbers less than 10. A "forward back         to the left of the reference for numbers less than 10. A "forward  back
4274         reference" of this type can make sense when a  repetition  is  involved         reference"  of  this  type can make sense when a repetition is involved
4275         and  the  subpattern to the right has participated in an earlier itera-         and the subpattern to the right has participated in an  earlier  itera-
4276         tion.         tion.
4277    
4278         It is not possible to have a numerical "forward back  reference"  to  a         It  is  not  possible to have a numerical "forward back reference" to a
4279         subpattern  whose  number  is  10  or  more using this syntax because a         subpattern whose number is 10 or  more  using  this  syntax  because  a
4280         sequence such as \50 is interpreted as a character  defined  in  octal.         sequence  such  as  \50 is interpreted as a character defined in octal.
4281         See the subsection entitled "Non-printing characters" above for further         See the subsection entitled "Non-printing characters" above for further
4282         details of the handling of digits following a backslash.  There  is  no         details  of  the  handling of digits following a backslash. There is no
4283         such  problem  when named parentheses are used. A back reference to any         such problem when named parentheses are used. A back reference  to  any
4284         subpattern is possible using named parentheses (see below).         subpattern is possible using named parentheses (see below).
4285    
4286         Another way of avoiding the ambiguity inherent in  the  use  of  digits         Another  way  of  avoiding  the ambiguity inherent in the use of digits
4287         following a backslash is to use the \g escape sequence, which is a fea-         following a backslash is to use the \g escape sequence, which is a fea-
4288         ture introduced in Perl 5.10.  This  escape  must  be  followed  by  an         ture  introduced  in  Perl  5.10.  This  escape  must be followed by an
4289         unsigned  number  or  a negative number, optionally enclosed in braces.         unsigned number or a negative number, optionally  enclosed  in  braces.
4290         These examples are all identical:         These examples are all identical:
4291    
4292           (ring), \1           (ring), \1
4293           (ring), \g1           (ring), \g1
4294           (ring), \g{1}           (ring), \g{1}
4295    
4296         An unsigned number specifies an absolute reference without the  ambigu-         An  unsigned number specifies an absolute reference without the ambigu-
4297         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
4298         digits follow the reference. A negative number is a relative reference.         digits follow the reference. A negative number is a relative reference.
4299         Consider this example:         Consider this example:
# Line 4020  BACK REFERENCES Line 4301  BACK REFERENCES
4301           (abc(def)ghi)\g{-1}           (abc(def)ghi)\g{-1}
4302    
4303         The sequence \g{-1} is a reference to the most recently started captur-         The sequence \g{-1} is a reference to the most recently started captur-
4304         ing subpattern before \g, that is, is it equivalent to  \2.  Similarly,         ing  subpattern  before \g, that is, is it equivalent to \2. Similarly,
4305         \g{-2} would be equivalent to \1. The use of relative references can be         \g{-2} would be equivalent to \1. The use of relative references can be
4306         helpful in long patterns, and also in  patterns  that  are  created  by         helpful  in  long  patterns,  and  also in patterns that are created by
4307         joining together fragments that contain references within themselves.         joining together fragments that contain references within themselves.
4308    
4309         A  back  reference matches whatever actually matched the capturing sub-         A back reference matches whatever actually matched the  capturing  sub-
4310         pattern in the current subject string, rather  than  anything  matching         pattern  in  the  current subject string, rather than anything matching
4311         the subpattern itself (see "Subpatterns as subroutines" below for a way         the subpattern itself (see "Subpatterns as subroutines" below for a way
4312         of doing that). So the pattern         of doing that). So the pattern
4313    
4314           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
4315    
4316         matches "sense and sensibility" and "response and responsibility",  but         matches  "sense and sensibility" and "response and responsibility", but
4317         not  "sense and responsibility". If caseful matching is in force at the         not "sense and responsibility". If caseful matching is in force at  the
4318         time of the back reference, the case of letters is relevant. For  exam-         time  of the back reference, the case of letters is relevant. For exam-
4319         ple,         ple,
4320    
4321           ((?i)rah)\s+\1           ((?i)rah)\s+\1
4322    
4323         matches  "rah  rah"  and  "RAH RAH", but not "RAH rah", even though the         matches "rah rah" and "RAH RAH", but not "RAH  rah",  even  though  the
4324         original capturing subpattern is matched caselessly.         original capturing subpattern is matched caselessly.
4325    
4326         There are several different ways of writing back  references  to  named         There  are  several  different ways of writing back references to named
4327         subpatterns.  The  .NET syntax \k{name} and the Perl syntax \k<name> or         subpatterns. The .NET syntax \k{name} and the Perl syntax  \k<name>  or
4328         \k'name' are supported, as is the Python syntax (?P=name). Perl  5.10's         \k'name'  are supported, as is the Python syntax (?P=name). Perl 5.10's
4329         unified back reference syntax, in which \g can be used for both numeric         unified back reference syntax, in which \g can be used for both numeric
4330         and named references, is also supported. We  could  rewrite  the  above         and  named  references,  is  also supported. We could rewrite the above
4331         example in any of the following ways:         example in any of the following ways:
4332    
4333           (?<p1>(?i)rah)\s+\k<p1>           (?<p1>(?i)rah)\s+\k<p1>
# Line 4054  BACK REFERENCES Line 4335  BACK REFERENCES
4335           (?P<p1>(?i)rah)\s+(?P=p1)           (?P<p1>(?i)rah)\s+(?P=p1)
4336           (?<p1>(?i)rah)\s+\g{p1}           (?<p1>(?i)rah)\s+\g{p1}
4337    
4338         A  subpattern  that  is  referenced  by  name may appear in the pattern         A subpattern that is referenced by  name  may  appear  in  the  pattern
4339         before or after the reference.         before or after the reference.
4340    
4341         There may be more than one back reference to the same subpattern. If  a         There  may be more than one back reference to the same subpattern. If a
4342         subpattern  has  not actually been used in a particular match, any back         subpattern has not actually been used in a particular match,  any  back
4343         references to it always fail. For example, the pattern         references to it always fail. For example, the pattern
4344    
4345           (a|(bc))\2           (a|(bc))\2
4346    
4347         always fails if it starts to match "a" rather than "bc". Because  there         always  fails if it starts to match "a" rather than "bc". Because there
4348         may  be  many  capturing parentheses in a pattern, all digits following         may be many capturing parentheses in a pattern,  all  digits  following
4349         the backslash are taken as part of a potential back  reference  number.         the  backslash  are taken as part of a potential back reference number.
4350         If the pattern continues with a digit character, some delimiter must be         If the pattern continues with a digit character, some delimiter must be
4351         used to terminate the back reference. If the  PCRE_EXTENDED  option  is         used  to  terminate  the back reference. If the PCRE_EXTENDED option is
4352         set,  this  can  be  whitespace.  Otherwise an empty comment (see "Com-         set, this can be whitespace.  Otherwise an  empty  comment  (see  "Com-
4353         ments" below) can be used.         ments" below) can be used.
4354    
4355         A back reference that occurs inside the parentheses to which it  refers         A  back reference that occurs inside the parentheses to which it refers
4356         fails  when  the subpattern is first used, so, for example, (a\1) never         fails when the subpattern is first used, so, for example,  (a\1)  never
4357         matches.  However, such references can be useful inside  repeated  sub-         matches.   However,  such references can be useful inside repeated sub-
4358         patterns. For example, the pattern         patterns. For example, the pattern
4359    
4360           (a|b\1)+           (a|b\1)+
4361    
4362         matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-         matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-
4363         ation of the subpattern,  the  back  reference  matches  the  character         ation  of  the  subpattern,  the  back  reference matches the character
4364         string  corresponding  to  the previous iteration. In order for this to         string corresponding to the previous iteration. In order  for  this  to
4365         work, the pattern must be such that the first iteration does  not  need         work,  the  pattern must be such that the first iteration does not need
4366         to  match the back reference. This can be done using alternation, as in         to match the back reference. This can be done using alternation, as  in
4367         the example above, or by a quantifier with a minimum of zero.         the example above, or by a quantifier with a minimum of zero.
4368    
4369    
4370  ASSERTIONS  ASSERTIONS
4371    
4372         An assertion is a test on the characters  following  or  preceding  the         An  assertion  is  a  test on the characters following or preceding the
4373         current  matching  point that does not actually consume any characters.         current matching point that does not actually consume  any  characters.
4374         The simple assertions coded as \b, \B, \A, \G, \Z,  \z,  ^  and  $  are         The  simple  assertions  coded  as  \b, \B, \A, \G, \Z, \z, ^ and $ are
4375         described above.         described above.
4376    
4377         More  complicated  assertions  are  coded as subpatterns. There are two         More complicated assertions are coded as  subpatterns.  There  are  two
4378         kinds: those that look ahead of the current  position  in  the  subject         kinds:  those  that  look  ahead of the current position in the subject
4379         string,  and  those  that  look  behind  it. An assertion subpattern is         string, and those that look  behind  it.  An  assertion  subpattern  is
4380         matched in the normal way, except that it does not  cause  the  current         matched  in  the  normal way, except that it does not cause the current
4381         matching position to be changed.         matching position to be changed.
4382    
4383         Assertion  subpatterns  are  not  capturing subpatterns, and may not be         Assertion subpatterns are not capturing subpatterns,  and  may  not  be
4384         repeated, because it makes no sense to assert the  same  thing  several         repeated,  because  it  makes no sense to assert the same thing several
4385         times.  If  any kind of assertion contains capturing subpatterns within         times. If any kind of assertion contains capturing  subpatterns  within
4386         it, these are counted for the purposes of numbering the capturing  sub-         it,  these are counted for the purposes of numbering the capturing sub-
4387         patterns in the whole pattern.  However, substring capturing is carried         patterns in the whole pattern.  However, substring capturing is carried
4388         out only for positive assertions, because it does not  make  sense  for         out  only  for  positive assertions, because it does not make sense for
4389         negative assertions.         negative assertions.
4390    
4391     Lookahead assertions     Lookahead assertions
# Line 4114  ASSERTIONS Line 4395  ASSERTIONS
4395    
4396           \w+(?=;)           \w+(?=;)
4397    
4398         matches a word followed by a semicolon, but does not include the  semi-         matches  a word followed by a semicolon, but does not include the semi-
4399         colon in the match, and         colon in the match, and
4400    
4401           foo(?!bar)           foo(?!bar)
4402    
4403         matches  any  occurrence  of  "foo" that is not followed by "bar". Note         matches any occurrence of "foo" that is not  followed  by  "bar".  Note
4404         that the apparently similar pattern         that the apparently similar pattern
4405    
4406           (?!foo)bar           (?!foo)bar
4407    
4408         does not find an occurrence of "bar"  that  is  preceded  by  something         does  not  find  an  occurrence  of "bar" that is preceded by something
4409         other  than "foo"; it finds any occurrence of "bar" whatsoever, because         other than "foo"; it finds any occurrence of "bar" whatsoever,  because
4410         the assertion (?!foo) is always true when the next three characters are         the assertion (?!foo) is always true when the next three characters are
4411         "bar". A lookbehind assertion is needed to achieve the other effect.         "bar". A lookbehind assertion is needed to achieve the other effect.
4412    
4413         If you want to force a matching failure at some point in a pattern, the         If you want to force a matching failure at some point in a pattern, the
4414         most convenient way to do it is  with  (?!)  because  an  empty  string         most  convenient  way  to  do  it  is with (?!) because an empty string
4415         always  matches, so an assertion that requires there not to be an empty         always matches, so an assertion that requires there not to be an  empty
4416         string must always fail.         string must always fail.
4417    
4418     Lookbehind assertions     Lookbehind assertions
4419    
4420         Lookbehind assertions start with (?<= for positive assertions and  (?<!         Lookbehind  assertions start with (?<= for positive assertions and (?<!
4421         for negative assertions. For example,         for negative assertions. For example,
4422    
4423           (?<!foo)bar           (?<!foo)bar
4424    
4425         does  find  an  occurrence  of "bar" that is not preceded by "foo". The         does find an occurrence of "bar" that is not  preceded  by  "foo".  The
4426         contents of a lookbehind assertion are restricted  such  that  all  the         contents  of  a  lookbehind  assertion are restricted such that all the
4427         strings it matches must have a fixed length. However, if there are sev-         strings it matches must have a fixed length. However, if there are sev-
4428         eral top-level alternatives, they do not all  have  to  have  the  same         eral  top-level  alternatives,  they  do  not all have to have the same
4429         fixed length. Thus         fixed length. Thus
4430    
4431           (?<=bullock|donkey)           (?<=bullock|donkey)
# Line 4153  ASSERTIONS Line 4434  ASSERTIONS
4434    
4435           (?<!dogs?|cats?)           (?<!dogs?|cats?)
4436    
4437         causes  an  error at compile time. Branches that match different length         causes an error at compile time. Branches that match  different  length
4438         strings are permitted only at the top level of a lookbehind  assertion.         strings  are permitted only at the top level of a lookbehind assertion.
4439         This  is  an  extension  compared  with  Perl (at least for 5.8), which         This is an extension compared with  Perl  (at  least  for  5.8),  which
4440         requires all branches to match the same length of string. An  assertion         requires  all branches to match the same length of string. An assertion
4441         such as         such as
4442    
4443           (?<=ab(c|de))           (?<=ab(c|de))
4444    
4445         is  not  permitted,  because  its single top-level branch can match two         is not permitted, because its single top-level  branch  can  match  two
4446         different lengths, but it is acceptable if rewritten to  use  two  top-         different  lengths,  but  it is acceptable if rewritten to use two top-
4447         level branches:         level branches:
4448    
4449           (?<=abc|abde)           (?<=abc|abde)
4450    
4451         In some cases, the Perl 5.10 escape sequence \K (see above) can be used         In some cases, the Perl 5.10 escape sequence \K (see above) can be used
4452         instead of a lookbehind assertion; this is not restricted to  a  fixed-         instead  of  a lookbehind assertion; this is not restricted to a fixed-
4453         length.         length.
4454    
4455         The  implementation  of lookbehind assertions is, for each alternative,         The implementation of lookbehind assertions is, for  each  alternative,
4456         to temporarily move the current position back by the fixed  length  and         to  temporarily  move the current position back by the fixed length and
4457         then try to match. If there are insufficient characters before the cur-         then try to match. If there are insufficient characters before the cur-
4458         rent position, the assertion fails.         rent position, the assertion fails.
4459    
4460         PCRE does not allow the \C escape (which matches a single byte in UTF-8         PCRE does not allow the \C escape (which matches a single byte in UTF-8
4461         mode)  to appear in lookbehind assertions, because it makes it impossi-         mode) to appear in lookbehind assertions, because it makes it  impossi-
4462         ble to calculate the length of the lookbehind. The \X and  \R  escapes,         ble  to  calculate the length of the lookbehind. The \X and \R escapes,
4463         which can match different numbers of bytes, are also not permitted.         which can match different numbers of bytes, are also not permitted.
4464    
4465         Possessive  quantifiers  can  be  used  in  conjunction with lookbehind         Possessive quantifiers can  be  used  in  conjunction  with  lookbehind
4466         assertions to specify efficient matching at  the  end  of  the  subject         assertions  to  specify  efficient  matching  at the end of the subject
4467         string. Consider a simple pattern such as         string. Consider a simple pattern such as
4468    
4469           abcd$           abcd$
4470    
4471         when  applied  to  a  long string that does not match. Because matching         when applied to a long string that does  not  match.  Because  matching
4472         proceeds from left to right, PCRE will look for each "a" in the subject         proceeds from left to right, PCRE will look for each "a" in the subject
4473         and  then  see  if what follows matches the rest of the pattern. If the         and then see if what follows matches the rest of the  pattern.  If  the
4474         pattern is specified as         pattern is specified as
4475    
4476           ^.*abcd$           ^.*abcd$
4477    
4478         the initial .* matches the entire string at first, but when this  fails         the  initial .* matches the entire string at first, but when this fails
4479         (because there is no following "a"), it backtracks to match all but the         (because there is no following "a"), it backtracks to match all but the
4480         last character, then all but the last two characters, and so  on.  Once         last  character,  then all but the last two characters, and so on. Once
4481         again  the search for "a" covers the entire string, from right to left,         again the search for "a" covers the entire string, from right to  left,
4482         so we are no better off. However, if the pattern is written as         so we are no better off. However, if the pattern is written as
4483    
4484           ^.*+(?<=abcd)           ^.*+(?<=abcd)
4485    
4486         there can be no backtracking for the .*+ item; it can  match  only  the         there  can  be  no backtracking for the .*+ item; it can match only the
4487         entire  string.  The subsequent lookbehind assertion does a single test         entire string. The subsequent lookbehind assertion does a  single  test
4488         on the last four characters. If it fails, the match fails  immediately.         on  the last four characters. If it fails, the match fails immediately.
4489         For  long  strings, this approach makes a significant difference to the         For long strings, this approach makes a significant difference  to  the
4490         processing time.         processing time.
4491    
4492     Using multiple assertions     Using multiple assertions
# Line 4214  ASSERTIONS Line 4495  ASSERTIONS
4495    
4496           (?<=\d{3})(?<!999)foo           (?<=\d{3})(?<!999)foo
4497    
4498         matches "foo" preceded by three digits that are not "999". Notice  that         matches  "foo" preceded by three digits that are not "999". Notice that
4499         each  of  the  assertions is applied independently at the same point in         each of the assertions is applied independently at the  same  point  in
4500         the subject string. First there is a  check  that  the  previous  three         the  subject  string.  First  there  is a check that the previous three
4501         characters  are  all  digits,  and  then there is a check that the same         characters are all digits, and then there is  a  check  that  the  same
4502         three characters are not "999".  This pattern does not match "foo" pre-         three characters are not "999".  This pattern does not match "foo" pre-
4503         ceded  by  six  characters,  the first of which are digits and the last         ceded by six characters, the first of which are  digits  and  the  last
4504         three of which are not "999". For example, it  doesn't  match  "123abc-         three  of  which  are not "999". For example, it doesn't match "123abc-
4505         foo". A pattern to do that is         foo". A pattern to do that is
4506    
4507           (?<=\d{3}...)(?<!999)foo           (?<=\d{3}...)(?<!999)foo
4508    
4509         This  time  the  first assertion looks at the preceding six characters,         This time the first assertion looks at the  preceding  six  characters,
4510         checking that the first three are digits, and then the second assertion         checking that the first three are digits, and then the second assertion
4511         checks that the preceding three characters are not "999".         checks that the preceding three characters are not "999".
4512    
# Line 4233  ASSERTIONS Line 4514  ASSERTIONS
4514    
4515           (?<=(?<!foo)bar)baz           (?<=(?<!foo)bar)baz
4516    
4517         matches  an occurrence of "baz" that is preceded by "bar" which in turn         matches an occurrence of "baz" that is preceded by "bar" which in  turn
4518         is not preceded by "foo", while         is not preceded by "foo", while
4519    
4520           (?<=\d{3}(?!999)...)foo           (?<=\d{3}(?!999)...)foo
4521    
4522         is another pattern that matches "foo" preceded by three digits and  any         is  another pattern that matches "foo" preceded by three digits and any
4523         three characters that are not "999".         three characters that are not "999".
4524    
4525    
4526  CONDITIONAL SUBPATTERNS  CONDITIONAL SUBPATTERNS
4527    
4528         It  is possible to cause the matching process to obey a subpattern con-         It is possible to cause the matching process to obey a subpattern  con-
4529         ditionally or to choose between two alternative subpatterns,  depending         ditionally  or to choose between two alternative subpatterns, depending
4530         on  the result of an assertion, or whether a previous capturing subpat-         on the result of an assertion, or whether a previous capturing  subpat-
4531         tern matched or not. The two possible forms of  conditional  subpattern         tern  matched  or not. The two possible forms of conditional subpattern
4532         are         are
4533    
4534           (?(condition)yes-pattern)           (?(condition)yes-pattern)
4535           (?(condition)yes-pattern|no-pattern)           (?(condition)yes-pattern|no-pattern)
4536    
4537         If  the  condition is satisfied, the yes-pattern is used; otherwise the         If the condition is satisfied, the yes-pattern is used;  otherwise  the
4538         no-pattern (if present) is used. If there are more  than  two  alterna-         no-pattern  (if  present)  is used. If there are more than two alterna-
4539         tives in the subpattern, a compile-time error occurs.         tives in the subpattern, a compile-time error occurs.
4540    
4541         There  are  four  kinds of condition: references to subpatterns, refer-         There are four kinds of condition: references  to  subpatterns,  refer-
4542         ences to recursion, a pseudo-condition called DEFINE, and assertions.         ences to recursion, a pseudo-condition called DEFINE, and assertions.
4543    
4544     Checking for a used subpattern by number     Checking for a used subpattern by number
4545    
4546         If the text between the parentheses consists of a sequence  of  digits,         If  the  text between the parentheses consists of a sequence of digits,
4547         the  condition  is  true if the capturing subpattern of that number has         the condition is true if the capturing subpattern of  that  number  has
4548         previously matched. An alternative notation is to  precede  the  digits         previously  matched.  An  alternative notation is to precede the digits
4549         with a plus or minus sign. In this case, the subpattern number is rela-         with a plus or minus sign. In this case, the subpattern number is rela-
4550         tive rather than absolute.  The most recently opened parentheses can be         tive rather than absolute.  The most recently opened parentheses can be
4551         referenced  by  (?(-1),  the  next most recent by (?(-2), and so on. In         referenced by (?(-1), the next most recent by (?(-2),  and  so  on.  In
4552         looping constructs it can also make sense to refer to subsequent groups         looping constructs it can also make sense to refer to subsequent groups
4553         with constructs such as (?(+2).         with constructs such as (?(+2).
4554    
4555         Consider  the  following  pattern, which contains non-significant white         Consider the following pattern, which  contains  non-significant  white
4556         space to make it more readable (assume the PCRE_EXTENDED option) and to         space to make it more readable (assume the PCRE_EXTENDED option) and to
4557         divide it into three parts for ease of discussion:         divide it into three parts for ease of discussion:
4558    
4559           ( \( )?    [^()]+    (?(1) \) )           ( \( )?    [^()]+    (?(1) \) )
4560    
4561         The  first  part  matches  an optional opening parenthesis, and if that         The first part matches an optional opening  parenthesis,  and  if  that
4562         character is present, sets it as the first captured substring. The sec-         character is present, sets it as the first captured substring. The sec-
4563         ond  part  matches one or more characters that are not parentheses. The         ond part matches one or more characters that are not  parentheses.  The
4564         third part is a conditional subpattern that tests whether the first set         third part is a conditional subpattern that tests whether the first set
4565         of parentheses matched or not. If they did, that is, if subject started         of parentheses matched or not. If they did, that is, if subject started
4566         with an opening parenthesis, the condition is true, and so the yes-pat-         with an opening parenthesis, the condition is true, and so the yes-pat-
4567         tern  is  executed  and  a  closing parenthesis is required. Otherwise,         tern is executed and a  closing  parenthesis  is  required.  Otherwise,
4568         since no-pattern is not present, the  subpattern  matches  nothing.  In         since  no-pattern  is  not  present, the subpattern matches nothing. In
4569         other  words,  this  pattern  matches  a  sequence  of non-parentheses,         other words,  this  pattern  matches  a  sequence  of  non-parentheses,
4570         optionally enclosed in parentheses.         optionally enclosed in parentheses.
4571    
4572         If you were embedding this pattern in a larger one,  you  could  use  a         If  you  were  embedding  this pattern in a larger one, you could use a
4573         relative reference:         relative reference:
4574    
4575           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...
4576    
4577         This  makes  the  fragment independent of the parentheses in the larger         This makes the fragment independent of the parentheses  in  the  larger
4578         pattern.         pattern.
4579    
4580     Checking for a used subpattern by name     Checking for a used subpattern by name
4581    
4582         Perl uses the syntax (?(<name>)...) or (?('name')...)  to  test  for  a         Perl  uses  the  syntax  (?(<name>)...) or (?('name')...) to test for a
4583         used  subpattern  by  name.  For compatibility with earlier versions of         used subpattern by name. For compatibility  with  earlier  versions  of
4584         PCRE, which had this facility before Perl, the syntax  (?(name)...)  is         PCRE,  which  had this facility before Perl, the syntax (?(name)...) is
4585         also  recognized. However, there is a possible ambiguity with this syn-         also recognized. However, there is a possible ambiguity with this  syn-
4586         tax, because subpattern names may  consist  entirely  of  digits.  PCRE         tax,  because  subpattern  names  may  consist entirely of digits. PCRE
4587         looks  first for a named subpattern; if it cannot find one and the name         looks first for a named subpattern; if it cannot find one and the  name
4588         consists entirely of digits, PCRE looks for a subpattern of  that  num-         consists  entirely  of digits, PCRE looks for a subpattern of that num-
4589         ber,  which must be greater than zero. Using subpattern names that con-         ber, which must be greater than zero. Using subpattern names that  con-
4590         sist entirely of digits is not recommended.         sist entirely of digits is not recommended.
4591    
4592         Rewriting the above example to use a named subpattern gives this:         Rewriting the above example to use a named subpattern gives this:
# Line 4316  CONDITIONAL SUBPATTERNS Line 4597  CONDITIONAL SUBPATTERNS
4597     Checking for pattern recursion     Checking for pattern recursion
4598    
4599         If the condition is the string (R), and there is no subpattern with the         If the condition is the string (R), and there is no subpattern with the
4600         name  R, the condition is true if a recursive call to the whole pattern         name R, the condition is true if a recursive call to the whole  pattern
4601         or any subpattern has been made. If digits or a name preceded by amper-         or any subpattern has been made. If digits or a name preceded by amper-
4602         sand follow the letter R, for example:         sand follow the letter R, for example:
4603    
4604           (?(R3)...) or (?(R&name)...)           (?(R3)...) or (?(R&name)...)
4605    
4606         the  condition is true if the most recent recursion is into the subpat-         the condition is true if the most recent recursion is into the  subpat-
4607         tern whose number or name is given. This condition does not  check  the         tern  whose  number or name is given. This condition does not check the
4608         entire recursion stack.         entire recursion stack.
4609    
4610         At  "top  level", all these recursion test conditions are false. Recur-         At "top level", all these recursion test conditions are  false.  Recur-
4611         sive patterns are described below.         sive patterns are described below.
4612    
4613     Defining subpatterns for use by reference only     Defining subpatterns for use by reference only
4614    
4615         If the condition is the string (DEFINE), and  there  is  no  subpattern         If  the  condition  is  the string (DEFINE), and there is no subpattern
4616         with  the  name  DEFINE,  the  condition is always false. In this case,         with the name DEFINE, the condition is  always  false.  In  this  case,
4617         there may be only one alternative  in  the  subpattern.  It  is  always         there  may  be  only  one  alternative  in the subpattern. It is always
4618         skipped  if  control  reaches  this  point  in the pattern; the idea of         skipped if control reaches this point  in  the  pattern;  the  idea  of
4619         DEFINE is that it can be used to define "subroutines" that can be  ref-         DEFINE  is that it can be used to define "subroutines" that can be ref-
4620         erenced  from elsewhere. (The use of "subroutines" is described below.)         erenced from elsewhere. (The use of "subroutines" is described  below.)
4621         For example, a pattern to match an IPv4 address could be  written  like         For  example,  a pattern to match an IPv4 address could be written like
4622         this (ignore whitespace and line breaks):         this (ignore whitespace and line breaks):
4623    
4624           (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )           (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
4625           \b (?&byte) (\.(?&byte)){3} \b           \b (?&byte) (\.(?&byte)){3} \b
4626    
4627         The  first part of the pattern is a DEFINE group inside which a another         The first part of the pattern is a DEFINE group inside which a  another
4628         group named "byte" is defined. This matches an individual component  of         group  named "byte" is defined. This matches an individual component of
4629         an  IPv4  address  (a number less than 256). When matching takes place,         an IPv4 address (a number less than 256). When  matching  takes  place,
4630         this part of the pattern is skipped because DEFINE acts  like  a  false         this  part  of  the pattern is skipped because DEFINE acts like a false
4631         condition.         condition.
4632    
4633         The rest of the pattern uses references to the named group to match the         The rest of the pattern uses references to the named group to match the
4634         four dot-separated components of an IPv4 address, insisting on  a  word         four  dot-separated  components of an IPv4 address, insisting on a word
4635         boundary at each end.         boundary at each end.
4636    
4637     Assertion conditions     Assertion conditions
4638    
4639         If  the  condition  is  not  in any of the above formats, it must be an         If the condition is not in any of the above  formats,  it  must  be  an
4640         assertion.  This may be a positive or negative lookahead or  lookbehind         assertion.   This may be a positive or negative lookahead or lookbehind
4641         assertion.  Consider  this  pattern,  again  containing non-significant         assertion. Consider  this  pattern,  again  containing  non-significant
4642         white space, and with the two alternatives on the second line:         white space, and with the two alternatives on the second line:
4643    
4644           (?(?=[^a-z]*[a-z])           (?(?=[^a-z]*[a-z])
4645           \d{2}-[a-z]{3}-\d{2}  |  \d{2}-\d{2}-\d{2} )           \d{2}-[a-z]{3}-\d{2}  |  \d{2}-\d{2}-\d{2} )
4646    
4647         The condition  is  a  positive  lookahead  assertion  that  matches  an         The  condition  is  a  positive  lookahead  assertion  that  matches an
4648         optional  sequence of non-letters followed by a letter. In other words,         optional sequence of non-letters followed by a letter. In other  words,
4649         it tests for the presence of at least one letter in the subject.  If  a         it  tests  for the presence of at least one letter in the subject. If a
4650         letter  is found, the subject is matched against the first alternative;         letter is found, the subject is matched against the first  alternative;
4651         otherwise it is  matched  against  the  second.  This  pattern  matches         otherwise  it  is  matched  against  the  second.  This pattern matches
4652         strings  in  one  of the two forms dd-aaa-dd or dd-dd-dd, where aaa are         strings in one of the two forms dd-aaa-dd or dd-dd-dd,  where  aaa  are
4653         letters and dd are digits.         letters and dd are digits.
4654    
4655    
4656  COMMENTS  COMMENTS
4657    
4658         The sequence (?# marks the start of a comment that continues up to  the         The  sequence (?# marks the start of a comment that continues up to the
4659         next  closing  parenthesis.  Nested  parentheses are not permitted. The         next closing parenthesis. Nested parentheses  are  not  permitted.  The
4660         characters that make up a comment play no part in the pattern  matching         characters  that make up a comment play no part in the pattern matching
4661         at all.         at all.
4662    
4663         If  the PCRE_EXTENDED option is set, an unescaped # character outside a         If the PCRE_EXTENDED option is set, an unescaped # character outside  a
4664         character class introduces a  comment  that  continues  to  immediately         character  class  introduces  a  comment  that continues to immediately
4665         after the next newline in the pattern.         after the next newline in the pattern.
4666    
4667    
4668  RECURSIVE PATTERNS  RECURSIVE PATTERNS
4669    
4670         Consider  the problem of matching a string in parentheses, allowing for         Consider the problem of matching a string in parentheses, allowing  for
4671         unlimited nested parentheses. Without the use of  recursion,  the  best         unlimited  nested  parentheses.  Without the use of recursion, the best
4672         that  can  be  done  is  to use a pattern that matches up to some fixed         that can be done is to use a pattern that  matches  up  to  some  fixed
4673         depth of nesting. It is not possible to  handle  an  arbitrary  nesting         depth  of  nesting.  It  is not possible to handle an arbitrary nesting
4674         depth.         depth.
4675    
4676         For some time, Perl has provided a facility that allows regular expres-         For some time, Perl has provided a facility that allows regular expres-
4677         sions to recurse (amongst other things). It does this by  interpolating         sions  to recurse (amongst other things). It does this by interpolating
4678         Perl  code in the expression at run time, and the code can refer to the         Perl code in the expression at run time, and the code can refer to  the
4679         expression itself. A Perl pattern using code interpolation to solve the         expression itself. A Perl pattern using code interpolation to solve the
4680         parentheses problem can be created like this:         parentheses problem can be created like this:
4681    
# Line 4404  RECURSIVE PATTERNS Line 4685  RECURSIVE PATTERNS
4685         refers recursively to the pattern in which it appears.         refers recursively to the pattern in which it appears.
4686    
4687         Obviously, PCRE cannot support the interpolation of Perl code. Instead,         Obviously, PCRE cannot support the interpolation of Perl code. Instead,
4688         it  supports  special  syntax  for recursion of the entire pattern, and         it supports special syntax for recursion of  the  entire  pattern,  and
4689         also for individual subpattern recursion.  After  its  introduction  in         also  for  individual  subpattern  recursion. After its introduction in
4690         PCRE  and  Python,  this  kind of recursion was introduced into Perl at         PCRE and Python, this kind of recursion was  introduced  into  Perl  at
4691         release 5.10.         release 5.10.
4692    
4693         A special item that consists of (? followed by a  number  greater  than         A  special  item  that consists of (? followed by a number greater than
4694         zero and a closing parenthesis is a recursive call of the subpattern of         zero and a closing parenthesis is a recursive call of the subpattern of
4695         the given number, provided that it occurs inside that  subpattern.  (If         the  given  number, provided that it occurs inside that subpattern. (If
4696         not,  it  is  a  "subroutine" call, which is described in the next sec-         not, it is a "subroutine" call, which is described  in  the  next  sec-
4697         tion.) The special item (?R) or (?0) is a recursive call of the  entire         tion.)  The special item (?R) or (?0) is a recursive call of the entire
4698         regular expression.         regular expression.
4699    
4700         In  PCRE (like Python, but unlike Perl), a recursive subpattern call is         In PCRE (like Python, but unlike Perl), a recursive subpattern call  is
4701         always treated as an atomic group. That is, once it has matched some of         always treated as an atomic group. That is, once it has matched some of
4702         the subject string, it is never re-entered, even if it contains untried         the subject string, it is never re-entered, even if it contains untried
4703         alternatives and there is a subsequent matching failure.         alternatives and there is a subsequent matching failure.
4704    
4705         This PCRE pattern solves the nested  parentheses  problem  (assume  the         This  PCRE  pattern  solves  the nested parentheses problem (assume the
4706         PCRE_EXTENDED option is set so that white space is ignored):         PCRE_EXTENDED option is set so that white space is ignored):
4707    
4708           \( ( (?>[^()]+) | (?R) )* \)           \( ( (?>[^()]+) | (?R) )* \)
4709    
4710         First  it matches an opening parenthesis. Then it matches any number of         First it matches an opening parenthesis. Then it matches any number  of
4711         substrings which can either be a  sequence  of  non-parentheses,  or  a         substrings  which  can  either  be  a sequence of non-parentheses, or a
4712         recursive  match  of the pattern itself (that is, a correctly parenthe-         recursive match of the pattern itself (that is, a  correctly  parenthe-
4713         sized substring).  Finally there is a closing parenthesis.         sized substring).  Finally there is a closing parenthesis.
4714    
4715         If this were part of a larger pattern, you would not  want  to  recurse         If  this  were  part of a larger pattern, you would not want to recurse
4716         the entire pattern, so instead you could use this:         the entire pattern, so instead you could use this:
4717    
4718           ( \( ( (?>[^()]+) | (?1) )* \) )           ( \( ( (?>[^()]+) | (?1) )* \) )
4719