1PCREPATTERN(3)             Library Functions Manual             PCREPATTERN(3)
2
3
4

NAME

6       PCRE - Perl-compatible regular expressions
7

PCRE REGULAR EXPRESSION DETAILS

9
10       The  syntax and semantics of the regular expressions that are supported
11       by PCRE are described in detail below. There is a quick-reference  syn‐
12       tax summary in the pcresyntax page. PCRE tries to match Perl syntax and
13       semantics as closely as it can. PCRE  also  supports  some  alternative
14       regular  expression  syntax (which does not conflict with the Perl syn‐
15       tax) in order to provide some compatibility with regular expressions in
16       Python, .NET, and Oniguruma.
17
18       Perl's  regular expressions are described in its own documentation, and
19       regular expressions in general are covered in a number of  books,  some
20       of which have copious examples. Jeffrey Friedl's "Mastering Regular Ex‐
21       pressions", published by O'Reilly, covers regular expressions in  great
22       detail.  This  description of PCRE's regular expressions is intended as
23       reference material.
24
25       This document discusses the patterns that are supported  by  PCRE  when
26       one    its    main   matching   functions,   pcre_exec()   (8-bit)   or
27       pcre[16|32]_exec() (16- or 32-bit), is used. PCRE also has  alternative
28       matching  functions,  pcre_dfa_exec()  and pcre[16|32_dfa_exec(), which
29       match using a different algorithm that is not Perl-compatible. Some  of
30       the  features  discussed  below  are not available when DFA matching is
31       used. The advantages and disadvantages of  the  alternative  functions,
32       and  how  they  differ  from the normal functions, are discussed in the
33       pcrematching page.
34

SPECIAL START-OF-PATTERN ITEMS

36
37       A number of options that can be passed to pcre_compile()  can  also  be
38       set by special items at the start of a pattern. These are not Perl-com‐
39       patible, but are provided to make these options accessible  to  pattern
40       writers  who are not able to change the program that processes the pat‐
41       tern. Any number of these items may appear, but they must  all  be  to‐
42       gether  right  at the start of the pattern string, and the letters must
43       be in upper case.
44
45   UTF support
46
47       The original operation of PCRE was on strings of  one-byte  characters.
48       However,  there  is  now also support for UTF-8 strings in the original
49       library, an extra library that supports  16-bit  and  UTF-16  character
50       strings,  and a third library that supports 32-bit and UTF-32 character
51       strings. To use these features, PCRE must be built to include appropri‐
52       ate  support. When using UTF strings you must either call the compiling
53       function with the PCRE_UTF8, PCRE_UTF16, or PCRE_UTF32 option,  or  the
54       pattern must start with one of these special sequences:
55
56         (*UTF8)
57         (*UTF16)
58         (*UTF32)
59         (*UTF)
60
61       (*UTF)  is  a  generic  sequence  that  can be used with any of the li‐
62       braries.  Starting a pattern with such a sequence is equivalent to set‐
63       ting the relevant option. How setting a UTF mode affects pattern match‐
64       ing is mentioned in several places below. There is also  a  summary  of
65       features in the pcreunicode page.
66
67       Some applications that allow their users to supply patterns may wish to
68       restrict  them  to  non-UTF  data  for   security   reasons.   If   the
69       PCRE_NEVER_UTF  option  is set at compile time, (*UTF) etc. are not al‐
70       lowed, and their appearance causes an error.
71
72   Unicode property support
73
74       Another special sequence that may appear at the start of a  pattern  is
75       (*UCP).   This  has  the same effect as setting the PCRE_UCP option: it
76       causes sequences such as \d and \w to use Unicode properties to  deter‐
77       mine character types, instead of recognizing only characters with codes
78       less than 128 via a lookup table.
79
80   Disabling auto-possessification
81
82       If a pattern starts with (*NO_AUTO_POSSESS), it has the same effect  as
83       setting  the  PCRE_NO_AUTO_POSSESS  option  at compile time. This stops
84       PCRE from making quantifiers possessive when what follows cannot  match
85       the  repeated item. For example, by default a+b is treated as a++b. For
86       more details, see the pcreapi documentation.
87
88   Disabling start-up optimizations
89
90       If a pattern starts with (*NO_START_OPT), it has  the  same  effect  as
91       setting the PCRE_NO_START_OPTIMIZE option either at compile or matching
92       time. This disables several  optimizations  for  quickly  reaching  "no
93       match" results. For more details, see the pcreapi documentation.
94
95   Newline conventions
96
97       PCRE  supports five different conventions for indicating line breaks in
98       strings: a single CR (carriage return) character, a  single  LF  (line‐
99       feed) character, the two-character sequence CRLF, any of the three pre‐
100       ceding, or any Unicode newline sequence. The pcreapi page  has  further
101       discussion  about newlines, and shows how to set the newline convention
102       in the options arguments for the compiling and matching functions.
103
104       It is also possible to specify a newline convention by starting a  pat‐
105       tern string with one of the following five sequences:
106
107         (*CR)        carriage return
108         (*LF)        linefeed
109         (*CRLF)      carriage return, followed by linefeed
110         (*ANYCRLF)   any of the three above
111         (*ANY)       all Unicode newline sequences
112
113       These override the default and the options given to the compiling func‐
114       tion. For example, on a Unix system where LF is the default newline se‐
115       quence, the pattern
116
117         (*CR)a.b
118
119       changes the convention to CR. That pattern matches "a\nb" because LF is
120       no longer a newline. If more than one of these settings is present, the
121       last one is used.
122
123       The  newline  convention affects where the circumflex and dollar asser‐
124       tions are true. It also affects the interpretation of the dot metachar‐
125       acter when PCRE_DOTALL is not set, and the behaviour of \N. However, it
126       does not affect what the \R escape sequence matches. By  default,  this
127       is  any Unicode newline sequence, for Perl compatibility. However, this
128       can be changed; see the description of \R in the section entitled "New‐
129       line  sequences"  below.  A change of \R setting can be combined with a
130       change of newline convention.
131
132   Setting match and recursion limits
133
134       The caller of pcre_exec() can set a limit on the number  of  times  the
135       internal  match() function is called and on the maximum depth of recur‐
136       sive calls. These facilities are provided to catch runaway matches that
137       are provoked by patterns with huge matching trees (a typical example is
138       a pattern with nested unlimited repeats) and to avoid  running  out  of
139       system  stack  by  too  much  recursion.  When  one  of these limits is
140       reached, pcre_exec() gives an error return. The limits can also be  set
141       by items at the start of the pattern of the form
142
143         (*LIMIT_MATCH=d)
144         (*LIMIT_RECURSION=d)
145
146       where d is any number of decimal digits. However, the value of the set‐
147       ting must be less than the value set (or defaulted) by  the  caller  of
148       pcre_exec()  for  it  to  have  any effect. In other words, the pattern
149       writer can lower the limits set by the programmer, but not raise  them.
150       If  there  is  more  than one setting of one of these limits, the lower
151       value is used.
152

EBCDIC CHARACTER CODES

154
155       PCRE can be compiled to run in an environment that uses EBCDIC  as  its
156       character code rather than ASCII or Unicode (typically a mainframe sys‐
157       tem). In the sections below, character code values are  ASCII  or  Uni‐
158       code; in an EBCDIC environment these characters may have different code
159       values, and there are no code points greater than 255.
160

CHARACTERS AND METACHARACTERS

162
163       A regular expression is a pattern that is  matched  against  a  subject
164       string  from  left  to right. Most characters stand for themselves in a
165       pattern, and match the corresponding characters in the  subject.  As  a
166       trivial example, the pattern
167
168         The quick brown fox
169
170       matches a portion of a subject string that is identical to itself. When
171       caseless matching is specified (the PCRE_CASELESS option), letters  are
172       matched  independently  of case. In a UTF mode, PCRE always understands
173       the concept of case for characters whose values are less than  128,  so
174       caseless  matching  is always possible. For characters with higher val‐
175       ues, the concept of case is supported if PCRE is compiled with  Unicode
176       property  support,  but  not  otherwise.   If  you want to use caseless
177       matching for characters 128 and above, you must  ensure  that  PCRE  is
178       compiled with Unicode property support as well as with UTF support.
179
180       The  power of regular expressions comes from the ability to include al‐
181       ternatives and repetitions in the pattern. These  are  encoded  in  the
182       pattern by the use of metacharacters, which do not stand for themselves
183       but instead are interpreted in some special way.
184
185       There are two different sets of metacharacters: those that  are  recog‐
186       nized  anywhere in the pattern except within square brackets, and those
187       that are recognized within square brackets.  Outside  square  brackets,
188       the metacharacters are as follows:
189
190         \      general escape character with several uses
191         ^      assert start of string (or line, in multiline mode)
192         $      assert end of string (or line, in multiline mode)
193         .      match any character except newline (by default)
194         [      start character class definition
195         |      start of alternative branch
196         (      start subpattern
197         )      end subpattern
198         ?      extends the meaning of (
199                also 0 or 1 quantifier
200                also quantifier minimizer
201         *      0 or more quantifier
202         +      1 or more quantifier
203                also "possessive quantifier"
204         {      start min/max quantifier
205
206       Part  of  a  pattern  that is in square brackets is called a "character
207       class". In a character class the only metacharacters are:
208
209         \      general escape character
210         ^      negate the class, but only if the first character
211         -      indicates character range
212         [      POSIX character class (only if followed by POSIX
213                  syntax)
214         ]      terminates the character class
215
216       The following sections describe the use of each of the metacharacters.
217

BACKSLASH

219
220       The backslash character has several uses. Firstly, if it is followed by
221       a character that is not a number or a letter, it takes away any special
222       meaning that character may have. This use of  backslash  as  an  escape
223       character applies both inside and outside character classes.
224
225       For  example,  if  you want to match a * character, you write \* in the
226       pattern.  This escaping action applies whether  or  not  the  following
227       character  would  otherwise be interpreted as a metacharacter, so it is
228       always safe to precede a non-alphanumeric  with  backslash  to  specify
229       that  it stands for itself. In particular, if you want to match a back‐
230       slash, you write \\.
231
232       In a UTF mode, only ASCII numbers and letters have any special  meaning
233       after  a  backslash.  All  other characters (in particular, those whose
234       codepoints are greater than 127) are treated as literals.
235
236       If a pattern is compiled with  the  PCRE_EXTENDED  option,  most  white
237       space  in the pattern (other than in a character class), and characters
238       between a # outside a character class and the next newline,  inclusive,
239       are ignored. An escaping backslash can be used to include a white space
240       or # character as part of the pattern.
241
242       If you want to remove the special meaning from a  sequence  of  charac‐
243       ters,  you can do so by putting them between \Q and \E. This is differ‐
244       ent from Perl in that $ and @ are handled as literals  in  \Q...\E  se‐
245       quences in PCRE, whereas in Perl, $ and @ cause variable interpolation.
246       Note the following examples:
247
248         Pattern            PCRE matches   Perl matches
249
250         \Qabc$xyz\E        abc$xyz        abc followed by the
251                                             contents of $xyz
252         \Qabc\$xyz\E       abc\$xyz       abc\$xyz
253         \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz
254
255       The \Q...\E sequence is recognized both inside  and  outside  character
256       classes.   An  isolated \E that is not preceded by \Q is ignored. If \Q
257       is not followed by \E later in the pattern, the literal  interpretation
258       continues  to  the  end  of  the pattern (that is, \E is assumed at the
259       end). If the isolated \Q is inside a character class,  this  causes  an
260       error, because the character class is not terminated.
261
262   Non-printing characters
263
264       A second use of backslash provides a way of encoding non-printing char‐
265       acters in patterns in a visible manner. There is no restriction on  the
266       appearance  of non-printing characters, apart from the binary zero that
267       terminates a pattern, but when a pattern  is  being  prepared  by  text
268       editing,  it  is  often  easier  to use one of the following escape se‐
269       quences than the binary character it represents.  In an ASCII  or  Uni‐
270       code environment, these escapes are as follows:
271
272         \a        alarm, that is, the BEL character (hex 07)
273         \cx       "control-x", where x is any ASCII character
274         \e        escape (hex 1B)
275         \f        form feed (hex 0C)
276         \n        linefeed (hex 0A)
277         \r        carriage return (hex 0D)
278         \t        tab (hex 09)
279         \0dd      character with octal code 0dd
280         \ddd      character with octal code ddd, or back reference
281         \o{ddd..} character with octal code ddd..
282         \xhh      character with hex code hh
283         \x{hhh..} character with hex code hhh.. (non-JavaScript mode)
284         \uhhhh    character with hex code hhhh (JavaScript mode only)
285
286       The  precise effect of \cx on ASCII characters is as follows: if x is a
287       lower case letter, it is converted to upper case. Then  bit  6  of  the
288       character (hex 40) is inverted. Thus \cA to \cZ become hex 01 to hex 1A
289       (A is 41, Z is 5A), but \c{ becomes hex 3B ({ is 7B), and  \c;  becomes
290       hex  7B (; is 3B). If the data item (byte or 16-bit value) following \c
291       has a value greater than 127, a compile-time error occurs.  This  locks
292       out non-ASCII characters in all modes.
293
294       When PCRE is compiled in EBCDIC mode, \a, \e, \f, \n, \r, and \t gener‐
295       ate the appropriate EBCDIC code values. The \c escape is  processed  as
296       specified for Perl in the perlebcdic document. The only characters that
297       are allowed after \c are A-Z, a-z, or one of @, [, \, ], ^,  _,  or  ?.
298       Any other character provokes a compile-time error. The sequence \c@ en‐
299       codes character code 0; after \c the letters (in  either  case)  encode
300       characters 1-26 (hex 01 to hex 1A); [, \, ], ^, and _ encode characters
301       27-31 (hex 1B to hex 1F), and \c? becomes either 255  (hex  FF)  or  95
302       (hex 5F).
303
304       Thus,  apart  from  \c?, these escapes generate the same character code
305       values as they do in an ASCII environment, though the meanings  of  the
306       values  mostly  differ. For example, \cG always generates code value 7,
307       which is BEL in ASCII but DEL in EBCDIC.
308
309       The sequence \c? generates DEL (127, hex 7F) in an  ASCII  environment,
310       but  because  127  is  not a control character in EBCDIC, Perl makes it
311       generate the APC character. Unfortunately, there are  several  variants
312       of  EBCDIC.  In  most  of them the APC character has the value 255 (hex
313       FF), but in the one Perl calls POSIX-BC its value is 95  (hex  5F).  If
314       certain  other characters have POSIX-BC values, PCRE makes \c? generate
315       95; otherwise it generates 255.
316
317       After \0 up to two further octal digits are read. If  there  are  fewer
318       than  two  digits,  just  those that are present are used. Thus the se‐
319       quence \0\x\015 specifies two binary zeros followed by a  CR  character
320       (code value 13). Make sure you supply two digits after the initial zero
321       if the pattern character that follows is itself an octal digit.
322
323       The escape \o must be followed by a sequence of octal digits,  enclosed
324       in  braces.  An  error occurs if this is not the case. This escape is a
325       recent addition to Perl; it provides way of specifying  character  code
326       points  as  octal  numbers  greater than 0777, and it also allows octal
327       numbers and back references to be unambiguously specified.
328
329       For greater clarity and unambiguity, it is best to avoid following \ by
330       a digit greater than zero. Instead, use \o{} or \x{} to specify charac‐
331       ter numbers, and \g{} to specify back references. The  following  para‐
332       graphs describe the old, ambiguous syntax.
333
334       The handling of a backslash followed by a digit other than 0 is compli‐
335       cated, and Perl has changed in recent releases, causing  PCRE  also  to
336       change. Outside a character class, PCRE reads the digit and any follow‐
337       ing digits as a decimal number. If the number is less  than  8,  or  if
338       there  have been at least that many previous capturing left parentheses
339       in the expression, the entire sequence is taken as a back reference.  A
340       description  of how this works is given later, following the discussion
341       of parenthesized subpatterns.
342
343       Inside a character class, or if  the  decimal  number  following  \  is
344       greater than 7 and there have not been that many capturing subpatterns,
345       PCRE handles \8 and \9 as the literal characters "8" and "9", and  oth‐
346       erwise re-reads up to three octal digits following the backslash, using
347       them to generate a data character.  Any  subsequent  digits  stand  for
348       themselves. For example:
349
350         \040   is another way of writing an ASCII space
351         \40    is the same, provided there are fewer than 40
352                   previous capturing subpatterns
353         \7     is always a back reference
354         \11    might be a back reference, or another way of
355                   writing a tab
356         \011   is always a tab
357         \0113  is a tab followed by the character "3"
358         \113   might be a back reference, otherwise the
359                   character with octal code 113
360         \377   might be a back reference, otherwise
361                   the value 255 (decimal)
362         \81    is either a back reference, or the two
363                   characters "8" and "1"
364
365       Note  that octal values of 100 or greater that are specified using this
366       syntax must not be introduced by a leading zero, because no  more  than
367       three octal digits are ever read.
368
369       By  default, after \x that is not followed by {, from zero to two hexa‐
370       decimal digits are read (letters can be in upper or  lower  case).  Any
371       number of hexadecimal digits may appear between \x{ and }. If a charac‐
372       ter other than a hexadecimal digit appears between \x{  and  },  or  if
373       there is no terminating }, an error occurs.
374
375       If  the  PCRE_JAVASCRIPT_COMPAT option is set, the interpretation of \x
376       is as just described only when it is followed by two  hexadecimal  dig‐
377       its.   Otherwise,  it  matches  a  literal "x" character. In JavaScript
378       mode, support for code points greater than 256 is provided by \u, which
379       must  be  followed  by  four hexadecimal digits; otherwise it matches a
380       literal "u" character.
381
382       Characters whose value is less than 256 can be defined by either of the
383       two  syntaxes for \x (or by \u in JavaScript mode). There is no differ‐
384       ence in the way they are handled. For example, \xdc is exactly the same
385       as \x{dc} (or \u00dc in JavaScript mode).
386
387   Constraints on character values
388
389       Characters  that  are  specified using octal or hexadecimal numbers are
390       limited to certain values, as follows:
391
392         8-bit non-UTF mode    less than 0x100
393         8-bit UTF-8 mode      less than 0x10ffff and a valid codepoint
394         16-bit non-UTF mode   less than 0x10000
395         16-bit UTF-16 mode    less than 0x10ffff and a valid codepoint
396         32-bit non-UTF mode   less than 0x100000000
397         32-bit UTF-32 mode    less than 0x10ffff and a valid codepoint
398
399       Invalid Unicode codepoints are the range  0xd800  to  0xdfff  (the  so-
400       called "surrogate" codepoints), and 0xffef.
401
402   Escape sequences in character classes
403
404       All the sequences that define a single character value can be used both
405       inside and outside character classes. In addition, inside  a  character
406       class, \b is interpreted as the backspace character (hex 08).
407
408       \N  is not allowed in a character class. \B, \R, and \X are not special
409       inside a character class. Like  other  unrecognized  escape  sequences,
410       they  are  treated  as  the literal characters "B", "R", and "X" by de‐
411       fault, but cause an error if the PCRE_EXTRA option is  set.  Outside  a
412       character class, these sequences have different meanings.
413
414   Unsupported escape sequences
415
416       In  Perl, the sequences \l, \L, \u, and \U are recognized by its string
417       handler and used to modify the case of  following  characters.  By  de‐
418       fault,  PCRE  does  not support these escape sequences. However, if the
419       PCRE_JAVASCRIPT_COMPAT option is set, \U matches a "U"  character,  and
420       \u can be used to define a character by code point, as described in the
421       previous section.
422
423   Absolute and relative back references
424
425       The sequence \g followed by an unsigned or a negative  number,  option‐
426       ally  enclosed  in braces, is an absolute or relative back reference. A
427       named back reference can be coded as \g{name}. Back references are dis‐
428       cussed later, following the discussion of parenthesized subpatterns.
429
430   Absolute and relative subroutine calls
431
432       For  compatibility with Oniguruma, the non-Perl syntax \g followed by a
433       name or a number enclosed either in angle brackets or single quotes, is
434       an  alternative  syntax for referencing a subpattern as a "subroutine".
435       Details are discussed later.   Note  that  \g{...}  (Perl  syntax)  and
436       \g<...>  (Oniguruma  syntax)  are  not synonymous. The former is a back
437       reference; the latter is a subroutine call.
438
439   Generic character types
440
441       Another use of backslash is for specifying generic character types:
442
443         \d     any decimal digit
444         \D     any character that is not a decimal digit
445         \h     any horizontal white space character
446         \H     any character that is not a horizontal white space character
447         \s     any white space character
448         \S     any character that is not a white space character
449         \v     any vertical white space character
450         \V     any character that is not a vertical white space character
451         \w     any "word" character
452         \W     any "non-word" character
453
454       There is also the single sequence \N, which matches a non-newline char‐
455       acter.   This  is the same as the "." metacharacter when PCRE_DOTALL is
456       not set. Perl also uses \N to match characters by name; PCRE  does  not
457       support this.
458
459       Each  pair of lower and upper case escape sequences partitions the com‐
460       plete set of characters into two disjoint  sets.  Any  given  character
461       matches  one, and only one, of each pair. The sequences can appear both
462       inside and outside character classes. They each match one character  of
463       the  appropriate  type.  If the current matching point is at the end of
464       the subject string, all of them fail, because there is no character  to
465       match.
466
467       For  compatibility with Perl, \s did not used to match the VT character
468       (code 11), which made it different from the the  POSIX  "space"  class.
469       However,  Perl  added VT at release 5.18, and PCRE followed suit at re‐
470       lease 8.34. The default \s characters are now HT (9), LF (10), VT (11),
471       FF  (12),  CR (13), and space (32), which are defined as white space in
472       the "C" locale. This list may vary if locale-specific matching is  tak‐
473       ing  place. For example, in some locales the "non-breaking space" char‐
474       acter (\xA0) is recognized as white space, and in others the VT charac‐
475       ter is not.
476
477       A  "word"  character is an underscore or any character that is a letter
478       or digit.  By default, the definition of letters  and  digits  is  con‐
479       trolled  by PCRE's low-valued character tables, and may vary if locale-
480       specific matching is taking place (see "Locale support" in the  pcreapi
481       page).  For  example,  in  a French locale such as "fr_FR" in Unix-like
482       systems, or "french" in Windows, some character codes greater than  127
483       are  used  for  accented letters, and these are then matched by \w. The
484       use of locales with Unicode is discouraged.
485
486       By default, characters whose code points are  greater  than  127  never
487       match \d, \s, or \w, and always match \D, \S, and \W, although this may
488       vary for characters in the range 128-255 when locale-specific  matching
489       is  happening.   These  escape sequences retain their original meanings
490       from before Unicode support was available, mainly for  efficiency  rea‐
491       sons.  If  PCRE  is  compiled  with  Unicode  property support, and the
492       PCRE_UCP option is set, the behaviour is changed so that Unicode  prop‐
493       erties are used to determine character types, as follows:
494
495         \d  any character that matches \p{Nd} (decimal digit)
496         \s  any character that matches \p{Z} or \h or \v
497         \w  any character that matches \p{L} or \p{N}, plus underscore
498
499       The  upper case escapes match the inverse sets of characters. Note that
500       \d matches only decimal digits, whereas \w matches any  Unicode  digit,
501       as  well as any Unicode letter, and underscore. Note also that PCRE_UCP
502       affects \b, and \B because they are defined in  terms  of  \w  and  \W.
503       Matching these sequences is noticeably slower when PCRE_UCP is set.
504
505       The  sequences  \h, \H, \v, and \V are features that were added to Perl
506       at release 5.10. In contrast to the other sequences, which  match  only
507       ASCII  characters  by  default,  these always match certain high-valued
508       code points, whether or not PCRE_UCP is set. The horizontal space char‐
509       acters are:
510
511         U+0009     Horizontal tab (HT)
512         U+0020     Space
513         U+00A0     Non-break space
514         U+1680     Ogham space mark
515         U+180E     Mongolian vowel separator
516         U+2000     En quad
517         U+2001     Em quad
518         U+2002     En space
519         U+2003     Em space
520         U+2004     Three-per-em space
521         U+2005     Four-per-em space
522         U+2006     Six-per-em space
523         U+2007     Figure space
524         U+2008     Punctuation space
525         U+2009     Thin space
526         U+200A     Hair space
527         U+202F     Narrow no-break space
528         U+205F     Medium mathematical space
529         U+3000     Ideographic space
530
531       The vertical space characters are:
532
533         U+000A     Linefeed (LF)
534         U+000B     Vertical tab (VT)
535         U+000C     Form feed (FF)
536         U+000D     Carriage return (CR)
537         U+0085     Next line (NEL)
538         U+2028     Line separator
539         U+2029     Paragraph separator
540
541       In 8-bit, non-UTF-8 mode, only the characters with codepoints less than
542       256 are relevant.
543
544   Newline sequences
545
546       Outside a character class, by default, the escape sequence  \R  matches
547       any  Unicode newline sequence. In 8-bit non-UTF-8 mode \R is equivalent
548       to the following:
549
550         (?>\r\n|\n|\x0b|\f|\r|\x85)
551
552       This is an example of an "atomic group", details of which are given be‐
553       low.   This  particular group matches either the two-character sequence
554       CR followed by LF, or  one  of  the  single  characters  LF  (linefeed,
555       U+000A),  VT  (vertical  tab, U+000B), FF (form feed, U+000C), CR (car‐
556       riage return, U+000D), or NEL (next line,  U+0085).  The  two-character
557       sequence is treated as a single unit that cannot be split.
558
559       In  other modes, two additional characters whose codepoints are greater
560       than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa‐
561       rator,  U+2029).   Unicode character property support is not needed for
562       these characters to be recognized.
563
564       It is possible to restrict \R to match only CR, LF, or CRLF (instead of
565       the  complete  set  of  Unicode  line  endings)  by  setting the option
566       PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched.
567       (BSR  is  an  abbreviation for "backslash R".) This can be made the de‐
568       fault when PCRE is built; if this is the case, the other behaviour  can
569       be  requested  via the PCRE_BSR_UNICODE option.  It is also possible to
570       specify these settings by starting a pattern string  with  one  of  the
571       following sequences:
572
573         (*BSR_ANYCRLF)   CR, LF, or CRLF only
574         (*BSR_UNICODE)   any Unicode newline sequence
575
576       These override the default and the options given to the compiling func‐
577       tion, but they can themselves be  overridden  by  options  given  to  a
578       matching  function.  Note  that  these  special settings, which are not
579       Perl-compatible, are recognized only at the very start  of  a  pattern,
580       and  that  they  must  be  in  upper  case. If more than one of them is
581       present, the last one is used. They can be combined with  a  change  of
582       newline convention; for example, a pattern can start with:
583
584         (*ANY)(*BSR_ANYCRLF)
585
586       They  can also be combined with the (*UTF8), (*UTF16), (*UTF32), (*UTF)
587       or (*UCP) special sequences. Inside a character class, \R is treated as
588       an  unrecognized  escape sequence, and so matches the letter "R" by de‐
589       fault, but causes an error if PCRE_EXTRA is set.
590
591   Unicode character properties
592
593       When PCRE is built with Unicode character property support, three addi‐
594       tional  escape sequences that match characters with specific properties
595       are available.  When in 8-bit non-UTF-8 mode, these  sequences  are  of
596       course  limited  to  testing  characters whose codepoints are less than
597       256, but they do work in this mode.  The extra escape sequences are:
598
599         \p{xx}   a character with the xx property
600         \P{xx}   a character without the xx property
601         \X       a Unicode extended grapheme cluster
602
603       The property names represented by xx above are limited to  the  Unicode
604       script names, the general category properties, "Any", which matches any
605       character (including newline), and some special  PCRE  properties  (de‐
606       scribed  in  the next section).  Other Perl properties such as "InMusi‐
607       calSymbols" are not currently supported by PCRE. Note that \P{Any} does
608       not match any characters, so always causes a match failure.
609
610       Sets of Unicode characters are defined as belonging to certain scripts.
611       A character from one of these sets can be matched using a script  name.
612       For example:
613
614         \p{Greek}
615         \P{Han}
616
617       Those  that are not part of an identified script are lumped together as
618       "Common". The current list of scripts is:
619
620       Arabic, Armenian, Avestan, Balinese, Bamum, Bassa_Vah, Batak,  Bengali,
621       Bopomofo,  Brahmi,  Braille, Buginese, Buhid, Canadian_Aboriginal, Car‐
622       ian, Caucasian_Albanian, Chakma, Cham, Cherokee, Common, Coptic, Cunei‐
623       form, Cypriot, Cyrillic, Deseret, Devanagari, Duployan, Egyptian_Hiero‐
624       glyphs,  Elbasan,  Ethiopic,  Georgian,  Glagolitic,  Gothic,  Grantha,
625       Greek,  Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hiragana, Im‐
626       perial_Aramaic,     Inherited,     Inscriptional_Pahlavi,      Inscrip‐
627       tional_Parthian,   Javanese,   Kaithi,   Kannada,  Katakana,  Kayah_Li,
628       Kharoshthi, Khmer, Khojki, Khudawadi, Lao, Latin, Lepcha,  Limbu,  Lin‐
629       ear_A,  Linear_B,  Lisu,  Lycian, Lydian, Mahajani, Malayalam, Mandaic,
630       Manichaean, Meetei_Mayek, Mende_Kikakui, Meroitic_Cursive, Meroitic_Hi‐
631       eroglyphs, Miao, Modi, Mongolian, Mro, Myanmar, Nabataean, New_Tai_Lue,
632       Nko,  Ogham,  Ol_Chiki,  Old_Italic,   Old_North_Arabian,   Old_Permic,
633       Old_Persian,   Old_South_Arabian,   Old_Turkic,   Oriya,  Osmanya,  Pa‐
634       hawh_Hmong,    Palmyrene,    Pau_Cin_Hau,     Phags_Pa,     Phoenician,
635       Psalter_Pahlavi,  Rejang,  Runic,  Samaritan, Saurashtra, Sharada, Sha‐
636       vian, Siddham, Sinhala, Sora_Sompeng, Sundanese, Syloti_Nagri,  Syriac,
637       Tagalog,  Tagbanwa,  Tai_Le,  Tai_Tham, Tai_Viet, Takri, Tamil, Telugu,
638       Thaana, Thai, Tibetan, Tifinagh, Tirhuta, Ugaritic,  Vai,  Warang_Citi,
639       Yi.
640
641       Each character has exactly one Unicode general category property, spec‐
642       ified by a two-letter abbreviation. For compatibility with Perl,  nega‐
643       tion  can  be  specified  by including a circumflex between the opening
644       brace and the property name.  For  example,  \p{^Lu}  is  the  same  as
645       \P{Lu}.
646
647       If only one letter is specified with \p or \P, it includes all the gen‐
648       eral category properties that start with that letter. In this case,  in
649       the  absence of negation, the curly brackets in the escape sequence are
650       optional; these two examples have the same effect:
651
652         \p{L}
653         \pL
654
655       The following general category property codes are supported:
656
657         C     Other
658         Cc    Control
659         Cf    Format
660         Cn    Unassigned
661         Co    Private use
662         Cs    Surrogate
663
664         L     Letter
665         Ll    Lower case letter
666         Lm    Modifier letter
667         Lo    Other letter
668         Lt    Title case letter
669         Lu    Upper case letter
670
671         M     Mark
672         Mc    Spacing mark
673         Me    Enclosing mark
674         Mn    Non-spacing mark
675
676         N     Number
677         Nd    Decimal number
678         Nl    Letter number
679         No    Other number
680
681         P     Punctuation
682         Pc    Connector punctuation
683         Pd    Dash punctuation
684         Pe    Close punctuation
685         Pf    Final punctuation
686         Pi    Initial punctuation
687         Po    Other punctuation
688         Ps    Open punctuation
689
690         S     Symbol
691         Sc    Currency symbol
692         Sk    Modifier symbol
693         Sm    Mathematical symbol
694         So    Other symbol
695
696         Z     Separator
697         Zl    Line separator
698         Zp    Paragraph separator
699         Zs    Space separator
700
701       The special property L& is also supported: it matches a character  that
702       has  the  Lu,  Ll, or Lt property, in other words, a letter that is not
703       classified as a modifier or "other".
704
705       The Cs (Surrogate) property applies only to  characters  in  the  range
706       U+D800  to U+DFFF. Such characters are not valid in Unicode strings and
707       so cannot be tested by PCRE, unless  UTF  validity  checking  has  been
708       turned    off    (see    the    discussion    of    PCRE_NO_UTF8_CHECK,
709       PCRE_NO_UTF16_CHECK and PCRE_NO_UTF32_CHECK in the pcreapi page).  Perl
710       does not support the Cs property.
711
712       The  long  synonyms  for  property  names  that  Perl supports (such as
713       \p{Letter}) are not supported by PCRE, nor is it  permitted  to  prefix
714       any of these properties with "Is".
715
716       No character that is in the Unicode table has the Cn (unassigned) prop‐
717       erty.  Instead, this property is assumed for any code point that is not
718       in the Unicode table.
719
720       Specifying  caseless  matching  does not affect these escape sequences.
721       For example, \p{Lu} always matches only upper  case  letters.  This  is
722       different from the behaviour of current versions of Perl.
723
724       Matching  characters  by Unicode property is not fast, because PCRE has
725       to do a multistage table lookup in order to find  a  character's  prop‐
726       erty. That is why the traditional escape sequences such as \d and \w do
727       not use Unicode properties in PCRE by default, though you can make them
728       do  so  by  setting the PCRE_UCP option or by starting the pattern with
729       (*UCP).
730
731   Extended grapheme clusters
732
733       The \X escape matches any number of Unicode  characters  that  form  an
734       "extended grapheme cluster", and treats the sequence as an atomic group
735       (see below).  Up to and including release 8.31, PCRE  matched  an  ear‐
736       lier, simpler definition that was equivalent to
737
738         (?>\PM\pM*)
739
740       That  is,  it matched a character without the "mark" property, followed
741       by zero or more characters with the "mark"  property.  Characters  with
742       the  "mark"  property are typically non-spacing accents that affect the
743       preceding character.
744
745       This simple definition was extended in Unicode to include more  compli‐
746       cated  kinds of composite character by giving each character a grapheme
747       breaking property, and creating rules that use these properties to  de‐
748       fine  the boundaries of extended grapheme clusters. In releases of PCRE
749       later than 8.31, \X matches one of these clusters.
750
751       \X always matches at least one character. Then it  decides  whether  to
752       add additional characters according to the following rules for ending a
753       cluster:
754
755       1. End at the end of the subject string.
756
757       2. Do not end between CR and LF; otherwise end after any control  char‐
758       acter.
759
760       3.  Do  not  break  Hangul (a Korean script) syllable sequences. Hangul
761       characters are of five types: L, V, T, LV, and LVT. An L character  may
762       be  followed by an L, V, LV, or LVT character; an LV or V character may
763       be followed by a V or T character; an LVT or T character  may  be  fol‐
764       lowed only by a T character.
765
766       4.  Do not end before extending characters or spacing marks. Characters
767       with the "mark" property always have  the  "extend"  grapheme  breaking
768       property.
769
770       5. Do not end after prepend characters.
771
772       6. Otherwise, end the cluster.
773
774   PCRE's additional properties
775
776       As  well  as the standard Unicode properties described above, PCRE sup‐
777       ports four more that make it possible to convert traditional escape se‐
778       quences  such  as  \w and \s to use Unicode properties. PCRE uses these
779       non-standard, non-Perl properties internally when PCRE_UCP is set. How‐
780       ever, they may also be used explicitly. These properties are:
781
782         Xan   Any alphanumeric character
783         Xps   Any POSIX space character
784         Xsp   Any Perl space character
785         Xwd   Any Perl "word" character
786
787       Xan  matches  characters that have either the L (letter) or the N (num‐
788       ber) property. Xps matches the characters tab, linefeed, vertical  tab,
789       form  feed,  or carriage return, and any other character that has the Z
790       (separator) property.  Xsp is the same as Xps; it used to exclude  ver‐
791       tical  tab,  for Perl compatibility, but Perl changed, and so PCRE fol‐
792       lowed at release 8.34. Xwd matches the same characters as Xan, plus un‐
793       derscore.
794
795       There  is another non-standard property, Xuc, which matches any charac‐
796       ter that can be represented by a Universal Character Name  in  C++  and
797       other  programming  languages.  These are the characters $, @, ` (grave
798       accent), and all characters with Unicode code points  greater  than  or
799       equal  to U+00A0, except for the surrogates U+D800 to U+DFFF. Note that
800       most base (ASCII) characters are excluded. (Universal  Character  Names
801       are  of  the  form \uHHHH or \UHHHHHHHH where H is a hexadecimal digit.
802       Note that the Xuc property does not match these sequences but the char‐
803       acters that they represent.)
804
805   Resetting the match start
806
807       The  escape sequence \K causes any previously matched characters not to
808       be included in the final matched sequence. For example, the pattern:
809
810         foo\Kbar
811
812       matches "foobar", but reports that it has matched "bar".  This  feature
813       is  similar  to  a lookbehind assertion (described below).  However, in
814       this case, the part of the subject before the real match does not  have
815       to  be of fixed length, as lookbehind assertions do. The use of \K does
816       not interfere with the setting of captured  substrings.   For  example,
817       when the pattern
818
819         (foo)\Kbar
820
821       matches "foobar", the first substring is still set to "foo".
822
823       Perl  documents  that  the use of \K within assertions is "not well de‐
824       fined". In PCRE, \K is acted upon when it occurs inside positive asser‐
825       tions,  but is ignored in negative assertions. Note that when a pattern
826       such as (?=ab\K) matches, the  reported  start  of  the  match  can  be
827       greater than the end of the match.
828
829   Simple assertions
830
831       The  final use of backslash is for certain simple assertions. An asser‐
832       tion specifies a condition that has to be met at a particular point  in
833       a  match, without consuming any characters from the subject string. The
834       use of subpatterns for more complicated assertions is described  below.
835       The backslashed assertions are:
836
837         \b     matches at a word boundary
838         \B     matches when not at a word boundary
839         \A     matches at the start of the subject
840         \Z     matches at the end of the subject
841                 also matches before a newline at the end of the subject
842         \z     matches only at the end of the subject
843         \G     matches at the first matching position in the subject
844
845       Inside  a  character  class, \b has a different meaning; it matches the
846       backspace character. If any other of  these  assertions  appears  in  a
847       character  class, by default it matches the corresponding literal char‐
848       acter (for example, \B matches the letter B). However, if the  PCRE_EX‐
849       TRA  option is set, an "invalid escape sequence" error is generated in‐
850       stead.
851
852       A word boundary is a position in the subject string where  the  current
853       character  and  the previous character do not both match \w or \W (i.e.
854       one matches \w and the other matches \W), or the start or  end  of  the
855       string  if  the  first or last character matches \w, respectively. In a
856       UTF mode, the meanings of \w and \W  can  be  changed  by  setting  the
857       PCRE_UCP  option. When this is done, it also affects \b and \B. Neither
858       PCRE nor Perl has a separate "start of word" or "end of  word"  metase‐
859       quence.  However,  whatever follows \b normally determines which it is.
860       For example, the fragment \ba matches "a" at the start of a word.
861
862       The \A, \Z, and \z assertions differ from  the  traditional  circumflex
863       and dollar (described in the next section) in that they only ever match
864       at the very start and end of the subject string, whatever  options  are
865       set.  Thus,  they are independent of multiline mode. These three asser‐
866       tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which
867       affect  only the behaviour of the circumflex and dollar metacharacters.
868       However, if the startoffset argument of pcre_exec() is non-zero,  indi‐
869       cating that matching is to start at a point other than the beginning of
870       the subject, \A can never match. The difference between \Z  and  \z  is
871       that \Z matches before a newline at the end of the string as well as at
872       the very end, whereas \z matches only at the end.
873
874       The \G assertion is true only when the current matching position is  at
875       the  start point of the match, as specified by the startoffset argument
876       of pcre_exec(). It differs from \A when the  value  of  startoffset  is
877       non-zero.  By calling pcre_exec() multiple times with appropriate argu‐
878       ments, you can mimic Perl's /g option, and it is in this kind of imple‐
879       mentation where \G can be useful.
880
881       Note,  however,  that  PCRE's interpretation of \G, as the start of the
882       current match, is subtly different from Perl's, which defines it as the
883       end  of  the  previous  match. In Perl, these can be different when the
884       previously matched string was empty. Because PCRE does just  one  match
885       at a time, it cannot reproduce this behaviour.
886
887       If  all  the alternatives of a pattern begin with \G, the expression is
888       anchored to the starting match position, and the "anchored" flag is set
889       in the compiled regular expression.
890

CIRCUMFLEX AND DOLLAR

892
893       The  circumflex  and  dollar  metacharacters are zero-width assertions.
894       That is, they test for a particular condition being true  without  con‐
895       suming any characters from the subject string.
896
897       Outside a character class, in the default matching mode, the circumflex
898       character is an assertion that is true only  if  the  current  matching
899       point  is  at the start of the subject string. If the startoffset argu‐
900       ment of pcre_exec() is non-zero, circumflex  can  never  match  if  the
901       PCRE_MULTILINE  option  is  unset. Inside a character class, circumflex
902       has an entirely different meaning (see below).
903
904       Circumflex need not be the first character of the pattern if  a  number
905       of  alternatives are involved, but it should be the first thing in each
906       alternative in which it appears if the pattern is ever  to  match  that
907       branch.  If all possible alternatives start with a circumflex, that is,
908       if the pattern is constrained to match only at the start  of  the  sub‐
909       ject,  it  is  said  to be an "anchored" pattern. (There are also other
910       constructs that can cause a pattern to be anchored.)
911
912       The dollar character is an assertion that is true only if  the  current
913       matching  point is at the end of the subject string, or immediately be‐
914       fore a newline at the end of the string (by  default).  Note,  however,
915       that  it  does  not  actually match the newline. Dollar need not be the
916       last character of the pattern if a number of alternatives are involved,
917       but  it should be the last item in any branch in which it appears. Dol‐
918       lar has no special meaning in a character class.
919
920       The meaning of dollar can be changed so that it  matches  only  at  the
921       very  end  of  the string, by setting the PCRE_DOLLAR_ENDONLY option at
922       compile time. This does not affect the \Z assertion.
923
924       The meanings of the circumflex and dollar characters are changed if the
925       PCRE_MULTILINE  option  is  set.  When  this  is the case, a circumflex
926       matches immediately after internal newlines as well as at the start  of
927       the  subject  string.  It  does not match after a newline that ends the
928       string. A dollar matches before any newlines in the string, as well  as
929       at  the very end, when PCRE_MULTILINE is set. When newline is specified
930       as the two-character sequence CRLF, isolated CR and  LF  characters  do
931       not indicate newlines.
932
933       For  example, the pattern /^abc$/ matches the subject string "def\nabc"
934       (where \n represents a newline) in multiline mode, but  not  otherwise.
935       Consequently,  patterns  that  are anchored in single line mode because
936       all branches start with ^ are not anchored in  multiline  mode,  and  a
937       match  for  circumflex  is  possible  when  the startoffset argument of
938       pcre_exec() is non-zero. The PCRE_DOLLAR_ENDONLY option is  ignored  if
939       PCRE_MULTILINE is set.
940
941       Note  that  the sequences \A, \Z, and \z can be used to match the start
942       and end of the subject in both modes, and if all branches of a  pattern
943       start  with  \A it is always anchored, whether or not PCRE_MULTILINE is
944       set.
945

FULL STOP (PERIOD, DOT) AND \N

947
948       Outside a character class, a dot in the pattern matches any one charac‐
949       ter  in  the subject string except (by default) a character that signi‐
950       fies the end of a line.
951
952       When a line ending is defined as a single character, dot never  matches
953       that  character; when the two-character sequence CRLF is used, dot does
954       not match CR if it is immediately followed  by  LF,  but  otherwise  it
955       matches  all characters (including isolated CRs and LFs). When any Uni‐
956       code line endings are being recognized, dot does not match CR or LF  or
957       any of the other line ending characters.
958
959       The  behaviour  of  dot  with regard to newlines can be changed. If the
960       PCRE_DOTALL option is set, a dot matches any one character, without ex‐
961       ception.  If  the two-character sequence CRLF is present in the subject
962       string, it takes two dots to match it.
963
964       The handling of dot is entirely independent of the handling of  circum‐
965       flex  and  dollar,  the  only relationship being that they both involve
966       newlines. Dot has no special meaning in a character class.
967
968       The escape sequence \N behaves like a dot, except that it  is  not  af‐
969       fected  by the PCRE_DOTALL option. In other words, it matches any char‐
970       acter except one that signifies the end of a line. Perl also uses \N to
971       match characters by name; PCRE does not support this.
972

MATCHING A SINGLE DATA UNIT

974
975       Outside  a character class, the escape sequence \C matches any one data
976       unit, whether or not a UTF mode is set. In the 8-bit library, one  data
977       unit  is  one  byte;  in the 16-bit library it is a 16-bit unit; in the
978       32-bit library it is a 32-bit unit. Unlike a  dot,  \C  always  matches
979       line-ending  characters.  The  feature  is provided in Perl in order to
980       match individual bytes in UTF-8 mode, but it is unclear how it can use‐
981       fully  be  used.  Because  \C breaks up characters into individual data
982       units, matching one unit with \C in a UTF mode means that the  rest  of
983       the string may start with a malformed UTF character. This has undefined
984       results, because PCRE assumes that it is dealing with valid UTF strings
985       (and  by  default  it checks this at the start of processing unless the
986       PCRE_NO_UTF8_CHECK, PCRE_NO_UTF16_CHECK or  PCRE_NO_UTF32_CHECK  option
987       is used).
988
989       PCRE  does  not  allow \C to appear in lookbehind assertions (described
990       below) in a UTF mode, because this would make it impossible  to  calcu‐
991       late the length of the lookbehind.
992
993       In general, the \C escape sequence is best avoided. However, one way of
994       using it that avoids the problem of malformed UTF characters is to  use
995       a  lookahead to check the length of the next character, as in this pat‐
996       tern, which could be used with a UTF-8 string (ignore white  space  and
997       line breaks):
998
999         (?| (?=[\x00-\x7f])(\C) |
1000             (?=[\x80-\x{7ff}])(\C)(\C) |
1001             (?=[\x{800}-\x{ffff}])(\C)(\C)(\C) |
1002             (?=[\x{10000}-\x{1fffff}])(\C)(\C)(\C)(\C))
1003
1004       A  group  that starts with (?| resets the capturing parentheses numbers
1005       in each alternative (see "Duplicate Subpattern Numbers" below). The as‐
1006       sertions at the start of each branch check the next UTF-8 character for
1007       values whose encoding uses 1, 2, 3, or 4 bytes, respectively. The char‐
1008       acter's individual bytes are then captured by the appropriate number of
1009       groups.
1010

SQUARE BRACKETS AND CHARACTER CLASSES

1012
1013       An opening square bracket introduces a character class, terminated by a
1014       closing square bracket. A closing square bracket on its own is not spe‐
1015       cial by default.  However, if the PCRE_JAVASCRIPT_COMPAT option is set,
1016       a lone closing square bracket causes a compile-time error. If a closing
1017       square bracket is required as a member of the class, it should  be  the
1018       first  data  character  in  the  class (after an initial circumflex, if
1019       present) or escaped with a backslash.
1020
1021       A character class matches a single character in the subject. In  a  UTF
1022       mode,  the  character  may  be  more than one data unit long. A matched
1023       character must be in the set of characters defined by the class, unless
1024       the  first  character in the class definition is a circumflex, in which
1025       case the subject character must not be in the set defined by the class.
1026       If  a  circumflex is actually required as a member of the class, ensure
1027       it is not the first character, or escape it with a backslash.
1028
1029       For example, the character class [aeiou] matches any lower case  vowel,
1030       while  [^aeiou]  matches  any character that is not a lower case vowel.
1031       Note that a circumflex is just a convenient notation for specifying the
1032       characters  that  are in the class by enumerating those that are not. A
1033       class that starts with a circumflex is not an assertion; it still  con‐
1034       sumes  a  character  from the subject string, and therefore it fails if
1035       the current pointer is at the end of the string.
1036
1037       In UTF-8 (UTF-16, UTF-32) mode, characters with values greater than 255
1038       (0xffff)  can be included in a class as a literal string of data units,
1039       or by using the \x{ escaping mechanism.
1040
1041       When caseless matching is set, any letters in a  class  represent  both
1042       their  upper  case  and lower case versions, so for example, a caseless
1043       [aeiou] matches "A" as well as "a", and a caseless  [^aeiou]  does  not
1044       match  "A", whereas a caseful version would. In a UTF mode, PCRE always
1045       understands the concept of case for characters whose  values  are  less
1046       than  128, so caseless matching is always possible. For characters with
1047       higher values, the concept of case is supported  if  PCRE  is  compiled
1048       with  Unicode  property support, but not otherwise.  If you want to use
1049       caseless matching in a UTF mode for characters 128 and above, you  must
1050       ensure  that  PCRE is compiled with Unicode property support as well as
1051       with UTF support.
1052
1053       Characters that might indicate line breaks are  never  treated  in  any
1054       special  way  when matching character classes, whatever line-ending se‐
1055       quence is in use, and whatever setting of the PCRE_DOTALL and PCRE_MUL‐
1056       TILINE  options  is  used.  A  class such as [^a] always matches one of
1057       these characters.
1058
1059       The minus (hyphen) character can be used to specify a range of  charac‐
1060       ters  in  a  character class. For example, [d-m] matches any letter be‐
1061       tween d and m, inclusive. If a minus character is required in a  class,
1062       it  must  be  escaped with a backslash or appear in a position where it
1063       cannot be interpreted as indicating a range, typically as the first  or
1064       last character in the class, or immediately after a range. For example,
1065       [b-d-z] matches letters in the range b to d, a hyphen character, or z.
1066
1067       It is not possible to have the literal character "]" as the end charac‐
1068       ter  of a range. A pattern such as [W-]46] is interpreted as a class of
1069       two characters ("W" and "-") followed by a literal string "46]", so  it
1070       would  match  "W46]"  or  "-46]". However, if the "]" is escaped with a
1071       backslash it is interpreted as the end of range, so [W-\]46] is  inter‐
1072       preted  as a class containing a range followed by two other characters.
1073       The octal or hexadecimal representation of "]" can also be used to  end
1074       a range.
1075
1076       An  error is generated if a POSIX character class (see below) or an es‐
1077       cape sequence other than one that defines a single character appears at
1078       a  point  where  a  range  ending  character  is expected. For example,
1079       [z-\xff] is valid, but [A-\d] and [A-[:digit:]] are not.
1080
1081       Ranges operate in the collating sequence of character values. They  can
1082       also   be  used  for  characters  specified  numerically,  for  example
1083       [\000-\037]. Ranges can include any characters that are valid  for  the
1084       current mode.
1085
1086       If a range that includes letters is used when caseless matching is set,
1087       it matches the letters in either case. For example, [W-c] is equivalent
1088       to  [][\\^_`wxyzabc],  matched  caselessly,  and  in a non-UTF mode, if
1089       character tables for a French locale are in  use,  [\xc8-\xcb]  matches
1090       accented  E  characters  in both cases. In UTF modes, PCRE supports the
1091       concept of case for characters with values greater than 128  only  when
1092       it is compiled with Unicode property support.
1093
1094       The  character escape sequences \d, \D, \h, \H, \p, \P, \s, \S, \v, \V,
1095       \w, and \W may appear in a character class, and add the characters that
1096       they  match to the class. For example, [\dABCDEF] matches any hexadeci‐
1097       mal digit. In UTF modes, the PCRE_UCP option affects  the  meanings  of
1098       \d, \s, \w and their upper case partners, just as it does when they ap‐
1099       pear outside a character class, as described in  the  section  entitled
1100       "Generic character types" above. The escape sequence \b has a different
1101       meaning inside a character class; it matches the  backspace  character.
1102       The  sequences  \B,  \N,  \R, and \X are not special inside a character
1103       class. Like any other unrecognized escape sequences, they  are  treated
1104       as  the literal characters "B", "N", "R", and "X" by default, but cause
1105       an error if the PCRE_EXTRA option is set.
1106
1107       A circumflex can conveniently be used with  the  upper  case  character
1108       types  to specify a more restricted set of characters than the matching
1109       lower case type.  For example, the class [^\W_] matches any  letter  or
1110       digit, but not underscore, whereas [\w] includes underscore. A positive
1111       character class should be read as "something OR something OR ..." and a
1112       negative class as "NOT something AND NOT something AND NOT ...".
1113
1114       The  only  metacharacters  that are recognized in character classes are
1115       backslash, hyphen (only where it can be  interpreted  as  specifying  a
1116       range),  circumflex  (only  at the start), opening square bracket (only
1117       when it can be interpreted as introducing a POSIX class name, or for  a
1118       special  compatibility  feature  -  see the next two sections), and the
1119       terminating closing square bracket.  However,  escaping  other  non-al‐
1120       phanumeric characters does no harm.
1121

POSIX CHARACTER CLASSES

1123
1124       Perl supports the POSIX notation for character classes. This uses names
1125       enclosed by [: and :] within the enclosing square brackets.  PCRE  also
1126       supports this notation. For example,
1127
1128         [01[:alpha:]%]
1129
1130       matches "0", "1", any alphabetic character, or "%". The supported class
1131       names are:
1132
1133         alnum    letters and digits
1134         alpha    letters
1135         ascii    character codes 0 - 127
1136         blank    space or tab only
1137         cntrl    control characters
1138         digit    decimal digits (same as \d)
1139         graph    printing characters, excluding space
1140         lower    lower case letters
1141         print    printing characters, including space
1142         punct    printing characters, excluding letters and digits and space
1143         space    white space (the same as \s from PCRE 8.34)
1144         upper    upper case letters
1145         word     "word" characters (same as \w)
1146         xdigit   hexadecimal digits
1147
1148       The default "space" characters are HT (9), LF (10), VT (11),  FF  (12),
1149       CR  (13),  and space (32). If locale-specific matching is taking place,
1150       the list of space characters may be different; there may  be  fewer  or
1151       more of them. "Space" used to be different to \s, which did not include
1152       VT, for Perl compatibility.  However, Perl changed at release 5.18, and
1153       PCRE  followed  at release 8.34.  "Space" and \s now match the same set
1154       of characters.
1155
1156       The name "word" is a Perl extension, and "blank"  is  a  GNU  extension
1157       from  Perl  5.8. Another Perl extension is negation, which is indicated
1158       by a ^ character after the colon. For example,
1159
1160         [12[:^digit:]]
1161
1162       matches "1", "2", or any non-digit. PCRE (and Perl) also recognize  the
1163       POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but
1164       these are not supported, and an error is given if they are encountered.
1165
1166       By default, characters with values greater than 128 do not match any of
1167       the  POSIX character classes. However, if the PCRE_UCP option is passed
1168       to pcre_compile(), some of the classes  are  changed  so  that  Unicode
1169       character  properties  are  used. This is achieved by replacing certain
1170       POSIX classes by other sequences, as follows:
1171
1172         [:alnum:]  becomes  \p{Xan}
1173         [:alpha:]  becomes  \p{L}
1174         [:blank:]  becomes  \h
1175         [:digit:]  becomes  \p{Nd}
1176         [:lower:]  becomes  \p{Ll}
1177         [:space:]  becomes  \p{Xps}
1178         [:upper:]  becomes  \p{Lu}
1179         [:word:]   becomes  \p{Xwd}
1180
1181       Negated versions, such as [:^alpha:] use \P instead of \p. Three  other
1182       POSIX classes are handled specially in UCP mode:
1183
1184       [:graph:] This  matches  characters that have glyphs that mark the page
1185                 when printed. In Unicode property terms, it matches all char‐
1186                 acters with the L, M, N, P, S, or Cf properties, except for:
1187
1188                   U+061C           Arabic Letter Mark
1189                   U+180E           Mongolian Vowel Separator
1190                   U+2066 - U+2069  Various "isolate"s
1191
1192
1193       [:print:] This  matches  the  same  characters  as [:graph:] plus space
1194                 characters that are not controls, that  is,  characters  with
1195                 the Zs property.
1196
1197       [:punct:] This matches all characters that have the Unicode P (punctua‐
1198                 tion) property, plus those characters whose code  points  are
1199                 less than 128 that have the S (Symbol) property.
1200
1201       The  other  POSIX classes are unchanged, and match only characters with
1202       code points less than 128.
1203

COMPATIBILITY FEATURE FOR WORD BOUNDARIES

1205
1206       In the POSIX.2 compliant library that was included in 4.4BSD Unix,  the
1207       ugly  syntax  [[:<:]]  and [[:>:]] is used for matching "start of word"
1208       and "end of word". PCRE treats these items as follows:
1209
1210         [[:<:]]  is converted to  \b(?=\w)
1211         [[:>:]]  is converted to  \b(?<=\w)
1212
1213       Only these exact character sequences are recognized. A sequence such as
1214       [a[:<:]b]  provokes  error  for  an unrecognized POSIX class name. This
1215       support is not compatible with Perl. It is provided to help  migrations
1216       from other environments, and is best not used in any new patterns. Note
1217       that \b matches at the start and the end of a word (see "Simple  asser‐
1218       tions"  above),  and in a Perl-style pattern the preceding or following
1219       character normally shows which is wanted, without the need for the  as‐
1220       sertions  that are used above in order to give exactly the POSIX behav‐
1221       iour.
1222

VERTICAL BAR

1224
1225       Vertical bar characters are used to separate alternative patterns.  For
1226       example, the pattern
1227
1228         gilbert|sullivan
1229
1230       matches  either "gilbert" or "sullivan". Any number of alternatives may
1231       appear, and an empty  alternative  is  permitted  (matching  the  empty
1232       string). The matching process tries each alternative in turn, from left
1233       to right, and the first one that succeeds is used. If the  alternatives
1234       are  within a subpattern (defined below), "succeeds" means matching the
1235       rest of the main pattern as well as the alternative in the subpattern.
1236

INTERNAL OPTION SETTING

1238
1239       The settings of the  PCRE_CASELESS,  PCRE_MULTILINE,  PCRE_DOTALL,  and
1240       PCRE_EXTENDED  options  (which are Perl-compatible) can be changed from
1241       within the pattern by a sequence of Perl option  letters  enclosed  be‐
1242       tween "(?" and ")".  The option letters are
1243
1244         i  for PCRE_CASELESS
1245         m  for PCRE_MULTILINE
1246         s  for PCRE_DOTALL
1247         x  for PCRE_EXTENDED
1248
1249       For example, (?im) sets caseless, multiline matching. It is also possi‐
1250       ble to unset these options by preceding the letter with a hyphen, and a
1251       combined  setting and unsetting such as (?im-sx), which sets PCRE_CASE‐
1252       LESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and  PCRE_EXTENDED,
1253       is  also  permitted.  If a letter appears both before and after the hy‐
1254       phen, the option is unset.
1255
1256       The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and  PCRE_EXTRA
1257       can  be changed in the same way as the Perl-compatible options by using
1258       the characters J, U and X respectively.
1259
1260       When one of these option changes occurs at top level (that is, not  in‐
1261       side  subpattern  parentheses),  the change applies to the remainder of
1262       the pattern that follows. An option change within a subpattern (see be‐
1263       low  for  a  description  of subpatterns) affects only that part of the
1264       subpattern that follows it, so
1265
1266         (a(?i)b)c
1267
1268       matches abc and aBc and no other strings (assuming PCRE_CASELESS is not
1269       used).   By  this means, options can be made to have different settings
1270       in different parts of the pattern. Any changes made in one  alternative
1271       do  carry  on  into subsequent branches within the same subpattern. For
1272       example,
1273
1274         (a(?i)b|c)
1275
1276       matches "ab", "aB", "c", and "C", even though  when  matching  "C"  the
1277       first  branch  is  abandoned before the option setting. This is because
1278       the effects of option settings happen at compile time. There  would  be
1279       some very weird behaviour otherwise.
1280
1281       Note:  There are other PCRE-specific options that can be set by the ap‐
1282       plication when the compiling or matching functions are called. In  some
1283       cases the pattern can contain special leading sequences such as (*CRLF)
1284       to override what the application has set or what  has  been  defaulted.
1285       Details  are  given  in the section entitled "Newline sequences" above.
1286       There are also the (*UTF8), (*UTF16),(*UTF32), and (*UCP)  leading  se‐
1287       quences  that  can  be used to set UTF and Unicode property modes; they
1288       are equivalent to setting the PCRE_UTF8, PCRE_UTF16, PCRE_UTF32 and the
1289       PCRE_UCP  options,  respectively. The (*UTF) sequence is a generic ver‐
1290       sion that can be used with any of the libraries. However, the  applica‐
1291       tion  can set the PCRE_NEVER_UTF option, which locks out the use of the
1292       (*UTF) sequences.
1293

SUBPATTERNS

1295
1296       Subpatterns are delimited by parentheses (round brackets), which can be
1297       nested.  Turning part of a pattern into a subpattern does two things:
1298
1299       1. It localizes a set of alternatives. For example, the pattern
1300
1301         cat(aract|erpillar|)
1302
1303       matches  "cataract",  "caterpillar", or "cat". Without the parentheses,
1304       it would match "cataract", "erpillar" or an empty string.
1305
1306       2. It sets up the subpattern as  a  capturing  subpattern.  This  means
1307       that,  when  the  whole  pattern  matches,  that portion of the subject
1308       string that matched the subpattern is passed back to the caller via the
1309       ovector  argument  of  the matching function. (This applies only to the
1310       traditional matching functions; the DFA matching functions do not  sup‐
1311       port capturing.)
1312
1313       Opening parentheses are counted from left to right (starting from 1) to
1314       obtain numbers for the  capturing  subpatterns.  For  example,  if  the
1315       string "the red king" is matched against the pattern
1316
1317         the ((red|white) (king|queen))
1318
1319       the captured substrings are "red king", "red", and "king", and are num‐
1320       bered 1, 2, and 3, respectively.
1321
1322       The fact that plain parentheses fulfil  two  functions  is  not  always
1323       helpful.   There are often times when a grouping subpattern is required
1324       without a capturing requirement. If an opening parenthesis is  followed
1325       by  a question mark and a colon, the subpattern does not do any captur‐
1326       ing, and is not counted when computing the  number  of  any  subsequent
1327       capturing  subpatterns. For example, if the string "the white queen" is
1328       matched against the pattern
1329
1330         the ((?:red|white) (king|queen))
1331
1332       the captured substrings are "white queen" and "queen", and are numbered
1333       1 and 2. The maximum number of capturing subpatterns is 65535.
1334
1335       As  a  convenient shorthand, if any option settings are required at the
1336       start of a non-capturing subpattern, the option letters may appear  be‐
1337       tween the "?" and the ":". Thus the two patterns
1338
1339         (?i:saturday|sunday)
1340         (?:(?i)saturday|sunday)
1341
1342       match exactly the same set of strings. Because alternative branches are
1343       tried from left to right, and options are not reset until  the  end  of
1344       the  subpattern is reached, an option setting in one branch does affect
1345       subsequent branches, so the above patterns match "SUNDAY"  as  well  as
1346       "Saturday".
1347

DUPLICATE SUBPATTERN NUMBERS

1349
1350       Perl 5.10 introduced a feature whereby each alternative in a subpattern
1351       uses the same numbers for its capturing parentheses. Such a  subpattern
1352       starts  with (?| and is itself a non-capturing subpattern. For example,
1353       consider this pattern:
1354
1355         (?|(Sat)ur|(Sun))day
1356
1357       Because the two alternatives are inside a (?| group, both sets of  cap‐
1358       turing  parentheses  are  numbered one. Thus, when the pattern matches,
1359       you can look at captured substring number  one,  whichever  alternative
1360       matched.  This  construct  is useful when you want to capture part, but
1361       not all, of one of a number of alternatives. Inside a (?| group, paren‐
1362       theses  are  numbered as usual, but the number is reset at the start of
1363       each branch. The numbers of any capturing parentheses that  follow  the
1364       subpattern  start after the highest number used in any branch. The fol‐
1365       lowing example is taken from the Perl documentation. The numbers under‐
1366       neath show in which buffer the captured content will be stored.
1367
1368         # before  ---------------branch-reset----------- after
1369         / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
1370         # 1            2         2  3        2     3     4
1371
1372       A  back  reference  to a numbered subpattern uses the most recent value
1373       that is set for that number by any subpattern.  The  following  pattern
1374       matches "abcabc" or "defdef":
1375
1376         /(?|(abc)|(def))\1/
1377
1378       In  contrast,  a subroutine call to a numbered subpattern always refers
1379       to the first one in the pattern with the given  number.  The  following
1380       pattern matches "abcabc" or "defabc":
1381
1382         /(?|(abc)|(def))(?1)/
1383
1384       If  a condition test for a subpattern's having matched refers to a non-
1385       unique number, the test is true if any of the subpatterns of that  num‐
1386       ber have matched.
1387
1388       An  alternative approach to using this "branch reset" feature is to use
1389       duplicate named subpatterns, as described in the next section.
1390

NAMED SUBPATTERNS

1392
1393       Identifying capturing parentheses by number is simple, but  it  can  be
1394       very  hard  to keep track of the numbers in complicated regular expres‐
1395       sions. Furthermore, if an  expression  is  modified,  the  numbers  may
1396       change.  To help with this difficulty, PCRE supports the naming of sub‐
1397       patterns. This feature was not added to Perl until release 5.10. Python
1398       had  the  feature earlier, and PCRE introduced it at release 4.0, using
1399       the Python syntax. PCRE now supports both the Perl and the Python  syn‐
1400       tax.  Perl  allows  identically  numbered subpatterns to have different
1401       names, but PCRE does not.
1402
1403       In PCRE, a subpattern can be named in one of three  ways:  (?<name>...)
1404       or  (?'name'...)  as in Perl, or (?P<name>...) as in Python. References
1405       to capturing parentheses from other parts of the pattern, such as  back
1406       references,  recursion,  and conditions, can be made by name as well as
1407       by number.
1408
1409       Names consist of up to 32 alphanumeric characters and underscores,  but
1410       must  start with a non-digit. Named capturing parentheses are still al‐
1411       located numbers as well as names, exactly as  if  the  names  were  not
1412       present.  The PCRE API provides function calls for extracting the name-
1413       to-number translation table from a compiled pattern. There  is  also  a
1414       convenience function for extracting a captured substring by name.
1415
1416       By  default, a name must be unique within a pattern, but it is possible
1417       to relax this constraint by setting the PCRE_DUPNAMES option at compile
1418       time.  (Duplicate  names are also always permitted for subpatterns with
1419       the same number, set up as described in the previous  section.)  Dupli‐
1420       cate  names  can  be useful for patterns where only one instance of the
1421       named parentheses can match. Suppose you want to match the  name  of  a
1422       weekday,  either as a 3-letter abbreviation or as the full name, and in
1423       both cases you want to extract the abbreviation. This pattern (ignoring
1424       the line breaks) does the job:
1425
1426         (?<DN>Mon|Fri|Sun)(?:day)?|
1427         (?<DN>Tue)(?:sday)?|
1428         (?<DN>Wed)(?:nesday)?|
1429         (?<DN>Thu)(?:rsday)?|
1430         (?<DN>Sat)(?:urday)?
1431
1432       There  are  five capturing substrings, but only one is ever set after a
1433       match.  (An alternative way of solving this problem is to use a "branch
1434       reset" subpattern, as described in the previous section.)
1435
1436       The  convenience  function  for extracting the data by name returns the
1437       substring for the first (and in this example, the only)  subpattern  of
1438       that  name  that  matched.  This saves searching to find which numbered
1439       subpattern it was.
1440
1441       If you make a back reference to  a  non-unique  named  subpattern  from
1442       elsewhere  in the pattern, the subpatterns to which the name refers are
1443       checked in the order in which they appear in the overall  pattern.  The
1444       first one that is set is used for the reference. For example, this pat‐
1445       tern matches both "foofoo" and "barbar" but not "foobar" or "barfoo":
1446
1447         (?:(?<n>foo)|(?<n>bar))\k<n>
1448
1449
1450       If you make a subroutine call to a non-unique named subpattern, the one
1451       that  corresponds  to  the first occurrence of the name is used. In the
1452       absence of duplicate numbers (see the previous section) this is the one
1453       with the lowest number.
1454
1455       If you use a named reference in a condition test (see the section about
1456       conditions below), either to check whether a subpattern has matched, or
1457       to  check for recursion, all subpatterns with the same name are tested.
1458       If the condition is true for any one of them, the overall condition  is
1459       true.  This is the same behaviour as testing by number. For further de‐
1460       tails of the interfaces for handling named subpatterns, see the pcreapi
1461       documentation.
1462
1463       Warning: You cannot use different names to distinguish between two sub‐
1464       patterns with the same number because PCRE uses only the  numbers  when
1465       matching. For this reason, an error is given at compile time if differ‐
1466       ent names are given to subpatterns with the same number.  However,  you
1467       can always give the same name to subpatterns with the same number, even
1468       when PCRE_DUPNAMES is not set.
1469

REPETITION

1471
1472       Repetition is specified by quantifiers, which can  follow  any  of  the
1473       following items:
1474
1475         a literal data character
1476         the dot metacharacter
1477         the \C escape sequence
1478         the \X escape sequence
1479         the \R escape sequence
1480         an escape such as \d or \pL that matches a single character
1481         a character class
1482         a back reference (see next section)
1483         a parenthesized subpattern (including assertions)
1484         a subroutine call to a subpattern (recursive or otherwise)
1485
1486       The  general repetition quantifier specifies a minimum and maximum num‐
1487       ber of permitted matches, by giving the two numbers in  curly  brackets
1488       (braces),  separated  by  a comma. The numbers must be less than 65536,
1489       and the first must be less than or equal to the second. For example:
1490
1491         z{2,4}
1492
1493       matches "zz", "zzz", or "zzzz". A closing brace on its  own  is  not  a
1494       special  character.  If  the second number is omitted, but the comma is
1495       present, there is no upper limit; if the second number  and  the  comma
1496       are  both omitted, the quantifier specifies an exact number of required
1497       matches. Thus
1498
1499         [aeiou]{3,}
1500
1501       matches at least 3 successive vowels, but may match many more, while
1502
1503         \d{8}
1504
1505       matches exactly 8 digits. An opening curly bracket that  appears  in  a
1506       position  where a quantifier is not allowed, or one that does not match
1507       the syntax of a quantifier, is taken as a literal character. For  exam‐
1508       ple, {,6} is not a quantifier, but a literal string of four characters.
1509
1510       In UTF modes, quantifiers apply to characters rather than to individual
1511       data units. Thus, for example, \x{100}{2} matches two characters,  each
1512       of which is represented by a two-byte sequence in a UTF-8 string. Simi‐
1513       larly, \X{3} matches three Unicode extended grapheme clusters, each  of
1514       which  may  be  several  data  units long (and they may be of different
1515       lengths).
1516
1517       The quantifier {0} is permitted, causing the expression to behave as if
1518       the previous item and the quantifier were not present. This may be use‐
1519       ful for subpatterns that are referenced as subroutines  from  elsewhere
1520       in the pattern (but see also the section entitled "Defining subpatterns
1521       for use by reference only" below). Items other  than  subpatterns  that
1522       have a {0} quantifier are omitted from the compiled pattern.
1523
1524       For  convenience, the three most common quantifiers have single-charac‐
1525       ter abbreviations:
1526
1527         *    is equivalent to {0,}
1528         +    is equivalent to {1,}
1529         ?    is equivalent to {0,1}
1530
1531       It is possible to construct infinite loops by  following  a  subpattern
1532       that can match no characters with a quantifier that has no upper limit,
1533       for example:
1534
1535         (a?)*
1536
1537       Earlier versions of Perl and PCRE used to give an error at compile time
1538       for  such  patterns. However, because there are cases where this can be
1539       useful, such patterns are now accepted, but if any  repetition  of  the
1540       subpattern  does in fact match no characters, the loop is forcibly bro‐
1541       ken.
1542
1543       By default, the quantifiers are "greedy", that is, they match  as  much
1544       as  possible  (up  to  the  maximum number of permitted times), without
1545       causing the rest of the pattern to fail. The classic example  of  where
1546       this gives problems is in trying to match comments in C programs. These
1547       appear between /* and */ and within the comment,  individual  *  and  /
1548       characters  may  appear. An attempt to match C comments by applying the
1549       pattern
1550
1551         /\*.*\*/
1552
1553       to the string
1554
1555         /* first comment */  not comment  /* second comment */
1556
1557       fails, because it matches the entire string owing to the greediness  of
1558       the .*  item.
1559
1560       However,  if  a quantifier is followed by a question mark, it ceases to
1561       be greedy, and instead matches the minimum number of times possible, so
1562       the pattern
1563
1564         /\*.*?\*/
1565
1566       does  the  right  thing with the C comments. The meaning of the various
1567       quantifiers is not otherwise changed,  just  the  preferred  number  of
1568       matches.   Do  not  confuse this use of question mark with its use as a
1569       quantifier in its own right. Because it has two uses, it can  sometimes
1570       appear doubled, as in
1571
1572         \d??\d
1573
1574       which matches one digit by preference, but can match two if that is the
1575       only way the rest of the pattern matches.
1576
1577       If the PCRE_UNGREEDY option is set (an option that is not available  in
1578       Perl),  the  quantifiers are not greedy by default, but individual ones
1579       can be made greedy by following them with a  question  mark.  In  other
1580       words, it inverts the default behaviour.
1581
1582       When  a  parenthesized  subpattern  is quantified with a minimum repeat
1583       count that is greater than 1 or with a limited maximum, more memory  is
1584       required  for  the  compiled  pattern, in proportion to the size of the
1585       minimum or maximum.
1586
1587       If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv‐
1588       alent  to  Perl's  /s) is set, thus allowing the dot to match newlines,
1589       the pattern is implicitly anchored, because whatever  follows  will  be
1590       tried  against every character position in the subject string, so there
1591       is no point in retrying the overall match at  any  position  after  the
1592       first.  PCRE  normally treats such a pattern as though it were preceded
1593       by \A.
1594
1595       In cases where it is known that the subject  string  contains  no  new‐
1596       lines,  it  is  worth setting PCRE_DOTALL in order to obtain this opti‐
1597       mization, or alternatively using ^ to indicate anchoring explicitly.
1598
1599       However, there are some cases where the optimization  cannot  be  used.
1600       When .*  is inside capturing parentheses that are the subject of a back
1601       reference elsewhere in the pattern, a match at the start may fail where
1602       a later one succeeds. Consider, for example:
1603
1604         (.*)abc\1
1605
1606       If  the subject is "xyz123abc123" the match point is the fourth charac‐
1607       ter. For this reason, such a pattern is not implicitly anchored.
1608
1609       Another case where implicit anchoring is not applied is when the  lead‐
1610       ing  .* is inside an atomic group. Once again, a match at the start may
1611       fail where a later one succeeds. Consider this pattern:
1612
1613         (?>.*?a)b
1614
1615       It matches "ab" in the subject "aab". The use of the backtracking  con‐
1616       trol verbs (*PRUNE) and (*SKIP) also disable this optimization.
1617
1618       When a capturing subpattern is repeated, the value captured is the sub‐
1619       string that matched the final iteration. For example, after
1620
1621         (tweedle[dume]{3}\s*)+
1622
1623       has matched "tweedledum tweedledee" the value of the captured substring
1624       is  "tweedledee".  However,  if there are nested capturing subpatterns,
1625       the corresponding captured values may have been set in previous  itera‐
1626       tions. For example, after
1627
1628         /(a|(b))+/
1629
1630       matches "aba" the value of the second captured substring is "b".
1631

ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS

1633
1634       With  both  maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
1635       repetition, failure of what follows normally causes the  repeated  item
1636       to  be  re-evaluated to see if a different number of repeats allows the
1637       rest of the pattern to match. Sometimes it is useful to  prevent  this,
1638       either  to  change the nature of the match, or to cause it fail earlier
1639       than it otherwise might, when the author of the pattern knows there  is
1640       no point in carrying on.
1641
1642       Consider,  for  example, the pattern \d+foo when applied to the subject
1643       line
1644
1645         123456bar
1646
1647       After matching all 6 digits and then failing to match "foo", the normal
1648       action  of  the matcher is to try again with only 5 digits matching the
1649       \d+ item, and then with  4,  and  so  on,  before  ultimately  failing.
1650       "Atomic  grouping"  (a  term taken from Jeffrey Friedl's book) provides
1651       the means for specifying that once a subpattern has matched, it is  not
1652       to be re-evaluated in this way.
1653
1654       If  we  use atomic grouping for the previous example, the matcher gives
1655       up immediately on failing to match "foo" the first time.  The  notation
1656       is a kind of special parenthesis, starting with (?> as in this example:
1657
1658         (?>\d+)foo
1659
1660       This  kind  of  parenthesis "locks up" the  part of the pattern it con‐
1661       tains once it has matched, and a failure further into  the  pattern  is
1662       prevented  from  backtracking into it. Backtracking past it to previous
1663       items, however, works as normal.
1664
1665       An alternative description is that a subpattern of  this  type  matches
1666       the  string  of  characters  that an identical standalone pattern would
1667       match, if anchored at the current point in the subject string.
1668
1669       Atomic grouping subpatterns are not capturing subpatterns. Simple cases
1670       such as the above example can be thought of as a maximizing repeat that
1671       must swallow everything it can. So, while both \d+ and  \d+?  are  pre‐
1672       pared  to  adjust  the number of digits they match in order to make the
1673       rest of the pattern match, (?>\d+) can only match an entire sequence of
1674       digits.
1675
1676       Atomic  groups in general can of course contain arbitrarily complicated
1677       subpatterns, and can be nested. However, when  the  subpattern  for  an
1678       atomic group is just a single repeated item, as in the example above, a
1679       simpler notation, called a "possessive quantifier" can  be  used.  This
1680       consists  of  an  additional  + character following a quantifier. Using
1681       this notation, the previous example can be rewritten as
1682
1683         \d++foo
1684
1685       Note that a possessive quantifier can be used with an entire group, for
1686       example:
1687
1688         (abc|xyz){2,3}+
1689
1690       Possessive  quantifiers  are always greedy; the setting of the PCRE_UN‐
1691       GREEDY option is ignored. They are a convenient notation for  the  sim‐
1692       pler  forms  of  atomic  group.  However, there is no difference in the
1693       meaning of a possessive quantifier and  the  equivalent  atomic  group,
1694       though  there  may  be a performance difference; possessive quantifiers
1695       should be slightly faster.
1696
1697       The possessive quantifier syntax is an extension to the Perl  5.8  syn‐
1698       tax.   Jeffrey  Friedl  originated the idea (and the name) in the first
1699       edition of his book. Mike McCloskey liked it, so implemented it when he
1700       built  Sun's Java package, and PCRE copied it from there. It ultimately
1701       found its way into Perl at release 5.10.
1702
1703       PCRE has an optimization that automatically "possessifies" certain sim‐
1704       ple  pattern  constructs.  For  example, the sequence A+B is treated as
1705       A++B because there is no point in backtracking into a sequence  of  A's
1706       when B must follow.
1707
1708       When  a  pattern  contains an unlimited repeat inside a subpattern that
1709       can itself be repeated an unlimited number of  times,  the  use  of  an
1710       atomic  group  is  the  only way to avoid some failing matches taking a
1711       very long time indeed. The pattern
1712
1713         (\D+|<\d+>)*[!?]
1714
1715       matches an unlimited number of substrings that either consist  of  non-
1716       digits,  or  digits  enclosed in <>, followed by either ! or ?. When it
1717       matches, it runs quickly. However, if it is applied to
1718
1719         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1720
1721       it takes a long time before reporting  failure.  This  is  because  the
1722       string  can be divided between the internal \D+ repeat and the external
1723       * repeat in a large number of ways, and all have to be tried. (The  ex‐
1724       ample uses [!?] rather than a single character at the end, because both
1725       PCRE and Perl have an optimization that allows for fast failure when  a
1726       single  character is used. They remember the last single character that
1727       is required for a match, and fail early if it is  not  present  in  the
1728       string.)  If  the  pattern  is changed so that it uses an atomic group,
1729       like this:
1730
1731         ((?>\D+)|<\d+>)*[!?]
1732
1733       sequences of non-digits cannot be broken, and failure happens quickly.
1734

BACK REFERENCES

1736
1737       Outside a character class, a backslash followed by a digit greater than
1738       0 (and possibly further digits) is a back reference to a capturing sub‐
1739       pattern earlier (that is, to its left) in the pattern,  provided  there
1740       have been that many previous capturing left parentheses.
1741
1742       However, if the decimal number following the backslash is less than 10,
1743       it is always taken as a back reference, and causes  an  error  only  if
1744       there  are  not that many capturing left parentheses in the entire pat‐
1745       tern. In other words, the parentheses that are referenced need  not  be
1746       to  the left of the reference for numbers less than 10. A "forward back
1747       reference" of this type can make sense when a  repetition  is  involved
1748       and  the  subpattern to the right has participated in an earlier itera‐
1749       tion.
1750
1751       It is not possible to have a numerical "forward back  reference"  to  a
1752       subpattern  whose  number is 10 or more using this syntax because a se‐
1753       quence such as \50 is interpreted as a character defined in octal.  See
1754       the subsection entitled "Non-printing characters" above for further de‐
1755       tails of the handling of digits following a backslash. There is no such
1756       problem  when  named parentheses are used. A back reference to any sub‐
1757       pattern is possible using named parentheses (see below).
1758
1759       Another way of avoiding the ambiguity inherent in  the  use  of  digits
1760       following  a  backslash  is  to use the \g escape sequence. This escape
1761       must be followed by an unsigned number or a negative number, optionally
1762       enclosed in braces. These examples are all identical:
1763
1764         (ring), \1
1765         (ring), \g1
1766         (ring), \g{1}
1767
1768       An  unsigned number specifies an absolute reference without the ambigu‐
1769       ity that is present in the older syntax. It is also useful when literal
1770       digits follow the reference. A negative number is a relative reference.
1771       Consider this example:
1772
1773         (abc(def)ghi)\g{-1}
1774
1775       The sequence \g{-1} is a reference to the most recently started captur‐
1776       ing subpattern before \g, that is, is it equivalent to \2 in this exam‐
1777       ple.  Similarly, \g{-2} would be equivalent to \1. The use of  relative
1778       references  can  be helpful in long patterns, and also in patterns that
1779       are created by  joining  together  fragments  that  contain  references
1780       within themselves.
1781
1782       A  back  reference matches whatever actually matched the capturing sub‐
1783       pattern in the current subject string, rather  than  anything  matching
1784       the subpattern itself (see "Subpatterns as subroutines" below for a way
1785       of doing that). So the pattern
1786
1787         (sens|respons)e and \1ibility
1788
1789       matches "sense and sensibility" and "response and responsibility",  but
1790       not  "sense and responsibility". If caseful matching is in force at the
1791       time of the back reference, the case of letters is relevant. For  exam‐
1792       ple,
1793
1794         ((?i)rah)\s+\1
1795
1796       matches  "rah  rah"  and  "RAH RAH", but not "RAH rah", even though the
1797       original capturing subpattern is matched caselessly.
1798
1799       There are several different ways of writing back  references  to  named
1800       subpatterns.  The  .NET syntax \k{name} and the Perl syntax \k<name> or
1801       \k'name' are supported, as is the Python syntax (?P=name). Perl  5.10's
1802       unified back reference syntax, in which \g can be used for both numeric
1803       and named references, is also supported. We could rewrite the above ex‐
1804       ample in any of the following ways:
1805
1806         (?<p1>(?i)rah)\s+\k<p1>
1807         (?'p1'(?i)rah)\s+\k{p1}
1808         (?P<p1>(?i)rah)\s+(?P=p1)
1809         (?<p1>(?i)rah)\s+\g{p1}
1810
1811       A  subpattern  that is referenced by name may appear in the pattern be‐
1812       fore or after the reference.
1813
1814       There may be more than one back reference to the same subpattern. If  a
1815       subpattern  has  not actually been used in a particular match, any back
1816       references to it always fail by default. For example, the pattern
1817
1818         (a|(bc))\2
1819
1820       always fails if it starts to match "a" rather than  "bc".  However,  if
1821       the PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back refer‐
1822       ence to an unset value matches an empty string.
1823
1824       Because there may be many capturing parentheses in a pattern, all  dig‐
1825       its  following a backslash are taken as part of a potential back refer‐
1826       ence number.  If the pattern continues with a digit character, some de‐
1827       limiter  must  be used to terminate the back reference. If the PCRE_EX‐
1828       TENDED option is set, this can be white space. Otherwise, the \g{  syn‐
1829       tax or an empty comment (see "Comments" below) can be used.
1830
1831   Recursive back references
1832
1833       A  back reference that occurs inside the parentheses to which it refers
1834       fails when the subpattern is first used, so, for example,  (a\1)  never
1835       matches.   However,  such references can be useful inside repeated sub‐
1836       patterns. For example, the pattern
1837
1838         (a|b\1)+
1839
1840       matches any number of "a"s and also "aba", "ababbaa" etc. At each iter‐
1841       ation  of  the  subpattern,  the  back  reference matches the character
1842       string corresponding to the previous iteration. In order  for  this  to
1843       work,  the  pattern must be such that the first iteration does not need
1844       to match the back reference. This can be done using alternation, as  in
1845       the example above, or by a quantifier with a minimum of zero.
1846
1847       Back  references of this type cause the group that they reference to be
1848       treated as an atomic group.  Once the whole group has been  matched,  a
1849       subsequent  matching  failure cannot cause backtracking into the middle
1850       of the group.
1851

ASSERTIONS

1853
1854       An assertion is a test on the characters  following  or  preceding  the
1855       current  matching  point that does not actually consume any characters.
1856       The simple assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are  de‐
1857       scribed above.
1858
1859       More  complicated  assertions  are  coded as subpatterns. There are two
1860       kinds: those that look ahead of the current  position  in  the  subject
1861       string,  and  those  that  look  behind  it. An assertion subpattern is
1862       matched in the normal way, except that it does not  cause  the  current
1863       matching position to be changed.
1864
1865       Assertion  subpatterns are not capturing subpatterns. If such an asser‐
1866       tion contains capturing subpatterns within it, these  are  counted  for
1867       the  purposes  of numbering the capturing subpatterns in the whole pat‐
1868       tern. However, substring capturing is carried out only for positive as‐
1869       sertions.  (Perl  sometimes, but not always, does do capturing in nega‐
1870       tive assertions.)
1871
1872       WARNING: If a positive assertion containing one or more capturing  sub‐
1873       patterns  succeeds,  but  failure  to match later in the pattern causes
1874       backtracking over this assertion, the captures within the assertion are
1875       reset only if no higher numbered captures are already set. This is, un‐
1876       fortunately, a fundamental limitation of  the  current  implementation,
1877       and  as PCRE1 is now in maintenance-only status, it is unlikely ever to
1878       change.
1879
1880       For compatibility with Perl, assertion  subpatterns  may  be  repeated;
1881       though  it  makes  no sense to assert the same thing several times, the
1882       side effect of capturing parentheses may  occasionally  be  useful.  In
1883       practice, there only three cases:
1884
1885       (1)  If  the  quantifier  is  {0}, the assertion is never obeyed during
1886       matching.  However, it may  contain  internal  capturing  parenthesized
1887       groups that are called from elsewhere via the subroutine mechanism.
1888
1889       (2)  If quantifier is {0,n} where n is greater than zero, it is treated
1890       as if it were {0,1}. At run time, the rest  of  the  pattern  match  is
1891       tried with and without the assertion, the order depending on the greed‐
1892       iness of the quantifier.
1893
1894       (3) If the minimum repetition is greater than zero, the  quantifier  is
1895       ignored.   The  assertion  is  obeyed just once when encountered during
1896       matching.
1897
1898   Lookahead assertions
1899
1900       Lookahead assertions start with (?= for positive assertions and (?! for
1901       negative assertions. For example,
1902
1903         \w+(?=;)
1904
1905       matches  a word followed by a semicolon, but does not include the semi‐
1906       colon in the match, and
1907
1908         foo(?!bar)
1909
1910       matches any occurrence of "foo" that is not  followed  by  "bar".  Note
1911       that the apparently similar pattern
1912
1913         (?!foo)bar
1914
1915       does  not  find  an  occurrence  of "bar" that is preceded by something
1916       other than "foo"; it finds any occurrence of "bar" whatsoever,  because
1917       the assertion (?!foo) is always true when the next three characters are
1918       "bar". A lookbehind assertion is needed to achieve the other effect.
1919
1920       If you want to force a matching failure at some point in a pattern, the
1921       most  convenient  way to do it is with (?!) because an empty string al‐
1922       ways matches, so an assertion that requires there not to  be  an  empty
1923       string must always fail.  The backtracking control verb (*FAIL) or (*F)
1924       is a synonym for (?!).
1925
1926   Lookbehind assertions
1927
1928       Lookbehind assertions start with (?<= for positive assertions and  (?<!
1929       for negative assertions. For example,
1930
1931         (?<!foo)bar
1932
1933       does  find  an  occurrence  of "bar" that is not preceded by "foo". The
1934       contents of a lookbehind assertion are restricted  such  that  all  the
1935       strings it matches must have a fixed length. However, if there are sev‐
1936       eral top-level alternatives, they do not all  have  to  have  the  same
1937       fixed length. Thus
1938
1939         (?<=bullock|donkey)
1940
1941       is permitted, but
1942
1943         (?<!dogs?|cats?)
1944
1945       causes  an  error at compile time. Branches that match different length
1946       strings are permitted only at the top level of a lookbehind  assertion.
1947       This is an extension compared with Perl, which requires all branches to
1948       match the same length of string. An assertion such as
1949
1950         (?<=ab(c|de))
1951
1952       is not permitted, because its single top-level  branch  can  match  two
1953       different lengths, but it is acceptable to PCRE if rewritten to use two
1954       top-level branches:
1955
1956         (?<=abc|abde)
1957
1958       In some cases, the escape sequence \K (see above) can be  used  instead
1959       of a lookbehind assertion to get round the fixed-length restriction.
1960
1961       The  implementation  of lookbehind assertions is, for each alternative,
1962       to temporarily move the current position back by the fixed  length  and
1963       then try to match. If there are insufficient characters before the cur‐
1964       rent position, the assertion fails.
1965
1966       In a UTF mode, PCRE does not allow the \C escape (which matches a  sin‐
1967       gle  data  unit even in a UTF mode) to appear in lookbehind assertions,
1968       because it makes it impossible to calculate the length of  the  lookbe‐
1969       hind.  The \X and \R escapes, which can match different numbers of data
1970       units, are also not permitted.
1971
1972       "Subroutine" calls (see below) such as (?2) or (?&X) are  permitted  in
1973       lookbehinds,  as  long as the subpattern matches a fixed-length string.
1974       Recursion, however, is not supported.
1975
1976       Possessive quantifiers can be used in conjunction with  lookbehind  as‐
1977       sertions  to  specify efficient matching of fixed-length strings at the
1978       end of subject strings. Consider a simple pattern such as
1979
1980         abcd$
1981
1982       when applied to a long string that does  not  match.  Because  matching
1983       proceeds from left to right, PCRE will look for each "a" in the subject
1984       and then see if what follows matches the rest of the  pattern.  If  the
1985       pattern is specified as
1986
1987         ^.*abcd$
1988
1989       the  initial .* matches the entire string at first, but when this fails
1990       (because there is no following "a"), it backtracks to match all but the
1991       last  character,  then all but the last two characters, and so on. Once
1992       again the search for "a" covers the entire string, from right to  left,
1993       so we are no better off. However, if the pattern is written as
1994
1995         ^.*+(?<=abcd)
1996
1997       there  can  be  no backtracking for the .*+ item; it can match only the
1998       entire string. The subsequent lookbehind assertion does a  single  test
1999       on  the last four characters. If it fails, the match fails immediately.
2000       For long strings, this approach makes a significant difference  to  the
2001       processing time.
2002
2003   Using multiple assertions
2004
2005       Several assertions (of any sort) may occur in succession. For example,
2006
2007         (?<=\d{3})(?<!999)foo
2008
2009       matches  "foo" preceded by three digits that are not "999". Notice that
2010       each of the assertions is applied independently at the  same  point  in
2011       the  subject  string.  First  there  is a check that the previous three
2012       characters are all digits, and then there is  a  check  that  the  same
2013       three characters are not "999".  This pattern does not match "foo" pre‐
2014       ceded by six characters, the first of which are  digits  and  the  last
2015       three  of  which  are not "999". For example, it doesn't match "123abc‐
2016       foo". A pattern to do that is
2017
2018         (?<=\d{3}...)(?<!999)foo
2019
2020       This time the first assertion looks at the  preceding  six  characters,
2021       checking that the first three are digits, and then the second assertion
2022       checks that the preceding three characters are not "999".
2023
2024       Assertions can be nested in any combination. For example,
2025
2026         (?<=(?<!foo)bar)baz
2027
2028       matches an occurrence of "baz" that is preceded by "bar" which in  turn
2029       is not preceded by "foo", while
2030
2031         (?<=\d{3}(?!999)...)foo
2032
2033       is  another pattern that matches "foo" preceded by three digits and any
2034       three characters that are not "999".
2035

CONDITIONAL SUBPATTERNS

2037
2038       It is possible to cause the matching process to obey a subpattern  con‐
2039       ditionally  or to choose between two alternative subpatterns, depending
2040       on the result of an assertion, or whether a specific capturing  subpat‐
2041       tern  has  already  been matched. The two possible forms of conditional
2042       subpattern are:
2043
2044         (?(condition)yes-pattern)
2045         (?(condition)yes-pattern|no-pattern)
2046
2047       If the condition is satisfied, the yes-pattern is used;  otherwise  the
2048       no-pattern  (if  present)  is used. If there are more than two alterna‐
2049       tives in the subpattern, a compile-time error occurs. Each of  the  two
2050       alternatives may itself contain nested subpatterns of any form, includ‐
2051       ing conditional subpatterns; the restriction to  two  alternatives  ap‐
2052       plies  only  at the level of the condition. This pattern fragment is an
2053       example where the alternatives are complex:
2054
2055         (?(1) (A|B|C) | (D | (?(2)E|F) | E) )
2056
2057
2058       There are four kinds of condition: references  to  subpatterns,  refer‐
2059       ences to recursion, a pseudo-condition called DEFINE, and assertions.
2060
2061   Checking for a used subpattern by number
2062
2063       If  the  text between the parentheses consists of a sequence of digits,
2064       the condition is true if a capturing subpattern of that number has pre‐
2065       viously  matched.  If  there is more than one capturing subpattern with
2066       the same number (see the earlier  section  about  duplicate  subpattern
2067       numbers),  the condition is true if any of them have matched. An alter‐
2068       native notation is to precede the digits with a plus or minus sign.  In
2069       this  case, the subpattern number is relative rather than absolute. The
2070       most recently opened parentheses can be referenced by (?(-1), the  next
2071       most  recent  by (?(-2), and so on. Inside loops it can also make sense
2072       to refer to subsequent groups. The next parentheses to be opened can be
2073       referenced  as (?(+1), and so on. (The value zero in any of these forms
2074       is not used; it provokes a compile-time error.)
2075
2076       Consider the following pattern, which  contains  non-significant  white
2077       space to make it more readable (assume the PCRE_EXTENDED option) and to
2078       divide it into three parts for ease of discussion:
2079
2080         ( \( )?    [^()]+    (?(1) \) )
2081
2082       The first part matches an optional opening  parenthesis,  and  if  that
2083       character is present, sets it as the first captured substring. The sec‐
2084       ond part matches one or more characters that are not  parentheses.  The
2085       third  part  is  a conditional subpattern that tests whether or not the
2086       first set of parentheses matched. If they  did,  that  is,  if  subject
2087       started  with an opening parenthesis, the condition is true, and so the
2088       yes-pattern is executed and a closing parenthesis is  required.  Other‐
2089       wise,  since no-pattern is not present, the subpattern matches nothing.
2090       In other words, this pattern matches a sequence of non-parentheses, op‐
2091       tionally enclosed in parentheses.
2092
2093       If  you  were  embedding  this pattern in a larger one, you could use a
2094       relative reference:
2095
2096         ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...
2097
2098       This makes the fragment independent of the parentheses  in  the  larger
2099       pattern.
2100
2101   Checking for a used subpattern by name
2102
2103       Perl  uses  the  syntax  (?(<name>)...) or (?('name')...) to test for a
2104       used subpattern by name. For compatibility  with  earlier  versions  of
2105       PCRE,  which  had this facility before Perl, the syntax (?(name)...) is
2106       also recognized.
2107
2108       Rewriting the above example to use a named subpattern gives this:
2109
2110         (?<OPEN> \( )?    [^()]+    (?(<OPEN>) \) )
2111
2112       If the name used in a condition of this kind is a duplicate,  the  test
2113       is  applied to all subpatterns of the same name, and is true if any one
2114       of them has matched.
2115
2116   Checking for pattern recursion
2117
2118       If the condition is the string (R), and there is no subpattern with the
2119       name  R, the condition is true if a recursive call to the whole pattern
2120       or any subpattern has been made. If digits or a name preceded by amper‐
2121       sand follow the letter R, for example:
2122
2123         (?(R3)...) or (?(R&name)...)
2124
2125       the condition is true if the most recent recursion is into a subpattern
2126       whose number or name is given. This condition does not check the entire
2127       recursion  stack. If the name used in a condition of this kind is a du‐
2128       plicate, the test is applied to all subpatterns of the same  name,  and
2129       is true if any one of them is the most recent recursion.
2130
2131       At  "top  level",  all  these recursion test conditions are false.  The
2132       syntax for recursive patterns is described below.
2133
2134   Defining subpatterns for use by reference only
2135
2136       If the condition is the string (DEFINE), and  there  is  no  subpattern
2137       with  the  name  DEFINE,  the  condition is always false. In this case,
2138       there may be only one alternative  in  the  subpattern.  It  is  always
2139       skipped  if  control reaches this point in the pattern; the idea of DE‐
2140       FINE is that it can be used to define subroutines that  can  be  refer‐
2141       enced  from elsewhere. (The use of subroutines is described below.) For
2142       example, a pattern to match an IPv4 address  such  as  "192.168.23.245"
2143       could be written like this (ignore white space and line breaks):
2144
2145         (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
2146         \b (?&byte) (\.(?&byte)){3} \b
2147
2148       The  first part of the pattern is a DEFINE group inside which a another
2149       group named "byte" is defined. This matches an individual component  of
2150       an  IPv4  address  (a number less than 256). When matching takes place,
2151       this part of the pattern is skipped because DEFINE acts  like  a  false
2152       condition.  The  rest of the pattern uses references to the named group
2153       to match the four dot-separated components of an IPv4 address,  insist‐
2154       ing on a word boundary at each end.
2155
2156   Assertion conditions
2157
2158       If  the condition is not in any of the above formats, it must be an as‐
2159       sertion.  This may be a positive or negative  lookahead  or  lookbehind
2160       assertion.  Consider  this  pattern,  again  containing non-significant
2161       white space, and with the two alternatives on the second line:
2162
2163         (?(?=[^a-z]*[a-z])
2164         \d{2}-[a-z]{3}-\d{2}  |  \d{2}-\d{2}-\d{2} )
2165
2166       The condition is a positive lookahead assertion  that  matches  an  op‐
2167       tional sequence of non-letters followed by a letter. In other words, it
2168       tests for the presence of at least one letter in the subject. If a let‐
2169       ter  is  found,  the  subject is matched against the first alternative;
2170       otherwise it is  matched  against  the  second.  This  pattern  matches
2171       strings  in  one  of the two forms dd-aaa-dd or dd-dd-dd, where aaa are
2172       letters and dd are digits.
2173

COMMENTS

2175
2176       There are two ways of including comments in patterns that are processed
2177       by PCRE. In both cases, the start of the comment must not be in a char‐
2178       acter class, nor in the middle of any other sequence of related charac‐
2179       ters  such  as  (?: or a subpattern name or number. The characters that
2180       make up a comment play no part in the pattern matching.
2181
2182       The sequence (?# marks the start of a comment that continues up to  the
2183       next  closing parenthesis. Nested parentheses are not permitted. If the
2184       PCRE_EXTENDED option is set, an unescaped # character also introduces a
2185       comment,  which  in  this  case continues to immediately after the next
2186       newline character or character sequence in the pattern.  Which  charac‐
2187       ters are interpreted as newlines is controlled by the options passed to
2188       a compiling function or by a special sequence at the start of the  pat‐
2189       tern, as described in the section entitled "Newline conventions" above.
2190       Note that the end of this type of comment is a literal newline sequence
2191       in  the pattern; escape sequences that happen to represent a newline do
2192       not count. For example, consider this  pattern  when  PCRE_EXTENDED  is
2193       set, and the default newline convention is in force:
2194
2195         abc #comment \n still comment
2196
2197       On  encountering  the  # character, pcre_compile() skips along, looking
2198       for a newline in the pattern. The sequence \n is still literal at  this
2199       stage,  so  it does not terminate the comment. Only an actual character
2200       with the code value 0x0a (the default newline) does so.
2201

RECURSIVE PATTERNS

2203
2204       Consider the problem of matching a string in parentheses, allowing  for
2205       unlimited  nested  parentheses.  Without the use of recursion, the best
2206       that can be done is to use a pattern that  matches  up  to  some  fixed
2207       depth  of  nesting.  It  is not possible to handle an arbitrary nesting
2208       depth.
2209
2210       For some time, Perl has provided a facility that allows regular expres‐
2211       sions  to recurse (amongst other things). It does this by interpolating
2212       Perl code in the expression at run time, and the code can refer to  the
2213       expression itself. A Perl pattern using code interpolation to solve the
2214       parentheses problem can be created like this:
2215
2216         $re = qr{\( (?: (?>[^()]+) | (?p{$re}) )* \)}x;
2217
2218       The (?p{...}) item interpolates Perl code at run time, and in this case
2219       refers recursively to the pattern in which it appears.
2220
2221       Obviously, PCRE cannot support the interpolation of Perl code. Instead,
2222       it supports special syntax for recursion of  the  entire  pattern,  and
2223       also  for  individual  subpattern  recursion. After its introduction in
2224       PCRE and Python, this kind of  recursion  was  subsequently  introduced
2225       into Perl at release 5.10.
2226
2227       A  special  item  that consists of (? followed by a number greater than
2228       zero and a closing parenthesis is a recursive subroutine  call  of  the
2229       subpattern  of  the  given  number, provided that it occurs inside that
2230       subpattern. (If not, it is a non-recursive subroutine  call,  which  is
2231       described  in the next section.) The special item (?R) or (?0) is a re‐
2232       cursive call of the entire regular expression.
2233
2234       This PCRE pattern solves the nested  parentheses  problem  (assume  the
2235       PCRE_EXTENDED option is set so that white space is ignored):
2236
2237         \( ( [^()]++ | (?R) )* \)
2238
2239       First  it matches an opening parenthesis. Then it matches any number of
2240       substrings which can either be a sequence of non-parentheses, or a  re‐
2241       cursive match of the pattern itself (that is, a correctly parenthesized
2242       substring).  Finally there is a closing parenthesis. Note the use of  a
2243       possessive  quantifier  to  avoid  backtracking  into sequences of non-
2244       parentheses.
2245
2246       If this were part of a larger pattern, you would not  want  to  recurse
2247       the entire pattern, so instead you could use this:
2248
2249         ( \( ( [^()]++ | (?1) )* \) )
2250
2251       We  have  put the pattern into parentheses, and caused the recursion to
2252       refer to them instead of the whole pattern.
2253
2254       In a larger pattern,  keeping  track  of  parenthesis  numbers  can  be
2255       tricky.  This is made easier by the use of relative references. Instead
2256       of (?1) in the pattern above you can write (?-2) to refer to the second
2257       most  recently  opened  parentheses  preceding  the recursion. In other
2258       words, a negative number counts capturing  parentheses  leftwards  from
2259       the point at which it is encountered.
2260
2261       It  is  also  possible  to refer to subsequently opened parentheses, by
2262       writing references such as (?+2). However, these  cannot  be  recursive
2263       because  the  reference  is  not inside the parentheses that are refer‐
2264       enced. They are always non-recursive subroutine calls, as described  in
2265       the next section.
2266
2267       An  alternative  approach is to use named parentheses instead. The Perl
2268       syntax for this is (?&name); PCRE's earlier syntax  (?P>name)  is  also
2269       supported. We could rewrite the above example as follows:
2270
2271         (?<pn> \( ( [^()]++ | (?&pn) )* \) )
2272
2273       If  there  is more than one subpattern with the same name, the earliest
2274       one is used.
2275
2276       This particular example pattern that we have been looking  at  contains
2277       nested unlimited repeats, and so the use of a possessive quantifier for
2278       matching strings of non-parentheses is important when applying the pat‐
2279       tern  to  strings  that do not match. For example, when this pattern is
2280       applied to
2281
2282         (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
2283
2284       it yields "no match" quickly. However, if a  possessive  quantifier  is
2285       not  used, the match runs for a very long time indeed because there are
2286       so many different ways the + and * repeats can carve  up  the  subject,
2287       and all have to be tested before failure can be reported.
2288
2289       At  the  end  of a match, the values of capturing parentheses are those
2290       from the outermost level. If you want to obtain intermediate values,  a
2291       callout  function can be used (see below and the pcrecallout documenta‐
2292       tion). If the pattern above is matched against
2293
2294         (ab(cd)ef)
2295
2296       the value for the inner capturing parentheses  (numbered  2)  is  "ef",
2297       which  is the last value taken on at the top level. If a capturing sub‐
2298       pattern is not matched at the top level, its final  captured  value  is
2299       unset,  even  if  it was (temporarily) set at a deeper level during the
2300       matching process.
2301
2302       If there are more than 15 capturing parentheses in a pattern, PCRE  has
2303       to  obtain extra memory to store data during a recursion, which it does
2304       by using pcre_malloc, freeing it via pcre_free afterwards. If no memory
2305       can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.
2306
2307       Do  not  confuse  the (?R) item with the condition (R), which tests for
2308       recursion.  Consider this pattern, which matches text in  angle  brack‐
2309       ets,  allowing for arbitrary nesting. Only digits are allowed in nested
2310       brackets (that is, when recursing), whereas any characters are  permit‐
2311       ted at the outer level.
2312
2313         < (?: (?(R) \d++  | [^<>]*+) | (?R)) * >
2314
2315       In  this  pattern, (?(R) is the start of a conditional subpattern, with
2316       two different alternatives for the recursive and  non-recursive  cases.
2317       The (?R) item is the actual recursive call.
2318
2319   Differences in recursion processing between PCRE and Perl
2320
2321       Recursion  processing  in PCRE differs from Perl in two important ways.
2322       In PCRE (like Python, but unlike Perl), a recursive subpattern call  is
2323       always treated as an atomic group. That is, once it has matched some of
2324       the subject string, it is never re-entered, even if it contains untried
2325       alternatives  and  there  is a subsequent matching failure. This can be
2326       illustrated by the following pattern, which purports to match a  palin‐
2327       dromic  string  that contains an odd number of characters (for example,
2328       "a", "aba", "abcba", "abcdcba"):
2329
2330         ^(.|(.)(?1)\2)$
2331
2332       The idea is that it either matches a single character, or two identical
2333       characters  surrounding  a sub-palindrome. In Perl, this pattern works;
2334       in PCRE it does not if the pattern is  longer  than  three  characters.
2335       Consider the subject string "abcba":
2336
2337       At  the  top level, the first character is matched, but as it is not at
2338       the end of the string, the first alternative fails; the second alterna‐
2339       tive is taken and the recursion kicks in. The recursive call to subpat‐
2340       tern 1 successfully matches the next character ("b").  (Note  that  the
2341       beginning and end of line tests are not part of the recursion).
2342
2343       Back  at  the top level, the next character ("c") is compared with what
2344       subpattern 2 matched, which was "a". This fails. Because the  recursion
2345       is  treated  as  an atomic group, there are now no backtracking points,
2346       and so the entire match fails. (Perl is able, at this point, to  re-en‐
2347       ter the recursion and try the second alternative.) However, if the pat‐
2348       tern is written with the alternatives in the other  order,  things  are
2349       different:
2350
2351         ^((.)(?1)\2|.)$
2352
2353       This  time,  the recursing alternative is tried first, and continues to
2354       recurse until it runs out of characters, at which point  the  recursion
2355       fails.  But  this  time  we  do  have another alternative to try at the
2356       higher level. That is the big difference: in the previous case the  re‐
2357       maining  alternative  is at a deeper recursion level, which PCRE cannot
2358       use.
2359
2360       To change the pattern so that it matches all palindromic  strings,  not
2361       just  those  with an odd number of characters, it is tempting to change
2362       the pattern to this:
2363
2364         ^((.)(?1)\2|.?)$
2365
2366       Again, this works in Perl, but not in PCRE, and for  the  same  reason.
2367       When  a  deeper  recursion has matched a single character, it cannot be
2368       entered again in order to match an empty string.  The  solution  is  to
2369       separate  the two cases, and write out the odd and even cases as alter‐
2370       natives at the higher level:
2371
2372         ^(?:((.)(?1)\2|)|((.)(?3)\4|.))
2373
2374       If you want to match typical palindromic phrases, the  pattern  has  to
2375       ignore all non-word characters, which can be done like this:
2376
2377         ^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$
2378
2379       If run with the PCRE_CASELESS option, this pattern matches phrases such
2380       as "A man, a plan, a canal: Panama!" and it works well in both PCRE and
2381       Perl.  Note the use of the possessive quantifier *+ to avoid backtrack‐
2382       ing into sequences of non-word characters. Without this, PCRE  takes  a
2383       great  deal  longer  (ten  times or more) to match typical phrases, and
2384       Perl takes so long that you think it has gone into a loop.
2385
2386       WARNING: The palindrome-matching patterns above work only if  the  sub‐
2387       ject  string  does not start with a palindrome that is shorter than the
2388       entire string.  For example, although "abcba" is correctly matched,  if
2389       the  subject  is "ababa", PCRE finds the palindrome "aba" at the start,
2390       then fails at top level because the end of the string does not  follow.
2391       Once  again, it cannot jump back into the recursion to try other alter‐
2392       natives, so the entire match fails.
2393
2394       The second way in which PCRE and Perl differ in  their  recursion  pro‐
2395       cessing  is in the handling of captured values. In Perl, when a subpat‐
2396       tern is called recursively or as a subpattern (see the  next  section),
2397       it  has  no  access to any values that were captured outside the recur‐
2398       sion, whereas in PCRE these values can  be  referenced.  Consider  this
2399       pattern:
2400
2401         ^(.)(\1|a(?2))
2402
2403       In  PCRE,  this  pattern matches "bab". The first capturing parentheses
2404       match "b", then in the second group, when the back reference  \1  fails
2405       to  match "b", the second alternative matches "a" and then recurses. In
2406       the recursion, \1 does now match "b" and so the whole  match  succeeds.
2407       In  Perl,  the pattern fails to match because inside the recursive call
2408       \1 cannot access the externally set value.
2409

SUBPATTERNS AS SUBROUTINES

2411
2412       If the syntax for a recursive subpattern call (either by number  or  by
2413       name)  is  used outside the parentheses to which it refers, it operates
2414       like a subroutine in a programming language. The called subpattern  may
2415       be  defined  before or after the reference. A numbered reference can be
2416       absolute or relative, as in these examples:
2417
2418         (...(absolute)...)...(?2)...
2419         (...(relative)...)...(?-1)...
2420         (...(?+1)...(relative)...
2421
2422       An earlier example pointed out that the pattern
2423
2424         (sens|respons)e and \1ibility
2425
2426       matches "sense and sensibility" and "response and responsibility",  but
2427       not "sense and responsibility". If instead the pattern
2428
2429         (sens|respons)e and (?1)ibility
2430
2431       is  used, it does match "sense and responsibility" as well as the other
2432       two strings. Another example is  given  in  the  discussion  of  DEFINE
2433       above.
2434
2435       All  subroutine  calls, whether recursive or not, are always treated as
2436       atomic groups. That is, once a subroutine has matched some of the  sub‐
2437       ject string, it is never re-entered, even if it contains untried alter‐
2438       natives and there is  a  subsequent  matching  failure.  Any  capturing
2439       parentheses  that  are  set  during the subroutine call revert to their
2440       previous values afterwards.
2441
2442       Processing options such as case-independence are fixed when  a  subpat‐
2443       tern  is defined, so if it is used as a subroutine, such options cannot
2444       be changed for different calls. For example, consider this pattern:
2445
2446         (abc)(?i:(?-1))
2447
2448       It matches "abcabc". It does not match "abcABC" because the  change  of
2449       processing option does not affect the called subpattern.
2450

ONIGURUMA SUBROUTINE SYNTAX

2452
2453       For  compatibility with Oniguruma, the non-Perl syntax \g followed by a
2454       name or a number enclosed either in angle brackets or single quotes, is
2455       an  alternative  syntax  for  referencing a subpattern as a subroutine,
2456       possibly recursively. Here are two of the examples used above,  rewrit‐
2457       ten using this syntax:
2458
2459         (?<pn> \( ( (?>[^()]+) | \g<pn> )* \) )
2460         (sens|respons)e and \g'1'ibility
2461
2462       PCRE  supports  an extension to Oniguruma: if a number is preceded by a
2463       plus or a minus sign it is taken as a relative reference. For example:
2464
2465         (abc)(?i:\g<-1>)
2466
2467       Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are  not
2468       synonymous.  The former is a back reference; the latter is a subroutine
2469       call.
2470

CALLOUTS

2472
2473       Perl has a feature whereby using the sequence (?{...}) causes arbitrary
2474       Perl  code to be obeyed in the middle of matching a regular expression.
2475       This makes it possible, amongst other things, to extract different sub‐
2476       strings that match the same pair of parentheses when there is a repeti‐
2477       tion.
2478
2479       PCRE provides a similar feature, but of course it cannot obey arbitrary
2480       Perl code. The feature is called "callout". The caller of PCRE provides
2481       an external function by putting its entry point in the global  variable
2482       pcre_callout  (8-bit  library) or pcre[16|32]_callout (16-bit or 32-bit
2483       library).  By default, this variable contains NULL, which disables  all
2484       calling out.
2485
2486       Within a regular expression, (?C) indicates the points at which the ex‐
2487       ternal function is to be called. If  you  want  to  identify  different
2488       callout  points, you can put a number less than 256 after the letter C.
2489       The default value is zero.  For example, this pattern has  two  callout
2490       points:
2491
2492         (?C1)abc(?C2)def
2493
2494       If  the PCRE_AUTO_CALLOUT flag is passed to a compiling function, call‐
2495       outs are automatically installed before each item in the pattern.  They
2496       are  all  numbered  255. If there is a conditional group in the pattern
2497       whose condition is an assertion, an additional callout is inserted just
2498       before the condition. An explicit callout may also be set at this posi‐
2499       tion, as in this example:
2500
2501         (?(?C9)(?=a)abc|def)
2502
2503       Note that this applies only to assertion conditions, not to other types
2504       of condition.
2505
2506       During  matching, when PCRE reaches a callout point, the external func‐
2507       tion is called. It is provided with the number of the callout, the  po‐
2508       sition  in  the  pattern,  and, optionally, one item of data originally
2509       supplied by the caller of the matching function. The  callout  function
2510       may cause matching to proceed, to backtrack, or to fail altogether.
2511
2512       By  default,  PCRE implements a number of optimizations at compile time
2513       and matching time, and one side-effect is that sometimes  callouts  are
2514       skipped.  If  you need all possible callouts to happen, you need to set
2515       options that disable the relevant optimizations. More  details,  and  a
2516       complete  description  of  the  interface  to the callout function, are
2517       given in the pcrecallout documentation.
2518

BACKTRACKING CONTROL

2520
2521       Perl 5.10 introduced a number of "Special Backtracking Control  Verbs",
2522       which  are  still  described in the Perl documentation as "experimental
2523       and subject to change or removal in a future version of Perl". It  goes
2524       on  to  say:  "Their  usage in production code should be noted to avoid
2525       problems during upgrades." The same remarks apply to the PCRE  features
2526       described in this section.
2527
2528       The  new verbs make use of what was previously invalid syntax: an open‐
2529       ing parenthesis followed by an asterisk. They are generally of the form
2530       (*VERB)  or  (*VERB:NAME). Some may take either form, possibly behaving
2531       differently depending on whether or not a name is present.  A  name  is
2532       any sequence of characters that does not include a closing parenthesis.
2533       The maximum length of name is 255 in the 8-bit library and 65535 in the
2534       16-bit  and  32-bit  libraries.  If  the name is empty, that is, if the
2535       closing parenthesis immediately follows the colon, the effect is as  if
2536       the  colon  were  not  there.  Any number of these verbs may occur in a
2537       pattern.
2538
2539       Since these verbs are specifically related  to  backtracking,  most  of
2540       them  can  be  used only when the pattern is to be matched using one of
2541       the traditional matching functions, because these  use  a  backtracking
2542       algorithm.  With the exception of (*FAIL), which behaves like a failing
2543       negative assertion, the backtracking control verbs cause  an  error  if
2544       encountered by a DFA matching function.
2545
2546       The  behaviour  of  these  verbs in repeated groups, assertions, and in
2547       subpatterns called as subroutines (whether or not recursively) is docu‐
2548       mented below.
2549
2550   Optimizations that affect backtracking verbs
2551
2552       PCRE  contains some optimizations that are used to speed up matching by
2553       running some checks at the start of each match attempt. For example, it
2554       may  know  the minimum length of matching subject, or that a particular
2555       character must be present. When one of these optimizations bypasses the
2556       running  of  a  match,  any  included  backtracking  verbs will not, of
2557       course, be processed. You can suppress the start-of-match optimizations
2558       by  setting  the  PCRE_NO_START_OPTIMIZE  option when calling pcre_com‐
2559       pile() or pcre_exec(), or by starting the pattern with (*NO_START_OPT).
2560       There is more discussion of this option in the section entitled "Option
2561       bits for pcre_exec()" in the pcreapi documentation.
2562
2563       Experiments with Perl suggest that it too  has  similar  optimizations,
2564       sometimes leading to anomalous results.
2565
2566   Verbs that act immediately
2567
2568       The  following  verbs act as soon as they are encountered. They may not
2569       be followed by a name.
2570
2571          (*ACCEPT)
2572
2573       This verb causes the match to end successfully, skipping the  remainder
2574       of  the pattern. However, when it is inside a subpattern that is called
2575       as a subroutine, only that subpattern is ended  successfully.  Matching
2576       then continues at the outer level. If (*ACCEPT) in triggered in a posi‐
2577       tive assertion, the assertion succeeds; in a  negative  assertion,  the
2578       assertion fails.
2579
2580       If  (*ACCEPT)  is inside capturing parentheses, the data so far is cap‐
2581       tured. For example:
2582
2583         A((?:A|B(*ACCEPT)|C)D)
2584
2585       This matches "AB", "AAD", or "ACD"; when it matches "AB", "B"  is  cap‐
2586       tured by the outer parentheses.
2587
2588         (*FAIL) or (*F)
2589
2590       This  verb causes a matching failure, forcing backtracking to occur. It
2591       is equivalent to (?!) but easier to read. The Perl documentation  notes
2592       that  it  is  probably  useful only when combined with (?{}) or (??{}).
2593       Those are, of course, Perl features that are not present in  PCRE.  The
2594       nearest  equivalent is the callout feature, as for example in this pat‐
2595       tern:
2596
2597         a+(?C)(*FAIL)
2598
2599       A match with the string "aaaa" always fails, but the callout  is  taken
2600       before each backtrack happens (in this example, 10 times).
2601
2602   Recording which path was taken
2603
2604       There  is  one  verb whose main purpose is to track how a match was ar‐
2605       rived at, though it also has a secondary use in  conjunction  with  ad‐
2606       vancing the match starting point (see (*SKIP) below).
2607
2608         (*MARK:NAME) or (*:NAME)
2609
2610       A  name  is  always  required  with this verb. There may be as many in‐
2611       stances of (*MARK) as you like in a pattern, and  their  names  do  not
2612       have to be unique.
2613
2614       When  a  match succeeds, the name of the last-encountered (*MARK:NAME),
2615       (*PRUNE:NAME), or (*THEN:NAME) on the matching path is passed  back  to
2616       the  caller  as  described  in  the  section  entitled  "Extra data for
2617       pcre_exec()" in the  pcreapi  documentation.  Here  is  an  example  of
2618       pcretest  output, where the /K modifier requests the retrieval and out‐
2619       putting of (*MARK) data:
2620
2621           re> /X(*MARK:A)Y|X(*MARK:B)Z/K
2622         data> XY
2623          0: XY
2624         MK: A
2625         XZ
2626          0: XZ
2627         MK: B
2628
2629       The (*MARK) name is tagged with "MK:" in this output, and in this exam‐
2630       ple  it indicates which of the two alternatives matched. This is a more
2631       efficient way of obtaining this information than putting each  alterna‐
2632       tive in its own capturing parentheses.
2633
2634       If  a  verb  with a name is encountered in a positive assertion that is
2635       true, the name is recorded and passed back if it  is  the  last-encoun‐
2636       tered. This does not happen for negative assertions or failing positive
2637       assertions.
2638
2639       After a partial match or a failed match, the last encountered  name  in
2640       the entire match process is returned. For example:
2641
2642           re> /X(*MARK:A)Y|X(*MARK:B)Z/K
2643         data> XP
2644         No match, mark = B
2645
2646       Note  that  in  this  unanchored  example the mark is retained from the
2647       match attempt that started at the letter "X" in the subject. Subsequent
2648       match attempts starting at "P" and then with an empty string do not get
2649       as far as the (*MARK) item, but nevertheless do not reset it.
2650
2651       If you are interested in  (*MARK)  values  after  failed  matches,  you
2652       should  probably  set  the PCRE_NO_START_OPTIMIZE option (see above) to
2653       ensure that the match is always attempted.
2654
2655   Verbs that act after backtracking
2656
2657       The following verbs do nothing when they are encountered. Matching con‐
2658       tinues  with what follows, but if there is no subsequent match, causing
2659       a backtrack to the verb, a failure is  forced.  That  is,  backtracking
2660       cannot  pass  to the left of the verb. However, when one of these verbs
2661       appears inside an atomic group or an assertion that is true, its effect
2662       is  confined  to  that  group, because once the group has been matched,
2663       there is never any backtracking into it. In this situation,  backtrack‐
2664       ing  can  "jump  back" to the left of the entire atomic group or asser‐
2665       tion. (Remember also, as stated above, that this localization also  ap‐
2666       plies in subroutine calls.)
2667
2668       These  verbs  differ  in exactly what kind of failure occurs when back‐
2669       tracking reaches them. The behaviour described below  is  what  happens
2670       when  the  verb is not in a subroutine or an assertion. Subsequent sec‐
2671       tions cover these special cases.
2672
2673         (*COMMIT)
2674
2675       This verb, which may not be followed by a name, causes the whole  match
2676       to fail outright if there is a later matching failure that causes back‐
2677       tracking to reach it. Even if the pattern is unanchored, no further at‐
2678       tempts  to  find a match by advancing the starting point take place. If
2679       (*COMMIT) is the only backtracking verb that is  encountered,  once  it
2680       has been passed pcre_exec() is committed to finding a match at the cur‐
2681       rent starting point, or not at all. For example:
2682
2683         a+(*COMMIT)b
2684
2685       This matches "xxaab" but not "aacaab". It can be thought of as  a  kind
2686       of dynamic anchor, or "I've started, so I must finish." The name of the
2687       most recently passed (*MARK) in the path is passed back when  (*COMMIT)
2688       forces a match failure.
2689
2690       If  there  is more than one backtracking verb in a pattern, a different
2691       one that follows (*COMMIT) may be triggered first,  so  merely  passing
2692       (*COMMIT) during a match does not always guarantee that a match must be
2693       at this starting point.
2694
2695       Note that (*COMMIT) at the start of a pattern is not the same as an an‐
2696       chor,  unless  PCRE's  start-of-match  optimizations are turned off, as
2697       shown in this output from pcretest:
2698
2699           re> /(*COMMIT)abc/
2700         data> xyzabc
2701          0: abc
2702         data> xyzabc\Y
2703         No match
2704
2705       For this pattern, PCRE knows that any match must start with "a", so the
2706       optimization skips along the subject to "a" before applying the pattern
2707       to the first set of data. The match attempt then succeeds. In the  sec‐
2708       ond  set of data, the escape sequence \Y is interpreted by the pcretest
2709       program. It causes the PCRE_NO_START_OPTIMIZE option  to  be  set  when
2710       pcre_exec() is called.  This disables the optimization that skips along
2711       to the first character. The pattern is now applied starting at "x", and
2712       so  the  (*COMMIT)  causes  the  match to fail without trying any other
2713       starting points.
2714
2715         (*PRUNE) or (*PRUNE:NAME)
2716
2717       This verb causes the match to fail at the current starting position  in
2718       the subject if there is a later matching failure that causes backtrack‐
2719       ing to reach it. If the pattern is unanchored, the  normal  "bumpalong"
2720       advance  to  the next starting character then happens. Backtracking can
2721       occur as usual to the left of (*PRUNE), before it is reached,  or  when
2722       matching  to  the  right  of  (*PRUNE), but if there is no match to the
2723       right, backtracking cannot cross (*PRUNE). In simple cases, the use  of
2724       (*PRUNE)  is just an alternative to an atomic group or possessive quan‐
2725       tifier, but there are some uses of (*PRUNE) that cannot be expressed in
2726       any  other  way. In an anchored pattern (*PRUNE) has the same effect as
2727       (*COMMIT).
2728
2729       The   behaviour   of   (*PRUNE:NAME)   is   the   not   the   same   as
2730       (*MARK:NAME)(*PRUNE).   It is like (*MARK:NAME) in that the name is re‐
2731       membered for passing back to the caller. However, (*SKIP:NAME) searches
2732       only for names set with (*MARK).
2733
2734         (*SKIP)
2735
2736       This  verb, when given without a name, is like (*PRUNE), except that if
2737       the pattern is unanchored, the "bumpalong" advance is not to  the  next
2738       character, but to the position in the subject where (*SKIP) was encoun‐
2739       tered. (*SKIP) signifies that whatever text was matched leading  up  to
2740       it cannot be part of a successful match. Consider:
2741
2742         a+(*SKIP)b
2743
2744       If  the  subject  is  "aaaac...",  after  the first match attempt fails
2745       (starting at the first character in the  string),  the  starting  point
2746       skips on to start the next attempt at "c". Note that a possessive quan‐
2747       tifier does not have the same effect as this example; although it would
2748       suppress  backtracking  during  the first match attempt, the second at‐
2749       tempt would start at the second character instead  of  skipping  on  to
2750       "c".
2751
2752         (*SKIP:NAME)
2753
2754       When (*SKIP) has an associated name, its behaviour is modified. When it
2755       is triggered, the previous path through the pattern is searched for the
2756       most  recent  (*MARK)  that  has  the  same  name. If one is found, the
2757       "bumpalong" advance is to the subject position that corresponds to that
2758       (*MARK) instead of to where (*SKIP) was encountered. If no (*MARK) with
2759       a matching name is found, the (*SKIP) is ignored.
2760
2761       Note that (*SKIP:NAME) searches only for names set by (*MARK:NAME).  It
2762       ignores names that are set by (*PRUNE:NAME) or (*THEN:NAME).
2763
2764         (*THEN) or (*THEN:NAME)
2765
2766       This  verb  causes  a skip to the next innermost alternative when back‐
2767       tracking reaches it. That  is,  it  cancels  any  further  backtracking
2768       within  the  current  alternative.  Its name comes from the observation
2769       that it can be used for a pattern-based if-then-else block:
2770
2771         ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
2772
2773       If the COND1 pattern matches, FOO is tried (and possibly further  items
2774       after  the  end  of the group if FOO succeeds); on failure, the matcher
2775       skips to the second alternative and tries COND2,  without  backtracking
2776       into  COND1.  If that succeeds and BAR fails, COND3 is tried. If subse‐
2777       quently BAZ fails, there are no more alternatives, so there is a  back‐
2778       track  to  whatever came before the entire group. If (*THEN) is not in‐
2779       side an alternation, it acts like (*PRUNE).
2780
2781       The   behaviour   of   (*THEN:NAME)   is   the   not   the   same    as
2782       (*MARK:NAME)(*THEN).   It  is like (*MARK:NAME) in that the name is re‐
2783       membered for passing back to the caller. However, (*SKIP:NAME) searches
2784       only for names set with (*MARK).
2785
2786       A  subpattern that does not contain a | character is just a part of the
2787       enclosing alternative; it is not a nested alternation with only one al‐
2788       ternative.  The  effect  of (*THEN) extends beyond such a subpattern to
2789       the enclosing alternative. Consider this pattern, where A, B, etc.  are
2790       complex  pattern fragments that do not contain any | characters at this
2791       level:
2792
2793         A (B(*THEN)C) | D
2794
2795       If A and B are matched, but there is a failure in C, matching does  not
2796       backtrack into A; instead it moves to the next alternative, that is, D.
2797       However, if the subpattern containing (*THEN) is given an  alternative,
2798       it behaves differently:
2799
2800         A (B(*THEN)C | (*FAIL)) | D
2801
2802       The  effect of (*THEN) is now confined to the inner subpattern. After a
2803       failure in C, matching moves to (*FAIL), which causes the whole subpat‐
2804       tern  to  fail  because  there are no more alternatives to try. In this
2805       case, matching does now backtrack into A.
2806
2807       Note that a conditional subpattern is not considered as having two  al‐
2808       ternatives,  because only one is ever used. In other words, the | char‐
2809       acter in a conditional subpattern has  a  different  meaning.  Ignoring
2810       white space, consider:
2811
2812         ^.*? (?(?=a) a | b(*THEN)c )
2813
2814       If the subject is "ba", this pattern does not match. Because .*? is un‐
2815       greedy, it initially matches zero characters. The condition (?=a)  then
2816       fails,  the  character  "b"  is matched, but "c" is not. At this point,
2817       matching does not backtrack to .*? as might perhaps  be  expected  from
2818       the  presence of the | character. The conditional subpattern is part of
2819       the single alternative that comprises the whole  pattern,  and  so  the
2820       match  fails.  (If there was a backtrack into .*?, allowing it to match
2821       "b", the match would succeed.)
2822
2823       The verbs just described provide four different "strengths" of  control
2824       when subsequent matching fails. (*THEN) is the weakest, carrying on the
2825       match at the next alternative. (*PRUNE) comes next, failing  the  match
2826       at  the  current starting position, but allowing an advance to the next
2827       character (for an unanchored pattern). (*SKIP) is similar, except  that
2828       the advance may be more than one character. (*COMMIT) is the strongest,
2829       causing the entire match to fail.
2830
2831   More than one backtracking verb
2832
2833       If more than one backtracking verb is present in  a  pattern,  the  one
2834       that  is  backtracked  onto first acts. For example, consider this pat‐
2835       tern, where A, B, etc. are complex pattern fragments:
2836
2837         (A(*COMMIT)B(*THEN)C|ABD)
2838
2839       If A matches but B fails, the backtrack to (*COMMIT) causes the  entire
2840       match to fail. However, if A and B match, but C fails, the backtrack to
2841       (*THEN) causes the next alternative (ABD) to be tried.  This  behaviour
2842       is  consistent,  but is not always the same as Perl's. It means that if
2843       two or more backtracking verbs appear in succession, all the  the  last
2844       of them has no effect. Consider this example:
2845
2846         ...(*COMMIT)(*PRUNE)...
2847
2848       If there is a matching failure to the right, backtracking onto (*PRUNE)
2849       causes it to be triggered, and its action is taken. There can never  be
2850       a backtrack onto (*COMMIT).
2851
2852   Backtracking verbs in repeated groups
2853
2854       PCRE  differs  from  Perl  in its handling of backtracking verbs in re‐
2855       peated groups. For example, consider:
2856
2857         /(a(*COMMIT)b)+ac/
2858
2859       If the subject is "abac", Perl matches,  but  PCRE  fails  because  the
2860       (*COMMIT) in the second repeat of the group acts.
2861
2862   Backtracking verbs in assertions
2863
2864       (*FAIL)  in  an assertion has its normal effect: it forces an immediate
2865       backtrack.
2866
2867       (*ACCEPT) in a positive assertion causes the assertion to succeed with‐
2868       out  any  further processing. In a negative assertion, (*ACCEPT) causes
2869       the assertion to fail without any further processing.
2870
2871       The other backtracking verbs are not treated specially if  they  appear
2872       in  a  positive assertion. In particular, (*THEN) skips to the next al‐
2873       ternative in the  innermost  enclosing  group  that  has  alternations,
2874       whether or not this is within the assertion.
2875
2876       Negative  assertions  are,  however, different, in order to ensure that
2877       changing a positive assertion into a negative assertion changes its re‐
2878       sult.  Backtracking into (*COMMIT), (*SKIP), or (*PRUNE) causes a nega‐
2879       tive assertion to be true, without considering any further  alternative
2880       branches in the assertion.  Backtracking into (*THEN) causes it to skip
2881       to the next enclosing alternative within the assertion (the normal  be‐
2882       haviour),  but  if  the  assertion  does  not have such an alternative,
2883       (*THEN) behaves like (*PRUNE).
2884
2885   Backtracking verbs in subroutines
2886
2887       These behaviours occur whether or not the subpattern is  called  recur‐
2888       sively.  Perl's treatment of subroutines is different in some cases.
2889
2890       (*FAIL)  in  a subpattern called as a subroutine has its normal effect:
2891       it forces an immediate backtrack.
2892
2893       (*ACCEPT) in a subpattern called as a subroutine causes the  subroutine
2894       match  to succeed without any further processing. Matching then contin‐
2895       ues after the subroutine call.
2896
2897       (*COMMIT), (*SKIP), and (*PRUNE) in a subpattern called as a subroutine
2898       cause the subroutine match to fail.
2899
2900       (*THEN)  skips to the next alternative in the innermost enclosing group
2901       within the subpattern that has alternatives. If there is no such  group
2902       within the subpattern, (*THEN) causes the subroutine match to fail.
2903

SEE ALSO

2905
2906       pcreapi(3),  pcrecallout(3),  pcrematching(3),  pcresyntax(3), pcre(3),
2907       pcre16(3), pcre32(3).
2908

AUTHOR

2910
2911       Philip Hazel
2912       University Computing Service
2913       Cambridge CB2 3QH, England.
2914

REVISION

2916
2917       Last updated: 23 October 2016
2918       Copyright (c) 1997-2016 University of Cambridge.
2919
2920
2921
2922PCRE 8.40                       23 October 2016                 PCREPATTERN(3)
Impressum