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

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

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

revision 1411 by ph10, Tue Nov 19 15:36:57 2013 UTC revision 1412 by ph10, Sun Dec 15 17:01:46 2013 UTC
# Line 5162  BACKSLASH Line 5162  BACKSLASH
5162         release 8.34. The default \s characters are now HT  (9),  LF  (10),  VT         release 8.34. The default \s characters are now HT  (9),  LF  (10),  VT
5163         (11),  FF  (12),  CR  (13),  and space (32), which are defined as white         (11),  FF  (12),  CR  (13),  and space (32), which are defined as white
5164         space in the "C" locale. This list may vary if locale-specific matching         space in the "C" locale. This list may vary if locale-specific matching
5165         is  taking  place;  in  particular,  in  some locales the "non-breaking         is  taking place. For example, in some locales the "non-breaking space"
5166         space" character (\xA0) is recognized as white space.         character (\xA0) is recognized as white space, and  in  others  the  VT
5167           character is not.
5168         A "word" character is an underscore or any character that is  a  letter  
5169         or  digit.   By  default,  the definition of letters and digits is con-         A  "word"  character is an underscore or any character that is a letter
5170         trolled by PCRE's low-valued character tables, and may vary if  locale-         or digit.  By default, the definition of letters  and  digits  is  con-
5171         specific  matching is taking place (see "Locale support" in the pcreapi         trolled  by PCRE's low-valued character tables, and may vary if locale-
5172         page). For example, in a French locale such  as  "fr_FR"  in  Unix-like         specific matching is taking place (see "Locale support" in the  pcreapi
5173         systems,  or "french" in Windows, some character codes greater than 127         page).  For  example,  in  a French locale such as "fr_FR" in Unix-like
5174         are used for accented letters, and these are then matched  by  \w.  The         systems, or "french" in Windows, some character codes greater than  127
5175           are  used  for  accented letters, and these are then matched by \w. The
5176         use of locales with Unicode is discouraged.         use of locales with Unicode is discouraged.
5177    
5178         By  default,  characters  whose  code points are greater than 127 never         By default, characters whose code points are  greater  than  127  never
5179         match \d, \s, or \w, and always match \D, \S, and \W, although this may         match \d, \s, or \w, and always match \D, \S, and \W, although this may
5180         vary  for characters in the range 128-255 when locale-specific matching         vary for characters in the range 128-255 when locale-specific  matching
5181         is happening.  These escape sequences retain  their  original  meanings         is  happening.   These  escape sequences retain their original meanings
5182         from  before  Unicode support was available, mainly for efficiency rea-         from before Unicode support was available, mainly for  efficiency  rea-
5183         sons. If PCRE is  compiled  with  Unicode  property  support,  and  the         sons.  If  PCRE  is  compiled  with  Unicode  property support, and the
5184         PCRE_UCP  option is set, the behaviour is changed so that Unicode prop-         PCRE_UCP option is set, the behaviour is changed so that Unicode  prop-
5185         erties are used to determine character types, as follows:         erties are used to determine character types, as follows:
5186    
5187           \d  any character that matches \p{Nd} (decimal digit)           \d  any character that matches \p{Nd} (decimal digit)
5188           \s  any character that matches \p{Z} or \h or \v           \s  any character that matches \p{Z} or \h or \v
5189           \w  any character that matches \p{L} or \p{N}, plus underscore           \w  any character that matches \p{L} or \p{N}, plus underscore
5190    
5191         The upper case escapes match the inverse sets of characters. Note  that         The  upper case escapes match the inverse sets of characters. Note that
5192         \d  matches  only decimal digits, whereas \w matches any Unicode digit,         \d matches only decimal digits, whereas \w matches any  Unicode  digit,
5193         as well as any Unicode letter, and underscore. Note also that  PCRE_UCP         as  well as any Unicode letter, and underscore. Note also that PCRE_UCP
5194         affects  \b,  and  \B  because  they are defined in terms of \w and \W.         affects \b, and \B because they are defined in  terms  of  \w  and  \W.
5195         Matching these sequences is noticeably slower when PCRE_UCP is set.         Matching these sequences is noticeably slower when PCRE_UCP is set.
5196    
5197         The sequences \h, \H, \v, and \V are features that were added  to  Perl         The  sequences  \h, \H, \v, and \V are features that were added to Perl
5198         at  release  5.10. In contrast to the other sequences, which match only         at release 5.10. In contrast to the other sequences, which  match  only
5199         ASCII characters by default, these  always  match  certain  high-valued         ASCII  characters  by  default,  these always match certain high-valued
5200         code points, whether or not PCRE_UCP is set. The horizontal space char-         code points, whether or not PCRE_UCP is set. The horizontal space char-
5201         acters are:         acters are:
5202    
# Line 5234  BACKSLASH Line 5235  BACKSLASH
5235    
5236     Newline sequences     Newline sequences
5237    
5238         Outside  a  character class, by default, the escape sequence \R matches         Outside a character class, by default, the escape sequence  \R  matches
5239         any Unicode newline sequence. In 8-bit non-UTF-8 mode \R is  equivalent         any  Unicode newline sequence. In 8-bit non-UTF-8 mode \R is equivalent
5240         to the following:         to the following:
5241    
5242           (?>\r\n|\n|\x0b|\f|\r|\x85)           (?>\r\n|\n|\x0b|\f|\r|\x85)
5243    
5244         This  is  an  example  of an "atomic group", details of which are given         This is an example of an "atomic group", details  of  which  are  given
5245         below.  This particular group matches either the two-character sequence         below.  This particular group matches either the two-character sequence
5246         CR  followed  by  LF,  or  one  of  the single characters LF (linefeed,         CR followed by LF, or  one  of  the  single  characters  LF  (linefeed,
5247         U+000A), VT (vertical tab, U+000B), FF (form feed,  U+000C),  CR  (car-         U+000A),  VT  (vertical  tab, U+000B), FF (form feed, U+000C), CR (car-
5248         riage  return,  U+000D),  or NEL (next line, U+0085). The two-character         riage return, U+000D), or NEL (next line,  U+0085).  The  two-character
5249         sequence is treated as a single unit that cannot be split.         sequence is treated as a single unit that cannot be split.
5250    
5251         In other modes, two additional characters whose codepoints are  greater         In  other modes, two additional characters whose codepoints are greater
5252         than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa-         than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa-
5253         rator, U+2029).  Unicode character property support is not  needed  for         rator,  U+2029).   Unicode character property support is not needed for
5254         these characters to be recognized.         these characters to be recognized.
5255    
5256         It is possible to restrict \R to match only CR, LF, or CRLF (instead of         It is possible to restrict \R to match only CR, LF, or CRLF (instead of
5257         the complete set  of  Unicode  line  endings)  by  setting  the  option         the  complete  set  of  Unicode  line  endings)  by  setting the option
5258         PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched.         PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched.
5259         (BSR is an abbrevation for "backslash R".) This can be made the default         (BSR is an abbrevation for "backslash R".) This can be made the default
5260         when  PCRE  is  built;  if this is the case, the other behaviour can be         when PCRE is built; if this is the case, the  other  behaviour  can  be
5261         requested via the PCRE_BSR_UNICODE option.   It  is  also  possible  to         requested  via  the  PCRE_BSR_UNICODE  option.   It is also possible to
5262         specify  these  settings  by  starting a pattern string with one of the         specify these settings by starting a pattern string  with  one  of  the
5263         following sequences:         following sequences:
5264    
5265           (*BSR_ANYCRLF)   CR, LF, or CRLF only           (*BSR_ANYCRLF)   CR, LF, or CRLF only
5266           (*BSR_UNICODE)   any Unicode newline sequence           (*BSR_UNICODE)   any Unicode newline sequence
5267    
5268         These override the default and the options given to the compiling func-         These override the default and the options given to the compiling func-
5269         tion,  but  they  can  themselves  be  overridden by options given to a         tion, but they can themselves be  overridden  by  options  given  to  a
5270         matching function. Note that these  special  settings,  which  are  not         matching  function.  Note  that  these  special settings, which are not
5271         Perl-compatible,  are  recognized  only at the very start of a pattern,         Perl-compatible, are recognized only at the very start  of  a  pattern,
5272         and that they must be in upper case.  If  more  than  one  of  them  is         and  that  they  must  be  in  upper  case. If more than one of them is
5273         present,  the  last  one is used. They can be combined with a change of         present, the last one is used. They can be combined with  a  change  of
5274         newline convention; for example, a pattern can start with:         newline convention; for example, a pattern can start with:
5275    
5276           (*ANY)(*BSR_ANYCRLF)           (*ANY)(*BSR_ANYCRLF)
5277    
5278         They can also be combined with the (*UTF8), (*UTF16), (*UTF32),  (*UTF)         They  can also be combined with the (*UTF8), (*UTF16), (*UTF32), (*UTF)
5279         or (*UCP) special sequences. Inside a character class, \R is treated as         or (*UCP) special sequences. Inside a character class, \R is treated as
5280         an unrecognized escape sequence, and  so  matches  the  letter  "R"  by         an  unrecognized  escape  sequence,  and  so  matches the letter "R" by
5281         default, but causes an error if PCRE_EXTRA is set.         default, but causes an error if PCRE_EXTRA is set.
5282    
5283     Unicode character properties     Unicode character properties
5284    
5285         When PCRE is built with Unicode character property support, three addi-         When PCRE is built with Unicode character property support, three addi-
5286         tional escape sequences that match characters with specific  properties         tional  escape sequences that match characters with specific properties
5287         are  available.   When  in 8-bit non-UTF-8 mode, these sequences are of         are available.  When in 8-bit non-UTF-8 mode, these  sequences  are  of
5288         course limited to testing characters whose  codepoints  are  less  than         course  limited  to  testing  characters whose codepoints are less than
5289         256, but they do work in this mode.  The extra escape sequences are:         256, but they do work in this mode.  The extra escape sequences are:
5290    
5291           \p{xx}   a character with the xx property           \p{xx}   a character with the xx property
5292           \P{xx}   a character without the xx property           \P{xx}   a character without the xx property
5293           \X       a Unicode extended grapheme cluster           \X       a Unicode extended grapheme cluster
5294    
5295         The  property  names represented by xx above are limited to the Unicode         The property names represented by xx above are limited to  the  Unicode
5296         script names, the general category properties, "Any", which matches any         script names, the general category properties, "Any", which matches any
5297         character   (including  newline),  and  some  special  PCRE  properties         character  (including  newline),  and  some  special  PCRE   properties
5298         (described in the next section).  Other Perl properties such as  "InMu-         (described  in the next section).  Other Perl properties such as "InMu-
5299         sicalSymbols"  are  not  currently supported by PCRE. Note that \P{Any}         sicalSymbols" are not currently supported by PCRE.  Note  that  \P{Any}
5300         does not match any characters, so always causes a match failure.         does not match any characters, so always causes a match failure.
5301    
5302         Sets of Unicode characters are defined as belonging to certain scripts.         Sets of Unicode characters are defined as belonging to certain scripts.
5303         A  character from one of these sets can be matched using a script name.         A character from one of these sets can be matched using a script  name.
5304         For example:         For example:
5305    
5306           \p{Greek}           \p{Greek}
5307           \P{Han}           \P{Han}
5308    
5309         Those that are not part of an identified script are lumped together  as         Those  that are not part of an identified script are lumped together as
5310         "Common". The current list of scripts is:         "Common". The current list of scripts is:
5311    
5312         Arabic,  Armenian,  Avestan, Balinese, Bamum, Batak, Bengali, Bopomofo,         Arabic, Armenian, Avestan, Balinese, Bamum, Batak,  Bengali,  Bopomofo,
5313         Brahmi, Braille, Buginese, Buhid, Canadian_Aboriginal, Carian,  Chakma,         Brahmi,  Braille, Buginese, Buhid, Canadian_Aboriginal, Carian, Chakma,
5314         Cham,  Cherokee, Common, Coptic, Cuneiform, Cypriot, Cyrillic, Deseret,         Cham, Cherokee, Common, Coptic, Cuneiform, Cypriot, Cyrillic,  Deseret,
5315         Devanagari,  Egyptian_Hieroglyphs,  Ethiopic,   Georgian,   Glagolitic,         Devanagari,   Egyptian_Hieroglyphs,   Ethiopic,  Georgian,  Glagolitic,
5316         Gothic,  Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hira-         Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew,  Hira-
5317         gana,  Imperial_Aramaic,  Inherited,  Inscriptional_Pahlavi,   Inscrip-         gana,   Imperial_Aramaic,  Inherited,  Inscriptional_Pahlavi,  Inscrip-
5318         tional_Parthian,   Javanese,   Kaithi,   Kannada,  Katakana,  Kayah_Li,         tional_Parthian,  Javanese,  Kaithi,   Kannada,   Katakana,   Kayah_Li,
5319         Kharoshthi, Khmer, Lao, Latin, Lepcha, Limbu, Linear_B,  Lisu,  Lycian,         Kharoshthi,  Khmer,  Lao, Latin, Lepcha, Limbu, Linear_B, Lisu, Lycian,
5320         Lydian,    Malayalam,    Mandaic,    Meetei_Mayek,    Meroitic_Cursive,         Lydian,    Malayalam,    Mandaic,    Meetei_Mayek,    Meroitic_Cursive,
5321         Meroitic_Hieroglyphs,  Miao,  Mongolian,  Myanmar,  New_Tai_Lue,   Nko,         Meroitic_Hieroglyphs,   Miao,  Mongolian,  Myanmar,  New_Tai_Lue,  Nko,
5322         Ogham,    Old_Italic,   Old_Persian,   Old_South_Arabian,   Old_Turkic,         Ogham,   Old_Italic,   Old_Persian,   Old_South_Arabian,    Old_Turkic,
5323         Ol_Chiki, Oriya, Osmanya, Phags_Pa, Phoenician, Rejang, Runic,  Samari-         Ol_Chiki,  Oriya, Osmanya, Phags_Pa, Phoenician, Rejang, Runic, Samari-
5324         tan,  Saurashtra,  Sharada,  Shavian, Sinhala, Sora_Sompeng, Sundanese,         tan, Saurashtra, Sharada, Shavian,  Sinhala,  Sora_Sompeng,  Sundanese,
5325         Syloti_Nagri, Syriac, Tagalog, Tagbanwa,  Tai_Le,  Tai_Tham,  Tai_Viet,         Syloti_Nagri,  Syriac,  Tagalog,  Tagbanwa, Tai_Le, Tai_Tham, Tai_Viet,
5326         Takri,  Tamil,  Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Vai,         Takri, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh,  Ugaritic,  Vai,
5327         Yi.         Yi.
5328    
5329         Each character has exactly one Unicode general category property, spec-         Each character has exactly one Unicode general category property, spec-
5330         ified  by a two-letter abbreviation. For compatibility with Perl, nega-         ified by a two-letter abbreviation. For compatibility with Perl,  nega-
5331         tion can be specified by including a  circumflex  between  the  opening         tion  can  be  specified  by including a circumflex between the opening
5332         brace  and  the  property  name.  For  example,  \p{^Lu} is the same as         brace and the property name.  For  example,  \p{^Lu}  is  the  same  as
5333         \P{Lu}.         \P{Lu}.
5334    
5335         If only one letter is specified with \p or \P, it includes all the gen-         If only one letter is specified with \p or \P, it includes all the gen-
5336         eral  category properties that start with that letter. In this case, in         eral category properties that start with that letter. In this case,  in
5337         the absence of negation, the curly brackets in the escape sequence  are         the  absence of negation, the curly brackets in the escape sequence are
5338         optional; these two examples have the same effect:         optional; these two examples have the same effect:
5339    
5340           \p{L}           \p{L}
# Line 5385  BACKSLASH Line 5386  BACKSLASH
5386           Zp    Paragraph separator           Zp    Paragraph separator
5387           Zs    Space separator           Zs    Space separator
5388    
5389         The  special property L& is also supported: it matches a character that         The special property L& is also supported: it matches a character  that
5390         has the Lu, Ll, or Lt property, in other words, a letter  that  is  not         has  the  Lu,  Ll, or Lt property, in other words, a letter that is not
5391         classified as a modifier or "other".         classified as a modifier or "other".
5392    
5393         The  Cs  (Surrogate)  property  applies only to characters in the range         The Cs (Surrogate) property applies only to  characters  in  the  range
5394         U+D800 to U+DFFF. Such characters are not valid in Unicode strings  and         U+D800  to U+DFFF. Such characters are not valid in Unicode strings and
5395         so  cannot  be  tested  by  PCRE, unless UTF validity checking has been         so cannot be tested by PCRE, unless  UTF  validity  checking  has  been
5396         turned    off    (see    the    discussion    of    PCRE_NO_UTF8_CHECK,         turned    off    (see    the    discussion    of    PCRE_NO_UTF8_CHECK,
5397         PCRE_NO_UTF16_CHECK  and PCRE_NO_UTF32_CHECK in the pcreapi page). Perl         PCRE_NO_UTF16_CHECK and PCRE_NO_UTF32_CHECK in the pcreapi page).  Perl
5398         does not support the Cs property.         does not support the Cs property.
5399    
5400         The long synonyms for  property  names  that  Perl  supports  (such  as         The  long  synonyms  for  property  names  that  Perl supports (such as
5401         \p{Letter})  are  not  supported by PCRE, nor is it permitted to prefix         \p{Letter}) are not supported by PCRE, nor is it  permitted  to  prefix
5402         any of these properties with "Is".         any of these properties with "Is".
5403    
5404         No character that is in the Unicode table has the Cn (unassigned) prop-         No character that is in the Unicode table has the Cn (unassigned) prop-
5405         erty.  Instead, this property is assumed for any code point that is not         erty.  Instead, this property is assumed for any code point that is not
5406         in the Unicode table.         in the Unicode table.
5407    
5408         Specifying caseless matching does not affect  these  escape  sequences.         Specifying  caseless  matching  does not affect these escape sequences.
5409         For  example,  \p{Lu}  always  matches only upper case letters. This is         For example, \p{Lu} always matches only upper  case  letters.  This  is
5410         different from the behaviour of current versions of Perl.         different from the behaviour of current versions of Perl.
5411    
5412         Matching characters by Unicode property is not fast, because  PCRE  has         Matching  characters  by Unicode property is not fast, because PCRE has
5413         to  do  a  multistage table lookup in order to find a character's prop-         to do a multistage table lookup in order to find  a  character's  prop-
5414         erty. That is why the traditional escape sequences such as \d and \w do         erty. That is why the traditional escape sequences such as \d and \w do
5415         not use Unicode properties in PCRE by default, though you can make them         not use Unicode properties in PCRE by default, though you can make them
5416         do so by setting the PCRE_UCP option or by starting  the  pattern  with         do  so  by  setting the PCRE_UCP option or by starting the pattern with
5417         (*UCP).         (*UCP).
5418    
5419     Extended grapheme clusters     Extended grapheme clusters
5420    
5421         The  \X  escape  matches  any number of Unicode characters that form an         The \X escape matches any number of Unicode  characters  that  form  an
5422         "extended grapheme cluster", and treats the sequence as an atomic group         "extended grapheme cluster", and treats the sequence as an atomic group
5423         (see  below).   Up  to and including release 8.31, PCRE matched an ear-         (see below).  Up to and including release 8.31, PCRE  matched  an  ear-
5424         lier, simpler definition that was equivalent to         lier, simpler definition that was equivalent to
5425    
5426           (?>\PM\pM*)           (?>\PM\pM*)
5427    
5428         That is, it matched a character without the "mark"  property,  followed         That  is,  it matched a character without the "mark" property, followed
5429         by  zero  or  more characters with the "mark" property. Characters with         by zero or more characters with the "mark"  property.  Characters  with
5430         the "mark" property are typically non-spacing accents that  affect  the         the  "mark"  property are typically non-spacing accents that affect the
5431         preceding character.         preceding character.
5432    
5433         This  simple definition was extended in Unicode to include more compli-         This simple definition was extended in Unicode to include more  compli-
5434         cated kinds of composite character by giving each character a  grapheme         cated  kinds of composite character by giving each character a grapheme
5435         breaking  property,  and  creating  rules  that use these properties to         breaking property, and creating rules  that  use  these  properties  to
5436         define the boundaries of extended grapheme  clusters.  In  releases  of         define  the  boundaries  of  extended grapheme clusters. In releases of
5437         PCRE later than 8.31, \X matches one of these clusters.         PCRE later than 8.31, \X matches one of these clusters.
5438    
5439         \X  always  matches  at least one character. Then it decides whether to         \X always matches at least one character. Then it  decides  whether  to
5440         add additional characters according to the following rules for ending a         add additional characters according to the following rules for ending a
5441         cluster:         cluster:
5442    
5443         1. End at the end of the subject string.         1. End at the end of the subject string.
5444    
5445         2.  Do not end between CR and LF; otherwise end after any control char-         2. Do not end between CR and LF; otherwise end after any control  char-
5446         acter.         acter.
5447    
5448         3. Do not break Hangul (a Korean  script)  syllable  sequences.  Hangul         3.  Do  not  break  Hangul (a Korean script) syllable sequences. Hangul
5449         characters  are of five types: L, V, T, LV, and LVT. An L character may         characters are of five types: L, V, T, LV, and LVT. An L character  may
5450         be followed by an L, V, LV, or LVT character; an LV or V character  may         be  followed by an L, V, LV, or LVT character; an LV or V character may
5451         be followed by a V or T character; an LVT or T character may be follwed         be followed by a V or T character; an LVT or T character may be follwed
5452         only by a T character.         only by a T character.
5453    
5454         4. Do not end before extending characters or spacing marks.  Characters         4.  Do not end before extending characters or spacing marks. Characters
5455         with  the  "mark"  property  always have the "extend" grapheme breaking         with the "mark" property always have  the  "extend"  grapheme  breaking
5456         property.         property.
5457    
5458         5. Do not end after prepend characters.         5. Do not end after prepend characters.
# Line 5460  BACKSLASH Line 5461  BACKSLASH
5461    
5462     PCRE's additional properties     PCRE's additional properties
5463    
5464         As well as the standard Unicode properties described above,  PCRE  sup-         As  well  as the standard Unicode properties described above, PCRE sup-
5465         ports  four  more  that  make it possible to convert traditional escape         ports four more that make it possible  to  convert  traditional  escape
5466         sequences such as \w and \s to use Unicode properties. PCRE uses  these         sequences  such as \w and \s to use Unicode properties. PCRE uses these
5467         non-standard, non-Perl properties internally when PCRE_UCP is set. How-         non-standard, non-Perl properties internally when PCRE_UCP is set. How-
5468         ever, they may also be used explicitly. These properties are:         ever, they may also be used explicitly. These properties are:
5469    
# Line 5471  BACKSLASH Line 5472  BACKSLASH
5472           Xsp   Any Perl space character           Xsp   Any Perl space character
5473           Xwd   Any Perl "word" character           Xwd   Any Perl "word" character
5474    
5475         Xan matches characters that have either the L (letter) or the  N  (num-         Xan  matches  characters that have either the L (letter) or the N (num-
5476         ber)  property. Xps matches the characters tab, linefeed, vertical tab,         ber) property. Xps matches the characters tab, linefeed, vertical  tab,
5477         form feed, or carriage return, and any other character that has  the  Z         form  feed,  or carriage return, and any other character that has the Z
5478         (separator)  property.  Xsp is the same as Xps; it used to exclude ver-         (separator) property.  Xsp is the same as Xps; it used to exclude  ver-
5479         tical tab, for Perl compatibility, but Perl changed, and so  PCRE  fol-         tical  tab,  for Perl compatibility, but Perl changed, and so PCRE fol-
5480         lowed  at  release  8.34.  Xwd matches the same characters as Xan, plus         lowed at release 8.34. Xwd matches the same  characters  as  Xan,  plus
5481         underscore.         underscore.
5482    
5483         There is another non-standard property, Xuc, which matches any  charac-         There  is another non-standard property, Xuc, which matches any charac-
5484         ter  that  can  be represented by a Universal Character Name in C++ and         ter that can be represented by a Universal Character Name  in  C++  and
5485         other programming languages. These are the characters $,  @,  `  (grave         other  programming  languages.  These are the characters $, @, ` (grave
5486         accent),  and  all  characters with Unicode code points greater than or         accent), and all characters with Unicode code points  greater  than  or
5487         equal to U+00A0, except for the surrogates U+D800 to U+DFFF. Note  that         equal  to U+00A0, except for the surrogates U+D800 to U+DFFF. Note that
5488         most  base  (ASCII) characters are excluded. (Universal Character Names         most base (ASCII) characters are excluded. (Universal  Character  Names
5489         are of the form \uHHHH or \UHHHHHHHH where H is  a  hexadecimal  digit.         are  of  the  form \uHHHH or \UHHHHHHHH where H is a hexadecimal digit.
5490         Note that the Xuc property does not match these sequences but the char-         Note that the Xuc property does not match these sequences but the char-
5491         acters that they represent.)         acters that they represent.)
5492    
5493     Resetting the match start     Resetting the match start
5494    
5495         The escape sequence \K causes any previously matched characters not  to         The  escape sequence \K causes any previously matched characters not to
5496         be included in the final matched sequence. For example, the pattern:         be included in the final matched sequence. For example, the pattern:
5497    
5498           foo\Kbar           foo\Kbar
5499    
5500         matches  "foobar",  but reports that it has matched "bar". This feature         matches "foobar", but reports that it has matched "bar".  This  feature
5501         is similar to a lookbehind assertion (described  below).   However,  in         is  similar  to  a lookbehind assertion (described below).  However, in
5502         this  case, the part of the subject before the real match does not have         this case, the part of the subject before the real match does not  have
5503         to be of fixed length, as lookbehind assertions do. The use of \K  does         to  be of fixed length, as lookbehind assertions do. The use of \K does
5504         not  interfere  with  the setting of captured substrings.  For example,         not interfere with the setting of captured  substrings.   For  example,
5505         when the pattern         when the pattern
5506    
5507           (foo)\Kbar           (foo)\Kbar
5508    
5509         matches "foobar", the first substring is still set to "foo".         matches "foobar", the first substring is still set to "foo".
5510    
5511         Perl documents that the use  of  \K  within  assertions  is  "not  well         Perl  documents  that  the  use  of  \K  within assertions is "not well
5512         defined".  In  PCRE,  \K  is  acted upon when it occurs inside positive         defined". In PCRE, \K is acted upon  when  it  occurs  inside  positive
5513         assertions, but is ignored in negative assertions.         assertions, but is ignored in negative assertions.
5514    
5515     Simple assertions     Simple assertions
5516    
5517         The final use of backslash is for certain simple assertions. An  asser-         The  final use of backslash is for certain simple assertions. An asser-
5518         tion  specifies a condition that has to be met at a particular point in         tion specifies a condition that has to be met at a particular point  in
5519         a match, without consuming any characters from the subject string.  The         a  match, without consuming any characters from the subject string. The
5520         use  of subpatterns for more complicated assertions is described below.         use of subpatterns for more complicated assertions is described  below.
5521         The backslashed assertions are:         The backslashed assertions are:
5522    
5523           \b     matches at a word boundary           \b     matches at a word boundary
# Line 5527  BACKSLASH Line 5528  BACKSLASH
5528           \z     matches only at the end of the subject           \z     matches only at the end of the subject
5529           \G     matches at the first matching position in the subject           \G     matches at the first matching position in the subject
5530    
5531         Inside a character class, \b has a different meaning;  it  matches  the         Inside  a  character  class, \b has a different meaning; it matches the
5532         backspace  character.  If  any  other  of these assertions appears in a         backspace character. If any other of  these  assertions  appears  in  a
5533         character class, by default it matches the corresponding literal  char-         character  class, by default it matches the corresponding literal char-
5534         acter  (for  example,  \B  matches  the  letter  B).  However,  if  the         acter  (for  example,  \B  matches  the  letter  B).  However,  if  the
5535         PCRE_EXTRA option is set, an "invalid escape sequence" error is  gener-         PCRE_EXTRA  option is set, an "invalid escape sequence" error is gener-
5536         ated instead.         ated instead.
5537    
5538         A  word  boundary is a position in the subject string where the current         A word boundary is a position in the subject string where  the  current
5539         character and the previous character do not both match \w or  \W  (i.e.         character  and  the previous character do not both match \w or \W (i.e.
5540         one  matches  \w  and the other matches \W), or the start or end of the         one matches \w and the other matches \W), or the start or  end  of  the
5541         string if the first or last character matches \w,  respectively.  In  a         string  if  the  first or last character matches \w, respectively. In a
5542         UTF  mode,  the  meanings  of  \w  and \W can be changed by setting the         UTF mode, the meanings of \w and \W  can  be  changed  by  setting  the
5543         PCRE_UCP option. When this is done, it also affects \b and \B.  Neither         PCRE_UCP  option. When this is done, it also affects \b and \B. Neither
5544         PCRE  nor  Perl has a separate "start of word" or "end of word" metase-         PCRE nor Perl has a separate "start of word" or "end of  word"  metase-
5545         quence. However, whatever follows \b normally determines which  it  is.         quence.  However,  whatever follows \b normally determines which it is.
5546         For example, the fragment \ba matches "a" at the start of a word.         For example, the fragment \ba matches "a" at the start of a word.
5547    
5548         The  \A,  \Z,  and \z assertions differ from the traditional circumflex         The \A, \Z, and \z assertions differ from  the  traditional  circumflex
5549         and dollar (described in the next section) in that they only ever match         and dollar (described in the next section) in that they only ever match
5550         at  the  very start and end of the subject string, whatever options are         at the very start and end of the subject string, whatever  options  are
5551         set. Thus, they are independent of multiline mode. These  three  asser-         set.  Thus,  they are independent of multiline mode. These three asser-
5552         tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which         tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which
5553         affect only the behaviour of the circumflex and dollar  metacharacters.         affect  only the behaviour of the circumflex and dollar metacharacters.
5554         However,  if the startoffset argument of pcre_exec() is non-zero, indi-         However, if the startoffset argument of pcre_exec() is non-zero,  indi-
5555         cating that matching is to start at a point other than the beginning of         cating that matching is to start at a point other than the beginning of
5556         the  subject,  \A  can never match. The difference between \Z and \z is         the subject, \A can never match. The difference between \Z  and  \z  is
5557         that \Z matches before a newline at the end of the string as well as at         that \Z matches before a newline at the end of the string as well as at
5558         the very end, whereas \z matches only at the end.         the very end, whereas \z matches only at the end.
5559    
5560         The  \G assertion is true only when the current matching position is at         The \G assertion is true only when the current matching position is  at
5561         the start point of the match, as specified by the startoffset  argument         the  start point of the match, as specified by the startoffset argument
5562         of  pcre_exec().  It  differs  from \A when the value of startoffset is         of pcre_exec(). It differs from \A when the  value  of  startoffset  is
5563         non-zero. By calling pcre_exec() multiple times with appropriate  argu-         non-zero.  By calling pcre_exec() multiple times with appropriate argu-
5564         ments, you can mimic Perl's /g option, and it is in this kind of imple-         ments, you can mimic Perl's /g option, and it is in this kind of imple-
5565         mentation where \G can be useful.         mentation where \G can be useful.
5566    
5567         Note, however, that PCRE's interpretation of \G, as the  start  of  the         Note,  however,  that  PCRE's interpretation of \G, as the start of the
5568         current match, is subtly different from Perl's, which defines it as the         current match, is subtly different from Perl's, which defines it as the
5569         end of the previous match. In Perl, these can  be  different  when  the         end  of  the  previous  match. In Perl, these can be different when the
5570         previously  matched  string was empty. Because PCRE does just one match         previously matched string was empty. Because PCRE does just  one  match
5571         at a time, it cannot reproduce this behaviour.         at a time, it cannot reproduce this behaviour.
5572    
5573         If all the alternatives of a pattern begin with \G, the  expression  is         If  all  the alternatives of a pattern begin with \G, the expression is
5574         anchored to the starting match position, and the "anchored" flag is set         anchored to the starting match position, and the "anchored" flag is set
5575         in the compiled regular expression.         in the compiled regular expression.
5576    
5577    
5578  CIRCUMFLEX AND DOLLAR  CIRCUMFLEX AND DOLLAR
5579    
5580         The circumflex and dollar  metacharacters  are  zero-width  assertions.         The  circumflex  and  dollar  metacharacters are zero-width assertions.
5581         That  is,  they test for a particular condition being true without con-         That is, they test for a particular condition being true  without  con-
5582         suming any characters from the subject string.         suming any characters from the subject string.
5583    
5584         Outside a character class, in the default matching mode, the circumflex         Outside a character class, in the default matching mode, the circumflex
5585         character  is  an  assertion  that is true only if the current matching         character is an assertion that is true only  if  the  current  matching
5586         point is at the start of the subject string. If the  startoffset  argu-         point  is  at the start of the subject string. If the startoffset argu-
5587         ment  of  pcre_exec()  is  non-zero,  circumflex can never match if the         ment of pcre_exec() is non-zero, circumflex  can  never  match  if  the
5588         PCRE_MULTILINE option is unset. Inside a  character  class,  circumflex         PCRE_MULTILINE  option  is  unset. Inside a character class, circumflex
5589         has an entirely different meaning (see below).         has an entirely different meaning (see below).
5590    
5591         Circumflex  need  not be the first character of the pattern if a number         Circumflex need not be the first character of the pattern if  a  number
5592         of alternatives are involved, but it should be the first thing in  each         of  alternatives are involved, but it should be the first thing in each
5593         alternative  in  which  it appears if the pattern is ever to match that         alternative in which it appears if the pattern is ever  to  match  that
5594         branch. If all possible alternatives start with a circumflex, that  is,         branch.  If all possible alternatives start with a circumflex, that is,
5595         if  the  pattern  is constrained to match only at the start of the sub-         if the pattern is constrained to match only at the start  of  the  sub-
5596         ject, it is said to be an "anchored" pattern.  (There  are  also  other         ject,  it  is  said  to be an "anchored" pattern. (There are also other
5597         constructs that can cause a pattern to be anchored.)         constructs that can cause a pattern to be anchored.)
5598    
5599         The  dollar  character is an assertion that is true only if the current         The dollar character is an assertion that is true only if  the  current
5600         matching point is at the end of  the  subject  string,  or  immediately         matching  point  is  at  the  end of the subject string, or immediately
5601         before  a newline at the end of the string (by default). Note, however,         before a newline at the end of the string (by default). Note,  however,
5602         that it does not actually match the newline. Dollar  need  not  be  the         that  it  does  not  actually match the newline. Dollar need not be the
5603         last character of the pattern if a number of alternatives are involved,         last character of the pattern if a number of alternatives are involved,
5604         but it should be the last item in any branch in which it appears.  Dol-         but  it should be the last item in any branch in which it appears. Dol-
5605         lar has no special meaning in a character class.         lar has no special meaning in a character class.
5606    
5607         The  meaning  of  dollar  can be changed so that it matches only at the         The meaning of dollar can be changed so that it  matches  only  at  the
5608         very end of the string, by setting the  PCRE_DOLLAR_ENDONLY  option  at         very  end  of  the string, by setting the PCRE_DOLLAR_ENDONLY option at
5609         compile time. This does not affect the \Z assertion.         compile time. This does not affect the \Z assertion.
5610    
5611         The meanings of the circumflex and dollar characters are changed if the         The meanings of the circumflex and dollar characters are changed if the
5612         PCRE_MULTILINE option is set. When  this  is  the  case,  a  circumflex         PCRE_MULTILINE  option  is  set.  When  this  is the case, a circumflex
5613         matches  immediately after internal newlines as well as at the start of         matches immediately after internal newlines as well as at the start  of
5614         the subject string. It does not match after a  newline  that  ends  the         the  subject  string.  It  does not match after a newline that ends the
5615         string.  A dollar matches before any newlines in the string, as well as         string. A dollar matches before any newlines in the string, as well  as
5616         at the very end, when PCRE_MULTILINE is set. When newline is  specified         at  the very end, when PCRE_MULTILINE is set. When newline is specified
5617         as  the  two-character  sequence CRLF, isolated CR and LF characters do         as the two-character sequence CRLF, isolated CR and  LF  characters  do
5618         not indicate newlines.         not indicate newlines.
5619    
5620         For example, the pattern /^abc$/ matches the subject string  "def\nabc"         For  example, the pattern /^abc$/ matches the subject string "def\nabc"
5621         (where  \n  represents a newline) in multiline mode, but not otherwise.         (where \n represents a newline) in multiline mode, but  not  otherwise.
5622         Consequently, patterns that are anchored in single  line  mode  because         Consequently,  patterns  that  are anchored in single line mode because
5623         all  branches  start  with  ^ are not anchored in multiline mode, and a         all branches start with ^ are not anchored in  multiline  mode,  and  a
5624         match for circumflex is  possible  when  the  startoffset  argument  of         match  for  circumflex  is  possible  when  the startoffset argument of
5625         pcre_exec()  is  non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if         pcre_exec() is non-zero. The PCRE_DOLLAR_ENDONLY option is  ignored  if
5626         PCRE_MULTILINE is set.         PCRE_MULTILINE is set.
5627    
5628         Note that the sequences \A, \Z, and \z can be used to match  the  start         Note  that  the sequences \A, \Z, and \z can be used to match the start
5629         and  end of the subject in both modes, and if all branches of a pattern         and end of the subject in both modes, and if all branches of a  pattern
5630         start with \A it is always anchored, whether or not  PCRE_MULTILINE  is         start  with  \A it is always anchored, whether or not PCRE_MULTILINE is
5631         set.         set.
5632    
5633    
5634  FULL STOP (PERIOD, DOT) AND \N  FULL STOP (PERIOD, DOT) AND \N
5635    
5636         Outside a character class, a dot in the pattern matches any one charac-         Outside a character class, a dot in the pattern matches any one charac-
5637         ter in the subject string except (by default) a character  that  signi-         ter  in  the subject string except (by default) a character that signi-
5638         fies the end of a line.         fies the end of a line.
5639    
5640         When  a line ending is defined as a single character, dot never matches         When a line ending is defined as a single character, dot never  matches
5641         that character; when the two-character sequence CRLF is used, dot  does         that  character; when the two-character sequence CRLF is used, dot does
5642         not  match  CR  if  it  is immediately followed by LF, but otherwise it         not match CR if it is immediately followed  by  LF,  but  otherwise  it
5643         matches all characters (including isolated CRs and LFs). When any  Uni-         matches  all characters (including isolated CRs and LFs). When any Uni-
5644         code  line endings are being recognized, dot does not match CR or LF or         code line endings are being recognized, dot does not match CR or LF  or
5645         any of the other line ending characters.         any of the other line ending characters.
5646    
5647         The behaviour of dot with regard to newlines can  be  changed.  If  the         The  behaviour  of  dot  with regard to newlines can be changed. If the
5648         PCRE_DOTALL  option  is  set,  a dot matches any one character, without         PCRE_DOTALL option is set, a dot matches  any  one  character,  without
5649         exception. If the two-character sequence CRLF is present in the subject         exception. If the two-character sequence CRLF is present in the subject
5650         string, it takes two dots to match it.         string, it takes two dots to match it.
5651    
5652         The  handling of dot is entirely independent of the handling of circum-         The handling of dot is entirely independent of the handling of  circum-
5653         flex and dollar, the only relationship being  that  they  both  involve         flex  and  dollar,  the  only relationship being that they both involve
5654         newlines. Dot has no special meaning in a character class.         newlines. Dot has no special meaning in a character class.
5655    
5656         The  escape  sequence  \N  behaves  like  a  dot, except that it is not         The escape sequence \N behaves like  a  dot,  except  that  it  is  not
5657         affected by the PCRE_DOTALL option. In  other  words,  it  matches  any         affected  by  the  PCRE_DOTALL  option.  In other words, it matches any
5658         character  except  one that signifies the end of a line. Perl also uses         character except one that signifies the end of a line. Perl  also  uses
5659         \N to match characters by name; PCRE does not support this.         \N to match characters by name; PCRE does not support this.
5660    
5661    
5662  MATCHING A SINGLE DATA UNIT  MATCHING A SINGLE DATA UNIT
5663    
5664         Outside a character class, the escape sequence \C matches any one  data         Outside  a character class, the escape sequence \C matches any one data
5665         unit,  whether or not a UTF mode is set. In the 8-bit library, one data         unit, whether or not a UTF mode is set. In the 8-bit library, one  data
5666         unit is one byte; in the 16-bit library it is a  16-bit  unit;  in  the         unit  is  one  byte;  in the 16-bit library it is a 16-bit unit; in the
5667         32-bit  library  it  is  a 32-bit unit. Unlike a dot, \C always matches         32-bit library it is a 32-bit unit. Unlike a  dot,  \C  always  matches
5668         line-ending characters. The feature is provided in  Perl  in  order  to         line-ending  characters.  The  feature  is provided in Perl in order to
5669         match individual bytes in UTF-8 mode, but it is unclear how it can use-         match individual bytes in UTF-8 mode, but it is unclear how it can use-
5670         fully be used. Because \C breaks up  characters  into  individual  data         fully  be  used.  Because  \C breaks up characters into individual data
5671         units,  matching  one unit with \C in a UTF mode means that the rest of         units, matching one unit with \C in a UTF mode means that the  rest  of
5672         the string may start with a malformed UTF character. This has undefined         the string may start with a malformed UTF character. This has undefined
5673         results, because PCRE assumes that it is dealing with valid UTF strings         results, because PCRE assumes that it is dealing with valid UTF strings
5674         (and by default it checks this at the start of  processing  unless  the         (and  by  default  it checks this at the start of processing unless the
5675         PCRE_NO_UTF8_CHECK,  PCRE_NO_UTF16_CHECK  or PCRE_NO_UTF32_CHECK option         PCRE_NO_UTF8_CHECK, PCRE_NO_UTF16_CHECK or  PCRE_NO_UTF32_CHECK  option
5676         is used).         is used).
5677    
5678         PCRE does not allow \C to appear in  lookbehind  assertions  (described         PCRE  does  not  allow \C to appear in lookbehind assertions (described
5679         below)  in  a UTF mode, because this would make it impossible to calcu-         below) in a UTF mode, because this would make it impossible  to  calcu-
5680         late the length of the lookbehind.         late the length of the lookbehind.
5681    
5682         In general, the \C escape sequence is best avoided. However, one way of         In general, the \C escape sequence is best avoided. However, one way of
5683         using  it that avoids the problem of malformed UTF characters is to use         using it that avoids the problem of malformed UTF characters is to  use
5684         a lookahead to check the length of the next character, as in this  pat-         a  lookahead to check the length of the next character, as in this pat-
5685         tern,  which  could be used with a UTF-8 string (ignore white space and         tern, which could be used with a UTF-8 string (ignore white  space  and
5686         line breaks):         line breaks):
5687    
5688           (?| (?=[\x00-\x7f])(\C) |           (?| (?=[\x00-\x7f])(\C) |
# Line 5689  MATCHING A SINGLE DATA UNIT Line 5690  MATCHING A SINGLE DATA UNIT
5690               (?=[\x{800}-\x{ffff}])(\C)(\C)(\C) |               (?=[\x{800}-\x{ffff}])(\C)(\C)(\C) |
5691               (?=[\x{10000}-\x{1fffff}])(\C)(\C)(\C)(\C))               (?=[\x{10000}-\x{1fffff}])(\C)(\C)(\C)(\C))
5692    
5693         A group that starts with (?| resets the capturing  parentheses  numbers         A  group  that starts with (?| resets the capturing parentheses numbers
5694         in  each  alternative  (see  "Duplicate Subpattern Numbers" below). The         in each alternative (see "Duplicate  Subpattern  Numbers"  below).  The
5695         assertions at the start of each branch check the next  UTF-8  character         assertions  at  the start of each branch check the next UTF-8 character
5696         for  values  whose encoding uses 1, 2, 3, or 4 bytes, respectively. The         for values whose encoding uses 1, 2, 3, or 4 bytes,  respectively.  The
5697         character's individual bytes are then captured by the appropriate  num-         character's  individual bytes are then captured by the appropriate num-
5698         ber of groups.         ber of groups.
5699    
5700    
# Line 5703  SQUARE BRACKETS AND CHARACTER CLASSES Line 5704  SQUARE BRACKETS AND CHARACTER CLASSES
5704         closing square bracket. A closing square bracket on its own is not spe-         closing square bracket. A closing square bracket on its own is not spe-
5705         cial by default.  However, if the PCRE_JAVASCRIPT_COMPAT option is set,         cial by default.  However, if the PCRE_JAVASCRIPT_COMPAT option is set,
5706         a lone closing square bracket causes a compile-time error. If a closing         a lone closing square bracket causes a compile-time error. If a closing
5707         square  bracket  is required as a member of the class, it should be the         square bracket is required as a member of the class, it should  be  the
5708         first data character in the class  (after  an  initial  circumflex,  if         first  data  character  in  the  class (after an initial circumflex, if
5709         present) or escaped with a backslash.         present) or escaped with a backslash.
5710    
5711         A  character  class matches a single character in the subject. In a UTF         A character class matches a single character in the subject. In  a  UTF
5712         mode, the character may be more than one  data  unit  long.  A  matched         mode,  the  character  may  be  more than one data unit long. A matched
5713         character must be in the set of characters defined by the class, unless         character must be in the set of characters defined by the class, unless
5714         the first character in the class definition is a circumflex,  in  which         the  first  character in the class definition is a circumflex, in which
5715         case the subject character must not be in the set defined by the class.         case the subject character must not be in the set defined by the class.
5716         If a circumflex is actually required as a member of the  class,  ensure         If  a  circumflex is actually required as a member of the class, ensure
5717         it is not the first character, or escape it with a backslash.         it is not the first character, or escape it with a backslash.
5718    
5719         For  example, the character class [aeiou] matches any lower case vowel,         For example, the character class [aeiou] matches any lower case  vowel,
5720         while [^aeiou] matches any character that is not a  lower  case  vowel.         while  [^aeiou]  matches  any character that is not a lower case vowel.
5721         Note that a circumflex is just a convenient notation for specifying the         Note that a circumflex is just a convenient notation for specifying the
5722         characters that are in the class by enumerating those that are  not.  A         characters  that  are in the class by enumerating those that are not. A
5723         class  that starts with a circumflex is not an assertion; it still con-         class that starts with a circumflex is not an assertion; it still  con-
5724         sumes a character from the subject string, and therefore  it  fails  if         sumes  a  character  from the subject string, and therefore it fails if
5725         the current pointer is at the end of the string.         the current pointer is at the end of the string.
5726    
5727         In UTF-8 (UTF-16, UTF-32) mode, characters with values greater than 255         In UTF-8 (UTF-16, UTF-32) mode, characters with values greater than 255
5728         (0xffff) can be included in a class as a literal string of data  units,         (0xffff)  can be included in a class as a literal string of data units,
5729         or by using the \x{ escaping mechanism.         or by using the \x{ escaping mechanism.
5730    
5731         When  caseless  matching  is set, any letters in a class represent both         When caseless matching is set, any letters in a  class  represent  both
5732         their upper case and lower case versions, so for  example,  a  caseless         their  upper  case  and lower case versions, so for example, a caseless
5733         [aeiou]  matches  "A"  as well as "a", and a caseless [^aeiou] does not         [aeiou] matches "A" as well as "a", and a caseless  [^aeiou]  does  not
5734         match "A", whereas a caseful version would. In a UTF mode, PCRE  always         match  "A", whereas a caseful version would. In a UTF mode, PCRE always
5735         understands  the  concept  of case for characters whose values are less         understands the concept of case for characters whose  values  are  less
5736         than 128, so caseless matching is always possible. For characters  with         than  128, so caseless matching is always possible. For characters with
5737         higher  values,  the  concept  of case is supported if PCRE is compiled         higher values, the concept of case is supported  if  PCRE  is  compiled
5738         with Unicode property support, but not otherwise.  If you want  to  use         with  Unicode  property support, but not otherwise.  If you want to use
5739         caseless  matching in a UTF mode for characters 128 and above, you must         caseless matching in a UTF mode for characters 128 and above, you  must
5740         ensure that PCRE is compiled with Unicode property support as  well  as         ensure  that  PCRE is compiled with Unicode property support as well as
5741         with UTF support.         with UTF support.
5742    
5743         Characters  that  might  indicate  line breaks are never treated in any         Characters that might indicate line breaks are  never  treated  in  any
5744         special way  when  matching  character  classes,  whatever  line-ending         special  way  when  matching  character  classes,  whatever line-ending
5745         sequence  is  in  use,  and  whatever  setting  of  the PCRE_DOTALL and         sequence is in  use,  and  whatever  setting  of  the  PCRE_DOTALL  and
5746         PCRE_MULTILINE options is used. A class such as [^a] always matches one         PCRE_MULTILINE options is used. A class such as [^a] always matches one
5747         of these characters.         of these characters.
5748    
5749         The  minus (hyphen) character can be used to specify a range of charac-         The minus (hyphen) character can be used to specify a range of  charac-
5750         ters in a character  class.  For  example,  [d-m]  matches  any  letter         ters  in  a  character  class.  For  example,  [d-m] matches any letter
5751         between  d  and  m,  inclusive.  If  a minus character is required in a         between d and m, inclusive. If a  minus  character  is  required  in  a
5752         class, it must be escaped with a backslash  or  appear  in  a  position         class,  it  must  be  escaped  with a backslash or appear in a position
5753         where  it cannot be interpreted as indicating a range, typically as the         where it cannot be interpreted as indicating a range, typically as  the
5754         first or last character in the class, or immediately after a range. For         first or last character in the class, or immediately after a range. For
5755         example,  [b-d-z] matches letters in the range b to d, a hyphen charac-         example, [b-d-z] matches letters in the range b to d, a hyphen  charac-
5756         ter, or z.         ter, or z.
5757    
5758         It is not possible to have the literal character "]" as the end charac-         It is not possible to have the literal character "]" as the end charac-
5759         ter  of a range. A pattern such as [W-]46] is interpreted as a class of         ter of a range. A pattern such as [W-]46] is interpreted as a class  of
5760         two characters ("W" and "-") followed by a literal string "46]", so  it         two  characters ("W" and "-") followed by a literal string "46]", so it
5761         would  match  "W46]"  or  "-46]". However, if the "]" is escaped with a         would match "W46]" or "-46]". However, if the "]"  is  escaped  with  a
5762         backslash it is interpreted as the end of range, so [W-\]46] is  inter-         backslash  it is interpreted as the end of range, so [W-\]46] is inter-
5763         preted  as a class containing a range followed by two other characters.         preted as a class containing a range followed by two other  characters.
5764         The octal or hexadecimal representation of "]" can also be used to  end         The  octal or hexadecimal representation of "]" can also be used to end
5765         a range.         a range.
5766    
5767         An  error  is  generated  if  a POSIX character class (see below) or an         An error is generated if a POSIX character  class  (see  below)  or  an
5768         escape sequence other than one that defines a single character  appears         escape  sequence other than one that defines a single character appears
5769         at  a  point  where  a range ending character is expected. For example,         at a point where a range ending character  is  expected.  For  example,
5770         [z-\xff] is valid, but [A-\d] and [A-[:digit:]] are not.         [z-\xff] is valid, but [A-\d] and [A-[:digit:]] are not.
5771    
5772         Ranges operate in the collating sequence of character values. They  can         Ranges  operate in the collating sequence of character values. They can
5773         also   be  used  for  characters  specified  numerically,  for  example         also  be  used  for  characters  specified  numerically,  for   example
5774         [\000-\037]. Ranges can include any characters that are valid  for  the         [\000-\037].  Ranges  can include any characters that are valid for the
5775         current mode.         current mode.
5776    
5777         If a range that includes letters is used when caseless matching is set,         If a range that includes letters is used when caseless matching is set,
5778         it matches the letters in either case. For example, [W-c] is equivalent         it matches the letters in either case. For example, [W-c] is equivalent
5779         to  [][\\^_`wxyzabc],  matched  caselessly,  and  in a non-UTF mode, if         to [][\\^_`wxyzabc], matched caselessly, and  in  a  non-UTF  mode,  if
5780         character tables for a French locale are in  use,  [\xc8-\xcb]  matches         character  tables  for  a French locale are in use, [\xc8-\xcb] matches
5781         accented  E  characters  in both cases. In UTF modes, PCRE supports the         accented E characters in both cases. In UTF modes,  PCRE  supports  the
5782         concept of case for characters with values greater than 128  only  when         concept  of  case for characters with values greater than 128 only when
5783         it is compiled with Unicode property support.         it is compiled with Unicode property support.
5784    
5785         The  character escape sequences \d, \D, \h, \H, \p, \P, \s, \S, \v, \V,         The character escape sequences \d, \D, \h, \H, \p, \P, \s, \S, \v,  \V,
5786         \w, and \W may appear in a character class, and add the characters that         \w, and \W may appear in a character class, and add the characters that
5787         they  match to the class. For example, [\dABCDEF] matches any hexadeci-         they match to the class. For example, [\dABCDEF] matches any  hexadeci-
5788         mal digit. In UTF modes, the PCRE_UCP option affects  the  meanings  of         mal  digit.  In  UTF modes, the PCRE_UCP option affects the meanings of
5789         \d,  \s,  \w  and  their upper case partners, just as it does when they         \d, \s, \w and their upper case partners, just as  it  does  when  they
5790         appear outside a character class, as described in the section  entitled         appear  outside a character class, as described in the section entitled
5791         "Generic character types" above. The escape sequence \b has a different         "Generic character types" above. The escape sequence \b has a different
5792         meaning inside a character class; it matches the  backspace  character.         meaning  inside  a character class; it matches the backspace character.
5793         The  sequences  \B,  \N,  \R, and \X are not special inside a character         The sequences \B, \N, \R, and \X are not  special  inside  a  character
5794         class. Like any other unrecognized escape sequences, they  are  treated         class.  Like  any other unrecognized escape sequences, they are treated
5795         as  the literal characters "B", "N", "R", and "X" by default, but cause         as the literal characters "B", "N", "R", and "X" by default, but  cause
5796         an error if the PCRE_EXTRA option is set.         an error if the PCRE_EXTRA option is set.
5797    
5798         A circumflex can conveniently be used with  the  upper  case  character         A  circumflex  can  conveniently  be used with the upper case character
5799         types  to specify a more restricted set of characters than the matching         types to specify a more restricted set of characters than the  matching
5800         lower case type.  For example, the class [^\W_] matches any  letter  or         lower  case  type.  For example, the class [^\W_] matches any letter or
5801         digit, but not underscore, whereas [\w] includes underscore. A positive         digit, but not underscore, whereas [\w] includes underscore. A positive
5802         character class should be read as "something OR something OR ..." and a         character class should be read as "something OR something OR ..." and a
5803         negative class as "NOT something AND NOT something AND NOT ...".         negative class as "NOT something AND NOT something AND NOT ...".
5804    
5805         The  only  metacharacters  that are recognized in character classes are         The only metacharacters that are recognized in  character  classes  are
5806         backslash, hyphen (only where it can be  interpreted  as  specifying  a         backslash,  hyphen  (only  where  it can be interpreted as specifying a
5807         range),  circumflex  (only  at the start), opening square bracket (only         range), circumflex (only at the start), opening  square  bracket  (only
5808         when it can be interpreted as introducing a POSIX class name - see  the         when  it can be interpreted as introducing a POSIX class name, or for a
5809         next  section),  and  the  terminating closing square bracket. However,         special compatibility feature - see the next  two  sections),  and  the
5810         escaping other non-alphanumeric characters does no harm.         terminating  closing  square  bracket.  However,  escaping  other  non-
5811           alphanumeric characters does no harm.
5812    
5813    
5814  POSIX CHARACTER CLASSES  POSIX CHARACTER CLASSES
# Line 5837  POSIX CHARACTER CLASSES Line 5839  POSIX CHARACTER CLASSES
5839    
5840         The  default  "space" characters are HT (9), LF (10), VT (11), FF (12),         The  default  "space" characters are HT (9), LF (10), VT (11), FF (12),
5841         CR (13), and space (32). If locale-specific matching is  taking  place,         CR (13), and space (32). If locale-specific matching is  taking  place,
5842         there  may be additional space characters. "Space" used to be different         the  list  of  space characters may be different; there may be fewer or
5843         to \s, which did not include VT, for Perl compatibility. However,  Perl         more of them. "Space" used to be different to \s, which did not include
5844         changed at release 5.18, and PCRE followed at release 8.34. "Space" and         VT, for Perl compatibility.  However, Perl changed at release 5.18, and
5845         \s now match the same set of characters.         PCRE followed at release 8.34.  "Space" and \s now match the  same  set
5846           of characters.
5847    
5848         The name "word" is a Perl extension, and "blank"  is  a  GNU  extension         The  name  "word"  is  a Perl extension, and "blank" is a GNU extension
5849         from  Perl  5.8. Another Perl extension is negation, which is indicated         from Perl 5.8. Another Perl extension is negation, which  is  indicated
5850         by a ^ character after the colon. For example,         by a ^ character after the colon. For example,
5851    
5852           [12[:^digit:]]           [12[:^digit:]]
5853    
5854         matches "1", "2", or any non-digit. PCRE (and Perl) also recognize  the         matches  "1", "2", or any non-digit. PCRE (and Perl) also recognize the
5855         POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but         POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but
5856         these are not supported, and an error is given if they are encountered.         these are not supported, and an error is given if they are encountered.
5857    
5858         By default, characters with values greater than 128 do not match any of         By default, characters with values greater than 128 do not match any of
5859         the  POSIX character classes. However, if the PCRE_UCP option is passed         the POSIX character classes. However, if the PCRE_UCP option is  passed
5860         to pcre_compile(), some of the classes  are  changed  so  that  Unicode         to  pcre_compile(),  some  of  the  classes are changed so that Unicode
5861         character  properties  are  used. This is achieved by replacing certain         character properties are used. This is achieved  by  replacing  certain
5862         POSIX classes by other sequences, as follows:         POSIX classes by other sequences, as follows:
5863    
5864           [:alnum:]  becomes  \p{Xan}           [:alnum:]  becomes  \p{Xan}
# Line 5867  POSIX CHARACTER CLASSES Line 5870  POSIX CHARACTER CLASSES
5870           [:upper:]  becomes  \p{Lu}           [:upper:]  becomes  \p{Lu}
5871           [:word:]   becomes  \p{Xwd}           [:word:]   becomes  \p{Xwd}
5872    
5873         Negated versions, such as [:^alpha:] use \P instead of \p. Three  other         Negated  versions, such as [:^alpha:] use \P instead of \p. Three other
5874         POSIX classes are handled specially in UCP mode:         POSIX classes are handled specially in UCP mode:
5875    
5876         [:graph:] This  matches  characters that have glyphs that mark the page         [:graph:] This matches characters that have glyphs that mark  the  page
5877                   when printed. In Unicode property terms, it matches all char-                   when printed. In Unicode property terms, it matches all char-
5878                   acters with the L, M, N, P, S, or Cf properties, except for:                   acters with the L, M, N, P, S, or Cf properties, except for:
5879    
# Line 5879  POSIX CHARACTER CLASSES Line 5882  POSIX CHARACTER CLASSES
5882                     U+2066 - U+2069  Various "isolate"s                     U+2066 - U+2069  Various "isolate"s
5883    
5884    
5885         [:print:] This  matches  the  same  characters  as [:graph:] plus space         [:print:] This matches the same  characters  as  [:graph:]  plus  space
5886                   characters that are not controls, that  is,  characters  with                   characters  that  are  not controls, that is, characters with
5887                   the Zs property.                   the Zs property.
5888    
5889         [:punct:] This matches all characters that have the Unicode P (punctua-         [:punct:] This matches all characters that have the Unicode P (punctua-
5890                   tion) property, plus those characters whose code  points  are                   tion)  property,  plus those characters whose code points are
5891                   less than 128 that have the S (Symbol) property.                   less than 128 that have the S (Symbol) property.
5892    
5893         The  other  POSIX classes are unchanged, and match only characters with         The other POSIX classes are unchanged, and match only  characters  with
5894         code points less than 128.         code points less than 128.
5895    
5896    
5897    COMPATIBILITY FEATURE FOR WORD BOUNDARIES
5898    
5899           In  the POSIX.2 compliant library that was included in 4.4BSD Unix, the
5900           ugly syntax [[:<:]] and [[:>:]] is used for matching  "start  of  word"
5901           and "end of word". PCRE treats these items as follows:
5902    
5903             [[:<:]]  is converted to  \b(?=\w)
5904             [[:>:]]  is converted to  \b(?<=\w)
5905    
5906           Only these exact character sequences are recognized. A sequence such as
5907           [a[:<:]b] provokes error for an unrecognized  POSIX  class  name.  This
5908           support  is not compatible with Perl. It is provided to help migrations
5909           from other environments, and is best not used in any new patterns. Note
5910           that  \b matches at the start and the end of a word (see "Simple asser-
5911           tions" above), and in a Perl-style pattern the preceding  or  following
5912           character  normally  shows  which  is  wanted, without the need for the
5913           assertions that are used above in order to give exactly the  POSIX  be-
5914           haviour.
5915    
5916    
5917  VERTICAL BAR  VERTICAL BAR
5918    
5919         Vertical bar characters are used to separate alternative patterns.  For         Vertical  bar characters are used to separate alternative patterns. For
5920         example, the pattern         example, the pattern
5921    
5922           gilbert|sullivan           gilbert|sullivan
5923    
5924         matches  either "gilbert" or "sullivan". Any number of alternatives may         matches either "gilbert" or "sullivan". Any number of alternatives  may
5925         appear, and an empty  alternative  is  permitted  (matching  the  empty         appear,  and  an  empty  alternative  is  permitted (matching the empty
5926         string). The matching process tries each alternative in turn, from left         string). The matching process tries each alternative in turn, from left
5927         to right, and the first one that succeeds is used. If the  alternatives         to  right, and the first one that succeeds is used. If the alternatives
5928         are  within a subpattern (defined below), "succeeds" means matching the         are within a subpattern (defined below), "succeeds" means matching  the
5929         rest of the main pattern as well as the alternative in the subpattern.         rest of the main pattern as well as the alternative in the subpattern.
5930    
5931    
5932  INTERNAL OPTION SETTING  INTERNAL OPTION SETTING
5933    
5934         The settings of the  PCRE_CASELESS,  PCRE_MULTILINE,  PCRE_DOTALL,  and         The  settings  of  the  PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
5935         PCRE_EXTENDED  options  (which are Perl-compatible) can be changed from         PCRE_EXTENDED options (which are Perl-compatible) can be  changed  from
5936         within the pattern by  a  sequence  of  Perl  option  letters  enclosed         within  the  pattern  by  a  sequence  of  Perl option letters enclosed
5937         between "(?" and ")".  The option letters are         between "(?" and ")".  The option letters are
5938    
5939           i  for PCRE_CASELESS           i  for PCRE_CASELESS
# Line 5920  INTERNAL OPTION SETTING Line 5943  INTERNAL OPTION SETTING
5943    
5944         For example, (?im) sets caseless, multiline matching. It is also possi-         For example, (?im) sets caseless, multiline matching. It is also possi-
5945         ble to unset these options by preceding the letter with a hyphen, and a         ble to unset these options by preceding the letter with a hyphen, and a
5946         combined  setting and unsetting such as (?im-sx), which sets PCRE_CASE-         combined setting and unsetting such as (?im-sx), which sets  PCRE_CASE-
5947         LESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and  PCRE_EXTENDED,         LESS  and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED,
5948         is  also  permitted.  If  a  letter  appears  both before and after the         is also permitted. If a  letter  appears  both  before  and  after  the
5949         hyphen, the option is unset.         hyphen, the option is unset.
5950    
5951         The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and  PCRE_EXTRA         The  PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA
5952         can  be changed in the same way as the Perl-compatible options by using         can be changed in the same way as the Perl-compatible options by  using
5953         the characters J, U and X respectively.         the characters J, U and X respectively.
5954    
5955         When one of these option changes occurs at  top  level  (that  is,  not         When  one  of  these  option  changes occurs at top level (that is, not
5956         inside  subpattern parentheses), the change applies to the remainder of         inside subpattern parentheses), the change applies to the remainder  of
5957         the pattern that follows. If the change is placed right at the start of         the pattern that follows. If the change is placed right at the start of
5958         a pattern, PCRE extracts it into the global options (and it will there-         a pattern, PCRE extracts it into the global options (and it will there-
5959         fore show up in data extracted by the pcre_fullinfo() function).         fore show up in data extracted by the pcre_fullinfo() function).
5960    
5961         An option change within a subpattern (see below for  a  description  of         An  option  change  within a subpattern (see below for a description of
5962         subpatterns)  affects only that part of the subpattern that follows it,         subpatterns) affects only that part of the subpattern that follows  it,
5963         so         so
5964    
5965           (a(?i)b)c           (a(?i)b)c
5966    
5967         matches abc and aBc and no other strings (assuming PCRE_CASELESS is not         matches abc and aBc and no other strings (assuming PCRE_CASELESS is not
5968         used).   By  this means, options can be made to have different settings         used).  By this means, options can be made to have  different  settings
5969         in different parts of the pattern. Any changes made in one  alternative         in  different parts of the pattern. Any changes made in one alternative
5970         do  carry  on  into subsequent branches within the same subpattern. For         do carry on into subsequent branches within the  same  subpattern.  For
5971         example,         example,
5972    
5973           (a(?i)b|c)           (a(?i)b|c)
5974    
5975         matches "ab", "aB", "c", and "C", even though  when  matching  "C"  the         matches  "ab",  "aB",  "c",  and "C", even though when matching "C" the
5976         first  branch  is  abandoned before the option setting. This is because         first branch is abandoned before the option setting.  This  is  because
5977         the effects of option settings happen at compile time. There  would  be         the  effects  of option settings happen at compile time. There would be
5978         some very weird behaviour otherwise.         some very weird behaviour otherwise.
5979    
5980         Note:  There  are  other  PCRE-specific  options that can be set by the         Note: There are other PCRE-specific options that  can  be  set  by  the
5981         application when the compiling or matching  functions  are  called.  In         application  when  the  compiling  or matching functions are called. In
5982         some  cases  the  pattern can contain special leading sequences such as         some cases the pattern can contain special leading  sequences  such  as
5983         (*CRLF) to override what the application  has  set  or  what  has  been         (*CRLF)  to  override  what  the  application  has set or what has been
5984         defaulted.   Details   are  given  in  the  section  entitled  "Newline         defaulted.  Details  are  given  in  the  section   entitled   "Newline
5985         sequences" above. There are also the  (*UTF8),  (*UTF16),(*UTF32),  and         sequences"  above.  There  are also the (*UTF8), (*UTF16),(*UTF32), and
5986         (*UCP)  leading sequences that can be used to set UTF and Unicode prop-         (*UCP) leading sequences that can be used to set UTF and Unicode  prop-
5987         erty modes; they are equivalent to setting the  PCRE_UTF8,  PCRE_UTF16,         erty  modes;  they are equivalent to setting the PCRE_UTF8, PCRE_UTF16,
5988         PCRE_UTF32  and the PCRE_UCP options, respectively. The (*UTF) sequence         PCRE_UTF32 and the PCRE_UCP options, respectively. The (*UTF)  sequence
5989         is a generic version that can be used with any of the  libraries.  How-         is  a  generic version that can be used with any of the libraries. How-
5990         ever,  the  application  can set the PCRE_NEVER_UTF option, which locks         ever, the application can set the PCRE_NEVER_UTF  option,  which  locks
5991         out the use of the (*UTF) sequences.         out the use of the (*UTF) sequences.
5992    
5993    
# Line 5977  SUBPATTERNS Line 6000  SUBPATTERNS
6000    
6001           cat(aract|erpillar|)           cat(aract|erpillar|)
6002    
6003         matches  "cataract",  "caterpillar", or "cat". Without the parentheses,         matches "cataract", "caterpillar", or "cat". Without  the  parentheses,
6004         it would match "cataract", "erpillar" or an empty string.         it would match "cataract", "erpillar" or an empty string.
6005    
6006         2. It sets up the subpattern as  a  capturing  subpattern.  This  means         2.  It  sets  up  the  subpattern as a capturing subpattern. This means
6007         that,  when  the  whole  pattern  matches,  that portion of the subject         that, when the whole pattern  matches,  that  portion  of  the  subject
6008         string that matched the subpattern is passed back to the caller via the         string that matched the subpattern is passed back to the caller via the
6009         ovector  argument  of  the matching function. (This applies only to the         ovector argument of the matching function. (This applies  only  to  the
6010         traditional matching functions; the DFA matching functions do not  sup-         traditional  matching functions; the DFA matching functions do not sup-
6011         port capturing.)         port capturing.)
6012    
6013         Opening parentheses are counted from left to right (starting from 1) to         Opening parentheses are counted from left to right (starting from 1) to
6014         obtain numbers for the  capturing  subpatterns.  For  example,  if  the         obtain  numbers  for  the  capturing  subpatterns.  For example, if the
6015         string "the red king" is matched against the pattern         string "the red king" is matched against the pattern
6016    
6017           the ((red|white) (king|queen))           the ((red|white) (king|queen))
# Line 5996  SUBPATTERNS Line 6019  SUBPATTERNS
6019         the captured substrings are "red king", "red", and "king", and are num-         the captured substrings are "red king", "red", and "king", and are num-
6020         bered 1, 2, and 3, respectively.         bered 1, 2, and 3, respectively.
6021    
6022         The fact that plain parentheses fulfil  two  functions  is  not  always         The  fact  that  plain  parentheses  fulfil two functions is not always
6023         helpful.   There are often times when a grouping subpattern is required         helpful.  There are often times when a grouping subpattern is  required
6024         without a capturing requirement. If an opening parenthesis is  followed         without  a capturing requirement. If an opening parenthesis is followed
6025         by  a question mark and a colon, the subpattern does not do any captur-         by a question mark and a colon, the subpattern does not do any  captur-
6026         ing, and is not counted when computing the  number  of  any  subsequent         ing,  and  is  not  counted when computing the number of any subsequent
6027         capturing  subpatterns. For example, if the string "the white queen" is         capturing subpatterns. For example, if the string "the white queen"  is
6028         matched against the pattern         matched against the pattern
6029    
6030           the ((?:red|white) (king|queen))           the ((?:red|white) (king|queen))
# Line 6009  SUBPATTERNS Line 6032  SUBPATTERNS
6032         the captured substrings are "white queen" and "queen", and are numbered         the captured substrings are "white queen" and "queen", and are numbered
6033         1 and 2. The maximum number of capturing subpatterns is 65535.         1 and 2. The maximum number of capturing subpatterns is 65535.
6034    
6035         As  a  convenient shorthand, if any option settings are required at the         As a convenient shorthand, if any option settings are required  at  the
6036         start of a non-capturing subpattern,  the  option  letters  may  appear         start  of  a  non-capturing  subpattern,  the option letters may appear
6037         between the "?" and the ":". Thus the two patterns         between the "?" and the ":". Thus the two patterns
6038    
6039           (?i:saturday|sunday)           (?i:saturday|sunday)
6040           (?:(?i)saturday|sunday)           (?:(?i)saturday|sunday)
6041    
6042         match exactly the same set of strings. Because alternative branches are         match exactly the same set of strings. Because alternative branches are
6043         tried from left to right, and options are not reset until  the  end  of         tried  from  left  to right, and options are not reset until the end of
6044         the  subpattern is reached, an option setting in one branch does affect         the subpattern is reached, an option setting in one branch does  affect
6045         subsequent branches, so the above patterns match "SUNDAY"  as  well  as         subsequent  branches,  so  the above patterns match "SUNDAY" as well as
6046         "Saturday".         "Saturday".
6047    
6048    
6049  DUPLICATE SUBPATTERN NUMBERS  DUPLICATE SUBPATTERN NUMBERS
6050    
6051         Perl 5.10 introduced a feature whereby each alternative in a subpattern         Perl 5.10 introduced a feature whereby each alternative in a subpattern
6052         uses the same numbers for its capturing parentheses. Such a  subpattern         uses  the same numbers for its capturing parentheses. Such a subpattern
6053         starts  with (?| and is itself a non-capturing subpattern. For example,         starts with (?| and is itself a non-capturing subpattern. For  example,
6054         consider this pattern:         consider this pattern:
6055    
6056           (?|(Sat)ur|(Sun))day           (?|(Sat)ur|(Sun))day
6057    
6058         Because the two alternatives are inside a (?| group, both sets of  cap-         Because  the two alternatives are inside a (?| group, both sets of cap-
6059         turing  parentheses  are  numbered one. Thus, when the pattern matches,         turing parentheses are numbered one. Thus, when  the  pattern  matches,
6060         you can look at captured substring number  one,  whichever  alternative         you  can  look  at captured substring number one, whichever alternative
6061         matched.  This  construct  is useful when you want to capture part, but         matched. This construct is useful when you want to  capture  part,  but
6062         not all, of one of a number of alternatives. Inside a (?| group, paren-         not all, of one of a number of alternatives. Inside a (?| group, paren-
6063         theses  are  numbered as usual, but the number is reset at the start of         theses are numbered as usual, but the number is reset at the  start  of
6064         each branch. The numbers of any capturing parentheses that  follow  the         each  branch.  The numbers of any capturing parentheses that follow the
6065         subpattern  start after the highest number used in any branch. The fol-         subpattern start after the highest number used in any branch. The  fol-
6066         lowing example is taken from the Perl documentation. The numbers under-         lowing example is taken from the Perl documentation. The numbers under-
6067         neath show in which buffer the captured content will be stored.         neath show in which buffer the captured content will be stored.
6068    
# Line 6047  DUPLICATE SUBPATTERN NUMBERS Line 6070  DUPLICATE SUBPATTERN NUMBERS
6070           / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x           / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
6071           # 1            2         2  3        2     3     4           # 1            2         2  3        2     3     4
6072    
6073         A  back  reference  to a numbered subpattern uses the most recent value         A back reference to a numbered subpattern uses the  most  recent  value
6074         that is set for that number by any subpattern.  The  following  pattern         that  is  set  for that number by any subpattern. The following pattern
6075         matches "abcabc" or "defdef":         matches "abcabc" or "defdef":
6076    
6077           /(?|(abc)|(def))\1/           /(?|(abc)|(def))\1/
6078    
6079         In  contrast,  a subroutine call to a numbered subpattern always refers         In contrast, a subroutine call to a numbered subpattern  always  refers
6080         to the first one in the pattern with the given  number.  The  following         to  the  first  one in the pattern with the given number. The following
6081         pattern matches "abcabc" or "defabc":         pattern matches "abcabc" or "defabc":
6082    
6083           /(?|(abc)|(def))(?1)/           /(?|(abc)|(def))(?1)/
6084    
6085         If  a condition test for a subpattern's having matched refers to a non-         If a condition test for a subpattern's having matched refers to a  non-
6086         unique number, the test is true if any of the subpatterns of that  num-         unique  number, the test is true if any of the subpatterns of that num-
6087         ber have matched.         ber have matched.
6088    
6089         An  alternative approach to using this "branch reset" feature is to use         An alternative approach to using this "branch reset" feature is to  use
6090         duplicate named subpatterns, as described in the next section.         duplicate named subpatterns, as described in the next section.
6091    
6092    
6093  NAMED SUBPATTERNS  NAMED SUBPATTERNS
6094    
6095         Identifying capturing parentheses by number is simple, but  it  can  be         Identifying  capturing  parentheses  by number is simple, but it can be
6096         very  hard  to keep track of the numbers in complicated regular expres-         very hard to keep track of the numbers in complicated  regular  expres-
6097         sions. Furthermore, if an  expression  is  modified,  the  numbers  may         sions.  Furthermore,  if  an  expression  is  modified, the numbers may
6098         change.  To help with this difficulty, PCRE supports the naming of sub-         change. To help with this difficulty, PCRE supports the naming of  sub-
6099         patterns. This feature was not added to Perl until release 5.10. Python         patterns. This feature was not added to Perl until release 5.10. Python
6100         had  the  feature earlier, and PCRE introduced it at release 4.0, using         had the feature earlier, and PCRE introduced it at release  4.0,  using
6101         the Python syntax. PCRE now supports both the Perl and the Python  syn-         the  Python syntax. PCRE now supports both the Perl and the Python syn-
6102         tax.  Perl  allows  identically  numbered subpatterns to have different         tax. Perl allows identically numbered  subpatterns  to  have  different
6103         names, but PCRE does not.         names, but PCRE does not.
6104    
6105         In PCRE, a subpattern can be named in one of three  ways:  (?<name>...)         In  PCRE,  a subpattern can be named in one of three ways: (?<name>...)
6106         or  (?'name'...)  as in Perl, or (?P<name>...) as in Python. References         or (?'name'...) as in Perl, or (?P<name>...) as in  Python.  References
6107         to capturing parentheses from other parts of the pattern, such as  back         to  capturing parentheses from other parts of the pattern, such as back
6108         references,  recursion,  and conditions, can be made by name as well as         references, recursion, and conditions, can be made by name as  well  as
6109         by number.         by number.
6110    
6111         Names consist of up to 32 alphanumeric characters and underscores,  but         Names  consist of up to 32 alphanumeric characters and underscores, but
6112         must  start  with  a  non-digit.  Named capturing parentheses are still         must start with a non-digit.  Named  capturing  parentheses  are  still
6113         allocated numbers as well as names, exactly as if the  names  were  not         allocated  numbers  as  well as names, exactly as if the names were not
6114         present.  The PCRE API provides function calls for extracting the name-         present. The PCRE API provides function calls for extracting the  name-
6115         to-number translation table from a compiled pattern. There  is  also  a         to-number  translation  table  from a compiled pattern. There is also a
6116         convenience function for extracting a captured substring by name.         convenience function for extracting a captured substring by name.
6117    
6118         By  default, a name must be unique within a pattern, but it is possible         By default, a name must be unique within a pattern, but it is  possible
6119         to relax this constraint by setting the PCRE_DUPNAMES option at compile         to relax this constraint by setting the PCRE_DUPNAMES option at compile
6120         time.  (Duplicate  names are also always permitted for subpatterns with         time. (Duplicate names are also always permitted for  subpatterns  with
6121         the same number, set up as described in the previous  section.)  Dupli-         the  same  number, set up as described in the previous section.) Dupli-
6122         cate  names  can  be useful for patterns where only one instance of the         cate names can be useful for patterns where only one  instance  of  the
6123         named parentheses can match. Suppose you want to match the  name  of  a         named  parentheses  can  match. Suppose you want to match the name of a
6124         weekday,  either as a 3-letter abbreviation or as the full name, and in         weekday, either as a 3-letter abbreviation or as the full name, and  in
6125         both cases you want to extract the abbreviation. This pattern (ignoring         both cases you want to extract the abbreviation. This pattern (ignoring
6126         the line breaks) does the job:         the line breaks) does the job:
6127    
# Line 6108  NAMED SUBPATTERNS Line 6131  NAMED SUBPATTERNS
6131           (?<DN>Thu)(?:rsday)?|           (?<DN>Thu)(?:rsday)?|
6132           (?<DN>Sat)(?:urday)?           (?<DN>Sat)(?:urday)?
6133    
6134         There  are  five capturing substrings, but only one is ever set after a         There are five capturing substrings, but only one is ever set  after  a
6135         match.  (An alternative way of solving this problem is to use a "branch         match.  (An alternative way of solving this problem is to use a "branch
6136         reset" subpattern, as described in the previous section.)         reset" subpattern, as described in the previous section.)
6137    
6138         The  convenience  function  for extracting the data by name returns the         The convenience function for extracting the data by  name  returns  the
6139         substring for the first (and in this example, the only)  subpattern  of         substring  for  the first (and in this example, the only) subpattern of
6140         that  name  that  matched.  This saves searching to find which numbered         that name that matched. This saves searching  to  find  which  numbered
6141         subpattern it was.         subpattern it was.
6142    
6143         If you make a back reference to  a  non-unique  named  subpattern  from         If  you  make  a  back  reference to a non-unique named subpattern from
6144         elsewhere  in the pattern, the subpatterns to which the name refers are         elsewhere in the pattern, the subpatterns to which the name refers  are
6145         checked in the order in which they appear in the overall  pattern.  The         checked  in  the order in which they appear in the overall pattern. The
6146         first one that is set is used for the reference. For example, this pat-         first one that is set is used for the reference. For example, this pat-
6147         tern matches both "foofoo" and "barbar" but not "foobar" or "barfoo":         tern matches both "foofoo" and "barbar" but not "foobar" or "barfoo":
6148    
# Line 6127  NAMED SUBPATTERNS Line 6150  NAMED SUBPATTERNS
6150    
6151    
6152         If you make a subroutine call to a non-unique named subpattern, the one         If you make a subroutine call to a non-unique named subpattern, the one
6153         that  corresponds  to  the first occurrence of the name is used. In the         that corresponds to the first occurrence of the name is  used.  In  the
6154         absence of duplicate numbers (see the previous section) this is the one         absence of duplicate numbers (see the previous section) this is the one
6155         with the lowest number.         with the lowest number.
6156    
6157         If you use a named reference in a condition test (see the section about         If you use a named reference in a condition test (see the section about
6158         conditions below), either to check whether a subpattern has matched, or         conditions below), either to check whether a subpattern has matched, or
6159         to  check for recursion, all subpatterns with the same name are tested.         to check for recursion, all subpatterns with the same name are  tested.
6160         If the condition is true for any one of them, the overall condition  is         If  the condition is true for any one of them, the overall condition is
6161         true.  This  is  the  same  behaviour as testing by number. For further         true. This is the same behaviour as  testing  by  number.  For  further
6162         details of the interfaces  for  handling  named  subpatterns,  see  the         details  of  the  interfaces  for  handling  named subpatterns, see the
6163         pcreapi documentation.         pcreapi documentation.
6164    
6165         Warning: You cannot use different names to distinguish between two sub-         Warning: You cannot use different names to distinguish between two sub-
6166         patterns with the same number because PCRE uses only the  numbers  when         patterns  with  the same number because PCRE uses only the numbers when
6167         matching. For this reason, an error is given at compile time if differ-         matching. For this reason, an error is given at compile time if differ-
6168         ent names are given to subpatterns with the same number.  However,  you         ent  names  are given to subpatterns with the same number. However, you
6169         can always give the same name to subpatterns with the same number, even         can always give the same name to subpatterns with the same number, even
6170         when PCRE_DUPNAMES is not set.         when PCRE_DUPNAMES is not set.
6171    
6172    
6173  REPETITION  REPETITION
6174    
6175         Repetition is specified by quantifiers, which can  follow  any  of  the         Repetition  is  specified  by  quantifiers, which can follow any of the
6176         following items:         following items:
6177    
6178           a literal data character           a literal data character
# Line 6163  REPETITION Line 6186  REPETITION
6186           a parenthesized subpattern (including assertions)           a parenthesized subpattern (including assertions)
6187           a subroutine call to a subpattern (recursive or otherwise)           a subroutine call to a subpattern (recursive or otherwise)
6188    
6189         The  general repetition quantifier specifies a minimum and maximum num-         The general repetition quantifier specifies a minimum and maximum  num-
6190         ber of permitted matches, by giving the two numbers in  curly  brackets         ber  of  permitted matches, by giving the two numbers in curly brackets
6191         (braces),  separated  by  a comma. The numbers must be less than 65536,         (braces), separated by a comma. The numbers must be  less  than  65536,
6192         and the first must be less than or equal to the second. For example:         and the first must be less than or equal to the second. For example:
6193    
6194           z{2,4}           z{2,4}
6195    
6196         matches "zz", "zzz", or "zzzz". A closing brace on its  own  is  not  a         matches  "zz",  "zzz",  or  "zzzz". A closing brace on its own is not a
6197         special  character.  If  the second number is omitted, but the comma is         special character. If the second number is omitted, but  the  comma  is
6198         present, there is no upper limit; if the second number  and  the  comma         present,  there  is  no upper limit; if the second number and the comma
6199         are  both omitted, the quantifier specifies an exact number of required         are both omitted, the quantifier specifies an exact number of  required
6200         matches. Thus         matches. Thus
6201    
6202           [aeiou]{3,}           [aeiou]{3,}
# Line 6182  REPETITION Line 6205  REPETITION
6205    
6206           \d{8}           \d{8}
6207    
6208         matches exactly 8 digits. An opening curly bracket that  appears  in  a         matches  exactly  8  digits. An opening curly bracket that appears in a
6209         position  where a quantifier is not allowed, or one that does not match         position where a quantifier is not allowed, or one that does not  match
6210         the syntax of a quantifier, is taken as a literal character. For  exam-         the  syntax of a quantifier, is taken as a literal character. For exam-
6211         ple, {,6} is not a quantifier, but a literal string of four characters.         ple, {,6} is not a quantifier, but a literal string of four characters.
6212    
6213         In UTF modes, quantifiers apply to characters rather than to individual         In UTF modes, quantifiers apply to characters rather than to individual
6214         data units. Thus, for example, \x{100}{2} matches two characters,  each         data  units. Thus, for example, \x{100}{2} matches two characters, each
6215         of which is represented by a two-byte sequence in a UTF-8 string. Simi-         of which is represented by a two-byte sequence in a UTF-8 string. Simi-
6216         larly, \X{3} matches three Unicode extended grapheme clusters, each  of         larly,  \X{3} matches three Unicode extended grapheme clusters, each of
6217         which  may  be  several  data  units long (and they may be of different         which may be several data units long (and  they  may  be  of  different
6218         lengths).         lengths).
6219    
6220         The quantifier {0} is permitted, causing the expression to behave as if         The quantifier {0} is permitted, causing the expression to behave as if
6221         the previous item and the quantifier were not present. This may be use-         the previous item and the quantifier were not present. This may be use-
6222         ful for subpatterns that are referenced as subroutines  from  elsewhere         ful  for  subpatterns that are referenced as subroutines from elsewhere
6223         in the pattern (but see also the section entitled "Defining subpatterns         in the pattern (but see also the section entitled "Defining subpatterns
6224         for use by reference only" below). Items other  than  subpatterns  that         for  use  by  reference only" below). Items other than subpatterns that
6225         have a {0} quantifier are omitted from the compiled pattern.         have a {0} quantifier are omitted from the compiled pattern.
6226    
6227         For  convenience, the three most common quantifiers have single-charac-         For convenience, the three most common quantifiers have  single-charac-
6228         ter abbreviations:         ter abbreviations:
6229    
6230           *    is equivalent to {0,}           *    is equivalent to {0,}
6231           +    is equivalent to {1,}           +    is equivalent to {1,}
6232           ?    is equivalent to {0,1}           ?    is equivalent to {0,1}
6233    
6234         It is possible to construct infinite loops by  following  a  subpattern         It  is  possible  to construct infinite loops by following a subpattern
6235         that can match no characters with a quantifier that has no upper limit,         that can match no characters with a quantifier that has no upper limit,
6236         for example:         for example:
6237    
6238           (a?)*           (a?)*
6239    
6240         Earlier versions of Perl and PCRE used to give an error at compile time         Earlier versions of Perl and PCRE used to give an error at compile time
6241         for  such  patterns. However, because there are cases where this can be         for such patterns. However, because there are cases where this  can  be
6242         useful, such patterns are now accepted, but if any  repetition  of  the         useful,  such  patterns  are now accepted, but if any repetition of the
6243         subpattern  does in fact match no characters, the loop is forcibly bro-         subpattern does in fact match no characters, the loop is forcibly  bro-
6244         ken.         ken.
6245    
6246         By default, the quantifiers are "greedy", that is, they match  as  much         By  default,  the quantifiers are "greedy", that is, they match as much
6247         as  possible  (up  to  the  maximum number of permitted times), without         as possible (up to the maximum  number  of  permitted  times),  without
6248         causing the rest of the pattern to fail. The classic example  of  where         causing  the  rest of the pattern to fail. The classic example of where
6249         this gives problems is in trying to match comments in C programs. These         this gives problems is in trying to match comments in C programs. These
6250         appear between /* and */ and within the comment,  individual  *  and  /         appear  between  /*  and  */ and within the comment, individual * and /
6251         characters  may  appear. An attempt to match C comments by applying the         characters may appear. An attempt to match C comments by  applying  the
6252         pattern         pattern
6253    
6254           /\*.*\*/           /\*.*\*/
# Line 6234  REPETITION Line 6257  REPETITION
6257    
6258           /* first comment */  not comment  /* second comment */           /* first comment */  not comment  /* second comment */
6259    
6260         fails, because it matches the entire string owing to the greediness  of         fails,  because it matches the entire string owing to the greediness of
6261         the .*  item.         the .*  item.
6262    
6263         However,  if  a quantifier is followed by a question mark, it ceases to         However, if a quantifier is followed by a question mark, it  ceases  to
6264         be greedy, and instead matches the minimum number of times possible, so         be greedy, and instead matches the minimum number of times possible, so
6265         the pattern         the pattern
6266    
6267           /\*.*?\*/           /\*.*?\*/
6268    
6269         does  the  right  thing with the C comments. The meaning of the various         does the right thing with the C comments. The meaning  of  the  various
6270         quantifiers is not otherwise changed,  just  the  preferred  number  of         quantifiers  is  not  otherwise  changed,  just the preferred number of
6271         matches.   Do  not  confuse this use of question mark with its use as a         matches.  Do not confuse this use of question mark with its  use  as  a
6272         quantifier in its own right. Because it has two uses, it can  sometimes         quantifier  in its own right. Because it has two uses, it can sometimes
6273         appear doubled, as in         appear doubled, as in
6274    
6275           \d??\d           \d??\d
# Line 6254  REPETITION Line 6277  REPETITION
6277         which matches one digit by preference, but can match two if that is the         which matches one digit by preference, but can match two if that is the
6278         only way the rest of the pattern matches.         only way the rest of the pattern matches.
6279    
6280         If the PCRE_UNGREEDY option is set (an option that is not available  in         If  the PCRE_UNGREEDY option is set (an option that is not available in
6281         Perl),  the  quantifiers are not greedy by default, but individual ones         Perl), the quantifiers are not greedy by default, but  individual  ones
6282         can be made greedy by following them with a  question  mark.  In  other         can  be  made  greedy  by following them with a question mark. In other
6283         words, it inverts the default behaviour.         words, it inverts the default behaviour.
6284    
6285         When  a  parenthesized  subpattern  is quantified with a minimum repeat         When a parenthesized subpattern is quantified  with  a  minimum  repeat
6286         count that is greater than 1 or with a limited maximum, more memory  is         count  that is greater than 1 or with a limited maximum, more memory is
6287         required  for  the  compiled  pattern, in proportion to the size of the         required for the compiled pattern, in proportion to  the  size  of  the
6288         minimum or maximum.         minimum or maximum.
6289    
6290         If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv-         If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv-
6291         alent  to  Perl's  /s) is set, thus allowing the dot to match newlines,         alent to Perl's /s) is set, thus allowing the dot  to  match  newlines,
6292         the pattern is implicitly anchored, because whatever  follows  will  be         the  pattern  is  implicitly anchored, because whatever follows will be
6293         tried  against every character position in the subject string, so there         tried against every character position in the subject string, so  there
6294         is no point in retrying the overall match at  any  position  after  the         is  no  point  in  retrying the overall match at any position after the
6295         first.  PCRE  normally treats such a pattern as though it were preceded         first. PCRE normally treats such a pattern as though it  were  preceded
6296         by \A.         by \A.
6297    
6298         In cases where it is known that the subject  string  contains  no  new-         In  cases  where  it  is known that the subject string contains no new-
6299         lines,  it  is  worth setting PCRE_DOTALL in order to obtain this opti-         lines, it is worth setting PCRE_DOTALL in order to  obtain  this  opti-
6300         mization, or alternatively using ^ to indicate anchoring explicitly.         mization, or alternatively using ^ to indicate anchoring explicitly.
6301    
6302         However, there are some cases where the optimization  cannot  be  used.         However,  there  are  some cases where the optimization cannot be used.
6303         When .*  is inside capturing parentheses that are the subject of a back         When .*  is inside capturing parentheses that are the subject of a back
6304         reference elsewhere in the pattern, a match at the start may fail where         reference elsewhere in the pattern, a match at the start may fail where
6305         a later one succeeds. Consider, for example:         a later one succeeds. Consider, for example:
6306    
6307           (.*)abc\1           (.*)abc\1
6308    
6309         If  the subject is "xyz123abc123" the match point is the fourth charac-         If the subject is "xyz123abc123" the match point is the fourth  charac-
6310         ter. For this reason, such a pattern is not implicitly anchored.         ter. For this reason, such a pattern is not implicitly anchored.
6311    
6312         Another case where implicit anchoring is not applied is when the  lead-         Another  case where implicit anchoring is not applied is when the lead-
6313         ing  .* is inside an atomic group. Once again, a match at the start may         ing .* is inside an atomic group. Once again, a match at the start  may
6314         fail where a later one succeeds. Consider this pattern:         fail where a later one succeeds. Consider this pattern:
6315    
6316           (?>.*?a)b           (?>.*?a)b
6317    
6318         It matches "ab" in the subject "aab". The use of the backtracking  con-         It  matches "ab" in the subject "aab". The use of the backtracking con-
6319         trol verbs (*PRUNE) and (*SKIP) also disable this optimization.         trol verbs (*PRUNE) and (*SKIP) also disable this optimization.
6320    
6321         When a capturing subpattern is repeated, the value captured is the sub-         When a capturing subpattern is repeated, the value captured is the sub-
# Line 6301  REPETITION Line 6324  REPETITION
6324           (tweedle[dume]{3}\s*)+           (tweedle[dume]{3}\s*)+
6325    
6326         has matched "tweedledum tweedledee" the value of the captured substring         has matched "tweedledum tweedledee" the value of the captured substring
6327         is  "tweedledee".  However,  if there are nested capturing subpatterns,         is "tweedledee". However, if there are  nested  capturing  subpatterns,
6328         the corresponding captured values may have been set in previous  itera-         the  corresponding captured values may have been set in previous itera-
6329         tions. For example, after         tions. For example, after
6330    
6331           /(a|(b))+/           /(a|(b))+/
# Line 6312  REPETITION Line 6335  REPETITION
6335    
6336  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS  ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS
6337    
6338         With  both  maximizing ("greedy") and minimizing ("ungreedy" or "lazy")         With both maximizing ("greedy") and minimizing ("ungreedy"  or  "lazy")
6339         repetition, failure of what follows normally causes the  repeated  item         repetition,  failure  of what follows normally causes the repeated item
6340         to  be  re-evaluated to see if a different number of repeats allows the         to be re-evaluated to see if a different number of repeats  allows  the
6341         rest of the pattern to match. Sometimes it is useful to  prevent  this,         rest  of  the pattern to match. Sometimes it is useful to prevent this,
6342         either  to  change the nature of the match, or to cause it fail earlier         either to change the nature of the match, or to cause it  fail  earlier
6343         than it otherwise might, when the author of the pattern knows there  is         than  it otherwise might, when the author of the pattern knows there is
6344         no point in carrying on.         no point in carrying on.
6345    
6346         Consider,  for  example, the pattern \d+foo when applied to the subject         Consider, for example, the pattern \d+foo when applied to  the  subject
6347         line         line
6348    
6349           123456bar           123456bar
6350    
6351         After matching all 6 digits and then failing to match "foo", the normal         After matching all 6 digits and then failing to match "foo", the normal
6352         action  of  the matcher is to try again with only 5 digits matching the         action of the matcher is to try again with only 5 digits  matching  the
6353         \d+ item, and then with  4,  and  so  on,  before  ultimately  failing.         \d+  item,  and  then  with  4,  and  so on, before ultimately failing.
6354         "Atomic  grouping"  (a  term taken from Jeffrey Friedl's book) provides         "Atomic grouping" (a term taken from Jeffrey  Friedl's  book)  provides
6355         the means for specifying that once a subpattern has matched, it is  not         the  means for specifying that once a subpattern has matched, it is not
6356         to be re-evaluated in this way.         to be re-evaluated in this way.
6357    
6358         If  we  use atomic grouping for the previous example, the matcher gives         If we use atomic grouping for the previous example, the  matcher  gives
6359         up immediately on failing to match "foo" the first time.  The  notation         up  immediately  on failing to match "foo" the first time. The notation
6360         is a kind of special parenthesis, starting with (?> as in this example:         is a kind of special parenthesis, starting with (?> as in this example:
6361    
6362           (?>\d+)foo           (?>\d+)foo
6363    
6364         This  kind  of  parenthesis "locks up" the  part of the pattern it con-         This kind of parenthesis "locks up" the  part of the  pattern  it  con-
6365         tains once it has matched, and a failure further into  the  pattern  is         tains  once  it  has matched, and a failure further into the pattern is
6366         prevented  from  backtracking into it. Backtracking past it to previous         prevented from backtracking into it. Backtracking past it  to  previous
6367         items, however, works as normal.         items, however, works as normal.
6368    
6369         An alternative description is that a subpattern of  this  type  matches         An  alternative  description  is that a subpattern of this type matches
6370         the  string  of  characters  that an identical standalone pattern would         the string of characters that an  identical  standalone  pattern  would
6371         match, if anchored at the current point in the subject string.         match, if anchored at the current point in the subject string.
6372    
6373         Atomic grouping subpatterns are not capturing subpatterns. Simple cases         Atomic grouping subpatterns are not capturing subpatterns. Simple cases
6374         such as the above example can be thought of as a maximizing repeat that         such as the above example can be thought of as a maximizing repeat that
6375         must swallow everything it can. So, while both \d+ and  \d+?  are  pre-         must  swallow  everything  it can. So, while both \d+ and \d+? are pre-
6376         pared  to  adjust  the number of digits they match in order to make the         pared to adjust the number of digits they match in order  to  make  the
6377         rest of the pattern match, (?>\d+) can only match an entire sequence of         rest of the pattern match, (?>\d+) can only match an entire sequence of
6378         digits.         digits.
6379    
6380         Atomic  groups in general can of course contain arbitrarily complicated         Atomic groups in general can of course contain arbitrarily  complicated
6381         subpatterns, and can be nested. However, when  the  subpattern  for  an         subpatterns,  and  can  be  nested. However, when the subpattern for an
6382         atomic group is just a single repeated item, as in the example above, a         atomic group is just a single repeated item, as in the example above, a
6383         simpler notation, called a "possessive quantifier" can  be  used.  This         simpler  notation,  called  a "possessive quantifier" can be used. This
6384         consists  of  an  additional  + character following a quantifier. Using         consists of an additional + character  following  a  quantifier.  Using
6385         this notation, the previous example can be rewritten as         this notation, the previous example can be rewritten as
6386    
6387           \d++foo           \d++foo
# Line 6368  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE Line 6391  ATOMIC GROUPING AND POSSESSIVE QUANTIFIE
6391    
6392           (abc|xyz){2,3}+           (abc|xyz){2,3}+
6393    
6394         Possessive   quantifiers   are   always  greedy;  the  setting  of  the         Possessive  quantifiers  are  always  greedy;  the   setting   of   the
6395         PCRE_UNGREEDY option is ignored. They are a convenient notation for the         PCRE_UNGREEDY option is ignored. They are a convenient notation for the
6396         simpler  forms  of atomic group. However, there is no difference in the         simpler forms of atomic group. However, there is no difference  in  the
6397         meaning of a possessive quantifier and  the  equivalent  atomic  group,         meaning  of  a  possessive  quantifier and the equivalent atomic group,
6398         though  there  may  be a performance difference; possessive quantifiers         though there may be a performance  difference;  possessive  quantifiers
6399         should be slightly faster.         should be slightly faster.
6400    
6401         The possessive quantifier syntax is an extension to the Perl  5.8  syn-         The  possessive  quantifier syntax is an extension to the Perl 5.8 syn-
6402         tax.   Jeffrey  Friedl  originated the idea (and the name) in the first         tax.  Jeffrey Friedl originated the idea (and the name)  in  the  first
6403         edition of his book. Mike McCloskey liked it, so implemented it when he         edition of his book. Mike McCloskey liked it, so implemented it when he
6404         built  Sun's Java package, and PCRE copied it from there. It ultimately         built Sun's Java package, and PCRE copied it from there. It  ultimately
6405         found its way into Perl at release 5.10.         found its way into Perl at release 5.10.
6406    
6407         PCRE has an optimization that automatically "possessifies" certain sim-         PCRE has an optimization that automatically "possessifies" certain sim-
6408         ple  pattern  constructs.  For  example, the sequence A+B is treated as         ple pattern constructs. For example, the sequence  A+B  is  treated  as
6409         A++B because there is no point in backtracking into a sequence  of  A's         A++B  because  there is no point in backtracking into a sequence of A's
6410         when B must follow.         when B must follow.
6411    
6412         When  a  pattern  contains an unlimited repeat inside a subpattern that         When a pattern contains an unlimited repeat inside  a  subpattern  that
6413         can itself be repeated an unlimited number of  times,  the  use  of  an         can  itself  be  repeated  an  unlimited number of times, the use of an
6414         atomic  group  is  the  only way to avoid some failing matches taking a         atomic group is the only way to avoid some  failing  matches  taking  a
6415         very long time indeed. The pattern         very long time indeed. The pattern
6416    
6417           (\D+|<\d+>)*[!?]           (\D+|<\d+>)*[!?]
6418    
6419         matches an unlimited number of substrings that either consist  of  non-         matches  an  unlimited number of substrings that either consist of non-
6420         digits,  or  digits  enclosed in <>, followed by either ! or ?. When it         digits, or digits enclosed in <>, followed by either ! or  ?.  When  it
6421         matches, it runs quickly. However, if it is applied to         matches, it runs quickly. However, if it is applied to
6422    
6423           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
6424    
6425         it takes a long time before reporting  failure.  This  is  because  the         it  takes  a  long  time  before reporting failure. This is because the
6426         string  can be divided between the internal \D+ repeat and the external         string can be divided between the internal \D+ repeat and the  external
6427         * repeat in a large number of ways, and all  have  to  be  tried.  (The         *  repeat  in  a  large  number of ways, and all have to be tried. (The
6428         example  uses  [!?]  rather than a single character at the end, because         example uses [!?] rather than a single character at  the  end,  because
6429         both PCRE and Perl have an optimization that allows  for  fast  failure         both  PCRE  and  Perl have an optimization that allows for fast failure
6430         when  a single character is used. They remember the last single charac-         when a single character is used. They remember the last single  charac-
6431         ter that is required for a match, and fail early if it is  not  present         ter  that  is required for a match, and fail early if it is not present
6432         in  the  string.)  If  the pattern is changed so that it uses an atomic         in the string.) If the pattern is changed so that  it  uses  an  atomic
6433         group, like this:         group, like this:
6434    
6435           ((?>\D+)|<\d+>)*[!?]           ((?>\D+)|<\d+>)*[!?]
# Line 6418  BACK REFERENCES Line 6441  BACK REFERENCES
6441    
6442         Outside a character class, a backslash followed by a digit greater than         Outside a character class, a backslash followed by a digit greater than
6443         0 (and possibly further digits) is a back reference to a capturing sub-         0 (and possibly further digits) is a back reference to a capturing sub-
6444         pattern earlier (that is, to its left) in the pattern,  provided  there         pattern  earlier  (that is, to its left) in the pattern, provided there
6445         have been that many previous capturing left parentheses.         have been that many previous capturing left parentheses.
6446    
6447         However, if the decimal number following the backslash is less than 10,         However, if the decimal number following the backslash is less than 10,
6448         it is always taken as a back reference, and causes  an  error  only  if         it  is  always  taken  as a back reference, and causes an error only if
6449         there  are  not that many capturing left parentheses in the entire pat-         there are not that many capturing left parentheses in the  entire  pat-
6450         tern. In other words, the parentheses that are referenced need  not  be         tern.  In  other words, the parentheses that are referenced need not be
6451         to  the left of the reference for numbers less than 10. A "forward back         to the left of the reference for numbers less than 10. A "forward  back
6452         reference" of this type can make sense when a  repetition  is  involved         reference"  of  this  type can make sense when a repetition is involved
6453         and  the  subpattern to the right has participated in an earlier itera-         and the subpattern to the right has participated in an  earlier  itera-
6454         tion.         tion.
6455    
6456         It is not possible to have a numerical "forward back  reference"  to  a         It  is  not  possible to have a numerical "forward back reference" to a
6457         subpattern  whose  number  is  10  or  more using this syntax because a         subpattern whose number is 10 or  more  using  this  syntax  because  a
6458         sequence such as \50 is interpreted as a character  defined  in  octal.         sequence  such  as  \50 is interpreted as a character defined in octal.
6459         See the subsection entitled "Non-printing characters" above for further         See the subsection entitled "Non-printing characters" above for further
6460         details of the handling of digits following a backslash.  There  is  no         details  of  the  handling of digits following a backslash. There is no
6461         such  problem  when named parentheses are used. A back reference to any         such problem when named parentheses are used. A back reference  to  any
6462         subpattern is possible using named parentheses (see below).         subpattern is possible using named parentheses (see below).
6463    
6464         Another way of avoiding the ambiguity inherent in  the  use  of  digits         Another  way  of  avoiding  the ambiguity inherent in the use of digits
6465         following  a  backslash  is  to use the \g escape sequence. This escape         following a backslash is to use the \g  escape  sequence.  This  escape
6466         must be followed by an unsigned number or a negative number, optionally         must be followed by an unsigned number or a negative number, optionally
6467         enclosed in braces. These examples are all identical:         enclosed in braces. These examples are all identical:
6468    
# Line 6447  BACK REFERENCES Line 6470  BACK REFERENCES
6470           (ring), \g1           (ring), \g1
6471           (ring), \g{1}           (ring), \g{1}
6472    
6473         An  unsigned number specifies an absolute reference without the ambigu-         An unsigned number specifies an absolute reference without the  ambigu-
6474         ity that is present in the older syntax. It is also useful when literal         ity that is present in the older syntax. It is also useful when literal
6475         digits follow the reference. A negative number is a relative reference.         digits follow the reference. A negative number is a relative reference.
6476         Consider this example:         Consider this example:
# Line 6456  BACK REFERENCES Line 6479  BACK REFERENCES
6479    
6480         The sequence \g{-1} is a reference to the most recently started captur-         The sequence \g{-1} is a reference to the most recently started captur-
6481         ing subpattern before \g, that is, is it equivalent to \2 in this exam-         ing subpattern before \g, that is, is it equivalent to \2 in this exam-
6482         ple.  Similarly, \g{-2} would be equivalent to \1. The use of  relative         ple.   Similarly, \g{-2} would be equivalent to \1. The use of relative
6483         references  can  be helpful in long patterns, and also in patterns that         references can be helpful in long patterns, and also in  patterns  that
6484         are created by  joining  together  fragments  that  contain  references         are  created  by  joining  together  fragments  that contain references
6485         within themselves.         within themselves.
6486    
6487         A  back  reference matches whatever actually matched the capturing sub-         A back reference matches whatever actually matched the  capturing  sub-
6488         pattern in the current subject string, rather  than  anything  matching         pattern  in  the  current subject string, rather than anything matching
6489         the subpattern itself (see "Subpatterns as subroutines" below for a way         the subpattern itself (see "Subpatterns as subroutines" below for a way
6490         of doing that). So the pattern         of doing that). So the pattern
6491    
6492           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
6493    
6494         matches "sense and sensibility" and "response and responsibility",  but         matches  "sense and sensibility" and "response and responsibility", but
6495         not  "sense and responsibility". If caseful matching is in force at the         not "sense and responsibility". If caseful matching is in force at  the
6496         time of the back reference, the case of letters is relevant. For  exam-         time  of the back reference, the case of letters is relevant. For exam-
6497         ple,         ple,
6498    
6499           ((?i)rah)\s+\1           ((?i)rah)\s+\1
6500    
6501         matches  "rah  rah"  and  "RAH RAH", but not "RAH rah", even though the         matches "rah rah" and "RAH RAH", but not "RAH  rah",  even  though  the
6502         original capturing subpattern is matched caselessly.         original capturing subpattern is matched caselessly.
6503    
6504         There are several different ways of writing back  references  to  named         There  are  several  different ways of writing back references to named
6505         subpatterns.  The  .NET syntax \k{name} and the Perl syntax \k<name> or         subpatterns. The .NET syntax \k{name} and the Perl syntax  \k<name>  or
6506         \k'name' are supported, as is the Python syntax (?P=name). Perl  5.10's         \k'name'  are supported, as is the Python syntax (?P=name). Perl 5.10's
6507         unified back reference syntax, in which \g can be used for both numeric         unified back reference syntax, in which \g can be used for both numeric
6508         and named references, is also supported. We  could  rewrite  the  above         and  named  references,  is  also supported. We could rewrite the above
6509         example in any of the following ways:         example in any of the following ways:
6510    
6511           (?<p1>(?i)rah)\s+\k<p1>           (?<p1>(?i)rah)\s+\k<p1>
# Line 6490  BACK REFERENCES Line 6513  BACK REFERENCES
6513           (?P<p1>(?i)rah)\s+(?P=p1)           (?P<p1>(?i)rah)\s+(?P=p1)
6514           (?<p1>(?i)rah)\s+\g{p1}           (?<p1>(?i)rah)\s+\g{p1}
6515    
6516         A  subpattern  that  is  referenced  by  name may appear in the pattern         A subpattern that is referenced by  name  may  appear  in  the  pattern
6517         before or after the reference.         before or after the reference.
6518    
6519         There may be more than one back reference to the same subpattern. If  a         There  may be more than one back reference to the same subpattern. If a
6520         subpattern  has  not actually been used in a particular match, any back         subpattern has not actually been used in a particular match,  any  back
6521         references to it always fail by default. For example, the pattern         references to it always fail by default. For example, the pattern
6522    
6523           (a|(bc))\2           (a|(bc))\2
6524    
6525         always fails if it starts to match "a" rather than  "bc".  However,  if         always  fails  if  it starts to match "a" rather than "bc". However, if
6526         the PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back refer-         the PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back refer-
6527         ence to an unset value matches an empty string.         ence to an unset value matches an empty string.
6528    
6529         Because there may be many capturing parentheses in a pattern, all  dig-         Because  there may be many capturing parentheses in a pattern, all dig-
6530         its  following a backslash are taken as part of a potential back refer-         its following a backslash are taken as part of a potential back  refer-
6531         ence number.  If the pattern continues with  a  digit  character,  some         ence  number.   If  the  pattern continues with a digit character, some
6532         delimiter  must  be  used  to  terminate  the  back  reference.  If the         delimiter must  be  used  to  terminate  the  back  reference.  If  the
6533         PCRE_EXTENDED option is set, this can be white  space.  Otherwise,  the         PCRE_EXTENDED  option  is  set, this can be white space. Otherwise, the
6534         \g{ syntax or an empty comment (see "Comments" below) can be used.         \g{ syntax or an empty comment (see "Comments" below) can be used.
6535    
6536     Recursive back references     Recursive back references
6537    
6538         A  back reference that occurs inside the parentheses to which it refers         A back reference that occurs inside the parentheses to which it  refers
6539         fails when the subpattern is first used, so, for example,  (a\1)  never         fails  when  the subpattern is first used, so, for example, (a\1) never
6540         matches.   However,  such references can be useful inside repeated sub-         matches.  However, such references can be useful inside  repeated  sub-
6541         patterns. For example, the pattern         patterns. For example, the pattern
6542    
6543           (a|b\1)+           (a|b\1)+
6544    
6545         matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-         matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-
6546         ation  of  the  subpattern,  the  back  reference matches the character         ation of the subpattern,  the  back  reference  matches  the  character
6547         string corresponding to the previous iteration. In order  for  this  to         string  corresponding  to  the previous iteration. In order for this to
6548         work,  the  pattern must be such that the first iteration does not need         work, the pattern must be such that the first iteration does  not  need
6549         to match the back reference. This can be done using alternation, as  in         to  match the back reference. This can be done using alternation, as in
6550         the example above, or by a quantifier with a minimum of zero.         the example above, or by a quantifier with a minimum of zero.
6551    
6552         Back  references of this type cause the group that they reference to be         Back references of this type cause the group that they reference to  be
6553         treated as an atomic group.  Once the whole group has been  matched,  a         treated  as  an atomic group.  Once the whole group has been matched, a
6554         subsequent  matching  failure cannot cause backtracking into the middle         subsequent matching failure cannot cause backtracking into  the  middle
6555         of the group.         of the group.
6556    
6557    
6558  ASSERTIONS  ASSERTIONS
6559    
6560         An assertion is a test on the characters  following  or  preceding  the         An  assertion  is  a  test on the characters following or preceding the
6561         current  matching  point that does not actually consume any characters.         current matching point that does not actually consume  any  characters.
6562         The simple assertions coded as \b, \B, \A, \G, \Z,  \z,  ^  and  $  are         The  simple  assertions  coded  as  \b, \B, \A, \G, \Z, \z, ^ and $ are
6563         described above.         described above.
6564    
6565         More  complicated  assertions  are  coded as subpatterns. There are two         More complicated assertions are coded as  subpatterns.  There  are  two
6566         kinds: those that look ahead of the current  position  in  the  subject         kinds:  those  that  look  ahead of the current position in the subject
6567         string,  and  those  that  look  behind  it. An assertion subpattern is         string, and those that look  behind  it.  An  assertion  subpattern  is
6568         matched in the normal way, except that it does not  cause  the  current         matched  in  the  normal way, except that it does not cause the current
6569         matching position to be changed.         matching position to be changed.
6570    
6571         Assertion  subpatterns are not capturing subpatterns. If such an asser-         Assertion subpatterns are not capturing subpatterns. If such an  asser-
6572         tion contains capturing subpatterns within it, these  are  counted  for         tion  contains  capturing  subpatterns within it, these are counted for
6573         the  purposes  of numbering the capturing subpatterns in the whole pat-         the purposes of numbering the capturing subpatterns in the  whole  pat-
6574         tern. However, substring capturing is carried  out  only  for  positive         tern.  However,  substring  capturing  is carried out only for positive
6575         assertions. (Perl sometimes, but not always, does do capturing in nega-         assertions. (Perl sometimes, but not always, does do capturing in nega-
6576         tive assertions.)         tive assertions.)
6577    
6578         For compatibility with Perl, assertion  subpatterns  may  be  repeated;         For  compatibility  with  Perl,  assertion subpatterns may be repeated;
6579         though  it  makes  no sense to assert the same thing several times, the         though it makes no sense to assert the same thing  several  times,  the
6580         side effect of capturing parentheses may  occasionally  be  useful.  In         side  effect  of  capturing  parentheses may occasionally be useful. In
6581         practice, there only three cases:         practice, there only three cases:
6582    
6583         (1)  If  the  quantifier  is  {0}, the assertion is never obeyed during         (1) If the quantifier is {0}, the  assertion  is  never  obeyed  during
6584         matching.  However, it may  contain  internal  capturing  parenthesized         matching.   However,  it  may  contain internal capturing parenthesized
6585         groups that are called from elsewhere via the subroutine mechanism.         groups that are called from elsewhere via the subroutine mechanism.
6586    
6587         (2)  If quantifier is {0,n} where n is greater than zero, it is treated         (2) If quantifier is {0,n} where n is greater than zero, it is  treated
6588         as if it were {0,1}. At run time, the rest  of  the  pattern  match  is         as  if  it  were  {0,1}.  At run time, the rest of the pattern match is
6589         tried with and without the assertion, the order depending on the greed-         tried with and without the assertion, the order depending on the greed-
6590         iness of the quantifier.         iness of the quantifier.
6591    
6592         (3) If the minimum repetition is greater than zero, the  quantifier  is         (3)  If  the minimum repetition is greater than zero, the quantifier is
6593         ignored.   The  assertion  is  obeyed just once when encountered during         ignored.  The assertion is obeyed just  once  when  encountered  during
6594         matching.         matching.
6595    
6596     Lookahead assertions     Lookahead assertions
# Line 6577  ASSERTIONS Line 6600  ASSERTIONS
6600    
6601           \w+(?=;)           \w+(?=;)
6602    
6603         matches  a word followed by a semicolon, but does not include the semi-         matches a word followed by a semicolon, but does not include the  semi-
6604         colon in the match, and         colon in the match, and
6605    
6606           foo(?!bar)           foo(?!bar)
6607    
6608         matches any occurrence of "foo" that is not  followed  by  "bar".  Note         matches  any  occurrence  of  "foo" that is not followed by "bar". Note
6609         that the apparently similar pattern         that the apparently similar pattern
6610    
6611           (?!foo)bar           (?!foo)bar
6612    
6613         does  not  find  an  occurrence  of "bar" that is preceded by something         does not find an occurrence of "bar"  that  is  preceded  by  something
6614         other than "foo"; it finds any occurrence of "bar" whatsoever,  because         other  than "foo"; it finds any occurrence of "bar" whatsoever, because
6615         the assertion (?!foo) is always true when the next three characters are         the assertion (?!foo) is always true when the next three characters are
6616         "bar". A lookbehind assertion is needed to achieve the other effect.         "bar". A lookbehind assertion is needed to achieve the other effect.
6617    
6618         If you want to force a matching failure at some point in a pattern, the         If you want to force a matching failure at some point in a pattern, the
6619         most  convenient  way  to  do  it  is with (?!) because an empty string         most convenient way to do it is  with  (?!)  because  an  empty  string
6620         always matches, so an assertion that requires there not to be an  empty         always  matches, so an assertion that requires there not to be an empty
6621         string must always fail.  The backtracking control verb (*FAIL) or (*F)         string must always fail.  The backtracking control verb (*FAIL) or (*F)
6622         is a synonym for (?!).         is a synonym for (?!).
6623    
6624     Lookbehind assertions     Lookbehind assertions
6625    
6626         Lookbehind assertions start with (?<= for positive assertions and  (?<!         Lookbehind  assertions start with (?<= for positive assertions and (?<!
6627         for negative assertions. For example,         for negative assertions. For example,
6628    
6629           (?<!foo)bar           (?<!foo)bar
6630    
6631         does  find  an  occurrence  of "bar" that is not preceded by "foo". The         does find an occurrence of "bar" that is not  preceded  by  "foo".  The
6632         contents of a lookbehind assertion are restricted  such  that  all  the         contents  of  a  lookbehind  assertion are restricted such that all the
6633         strings it matches must have a fixed length. However, if there are sev-         strings it matches must have a fixed length. However, if there are sev-
6634         eral top-level alternatives, they do not all  have  to  have  the  same         eral  top-level  alternatives,  they  do  not all have to have the same
6635         fixed length. Thus         fixed length. Thus
6636    
6637           (?<=bullock|donkey)           (?<=bullock|donkey)
# Line 6617  ASSERTIONS Line 6640  ASSERTIONS
6640    
6641           (?<!dogs?|cats?)           (?<!dogs?|cats?)
6642    
6643         causes  an  error at compile time. Branches that match different length         causes an error at compile time. Branches that match  different  length
6644         strings are permitted only at the top level of a lookbehind  assertion.         strings  are permitted only at the top level of a lookbehind assertion.
6645         This is an extension compared with Perl, which requires all branches to         This is an extension compared with Perl, which requires all branches to
6646         match the same length of string. An assertion such as         match the same length of string. An assertion such as
6647    
6648           (?<=ab(c|de))           (?<=ab(c|de))
6649    
6650         is not permitted, because its single top-level  branch  can  match  two         is  not  permitted,  because  its single top-level branch can match two
6651         different lengths, but it is acceptable to PCRE if rewritten to use two         different lengths, but it is acceptable to PCRE if rewritten to use two
6652         top-level branches:         top-level branches:
6653    
6654           (?<=abc|abde)           (?<=abc|abde)
6655    
6656         In some cases, the escape sequence \K (see above) can be  used  instead         In  some  cases, the escape sequence \K (see above) can be used instead
6657         of a lookbehind assertion to get round the fixed-length restriction.         of a lookbehind assertion to get round the fixed-length restriction.
6658    
6659         The  implementation  of lookbehind assertions is, for each alternative,         The implementation of lookbehind assertions is, for  each  alternative,
6660         to temporarily move the current position back by the fixed  length  and         to  temporarily  move the current position back by the fixed length and
6661         then try to match. If there are insufficient characters before the cur-         then try to match. If there are insufficient characters before the cur-
6662         rent position, the assertion fails.         rent position, the assertion fails.
6663    
6664         In a UTF mode, PCRE does not allow the \C escape (which matches a  sin-         In  a UTF mode, PCRE does not allow the \C escape (which matches a sin-
6665         gle  data  unit even in a UTF mode) to appear in lookbehind assertions,         gle data unit even in a UTF mode) to appear in  lookbehind  assertions,
6666         because it makes it impossible to calculate the length of  the  lookbe-         because  it  makes it impossible to calculate the length of the lookbe-
6667         hind.  The \X and \R escapes, which can match different numbers of data         hind. The \X and \R escapes, which can match different numbers of  data
6668         units, are also not permitted.         units, are also not permitted.
6669    
6670         "Subroutine" calls (see below) such as (?2) or (?&X) are  permitted  in         "Subroutine"  calls  (see below) such as (?2) or (?&X) are permitted in
6671         lookbehinds,  as  long as the subpattern matches a fixed-length string.         lookbehinds, as long as the subpattern matches a  fixed-length  string.
6672         Recursion, however, is not supported.         Recursion, however, is not supported.
6673    
6674         Possessive quantifiers can  be  used  in  conjunction  with  lookbehind         Possessive  quantifiers  can  be  used  in  conjunction with lookbehind
6675         assertions to specify efficient matching of fixed-length strings at the         assertions to specify efficient matching of fixed-length strings at the
6676         end of subject strings. Consider a simple pattern such as         end of subject strings. Consider a simple pattern such as
6677    
6678           abcd$           abcd$
6679    
6680         when applied to a long string that does  not  match.  Because  matching         when  applied  to  a  long string that does not match. Because matching
6681         proceeds from left to right, PCRE will look for each "a" in the subject         proceeds from left to right, PCRE will look for each "a" in the subject
6682         and then see if what follows matches the rest of the  pattern.  If  the         and  then  see  if what follows matches the rest of the pattern. If the
6683         pattern is specified as         pattern is specified as
6684    
6685           ^.*abcd$           ^.*abcd$
6686    
6687         the  initial .* matches the entire string at first, but when this fails         the initial .* matches the entire string at first, but when this  fails
6688         (because there is no following "a"), it backtracks to match all but the         (because there is no following "a"), it backtracks to match all but the
6689         last  character,  then all but the last two characters, and so on. Once         last character, then all but the last two characters, and so  on.  Once
6690         again the search for "a" covers the entire string, from right to  left,         again  the search for "a" covers the entire string, from right to left,
6691         so we are no better off. However, if the pattern is written as         so we are no better off. However, if the pattern is written as
6692    
6693           ^.*+(?<=abcd)           ^.*+(?<=abcd)
6694    
6695         there  can  be  no backtracking for the .*+ item; it can match only the         there can be no backtracking for the .*+ item; it can  match  only  the
6696         entire string. The subsequent lookbehind assertion does a  single  test         entire  string.  The subsequent lookbehind assertion does a single test
6697         on  the last four characters. If it fails, the match fails immediately.         on the last four characters. If it fails, the match fails  immediately.
6698         For long strings, this approach makes a significant difference  to  the         For  long  strings, this approach makes a significant difference to the
6699         processing time.         processing time.
6700    
6701     Using multiple assertions     Using multiple assertions
# Line 6681  ASSERTIONS Line 6704  ASSERTIONS
6704    
6705           (?<=\d{3})(?<!999)foo           (?<=\d{3})(?<!999)foo
6706    
6707         matches  "foo" preceded by three digits that are not "999". Notice that         matches "foo" preceded by three digits that are not "999". Notice  that
6708         each of the assertions is applied independently at the  same  point  in         each  of  the  assertions is applied independently at the same point in
6709         the  subject  string.  First  there  is a check that the previous three         the subject string. First there is a  check  that  the  previous  three
6710         characters are all digits, and then there is  a  check  that  the  same         characters  are  all  digits,  and  then there is a check that the same
6711         three characters are not "999".  This pattern does not match "foo" pre-         three characters are not "999".  This pattern does not match "foo" pre-
6712         ceded by six characters, the first of which are  digits  and  the  last         ceded  by  six  characters,  the first of which are digits and the last
6713         three  of  which  are not "999". For example, it doesn't match "123abc-         three of which are not "999". For example, it  doesn't  match  "123abc-
6714         foo". A pattern to do that is         foo". A pattern to do that is
6715    
6716           (?<=\d{3}...)(?<!999)foo           (?<=\d{3}...)(?<!999)foo
6717    
6718         This time the first assertion looks at the  preceding  six  characters,         This  time  the  first assertion looks at the preceding six characters,
6719         checking that the first three are digits, and then the second assertion         checking that the first three are digits, and then the second assertion
6720         checks that the preceding three characters are not "999".         checks that the preceding three characters are not "999".
6721    
# Line 6700  ASSERTIONS Line 6723  ASSERTIONS
6723    
6724           (?<=(?<!foo)bar)baz           (?<=(?<!foo)bar)baz
6725    
6726         matches an occurrence of "baz" that is preceded by "bar" which in  turn         matches  an occurrence of "baz" that is preceded by "bar" which in turn
6727         is not preceded by "foo", while         is not preceded by "foo", while
6728    
6729           (?<=\d{3}(?!999)...)foo           (?<=\d{3}(?!999)...)foo
6730    
6731         is  another pattern that matches "foo" preceded by three digits and any         is another pattern that matches "foo" preceded by three digits and  any
6732         three characters that are not "999".         three characters that are not "999".
6733    
6734    
6735  CONDITIONAL SUBPATTERNS  CONDITIONAL SUBPATTERNS
6736    
6737         It is possible to cause the matching process to obey a subpattern  con-         It  is possible to cause the matching process to obey a subpattern con-
6738         ditionally  or to choose between two alternative subpatterns, depending         ditionally or to choose between two alternative subpatterns,  depending
6739         on the result of an assertion, or whether a specific capturing  subpat-         on  the result of an assertion, or whether a specific capturing subpat-
6740         tern  has  already  been matched. The two possible forms of conditional         tern has already been matched. The two possible  forms  of  conditional
6741         subpattern are:         subpattern are:
6742    
6743           (?(condition)yes-pattern)           (?(condition)yes-pattern)
6744           (?(condition)yes-pattern|no-pattern)           (?(condition)yes-pattern|no-pattern)
6745    
6746         If the condition is satisfied, the yes-pattern is used;  otherwise  the         If  the  condition is satisfied, the yes-pattern is used; otherwise the
6747         no-pattern  (if  present)  is used. If there are more than two alterna-         no-pattern (if present) is used. If there are more  than  two  alterna-
6748         tives in the subpattern, a compile-time error occurs. Each of  the  two         tives  in  the subpattern, a compile-time error occurs. Each of the two
6749         alternatives may itself contain nested subpatterns of any form, includ-         alternatives may itself contain nested subpatterns of any form, includ-
6750         ing  conditional  subpatterns;  the  restriction  to  two  alternatives         ing  conditional  subpatterns;  the  restriction  to  two  alternatives
6751         applies only at the level of the condition. This pattern fragment is an         applies only at the level of the condition. This pattern fragment is an
# Line 6731  CONDITIONAL SUBPATTERNS Line 6754  CONDITIONAL SUBPATTERNS
6754           (?(1) (A|B|C) | (D | (?(2)E|F) | E) )           (?(1) (A|B|C) | (D | (?(2)E|F) | E) )
6755    
6756    
6757         There are four kinds of condition: references  to  subpatterns,  refer-         There  are  four  kinds of condition: references to subpatterns, refer-
6758         ences to recursion, a pseudo-condition called DEFINE, and assertions.         ences to recursion, a pseudo-condition called DEFINE, and assertions.
6759    
6760     Checking for a used subpattern by number     Checking for a used subpattern by number
6761    
6762         If  the  text between the parentheses consists of a sequence of digits,         If the text between the parentheses consists of a sequence  of  digits,
6763         the condition is true if a capturing subpattern of that number has pre-         the condition is true if a capturing subpattern of that number has pre-
6764         viously  matched.  If  there is more than one capturing subpattern with         viously matched. If there is more than one  capturing  subpattern  with
6765         the same number (see the earlier  section  about  duplicate  subpattern         the  same  number  (see  the earlier section about duplicate subpattern
6766         numbers),  the condition is true if any of them have matched. An alter-         numbers), the condition is true if any of them have matched. An  alter-
6767         native notation is to precede the digits with a plus or minus sign.  In         native  notation is to precede the digits with a plus or minus sign. In
6768         this  case, the subpattern number is relative rather than absolute. The         this case, the subpattern number is relative rather than absolute.  The
6769         most recently opened parentheses can be referenced by (?(-1), the  next         most  recently opened parentheses can be referenced by (?(-1), the next
6770         most  recent  by (?(-2), and so on. Inside loops it can also make sense         most recent by (?(-2), and so on. Inside loops it can also  make  sense
6771         to refer to subsequent groups. The next parentheses to be opened can be         to refer to subsequent groups. The next parentheses to be opened can be
6772         referenced  as (?(+1), and so on. (The value zero in any of these forms         referenced as (?(+1), and so on. (The value zero in any of these  forms
6773         is not used; it provokes a compile-time error.)         is not used; it provokes a compile-time error.)
6774    
6775         Consider the following pattern, which  contains  non-significant  white         Consider  the  following  pattern, which contains non-significant white
6776         space to make it more readable (assume the PCRE_EXTENDED option) and to         space to make it more readable (assume the PCRE_EXTENDED option) and to
6777         divide it into three parts for ease of discussion:         divide it into three parts for ease of discussion:
6778    
6779           ( \( )?    [^()]+    (?(1) \) )           ( \( )?    [^()]+    (?(1) \) )
6780    
6781         The first part matches an optional opening  parenthesis,  and  if  that         The  first  part  matches  an optional opening parenthesis, and if that
6782         character is present, sets it as the first captured substring. The sec-         character is present, sets it as the first captured substring. The sec-
6783         ond part matches one or more characters that are not  parentheses.  The         ond  part  matches one or more characters that are not parentheses. The
6784         third  part  is  a conditional subpattern that tests whether or not the         third part is a conditional subpattern that tests whether  or  not  the
6785         first set of parentheses matched. If they  did,  that  is,  if  subject         first  set  of  parentheses  matched.  If they did, that is, if subject
6786         started  with an opening parenthesis, the condition is true, and so the         started with an opening parenthesis, the condition is true, and so  the
6787         yes-pattern is executed and a closing parenthesis is  required.  Other-         yes-pattern  is  executed and a closing parenthesis is required. Other-
6788         wise,  since no-pattern is not present, the subpattern matches nothing.         wise, since no-pattern is not present, the subpattern matches  nothing.
6789         In other words, this pattern matches  a  sequence  of  non-parentheses,         In  other  words,  this  pattern matches a sequence of non-parentheses,
6790         optionally enclosed in parentheses.         optionally enclosed in parentheses.
6791    
6792         If  you  were  embedding  this pattern in a larger one, you could use a         If you were embedding this pattern in a larger one,  you  could  use  a
6793         relative reference:         relative reference:
6794    
6795           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...           ...other stuff... ( \( )?    [^()]+    (?(-1) \) ) ...
6796    
6797         This makes the fragment independent of the parentheses  in  the  larger         This  makes  the  fragment independent of the parentheses in the larger
6798         pattern.         pattern.
6799    
6800     Checking for a used subpattern by name     Checking for a used subpattern by name
6801    
6802         Perl  uses  the  syntax  (?(<name>)...) or (?('name')...) to test for a         Perl uses the syntax (?(<name>)...) or (?('name')...)  to  test  for  a
6803         used subpattern by name. For compatibility  with  earlier  versions  of         used  subpattern  by  name.  For compatibility with earlier versions of
6804         PCRE,  which  had this facility before Perl, the syntax (?(name)...) is         PCRE, which had this facility before Perl, the syntax  (?(name)...)  is
6805         also recognized.         also recognized.
6806    
6807         Rewriting the above example to use a named subpattern gives this:         Rewriting the above example to use a named subpattern gives this:
6808    
6809           (?<OPEN> \( )?    [^()]+    (?(<OPEN>) \) )           (?<OPEN> \( )?    [^()]+    (?(<OPEN>) \) )
6810    
6811         If the name used in a condition of this kind is a duplicate,  the  test         If  the  name used in a condition of this kind is a duplicate, the test
6812         is  applied to all subpatterns of the same name, and is true if any one         is applied to all subpatterns of the same name, and is true if any  one
6813         of them has matched.         of them has matched.
6814    
6815     Checking for pattern recursion     Checking for pattern recursion
6816    
6817         If the condition is the string (R), and there is no subpattern with the         If the condition is the string (R), and there is no subpattern with the
6818         name  R, the condition is true if a recursive call to the whole pattern         name R, the condition is true if a recursive call to the whole  pattern
6819         or any subpattern has been made. If digits or a name preceded by amper-         or any subpattern has been made. If digits or a name preceded by amper-
6820         sand follow the letter R, for example:         sand follow the letter R, for example:
6821    
# Line 6800  CONDITIONAL SUBPATTERNS Line 6823  CONDITIONAL SUBPATTERNS
6823    
6824         the condition is true if the most recent recursion is into a subpattern         the condition is true if the most recent recursion is into a subpattern
6825         whose number or name is given. This condition does not check the entire         whose number or name is given. This condition does not check the entire
6826         recursion  stack.  If  the  name  used in a condition of this kind is a         recursion stack. If the name used in a condition  of  this  kind  is  a
6827         duplicate, the test is applied to all subpatterns of the same name, and         duplicate, the test is applied to all subpatterns of the same name, and
6828         is true if any one of them is the most recent recursion.         is true if any one of them is the most recent recursion.
6829    
6830         At  "top  level",  all  these recursion test conditions are false.  The         At "top level", all these recursion test  conditions  are  false.   The
6831         syntax for recursive patterns is described below.         syntax for recursive patterns is described below.
6832    
6833     Defining subpatterns for use by reference only     Defining subpatterns for use by reference only
6834    
6835         If the condition is the string (DEFINE), and  there  is  no  subpattern         If  the  condition  is  the string (DEFINE), and there is no subpattern
6836         with  the  name  DEFINE,  the  condition is always false. In this case,         with the name DEFINE, the condition is  always  false.  In  this  case,
6837         there may be only one alternative  in  the  subpattern.  It  is  always         there  may  be  only  one  alternative  in the subpattern. It is always
6838         skipped  if  control  reaches  this  point  in the pattern; the idea of         skipped if control reaches this point  in  the  pattern;  the  idea  of
6839         DEFINE is that it can be used to define subroutines that can be  refer-         DEFINE  is that it can be used to define subroutines that can be refer-
6840         enced  from elsewhere. (The use of subroutines is described below.) For         enced from elsewhere. (The use of subroutines is described below.)  For
6841         example, a pattern to match an IPv4 address  such  as  "192.168.23.245"         example,  a  pattern  to match an IPv4 address such as "192.168.23.245"
6842         could be written like this (ignore white space and line breaks):         could be written like this (ignore white space and line breaks):
6843    
6844           (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )           (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
6845           \b (?&byte) (\.(?&byte)){3} \b           \b (?&byte) (\.(?&byte)){3} \b
6846    
6847         The  first part of the pattern is a DEFINE group inside which a another         The first part of the pattern is a DEFINE group inside which a  another
6848         group named "byte" is defined. This matches an individual component  of         group  named "byte" is defined. This matches an individual component of
6849         an  IPv4  address  (a number less than 256). When matching takes place,         an IPv4 address (a number less than 256). When  matching  takes  place,
6850         this part of the pattern is skipped because DEFINE acts  like  a  false         this  part  of  the pattern is skipped because DEFINE acts like a false
6851         condition.  The  rest of the pattern uses references to the named group         condition. The rest of the pattern uses references to the  named  group
6852         to match the four dot-separated components of an IPv4 address,  insist-         to  match the four dot-separated components of an IPv4 address, insist-
6853         ing on a word boundary at each end.         ing on a word boundary at each end.
6854    
6855     Assertion conditions     Assertion conditions
6856    
6857         If  the  condition  is  not  in any of the above formats, it must be an         If the condition is not in any of the above  formats,  it  must  be  an
6858         assertion.  This may be a positive or negative lookahead or  lookbehind         assertion.   This may be a positive or negative lookahead or lookbehind
6859         assertion.  Consider  this  pattern,  again  containing non-significant         assertion. Consider  this  pattern,  again  containing  non-significant
6860         white space, and with the two alternatives on the second line:         white space, and with the two alternatives on the second line:
6861    
6862           (?(?=[^a-z]*[a-z])           (?(?=[^a-z]*[a-z])
6863           \d{2}-[a-z]{3}-\d{2}  |  \d{2}-\d{2}-\d{2} )           \d{2}-[a-z]{3}-\d{2}  |  \d{2}-\d{2}-\d{2} )
6864    
6865         The condition  is  a  positive  lookahead  assertion  that  matches  an         The  condition  is  a  positive  lookahead  assertion  that  matches an
6866         optional  sequence of non-letters followed by a letter. In other words,         optional sequence of non-letters followed by a letter. In other  words,
6867         it tests for the presence of at least one letter in the subject.  If  a         it  tests  for the presence of at least one letter in the subject. If a
6868         letter  is found, the subject is matched against the first alternative;         letter is found, the subject is matched against the first  alternative;
6869         otherwise it is  matched  against  the  second.  This  pattern  matches         otherwise  it  is  matched  against  the  second.  This pattern matches
6870         strings  in  one  of the two forms dd-aaa-dd or dd-dd-dd, where aaa are         strings in one of the two forms dd-aaa-dd or dd-dd-dd,  where  aaa  are
6871         letters and dd are digits.         letters and dd are digits.
6872    
6873    
# Line 6853  COMMENTS Line 6876  COMMENTS
6876         There are two ways of including comments in patterns that are processed         There are two ways of including comments in patterns that are processed
6877         by PCRE. In both cases, the start of the comment must not be in a char-         by PCRE. In both cases, the start of the comment must not be in a char-
6878         acter class, nor in the middle of any other sequence of related charac-         acter class, nor in the middle of any other sequence of related charac-
6879         ters  such  as  (?: or a subpattern name or number. The characters that         ters such as (?: or a subpattern name or number.  The  characters  that
6880         make up a comment play no part in the pattern matching.         make up a comment play no part in the pattern matching.
6881    
6882         The sequence (?# marks the start of a comment that continues up to  the         The  sequence (?# marks the start of a comment that continues up to the
6883         next  closing parenthesis. Nested parentheses are not permitted. If the         next closing parenthesis. Nested parentheses are not permitted. If  the
6884         PCRE_EXTENDED option is set, an unescaped # character also introduces a         PCRE_EXTENDED option is set, an unescaped # character also introduces a
6885         comment,  which  in  this  case continues to immediately after the next         comment, which in this case continues to  immediately  after  the  next
6886         newline character or character sequence in the pattern.  Which  charac-         newline  character  or character sequence in the pattern. Which charac-
6887         ters are interpreted as newlines is controlled by the options passed to         ters are interpreted as newlines is controlled by the options passed to
6888         a compiling function or by a special sequence at the start of the  pat-         a  compiling function or by a special sequence at the start of the pat-
6889         tern, as described in the section entitled "Newline conventions" above.         tern, as described in the section entitled "Newline conventions" above.
6890         Note that the end of this type of comment is a literal newline sequence         Note that the end of this type of comment is a literal newline sequence
6891         in  the pattern; escape sequences that happen to represent a newline do         in the pattern; escape sequences that happen to represent a newline  do
6892         not count. For example, consider this  pattern  when  PCRE_EXTENDED  is         not  count.  For  example,  consider this pattern when PCRE_EXTENDED is
6893         set, and the default newline convention is in force:         set, and the default newline convention is in force:
6894    
6895           abc #comment \n still comment           abc #comment \n still comment
6896    
6897         On  encountering  the  # character, pcre_compile() skips along, looking         On encountering the # character, pcre_compile()  skips  along,  looking
6898         for a newline in the pattern. The sequence \n is still literal at  this         for  a newline in the pattern. The sequence \n is still literal at this
6899         stage,  so  it does not terminate the comment. Only an actual character         stage, so it does not terminate the comment. Only an  actual  character
6900         with the code value 0x0a (the default newline) does so.         with the code value 0x0a (the default newline) does so.
6901    
6902    
6903  RECURSIVE PATTERNS  RECURSIVE PATTERNS
6904    
6905         Consider the problem of matching a string in parentheses, allowing  for         Consider  the problem of matching a string in parentheses, allowing for
6906         unlimited  nested  parentheses.  Without the use of recursion, the best         unlimited nested parentheses. Without the use of  recursion,  the  best
6907         that can be done is to use a pattern that  matches  up  to  some  fixed         that  can  be  done  is  to use a pattern that matches up to some fixed
6908         depth  of  nesting.  It  is not possible to handle an arbitrary nesting         depth of nesting. It is not possible to  handle  an  arbitrary  nesting
6909         depth.         depth.
6910    
6911         For some time, Perl has provided a facility that allows regular expres-         For some time, Perl has provided a facility that allows regular expres-
6912         sions  to recurse (amongst other things). It does this by interpolating         sions to recurse (amongst other things). It does this by  interpolating
6913         Perl code in the expression at run time, and the code can refer to  the         Perl  code in the expression at run time, and the code can refer to the
6914         expression itself. A Perl pattern using code interpolation to solve the         expression itself. A Perl pattern using code interpolation to solve the
6915         parentheses problem can be created like this:         parentheses problem can be created like this:
6916    
# Line 6897  RECURSIVE PATTERNS Line 6920  RECURSIVE PATTERNS
6920         refers recursively to the pattern in which it appears.         refers recursively to the pattern in which it appears.
6921    
6922         Obviously, PCRE cannot support the interpolation of Perl code. Instead,         Obviously, PCRE cannot support the interpolation of Perl code. Instead,
6923         it supports special syntax for recursion of  the  entire  pattern,  and         it  supports  special  syntax  for recursion of the entire pattern, and
6924         also  for  individual  subpattern  recursion. After its introduction in         also for individual subpattern recursion.  After  its  introduction  in
6925         PCRE and Python, this kind of  recursion  was  subsequently  introduced         PCRE  and  Python,  this  kind of recursion was subsequently introduced
6926         into Perl at release 5.10.         into Perl at release 5.10.
6927    
6928         A  special  item  that consists of (? followed by a number greater than         A special item that consists of (? followed by a  number  greater  than
6929         zero and a closing parenthesis is a recursive subroutine  call  of  the         zero  and  a  closing parenthesis is a recursive subroutine call of the
6930         subpattern  of  the  given  number, provided that it occurs inside that         subpattern of the given number, provided that  it  occurs  inside  that
6931         subpattern. (If not, it is a non-recursive subroutine  call,  which  is         subpattern.  (If  not,  it is a non-recursive subroutine call, which is
6932         described  in  the  next  section.)  The special item (?R) or (?0) is a         described in the next section.) The special item  (?R)  or  (?0)  is  a
6933         recursive call of the entire regular expression.         recursive call of the entire regular expression.
6934    
6935         This PCRE pattern solves the nested  parentheses  problem  (assume  the         This  PCRE  pattern  solves  the nested parentheses problem (assume the
6936         PCRE_EXTENDED option is set so that white space is ignored):         PCRE_EXTENDED option is set so that white space is ignored):
6937    
6938           \( ( [^()]++ | (?R) )* \)           \( ( [^()]++ | (?R) )* \)
6939    
6940         First  it matches an opening parenthesis. Then it matches any number of         First it matches an opening parenthesis. Then it matches any number  of
6941         substrings which can either be a  sequence  of  non-parentheses,  or  a         substrings  which  can  either  be  a sequence of non-parentheses, or a
6942         recursive  match  of the pattern itself (that is, a correctly parenthe-         recursive match of the pattern itself (that is, a  correctly  parenthe-
6943         sized substring).  Finally there is a closing parenthesis. Note the use         sized substring).  Finally there is a closing parenthesis. Note the use
6944         of a possessive quantifier to avoid backtracking into sequences of non-         of a possessive quantifier to avoid backtracking into sequences of non-
6945         parentheses.         parentheses.
6946    
6947         If this were part of a larger pattern, you would not  want  to  recurse         If  this  were  part of a larger pattern, you would not want to recurse
6948         the entire pattern, so instead you could use this:         the entire pattern, so instead you could use this:
6949    
6950           ( \( ( [^()]++ | (?1) )* \) )           ( \( ( [^()]++ | (?1) )* \) )
6951    
6952         We  have  put the pattern into parentheses, and caused the recursion to         We have put the pattern into parentheses, and caused the  recursion  to
6953         refer to them instead of the whole pattern.         refer to them instead of the whole pattern.
6954    
6955         In a larger pattern,  keeping  track  of  parenthesis  numbers  can  be         In  a  larger  pattern,  keeping  track  of  parenthesis numbers can be
6956         tricky.  This is made easier by the use of relative references. Instead         tricky. This is made easier by the use of relative references.  Instead
6957         of (?1) in the pattern above you can write (?-2) to refer to the second         of (?1) in the pattern above you can write (?-2) to refer to the second
6958         most  recently  opened  parentheses  preceding  the recursion. In other         most recently opened parentheses  preceding  the  recursion.  In  other
6959         words, a negative number counts capturing  parentheses  leftwards  from         words,  a  negative  number counts capturing parentheses leftwards from
6960         the point at which it is encountered.         the point at which it is encountered.
6961    
6962         It  is  also  possible  to refer to subsequently opened parentheses, by         It is also possible to refer to  subsequently  opened  parentheses,  by
6963         writing references such as (?+2). However, these  cannot  be  recursive         writing  references  such  as (?+2). However, these cannot be recursive
6964         because  the  reference  is  not inside the parentheses that are refer-         because the reference is not inside the  parentheses  that  are  refer-
6965         enced. They are always non-recursive subroutine calls, as described  in         enced.  They are always non-recursive subroutine calls, as described in
6966         the next section.         the next section.
6967    
6968         An  alternative  approach is to use named parentheses instead. The Perl         An alternative approach is to use named parentheses instead.  The  Perl
6969         syntax for this is (?&name); PCRE's earlier syntax  (?P>name)  is  also         syntax  for  this  is (?&name); PCRE's earlier syntax (?P>name) is also
6970         supported. We could rewrite the above example as follows:         supported. We could rewrite the above example as follows:
6971    
6972           (?<pn> \( ( [^()]++ | (?&pn) )* \) )           (?<pn> \( ( [^()]++ | (?&pn) )* \) )
6973    
6974         If  there  is more than one subpattern with the same name, the earliest         If there is more than one subpattern with the same name,  the  earliest
6975         one is used.         one is used.
6976    
6977         This particular example pattern that we have been looking  at  contains         This  particular  example pattern that we have been looking at contains
6978         nested unlimited repeats, and so the use of a possessive quantifier for         nested unlimited repeats, and so the use of a possessive quantifier for
6979         matching strings of non-parentheses is important when applying the pat-         matching strings of non-parentheses is important when applying the pat-
6980         tern  to  strings  that do not match. For example, when this pattern is         tern to strings that do not match. For example, when  this  pattern  is
6981         applied to         applied to
6982    
6983           (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()           (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
6984    
6985         it yields "no match" quickly. However, if a  possessive  quantifier  is         it  yields  "no  match" quickly. However, if a possessive quantifier is
6986         not  used, the match runs for a very long time indeed because there are         not used, the match runs for a very long time indeed because there  are
6987         so many different ways the + and * repeats can carve  up  the  subject,         so  many  different  ways the + and * repeats can carve up the subject,
6988         and all have to be tested before failure can be reported.         and all have to be tested before failure can be reported.
6989    
6990         At  the  end  of a match, the values of capturing parentheses are those         At the end of a match, the values of capturing  parentheses  are  those
6991         from the outermost level. If you want to obtain intermediate values,  a         from  the outermost level. If you want to obtain intermediate values, a
6992         callout  function can be used (see below and the pcrecallout documenta-         callout function can be used (see below and the pcrecallout  documenta-
6993         tion). If the pattern above is matched against         tion). If the pattern above is matched against
6994    
6995           (ab(cd)ef)           (ab(cd)ef)
6996    
6997         the value for the inner capturing parentheses  (numbered  2)  is  "ef",         the  value  for  the  inner capturing parentheses (numbered 2) is "ef",
6998         which  is the last value taken on at the top level. If a capturing sub-         which is the last value taken on at the top level. If a capturing  sub-
6999         pattern is not matched at the top level, its final  captured  value  is         pattern  is  not  matched at the top level, its final captured value is
7000         unset,  even  if  it was (temporarily) set at a deeper level during the         unset, even if it was (temporarily) set at a deeper  level  during  the
7001         matching process.         matching process.
7002    
7003         If there are more than 15 capturing parentheses in a pattern, PCRE  has         If  there are more than 15 capturing parentheses in a pattern, PCRE has
7004         to  obtain extra memory to store data during a recursion, which it does         to obtain extra memory to store data during a recursion, which it  does
7005         by using pcre_malloc, freeing it via pcre_free afterwards. If no memory         by using pcre_malloc, freeing it via pcre_free afterwards. If no memory
7006         can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.         can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.
7007    
7008         Do  not  confuse  the (?R) item with the condition (R), which tests for         Do not confuse the (?R) item with the condition (R),  which  tests  for
7009         recursion.  Consider this pattern, which matches text in  angle  brack-         recursion.   Consider  this pattern, which matches text in angle brack-
7010         ets,  allowing for arbitrary nesting. Only digits are allowed in nested         ets, allowing for arbitrary nesting. Only digits are allowed in  nested
7011         brackets (that is, when recursing), whereas any characters are  permit-         brackets  (that is, when recursing), whereas any characters are permit-
7012         ted at the outer level.         ted at the outer level.
7013    
7014           < (?: (?(R) \d++  | [^<>]*+) | (?R)) * >           < (?: (?(R) \d++  | [^<>]*+) | (?R)) * >
7015    
7016         In  this  pattern, (?(R) is the start of a conditional subpattern, with         In this pattern, (?(R) is the start of a conditional  subpattern,  with
7017         two different alternatives for the recursive and  non-recursive  cases.         two  different  alternatives for the recursive and non-recursive cases.
7018         The (?R) item is the actual recursive call.         The (?R) item is the actual recursive call.
7019    
7020     Differences in recursion processing between PCRE and Perl     Differences in recursion processing between PCRE and Perl
7021    
7022         Recursion  processing  in PCRE differs from Perl in two important ways.         Recursion processing in PCRE differs from Perl in two  important  ways.
7023         In PCRE (like Python, but unlike Perl), a recursive subpattern call  is         In  PCRE (like Python, but unlike Perl), a recursive subpattern call is
7024         always treated as an atomic group. That is, once it has matched some of         always treated as an atomic group. That is, once it has matched some of
7025         the subject string, it is never re-entered, even if it contains untried         the subject string, it is never re-entered, even if it contains untried
7026         alternatives  and  there  is a subsequent matching failure. This can be         alternatives and there is a subsequent matching failure.  This  can  be
7027         illustrated by the following pattern, which purports to match a  palin-         illustrated  by the following pattern, which purports to match a palin-
7028         dromic  string  that contains an odd number of characters (for example,         dromic string that contains an odd number of characters  (for  example,
7029         "a", "aba", "abcba", "abcdcba"):         "a", "aba", "abcba", "abcdcba"):
7030    
7031           ^(.|(.)(?1)\2)$           ^(.|(.)(?1)\2)$
7032    
7033         The idea is that it either matches a single character, or two identical         The idea is that it either matches a single character, or two identical
7034         characters  surrounding  a sub-palindrome. In Perl, this pattern works;         characters surrounding a sub-palindrome. In Perl, this  pattern  works;
7035         in PCRE it does not if the pattern is  longer  than  three  characters.         in  PCRE  it  does  not if the pattern is longer than three characters.
7036         Consider the subject string "abcba":         Consider the subject string "abcba":
7037    
7038         At  the  top level, the first character is matched, but as it is not at         At the top level, the first character is matched, but as it is  not  at
7039         the end of the string, the first alternative fails; the second alterna-         the end of the string, the first alternative fails; the second alterna-
7040         tive is taken and the recursion kicks in. The recursive call to subpat-         tive is taken and the recursion kicks in. The recursive call to subpat-
7041         tern 1 successfully matches the next character ("b").  (Note  that  the         tern  1  successfully  matches the next character ("b"). (Note that the
7042         beginning and end of line tests are not part of the recursion).         beginning and end of line tests are not part of the recursion).
7043    
7044         Back  at  the top level, the next character ("c") is compared with what         Back at the top level, the next character ("c") is compared  with  what
7045         subpattern 2 matched, which was "a". This fails. Because the  recursion         subpattern  2 matched, which was "a". This fails. Because the recursion
7046         is  treated  as  an atomic group, there are now no backtracking points,         is treated as an atomic group, there are now  no  backtracking  points,
7047         and so the entire match fails. (Perl is able, at  this  point,  to  re-         and  so  the  entire  match fails. (Perl is able, at this point, to re-
7048         enter  the  recursion  and try the second alternative.) However, if the         enter the recursion and try the second alternative.)  However,  if  the
7049         pattern is written with the alternatives in the other order, things are         pattern is written with the alternatives in the other order, things are
7050         different:         different:
7051    
7052           ^((.)(?1)\2|.)$           ^((.)(?1)\2|.)$
7053    
7054         This  time,  the recursing alternative is tried first, and continues to         This time, the recursing alternative is tried first, and  continues  to
7055         recurse until it runs out of characters, at which point  the  recursion         recurse  until  it runs out of characters, at which point the recursion
7056         fails.  But  this  time  we  do  have another alternative to try at the         fails. But this time we do have  another  alternative  to  try  at  the
7057         higher level. That is the big difference:  in  the  previous  case  the         higher  level.  That  is  the  big difference: in the previous case the
7058         remaining alternative is at a deeper recursion level, which PCRE cannot         remaining alternative is at a deeper recursion level, which PCRE cannot
7059         use.         use.
7060    
7061         To change the pattern so that it matches all palindromic  strings,  not         To  change  the pattern so that it matches all palindromic strings, not
7062         just  those  with an odd number of characters, it is tempting to change         just those with an odd number of characters, it is tempting  to  change
7063         the pattern to this:         the pattern to this:
7064    
7065           ^((.)(?1)\2|.?)$           ^((.)(?1)\2|.?)$
7066    
7067         Again, this works in Perl, but not in PCRE, and for  the  same  reason.         Again,  this  works  in Perl, but not in PCRE, and for the same reason.
7068         When  a  deeper  recursion has matched a single character, it cannot be         When a deeper recursion has matched a single character,  it  cannot  be
7069         entered again in order to match an empty string.  The  solution  is  to         entered  again  in  order  to match an empty string. The solution is to
7070         separate  the two cases, and write out the odd and even cases as alter-         separate the two cases, and write out the odd and even cases as  alter-
7071         natives at the higher level:         natives at the higher level:
7072    
7073           ^(?:((.)(?1)\2|)|((.)(?3)\4|.))           ^(?:((.)(?1)\2|)|((.)(?3)\4|.))
7074    
7075         If you want to match typical palindromic phrases, the  pattern  has  to         If  you  want  to match typical palindromic phrases, the pattern has to
7076         ignore all non-word characters, which can be done like this:         ignore all non-word characters, which can be done like this:
7077    
7078           ^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$           ^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$
7079    
7080         If run with the PCRE_CASELESS option, this pattern matches phrases such         If run with the PCRE_CASELESS option, this pattern matches phrases such
7081         as "A man, a plan, a canal: Panama!" and it works well in both PCRE and         as "A man, a plan, a canal: Panama!" and it works well in both PCRE and
7082         Perl.  Note the use of the possessive quantifier *+ to avoid backtrack-         Perl. Note the use of the possessive quantifier *+ to avoid  backtrack-
7083         ing into sequences of non-word characters. Without this, PCRE  takes  a         ing  into  sequences of non-word characters. Without this, PCRE takes a
7084         great  deal  longer  (ten  times or more) to match typical phrases, and         great deal longer (ten times or more) to  match  typical  phrases,  and
7085         Perl takes so long that you think it has gone into a loop.         Perl takes so long that you think it has gone into a loop.
7086    
7087         WARNING: The palindrome-matching patterns above work only if  the  sub-         WARNING:  The  palindrome-matching patterns above work only if the sub-
7088         ject  string  does not start with a palindrome that is shorter than the         ject string does not start with a palindrome that is shorter  than  the
7089         entire string.  For example, although "abcba" is correctly matched,  if         entire  string.  For example, although "abcba" is correctly matched, if
7090         the  subject  is "ababa", PCRE finds the palindrome "aba" at the start,         the subject is "ababa", PCRE finds the palindrome "aba" at  the  start,
7091         then fails at top level because the end of the string does not  follow.         then  fails at top level because the end of the string does not follow.
7092         Once  again, it cannot jump back into the recursion to try other alter-         Once again, it cannot jump back into the recursion to try other  alter-
7093         natives, so the entire match fails.         natives, so the entire match fails.
7094    
7095         The second way in which PCRE and Perl differ in  their  recursion  pro-         The  second  way  in which PCRE and Perl differ in their recursion pro-
7096         cessing  is in the handling of captured values. In Perl, when a subpat-         cessing is in the handling of captured values. In Perl, when a  subpat-
7097         tern is called recursively or as a subpattern (see the  next  section),         tern  is  called recursively or as a subpattern (see the next section),
7098         it  has  no  access to any values that were captured outside the recur-         it has no access to any values that were captured  outside  the  recur-
7099         sion, whereas in PCRE these values can  be  referenced.  Consider  this         sion,  whereas  in  PCRE  these values can be referenced. Consider this
7100         pattern:         pattern:
7101    
7102           ^(.)(\1|a(?2))           ^(.)(\1|a(?2))
7103    
7104         In  PCRE,  this  pattern matches "bab". The first capturing parentheses         In PCRE, this pattern matches "bab". The  first  capturing  parentheses
7105         match "b", then in the second group, when the back reference  \1  fails         match  "b",  then in the second group, when the back reference \1 fails
7106         to  match "b", the second alternative matches "a" and then recurses. In         to match "b", the second alternative matches "a" and then recurses.  In
7107         the recursion, \1 does now match "b" and so the whole  match  succeeds.         the  recursion,  \1 does now match "b" and so the whole match succeeds.
7108         In  Perl,  the pattern fails to match because inside the recursive call         In Perl, the pattern fails to match because inside the  recursive  call
7109         \1 cannot access the externally set value.         \1 cannot access the externally set value.
7110    
7111    
7112  SUBPATTERNS AS SUBROUTINES  SUBPATTERNS AS SUBROUTINES
7113    
7114         If the syntax for a recursive subpattern call (either by number  or  by         If  the  syntax for a recursive subpattern call (either by number or by
7115         name)  is  used outside the parentheses to which it refers, it operates         name) is used outside the parentheses to which it refers,  it  operates
7116         like a subroutine in a programming language. The called subpattern  may         like  a subroutine in a programming language. The called subpattern may
7117         be  defined  before or after the reference. A numbered reference can be         be defined before or after the reference. A numbered reference  can  be
7118         absolute or relative, as in these examples:         absolute or relative, as in these examples:
7119    
7120           (...(absolute)...)...(?2)...           (...(absolute)...)...(?2)...
# Line 7102  SUBPATTERNS AS SUBROUTINES Line 7125  SUBPATTERNS AS SUBROUTINES
7125    
7126           (sens|respons)e and \1ibility           (sens|respons)e and \1ibility
7127    
7128         matches "sense and sensibility" and "response and responsibility",  but         matches  "sense and sensibility" and "response and responsibility", but
7129         not "sense and responsibility". If instead the pattern         not "sense and responsibility". If instead the pattern
7130    
7131           (sens|respons)e and (?1)ibility           (sens|respons)e and (?1)ibility
7132    
7133         is  used, it does match "sense and responsibility" as well as the other         is used, it does match "sense and responsibility" as well as the  other
7134         two strings. Another example is  given  in  the  discussion  of  DEFINE         two  strings.  Another  example  is  given  in the discussion of DEFINE
7135         above.         above.
7136    
7137         All  subroutine  calls, whether recursive or not, are always treated as         All subroutine calls, whether recursive or not, are always  treated  as
7138         atomic groups. That is, once a subroutine has matched some of the  sub-         atomic  groups. That is, once a subroutine has matched some of the sub-
7139         ject string, it is never re-entered, even if it contains untried alter-         ject string, it is never re-entered, even if it contains untried alter-
7140         natives and there is  a  subsequent  matching  failure.  Any  capturing         natives  and  there  is  a  subsequent  matching failure. Any capturing
7141         parentheses  that  are  set  during the subroutine call revert to their         parentheses that are set during the subroutine  call  revert  to  their
7142         previous values afterwards.         previous values afterwards.
7143    
7144         Processing options such as case-independence are fixed when  a  subpat-         Processing  options  such as case-independence are fixed when a subpat-
7145         tern  is defined, so if it is used as a subroutine, such options cannot         tern is defined, so if it is used as a subroutine, such options  cannot
7146         be changed for different calls. For example, consider this pattern:         be changed for different calls. For example, consider this pattern:
7147    
7148           (abc)(?i:(?-1))           (abc)(?i:(?-1))
7149    
7150         It matches "abcabc". It does not match "abcABC" because the  change  of         It  matches  "abcabc". It does not match "abcABC" because the change of
7151         processing option does not affect the called subpattern.         processing option does not affect the called subpattern.
7152    
7153    
7154  ONIGURUMA SUBROUTINE SYNTAX  ONIGURUMA SUBROUTINE SYNTAX
7155    
7156         For  compatibility with Oniguruma, the non-Perl syntax \g followed by a         For compatibility with Oniguruma, the non-Perl syntax \g followed by  a
7157         name or a number enclosed either in angle brackets or single quotes, is         name or a number enclosed either in angle brackets or single quotes, is
7158         an  alternative  syntax  for  referencing a subpattern as a subroutine,         an alternative syntax for referencing a  subpattern  as  a  subroutine,
7159         possibly recursively. Here are two of the examples used above,  rewrit-         possibly  recursively. Here are two of the examples used above, rewrit-
7160         ten using this syntax:         ten using this syntax:
7161    
7162           (?<pn> \( ( (?>[^()]+) | \g<pn> )* \) )           (?<pn> \( ( (?>[^()]+) | \g<pn> )* \) )
7163           (sens|respons)e and \g'1'ibility           (sens|respons)e and \g'1'ibility
7164    
7165         PCRE  supports  an extension to Oniguruma: if a number is preceded by a         PCRE supports an extension to Oniguruma: if a number is preceded  by  a
7166         plus or a minus sign it is taken as a relative reference. For example:         plus or a minus sign it is taken as a relative reference. For example:
7167    
7168           (abc)(?i:\g<-1>)           (abc)(?i:\g<-1>)
7169    
7170         Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are  not         Note  that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are not
7171         synonymous.  The former is a back reference; the latter is a subroutine         synonymous. The former is a back reference; the latter is a  subroutine
7172         call.         call.
7173    
7174    
7175  CALLOUTS  CALLOUTS
7176    
7177         Perl has a feature whereby using the sequence (?{...}) causes arbitrary         Perl has a feature whereby using the sequence (?{...}) causes arbitrary
7178         Perl  code to be obeyed in the middle of matching a regular expression.         Perl code to be obeyed in the middle of matching a regular  expression.
7179         This makes it possible, amongst other things, to extract different sub-         This makes it possible, amongst other things, to extract different sub-
7180         strings that match the same pair of parentheses when there is a repeti-         strings that match the same pair of parentheses when there is a repeti-
7181         tion.         tion.
7182    
7183         PCRE provides a similar feature, but of course it cannot obey arbitrary         PCRE provides a similar feature, but of course it cannot obey arbitrary
7184         Perl code. The feature is called "callout". The caller of PCRE provides         Perl code. The feature is called "callout". The caller of PCRE provides
7185         an external function by putting its entry point in the global  variable         an  external function by putting its entry point in the global variable
7186         pcre_callout  (8-bit  library) or pcre[16|32]_callout (16-bit or 32-bit         pcre_callout (8-bit library) or pcre[16|32]_callout (16-bit  or  32-bit
7187         library).  By default, this variable contains NULL, which disables  all         library).   By default, this variable contains NULL, which disables all
7188         calling out.         calling out.
7189    
7190         Within  a  regular  expression,  (?C) indicates the points at which the         Within a regular expression, (?C) indicates the  points  at  which  the
7191         external function is to be called. If you want  to  identify  different         external  function  is  to be called. If you want to identify different
7192         callout  points, you can put a number less than 256 after the letter C.         callout points, you can put a number less than 256 after the letter  C.
7193         The default value is zero.  For example, this pattern has  two  callout         The  default  value is zero.  For example, this pattern has two callout
7194         points:         points:
7195    
7196           (?C1)abc(?C2)def           (?C1)abc(?C2)def
7197    
7198         If  the PCRE_AUTO_CALLOUT flag is passed to a compiling function, call-         If the PCRE_AUTO_CALLOUT flag is passed to a compiling function,  call-
7199         outs are automatically installed before each item in the pattern.  They         outs  are automatically installed before each item in the pattern. They
7200         are  all  numbered  255. If there is a conditional group in the pattern         are all numbered 255. If there is a conditional group  in  the  pattern
7201         whose condition is an assertion, an additional callout is inserted just         whose condition is an assertion, an additional callout is inserted just
7202         before the condition. An explicit callout may also be set at this posi-         before the condition. An explicit callout may also be set at this posi-
7203         tion, as in this example:         tion, as in this example:
# Line 7184  CALLOUTS Line 7207  CALLOUTS
7207         Note that this applies only to assertion conditions, not to other types         Note that this applies only to assertion conditions, not to other types
7208         of condition.         of condition.
7209    
7210         During  matching, when PCRE reaches a callout point, the external func-         During matching, when PCRE reaches a callout point, the external  func-
7211         tion is called. It is provided with the  number  of  the  callout,  the         tion  is  called.  It  is  provided with the number of the callout, the
7212         position  in  the pattern, and, optionally, one item of data originally         position in the pattern, and, optionally, one item of  data  originally
7213         supplied by the caller of the matching function. The  callout  function         supplied  by  the caller of the matching function. The callout function
7214         may cause matching to proceed, to backtrack, or to fail altogether.         may cause matching to proceed, to backtrack, or to fail altogether.
7215    
7216         By  default,  PCRE implements a number of optimizations at compile time         By default, PCRE implements a number of optimizations at  compile  time
7217         and matching time, and one side-effect is that sometimes  callouts  are         and  matching  time, and one side-effect is that sometimes callouts are
7218         skipped.  If  you need all possible callouts to happen, you need to set         skipped. If you need all possible callouts to happen, you need  to  set
7219         options that disable the relevant optimizations. More  details,  and  a         options  that  disable  the relevant optimizations. More details, and a
7220         complete  description  of  the  interface  to the callout function, are         complete description of the interface  to  the  callout  function,  are
7221         given in the pcrecallout documentation.         given in the pcrecallout documentation.
7222    
7223    
7224  BACKTRACKING CONTROL  BACKTRACKING CONTROL
7225    
7226         Perl 5.10 introduced a number of "Special Backtracking Control  Verbs",         Perl  5.10 introduced a number of "Special Backtracking Control Verbs",
7227         which  are  still  described in the Perl documentation as "experimental         which are still described in the Perl  documentation  as  "experimental
7228         and subject to change or removal in a future version of Perl". It  goes         and  subject to change or removal in a future version of Perl". It goes
7229         on  to  say:  "Their  usage in production code should be noted to avoid         on to say: "Their usage in production code should  be  noted  to  avoid
7230         problems during upgrades." The same remarks apply to the PCRE  features         problems  during upgrades." The same remarks apply to the PCRE features
7231         described in this section.         described in this section.
7232    
7233         The  new verbs make use of what was previously invalid syntax: an open-         The new verbs make use of what was previously invalid syntax: an  open-
7234         ing parenthesis followed by an asterisk. They are generally of the form         ing parenthesis followed by an asterisk. They are generally of the form
7235         (*VERB)  or  (*VERB:NAME). Some may take either form, possibly behaving         (*VERB) or (*VERB:NAME). Some may take either form,  possibly  behaving
7236         differently depending on whether or not a name is present.  A  name  is         differently  depending  on  whether or not a name is present. A name is
7237         any sequence of characters that does not include a closing parenthesis.         any sequence of characters that does not include a closing parenthesis.
7238         The maximum length of name is 255 in the 8-bit library and 65535 in the         The maximum length of name is 255 in the 8-bit library and 65535 in the
7239         16-bit  and  32-bit  libraries.  If  the name is empty, that is, if the         16-bit and 32-bit libraries. If the name is  empty,  that  is,  if  the
7240         closing parenthesis immediately follows the colon, the effect is as  if         closing  parenthesis immediately follows the colon, the effect is as if
7241         the  colon  were  not  there.  Any number of these verbs may occur in a         the colon were not there.  Any number of these verbs  may  occur  in  a
7242         pattern.         pattern.
7243    
7244         Since these verbs are specifically related  to  backtracking,  most  of         Since  these  verbs  are  specifically related to backtracking, most of
7245         them  can  be  used only when the pattern is to be matched using one of         them can be used only when the pattern is to be matched  using  one  of
7246         the traditional matching functions, because these  use  a  backtracking         the  traditional  matching  functions, because these use a backtracking
7247         algorithm.  With the exception of (*FAIL), which behaves like a failing         algorithm. With the exception of (*FAIL), which behaves like a  failing
7248         negative assertion, the backtracking control verbs cause  an  error  if         negative  assertion,  the  backtracking control verbs cause an error if
7249         encountered by a DFA matching function.         encountered by a DFA matching function.
7250    
7251         The  behaviour  of  these  verbs in repeated groups, assertions, and in         The behaviour of these verbs in repeated  groups,  assertions,  and  in
7252         subpatterns called as subroutines (whether or not recursively) is docu-         subpatterns called as subroutines (whether or not recursively) is docu-
7253         mented below.         mented below.
7254    
7255     Optimizations that affect backtracking verbs     Optimizations that affect backtracking verbs
7256    
7257         PCRE  contains some optimizations that are used to speed up matching by         PCRE contains some optimizations that are used to speed up matching  by
7258         running some checks at the start of each match attempt. For example, it         running some checks at the start of each match attempt. For example, it
7259         may  know  the minimum length of matching subject, or that a particular         may know the minimum length of matching subject, or that  a  particular
7260         character must be present. When one of these optimizations bypasses the         character must be present. When one of these optimizations bypasses the
7261         running  of  a  match,  any  included  backtracking  verbs will not, of         running of a match,  any  included  backtracking  verbs  will  not,  of
7262         course, be processed. You can suppress the start-of-match optimizations         course, be processed. You can suppress the start-of-match optimizations
7263         by  setting  the  PCRE_NO_START_OPTIMIZE  option when calling pcre_com-         by setting the PCRE_NO_START_OPTIMIZE  option  when  calling  pcre_com-
7264         pile() or pcre_exec(), or by starting the pattern with (*NO_START_OPT).         pile() or pcre_exec(), or by starting the pattern with (*NO_START_OPT).
7265         There is more discussion of this option in the section entitled "Option         There is more discussion of this option in the section entitled "Option
7266         bits for pcre_exec()" in the pcreapi documentation.         bits for pcre_exec()" in the pcreapi documentation.
7267    
7268         Experiments with Perl suggest that it too  has  similar  optimizations,         Experiments  with  Perl  suggest that it too has similar optimizations,
7269         sometimes leading to anomalous results.         sometimes leading to anomalous results.
7270    
7271     Verbs that act immediately     Verbs that act immediately
7272    
7273         The  following  verbs act as soon as they are encountered. They may not         The following verbs act as soon as they are encountered. They  may  not
7274         be followed by a name.         be followed by a name.
7275    
7276            (*ACCEPT)            (*ACCEPT)
7277    
7278         This verb causes the match to end successfully, skipping the  remainder         This  verb causes the match to end successfully, skipping the remainder
7279         of  the pattern. However, when it is inside a subpattern that is called         of the pattern. However, when it is inside a subpattern that is  called
7280         as a subroutine, only that subpattern is ended  successfully.  Matching         as  a  subroutine, only that subpattern is ended successfully. Matching
7281         then continues at the outer level. If (*ACCEPT) in triggered in a posi-         then continues at the outer level. If (*ACCEPT) in triggered in a posi-
7282         tive assertion, the assertion succeeds; in a  negative  assertion,  the         tive  assertion,  the  assertion succeeds; in a negative assertion, the
7283         assertion fails.         assertion fails.
7284    
7285         If  (*ACCEPT)  is inside capturing parentheses, the data so far is cap-         If (*ACCEPT) is inside capturing parentheses, the data so far  is  cap-
7286         tured. For example:         tured. For example:
7287    
7288           A((?:A|B(*ACCEPT)|C)D)           A((?:A|B(*ACCEPT)|C)D)
7289    
7290         This matches "AB", "AAD", or "ACD"; when it matches "AB", "B"  is  cap-         This  matches  "AB", "AAD", or "ACD"; when it matches "AB", "B" is cap-
7291         tured by the outer parentheses.         tured by the outer parentheses.
7292    
7293           (*FAIL) or (*F)           (*FAIL) or (*F)
7294    
7295         This  verb causes a matching failure, forcing backtracking to occur. It         This verb causes a matching failure, forcing backtracking to occur.  It
7296         is equivalent to (?!) but easier to read. The Perl documentation  notes         is  equivalent to (?!) but easier to read. The Perl documentation notes
7297         that  it  is  probably  useful only when combined with (?{}) or (??{}).         that it is probably useful only when combined  with  (?{})  or  (??{}).
7298         Those are, of course, Perl features that are not present in  PCRE.  The         Those  are,  of course, Perl features that are not present in PCRE. The
7299         nearest  equivalent is the callout feature, as for example in this pat-         nearest equivalent is the callout feature, as for example in this  pat-
7300         tern:         tern:
7301    
7302           a+(?C)(*FAIL)           a+(?C)(*FAIL)
7303    
7304         A match with the string "aaaa" always fails, but the callout  is  taken         A  match  with the string "aaaa" always fails, but the callout is taken
7305         before each backtrack happens (in this example, 10 times).         before each backtrack happens (in this example, 10 times).
7306    
7307     Recording which path was taken     Recording which path was taken
7308    
7309         There  is  one  verb  whose  main  purpose  is to track how a match was         There is one verb whose main purpose  is  to  track  how  a  match  was
7310         arrived at, though it also has a  secondary  use  in  conjunction  with         arrived  at,  though  it  also  has a secondary use in conjunction with
7311         advancing the match starting point (see (*SKIP) below).         advancing the match starting point (see (*SKIP) below).
7312    
7313           (*MARK:NAME) or (*:NAME)           (*MARK:NAME) or (*:NAME)
7314    
7315         A  name  is  always  required  with  this  verb.  There  may be as many         A name is always  required  with  this  verb.  There  may  be  as  many
7316         instances of (*MARK) as you like in a pattern, and their names  do  not         instances  of  (*MARK) as you like in a pattern, and their names do not
7317         have to be unique.         have to be unique.
7318    
7319         When  a  match succeeds, the name of the last-encountered (*MARK:NAME),         When a match succeeds, the name of the  last-encountered  (*MARK:NAME),
7320         (*PRUNE:NAME), or (*THEN:NAME) on the matching path is passed  back  to         (*PRUNE:NAME),  or  (*THEN:NAME) on the matching path is passed back to
7321         the  caller  as  described  in  the  section  entitled  "Extra data for         the caller as  described  in  the  section  entitled  "Extra  data  for
7322         pcre_exec()" in the  pcreapi  documentation.  Here  is  an  example  of         pcre_exec()"  in  the  pcreapi  documentation.  Here  is  an example of
7323         pcretest  output, where the /K modifier requests the retrieval and out-         pcretest output, where the /K modifier requests the retrieval and  out-
7324         putting of (*MARK) data:         putting of (*MARK) data:
7325    
7326             re> /X(*MARK:A)Y|X(*MARK:B)Z/K             re> /X(*MARK:A)Y|X(*MARK:B)Z/K
# Line 7309  BACKTRACKING CONTROL Line 7332  BACKTRACKING CONTROL
7332           MK: B           MK: B
7333    
7334         The (*MARK) name is tagged with "MK:" in this output, and in this exam-         The (*MARK) name is tagged with "MK:" in this output, and in this exam-
7335         ple  it indicates which of the two alternatives matched. This is a more         ple it indicates which of the two alternatives matched. This is a  more
7336         efficient way of obtaining this information than putting each  alterna-         efficient  way of obtaining this information than putting each alterna-
7337         tive in its own capturing parentheses.         tive in its own capturing parentheses.
7338    
7339         If  a  verb  with a name is encountered in a positive assertion that is         If a verb with a name is encountered in a positive  assertion  that  is
7340         true, the name is recorded and passed back if it  is  the  last-encoun-         true,  the  name  is recorded and passed back if it is the last-encoun-
7341         tered. This does not happen for negative assertions or failing positive         tered. This does not happen for negative assertions or failing positive
7342         assertions.         assertions.
7343    
7344         After a partial match or a failed match, the last encountered  name  in         After  a  partial match or a failed match, the last encountered name in
7345         the entire match process is returned. For example:         the entire match process is returned. For example:
7346    
7347             re> /X(*MARK:A)Y|X(*MARK:B)Z/K             re> /X(*MARK:A)Y|X(*MARK:B)Z/K
7348           data> XP           data> XP
7349           No match, mark = B           No match, mark = B
7350    
7351         Note  that  in  this  unanchored  example the mark is retained from the         Note that in this unanchored example the  mark  is  retained  from  the
7352         match attempt that started at the letter "X" in the subject. Subsequent         match attempt that started at the letter "X" in the subject. Subsequent
7353         match attempts starting at "P" and then with an empty string do not get         match attempts starting at "P" and then with an empty string do not get
7354         as far as the (*MARK) item, but nevertheless do not reset it.         as far as the (*MARK) item, but nevertheless do not reset it.
7355    
7356         If you are interested in  (*MARK)  values  after  failed  matches,  you         If  you  are  interested  in  (*MARK)  values after failed matches, you
7357         should  probably  set  the PCRE_NO_START_OPTIMIZE option (see above) to         should probably set the PCRE_NO_START_OPTIMIZE option  (see  above)  to
7358         ensure that the match is always attempted.         ensure that the match is always attempted.
7359    
7360     Verbs that act after backtracking     Verbs that act after backtracking
7361    
7362         The following verbs do nothing when they are encountered. Matching con-         The following verbs do nothing when they are encountered. Matching con-
7363         tinues  with what follows, but if there is no subsequent match, causing         tinues with what follows, but if there is no subsequent match,  causing
7364         a backtrack to the verb, a failure is  forced.  That  is,  backtracking         a  backtrack  to  the  verb, a failure is forced. That is, backtracking
7365         cannot  pass  to the left of the verb. However, when one of these verbs         cannot pass to the left of the verb. However, when one of  these  verbs
7366         appears inside an atomic group or an assertion that is true, its effect         appears inside an atomic group or an assertion that is true, its effect
7367         is  confined  to  that  group, because once the group has been matched,         is confined to that group, because once the  group  has  been  matched,
7368         there is never any backtracking into it. In this situation,  backtrack-         there  is never any backtracking into it. In this situation, backtrack-
7369         ing  can  "jump  back" to the left of the entire atomic group or asser-         ing can "jump back" to the left of the entire atomic  group  or  asser-
7370         tion. (Remember also, as stated  above,  that  this  localization  also         tion.  (Remember  also,  as  stated  above, that this localization also
7371         applies in subroutine calls.)         applies in subroutine calls.)
7372    
7373         These  verbs  differ  in exactly what kind of failure occurs when back-         These verbs differ in exactly what kind of failure  occurs  when  back-
7374         tracking reaches them. The behaviour described below  is  what  happens         tracking  reaches  them.  The behaviour described below is what happens
7375         when  the  verb is not in a subroutine or an assertion. Subsequent sec-         when the verb is not in a subroutine or an assertion.  Subsequent  sec-
7376         tions cover these special cases.         tions cover these special cases.
7377    
7378           (*COMMIT)           (*COMMIT)
7379    
7380         This verb, which may not be followed by a name, causes the whole  match         This  verb, which may not be followed by a name, causes the whole match
7381         to fail outright if there is a later matching failure that causes back-         to fail outright if there is a later matching failure that causes back-
7382         tracking to reach it. Even if the pattern  is  unanchored,  no  further         tracking  to  reach  it.  Even if the pattern is unanchored, no further
7383         attempts to find a match by advancing the starting point take place. If         attempts to find a match by advancing the starting point take place. If
7384         (*COMMIT) is the only backtracking verb that is  encountered,  once  it         (*COMMIT)  is  the  only backtracking verb that is encountered, once it
7385         has been passed pcre_exec() is committed to finding a match at the cur-         has been passed pcre_exec() is committed to finding a match at the cur-
7386         rent starting point, or not at all. For example:         rent starting point, or not at all. For example:
7387    
7388           a+(*COMMIT)b           a+(*COMMIT)b
7389    
7390         This matches "xxaab" but not "aacaab". It can be thought of as  a  kind         This  matches  "xxaab" but not "aacaab". It can be thought of as a kind
7391         of dynamic anchor, or "I've started, so I must finish." The name of the         of dynamic anchor, or "I've started, so I must finish." The name of the
7392         most recently passed (*MARK) in the path is passed back when  (*COMMIT)         most  recently passed (*MARK) in the path is passed back when (*COMMIT)
7393         forces a match failure.         forces a match failure.
7394    
7395         If  there  is more than one backtracking verb in a pattern, a different         If there is more than one backtracking verb in a pattern,  a  different
7396         one that follows (*COMMIT) may be triggered first,  so  merely  passing         one  that  follows  (*COMMIT) may be triggered first, so merely passing
7397         (*COMMIT) during a match does not always guarantee that a match must be         (*COMMIT) during a match does not always guarantee that a match must be
7398         at this starting point.         at this starting point.
7399    
7400         Note that (*COMMIT) at the start of a pattern is not  the  same  as  an         Note  that  (*COMMIT)  at  the start of a pattern is not the same as an
7401         anchor,  unless  PCRE's start-of-match optimizations are turned off, as         anchor, unless PCRE's start-of-match optimizations are turned  off,  as
7402         shown in this pcretest example:         shown in this pcretest example:
7403    
7404             re> /(*COMMIT)abc/             re> /(*COMMIT)abc/
# Line 7384  BACKTRACKING CONTROL Line 7407  BACKTRACKING CONTROL
7407           xyzabc\Y           xyzabc\Y
7408           No match           No match
7409    
7410         PCRE knows that any match must start  with  "a",  so  the  optimization         PCRE  knows  that  any  match  must start with "a", so the optimization
7411         skips  along the subject to "a" before running the first match attempt,         skips along the subject to "a" before running the first match  attempt,
7412         which succeeds. When the optimization is disabled by the \Y  escape  in         which  succeeds.  When the optimization is disabled by the \Y escape in
7413         the second subject, the match starts at "x" and so the (*COMMIT) causes         the second subject, the match starts at "x" and so the (*COMMIT) causes
7414         it to fail without trying any other starting points.         it to fail without trying any other starting points.
7415    
7416           (*PRUNE) or (*PRUNE:NAME)           (*PRUNE) or (*PRUNE:NAME)
7417    
7418         This verb causes the match to fail at the current starting position  in         This  verb causes the match to fail at the current starting position in
7419         the subject if there is a later matching failure that causes backtrack-         the subject if there is a later matching failure that causes backtrack-
7420         ing to reach it. If the pattern is unanchored, the  normal  "bumpalong"         ing  to  reach it. If the pattern is unanchored, the normal "bumpalong"
7421         advance  to  the next starting character then happens. Backtracking can         advance to the next starting character then happens.  Backtracking  can
7422         occur as usual to the left of (*PRUNE), before it is reached,  or  when         occur  as  usual to the left of (*PRUNE), before it is reached, or when
7423         matching  to  the  right  of  (*PRUNE), but if there is no match to the         matching to the right of (*PRUNE), but if there  is  no  match  to  the
7424         right, backtracking cannot cross (*PRUNE). In simple cases, the use  of         right,  backtracking cannot cross (*PRUNE). In simple cases, the use of
7425         (*PRUNE)  is just an alternative to an atomic group or possessive quan-         (*PRUNE) is just an alternative to an atomic group or possessive  quan-
7426         tifier, but there are some uses of (*PRUNE) that cannot be expressed in         tifier, but there are some uses of (*PRUNE) that cannot be expressed in
7427         any  other  way. In an anchored pattern (*PRUNE) has the same effect as         any other way. In an anchored pattern (*PRUNE) has the same  effect  as
7428         (*COMMIT).         (*COMMIT).
7429    
7430         The   behaviour   of   (*PRUNE:NAME)   is   the   not   the   same   as         The   behaviour   of   (*PRUNE:NAME)   is   the   not   the   same   as
7431         (*MARK:NAME)(*PRUNE).   It  is  like  (*MARK:NAME)  in that the name is         (*MARK:NAME)(*PRUNE).  It is like (*MARK:NAME)  in  that  the  name  is
7432         remembered for  passing  back  to  the  caller.  However,  (*SKIP:NAME)         remembered  for  passing  back  to  the  caller.  However, (*SKIP:NAME)
7433         searches only for names set with (*MARK).         searches only for names set with (*MARK).
7434    
7435           (*SKIP)           (*SKIP)
7436    
7437         This  verb, when given without a name, is like (*PRUNE), except that if         This verb, when given without a name, is like (*PRUNE), except that  if
7438         the pattern is unanchored, the "bumpalong" advance is not to  the  next         the  pattern  is unanchored, the "bumpalong" advance is not to the next
7439         character, but to the position in the subject where (*SKIP) was encoun-         character, but to the position in the subject where (*SKIP) was encoun-
7440         tered. (*SKIP) signifies that whatever text was matched leading  up  to         tered.  (*SKIP)  signifies that whatever text was matched leading up to
7441         it cannot be part of a successful match. Consider:         it cannot be part of a successful match. Consider:
7442    
7443           a+(*SKIP)b           a+(*SKIP)b
7444    
7445         If  the  subject  is  "aaaac...",  after  the first match attempt fails         If the subject is "aaaac...",  after  the  first  match  attempt  fails
7446         (starting at the first character in the  string),  the  starting  point         (starting  at  the  first  character in the string), the starting point
7447         skips on to start the next attempt at "c". Note that a possessive quan-         skips on to start the next attempt at "c". Note that a possessive quan-
7448         tifer does not have the same effect as this example; although it  would         tifer  does not have the same effect as this example; although it would
7449         suppress  backtracking  during  the  first  match  attempt,  the second         suppress backtracking  during  the  first  match  attempt,  the  second
7450         attempt would start at the second character instead of skipping  on  to         attempt  would  start at the second character instead of skipping on to
7451         "c".         "c".
7452    
7453           (*SKIP:NAME)           (*SKIP:NAME)
7454    
7455         When (*SKIP) has an associated name, its behaviour is modified. When it         When (*SKIP) has an associated name, its behaviour is modified. When it
7456         is triggered, the previous path through the pattern is searched for the         is triggered, the previous path through the pattern is searched for the
7457         most  recent  (*MARK)  that  has  the  same  name. If one is found, the         most recent (*MARK) that has the  same  name.  If  one  is  found,  the
7458         "bumpalong" advance is to the subject position that corresponds to that         "bumpalong" advance is to the subject position that corresponds to that
7459         (*MARK) instead of to where (*SKIP) was encountered. If no (*MARK) with         (*MARK) instead of to where (*SKIP) was encountered. If no (*MARK) with
7460         a matching name is found, the (*SKIP) is ignored.         a matching name is found, the (*SKIP) is ignored.
7461    
7462         Note that (*SKIP:NAME) searches only for names set by (*MARK:NAME).  It         Note  that (*SKIP:NAME) searches only for names set by (*MARK:NAME). It
7463         ignores names that are set by (*PRUNE:NAME) or (*THEN:NAME).         ignores names that are set by (*PRUNE:NAME) or (*THEN:NAME).
7464    
7465           (*THEN) or (*THEN:NAME)           (*THEN) or (*THEN:NAME)
7466    
7467         This  verb  causes  a skip to the next innermost alternative when back-         This verb causes a skip to the next innermost  alternative  when  back-
7468         tracking reaches it. That  is,  it  cancels  any  further  backtracking         tracking  reaches  it.  That  is,  it  cancels any further backtracking
7469         within  the  current  alternative.  Its name comes from the observation         within the current alternative. Its name  comes  from  the  observation
7470         that it can be used for a pattern-based if-then-else block:         that it can be used for a pattern-based if-then-else block:
7471    
7472           ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...           ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
7473    
7474         If the COND1 pattern matches, FOO is tried (and possibly further  items         If  the COND1 pattern matches, FOO is tried (and possibly further items
7475         after  the  end  of the group if FOO succeeds); on failure, the matcher         after the end of the group if FOO succeeds); on  failure,  the  matcher
7476         skips to the second alternative and tries COND2,  without  backtracking         skips  to  the second alternative and tries COND2, without backtracking
7477         into  COND1.  If that succeeds and BAR fails, COND3 is tried. If subse-         into COND1. If that succeeds and BAR fails, COND3 is tried.  If  subse-
7478         quently BAZ fails, there are no more alternatives, so there is a  back-         quently  BAZ fails, there are no more alternatives, so there is a back-
7479         track  to  whatever  came  before  the  entire group. If (*THEN) is not         track to whatever came before the  entire  group.  If  (*THEN)  is  not
7480         inside an alternation, it acts like (*PRUNE).         inside an alternation, it acts like (*PRUNE).
7481    
7482         The   behaviour   of   (*THEN:NAME)   is   the   not   the   same    as         The    behaviour   of   (*THEN:NAME)   is   the   not   the   same   as
7483         (*MARK:NAME)(*THEN).   It  is  like  (*MARK:NAME)  in  that the name is         (*MARK:NAME)(*THEN).  It is like  (*MARK:NAME)  in  that  the  name  is
7484         remembered for  passing  back  to  the  caller.  However,  (*SKIP:NAME)         remembered  for  passing  back  to  the  caller.  However, (*SKIP:NAME)
7485         searches only for names set with (*MARK).         searches only for names set with (*MARK).
7486    
7487         A  subpattern that does not contain a | character is just a part of the         A subpattern that does not contain a | character is just a part of  the
7488         enclosing alternative; it is not a nested  alternation  with  only  one         enclosing  alternative;  it  is  not a nested alternation with only one
7489         alternative.  The effect of (*THEN) extends beyond such a subpattern to         alternative. The effect of (*THEN) extends beyond such a subpattern  to
7490         the enclosing alternative. Consider this pattern, where A, B, etc.  are         the  enclosing alternative. Consider this pattern, where A, B, etc. are
7491         complex  pattern fragments that do not contain any | characters at this         complex pattern fragments that do not contain any | characters at  this
7492         level:         level:
7493    
7494           A (B(*THEN)C) | D           A (B(*THEN)C) | D
7495    
7496         If A and B are matched, but there is a failure in C, matching does  not         If  A and B are matched, but there is a failure in C, matching does not
7497         backtrack into A; instead it moves to the next alternative, that is, D.         backtrack into A; instead it moves to the next alternative, that is, D.
7498         However, if the subpattern containing (*THEN) is given an  alternative,         However,  if the subpattern containing (*THEN) is given an alternative,
7499         it behaves differently:         it behaves differently:
7500    
7501           A (B(*THEN)C | (*FAIL)) | D           A (B(*THEN)C | (*FAIL)) | D
7502    
7503         The  effect of (*THEN) is now confined to the inner subpattern. After a         The effect of (*THEN) is now confined to the inner subpattern. After  a
7504         failure in C, matching moves to (*FAIL), which causes the whole subpat-         failure in C, matching moves to (*FAIL), which causes the whole subpat-
7505         tern  to  fail  because  there are no more alternatives to try. In this         tern to fail because there are no more alternatives  to  try.  In  this
7506         case, matching does now backtrack into A.         case, matching does now backtrack into A.
7507    
7508         Note that a conditional subpattern is  not  considered  as  having  two         Note  that  a  conditional  subpattern  is not considered as having two
7509         alternatives,  because  only  one  is  ever used. In other words, the |         alternatives, because only one is ever used.  In  other  words,  the  |
7510         character in a conditional subpattern has a different meaning. Ignoring         character in a conditional subpattern has a different meaning. Ignoring
7511         white space, consider:         white space, consider:
7512    
7513           ^.*? (?(?=a) a | b(*THEN)c )           ^.*? (?(?=a) a | b(*THEN)c )
7514    
7515         If  the  subject  is  "ba", this pattern does not match. Because .*? is         If the subject is "ba", this pattern does not  match.  Because  .*?  is
7516         ungreedy, it initially matches zero  characters.  The  condition  (?=a)         ungreedy,  it  initially  matches  zero characters. The condition (?=a)
7517         then  fails,  the  character  "b"  is  matched, but "c" is not. At this         then fails, the character "b" is matched,  but  "c"  is  not.  At  this
7518         point, matching does not backtrack to .*? as might perhaps be  expected         point,  matching does not backtrack to .*? as might perhaps be expected
7519         from  the  presence  of  the | character. The conditional subpattern is         from the presence of the | character.  The  conditional  subpattern  is
7520         part of the single alternative that comprises the whole pattern, and so         part of the single alternative that comprises the whole pattern, and so
7521         the  match  fails.  (If  there was a backtrack into .*?, allowing it to         the match fails. (If there was a backtrack into  .*?,  allowing  it  to
7522         match "b", the match would succeed.)         match "b", the match would succeed.)
7523    
7524         The verbs just described provide four different "strengths" of  control         The  verbs just described provide four different "strengths" of control
7525         when subsequent matching fails. (*THEN) is the weakest, carrying on the         when subsequent matching fails. (*THEN) is the weakest, carrying on the
7526         match at the next alternative. (*PRUNE) comes next, failing  the  match         match  at  the next alternative. (*PRUNE) comes next, failing the match
7527         at  the  current starting position, but allowing an advance to the next         at the current starting position, but allowing an advance to  the  next
7528         character (for an unanchored pattern). (*SKIP) is similar, except  that         character  (for an unanchored pattern). (*SKIP) is similar, except that
7529         the advance may be more than one character. (*COMMIT) is the strongest,         the advance may be more than one character. (*COMMIT) is the strongest,
7530         causing the entire match to fail.         causing the entire match to fail.
7531    
7532     More than one backtracking verb     More than one backtracking verb
7533    
7534         If more than one backtracking verb is present in  a  pattern,  the  one         If  more  than  one  backtracking verb is present in a pattern, the one
7535         that  is  backtracked  onto first acts. For example, consider this pat-         that is backtracked onto first acts. For example,  consider  this  pat-
7536         tern, where A, B, etc. are complex pattern fragments:         tern, where A, B, etc. are complex pattern fragments:
7537    
7538           (A(*COMMIT)B(*THEN)C|ABD)           (A(*COMMIT)B(*THEN)C|ABD)
7539    
7540         If A matches but B fails, the backtrack to (*COMMIT) causes the  entire         If  A matches but B fails, the backtrack to (*COMMIT) causes the entire
7541         match to fail. However, if A and B match, but C fails, the backtrack to         match to fail. However, if A and B match, but C fails, the backtrack to
7542         (*THEN) causes the next alternative (ABD) to be tried.  This  behaviour         (*THEN)  causes  the next alternative (ABD) to be tried. This behaviour
7543         is  consistent,  but is not always the same as Perl's. It means that if         is consistent, but is not always the same as Perl's. It means  that  if
7544         two or more backtracking verbs appear in succession, all the  the  last         two  or  more backtracking verbs appear in succession, all the the last
7545         of them has no effect. Consider this example:         of them has no effect. Consider this example:
7546    
7547           ...(*COMMIT)(*PRUNE)...           ...(*COMMIT)(*PRUNE)...
7548    
7549         If there is a matching failure to the right, backtracking onto (*PRUNE)         If there is a matching failure to the right, backtracking onto (*PRUNE)
7550         causes it to be triggered, and its action is taken. There can never  be         causes  it to be triggered, and its action is taken. There can never be
7551         a backtrack onto (*COMMIT).         a backtrack onto (*COMMIT).
7552    
7553     Backtracking verbs in repeated groups     Backtracking verbs in repeated groups
7554    
7555         PCRE  differs  from  Perl  in  its  handling  of  backtracking verbs in         PCRE differs from  Perl  in  its  handling  of  backtracking  verbs  in
7556         repeated groups. For example, consider:         repeated groups. For example, consider:
7557    
7558           /(a(*COMMIT)b)+ac/           /(a(*COMMIT)b)+ac/
7559    
7560         If the subject is "abac", Perl matches,  but  PCRE  fails  because  the         If  the  subject  is  "abac",  Perl matches, but PCRE fails because the
7561         (*COMMIT) in the second repeat of the group acts.         (*COMMIT) in the second repeat of the group acts.
7562    
7563     Backtracking verbs in assertions     Backtracking verbs in assertions
7564    
7565         (*FAIL)  in  an assertion has its normal effect: it forces an immediate         (*FAIL) in an assertion has its normal effect: it forces  an  immediate
7566         backtrack.         backtrack.
7567    
7568         (*ACCEPT) in a positive assertion causes the assertion to succeed with-         (*ACCEPT) in a positive assertion causes the assertion to succeed with-
7569         out  any  further processing. In a negative assertion, (*ACCEPT) causes         out any further processing. In a negative assertion,  (*ACCEPT)  causes
7570         the assertion to fail without any further processing.         the assertion to fail without any further processing.
7571    
7572         The other backtracking verbs are not treated specially if  they  appear         The  other  backtracking verbs are not treated specially if they appear
7573         in  a  positive  assertion.  In  particular,  (*THEN) skips to the next         in a positive assertion. In  particular,  (*THEN)  skips  to  the  next
7574         alternative in the innermost enclosing  group  that  has  alternations,         alternative  in  the  innermost  enclosing group that has alternations,
7575         whether or not this is within the assertion.         whether or not this is within the assertion.
7576    
7577         Negative  assertions  are,  however, different, in order to ensure that         Negative assertions are, however, different, in order  to  ensure  that
7578         changing a positive assertion into a  negative  assertion  changes  its         changing  a  positive  assertion  into a negative assertion changes its
7579         result. Backtracking into (*COMMIT), (*SKIP), or (*PRUNE) causes a neg-         result. Backtracking into (*COMMIT), (*SKIP), or (*PRUNE) causes a neg-
7580         ative assertion to be true, without considering any further alternative         ative assertion to be true, without considering any further alternative
7581         branches in the assertion.  Backtracking into (*THEN) causes it to skip         branches in the assertion.  Backtracking into (*THEN) causes it to skip
7582         to the next enclosing alternative within the assertion (the normal  be-         to  the next enclosing alternative within the assertion (the normal be-
7583         haviour),  but  if  the  assertion  does  not have such an alternative,         haviour), but if the assertion  does  not  have  such  an  alternative,
7584         (*THEN) behaves like (*PRUNE).         (*THEN) behaves like (*PRUNE).
7585    
7586     Backtracking verbs in subroutines     Backtracking verbs in subroutines
7587    
7588         These behaviours occur whether or not the subpattern is  called  recur-         These  behaviours  occur whether or not the subpattern is called recur-
7589         sively.  Perl's treatment of subroutines is different in some cases.         sively.  Perl's treatment of subroutines is different in some cases.
7590    
7591         (*FAIL)  in  a subpattern called as a subroutine has its normal effect:         (*FAIL) in a subpattern called as a subroutine has its  normal  effect:
7592         it forces an immediate backtrack.         it forces an immediate backtrack.
7593    
7594         (*ACCEPT) in a subpattern called as a subroutine causes the  subroutine         (*ACCEPT)  in a subpattern called as a subroutine causes the subroutine
7595         match  to succeed without any further processing. Matching then contin-         match to succeed without any further processing. Matching then  contin-
7596         ues after the subroutine call.         ues after the subroutine call.
7597    
7598         (*COMMIT), (*SKIP), and (*PRUNE) in a subpattern called as a subroutine         (*COMMIT), (*SKIP), and (*PRUNE) in a subpattern called as a subroutine
7599         cause the subroutine match to fail.         cause the subroutine match to fail.
7600    
7601         (*THEN)  skips to the next alternative in the innermost enclosing group         (*THEN) skips to the next alternative in the innermost enclosing  group
7602         within the subpattern that has alternatives. If there is no such  group         within  the subpattern that has alternatives. If there is no such group
7603         within the subpattern, (*THEN) causes the subroutine match to fail.         within the subpattern, (*THEN) causes the subroutine match to fail.
7604    
7605    
7606  SEE ALSO  SEE ALSO
7607    
7608         pcreapi(3),  pcrecallout(3),  pcrematching(3),  pcresyntax(3), pcre(3),         pcreapi(3), pcrecallout(3),  pcrematching(3),  pcresyntax(3),  pcre(3),
7609         pcre16(3), pcre32(3).         pcre16(3), pcre32(3).
7610    
7611    
# Line 7595  AUTHOR Line 7618  AUTHOR
7618    
7619  REVISION  REVISION
7620    
7621         Last updated: 12 November 2013         Last updated: 03 December 2013
7622         Copyright (c) 1997-2013 University of Cambridge.         Copyright (c) 1997-2013 University of Cambridge.
7623  ------------------------------------------------------------------------------  ------------------------------------------------------------------------------
7624    

Legend:
Removed from v.1411  
changed lines
  Added in v.1412

  ViewVC Help
Powered by ViewVC 1.1.5