pcrepattern.html 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276
  1. <html>
  2. <head>
  3. <title>pcrepattern specification</title>
  4. </head>
  5. <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
  6. <h1>pcrepattern man page</h1>
  7. <p>
  8. Return to the <a href="index.html">PCRE index page</a>.
  9. </p>
  10. <p>
  11. This page is part of the PCRE HTML documentation. It was generated automatically
  12. from the original man page. If there is any nonsense in it, please consult the
  13. man page, in case the conversion went wrong.
  14. <br>
  15. <ul>
  16. <li><a name="TOC1" href="#SEC1">PCRE REGULAR EXPRESSION DETAILS</a>
  17. <li><a name="TOC2" href="#SEC2">SPECIAL START-OF-PATTERN ITEMS</a>
  18. <li><a name="TOC3" href="#SEC3">EBCDIC CHARACTER CODES</a>
  19. <li><a name="TOC4" href="#SEC4">CHARACTERS AND METACHARACTERS</a>
  20. <li><a name="TOC5" href="#SEC5">BACKSLASH</a>
  21. <li><a name="TOC6" href="#SEC6">CIRCUMFLEX AND DOLLAR</a>
  22. <li><a name="TOC7" href="#SEC7">FULL STOP (PERIOD, DOT) AND \N</a>
  23. <li><a name="TOC8" href="#SEC8">MATCHING A SINGLE DATA UNIT</a>
  24. <li><a name="TOC9" href="#SEC9">SQUARE BRACKETS AND CHARACTER CLASSES</a>
  25. <li><a name="TOC10" href="#SEC10">POSIX CHARACTER CLASSES</a>
  26. <li><a name="TOC11" href="#SEC11">COMPATIBILITY FEATURE FOR WORD BOUNDARIES</a>
  27. <li><a name="TOC12" href="#SEC12">VERTICAL BAR</a>
  28. <li><a name="TOC13" href="#SEC13">INTERNAL OPTION SETTING</a>
  29. <li><a name="TOC14" href="#SEC14">SUBPATTERNS</a>
  30. <li><a name="TOC15" href="#SEC15">DUPLICATE SUBPATTERN NUMBERS</a>
  31. <li><a name="TOC16" href="#SEC16">NAMED SUBPATTERNS</a>
  32. <li><a name="TOC17" href="#SEC17">REPETITION</a>
  33. <li><a name="TOC18" href="#SEC18">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a>
  34. <li><a name="TOC19" href="#SEC19">BACK REFERENCES</a>
  35. <li><a name="TOC20" href="#SEC20">ASSERTIONS</a>
  36. <li><a name="TOC21" href="#SEC21">CONDITIONAL SUBPATTERNS</a>
  37. <li><a name="TOC22" href="#SEC22">COMMENTS</a>
  38. <li><a name="TOC23" href="#SEC23">RECURSIVE PATTERNS</a>
  39. <li><a name="TOC24" href="#SEC24">SUBPATTERNS AS SUBROUTINES</a>
  40. <li><a name="TOC25" href="#SEC25">ONIGURUMA SUBROUTINE SYNTAX</a>
  41. <li><a name="TOC26" href="#SEC26">CALLOUTS</a>
  42. <li><a name="TOC27" href="#SEC27">BACKTRACKING CONTROL</a>
  43. <li><a name="TOC28" href="#SEC28">SEE ALSO</a>
  44. <li><a name="TOC29" href="#SEC29">AUTHOR</a>
  45. <li><a name="TOC30" href="#SEC30">REVISION</a>
  46. </ul>
  47. <br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br>
  48. <P>
  49. The syntax and semantics of the regular expressions that are supported by PCRE
  50. are described in detail below. There is a quick-reference syntax summary in the
  51. <a href="pcresyntax.html"><b>pcresyntax</b></a>
  52. page. PCRE tries to match Perl syntax and semantics as closely as it can. PCRE
  53. also supports some alternative regular expression syntax (which does not
  54. conflict with the Perl syntax) in order to provide some compatibility with
  55. regular expressions in Python, .NET, and Oniguruma.
  56. </P>
  57. <P>
  58. Perl's regular expressions are described in its own documentation, and
  59. regular expressions in general are covered in a number of books, some of which
  60. have copious examples. Jeffrey Friedl's "Mastering Regular Expressions",
  61. published by O'Reilly, covers regular expressions in great detail. This
  62. description of PCRE's regular expressions is intended as reference material.
  63. </P>
  64. <P>
  65. This document discusses the patterns that are supported by PCRE when one its
  66. main matching functions, <b>pcre_exec()</b> (8-bit) or <b>pcre[16|32]_exec()</b>
  67. (16- or 32-bit), is used. PCRE also has alternative matching functions,
  68. <b>pcre_dfa_exec()</b> and <b>pcre[16|32_dfa_exec()</b>, which match using a
  69. different algorithm that is not Perl-compatible. Some of the features discussed
  70. below are not available when DFA matching is used. The advantages and
  71. disadvantages of the alternative functions, and how they differ from the normal
  72. functions, are discussed in the
  73. <a href="pcrematching.html"><b>pcrematching</b></a>
  74. page.
  75. </P>
  76. <br><a name="SEC2" href="#TOC1">SPECIAL START-OF-PATTERN ITEMS</a><br>
  77. <P>
  78. A number of options that can be passed to <b>pcre_compile()</b> can also be set
  79. by special items at the start of a pattern. These are not Perl-compatible, but
  80. are provided to make these options accessible to pattern writers who are not
  81. able to change the program that processes the pattern. Any number of these
  82. items may appear, but they must all be together right at the start of the
  83. pattern string, and the letters must be in upper case.
  84. </P>
  85. <br><b>
  86. UTF support
  87. </b><br>
  88. <P>
  89. The original operation of PCRE was on strings of one-byte characters. However,
  90. there is now also support for UTF-8 strings in the original library, an
  91. extra library that supports 16-bit and UTF-16 character strings, and a
  92. third library that supports 32-bit and UTF-32 character strings. To use these
  93. features, PCRE must be built to include appropriate support. When using UTF
  94. strings you must either call the compiling function with the PCRE_UTF8,
  95. PCRE_UTF16, or PCRE_UTF32 option, or the pattern must start with one of
  96. these special sequences:
  97. <pre>
  98. (*UTF8)
  99. (*UTF16)
  100. (*UTF32)
  101. (*UTF)
  102. </pre>
  103. (*UTF) is a generic sequence that can be used with any of the libraries.
  104. Starting a pattern with such a sequence is equivalent to setting the relevant
  105. option. How setting a UTF mode affects pattern matching is mentioned in several
  106. places below. There is also a summary of features in the
  107. <a href="pcreunicode.html"><b>pcreunicode</b></a>
  108. page.
  109. </P>
  110. <P>
  111. Some applications that allow their users to supply patterns may wish to
  112. restrict them to non-UTF data for security reasons. If the PCRE_NEVER_UTF
  113. option is set at compile time, (*UTF) etc. are not allowed, and their
  114. appearance causes an error.
  115. </P>
  116. <br><b>
  117. Unicode property support
  118. </b><br>
  119. <P>
  120. Another special sequence that may appear at the start of a pattern is (*UCP).
  121. This has the same effect as setting the PCRE_UCP option: it causes sequences
  122. such as \d and \w to use Unicode properties to determine character types,
  123. instead of recognizing only characters with codes less than 128 via a lookup
  124. table.
  125. </P>
  126. <br><b>
  127. Disabling auto-possessification
  128. </b><br>
  129. <P>
  130. If a pattern starts with (*NO_AUTO_POSSESS), it has the same effect as setting
  131. the PCRE_NO_AUTO_POSSESS option at compile time. This stops PCRE from making
  132. quantifiers possessive when what follows cannot match the repeated item. For
  133. example, by default a+b is treated as a++b. For more details, see the
  134. <a href="pcreapi.html"><b>pcreapi</b></a>
  135. documentation.
  136. </P>
  137. <br><b>
  138. Disabling start-up optimizations
  139. </b><br>
  140. <P>
  141. If a pattern starts with (*NO_START_OPT), it has the same effect as setting the
  142. PCRE_NO_START_OPTIMIZE option either at compile or matching time. This disables
  143. several optimizations for quickly reaching "no match" results. For more
  144. details, see the
  145. <a href="pcreapi.html"><b>pcreapi</b></a>
  146. documentation.
  147. <a name="newlines"></a></P>
  148. <br><b>
  149. Newline conventions
  150. </b><br>
  151. <P>
  152. PCRE supports five different conventions for indicating line breaks in
  153. strings: a single CR (carriage return) character, a single LF (linefeed)
  154. character, the two-character sequence CRLF, any of the three preceding, or any
  155. Unicode newline sequence. The
  156. <a href="pcreapi.html"><b>pcreapi</b></a>
  157. page has
  158. <a href="pcreapi.html#newlines">further discussion</a>
  159. about newlines, and shows how to set the newline convention in the
  160. <i>options</i> arguments for the compiling and matching functions.
  161. </P>
  162. <P>
  163. It is also possible to specify a newline convention by starting a pattern
  164. string with one of the following five sequences:
  165. <pre>
  166. (*CR) carriage return
  167. (*LF) linefeed
  168. (*CRLF) carriage return, followed by linefeed
  169. (*ANYCRLF) any of the three above
  170. (*ANY) all Unicode newline sequences
  171. </pre>
  172. These override the default and the options given to the compiling function. For
  173. example, on a Unix system where LF is the default newline sequence, the pattern
  174. <pre>
  175. (*CR)a.b
  176. </pre>
  177. changes the convention to CR. That pattern matches "a\nb" because LF is no
  178. longer a newline. If more than one of these settings is present, the last one
  179. is used.
  180. </P>
  181. <P>
  182. The newline convention affects where the circumflex and dollar assertions are
  183. true. It also affects the interpretation of the dot metacharacter when
  184. PCRE_DOTALL is not set, and the behaviour of \N. However, it does not affect
  185. what the \R escape sequence matches. By default, this is any Unicode newline
  186. sequence, for Perl compatibility. However, this can be changed; see the
  187. description of \R in the section entitled
  188. <a href="#newlineseq">"Newline sequences"</a>
  189. below. A change of \R setting can be combined with a change of newline
  190. convention.
  191. </P>
  192. <br><b>
  193. Setting match and recursion limits
  194. </b><br>
  195. <P>
  196. The caller of <b>pcre_exec()</b> can set a limit on the number of times the
  197. internal <b>match()</b> function is called and on the maximum depth of
  198. recursive calls. These facilities are provided to catch runaway matches that
  199. are provoked by patterns with huge matching trees (a typical example is a
  200. pattern with nested unlimited repeats) and to avoid running out of system stack
  201. by too much recursion. When one of these limits is reached, <b>pcre_exec()</b>
  202. gives an error return. The limits can also be set by items at the start of the
  203. pattern of the form
  204. <pre>
  205. (*LIMIT_MATCH=d)
  206. (*LIMIT_RECURSION=d)
  207. </pre>
  208. where d is any number of decimal digits. However, the value of the setting must
  209. be less than the value set (or defaulted) by the caller of <b>pcre_exec()</b>
  210. for it to have any effect. In other words, the pattern writer can lower the
  211. limits set by the programmer, but not raise them. If there is more than one
  212. setting of one of these limits, the lower value is used.
  213. </P>
  214. <br><a name="SEC3" href="#TOC1">EBCDIC CHARACTER CODES</a><br>
  215. <P>
  216. PCRE can be compiled to run in an environment that uses EBCDIC as its character
  217. code rather than ASCII or Unicode (typically a mainframe system). In the
  218. sections below, character code values are ASCII or Unicode; in an EBCDIC
  219. environment these characters may have different code values, and there are no
  220. code points greater than 255.
  221. </P>
  222. <br><a name="SEC4" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br>
  223. <P>
  224. A regular expression is a pattern that is matched against a subject string from
  225. left to right. Most characters stand for themselves in a pattern, and match the
  226. corresponding characters in the subject. As a trivial example, the pattern
  227. <pre>
  228. The quick brown fox
  229. </pre>
  230. matches a portion of a subject string that is identical to itself. When
  231. caseless matching is specified (the PCRE_CASELESS option), letters are matched
  232. independently of case. In a UTF mode, PCRE always understands the concept of
  233. case for characters whose values are less than 128, so caseless matching is
  234. always possible. For characters with higher values, the concept of case is
  235. supported if PCRE is compiled with Unicode property support, but not otherwise.
  236. If you want to use caseless matching for characters 128 and above, you must
  237. ensure that PCRE is compiled with Unicode property support as well as with
  238. UTF support.
  239. </P>
  240. <P>
  241. The power of regular expressions comes from the ability to include alternatives
  242. and repetitions in the pattern. These are encoded in the pattern by the use of
  243. <i>metacharacters</i>, which do not stand for themselves but instead are
  244. interpreted in some special way.
  245. </P>
  246. <P>
  247. There are two different sets of metacharacters: those that are recognized
  248. anywhere in the pattern except within square brackets, and those that are
  249. recognized within square brackets. Outside square brackets, the metacharacters
  250. are as follows:
  251. <pre>
  252. \ general escape character with several uses
  253. ^ assert start of string (or line, in multiline mode)
  254. $ assert end of string (or line, in multiline mode)
  255. . match any character except newline (by default)
  256. [ start character class definition
  257. | start of alternative branch
  258. ( start subpattern
  259. ) end subpattern
  260. ? extends the meaning of (
  261. also 0 or 1 quantifier
  262. also quantifier minimizer
  263. * 0 or more quantifier
  264. + 1 or more quantifier
  265. also "possessive quantifier"
  266. { start min/max quantifier
  267. </pre>
  268. Part of a pattern that is in square brackets is called a "character class". In
  269. a character class the only metacharacters are:
  270. <pre>
  271. \ general escape character
  272. ^ negate the class, but only if the first character
  273. - indicates character range
  274. [ POSIX character class (only if followed by POSIX syntax)
  275. ] terminates the character class
  276. </pre>
  277. The following sections describe the use of each of the metacharacters.
  278. </P>
  279. <br><a name="SEC5" href="#TOC1">BACKSLASH</a><br>
  280. <P>
  281. The backslash character has several uses. Firstly, if it is followed by a
  282. character that is not a number or a letter, it takes away any special meaning
  283. that character may have. This use of backslash as an escape character applies
  284. both inside and outside character classes.
  285. </P>
  286. <P>
  287. For example, if you want to match a * character, you write \* in the pattern.
  288. This escaping action applies whether or not the following character would
  289. otherwise be interpreted as a metacharacter, so it is always safe to precede a
  290. non-alphanumeric with backslash to specify that it stands for itself. In
  291. particular, if you want to match a backslash, you write \\.
  292. </P>
  293. <P>
  294. In a UTF mode, only ASCII numbers and letters have any special meaning after a
  295. backslash. All other characters (in particular, those whose codepoints are
  296. greater than 127) are treated as literals.
  297. </P>
  298. <P>
  299. If a pattern is compiled with the PCRE_EXTENDED option, most white space in the
  300. pattern (other than in a character class), and characters between a # outside a
  301. character class and the next newline, inclusive, are ignored. An escaping
  302. backslash can be used to include a white space or # character as part of the
  303. pattern.
  304. </P>
  305. <P>
  306. If you want to remove the special meaning from a sequence of characters, you
  307. can do so by putting them between \Q and \E. This is different from Perl in
  308. that $ and @ are handled as literals in \Q...\E sequences in PCRE, whereas in
  309. Perl, $ and @ cause variable interpolation. Note the following examples:
  310. <pre>
  311. Pattern PCRE matches Perl matches
  312. \Qabc$xyz\E abc$xyz abc followed by the contents of $xyz
  313. \Qabc\$xyz\E abc\$xyz abc\$xyz
  314. \Qabc\E\$\Qxyz\E abc$xyz abc$xyz
  315. </pre>
  316. The \Q...\E sequence is recognized both inside and outside character classes.
  317. An isolated \E that is not preceded by \Q is ignored. If \Q is not followed
  318. by \E later in the pattern, the literal interpretation continues to the end of
  319. the pattern (that is, \E is assumed at the end). If the isolated \Q is inside
  320. a character class, this causes an error, because the character class is not
  321. terminated.
  322. <a name="digitsafterbackslash"></a></P>
  323. <br><b>
  324. Non-printing characters
  325. </b><br>
  326. <P>
  327. A second use of backslash provides a way of encoding non-printing characters
  328. in patterns in a visible manner. There is no restriction on the appearance of
  329. non-printing characters, apart from the binary zero that terminates a pattern,
  330. but when a pattern is being prepared by text editing, it is often easier to use
  331. one of the following escape sequences than the binary character it represents.
  332. In an ASCII or Unicode environment, these escapes are as follows:
  333. <pre>
  334. \a alarm, that is, the BEL character (hex 07)
  335. \cx "control-x", where x is any ASCII character
  336. \e escape (hex 1B)
  337. \f form feed (hex 0C)
  338. \n linefeed (hex 0A)
  339. \r carriage return (hex 0D)
  340. \t tab (hex 09)
  341. \0dd character with octal code 0dd
  342. \ddd character with octal code ddd, or back reference
  343. \o{ddd..} character with octal code ddd..
  344. \xhh character with hex code hh
  345. \x{hhh..} character with hex code hhh.. (non-JavaScript mode)
  346. \uhhhh character with hex code hhhh (JavaScript mode only)
  347. </pre>
  348. The precise effect of \cx on ASCII characters is as follows: if x is a lower
  349. case letter, it is converted to upper case. Then bit 6 of the character (hex
  350. 40) is inverted. Thus \cA to \cZ become hex 01 to hex 1A (A is 41, Z is 5A),
  351. but \c{ becomes hex 3B ({ is 7B), and \c; becomes hex 7B (; is 3B). If the
  352. data item (byte or 16-bit value) following \c has a value greater than 127, a
  353. compile-time error occurs. This locks out non-ASCII characters in all modes.
  354. </P>
  355. <P>
  356. When PCRE is compiled in EBCDIC mode, \a, \e, \f, \n, \r, and \t
  357. generate the appropriate EBCDIC code values. The \c escape is processed
  358. as specified for Perl in the <b>perlebcdic</b> document. The only characters
  359. that are allowed after \c are A-Z, a-z, or one of @, [, \, ], ^, _, or ?. Any
  360. other character provokes a compile-time error. The sequence \c@ encodes
  361. character code 0; after \c the letters (in either case) encode characters 1-26
  362. (hex 01 to hex 1A); [, \, ], ^, and _ encode characters 27-31 (hex 1B to hex
  363. 1F), and \c? becomes either 255 (hex FF) or 95 (hex 5F).
  364. </P>
  365. <P>
  366. Thus, apart from \c?, these escapes generate the same character code values as
  367. they do in an ASCII environment, though the meanings of the values mostly
  368. differ. For example, \cG always generates code value 7, which is BEL in ASCII
  369. but DEL in EBCDIC.
  370. </P>
  371. <P>
  372. The sequence \c? generates DEL (127, hex 7F) in an ASCII environment, but
  373. because 127 is not a control character in EBCDIC, Perl makes it generate the
  374. APC character. Unfortunately, there are several variants of EBCDIC. In most of
  375. them the APC character has the value 255 (hex FF), but in the one Perl calls
  376. POSIX-BC its value is 95 (hex 5F). If certain other characters have POSIX-BC
  377. values, PCRE makes \c? generate 95; otherwise it generates 255.
  378. </P>
  379. <P>
  380. After \0 up to two further octal digits are read. If there are fewer than two
  381. digits, just those that are present are used. Thus the sequence \0\x\015
  382. specifies two binary zeros followed by a CR character (code value 13). Make
  383. sure you supply two digits after the initial zero if the pattern character that
  384. follows is itself an octal digit.
  385. </P>
  386. <P>
  387. The escape \o must be followed by a sequence of octal digits, enclosed in
  388. braces. An error occurs if this is not the case. This escape is a recent
  389. addition to Perl; it provides way of specifying character code points as octal
  390. numbers greater than 0777, and it also allows octal numbers and back references
  391. to be unambiguously specified.
  392. </P>
  393. <P>
  394. For greater clarity and unambiguity, it is best to avoid following \ by a
  395. digit greater than zero. Instead, use \o{} or \x{} to specify character
  396. numbers, and \g{} to specify back references. The following paragraphs
  397. describe the old, ambiguous syntax.
  398. </P>
  399. <P>
  400. The handling of a backslash followed by a digit other than 0 is complicated,
  401. and Perl has changed in recent releases, causing PCRE also to change. Outside a
  402. character class, PCRE reads the digit and any following digits as a decimal
  403. number. If the number is less than 8, or if there have been at least that many
  404. previous capturing left parentheses in the expression, the entire sequence is
  405. taken as a <i>back reference</i>. A description of how this works is given
  406. <a href="#backreferences">later,</a>
  407. following the discussion of
  408. <a href="#subpattern">parenthesized subpatterns.</a>
  409. </P>
  410. <P>
  411. Inside a character class, or if the decimal number following \ is greater than
  412. 7 and there have not been that many capturing subpatterns, PCRE handles \8 and
  413. \9 as the literal characters "8" and "9", and otherwise re-reads up to three
  414. octal digits following the backslash, using them to generate a data character.
  415. Any subsequent digits stand for themselves. For example:
  416. <pre>
  417. \040 is another way of writing an ASCII space
  418. \40 is the same, provided there are fewer than 40 previous capturing subpatterns
  419. \7 is always a back reference
  420. \11 might be a back reference, or another way of writing a tab
  421. \011 is always a tab
  422. \0113 is a tab followed by the character "3"
  423. \113 might be a back reference, otherwise the character with octal code 113
  424. \377 might be a back reference, otherwise the value 255 (decimal)
  425. \81 is either a back reference, or the two characters "8" and "1"
  426. </pre>
  427. Note that octal values of 100 or greater that are specified using this syntax
  428. must not be introduced by a leading zero, because no more than three octal
  429. digits are ever read.
  430. </P>
  431. <P>
  432. By default, after \x that is not followed by {, from zero to two hexadecimal
  433. digits are read (letters can be in upper or lower case). Any number of
  434. hexadecimal digits may appear between \x{ and }. If a character other than
  435. a hexadecimal digit appears between \x{ and }, or if there is no terminating
  436. }, an error occurs.
  437. </P>
  438. <P>
  439. If the PCRE_JAVASCRIPT_COMPAT option is set, the interpretation of \x is
  440. as just described only when it is followed by two hexadecimal digits.
  441. Otherwise, it matches a literal "x" character. In JavaScript mode, support for
  442. code points greater than 256 is provided by \u, which must be followed by
  443. four hexadecimal digits; otherwise it matches a literal "u" character.
  444. </P>
  445. <P>
  446. Characters whose value is less than 256 can be defined by either of the two
  447. syntaxes for \x (or by \u in JavaScript mode). There is no difference in the
  448. way they are handled. For example, \xdc is exactly the same as \x{dc} (or
  449. \u00dc in JavaScript mode).
  450. </P>
  451. <br><b>
  452. Constraints on character values
  453. </b><br>
  454. <P>
  455. Characters that are specified using octal or hexadecimal numbers are
  456. limited to certain values, as follows:
  457. <pre>
  458. 8-bit non-UTF mode less than 0x100
  459. 8-bit UTF-8 mode less than 0x10ffff and a valid codepoint
  460. 16-bit non-UTF mode less than 0x10000
  461. 16-bit UTF-16 mode less than 0x10ffff and a valid codepoint
  462. 32-bit non-UTF mode less than 0x100000000
  463. 32-bit UTF-32 mode less than 0x10ffff and a valid codepoint
  464. </pre>
  465. Invalid Unicode codepoints are the range 0xd800 to 0xdfff (the so-called
  466. "surrogate" codepoints), and 0xffef.
  467. </P>
  468. <br><b>
  469. Escape sequences in character classes
  470. </b><br>
  471. <P>
  472. All the sequences that define a single character value can be used both inside
  473. and outside character classes. In addition, inside a character class, \b is
  474. interpreted as the backspace character (hex 08).
  475. </P>
  476. <P>
  477. \N is not allowed in a character class. \B, \R, and \X are not special
  478. inside a character class. Like other unrecognized escape sequences, they are
  479. treated as the literal characters "B", "R", and "X" by default, but cause an
  480. error if the PCRE_EXTRA option is set. Outside a character class, these
  481. sequences have different meanings.
  482. </P>
  483. <br><b>
  484. Unsupported escape sequences
  485. </b><br>
  486. <P>
  487. In Perl, the sequences \l, \L, \u, and \U are recognized by its string
  488. handler and used to modify the case of following characters. By default, PCRE
  489. does not support these escape sequences. However, if the PCRE_JAVASCRIPT_COMPAT
  490. option is set, \U matches a "U" character, and \u can be used to define a
  491. character by code point, as described in the previous section.
  492. </P>
  493. <br><b>
  494. Absolute and relative back references
  495. </b><br>
  496. <P>
  497. The sequence \g followed by an unsigned or a negative number, optionally
  498. enclosed in braces, is an absolute or relative back reference. A named back
  499. reference can be coded as \g{name}. Back references are discussed
  500. <a href="#backreferences">later,</a>
  501. following the discussion of
  502. <a href="#subpattern">parenthesized subpatterns.</a>
  503. </P>
  504. <br><b>
  505. Absolute and relative subroutine calls
  506. </b><br>
  507. <P>
  508. For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
  509. a number enclosed either in angle brackets or single quotes, is an alternative
  510. syntax for referencing a subpattern as a "subroutine". Details are discussed
  511. <a href="#onigurumasubroutines">later.</a>
  512. Note that \g{...} (Perl syntax) and \g&#60;...&#62; (Oniguruma syntax) are <i>not</i>
  513. synonymous. The former is a back reference; the latter is a
  514. <a href="#subpatternsassubroutines">subroutine</a>
  515. call.
  516. <a name="genericchartypes"></a></P>
  517. <br><b>
  518. Generic character types
  519. </b><br>
  520. <P>
  521. Another use of backslash is for specifying generic character types:
  522. <pre>
  523. \d any decimal digit
  524. \D any character that is not a decimal digit
  525. \h any horizontal white space character
  526. \H any character that is not a horizontal white space character
  527. \s any white space character
  528. \S any character that is not a white space character
  529. \v any vertical white space character
  530. \V any character that is not a vertical white space character
  531. \w any "word" character
  532. \W any "non-word" character
  533. </pre>
  534. There is also the single sequence \N, which matches a non-newline character.
  535. This is the same as
  536. <a href="#fullstopdot">the "." metacharacter</a>
  537. when PCRE_DOTALL is not set. Perl also uses \N to match characters by name;
  538. PCRE does not support this.
  539. </P>
  540. <P>
  541. Each pair of lower and upper case escape sequences partitions the complete set
  542. of characters into two disjoint sets. Any given character matches one, and only
  543. one, of each pair. The sequences can appear both inside and outside character
  544. classes. They each match one character of the appropriate type. If the current
  545. matching point is at the end of the subject string, all of them fail, because
  546. there is no character to match.
  547. </P>
  548. <P>
  549. For compatibility with Perl, \s did not used to match the VT character (code
  550. 11), which made it different from the the POSIX "space" class. However, Perl
  551. added VT at release 5.18, and PCRE followed suit at release 8.34. The default
  552. \s characters are now HT (9), LF (10), VT (11), FF (12), CR (13), and space
  553. (32), which are defined as white space in the "C" locale. This list may vary if
  554. locale-specific matching is taking place. For example, in some locales the
  555. "non-breaking space" character (\xA0) is recognized as white space, and in
  556. others the VT character is not.
  557. </P>
  558. <P>
  559. A "word" character is an underscore or any character that is a letter or digit.
  560. By default, the definition of letters and digits is controlled by PCRE's
  561. low-valued character tables, and may vary if locale-specific matching is taking
  562. place (see
  563. <a href="pcreapi.html#localesupport">"Locale support"</a>
  564. in the
  565. <a href="pcreapi.html"><b>pcreapi</b></a>
  566. page). For example, in a French locale such as "fr_FR" in Unix-like systems,
  567. or "french" in Windows, some character codes greater than 127 are used for
  568. accented letters, and these are then matched by \w. The use of locales with
  569. Unicode is discouraged.
  570. </P>
  571. <P>
  572. By default, characters whose code points are greater than 127 never match \d,
  573. \s, or \w, and always match \D, \S, and \W, although this may vary for
  574. characters in the range 128-255 when locale-specific matching is happening.
  575. These escape sequences retain their original meanings from before Unicode
  576. support was available, mainly for efficiency reasons. If PCRE is compiled with
  577. Unicode property support, and the PCRE_UCP option is set, the behaviour is
  578. changed so that Unicode properties are used to determine character types, as
  579. follows:
  580. <pre>
  581. \d any character that matches \p{Nd} (decimal digit)
  582. \s any character that matches \p{Z} or \h or \v
  583. \w any character that matches \p{L} or \p{N}, plus underscore
  584. </pre>
  585. The upper case escapes match the inverse sets of characters. Note that \d
  586. matches only decimal digits, whereas \w matches any Unicode digit, as well as
  587. any Unicode letter, and underscore. Note also that PCRE_UCP affects \b, and
  588. \B because they are defined in terms of \w and \W. Matching these sequences
  589. is noticeably slower when PCRE_UCP is set.
  590. </P>
  591. <P>
  592. The sequences \h, \H, \v, and \V are features that were added to Perl at
  593. release 5.10. In contrast to the other sequences, which match only ASCII
  594. characters by default, these always match certain high-valued code points,
  595. whether or not PCRE_UCP is set. The horizontal space characters are:
  596. <pre>
  597. U+0009 Horizontal tab (HT)
  598. U+0020 Space
  599. U+00A0 Non-break space
  600. U+1680 Ogham space mark
  601. U+180E Mongolian vowel separator
  602. U+2000 En quad
  603. U+2001 Em quad
  604. U+2002 En space
  605. U+2003 Em space
  606. U+2004 Three-per-em space
  607. U+2005 Four-per-em space
  608. U+2006 Six-per-em space
  609. U+2007 Figure space
  610. U+2008 Punctuation space
  611. U+2009 Thin space
  612. U+200A Hair space
  613. U+202F Narrow no-break space
  614. U+205F Medium mathematical space
  615. U+3000 Ideographic space
  616. </pre>
  617. The vertical space characters are:
  618. <pre>
  619. U+000A Linefeed (LF)
  620. U+000B Vertical tab (VT)
  621. U+000C Form feed (FF)
  622. U+000D Carriage return (CR)
  623. U+0085 Next line (NEL)
  624. U+2028 Line separator
  625. U+2029 Paragraph separator
  626. </pre>
  627. In 8-bit, non-UTF-8 mode, only the characters with codepoints less than 256 are
  628. relevant.
  629. <a name="newlineseq"></a></P>
  630. <br><b>
  631. Newline sequences
  632. </b><br>
  633. <P>
  634. Outside a character class, by default, the escape sequence \R matches any
  635. Unicode newline sequence. In 8-bit non-UTF-8 mode \R is equivalent to the
  636. following:
  637. <pre>
  638. (?&#62;\r\n|\n|\x0b|\f|\r|\x85)
  639. </pre>
  640. This is an example of an "atomic group", details of which are given
  641. <a href="#atomicgroup">below.</a>
  642. This particular group matches either the two-character sequence CR followed by
  643. LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,
  644. U+000B), FF (form feed, U+000C), CR (carriage return, U+000D), or NEL (next
  645. line, U+0085). The two-character sequence is treated as a single unit that
  646. cannot be split.
  647. </P>
  648. <P>
  649. In other modes, two additional characters whose codepoints are greater than 255
  650. are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).
  651. Unicode character property support is not needed for these characters to be
  652. recognized.
  653. </P>
  654. <P>
  655. It is possible to restrict \R to match only CR, LF, or CRLF (instead of the
  656. complete set of Unicode line endings) by setting the option PCRE_BSR_ANYCRLF
  657. either at compile time or when the pattern is matched. (BSR is an abbrevation
  658. for "backslash R".) This can be made the default when PCRE is built; if this is
  659. the case, the other behaviour can be requested via the PCRE_BSR_UNICODE option.
  660. It is also possible to specify these settings by starting a pattern string with
  661. one of the following sequences:
  662. <pre>
  663. (*BSR_ANYCRLF) CR, LF, or CRLF only
  664. (*BSR_UNICODE) any Unicode newline sequence
  665. </pre>
  666. These override the default and the options given to the compiling function, but
  667. they can themselves be overridden by options given to a matching function. Note
  668. that these special settings, which are not Perl-compatible, are recognized only
  669. at the very start of a pattern, and that they must be in upper case. If more
  670. than one of them is present, the last one is used. They can be combined with a
  671. change of newline convention; for example, a pattern can start with:
  672. <pre>
  673. (*ANY)(*BSR_ANYCRLF)
  674. </pre>
  675. They can also be combined with the (*UTF8), (*UTF16), (*UTF32), (*UTF) or
  676. (*UCP) special sequences. Inside a character class, \R is treated as an
  677. unrecognized escape sequence, and so matches the letter "R" by default, but
  678. causes an error if PCRE_EXTRA is set.
  679. <a name="uniextseq"></a></P>
  680. <br><b>
  681. Unicode character properties
  682. </b><br>
  683. <P>
  684. When PCRE is built with Unicode character property support, three additional
  685. escape sequences that match characters with specific properties are available.
  686. When in 8-bit non-UTF-8 mode, these sequences are of course limited to testing
  687. characters whose codepoints are less than 256, but they do work in this mode.
  688. The extra escape sequences are:
  689. <pre>
  690. \p{<i>xx</i>} a character with the <i>xx</i> property
  691. \P{<i>xx</i>} a character without the <i>xx</i> property
  692. \X a Unicode extended grapheme cluster
  693. </pre>
  694. The property names represented by <i>xx</i> above are limited to the Unicode
  695. script names, the general category properties, "Any", which matches any
  696. character (including newline), and some special PCRE properties (described
  697. in the
  698. <a href="#extraprops">next section).</a>
  699. Other Perl properties such as "InMusicalSymbols" are not currently supported by
  700. PCRE. Note that \P{Any} does not match any characters, so always causes a
  701. match failure.
  702. </P>
  703. <P>
  704. Sets of Unicode characters are defined as belonging to certain scripts. A
  705. character from one of these sets can be matched using a script name. For
  706. example:
  707. <pre>
  708. \p{Greek}
  709. \P{Han}
  710. </pre>
  711. Those that are not part of an identified script are lumped together as
  712. "Common". The current list of scripts is:
  713. </P>
  714. <P>
  715. Arabic,
  716. Armenian,
  717. Avestan,
  718. Balinese,
  719. Bamum,
  720. Bassa_Vah,
  721. Batak,
  722. Bengali,
  723. Bopomofo,
  724. Brahmi,
  725. Braille,
  726. Buginese,
  727. Buhid,
  728. Canadian_Aboriginal,
  729. Carian,
  730. Caucasian_Albanian,
  731. Chakma,
  732. Cham,
  733. Cherokee,
  734. Common,
  735. Coptic,
  736. Cuneiform,
  737. Cypriot,
  738. Cyrillic,
  739. Deseret,
  740. Devanagari,
  741. Duployan,
  742. Egyptian_Hieroglyphs,
  743. Elbasan,
  744. Ethiopic,
  745. Georgian,
  746. Glagolitic,
  747. Gothic,
  748. Grantha,
  749. Greek,
  750. Gujarati,
  751. Gurmukhi,
  752. Han,
  753. Hangul,
  754. Hanunoo,
  755. Hebrew,
  756. Hiragana,
  757. Imperial_Aramaic,
  758. Inherited,
  759. Inscriptional_Pahlavi,
  760. Inscriptional_Parthian,
  761. Javanese,
  762. Kaithi,
  763. Kannada,
  764. Katakana,
  765. Kayah_Li,
  766. Kharoshthi,
  767. Khmer,
  768. Khojki,
  769. Khudawadi,
  770. Lao,
  771. Latin,
  772. Lepcha,
  773. Limbu,
  774. Linear_A,
  775. Linear_B,
  776. Lisu,
  777. Lycian,
  778. Lydian,
  779. Mahajani,
  780. Malayalam,
  781. Mandaic,
  782. Manichaean,
  783. Meetei_Mayek,
  784. Mende_Kikakui,
  785. Meroitic_Cursive,
  786. Meroitic_Hieroglyphs,
  787. Miao,
  788. Modi,
  789. Mongolian,
  790. Mro,
  791. Myanmar,
  792. Nabataean,
  793. New_Tai_Lue,
  794. Nko,
  795. Ogham,
  796. Ol_Chiki,
  797. Old_Italic,
  798. Old_North_Arabian,
  799. Old_Permic,
  800. Old_Persian,
  801. Old_South_Arabian,
  802. Old_Turkic,
  803. Oriya,
  804. Osmanya,
  805. Pahawh_Hmong,
  806. Palmyrene,
  807. Pau_Cin_Hau,
  808. Phags_Pa,
  809. Phoenician,
  810. Psalter_Pahlavi,
  811. Rejang,
  812. Runic,
  813. Samaritan,
  814. Saurashtra,
  815. Sharada,
  816. Shavian,
  817. Siddham,
  818. Sinhala,
  819. Sora_Sompeng,
  820. Sundanese,
  821. Syloti_Nagri,
  822. Syriac,
  823. Tagalog,
  824. Tagbanwa,
  825. Tai_Le,
  826. Tai_Tham,
  827. Tai_Viet,
  828. Takri,
  829. Tamil,
  830. Telugu,
  831. Thaana,
  832. Thai,
  833. Tibetan,
  834. Tifinagh,
  835. Tirhuta,
  836. Ugaritic,
  837. Vai,
  838. Warang_Citi,
  839. Yi.
  840. </P>
  841. <P>
  842. Each character has exactly one Unicode general category property, specified by
  843. a two-letter abbreviation. For compatibility with Perl, negation can be
  844. specified by including a circumflex between the opening brace and the property
  845. name. For example, \p{^Lu} is the same as \P{Lu}.
  846. </P>
  847. <P>
  848. If only one letter is specified with \p or \P, it includes all the general
  849. category properties that start with that letter. In this case, in the absence
  850. of negation, the curly brackets in the escape sequence are optional; these two
  851. examples have the same effect:
  852. <pre>
  853. \p{L}
  854. \pL
  855. </pre>
  856. The following general category property codes are supported:
  857. <pre>
  858. C Other
  859. Cc Control
  860. Cf Format
  861. Cn Unassigned
  862. Co Private use
  863. Cs Surrogate
  864. L Letter
  865. Ll Lower case letter
  866. Lm Modifier letter
  867. Lo Other letter
  868. Lt Title case letter
  869. Lu Upper case letter
  870. M Mark
  871. Mc Spacing mark
  872. Me Enclosing mark
  873. Mn Non-spacing mark
  874. N Number
  875. Nd Decimal number
  876. Nl Letter number
  877. No Other number
  878. P Punctuation
  879. Pc Connector punctuation
  880. Pd Dash punctuation
  881. Pe Close punctuation
  882. Pf Final punctuation
  883. Pi Initial punctuation
  884. Po Other punctuation
  885. Ps Open punctuation
  886. S Symbol
  887. Sc Currency symbol
  888. Sk Modifier symbol
  889. Sm Mathematical symbol
  890. So Other symbol
  891. Z Separator
  892. Zl Line separator
  893. Zp Paragraph separator
  894. Zs Space separator
  895. </pre>
  896. The special property L& is also supported: it matches a character that has
  897. the Lu, Ll, or Lt property, in other words, a letter that is not classified as
  898. a modifier or "other".
  899. </P>
  900. <P>
  901. The Cs (Surrogate) property applies only to characters in the range U+D800 to
  902. U+DFFF. Such characters are not valid in Unicode strings and so
  903. cannot be tested by PCRE, unless UTF validity checking has been turned off
  904. (see the discussion of PCRE_NO_UTF8_CHECK, PCRE_NO_UTF16_CHECK and
  905. PCRE_NO_UTF32_CHECK in the
  906. <a href="pcreapi.html"><b>pcreapi</b></a>
  907. page). Perl does not support the Cs property.
  908. </P>
  909. <P>
  910. The long synonyms for property names that Perl supports (such as \p{Letter})
  911. are not supported by PCRE, nor is it permitted to prefix any of these
  912. properties with "Is".
  913. </P>
  914. <P>
  915. No character that is in the Unicode table has the Cn (unassigned) property.
  916. Instead, this property is assumed for any code point that is not in the
  917. Unicode table.
  918. </P>
  919. <P>
  920. Specifying caseless matching does not affect these escape sequences. For
  921. example, \p{Lu} always matches only upper case letters. This is different from
  922. the behaviour of current versions of Perl.
  923. </P>
  924. <P>
  925. Matching characters by Unicode property is not fast, because PCRE has to do a
  926. multistage table lookup in order to find a character's property. That is why
  927. the traditional escape sequences such as \d and \w do not use Unicode
  928. properties in PCRE by default, though you can make them do so by setting the
  929. PCRE_UCP option or by starting the pattern with (*UCP).
  930. </P>
  931. <br><b>
  932. Extended grapheme clusters
  933. </b><br>
  934. <P>
  935. The \X escape matches any number of Unicode characters that form an "extended
  936. grapheme cluster", and treats the sequence as an atomic group
  937. <a href="#atomicgroup">(see below).</a>
  938. Up to and including release 8.31, PCRE matched an earlier, simpler definition
  939. that was equivalent to
  940. <pre>
  941. (?&#62;\PM\pM*)
  942. </pre>
  943. That is, it matched a character without the "mark" property, followed by zero
  944. or more characters with the "mark" property. Characters with the "mark"
  945. property are typically non-spacing accents that affect the preceding character.
  946. </P>
  947. <P>
  948. This simple definition was extended in Unicode to include more complicated
  949. kinds of composite character by giving each character a grapheme breaking
  950. property, and creating rules that use these properties to define the boundaries
  951. of extended grapheme clusters. In releases of PCRE later than 8.31, \X matches
  952. one of these clusters.
  953. </P>
  954. <P>
  955. \X always matches at least one character. Then it decides whether to add
  956. additional characters according to the following rules for ending a cluster:
  957. </P>
  958. <P>
  959. 1. End at the end of the subject string.
  960. </P>
  961. <P>
  962. 2. Do not end between CR and LF; otherwise end after any control character.
  963. </P>
  964. <P>
  965. 3. Do not break Hangul (a Korean script) syllable sequences. Hangul characters
  966. are of five types: L, V, T, LV, and LVT. An L character may be followed by an
  967. L, V, LV, or LVT character; an LV or V character may be followed by a V or T
  968. character; an LVT or T character may be follwed only by a T character.
  969. </P>
  970. <P>
  971. 4. Do not end before extending characters or spacing marks. Characters with
  972. the "mark" property always have the "extend" grapheme breaking property.
  973. </P>
  974. <P>
  975. 5. Do not end after prepend characters.
  976. </P>
  977. <P>
  978. 6. Otherwise, end the cluster.
  979. <a name="extraprops"></a></P>
  980. <br><b>
  981. PCRE's additional properties
  982. </b><br>
  983. <P>
  984. As well as the standard Unicode properties described above, PCRE supports four
  985. more that make it possible to convert traditional escape sequences such as \w
  986. and \s to use Unicode properties. PCRE uses these non-standard, non-Perl
  987. properties internally when PCRE_UCP is set. However, they may also be used
  988. explicitly. These properties are:
  989. <pre>
  990. Xan Any alphanumeric character
  991. Xps Any POSIX space character
  992. Xsp Any Perl space character
  993. Xwd Any Perl "word" character
  994. </pre>
  995. Xan matches characters that have either the L (letter) or the N (number)
  996. property. Xps matches the characters tab, linefeed, vertical tab, form feed, or
  997. carriage return, and any other character that has the Z (separator) property.
  998. Xsp is the same as Xps; it used to exclude vertical tab, for Perl
  999. compatibility, but Perl changed, and so PCRE followed at release 8.34. Xwd
  1000. matches the same characters as Xan, plus underscore.
  1001. </P>
  1002. <P>
  1003. There is another non-standard property, Xuc, which matches any character that
  1004. can be represented by a Universal Character Name in C++ and other programming
  1005. languages. These are the characters $, @, ` (grave accent), and all characters
  1006. with Unicode code points greater than or equal to U+00A0, except for the
  1007. surrogates U+D800 to U+DFFF. Note that most base (ASCII) characters are
  1008. excluded. (Universal Character Names are of the form \uHHHH or \UHHHHHHHH
  1009. where H is a hexadecimal digit. Note that the Xuc property does not match these
  1010. sequences but the characters that they represent.)
  1011. <a name="resetmatchstart"></a></P>
  1012. <br><b>
  1013. Resetting the match start
  1014. </b><br>
  1015. <P>
  1016. The escape sequence \K causes any previously matched characters not to be
  1017. included in the final matched sequence. For example, the pattern:
  1018. <pre>
  1019. foo\Kbar
  1020. </pre>
  1021. matches "foobar", but reports that it has matched "bar". This feature is
  1022. similar to a lookbehind assertion
  1023. <a href="#lookbehind">(described below).</a>
  1024. However, in this case, the part of the subject before the real match does not
  1025. have to be of fixed length, as lookbehind assertions do. The use of \K does
  1026. not interfere with the setting of
  1027. <a href="#subpattern">captured substrings.</a>
  1028. For example, when the pattern
  1029. <pre>
  1030. (foo)\Kbar
  1031. </pre>
  1032. matches "foobar", the first substring is still set to "foo".
  1033. </P>
  1034. <P>
  1035. Perl documents that the use of \K within assertions is "not well defined". In
  1036. PCRE, \K is acted upon when it occurs inside positive assertions, but is
  1037. ignored in negative assertions. Note that when a pattern such as (?=ab\K)
  1038. matches, the reported start of the match can be greater than the end of the
  1039. match.
  1040. <a name="smallassertions"></a></P>
  1041. <br><b>
  1042. Simple assertions
  1043. </b><br>
  1044. <P>
  1045. The final use of backslash is for certain simple assertions. An assertion
  1046. specifies a condition that has to be met at a particular point in a match,
  1047. without consuming any characters from the subject string. The use of
  1048. subpatterns for more complicated assertions is described
  1049. <a href="#bigassertions">below.</a>
  1050. The backslashed assertions are:
  1051. <pre>
  1052. \b matches at a word boundary
  1053. \B matches when not at a word boundary
  1054. \A matches at the start of the subject
  1055. \Z matches at the end of the subject
  1056. also matches before a newline at the end of the subject
  1057. \z matches only at the end of the subject
  1058. \G matches at the first matching position in the subject
  1059. </pre>
  1060. Inside a character class, \b has a different meaning; it matches the backspace
  1061. character. If any other of these assertions appears in a character class, by
  1062. default it matches the corresponding literal character (for example, \B
  1063. matches the letter B). However, if the PCRE_EXTRA option is set, an "invalid
  1064. escape sequence" error is generated instead.
  1065. </P>
  1066. <P>
  1067. A word boundary is a position in the subject string where the current character
  1068. and the previous character do not both match \w or \W (i.e. one matches
  1069. \w and the other matches \W), or the start or end of the string if the
  1070. first or last character matches \w, respectively. In a UTF mode, the meanings
  1071. of \w and \W can be changed by setting the PCRE_UCP option. When this is
  1072. done, it also affects \b and \B. Neither PCRE nor Perl has a separate "start
  1073. of word" or "end of word" metasequence. However, whatever follows \b normally
  1074. determines which it is. For example, the fragment \ba matches "a" at the start
  1075. of a word.
  1076. </P>
  1077. <P>
  1078. The \A, \Z, and \z assertions differ from the traditional circumflex and
  1079. dollar (described in the next section) in that they only ever match at the very
  1080. start and end of the subject string, whatever options are set. Thus, they are
  1081. independent of multiline mode. These three assertions are not affected by the
  1082. PCRE_NOTBOL or PCRE_NOTEOL options, which affect only the behaviour of the
  1083. circumflex and dollar metacharacters. However, if the <i>startoffset</i>
  1084. argument of <b>pcre_exec()</b> is non-zero, indicating that matching is to start
  1085. at a point other than the beginning of the subject, \A can never match. The
  1086. difference between \Z and \z is that \Z matches before a newline at the end
  1087. of the string as well as at the very end, whereas \z matches only at the end.
  1088. </P>
  1089. <P>
  1090. The \G assertion is true only when the current matching position is at the
  1091. start point of the match, as specified by the <i>startoffset</i> argument of
  1092. <b>pcre_exec()</b>. It differs from \A when the value of <i>startoffset</i> is
  1093. non-zero. By calling <b>pcre_exec()</b> multiple times with appropriate
  1094. arguments, you can mimic Perl's /g option, and it is in this kind of
  1095. implementation where \G can be useful.
  1096. </P>
  1097. <P>
  1098. Note, however, that PCRE's interpretation of \G, as the start of the current
  1099. match, is subtly different from Perl's, which defines it as the end of the
  1100. previous match. In Perl, these can be different when the previously matched
  1101. string was empty. Because PCRE does just one match at a time, it cannot
  1102. reproduce this behaviour.
  1103. </P>
  1104. <P>
  1105. If all the alternatives of a pattern begin with \G, the expression is anchored
  1106. to the starting match position, and the "anchored" flag is set in the compiled
  1107. regular expression.
  1108. </P>
  1109. <br><a name="SEC6" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>
  1110. <P>
  1111. The circumflex and dollar metacharacters are zero-width assertions. That is,
  1112. they test for a particular condition being true without consuming any
  1113. characters from the subject string.
  1114. </P>
  1115. <P>
  1116. Outside a character class, in the default matching mode, the circumflex
  1117. character is an assertion that is true only if the current matching point is at
  1118. the start of the subject string. If the <i>startoffset</i> argument of
  1119. <b>pcre_exec()</b> is non-zero, circumflex can never match if the PCRE_MULTILINE
  1120. option is unset. Inside a character class, circumflex has an entirely different
  1121. meaning
  1122. <a href="#characterclass">(see below).</a>
  1123. </P>
  1124. <P>
  1125. Circumflex need not be the first character of the pattern if a number of
  1126. alternatives are involved, but it should be the first thing in each alternative
  1127. in which it appears if the pattern is ever to match that branch. If all
  1128. possible alternatives start with a circumflex, that is, if the pattern is
  1129. constrained to match only at the start of the subject, it is said to be an
  1130. "anchored" pattern. (There are also other constructs that can cause a pattern
  1131. to be anchored.)
  1132. </P>
  1133. <P>
  1134. The dollar character is an assertion that is true only if the current matching
  1135. point is at the end of the subject string, or immediately before a newline at
  1136. the end of the string (by default). Note, however, that it does not actually
  1137. match the newline. Dollar need not be the last character of the pattern if a
  1138. number of alternatives are involved, but it should be the last item in any
  1139. branch in which it appears. Dollar has no special meaning in a character class.
  1140. </P>
  1141. <P>
  1142. The meaning of dollar can be changed so that it matches only at the very end of
  1143. the string, by setting the PCRE_DOLLAR_ENDONLY option at compile time. This
  1144. does not affect the \Z assertion.
  1145. </P>
  1146. <P>
  1147. The meanings of the circumflex and dollar characters are changed if the
  1148. PCRE_MULTILINE option is set. When this is the case, a circumflex matches
  1149. immediately after internal newlines as well as at the start of the subject
  1150. string. It does not match after a newline that ends the string. A dollar
  1151. matches before any newlines in the string, as well as at the very end, when
  1152. PCRE_MULTILINE is set. When newline is specified as the two-character
  1153. sequence CRLF, isolated CR and LF characters do not indicate newlines.
  1154. </P>
  1155. <P>
  1156. For example, the pattern /^abc$/ matches the subject string "def\nabc" (where
  1157. \n represents a newline) in multiline mode, but not otherwise. Consequently,
  1158. patterns that are anchored in single line mode because all branches start with
  1159. ^ are not anchored in multiline mode, and a match for circumflex is possible
  1160. when the <i>startoffset</i> argument of <b>pcre_exec()</b> is non-zero. The
  1161. PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set.
  1162. </P>
  1163. <P>
  1164. Note that the sequences \A, \Z, and \z can be used to match the start and
  1165. end of the subject in both modes, and if all branches of a pattern start with
  1166. \A it is always anchored, whether or not PCRE_MULTILINE is set.
  1167. <a name="fullstopdot"></a></P>
  1168. <br><a name="SEC7" href="#TOC1">FULL STOP (PERIOD, DOT) AND \N</a><br>
  1169. <P>
  1170. Outside a character class, a dot in the pattern matches any one character in
  1171. the subject string except (by default) a character that signifies the end of a
  1172. line.
  1173. </P>
  1174. <P>
  1175. When a line ending is defined as a single character, dot never matches that
  1176. character; when the two-character sequence CRLF is used, dot does not match CR
  1177. if it is immediately followed by LF, but otherwise it matches all characters
  1178. (including isolated CRs and LFs). When any Unicode line endings are being
  1179. recognized, dot does not match CR or LF or any of the other line ending
  1180. characters.
  1181. </P>
  1182. <P>
  1183. The behaviour of dot with regard to newlines can be changed. If the PCRE_DOTALL
  1184. option is set, a dot matches any one character, without exception. If the
  1185. two-character sequence CRLF is present in the subject string, it takes two dots
  1186. to match it.
  1187. </P>
  1188. <P>
  1189. The handling of dot is entirely independent of the handling of circumflex and
  1190. dollar, the only relationship being that they both involve newlines. Dot has no
  1191. special meaning in a character class.
  1192. </P>
  1193. <P>
  1194. The escape sequence \N behaves like a dot, except that it is not affected by
  1195. the PCRE_DOTALL option. In other words, it matches any character except one
  1196. that signifies the end of a line. Perl also uses \N to match characters by
  1197. name; PCRE does not support this.
  1198. </P>
  1199. <br><a name="SEC8" href="#TOC1">MATCHING A SINGLE DATA UNIT</a><br>
  1200. <P>
  1201. Outside a character class, the escape sequence \C matches any one data unit,
  1202. whether or not a UTF mode is set. In the 8-bit library, one data unit is one
  1203. byte; in the 16-bit library it is a 16-bit unit; in the 32-bit library it is
  1204. a 32-bit unit. Unlike a dot, \C always
  1205. matches line-ending characters. The feature is provided in Perl in order to
  1206. match individual bytes in UTF-8 mode, but it is unclear how it can usefully be
  1207. used. Because \C breaks up characters into individual data units, matching one
  1208. unit with \C in a UTF mode means that the rest of the string may start with a
  1209. malformed UTF character. This has undefined results, because PCRE assumes that
  1210. it is dealing with valid UTF strings (and by default it checks this at the
  1211. start of processing unless the PCRE_NO_UTF8_CHECK, PCRE_NO_UTF16_CHECK or
  1212. PCRE_NO_UTF32_CHECK option is used).
  1213. </P>
  1214. <P>
  1215. PCRE does not allow \C to appear in lookbehind assertions
  1216. <a href="#lookbehind">(described below)</a>
  1217. in a UTF mode, because this would make it impossible to calculate the length of
  1218. the lookbehind.
  1219. </P>
  1220. <P>
  1221. In general, the \C escape sequence is best avoided. However, one
  1222. way of using it that avoids the problem of malformed UTF characters is to use a
  1223. lookahead to check the length of the next character, as in this pattern, which
  1224. could be used with a UTF-8 string (ignore white space and line breaks):
  1225. <pre>
  1226. (?| (?=[\x00-\x7f])(\C) |
  1227. (?=[\x80-\x{7ff}])(\C)(\C) |
  1228. (?=[\x{800}-\x{ffff}])(\C)(\C)(\C) |
  1229. (?=[\x{10000}-\x{1fffff}])(\C)(\C)(\C)(\C))
  1230. </pre>
  1231. A group that starts with (?| resets the capturing parentheses numbers in each
  1232. alternative (see
  1233. <a href="#dupsubpatternnumber">"Duplicate Subpattern Numbers"</a>
  1234. below). The assertions at the start of each branch check the next UTF-8
  1235. character for values whose encoding uses 1, 2, 3, or 4 bytes, respectively. The
  1236. character's individual bytes are then captured by the appropriate number of
  1237. groups.
  1238. <a name="characterclass"></a></P>
  1239. <br><a name="SEC9" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br>
  1240. <P>
  1241. An opening square bracket introduces a character class, terminated by a closing
  1242. square bracket. A closing square bracket on its own is not special by default.
  1243. However, if the PCRE_JAVASCRIPT_COMPAT option is set, a lone closing square
  1244. bracket causes a compile-time error. If a closing square bracket is required as
  1245. a member of the class, it should be the first data character in the class
  1246. (after an initial circumflex, if present) or escaped with a backslash.
  1247. </P>
  1248. <P>
  1249. A character class matches a single character in the subject. In a UTF mode, the
  1250. character may be more than one data unit long. A matched character must be in
  1251. the set of characters defined by the class, unless the first character in the
  1252. class definition is a circumflex, in which case the subject character must not
  1253. be in the set defined by the class. If a circumflex is actually required as a
  1254. member of the class, ensure it is not the first character, or escape it with a
  1255. backslash.
  1256. </P>
  1257. <P>
  1258. For example, the character class [aeiou] matches any lower case vowel, while
  1259. [^aeiou] matches any character that is not a lower case vowel. Note that a
  1260. circumflex is just a convenient notation for specifying the characters that
  1261. are in the class by enumerating those that are not. A class that starts with a
  1262. circumflex is not an assertion; it still consumes a character from the subject
  1263. string, and therefore it fails if the current pointer is at the end of the
  1264. string.
  1265. </P>
  1266. <P>
  1267. In UTF-8 (UTF-16, UTF-32) mode, characters with values greater than 255 (0xffff)
  1268. can be included in a class as a literal string of data units, or by using the
  1269. \x{ escaping mechanism.
  1270. </P>
  1271. <P>
  1272. When caseless matching is set, any letters in a class represent both their
  1273. upper case and lower case versions, so for example, a caseless [aeiou] matches
  1274. "A" as well as "a", and a caseless [^aeiou] does not match "A", whereas a
  1275. caseful version would. In a UTF mode, PCRE always understands the concept of
  1276. case for characters whose values are less than 128, so caseless matching is
  1277. always possible. For characters with higher values, the concept of case is
  1278. supported if PCRE is compiled with Unicode property support, but not otherwise.
  1279. If you want to use caseless matching in a UTF mode for characters 128 and
  1280. above, you must ensure that PCRE is compiled with Unicode property support as
  1281. well as with UTF support.
  1282. </P>
  1283. <P>
  1284. Characters that might indicate line breaks are never treated in any special way
  1285. when matching character classes, whatever line-ending sequence is in use, and
  1286. whatever setting of the PCRE_DOTALL and PCRE_MULTILINE options is used. A class
  1287. such as [^a] always matches one of these characters.
  1288. </P>
  1289. <P>
  1290. The minus (hyphen) character can be used to specify a range of characters in a
  1291. character class. For example, [d-m] matches any letter between d and m,
  1292. inclusive. If a minus character is required in a class, it must be escaped with
  1293. a backslash or appear in a position where it cannot be interpreted as
  1294. indicating a range, typically as the first or last character in the class, or
  1295. immediately after a range. For example, [b-d-z] matches letters in the range b
  1296. to d, a hyphen character, or z.
  1297. </P>
  1298. <P>
  1299. It is not possible to have the literal character "]" as the end character of a
  1300. range. A pattern such as [W-]46] is interpreted as a class of two characters
  1301. ("W" and "-") followed by a literal string "46]", so it would match "W46]" or
  1302. "-46]". However, if the "]" is escaped with a backslash it is interpreted as
  1303. the end of range, so [W-\]46] is interpreted as a class containing a range
  1304. followed by two other characters. The octal or hexadecimal representation of
  1305. "]" can also be used to end a range.
  1306. </P>
  1307. <P>
  1308. An error is generated if a POSIX character class (see below) or an escape
  1309. sequence other than one that defines a single character appears at a point
  1310. where a range ending character is expected. For example, [z-\xff] is valid,
  1311. but [A-\d] and [A-[:digit:]] are not.
  1312. </P>
  1313. <P>
  1314. Ranges operate in the collating sequence of character values. They can also be
  1315. used for characters specified numerically, for example [\000-\037]. Ranges
  1316. can include any characters that are valid for the current mode.
  1317. </P>
  1318. <P>
  1319. If a range that includes letters is used when caseless matching is set, it
  1320. matches the letters in either case. For example, [W-c] is equivalent to
  1321. [][\\^_`wxyzabc], matched caselessly, and in a non-UTF mode, if character
  1322. tables for a French locale are in use, [\xc8-\xcb] matches accented E
  1323. characters in both cases. In UTF modes, PCRE supports the concept of case for
  1324. characters with values greater than 128 only when it is compiled with Unicode
  1325. property support.
  1326. </P>
  1327. <P>
  1328. The character escape sequences \d, \D, \h, \H, \p, \P, \s, \S, \v,
  1329. \V, \w, and \W may appear in a character class, and add the characters that
  1330. they match to the class. For example, [\dABCDEF] matches any hexadecimal
  1331. digit. In UTF modes, the PCRE_UCP option affects the meanings of \d, \s, \w
  1332. and their upper case partners, just as it does when they appear outside a
  1333. character class, as described in the section entitled
  1334. <a href="#genericchartypes">"Generic character types"</a>
  1335. above. The escape sequence \b has a different meaning inside a character
  1336. class; it matches the backspace character. The sequences \B, \N, \R, and \X
  1337. are not special inside a character class. Like any other unrecognized escape
  1338. sequences, they are treated as the literal characters "B", "N", "R", and "X" by
  1339. default, but cause an error if the PCRE_EXTRA option is set.
  1340. </P>
  1341. <P>
  1342. A circumflex can conveniently be used with the upper case character types to
  1343. specify a more restricted set of characters than the matching lower case type.
  1344. For example, the class [^\W_] matches any letter or digit, but not underscore,
  1345. whereas [\w] includes underscore. A positive character class should be read as
  1346. "something OR something OR ..." and a negative class as "NOT something AND NOT
  1347. something AND NOT ...".
  1348. </P>
  1349. <P>
  1350. The only metacharacters that are recognized in character classes are backslash,
  1351. hyphen (only where it can be interpreted as specifying a range), circumflex
  1352. (only at the start), opening square bracket (only when it can be interpreted as
  1353. introducing a POSIX class name, or for a special compatibility feature - see
  1354. the next two sections), and the terminating closing square bracket. However,
  1355. escaping other non-alphanumeric characters does no harm.
  1356. </P>
  1357. <br><a name="SEC10" href="#TOC1">POSIX CHARACTER CLASSES</a><br>
  1358. <P>
  1359. Perl supports the POSIX notation for character classes. This uses names
  1360. enclosed by [: and :] within the enclosing square brackets. PCRE also supports
  1361. this notation. For example,
  1362. <pre>
  1363. [01[:alpha:]%]
  1364. </pre>
  1365. matches "0", "1", any alphabetic character, or "%". The supported class names
  1366. are:
  1367. <pre>
  1368. alnum letters and digits
  1369. alpha letters
  1370. ascii character codes 0 - 127
  1371. blank space or tab only
  1372. cntrl control characters
  1373. digit decimal digits (same as \d)
  1374. graph printing characters, excluding space
  1375. lower lower case letters
  1376. print printing characters, including space
  1377. punct printing characters, excluding letters and digits and space
  1378. space white space (the same as \s from PCRE 8.34)
  1379. upper upper case letters
  1380. word "word" characters (same as \w)
  1381. xdigit hexadecimal digits
  1382. </pre>
  1383. The default "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13),
  1384. and space (32). If locale-specific matching is taking place, the list of space
  1385. characters may be different; there may be fewer or more of them. "Space" used
  1386. to be different to \s, which did not include VT, for Perl compatibility.
  1387. However, Perl changed at release 5.18, and PCRE followed at release 8.34.
  1388. "Space" and \s now match the same set of characters.
  1389. </P>
  1390. <P>
  1391. The name "word" is a Perl extension, and "blank" is a GNU extension from Perl
  1392. 5.8. Another Perl extension is negation, which is indicated by a ^ character
  1393. after the colon. For example,
  1394. <pre>
  1395. [12[:^digit:]]
  1396. </pre>
  1397. matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the POSIX
  1398. syntax [.ch.] and [=ch=] where "ch" is a "collating element", but these are not
  1399. supported, and an error is given if they are encountered.
  1400. </P>
  1401. <P>
  1402. By default, characters with values greater than 128 do not match any of the
  1403. POSIX character classes. However, if the PCRE_UCP option is passed to
  1404. <b>pcre_compile()</b>, some of the classes are changed so that Unicode character
  1405. properties are used. This is achieved by replacing certain POSIX classes by
  1406. other sequences, as follows:
  1407. <pre>
  1408. [:alnum:] becomes \p{Xan}
  1409. [:alpha:] becomes \p{L}
  1410. [:blank:] becomes \h
  1411. [:digit:] becomes \p{Nd}
  1412. [:lower:] becomes \p{Ll}
  1413. [:space:] becomes \p{Xps}
  1414. [:upper:] becomes \p{Lu}
  1415. [:word:] becomes \p{Xwd}
  1416. </pre>
  1417. Negated versions, such as [:^alpha:] use \P instead of \p. Three other POSIX
  1418. classes are handled specially in UCP mode:
  1419. </P>
  1420. <P>
  1421. [:graph:]
  1422. This matches characters that have glyphs that mark the page when printed. In
  1423. Unicode property terms, it matches all characters with the L, M, N, P, S, or Cf
  1424. properties, except for:
  1425. <pre>
  1426. U+061C Arabic Letter Mark
  1427. U+180E Mongolian Vowel Separator
  1428. U+2066 - U+2069 Various "isolate"s
  1429. </PRE>
  1430. </P>
  1431. <P>
  1432. [:print:]
  1433. This matches the same characters as [:graph:] plus space characters that are
  1434. not controls, that is, characters with the Zs property.
  1435. </P>
  1436. <P>
  1437. [:punct:]
  1438. This matches all characters that have the Unicode P (punctuation) property,
  1439. plus those characters whose code points are less than 128 that have the S
  1440. (Symbol) property.
  1441. </P>
  1442. <P>
  1443. The other POSIX classes are unchanged, and match only characters with code
  1444. points less than 128.
  1445. </P>
  1446. <br><a name="SEC11" href="#TOC1">COMPATIBILITY FEATURE FOR WORD BOUNDARIES</a><br>
  1447. <P>
  1448. In the POSIX.2 compliant library that was included in 4.4BSD Unix, the ugly
  1449. syntax [[:&#60;:]] and [[:&#62;:]] is used for matching "start of word" and "end of
  1450. word". PCRE treats these items as follows:
  1451. <pre>
  1452. [[:&#60;:]] is converted to \b(?=\w)
  1453. [[:&#62;:]] is converted to \b(?&#60;=\w)
  1454. </pre>
  1455. Only these exact character sequences are recognized. A sequence such as
  1456. [a[:&#60;:]b] provokes error for an unrecognized POSIX class name. This support is
  1457. not compatible with Perl. It is provided to help migrations from other
  1458. environments, and is best not used in any new patterns. Note that \b matches
  1459. at the start and the end of a word (see
  1460. <a href="#smallassertions">"Simple assertions"</a>
  1461. above), and in a Perl-style pattern the preceding or following character
  1462. normally shows which is wanted, without the need for the assertions that are
  1463. used above in order to give exactly the POSIX behaviour.
  1464. </P>
  1465. <br><a name="SEC12" href="#TOC1">VERTICAL BAR</a><br>
  1466. <P>
  1467. Vertical bar characters are used to separate alternative patterns. For example,
  1468. the pattern
  1469. <pre>
  1470. gilbert|sullivan
  1471. </pre>
  1472. matches either "gilbert" or "sullivan". Any number of alternatives may appear,
  1473. and an empty alternative is permitted (matching the empty string). The matching
  1474. process tries each alternative in turn, from left to right, and the first one
  1475. that succeeds is used. If the alternatives are within a subpattern
  1476. <a href="#subpattern">(defined below),</a>
  1477. "succeeds" means matching the rest of the main pattern as well as the
  1478. alternative in the subpattern.
  1479. </P>
  1480. <br><a name="SEC13" href="#TOC1">INTERNAL OPTION SETTING</a><br>
  1481. <P>
  1482. The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
  1483. PCRE_EXTENDED options (which are Perl-compatible) can be changed from within
  1484. the pattern by a sequence of Perl option letters enclosed between "(?" and ")".
  1485. The option letters are
  1486. <pre>
  1487. i for PCRE_CASELESS
  1488. m for PCRE_MULTILINE
  1489. s for PCRE_DOTALL
  1490. x for PCRE_EXTENDED
  1491. </pre>
  1492. For example, (?im) sets caseless, multiline matching. It is also possible to
  1493. unset these options by preceding the letter with a hyphen, and a combined
  1494. setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and
  1495. PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, is also
  1496. permitted. If a letter appears both before and after the hyphen, the option is
  1497. unset.
  1498. </P>
  1499. <P>
  1500. The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be
  1501. changed in the same way as the Perl-compatible options by using the characters
  1502. J, U and X respectively.
  1503. </P>
  1504. <P>
  1505. When one of these option changes occurs at top level (that is, not inside
  1506. subpattern parentheses), the change applies to the remainder of the pattern
  1507. that follows. An option change within a subpattern (see below for a description
  1508. of subpatterns) affects only that part of the subpattern that follows it, so
  1509. <pre>
  1510. (a(?i)b)c
  1511. </pre>
  1512. matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used).
  1513. By this means, options can be made to have different settings in different
  1514. parts of the pattern. Any changes made in one alternative do carry on
  1515. into subsequent branches within the same subpattern. For example,
  1516. <pre>
  1517. (a(?i)b|c)
  1518. </pre>
  1519. matches "ab", "aB", "c", and "C", even though when matching "C" the first
  1520. branch is abandoned before the option setting. This is because the effects of
  1521. option settings happen at compile time. There would be some very weird
  1522. behaviour otherwise.
  1523. </P>
  1524. <P>
  1525. <b>Note:</b> There are other PCRE-specific options that can be set by the
  1526. application when the compiling or matching functions are called. In some cases
  1527. the pattern can contain special leading sequences such as (*CRLF) to override
  1528. what the application has set or what has been defaulted. Details are given in
  1529. the section entitled
  1530. <a href="#newlineseq">"Newline sequences"</a>
  1531. above. There are also the (*UTF8), (*UTF16),(*UTF32), and (*UCP) leading
  1532. sequences that can be used to set UTF and Unicode property modes; they are
  1533. equivalent to setting the PCRE_UTF8, PCRE_UTF16, PCRE_UTF32 and the PCRE_UCP
  1534. options, respectively. The (*UTF) sequence is a generic version that can be
  1535. used with any of the libraries. However, the application can set the
  1536. PCRE_NEVER_UTF option, which locks out the use of the (*UTF) sequences.
  1537. <a name="subpattern"></a></P>
  1538. <br><a name="SEC14" href="#TOC1">SUBPATTERNS</a><br>
  1539. <P>
  1540. Subpatterns are delimited by parentheses (round brackets), which can be nested.
  1541. Turning part of a pattern into a subpattern does two things:
  1542. <br>
  1543. <br>
  1544. 1. It localizes a set of alternatives. For example, the pattern
  1545. <pre>
  1546. cat(aract|erpillar|)
  1547. </pre>
  1548. matches "cataract", "caterpillar", or "cat". Without the parentheses, it would
  1549. match "cataract", "erpillar" or an empty string.
  1550. <br>
  1551. <br>
  1552. 2. It sets up the subpattern as a capturing subpattern. This means that, when
  1553. the whole pattern matches, that portion of the subject string that matched the
  1554. subpattern is passed back to the caller via the <i>ovector</i> argument of the
  1555. matching function. (This applies only to the traditional matching functions;
  1556. the DFA matching functions do not support capturing.)
  1557. </P>
  1558. <P>
  1559. Opening parentheses are counted from left to right (starting from 1) to obtain
  1560. numbers for the capturing subpatterns. For example, if the string "the red
  1561. king" is matched against the pattern
  1562. <pre>
  1563. the ((red|white) (king|queen))
  1564. </pre>
  1565. the captured substrings are "red king", "red", and "king", and are numbered 1,
  1566. 2, and 3, respectively.
  1567. </P>
  1568. <P>
  1569. The fact that plain parentheses fulfil two functions is not always helpful.
  1570. There are often times when a grouping subpattern is required without a
  1571. capturing requirement. If an opening parenthesis is followed by a question mark
  1572. and a colon, the subpattern does not do any capturing, and is not counted when
  1573. computing the number of any subsequent capturing subpatterns. For example, if
  1574. the string "the white queen" is matched against the pattern
  1575. <pre>
  1576. the ((?:red|white) (king|queen))
  1577. </pre>
  1578. the captured substrings are "white queen" and "queen", and are numbered 1 and
  1579. 2. The maximum number of capturing subpatterns is 65535.
  1580. </P>
  1581. <P>
  1582. As a convenient shorthand, if any option settings are required at the start of
  1583. a non-capturing subpattern, the option letters may appear between the "?" and
  1584. the ":". Thus the two patterns
  1585. <pre>
  1586. (?i:saturday|sunday)
  1587. (?:(?i)saturday|sunday)
  1588. </pre>
  1589. match exactly the same set of strings. Because alternative branches are tried
  1590. from left to right, and options are not reset until the end of the subpattern
  1591. is reached, an option setting in one branch does affect subsequent branches, so
  1592. the above patterns match "SUNDAY" as well as "Saturday".
  1593. <a name="dupsubpatternnumber"></a></P>
  1594. <br><a name="SEC15" href="#TOC1">DUPLICATE SUBPATTERN NUMBERS</a><br>
  1595. <P>
  1596. Perl 5.10 introduced a feature whereby each alternative in a subpattern uses
  1597. the same numbers for its capturing parentheses. Such a subpattern starts with
  1598. (?| and is itself a non-capturing subpattern. For example, consider this
  1599. pattern:
  1600. <pre>
  1601. (?|(Sat)ur|(Sun))day
  1602. </pre>
  1603. Because the two alternatives are inside a (?| group, both sets of capturing
  1604. parentheses are numbered one. Thus, when the pattern matches, you can look
  1605. at captured substring number one, whichever alternative matched. This construct
  1606. is useful when you want to capture part, but not all, of one of a number of
  1607. alternatives. Inside a (?| group, parentheses are numbered as usual, but the
  1608. number is reset at the start of each branch. The numbers of any capturing
  1609. parentheses that follow the subpattern start after the highest number used in
  1610. any branch. The following example is taken from the Perl documentation. The
  1611. numbers underneath show in which buffer the captured content will be stored.
  1612. <pre>
  1613. # before ---------------branch-reset----------- after
  1614. / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
  1615. # 1 2 2 3 2 3 4
  1616. </pre>
  1617. A back reference to a numbered subpattern uses the most recent value that is
  1618. set for that number by any subpattern. The following pattern matches "abcabc"
  1619. or "defdef":
  1620. <pre>
  1621. /(?|(abc)|(def))\1/
  1622. </pre>
  1623. In contrast, a subroutine call to a numbered subpattern always refers to the
  1624. first one in the pattern with the given number. The following pattern matches
  1625. "abcabc" or "defabc":
  1626. <pre>
  1627. /(?|(abc)|(def))(?1)/
  1628. </pre>
  1629. If a
  1630. <a href="#conditions">condition test</a>
  1631. for a subpattern's having matched refers to a non-unique number, the test is
  1632. true if any of the subpatterns of that number have matched.
  1633. </P>
  1634. <P>
  1635. An alternative approach to using this "branch reset" feature is to use
  1636. duplicate named subpatterns, as described in the next section.
  1637. </P>
  1638. <br><a name="SEC16" href="#TOC1">NAMED SUBPATTERNS</a><br>
  1639. <P>
  1640. Identifying capturing parentheses by number is simple, but it can be very hard
  1641. to keep track of the numbers in complicated regular expressions. Furthermore,
  1642. if an expression is modified, the numbers may change. To help with this
  1643. difficulty, PCRE supports the naming of subpatterns. This feature was not
  1644. added to Perl until release 5.10. Python had the feature earlier, and PCRE
  1645. introduced it at release 4.0, using the Python syntax. PCRE now supports both
  1646. the Perl and the Python syntax. Perl allows identically numbered subpatterns to
  1647. have different names, but PCRE does not.
  1648. </P>
  1649. <P>
  1650. In PCRE, a subpattern can be named in one of three ways: (?&#60;name&#62;...) or
  1651. (?'name'...) as in Perl, or (?P&#60;name&#62;...) as in Python. References to capturing
  1652. parentheses from other parts of the pattern, such as
  1653. <a href="#backreferences">back references,</a>
  1654. <a href="#recursion">recursion,</a>
  1655. and
  1656. <a href="#conditions">conditions,</a>
  1657. can be made by name as well as by number.
  1658. </P>
  1659. <P>
  1660. Names consist of up to 32 alphanumeric characters and underscores, but must
  1661. start with a non-digit. Named capturing parentheses are still allocated numbers
  1662. as well as names, exactly as if the names were not present. The PCRE API
  1663. provides function calls for extracting the name-to-number translation table
  1664. from a compiled pattern. There is also a convenience function for extracting a
  1665. captured substring by name.
  1666. </P>
  1667. <P>
  1668. By default, a name must be unique within a pattern, but it is possible to relax
  1669. this constraint by setting the PCRE_DUPNAMES option at compile time. (Duplicate
  1670. names are also always permitted for subpatterns with the same number, set up as
  1671. described in the previous section.) Duplicate names can be useful for patterns
  1672. where only one instance of the named parentheses can match. Suppose you want to
  1673. match the name of a weekday, either as a 3-letter abbreviation or as the full
  1674. name, and in both cases you want to extract the abbreviation. This pattern
  1675. (ignoring the line breaks) does the job:
  1676. <pre>
  1677. (?&#60;DN&#62;Mon|Fri|Sun)(?:day)?|
  1678. (?&#60;DN&#62;Tue)(?:sday)?|
  1679. (?&#60;DN&#62;Wed)(?:nesday)?|
  1680. (?&#60;DN&#62;Thu)(?:rsday)?|
  1681. (?&#60;DN&#62;Sat)(?:urday)?
  1682. </pre>
  1683. There are five capturing substrings, but only one is ever set after a match.
  1684. (An alternative way of solving this problem is to use a "branch reset"
  1685. subpattern, as described in the previous section.)
  1686. </P>
  1687. <P>
  1688. The convenience function for extracting the data by name returns the substring
  1689. for the first (and in this example, the only) subpattern of that name that
  1690. matched. This saves searching to find which numbered subpattern it was.
  1691. </P>
  1692. <P>
  1693. If you make a back reference to a non-unique named subpattern from elsewhere in
  1694. the pattern, the subpatterns to which the name refers are checked in the order
  1695. in which they appear in the overall pattern. The first one that is set is used
  1696. for the reference. For example, this pattern matches both "foofoo" and
  1697. "barbar" but not "foobar" or "barfoo":
  1698. <pre>
  1699. (?:(?&#60;n&#62;foo)|(?&#60;n&#62;bar))\k&#60;n&#62;
  1700. </PRE>
  1701. </P>
  1702. <P>
  1703. If you make a subroutine call to a non-unique named subpattern, the one that
  1704. corresponds to the first occurrence of the name is used. In the absence of
  1705. duplicate numbers (see the previous section) this is the one with the lowest
  1706. number.
  1707. </P>
  1708. <P>
  1709. If you use a named reference in a condition
  1710. test (see the
  1711. <a href="#conditions">section about conditions</a>
  1712. below), either to check whether a subpattern has matched, or to check for
  1713. recursion, all subpatterns with the same name are tested. If the condition is
  1714. true for any one of them, the overall condition is true. This is the same
  1715. behaviour as testing by number. For further details of the interfaces for
  1716. handling named subpatterns, see the
  1717. <a href="pcreapi.html"><b>pcreapi</b></a>
  1718. documentation.
  1719. </P>
  1720. <P>
  1721. <b>Warning:</b> You cannot use different names to distinguish between two
  1722. subpatterns with the same number because PCRE uses only the numbers when
  1723. matching. For this reason, an error is given at compile time if different names
  1724. are given to subpatterns with the same number. However, you can always give the
  1725. same name to subpatterns with the same number, even when PCRE_DUPNAMES is not
  1726. set.
  1727. </P>
  1728. <br><a name="SEC17" href="#TOC1">REPETITION</a><br>
  1729. <P>
  1730. Repetition is specified by quantifiers, which can follow any of the following
  1731. items:
  1732. <pre>
  1733. a literal data character
  1734. the dot metacharacter
  1735. the \C escape sequence
  1736. the \X escape sequence
  1737. the \R escape sequence
  1738. an escape such as \d or \pL that matches a single character
  1739. a character class
  1740. a back reference (see next section)
  1741. a parenthesized subpattern (including assertions)
  1742. a subroutine call to a subpattern (recursive or otherwise)
  1743. </pre>
  1744. The general repetition quantifier specifies a minimum and maximum number of
  1745. permitted matches, by giving the two numbers in curly brackets (braces),
  1746. separated by a comma. The numbers must be less than 65536, and the first must
  1747. be less than or equal to the second. For example:
  1748. <pre>
  1749. z{2,4}
  1750. </pre>
  1751. matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special
  1752. character. If the second number is omitted, but the comma is present, there is
  1753. no upper limit; if the second number and the comma are both omitted, the
  1754. quantifier specifies an exact number of required matches. Thus
  1755. <pre>
  1756. [aeiou]{3,}
  1757. </pre>
  1758. matches at least 3 successive vowels, but may match many more, while
  1759. <pre>
  1760. \d{8}
  1761. </pre>
  1762. matches exactly 8 digits. An opening curly bracket that appears in a position
  1763. where a quantifier is not allowed, or one that does not match the syntax of a
  1764. quantifier, is taken as a literal character. For example, {,6} is not a
  1765. quantifier, but a literal string of four characters.
  1766. </P>
  1767. <P>
  1768. In UTF modes, quantifiers apply to characters rather than to individual data
  1769. units. Thus, for example, \x{100}{2} matches two characters, each of
  1770. which is represented by a two-byte sequence in a UTF-8 string. Similarly,
  1771. \X{3} matches three Unicode extended grapheme clusters, each of which may be
  1772. several data units long (and they may be of different lengths).
  1773. </P>
  1774. <P>
  1775. The quantifier {0} is permitted, causing the expression to behave as if the
  1776. previous item and the quantifier were not present. This may be useful for
  1777. subpatterns that are referenced as
  1778. <a href="#subpatternsassubroutines">subroutines</a>
  1779. from elsewhere in the pattern (but see also the section entitled
  1780. <a href="#subdefine">"Defining subpatterns for use by reference only"</a>
  1781. below). Items other than subpatterns that have a {0} quantifier are omitted
  1782. from the compiled pattern.
  1783. </P>
  1784. <P>
  1785. For convenience, the three most common quantifiers have single-character
  1786. abbreviations:
  1787. <pre>
  1788. * is equivalent to {0,}
  1789. + is equivalent to {1,}
  1790. ? is equivalent to {0,1}
  1791. </pre>
  1792. It is possible to construct infinite loops by following a subpattern that can
  1793. match no characters with a quantifier that has no upper limit, for example:
  1794. <pre>
  1795. (a?)*
  1796. </pre>
  1797. Earlier versions of Perl and PCRE used to give an error at compile time for
  1798. such patterns. However, because there are cases where this can be useful, such
  1799. patterns are now accepted, but if any repetition of the subpattern does in fact
  1800. match no characters, the loop is forcibly broken.
  1801. </P>
  1802. <P>
  1803. By default, the quantifiers are "greedy", that is, they match as much as
  1804. possible (up to the maximum number of permitted times), without causing the
  1805. rest of the pattern to fail. The classic example of where this gives problems
  1806. is in trying to match comments in C programs. These appear between /* and */
  1807. and within the comment, individual * and / characters may appear. An attempt to
  1808. match C comments by applying the pattern
  1809. <pre>
  1810. /\*.*\*/
  1811. </pre>
  1812. to the string
  1813. <pre>
  1814. /* first comment */ not comment /* second comment */
  1815. </pre>
  1816. fails, because it matches the entire string owing to the greediness of the .*
  1817. item.
  1818. </P>
  1819. <P>
  1820. However, if a quantifier is followed by a question mark, it ceases to be
  1821. greedy, and instead matches the minimum number of times possible, so the
  1822. pattern
  1823. <pre>
  1824. /\*.*?\*/
  1825. </pre>
  1826. does the right thing with the C comments. The meaning of the various
  1827. quantifiers is not otherwise changed, just the preferred number of matches.
  1828. Do not confuse this use of question mark with its use as a quantifier in its
  1829. own right. Because it has two uses, it can sometimes appear doubled, as in
  1830. <pre>
  1831. \d??\d
  1832. </pre>
  1833. which matches one digit by preference, but can match two if that is the only
  1834. way the rest of the pattern matches.
  1835. </P>
  1836. <P>
  1837. If the PCRE_UNGREEDY option is set (an option that is not available in Perl),
  1838. the quantifiers are not greedy by default, but individual ones can be made
  1839. greedy by following them with a question mark. In other words, it inverts the
  1840. default behaviour.
  1841. </P>
  1842. <P>
  1843. When a parenthesized subpattern is quantified with a minimum repeat count that
  1844. is greater than 1 or with a limited maximum, more memory is required for the
  1845. compiled pattern, in proportion to the size of the minimum or maximum.
  1846. </P>
  1847. <P>
  1848. If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equivalent
  1849. to Perl's /s) is set, thus allowing the dot to match newlines, the pattern is
  1850. implicitly anchored, because whatever follows will be tried against every
  1851. character position in the subject string, so there is no point in retrying the
  1852. overall match at any position after the first. PCRE normally treats such a
  1853. pattern as though it were preceded by \A.
  1854. </P>
  1855. <P>
  1856. In cases where it is known that the subject string contains no newlines, it is
  1857. worth setting PCRE_DOTALL in order to obtain this optimization, or
  1858. alternatively using ^ to indicate anchoring explicitly.
  1859. </P>
  1860. <P>
  1861. However, there are some cases where the optimization cannot be used. When .*
  1862. is inside capturing parentheses that are the subject of a back reference
  1863. elsewhere in the pattern, a match at the start may fail where a later one
  1864. succeeds. Consider, for example:
  1865. <pre>
  1866. (.*)abc\1
  1867. </pre>
  1868. If the subject is "xyz123abc123" the match point is the fourth character. For
  1869. this reason, such a pattern is not implicitly anchored.
  1870. </P>
  1871. <P>
  1872. Another case where implicit anchoring is not applied is when the leading .* is
  1873. inside an atomic group. Once again, a match at the start may fail where a later
  1874. one succeeds. Consider this pattern:
  1875. <pre>
  1876. (?&#62;.*?a)b
  1877. </pre>
  1878. It matches "ab" in the subject "aab". The use of the backtracking control verbs
  1879. (*PRUNE) and (*SKIP) also disable this optimization.
  1880. </P>
  1881. <P>
  1882. When a capturing subpattern is repeated, the value captured is the substring
  1883. that matched the final iteration. For example, after
  1884. <pre>
  1885. (tweedle[dume]{3}\s*)+
  1886. </pre>
  1887. has matched "tweedledum tweedledee" the value of the captured substring is
  1888. "tweedledee". However, if there are nested capturing subpatterns, the
  1889. corresponding captured values may have been set in previous iterations. For
  1890. example, after
  1891. <pre>
  1892. /(a|(b))+/
  1893. </pre>
  1894. matches "aba" the value of the second captured substring is "b".
  1895. <a name="atomicgroup"></a></P>
  1896. <br><a name="SEC18" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br>
  1897. <P>
  1898. With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
  1899. repetition, failure of what follows normally causes the repeated item to be
  1900. re-evaluated to see if a different number of repeats allows the rest of the
  1901. pattern to match. Sometimes it is useful to prevent this, either to change the
  1902. nature of the match, or to cause it fail earlier than it otherwise might, when
  1903. the author of the pattern knows there is no point in carrying on.
  1904. </P>
  1905. <P>
  1906. Consider, for example, the pattern \d+foo when applied to the subject line
  1907. <pre>
  1908. 123456bar
  1909. </pre>
  1910. After matching all 6 digits and then failing to match "foo", the normal
  1911. action of the matcher is to try again with only 5 digits matching the \d+
  1912. item, and then with 4, and so on, before ultimately failing. "Atomic grouping"
  1913. (a term taken from Jeffrey Friedl's book) provides the means for specifying
  1914. that once a subpattern has matched, it is not to be re-evaluated in this way.
  1915. </P>
  1916. <P>
  1917. If we use atomic grouping for the previous example, the matcher gives up
  1918. immediately on failing to match "foo" the first time. The notation is a kind of
  1919. special parenthesis, starting with (?&#62; as in this example:
  1920. <pre>
  1921. (?&#62;\d+)foo
  1922. </pre>
  1923. This kind of parenthesis "locks up" the part of the pattern it contains once
  1924. it has matched, and a failure further into the pattern is prevented from
  1925. backtracking into it. Backtracking past it to previous items, however, works as
  1926. normal.
  1927. </P>
  1928. <P>
  1929. An alternative description is that a subpattern of this type matches the string
  1930. of characters that an identical standalone pattern would match, if anchored at
  1931. the current point in the subject string.
  1932. </P>
  1933. <P>
  1934. Atomic grouping subpatterns are not capturing subpatterns. Simple cases such as
  1935. the above example can be thought of as a maximizing repeat that must swallow
  1936. everything it can. So, while both \d+ and \d+? are prepared to adjust the
  1937. number of digits they match in order to make the rest of the pattern match,
  1938. (?&#62;\d+) can only match an entire sequence of digits.
  1939. </P>
  1940. <P>
  1941. Atomic groups in general can of course contain arbitrarily complicated
  1942. subpatterns, and can be nested. However, when the subpattern for an atomic
  1943. group is just a single repeated item, as in the example above, a simpler
  1944. notation, called a "possessive quantifier" can be used. This consists of an
  1945. additional + character following a quantifier. Using this notation, the
  1946. previous example can be rewritten as
  1947. <pre>
  1948. \d++foo
  1949. </pre>
  1950. Note that a possessive quantifier can be used with an entire group, for
  1951. example:
  1952. <pre>
  1953. (abc|xyz){2,3}+
  1954. </pre>
  1955. Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY
  1956. option is ignored. They are a convenient notation for the simpler forms of
  1957. atomic group. However, there is no difference in the meaning of a possessive
  1958. quantifier and the equivalent atomic group, though there may be a performance
  1959. difference; possessive quantifiers should be slightly faster.
  1960. </P>
  1961. <P>
  1962. The possessive quantifier syntax is an extension to the Perl 5.8 syntax.
  1963. Jeffrey Friedl originated the idea (and the name) in the first edition of his
  1964. book. Mike McCloskey liked it, so implemented it when he built Sun's Java
  1965. package, and PCRE copied it from there. It ultimately found its way into Perl
  1966. at release 5.10.
  1967. </P>
  1968. <P>
  1969. PCRE has an optimization that automatically "possessifies" certain simple
  1970. pattern constructs. For example, the sequence A+B is treated as A++B because
  1971. there is no point in backtracking into a sequence of A's when B must follow.
  1972. </P>
  1973. <P>
  1974. When a pattern contains an unlimited repeat inside a subpattern that can itself
  1975. be repeated an unlimited number of times, the use of an atomic group is the
  1976. only way to avoid some failing matches taking a very long time indeed. The
  1977. pattern
  1978. <pre>
  1979. (\D+|&#60;\d+&#62;)*[!?]
  1980. </pre>
  1981. matches an unlimited number of substrings that either consist of non-digits, or
  1982. digits enclosed in &#60;&#62;, followed by either ! or ?. When it matches, it runs
  1983. quickly. However, if it is applied to
  1984. <pre>
  1985. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  1986. </pre>
  1987. it takes a long time before reporting failure. This is because the string can
  1988. be divided between the internal \D+ repeat and the external * repeat in a
  1989. large number of ways, and all have to be tried. (The example uses [!?] rather
  1990. than a single character at the end, because both PCRE and Perl have an
  1991. optimization that allows for fast failure when a single character is used. They
  1992. remember the last single character that is required for a match, and fail early
  1993. if it is not present in the string.) If the pattern is changed so that it uses
  1994. an atomic group, like this:
  1995. <pre>
  1996. ((?&#62;\D+)|&#60;\d+&#62;)*[!?]
  1997. </pre>
  1998. sequences of non-digits cannot be broken, and failure happens quickly.
  1999. <a name="backreferences"></a></P>
  2000. <br><a name="SEC19" href="#TOC1">BACK REFERENCES</a><br>
  2001. <P>
  2002. Outside a character class, a backslash followed by a digit greater than 0 (and
  2003. possibly further digits) is a back reference to a capturing subpattern earlier
  2004. (that is, to its left) in the pattern, provided there have been that many
  2005. previous capturing left parentheses.
  2006. </P>
  2007. <P>
  2008. However, if the decimal number following the backslash is less than 10, it is
  2009. always taken as a back reference, and causes an error only if there are not
  2010. that many capturing left parentheses in the entire pattern. In other words, the
  2011. parentheses that are referenced need not be to the left of the reference for
  2012. numbers less than 10. A "forward back reference" of this type can make sense
  2013. when a repetition is involved and the subpattern to the right has participated
  2014. in an earlier iteration.
  2015. </P>
  2016. <P>
  2017. It is not possible to have a numerical "forward back reference" to a subpattern
  2018. whose number is 10 or more using this syntax because a sequence such as \50 is
  2019. interpreted as a character defined in octal. See the subsection entitled
  2020. "Non-printing characters"
  2021. <a href="#digitsafterbackslash">above</a>
  2022. for further details of the handling of digits following a backslash. There is
  2023. no such problem when named parentheses are used. A back reference to any
  2024. subpattern is possible using named parentheses (see below).
  2025. </P>
  2026. <P>
  2027. Another way of avoiding the ambiguity inherent in the use of digits following a
  2028. backslash is to use the \g escape sequence. This escape must be followed by an
  2029. unsigned number or a negative number, optionally enclosed in braces. These
  2030. examples are all identical:
  2031. <pre>
  2032. (ring), \1
  2033. (ring), \g1
  2034. (ring), \g{1}
  2035. </pre>
  2036. An unsigned number specifies an absolute reference without the ambiguity that
  2037. is present in the older syntax. It is also useful when literal digits follow
  2038. the reference. A negative number is a relative reference. Consider this
  2039. example:
  2040. <pre>
  2041. (abc(def)ghi)\g{-1}
  2042. </pre>
  2043. The sequence \g{-1} is a reference to the most recently started capturing
  2044. subpattern before \g, that is, is it equivalent to \2 in this example.
  2045. Similarly, \g{-2} would be equivalent to \1. The use of relative references
  2046. can be helpful in long patterns, and also in patterns that are created by
  2047. joining together fragments that contain references within themselves.
  2048. </P>
  2049. <P>
  2050. A back reference matches whatever actually matched the capturing subpattern in
  2051. the current subject string, rather than anything matching the subpattern
  2052. itself (see
  2053. <a href="#subpatternsassubroutines">"Subpatterns as subroutines"</a>
  2054. below for a way of doing that). So the pattern
  2055. <pre>
  2056. (sens|respons)e and \1ibility
  2057. </pre>
  2058. matches "sense and sensibility" and "response and responsibility", but not
  2059. "sense and responsibility". If caseful matching is in force at the time of the
  2060. back reference, the case of letters is relevant. For example,
  2061. <pre>
  2062. ((?i)rah)\s+\1
  2063. </pre>
  2064. matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original
  2065. capturing subpattern is matched caselessly.
  2066. </P>
  2067. <P>
  2068. There are several different ways of writing back references to named
  2069. subpatterns. The .NET syntax \k{name} and the Perl syntax \k&#60;name&#62; or
  2070. \k'name' are supported, as is the Python syntax (?P=name). Perl 5.10's unified
  2071. back reference syntax, in which \g can be used for both numeric and named
  2072. references, is also supported. We could rewrite the above example in any of
  2073. the following ways:
  2074. <pre>
  2075. (?&#60;p1&#62;(?i)rah)\s+\k&#60;p1&#62;
  2076. (?'p1'(?i)rah)\s+\k{p1}
  2077. (?P&#60;p1&#62;(?i)rah)\s+(?P=p1)
  2078. (?&#60;p1&#62;(?i)rah)\s+\g{p1}
  2079. </pre>
  2080. A subpattern that is referenced by name may appear in the pattern before or
  2081. after the reference.
  2082. </P>
  2083. <P>
  2084. There may be more than one back reference to the same subpattern. If a
  2085. subpattern has not actually been used in a particular match, any back
  2086. references to it always fail by default. For example, the pattern
  2087. <pre>
  2088. (a|(bc))\2
  2089. </pre>
  2090. always fails if it starts to match "a" rather than "bc". However, if the
  2091. PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back reference to an
  2092. unset value matches an empty string.
  2093. </P>
  2094. <P>
  2095. Because there may be many capturing parentheses in a pattern, all digits
  2096. following a backslash are taken as part of a potential back reference number.
  2097. If the pattern continues with a digit character, some delimiter must be used to
  2098. terminate the back reference. If the PCRE_EXTENDED option is set, this can be
  2099. white space. Otherwise, the \g{ syntax or an empty comment (see
  2100. <a href="#comments">"Comments"</a>
  2101. below) can be used.
  2102. </P>
  2103. <br><b>
  2104. Recursive back references
  2105. </b><br>
  2106. <P>
  2107. A back reference that occurs inside the parentheses to which it refers fails
  2108. when the subpattern is first used, so, for example, (a\1) never matches.
  2109. However, such references can be useful inside repeated subpatterns. For
  2110. example, the pattern
  2111. <pre>
  2112. (a|b\1)+
  2113. </pre>
  2114. matches any number of "a"s and also "aba", "ababbaa" etc. At each iteration of
  2115. the subpattern, the back reference matches the character string corresponding
  2116. to the previous iteration. In order for this to work, the pattern must be such
  2117. that the first iteration does not need to match the back reference. This can be
  2118. done using alternation, as in the example above, or by a quantifier with a
  2119. minimum of zero.
  2120. </P>
  2121. <P>
  2122. Back references of this type cause the group that they reference to be treated
  2123. as an
  2124. <a href="#atomicgroup">atomic group.</a>
  2125. Once the whole group has been matched, a subsequent matching failure cannot
  2126. cause backtracking into the middle of the group.
  2127. <a name="bigassertions"></a></P>
  2128. <br><a name="SEC20" href="#TOC1">ASSERTIONS</a><br>
  2129. <P>
  2130. An assertion is a test on the characters following or preceding the current
  2131. matching point that does not actually consume any characters. The simple
  2132. assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are described
  2133. <a href="#smallassertions">above.</a>
  2134. </P>
  2135. <P>
  2136. More complicated assertions are coded as subpatterns. There are two kinds:
  2137. those that look ahead of the current position in the subject string, and those
  2138. that look behind it. An assertion subpattern is matched in the normal way,
  2139. except that it does not cause the current matching position to be changed.
  2140. </P>
  2141. <P>
  2142. Assertion subpatterns are not capturing subpatterns. If such an assertion
  2143. contains capturing subpatterns within it, these are counted for the purposes of
  2144. numbering the capturing subpatterns in the whole pattern. However, substring
  2145. capturing is carried out only for positive assertions. (Perl sometimes, but not
  2146. always, does do capturing in negative assertions.)
  2147. </P>
  2148. <P>
  2149. WARNING: If a positive assertion containing one or more capturing subpatterns
  2150. succeeds, but failure to match later in the pattern causes backtracking over
  2151. this assertion, the captures within the assertion are reset only if no higher
  2152. numbered captures are already set. This is, unfortunately, a fundamental
  2153. limitation of the current implementation, and as PCRE1 is now in
  2154. maintenance-only status, it is unlikely ever to change.
  2155. </P>
  2156. <P>
  2157. For compatibility with Perl, assertion subpatterns may be repeated; though
  2158. it makes no sense to assert the same thing several times, the side effect of
  2159. capturing parentheses may occasionally be useful. In practice, there only three
  2160. cases:
  2161. <br>
  2162. <br>
  2163. (1) If the quantifier is {0}, the assertion is never obeyed during matching.
  2164. However, it may contain internal capturing parenthesized groups that are called
  2165. from elsewhere via the
  2166. <a href="#subpatternsassubroutines">subroutine mechanism.</a>
  2167. <br>
  2168. <br>
  2169. (2) If quantifier is {0,n} where n is greater than zero, it is treated as if it
  2170. were {0,1}. At run time, the rest of the pattern match is tried with and
  2171. without the assertion, the order depending on the greediness of the quantifier.
  2172. <br>
  2173. <br>
  2174. (3) If the minimum repetition is greater than zero, the quantifier is ignored.
  2175. The assertion is obeyed just once when encountered during matching.
  2176. </P>
  2177. <br><b>
  2178. Lookahead assertions
  2179. </b><br>
  2180. <P>
  2181. Lookahead assertions start with (?= for positive assertions and (?! for
  2182. negative assertions. For example,
  2183. <pre>
  2184. \w+(?=;)
  2185. </pre>
  2186. matches a word followed by a semicolon, but does not include the semicolon in
  2187. the match, and
  2188. <pre>
  2189. foo(?!bar)
  2190. </pre>
  2191. matches any occurrence of "foo" that is not followed by "bar". Note that the
  2192. apparently similar pattern
  2193. <pre>
  2194. (?!foo)bar
  2195. </pre>
  2196. does not find an occurrence of "bar" that is preceded by something other than
  2197. "foo"; it finds any occurrence of "bar" whatsoever, because the assertion
  2198. (?!foo) is always true when the next three characters are "bar". A
  2199. lookbehind assertion is needed to achieve the other effect.
  2200. </P>
  2201. <P>
  2202. If you want to force a matching failure at some point in a pattern, the most
  2203. convenient way to do it is with (?!) because an empty string always matches, so
  2204. an assertion that requires there not to be an empty string must always fail.
  2205. The backtracking control verb (*FAIL) or (*F) is a synonym for (?!).
  2206. <a name="lookbehind"></a></P>
  2207. <br><b>
  2208. Lookbehind assertions
  2209. </b><br>
  2210. <P>
  2211. Lookbehind assertions start with (?&#60;= for positive assertions and (?&#60;! for
  2212. negative assertions. For example,
  2213. <pre>
  2214. (?&#60;!foo)bar
  2215. </pre>
  2216. does find an occurrence of "bar" that is not preceded by "foo". The contents of
  2217. a lookbehind assertion are restricted such that all the strings it matches must
  2218. have a fixed length. However, if there are several top-level alternatives, they
  2219. do not all have to have the same fixed length. Thus
  2220. <pre>
  2221. (?&#60;=bullock|donkey)
  2222. </pre>
  2223. is permitted, but
  2224. <pre>
  2225. (?&#60;!dogs?|cats?)
  2226. </pre>
  2227. causes an error at compile time. Branches that match different length strings
  2228. are permitted only at the top level of a lookbehind assertion. This is an
  2229. extension compared with Perl, which requires all branches to match the same
  2230. length of string. An assertion such as
  2231. <pre>
  2232. (?&#60;=ab(c|de))
  2233. </pre>
  2234. is not permitted, because its single top-level branch can match two different
  2235. lengths, but it is acceptable to PCRE if rewritten to use two top-level
  2236. branches:
  2237. <pre>
  2238. (?&#60;=abc|abde)
  2239. </pre>
  2240. In some cases, the escape sequence \K
  2241. <a href="#resetmatchstart">(see above)</a>
  2242. can be used instead of a lookbehind assertion to get round the fixed-length
  2243. restriction.
  2244. </P>
  2245. <P>
  2246. The implementation of lookbehind assertions is, for each alternative, to
  2247. temporarily move the current position back by the fixed length and then try to
  2248. match. If there are insufficient characters before the current position, the
  2249. assertion fails.
  2250. </P>
  2251. <P>
  2252. In a UTF mode, PCRE does not allow the \C escape (which matches a single data
  2253. unit even in a UTF mode) to appear in lookbehind assertions, because it makes
  2254. it impossible to calculate the length of the lookbehind. The \X and \R
  2255. escapes, which can match different numbers of data units, are also not
  2256. permitted.
  2257. </P>
  2258. <P>
  2259. <a href="#subpatternsassubroutines">"Subroutine"</a>
  2260. calls (see below) such as (?2) or (?&X) are permitted in lookbehinds, as long
  2261. as the subpattern matches a fixed-length string.
  2262. <a href="#recursion">Recursion,</a>
  2263. however, is not supported.
  2264. </P>
  2265. <P>
  2266. Possessive quantifiers can be used in conjunction with lookbehind assertions to
  2267. specify efficient matching of fixed-length strings at the end of subject
  2268. strings. Consider a simple pattern such as
  2269. <pre>
  2270. abcd$
  2271. </pre>
  2272. when applied to a long string that does not match. Because matching proceeds
  2273. from left to right, PCRE will look for each "a" in the subject and then see if
  2274. what follows matches the rest of the pattern. If the pattern is specified as
  2275. <pre>
  2276. ^.*abcd$
  2277. </pre>
  2278. the initial .* matches the entire string at first, but when this fails (because
  2279. there is no following "a"), it backtracks to match all but the last character,
  2280. then all but the last two characters, and so on. Once again the search for "a"
  2281. covers the entire string, from right to left, so we are no better off. However,
  2282. if the pattern is written as
  2283. <pre>
  2284. ^.*+(?&#60;=abcd)
  2285. </pre>
  2286. there can be no backtracking for the .*+ item; it can match only the entire
  2287. string. The subsequent lookbehind assertion does a single test on the last four
  2288. characters. If it fails, the match fails immediately. For long strings, this
  2289. approach makes a significant difference to the processing time.
  2290. </P>
  2291. <br><b>
  2292. Using multiple assertions
  2293. </b><br>
  2294. <P>
  2295. Several assertions (of any sort) may occur in succession. For example,
  2296. <pre>
  2297. (?&#60;=\d{3})(?&#60;!999)foo
  2298. </pre>
  2299. matches "foo" preceded by three digits that are not "999". Notice that each of
  2300. the assertions is applied independently at the same point in the subject
  2301. string. First there is a check that the previous three characters are all
  2302. digits, and then there is a check that the same three characters are not "999".
  2303. This pattern does <i>not</i> match "foo" preceded by six characters, the first
  2304. of which are digits and the last three of which are not "999". For example, it
  2305. doesn't match "123abcfoo". A pattern to do that is
  2306. <pre>
  2307. (?&#60;=\d{3}...)(?&#60;!999)foo
  2308. </pre>
  2309. This time the first assertion looks at the preceding six characters, checking
  2310. that the first three are digits, and then the second assertion checks that the
  2311. preceding three characters are not "999".
  2312. </P>
  2313. <P>
  2314. Assertions can be nested in any combination. For example,
  2315. <pre>
  2316. (?&#60;=(?&#60;!foo)bar)baz
  2317. </pre>
  2318. matches an occurrence of "baz" that is preceded by "bar" which in turn is not
  2319. preceded by "foo", while
  2320. <pre>
  2321. (?&#60;=\d{3}(?!999)...)foo
  2322. </pre>
  2323. is another pattern that matches "foo" preceded by three digits and any three
  2324. characters that are not "999".
  2325. <a name="conditions"></a></P>
  2326. <br><a name="SEC21" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>
  2327. <P>
  2328. It is possible to cause the matching process to obey a subpattern
  2329. conditionally or to choose between two alternative subpatterns, depending on
  2330. the result of an assertion, or whether a specific capturing subpattern has
  2331. already been matched. The two possible forms of conditional subpattern are:
  2332. <pre>
  2333. (?(condition)yes-pattern)
  2334. (?(condition)yes-pattern|no-pattern)
  2335. </pre>
  2336. If the condition is satisfied, the yes-pattern is used; otherwise the
  2337. no-pattern (if present) is used. If there are more than two alternatives in the
  2338. subpattern, a compile-time error occurs. Each of the two alternatives may
  2339. itself contain nested subpatterns of any form, including conditional
  2340. subpatterns; the restriction to two alternatives applies only at the level of
  2341. the condition. This pattern fragment is an example where the alternatives are
  2342. complex:
  2343. <pre>
  2344. (?(1) (A|B|C) | (D | (?(2)E|F) | E) )
  2345. </PRE>
  2346. </P>
  2347. <P>
  2348. There are four kinds of condition: references to subpatterns, references to
  2349. recursion, a pseudo-condition called DEFINE, and assertions.
  2350. </P>
  2351. <br><b>
  2352. Checking for a used subpattern by number
  2353. </b><br>
  2354. <P>
  2355. If the text between the parentheses consists of a sequence of digits, the
  2356. condition is true if a capturing subpattern of that number has previously
  2357. matched. If there is more than one capturing subpattern with the same number
  2358. (see the earlier
  2359. <a href="#recursion">section about duplicate subpattern numbers),</a>
  2360. the condition is true if any of them have matched. An alternative notation is
  2361. to precede the digits with a plus or minus sign. In this case, the subpattern
  2362. number is relative rather than absolute. The most recently opened parentheses
  2363. can be referenced by (?(-1), the next most recent by (?(-2), and so on. Inside
  2364. loops it can also make sense to refer to subsequent groups. The next
  2365. parentheses to be opened can be referenced as (?(+1), and so on. (The value
  2366. zero in any of these forms is not used; it provokes a compile-time error.)
  2367. </P>
  2368. <P>
  2369. Consider the following pattern, which contains non-significant white space to
  2370. make it more readable (assume the PCRE_EXTENDED option) and to divide it into
  2371. three parts for ease of discussion:
  2372. <pre>
  2373. ( \( )? [^()]+ (?(1) \) )
  2374. </pre>
  2375. The first part matches an optional opening parenthesis, and if that
  2376. character is present, sets it as the first captured substring. The second part
  2377. matches one or more characters that are not parentheses. The third part is a
  2378. conditional subpattern that tests whether or not the first set of parentheses
  2379. matched. If they did, that is, if subject started with an opening parenthesis,
  2380. the condition is true, and so the yes-pattern is executed and a closing
  2381. parenthesis is required. Otherwise, since no-pattern is not present, the
  2382. subpattern matches nothing. In other words, this pattern matches a sequence of
  2383. non-parentheses, optionally enclosed in parentheses.
  2384. </P>
  2385. <P>
  2386. If you were embedding this pattern in a larger one, you could use a relative
  2387. reference:
  2388. <pre>
  2389. ...other stuff... ( \( )? [^()]+ (?(-1) \) ) ...
  2390. </pre>
  2391. This makes the fragment independent of the parentheses in the larger pattern.
  2392. </P>
  2393. <br><b>
  2394. Checking for a used subpattern by name
  2395. </b><br>
  2396. <P>
  2397. Perl uses the syntax (?(&#60;name&#62;)...) or (?('name')...) to test for a used
  2398. subpattern by name. For compatibility with earlier versions of PCRE, which had
  2399. this facility before Perl, the syntax (?(name)...) is also recognized.
  2400. </P>
  2401. <P>
  2402. Rewriting the above example to use a named subpattern gives this:
  2403. <pre>
  2404. (?&#60;OPEN&#62; \( )? [^()]+ (?(&#60;OPEN&#62;) \) )
  2405. </pre>
  2406. If the name used in a condition of this kind is a duplicate, the test is
  2407. applied to all subpatterns of the same name, and is true if any one of them has
  2408. matched.
  2409. </P>
  2410. <br><b>
  2411. Checking for pattern recursion
  2412. </b><br>
  2413. <P>
  2414. If the condition is the string (R), and there is no subpattern with the name R,
  2415. the condition is true if a recursive call to the whole pattern or any
  2416. subpattern has been made. If digits or a name preceded by ampersand follow the
  2417. letter R, for example:
  2418. <pre>
  2419. (?(R3)...) or (?(R&name)...)
  2420. </pre>
  2421. the condition is true if the most recent recursion is into a subpattern whose
  2422. number or name is given. This condition does not check the entire recursion
  2423. stack. If the name used in a condition of this kind is a duplicate, the test is
  2424. applied to all subpatterns of the same name, and is true if any one of them is
  2425. the most recent recursion.
  2426. </P>
  2427. <P>
  2428. At "top level", all these recursion test conditions are false.
  2429. <a href="#recursion">The syntax for recursive patterns</a>
  2430. is described below.
  2431. <a name="subdefine"></a></P>
  2432. <br><b>
  2433. Defining subpatterns for use by reference only
  2434. </b><br>
  2435. <P>
  2436. If the condition is the string (DEFINE), and there is no subpattern with the
  2437. name DEFINE, the condition is always false. In this case, there may be only one
  2438. alternative in the subpattern. It is always skipped if control reaches this
  2439. point in the pattern; the idea of DEFINE is that it can be used to define
  2440. subroutines that can be referenced from elsewhere. (The use of
  2441. <a href="#subpatternsassubroutines">subroutines</a>
  2442. is described below.) For example, a pattern to match an IPv4 address such as
  2443. "192.168.23.245" could be written like this (ignore white space and line
  2444. breaks):
  2445. <pre>
  2446. (?(DEFINE) (?&#60;byte&#62; 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
  2447. \b (?&byte) (\.(?&byte)){3} \b
  2448. </pre>
  2449. The first part of the pattern is a DEFINE group inside which a another group
  2450. named "byte" is defined. This matches an individual component of an IPv4
  2451. address (a number less than 256). When matching takes place, this part of the
  2452. pattern is skipped because DEFINE acts like a false condition. The rest of the
  2453. pattern uses references to the named group to match the four dot-separated
  2454. components of an IPv4 address, insisting on a word boundary at each end.
  2455. </P>
  2456. <br><b>
  2457. Assertion conditions
  2458. </b><br>
  2459. <P>
  2460. If the condition is not in any of the above formats, it must be an assertion.
  2461. This may be a positive or negative lookahead or lookbehind assertion. Consider
  2462. this pattern, again containing non-significant white space, and with the two
  2463. alternatives on the second line:
  2464. <pre>
  2465. (?(?=[^a-z]*[a-z])
  2466. \d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} )
  2467. </pre>
  2468. The condition is a positive lookahead assertion that matches an optional
  2469. sequence of non-letters followed by a letter. In other words, it tests for the
  2470. presence of at least one letter in the subject. If a letter is found, the
  2471. subject is matched against the first alternative; otherwise it is matched
  2472. against the second. This pattern matches strings in one of the two forms
  2473. dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.
  2474. <a name="comments"></a></P>
  2475. <br><a name="SEC22" href="#TOC1">COMMENTS</a><br>
  2476. <P>
  2477. There are two ways of including comments in patterns that are processed by
  2478. PCRE. In both cases, the start of the comment must not be in a character class,
  2479. nor in the middle of any other sequence of related characters such as (?: or a
  2480. subpattern name or number. The characters that make up a comment play no part
  2481. in the pattern matching.
  2482. </P>
  2483. <P>
  2484. The sequence (?# marks the start of a comment that continues up to the next
  2485. closing parenthesis. Nested parentheses are not permitted. If the PCRE_EXTENDED
  2486. option is set, an unescaped # character also introduces a comment, which in
  2487. this case continues to immediately after the next newline character or
  2488. character sequence in the pattern. Which characters are interpreted as newlines
  2489. is controlled by the options passed to a compiling function or by a special
  2490. sequence at the start of the pattern, as described in the section entitled
  2491. <a href="#newlines">"Newline conventions"</a>
  2492. above. Note that the end of this type of comment is a literal newline sequence
  2493. in the pattern; escape sequences that happen to represent a newline do not
  2494. count. For example, consider this pattern when PCRE_EXTENDED is set, and the
  2495. default newline convention is in force:
  2496. <pre>
  2497. abc #comment \n still comment
  2498. </pre>
  2499. On encountering the # character, <b>pcre_compile()</b> skips along, looking for
  2500. a newline in the pattern. The sequence \n is still literal at this stage, so
  2501. it does not terminate the comment. Only an actual character with the code value
  2502. 0x0a (the default newline) does so.
  2503. <a name="recursion"></a></P>
  2504. <br><a name="SEC23" href="#TOC1">RECURSIVE PATTERNS</a><br>
  2505. <P>
  2506. Consider the problem of matching a string in parentheses, allowing for
  2507. unlimited nested parentheses. Without the use of recursion, the best that can
  2508. be done is to use a pattern that matches up to some fixed depth of nesting. It
  2509. is not possible to handle an arbitrary nesting depth.
  2510. </P>
  2511. <P>
  2512. For some time, Perl has provided a facility that allows regular expressions to
  2513. recurse (amongst other things). It does this by interpolating Perl code in the
  2514. expression at run time, and the code can refer to the expression itself. A Perl
  2515. pattern using code interpolation to solve the parentheses problem can be
  2516. created like this:
  2517. <pre>
  2518. $re = qr{\( (?: (?&#62;[^()]+) | (?p{$re}) )* \)}x;
  2519. </pre>
  2520. The (?p{...}) item interpolates Perl code at run time, and in this case refers
  2521. recursively to the pattern in which it appears.
  2522. </P>
  2523. <P>
  2524. Obviously, PCRE cannot support the interpolation of Perl code. Instead, it
  2525. supports special syntax for recursion of the entire pattern, and also for
  2526. individual subpattern recursion. After its introduction in PCRE and Python,
  2527. this kind of recursion was subsequently introduced into Perl at release 5.10.
  2528. </P>
  2529. <P>
  2530. A special item that consists of (? followed by a number greater than zero and a
  2531. closing parenthesis is a recursive subroutine call of the subpattern of the
  2532. given number, provided that it occurs inside that subpattern. (If not, it is a
  2533. <a href="#subpatternsassubroutines">non-recursive subroutine</a>
  2534. call, which is described in the next section.) The special item (?R) or (?0) is
  2535. a recursive call of the entire regular expression.
  2536. </P>
  2537. <P>
  2538. This PCRE pattern solves the nested parentheses problem (assume the
  2539. PCRE_EXTENDED option is set so that white space is ignored):
  2540. <pre>
  2541. \( ( [^()]++ | (?R) )* \)
  2542. </pre>
  2543. First it matches an opening parenthesis. Then it matches any number of
  2544. substrings which can either be a sequence of non-parentheses, or a recursive
  2545. match of the pattern itself (that is, a correctly parenthesized substring).
  2546. Finally there is a closing parenthesis. Note the use of a possessive quantifier
  2547. to avoid backtracking into sequences of non-parentheses.
  2548. </P>
  2549. <P>
  2550. If this were part of a larger pattern, you would not want to recurse the entire
  2551. pattern, so instead you could use this:
  2552. <pre>
  2553. ( \( ( [^()]++ | (?1) )* \) )
  2554. </pre>
  2555. We have put the pattern into parentheses, and caused the recursion to refer to
  2556. them instead of the whole pattern.
  2557. </P>
  2558. <P>
  2559. In a larger pattern, keeping track of parenthesis numbers can be tricky. This
  2560. is made easier by the use of relative references. Instead of (?1) in the
  2561. pattern above you can write (?-2) to refer to the second most recently opened
  2562. parentheses preceding the recursion. In other words, a negative number counts
  2563. capturing parentheses leftwards from the point at which it is encountered.
  2564. </P>
  2565. <P>
  2566. It is also possible to refer to subsequently opened parentheses, by writing
  2567. references such as (?+2). However, these cannot be recursive because the
  2568. reference is not inside the parentheses that are referenced. They are always
  2569. <a href="#subpatternsassubroutines">non-recursive subroutine</a>
  2570. calls, as described in the next section.
  2571. </P>
  2572. <P>
  2573. An alternative approach is to use named parentheses instead. The Perl syntax
  2574. for this is (?&name); PCRE's earlier syntax (?P&#62;name) is also supported. We
  2575. could rewrite the above example as follows:
  2576. <pre>
  2577. (?&#60;pn&#62; \( ( [^()]++ | (?&pn) )* \) )
  2578. </pre>
  2579. If there is more than one subpattern with the same name, the earliest one is
  2580. used.
  2581. </P>
  2582. <P>
  2583. This particular example pattern that we have been looking at contains nested
  2584. unlimited repeats, and so the use of a possessive quantifier for matching
  2585. strings of non-parentheses is important when applying the pattern to strings
  2586. that do not match. For example, when this pattern is applied to
  2587. <pre>
  2588. (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
  2589. </pre>
  2590. it yields "no match" quickly. However, if a possessive quantifier is not used,
  2591. the match runs for a very long time indeed because there are so many different
  2592. ways the + and * repeats can carve up the subject, and all have to be tested
  2593. before failure can be reported.
  2594. </P>
  2595. <P>
  2596. At the end of a match, the values of capturing parentheses are those from
  2597. the outermost level. If you want to obtain intermediate values, a callout
  2598. function can be used (see below and the
  2599. <a href="pcrecallout.html"><b>pcrecallout</b></a>
  2600. documentation). If the pattern above is matched against
  2601. <pre>
  2602. (ab(cd)ef)
  2603. </pre>
  2604. the value for the inner capturing parentheses (numbered 2) is "ef", which is
  2605. the last value taken on at the top level. If a capturing subpattern is not
  2606. matched at the top level, its final captured value is unset, even if it was
  2607. (temporarily) set at a deeper level during the matching process.
  2608. </P>
  2609. <P>
  2610. If there are more than 15 capturing parentheses in a pattern, PCRE has to
  2611. obtain extra memory to store data during a recursion, which it does by using
  2612. <b>pcre_malloc</b>, freeing it via <b>pcre_free</b> afterwards. If no memory can
  2613. be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.
  2614. </P>
  2615. <P>
  2616. Do not confuse the (?R) item with the condition (R), which tests for recursion.
  2617. Consider this pattern, which matches text in angle brackets, allowing for
  2618. arbitrary nesting. Only digits are allowed in nested brackets (that is, when
  2619. recursing), whereas any characters are permitted at the outer level.
  2620. <pre>
  2621. &#60; (?: (?(R) \d++ | [^&#60;&#62;]*+) | (?R)) * &#62;
  2622. </pre>
  2623. In this pattern, (?(R) is the start of a conditional subpattern, with two
  2624. different alternatives for the recursive and non-recursive cases. The (?R) item
  2625. is the actual recursive call.
  2626. <a name="recursiondifference"></a></P>
  2627. <br><b>
  2628. Differences in recursion processing between PCRE and Perl
  2629. </b><br>
  2630. <P>
  2631. Recursion processing in PCRE differs from Perl in two important ways. In PCRE
  2632. (like Python, but unlike Perl), a recursive subpattern call is always treated
  2633. as an atomic group. That is, once it has matched some of the subject string, it
  2634. is never re-entered, even if it contains untried alternatives and there is a
  2635. subsequent matching failure. This can be illustrated by the following pattern,
  2636. which purports to match a palindromic string that contains an odd number of
  2637. characters (for example, "a", "aba", "abcba", "abcdcba"):
  2638. <pre>
  2639. ^(.|(.)(?1)\2)$
  2640. </pre>
  2641. The idea is that it either matches a single character, or two identical
  2642. characters surrounding a sub-palindrome. In Perl, this pattern works; in PCRE
  2643. it does not if the pattern is longer than three characters. Consider the
  2644. subject string "abcba":
  2645. </P>
  2646. <P>
  2647. At the top level, the first character is matched, but as it is not at the end
  2648. of the string, the first alternative fails; the second alternative is taken
  2649. and the recursion kicks in. The recursive call to subpattern 1 successfully
  2650. matches the next character ("b"). (Note that the beginning and end of line
  2651. tests are not part of the recursion).
  2652. </P>
  2653. <P>
  2654. Back at the top level, the next character ("c") is compared with what
  2655. subpattern 2 matched, which was "a". This fails. Because the recursion is
  2656. treated as an atomic group, there are now no backtracking points, and so the
  2657. entire match fails. (Perl is able, at this point, to re-enter the recursion and
  2658. try the second alternative.) However, if the pattern is written with the
  2659. alternatives in the other order, things are different:
  2660. <pre>
  2661. ^((.)(?1)\2|.)$
  2662. </pre>
  2663. This time, the recursing alternative is tried first, and continues to recurse
  2664. until it runs out of characters, at which point the recursion fails. But this
  2665. time we do have another alternative to try at the higher level. That is the big
  2666. difference: in the previous case the remaining alternative is at a deeper
  2667. recursion level, which PCRE cannot use.
  2668. </P>
  2669. <P>
  2670. To change the pattern so that it matches all palindromic strings, not just
  2671. those with an odd number of characters, it is tempting to change the pattern to
  2672. this:
  2673. <pre>
  2674. ^((.)(?1)\2|.?)$
  2675. </pre>
  2676. Again, this works in Perl, but not in PCRE, and for the same reason. When a
  2677. deeper recursion has matched a single character, it cannot be entered again in
  2678. order to match an empty string. The solution is to separate the two cases, and
  2679. write out the odd and even cases as alternatives at the higher level:
  2680. <pre>
  2681. ^(?:((.)(?1)\2|)|((.)(?3)\4|.))
  2682. </pre>
  2683. If you want to match typical palindromic phrases, the pattern has to ignore all
  2684. non-word characters, which can be done like this:
  2685. <pre>
  2686. ^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$
  2687. </pre>
  2688. If run with the PCRE_CASELESS option, this pattern matches phrases such as "A
  2689. man, a plan, a canal: Panama!" and it works well in both PCRE and Perl. Note
  2690. the use of the possessive quantifier *+ to avoid backtracking into sequences of
  2691. non-word characters. Without this, PCRE takes a great deal longer (ten times or
  2692. more) to match typical phrases, and Perl takes so long that you think it has
  2693. gone into a loop.
  2694. </P>
  2695. <P>
  2696. <b>WARNING</b>: The palindrome-matching patterns above work only if the subject
  2697. string does not start with a palindrome that is shorter than the entire string.
  2698. For example, although "abcba" is correctly matched, if the subject is "ababa",
  2699. PCRE finds the palindrome "aba" at the start, then fails at top level because
  2700. the end of the string does not follow. Once again, it cannot jump back into the
  2701. recursion to try other alternatives, so the entire match fails.
  2702. </P>
  2703. <P>
  2704. The second way in which PCRE and Perl differ in their recursion processing is
  2705. in the handling of captured values. In Perl, when a subpattern is called
  2706. recursively or as a subpattern (see the next section), it has no access to any
  2707. values that were captured outside the recursion, whereas in PCRE these values
  2708. can be referenced. Consider this pattern:
  2709. <pre>
  2710. ^(.)(\1|a(?2))
  2711. </pre>
  2712. In PCRE, this pattern matches "bab". The first capturing parentheses match "b",
  2713. then in the second group, when the back reference \1 fails to match "b", the
  2714. second alternative matches "a" and then recurses. In the recursion, \1 does
  2715. now match "b" and so the whole match succeeds. In Perl, the pattern fails to
  2716. match because inside the recursive call \1 cannot access the externally set
  2717. value.
  2718. <a name="subpatternsassubroutines"></a></P>
  2719. <br><a name="SEC24" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>
  2720. <P>
  2721. If the syntax for a recursive subpattern call (either by number or by
  2722. name) is used outside the parentheses to which it refers, it operates like a
  2723. subroutine in a programming language. The called subpattern may be defined
  2724. before or after the reference. A numbered reference can be absolute or
  2725. relative, as in these examples:
  2726. <pre>
  2727. (...(absolute)...)...(?2)...
  2728. (...(relative)...)...(?-1)...
  2729. (...(?+1)...(relative)...
  2730. </pre>
  2731. An earlier example pointed out that the pattern
  2732. <pre>
  2733. (sens|respons)e and \1ibility
  2734. </pre>
  2735. matches "sense and sensibility" and "response and responsibility", but not
  2736. "sense and responsibility". If instead the pattern
  2737. <pre>
  2738. (sens|respons)e and (?1)ibility
  2739. </pre>
  2740. is used, it does match "sense and responsibility" as well as the other two
  2741. strings. Another example is given in the discussion of DEFINE above.
  2742. </P>
  2743. <P>
  2744. All subroutine calls, whether recursive or not, are always treated as atomic
  2745. groups. That is, once a subroutine has matched some of the subject string, it
  2746. is never re-entered, even if it contains untried alternatives and there is a
  2747. subsequent matching failure. Any capturing parentheses that are set during the
  2748. subroutine call revert to their previous values afterwards.
  2749. </P>
  2750. <P>
  2751. Processing options such as case-independence are fixed when a subpattern is
  2752. defined, so if it is used as a subroutine, such options cannot be changed for
  2753. different calls. For example, consider this pattern:
  2754. <pre>
  2755. (abc)(?i:(?-1))
  2756. </pre>
  2757. It matches "abcabc". It does not match "abcABC" because the change of
  2758. processing option does not affect the called subpattern.
  2759. <a name="onigurumasubroutines"></a></P>
  2760. <br><a name="SEC25" href="#TOC1">ONIGURUMA SUBROUTINE SYNTAX</a><br>
  2761. <P>
  2762. For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
  2763. a number enclosed either in angle brackets or single quotes, is an alternative
  2764. syntax for referencing a subpattern as a subroutine, possibly recursively. Here
  2765. are two of the examples used above, rewritten using this syntax:
  2766. <pre>
  2767. (?&#60;pn&#62; \( ( (?&#62;[^()]+) | \g&#60;pn&#62; )* \) )
  2768. (sens|respons)e and \g'1'ibility
  2769. </pre>
  2770. PCRE supports an extension to Oniguruma: if a number is preceded by a
  2771. plus or a minus sign it is taken as a relative reference. For example:
  2772. <pre>
  2773. (abc)(?i:\g&#60;-1&#62;)
  2774. </pre>
  2775. Note that \g{...} (Perl syntax) and \g&#60;...&#62; (Oniguruma syntax) are <i>not</i>
  2776. synonymous. The former is a back reference; the latter is a subroutine call.
  2777. </P>
  2778. <br><a name="SEC26" href="#TOC1">CALLOUTS</a><br>
  2779. <P>
  2780. Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl
  2781. code to be obeyed in the middle of matching a regular expression. This makes it
  2782. possible, amongst other things, to extract different substrings that match the
  2783. same pair of parentheses when there is a repetition.
  2784. </P>
  2785. <P>
  2786. PCRE provides a similar feature, but of course it cannot obey arbitrary Perl
  2787. code. The feature is called "callout". The caller of PCRE provides an external
  2788. function by putting its entry point in the global variable <i>pcre_callout</i>
  2789. (8-bit library) or <i>pcre[16|32]_callout</i> (16-bit or 32-bit library).
  2790. By default, this variable contains NULL, which disables all calling out.
  2791. </P>
  2792. <P>
  2793. Within a regular expression, (?C) indicates the points at which the external
  2794. function is to be called. If you want to identify different callout points, you
  2795. can put a number less than 256 after the letter C. The default value is zero.
  2796. For example, this pattern has two callout points:
  2797. <pre>
  2798. (?C1)abc(?C2)def
  2799. </pre>
  2800. If the PCRE_AUTO_CALLOUT flag is passed to a compiling function, callouts are
  2801. automatically installed before each item in the pattern. They are all numbered
  2802. 255. If there is a conditional group in the pattern whose condition is an
  2803. assertion, an additional callout is inserted just before the condition. An
  2804. explicit callout may also be set at this position, as in this example:
  2805. <pre>
  2806. (?(?C9)(?=a)abc|def)
  2807. </pre>
  2808. Note that this applies only to assertion conditions, not to other types of
  2809. condition.
  2810. </P>
  2811. <P>
  2812. During matching, when PCRE reaches a callout point, the external function is
  2813. called. It is provided with the number of the callout, the position in the
  2814. pattern, and, optionally, one item of data originally supplied by the caller of
  2815. the matching function. The callout function may cause matching to proceed, to
  2816. backtrack, or to fail altogether.
  2817. </P>
  2818. <P>
  2819. By default, PCRE implements a number of optimizations at compile time and
  2820. matching time, and one side-effect is that sometimes callouts are skipped. If
  2821. you need all possible callouts to happen, you need to set options that disable
  2822. the relevant optimizations. More details, and a complete description of the
  2823. interface to the callout function, are given in the
  2824. <a href="pcrecallout.html"><b>pcrecallout</b></a>
  2825. documentation.
  2826. <a name="backtrackcontrol"></a></P>
  2827. <br><a name="SEC27" href="#TOC1">BACKTRACKING CONTROL</a><br>
  2828. <P>
  2829. Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which
  2830. are still described in the Perl documentation as "experimental and subject to
  2831. change or removal in a future version of Perl". It goes on to say: "Their usage
  2832. in production code should be noted to avoid problems during upgrades." The same
  2833. remarks apply to the PCRE features described in this section.
  2834. </P>
  2835. <P>
  2836. The new verbs make use of what was previously invalid syntax: an opening
  2837. parenthesis followed by an asterisk. They are generally of the form
  2838. (*VERB) or (*VERB:NAME). Some may take either form, possibly behaving
  2839. differently depending on whether or not a name is present. A name is any
  2840. sequence of characters that does not include a closing parenthesis. The maximum
  2841. length of name is 255 in the 8-bit library and 65535 in the 16-bit and 32-bit
  2842. libraries. If the name is empty, that is, if the closing parenthesis
  2843. immediately follows the colon, the effect is as if the colon were not there.
  2844. Any number of these verbs may occur in a pattern.
  2845. </P>
  2846. <P>
  2847. Since these verbs are specifically related to backtracking, most of them can be
  2848. used only when the pattern is to be matched using one of the traditional
  2849. matching functions, because these use a backtracking algorithm. With the
  2850. exception of (*FAIL), which behaves like a failing negative assertion, the
  2851. backtracking control verbs cause an error if encountered by a DFA matching
  2852. function.
  2853. </P>
  2854. <P>
  2855. The behaviour of these verbs in
  2856. <a href="#btrepeat">repeated groups,</a>
  2857. <a href="#btassert">assertions,</a>
  2858. and in
  2859. <a href="#btsub">subpatterns called as subroutines</a>
  2860. (whether or not recursively) is documented below.
  2861. <a name="nooptimize"></a></P>
  2862. <br><b>
  2863. Optimizations that affect backtracking verbs
  2864. </b><br>
  2865. <P>
  2866. PCRE contains some optimizations that are used to speed up matching by running
  2867. some checks at the start of each match attempt. For example, it may know the
  2868. minimum length of matching subject, or that a particular character must be
  2869. present. When one of these optimizations bypasses the running of a match, any
  2870. included backtracking verbs will not, of course, be processed. You can suppress
  2871. the start-of-match optimizations by setting the PCRE_NO_START_OPTIMIZE option
  2872. when calling <b>pcre_compile()</b> or <b>pcre_exec()</b>, or by starting the
  2873. pattern with (*NO_START_OPT). There is more discussion of this option in the
  2874. section entitled
  2875. <a href="pcreapi.html#execoptions">"Option bits for <b>pcre_exec()</b>"</a>
  2876. in the
  2877. <a href="pcreapi.html"><b>pcreapi</b></a>
  2878. documentation.
  2879. </P>
  2880. <P>
  2881. Experiments with Perl suggest that it too has similar optimizations, sometimes
  2882. leading to anomalous results.
  2883. </P>
  2884. <br><b>
  2885. Verbs that act immediately
  2886. </b><br>
  2887. <P>
  2888. The following verbs act as soon as they are encountered. They may not be
  2889. followed by a name.
  2890. <pre>
  2891. (*ACCEPT)
  2892. </pre>
  2893. This verb causes the match to end successfully, skipping the remainder of the
  2894. pattern. However, when it is inside a subpattern that is called as a
  2895. subroutine, only that subpattern is ended successfully. Matching then continues
  2896. at the outer level. If (*ACCEPT) in triggered in a positive assertion, the
  2897. assertion succeeds; in a negative assertion, the assertion fails.
  2898. </P>
  2899. <P>
  2900. If (*ACCEPT) is inside capturing parentheses, the data so far is captured. For
  2901. example:
  2902. <pre>
  2903. A((?:A|B(*ACCEPT)|C)D)
  2904. </pre>
  2905. This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is captured by
  2906. the outer parentheses.
  2907. <pre>
  2908. (*FAIL) or (*F)
  2909. </pre>
  2910. This verb causes a matching failure, forcing backtracking to occur. It is
  2911. equivalent to (?!) but easier to read. The Perl documentation notes that it is
  2912. probably useful only when combined with (?{}) or (??{}). Those are, of course,
  2913. Perl features that are not present in PCRE. The nearest equivalent is the
  2914. callout feature, as for example in this pattern:
  2915. <pre>
  2916. a+(?C)(*FAIL)
  2917. </pre>
  2918. A match with the string "aaaa" always fails, but the callout is taken before
  2919. each backtrack happens (in this example, 10 times).
  2920. </P>
  2921. <br><b>
  2922. Recording which path was taken
  2923. </b><br>
  2924. <P>
  2925. There is one verb whose main purpose is to track how a match was arrived at,
  2926. though it also has a secondary use in conjunction with advancing the match
  2927. starting point (see (*SKIP) below).
  2928. <pre>
  2929. (*MARK:NAME) or (*:NAME)
  2930. </pre>
  2931. A name is always required with this verb. There may be as many instances of
  2932. (*MARK) as you like in a pattern, and their names do not have to be unique.
  2933. </P>
  2934. <P>
  2935. When a match succeeds, the name of the last-encountered (*MARK:NAME),
  2936. (*PRUNE:NAME), or (*THEN:NAME) on the matching path is passed back to the
  2937. caller as described in the section entitled
  2938. <a href="pcreapi.html#extradata">"Extra data for <b>pcre_exec()</b>"</a>
  2939. in the
  2940. <a href="pcreapi.html"><b>pcreapi</b></a>
  2941. documentation. Here is an example of <b>pcretest</b> output, where the /K
  2942. modifier requests the retrieval and outputting of (*MARK) data:
  2943. <pre>
  2944. re&#62; /X(*MARK:A)Y|X(*MARK:B)Z/K
  2945. data&#62; XY
  2946. 0: XY
  2947. MK: A
  2948. XZ
  2949. 0: XZ
  2950. MK: B
  2951. </pre>
  2952. The (*MARK) name is tagged with "MK:" in this output, and in this example it
  2953. indicates which of the two alternatives matched. This is a more efficient way
  2954. of obtaining this information than putting each alternative in its own
  2955. capturing parentheses.
  2956. </P>
  2957. <P>
  2958. If a verb with a name is encountered in a positive assertion that is true, the
  2959. name is recorded and passed back if it is the last-encountered. This does not
  2960. happen for negative assertions or failing positive assertions.
  2961. </P>
  2962. <P>
  2963. After a partial match or a failed match, the last encountered name in the
  2964. entire match process is returned. For example:
  2965. <pre>
  2966. re&#62; /X(*MARK:A)Y|X(*MARK:B)Z/K
  2967. data&#62; XP
  2968. No match, mark = B
  2969. </pre>
  2970. Note that in this unanchored example the mark is retained from the match
  2971. attempt that started at the letter "X" in the subject. Subsequent match
  2972. attempts starting at "P" and then with an empty string do not get as far as the
  2973. (*MARK) item, but nevertheless do not reset it.
  2974. </P>
  2975. <P>
  2976. If you are interested in (*MARK) values after failed matches, you should
  2977. probably set the PCRE_NO_START_OPTIMIZE option
  2978. <a href="#nooptimize">(see above)</a>
  2979. to ensure that the match is always attempted.
  2980. </P>
  2981. <br><b>
  2982. Verbs that act after backtracking
  2983. </b><br>
  2984. <P>
  2985. The following verbs do nothing when they are encountered. Matching continues
  2986. with what follows, but if there is no subsequent match, causing a backtrack to
  2987. the verb, a failure is forced. That is, backtracking cannot pass to the left of
  2988. the verb. However, when one of these verbs appears inside an atomic group or an
  2989. assertion that is true, its effect is confined to that group, because once the
  2990. group has been matched, there is never any backtracking into it. In this
  2991. situation, backtracking can "jump back" to the left of the entire atomic group
  2992. or assertion. (Remember also, as stated above, that this localization also
  2993. applies in subroutine calls.)
  2994. </P>
  2995. <P>
  2996. These verbs differ in exactly what kind of failure occurs when backtracking
  2997. reaches them. The behaviour described below is what happens when the verb is
  2998. not in a subroutine or an assertion. Subsequent sections cover these special
  2999. cases.
  3000. <pre>
  3001. (*COMMIT)
  3002. </pre>
  3003. This verb, which may not be followed by a name, causes the whole match to fail
  3004. outright if there is a later matching failure that causes backtracking to reach
  3005. it. Even if the pattern is unanchored, no further attempts to find a match by
  3006. advancing the starting point take place. If (*COMMIT) is the only backtracking
  3007. verb that is encountered, once it has been passed <b>pcre_exec()</b> is
  3008. committed to finding a match at the current starting point, or not at all. For
  3009. example:
  3010. <pre>
  3011. a+(*COMMIT)b
  3012. </pre>
  3013. This matches "xxaab" but not "aacaab". It can be thought of as a kind of
  3014. dynamic anchor, or "I've started, so I must finish." The name of the most
  3015. recently passed (*MARK) in the path is passed back when (*COMMIT) forces a
  3016. match failure.
  3017. </P>
  3018. <P>
  3019. If there is more than one backtracking verb in a pattern, a different one that
  3020. follows (*COMMIT) may be triggered first, so merely passing (*COMMIT) during a
  3021. match does not always guarantee that a match must be at this starting point.
  3022. </P>
  3023. <P>
  3024. Note that (*COMMIT) at the start of a pattern is not the same as an anchor,
  3025. unless PCRE's start-of-match optimizations are turned off, as shown in this
  3026. output from <b>pcretest</b>:
  3027. <pre>
  3028. re&#62; /(*COMMIT)abc/
  3029. data&#62; xyzabc
  3030. 0: abc
  3031. data&#62; xyzabc\Y
  3032. No match
  3033. </pre>
  3034. For this pattern, PCRE knows that any match must start with "a", so the
  3035. optimization skips along the subject to "a" before applying the pattern to the
  3036. first set of data. The match attempt then succeeds. In the second set of data,
  3037. the escape sequence \Y is interpreted by the <b>pcretest</b> program. It causes
  3038. the PCRE_NO_START_OPTIMIZE option to be set when <b>pcre_exec()</b> is called.
  3039. This disables the optimization that skips along to the first character. The
  3040. pattern is now applied starting at "x", and so the (*COMMIT) causes the match
  3041. to fail without trying any other starting points.
  3042. <pre>
  3043. (*PRUNE) or (*PRUNE:NAME)
  3044. </pre>
  3045. This verb causes the match to fail at the current starting position in the
  3046. subject if there is a later matching failure that causes backtracking to reach
  3047. it. If the pattern is unanchored, the normal "bumpalong" advance to the next
  3048. starting character then happens. Backtracking can occur as usual to the left of
  3049. (*PRUNE), before it is reached, or when matching to the right of (*PRUNE), but
  3050. if there is no match to the right, backtracking cannot cross (*PRUNE). In
  3051. simple cases, the use of (*PRUNE) is just an alternative to an atomic group or
  3052. possessive quantifier, but there are some uses of (*PRUNE) that cannot be
  3053. expressed in any other way. In an anchored pattern (*PRUNE) has the same effect
  3054. as (*COMMIT).
  3055. </P>
  3056. <P>
  3057. The behaviour of (*PRUNE:NAME) is the not the same as (*MARK:NAME)(*PRUNE).
  3058. It is like (*MARK:NAME) in that the name is remembered for passing back to the
  3059. caller. However, (*SKIP:NAME) searches only for names set with (*MARK).
  3060. <pre>
  3061. (*SKIP)
  3062. </pre>
  3063. This verb, when given without a name, is like (*PRUNE), except that if the
  3064. pattern is unanchored, the "bumpalong" advance is not to the next character,
  3065. but to the position in the subject where (*SKIP) was encountered. (*SKIP)
  3066. signifies that whatever text was matched leading up to it cannot be part of a
  3067. successful match. Consider:
  3068. <pre>
  3069. a+(*SKIP)b
  3070. </pre>
  3071. If the subject is "aaaac...", after the first match attempt fails (starting at
  3072. the first character in the string), the starting point skips on to start the
  3073. next attempt at "c". Note that a possessive quantifer does not have the same
  3074. effect as this example; although it would suppress backtracking during the
  3075. first match attempt, the second attempt would start at the second character
  3076. instead of skipping on to "c".
  3077. <pre>
  3078. (*SKIP:NAME)
  3079. </pre>
  3080. When (*SKIP) has an associated name, its behaviour is modified. When it is
  3081. triggered, the previous path through the pattern is searched for the most
  3082. recent (*MARK) that has the same name. If one is found, the "bumpalong" advance
  3083. is to the subject position that corresponds to that (*MARK) instead of to where
  3084. (*SKIP) was encountered. If no (*MARK) with a matching name is found, the
  3085. (*SKIP) is ignored.
  3086. </P>
  3087. <P>
  3088. Note that (*SKIP:NAME) searches only for names set by (*MARK:NAME). It ignores
  3089. names that are set by (*PRUNE:NAME) or (*THEN:NAME).
  3090. <pre>
  3091. (*THEN) or (*THEN:NAME)
  3092. </pre>
  3093. This verb causes a skip to the next innermost alternative when backtracking
  3094. reaches it. That is, it cancels any further backtracking within the current
  3095. alternative. Its name comes from the observation that it can be used for a
  3096. pattern-based if-then-else block:
  3097. <pre>
  3098. ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
  3099. </pre>
  3100. If the COND1 pattern matches, FOO is tried (and possibly further items after
  3101. the end of the group if FOO succeeds); on failure, the matcher skips to the
  3102. second alternative and tries COND2, without backtracking into COND1. If that
  3103. succeeds and BAR fails, COND3 is tried. If subsequently BAZ fails, there are no
  3104. more alternatives, so there is a backtrack to whatever came before the entire
  3105. group. If (*THEN) is not inside an alternation, it acts like (*PRUNE).
  3106. </P>
  3107. <P>
  3108. The behaviour of (*THEN:NAME) is the not the same as (*MARK:NAME)(*THEN).
  3109. It is like (*MARK:NAME) in that the name is remembered for passing back to the
  3110. caller. However, (*SKIP:NAME) searches only for names set with (*MARK).
  3111. </P>
  3112. <P>
  3113. A subpattern that does not contain a | character is just a part of the
  3114. enclosing alternative; it is not a nested alternation with only one
  3115. alternative. The effect of (*THEN) extends beyond such a subpattern to the
  3116. enclosing alternative. Consider this pattern, where A, B, etc. are complex
  3117. pattern fragments that do not contain any | characters at this level:
  3118. <pre>
  3119. A (B(*THEN)C) | D
  3120. </pre>
  3121. If A and B are matched, but there is a failure in C, matching does not
  3122. backtrack into A; instead it moves to the next alternative, that is, D.
  3123. However, if the subpattern containing (*THEN) is given an alternative, it
  3124. behaves differently:
  3125. <pre>
  3126. A (B(*THEN)C | (*FAIL)) | D
  3127. </pre>
  3128. The effect of (*THEN) is now confined to the inner subpattern. After a failure
  3129. in C, matching moves to (*FAIL), which causes the whole subpattern to fail
  3130. because there are no more alternatives to try. In this case, matching does now
  3131. backtrack into A.
  3132. </P>
  3133. <P>
  3134. Note that a conditional subpattern is not considered as having two
  3135. alternatives, because only one is ever used. In other words, the | character in
  3136. a conditional subpattern has a different meaning. Ignoring white space,
  3137. consider:
  3138. <pre>
  3139. ^.*? (?(?=a) a | b(*THEN)c )
  3140. </pre>
  3141. If the subject is "ba", this pattern does not match. Because .*? is ungreedy,
  3142. it initially matches zero characters. The condition (?=a) then fails, the
  3143. character "b" is matched, but "c" is not. At this point, matching does not
  3144. backtrack to .*? as might perhaps be expected from the presence of the |
  3145. character. The conditional subpattern is part of the single alternative that
  3146. comprises the whole pattern, and so the match fails. (If there was a backtrack
  3147. into .*?, allowing it to match "b", the match would succeed.)
  3148. </P>
  3149. <P>
  3150. The verbs just described provide four different "strengths" of control when
  3151. subsequent matching fails. (*THEN) is the weakest, carrying on the match at the
  3152. next alternative. (*PRUNE) comes next, failing the match at the current
  3153. starting position, but allowing an advance to the next character (for an
  3154. unanchored pattern). (*SKIP) is similar, except that the advance may be more
  3155. than one character. (*COMMIT) is the strongest, causing the entire match to
  3156. fail.
  3157. </P>
  3158. <br><b>
  3159. More than one backtracking verb
  3160. </b><br>
  3161. <P>
  3162. If more than one backtracking verb is present in a pattern, the one that is
  3163. backtracked onto first acts. For example, consider this pattern, where A, B,
  3164. etc. are complex pattern fragments:
  3165. <pre>
  3166. (A(*COMMIT)B(*THEN)C|ABD)
  3167. </pre>
  3168. If A matches but B fails, the backtrack to (*COMMIT) causes the entire match to
  3169. fail. However, if A and B match, but C fails, the backtrack to (*THEN) causes
  3170. the next alternative (ABD) to be tried. This behaviour is consistent, but is
  3171. not always the same as Perl's. It means that if two or more backtracking verbs
  3172. appear in succession, all the the last of them has no effect. Consider this
  3173. example:
  3174. <pre>
  3175. ...(*COMMIT)(*PRUNE)...
  3176. </pre>
  3177. If there is a matching failure to the right, backtracking onto (*PRUNE) causes
  3178. it to be triggered, and its action is taken. There can never be a backtrack
  3179. onto (*COMMIT).
  3180. <a name="btrepeat"></a></P>
  3181. <br><b>
  3182. Backtracking verbs in repeated groups
  3183. </b><br>
  3184. <P>
  3185. PCRE differs from Perl in its handling of backtracking verbs in repeated
  3186. groups. For example, consider:
  3187. <pre>
  3188. /(a(*COMMIT)b)+ac/
  3189. </pre>
  3190. If the subject is "abac", Perl matches, but PCRE fails because the (*COMMIT) in
  3191. the second repeat of the group acts.
  3192. <a name="btassert"></a></P>
  3193. <br><b>
  3194. Backtracking verbs in assertions
  3195. </b><br>
  3196. <P>
  3197. (*FAIL) in an assertion has its normal effect: it forces an immediate backtrack.
  3198. </P>
  3199. <P>
  3200. (*ACCEPT) in a positive assertion causes the assertion to succeed without any
  3201. further processing. In a negative assertion, (*ACCEPT) causes the assertion to
  3202. fail without any further processing.
  3203. </P>
  3204. <P>
  3205. The other backtracking verbs are not treated specially if they appear in a
  3206. positive assertion. In particular, (*THEN) skips to the next alternative in the
  3207. innermost enclosing group that has alternations, whether or not this is within
  3208. the assertion.
  3209. </P>
  3210. <P>
  3211. Negative assertions are, however, different, in order to ensure that changing a
  3212. positive assertion into a negative assertion changes its result. Backtracking
  3213. into (*COMMIT), (*SKIP), or (*PRUNE) causes a negative assertion to be true,
  3214. without considering any further alternative branches in the assertion.
  3215. Backtracking into (*THEN) causes it to skip to the next enclosing alternative
  3216. within the assertion (the normal behaviour), but if the assertion does not have
  3217. such an alternative, (*THEN) behaves like (*PRUNE).
  3218. <a name="btsub"></a></P>
  3219. <br><b>
  3220. Backtracking verbs in subroutines
  3221. </b><br>
  3222. <P>
  3223. These behaviours occur whether or not the subpattern is called recursively.
  3224. Perl's treatment of subroutines is different in some cases.
  3225. </P>
  3226. <P>
  3227. (*FAIL) in a subpattern called as a subroutine has its normal effect: it forces
  3228. an immediate backtrack.
  3229. </P>
  3230. <P>
  3231. (*ACCEPT) in a subpattern called as a subroutine causes the subroutine match to
  3232. succeed without any further processing. Matching then continues after the
  3233. subroutine call.
  3234. </P>
  3235. <P>
  3236. (*COMMIT), (*SKIP), and (*PRUNE) in a subpattern called as a subroutine cause
  3237. the subroutine match to fail.
  3238. </P>
  3239. <P>
  3240. (*THEN) skips to the next alternative in the innermost enclosing group within
  3241. the subpattern that has alternatives. If there is no such group within the
  3242. subpattern, (*THEN) causes the subroutine match to fail.
  3243. </P>
  3244. <br><a name="SEC28" href="#TOC1">SEE ALSO</a><br>
  3245. <P>
  3246. <b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3),
  3247. <b>pcresyntax</b>(3), <b>pcre</b>(3), <b>pcre16(3)</b>, <b>pcre32(3)</b>.
  3248. </P>
  3249. <br><a name="SEC29" href="#TOC1">AUTHOR</a><br>
  3250. <P>
  3251. Philip Hazel
  3252. <br>
  3253. University Computing Service
  3254. <br>
  3255. Cambridge CB2 3QH, England.
  3256. <br>
  3257. </P>
  3258. <br><a name="SEC30" href="#TOC1">REVISION</a><br>
  3259. <P>
  3260. Last updated: 23 October 2016
  3261. <br>
  3262. Copyright &copy; 1997-2016 University of Cambridge.
  3263. <br>
  3264. <p>
  3265. Return to the <a href="index.html">PCRE index page</a>.
  3266. </p>