pcre2pattern.3 158 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903
  1. .TH PCRE2PATTERN 3 "06 October 2020" "PCRE2 10.35"
  2. .SH NAME
  3. PCRE2 - Perl-compatible regular expressions (revised API)
  4. .SH "PCRE2 REGULAR EXPRESSION DETAILS"
  5. .rs
  6. .sp
  7. The syntax and semantics of the regular expressions that are supported by PCRE2
  8. are described in detail below. There is a quick-reference syntax summary in the
  9. .\" HREF
  10. \fBpcre2syntax\fP
  11. .\"
  12. page. PCRE2 tries to match Perl syntax and semantics as closely as it can.
  13. PCRE2 also supports some alternative regular expression syntax (which does not
  14. conflict with the Perl syntax) in order to provide some compatibility with
  15. regular expressions in Python, .NET, and Oniguruma.
  16. .P
  17. Perl's regular expressions are described in its own documentation, and regular
  18. expressions in general are covered in a number of books, some of which have
  19. copious examples. Jeffrey Friedl's "Mastering Regular Expressions", published
  20. by O'Reilly, covers regular expressions in great detail. This description of
  21. PCRE2's regular expressions is intended as reference material.
  22. .P
  23. This document discusses the regular expression patterns that are supported by
  24. PCRE2 when its main matching function, \fBpcre2_match()\fP, is used. PCRE2 also
  25. has an alternative matching function, \fBpcre2_dfa_match()\fP, which matches
  26. using a different algorithm that is not Perl-compatible. Some of the features
  27. discussed below are not available when DFA matching is used. The advantages and
  28. disadvantages of the alternative function, and how it differs from the normal
  29. function, are discussed in the
  30. .\" HREF
  31. \fBpcre2matching\fP
  32. .\"
  33. page.
  34. .
  35. .
  36. .SH "SPECIAL START-OF-PATTERN ITEMS"
  37. .rs
  38. .sp
  39. A number of options that can be passed to \fBpcre2_compile()\fP can also be set
  40. by special items at the start of a pattern. These are not Perl-compatible, but
  41. are provided to make these options accessible to pattern writers who are not
  42. able to change the program that processes the pattern. Any number of these
  43. items may appear, but they must all be together right at the start of the
  44. pattern string, and the letters must be in upper case.
  45. .
  46. .
  47. .SS "UTF support"
  48. .rs
  49. .sp
  50. In the 8-bit and 16-bit PCRE2 libraries, characters may be coded either as
  51. single code units, or as multiple UTF-8 or UTF-16 code units. UTF-32 can be
  52. specified for the 32-bit library, in which case it constrains the character
  53. values to valid Unicode code points. To process UTF strings, PCRE2 must be
  54. built to include Unicode support (which is the default). When using UTF strings
  55. you must either call the compiling function with one or both of the PCRE2_UTF
  56. or PCRE2_MATCH_INVALID_UTF options, or the pattern must start with the special
  57. sequence (*UTF), which is equivalent to setting the relevant PCRE2_UTF. How
  58. setting a UTF mode affects pattern matching is mentioned in several places
  59. below. There is also a summary of features in the
  60. .\" HREF
  61. \fBpcre2unicode\fP
  62. .\"
  63. page.
  64. .P
  65. Some applications that allow their users to supply patterns may wish to
  66. restrict them to non-UTF data for security reasons. If the PCRE2_NEVER_UTF
  67. option is passed to \fBpcre2_compile()\fP, (*UTF) is not allowed, and its
  68. appearance in a pattern causes an error.
  69. .
  70. .
  71. .SS "Unicode property support"
  72. .rs
  73. .sp
  74. Another special sequence that may appear at the start of a pattern is (*UCP).
  75. This has the same effect as setting the PCRE2_UCP option: it causes sequences
  76. such as \ed and \ew to use Unicode properties to determine character types,
  77. instead of recognizing only characters with codes less than 256 via a lookup
  78. table. If also causes upper/lower casing operations to use Unicode properties
  79. for characters with code points greater than 127, even when UTF is not set.
  80. .P
  81. Some applications that allow their users to supply patterns may wish to
  82. restrict them for security reasons. If the PCRE2_NEVER_UCP option is passed to
  83. \fBpcre2_compile()\fP, (*UCP) is not allowed, and its appearance in a pattern
  84. causes an error.
  85. .
  86. .
  87. .SS "Locking out empty string matching"
  88. .rs
  89. .sp
  90. Starting a pattern with (*NOTEMPTY) or (*NOTEMPTY_ATSTART) has the same effect
  91. as passing the PCRE2_NOTEMPTY or PCRE2_NOTEMPTY_ATSTART option to whichever
  92. matching function is subsequently called to match the pattern. These options
  93. lock out the matching of empty strings, either entirely, or only at the start
  94. of the subject.
  95. .
  96. .
  97. .SS "Disabling auto-possessification"
  98. .rs
  99. .sp
  100. If a pattern starts with (*NO_AUTO_POSSESS), it has the same effect as setting
  101. the PCRE2_NO_AUTO_POSSESS option. This stops PCRE2 from making quantifiers
  102. possessive when what follows cannot match the repeated item. For example, by
  103. default a+b is treated as a++b. For more details, see the
  104. .\" HREF
  105. \fBpcre2api\fP
  106. .\"
  107. documentation.
  108. .
  109. .
  110. .SS "Disabling start-up optimizations"
  111. .rs
  112. .sp
  113. If a pattern starts with (*NO_START_OPT), it has the same effect as setting the
  114. PCRE2_NO_START_OPTIMIZE option. This disables several optimizations for quickly
  115. reaching "no match" results. For more details, see the
  116. .\" HREF
  117. \fBpcre2api\fP
  118. .\"
  119. documentation.
  120. .
  121. .
  122. .SS "Disabling automatic anchoring"
  123. .rs
  124. .sp
  125. If a pattern starts with (*NO_DOTSTAR_ANCHOR), it has the same effect as
  126. setting the PCRE2_NO_DOTSTAR_ANCHOR option. This disables optimizations that
  127. apply to patterns whose top-level branches all start with .* (match any number
  128. of arbitrary characters). For more details, see the
  129. .\" HREF
  130. \fBpcre2api\fP
  131. .\"
  132. documentation.
  133. .
  134. .
  135. .SS "Disabling JIT compilation"
  136. .rs
  137. .sp
  138. If a pattern that starts with (*NO_JIT) is successfully compiled, an attempt by
  139. the application to apply the JIT optimization by calling
  140. \fBpcre2_jit_compile()\fP is ignored.
  141. .
  142. .
  143. .SS "Setting match resource limits"
  144. .rs
  145. .sp
  146. The \fBpcre2_match()\fP function contains a counter that is incremented every
  147. time it goes round its main loop. The caller of \fBpcre2_match()\fP can set a
  148. limit on this counter, which therefore limits the amount of computing resource
  149. used for a match. The maximum depth of nested backtracking can also be limited;
  150. this indirectly restricts the amount of heap memory that is used, but there is
  151. also an explicit memory limit that can be set.
  152. .P
  153. These facilities are provided to catch runaway matches that are provoked by
  154. patterns with huge matching trees. A common example is a pattern with nested
  155. unlimited repeats applied to a long string that does not match. When one of
  156. these limits is reached, \fBpcre2_match()\fP gives an error return. The limits
  157. can also be set by items at the start of the pattern of the form
  158. .sp
  159. (*LIMIT_HEAP=d)
  160. (*LIMIT_MATCH=d)
  161. (*LIMIT_DEPTH=d)
  162. .sp
  163. where d is any number of decimal digits. However, the value of the setting must
  164. be less than the value set (or defaulted) by the caller of \fBpcre2_match()\fP
  165. for it to have any effect. In other words, the pattern writer can lower the
  166. limits set by the programmer, but not raise them. If there is more than one
  167. setting of one of these limits, the lower value is used. The heap limit is
  168. specified in kibibytes (units of 1024 bytes).
  169. .P
  170. Prior to release 10.30, LIMIT_DEPTH was called LIMIT_RECURSION. This name is
  171. still recognized for backwards compatibility.
  172. .P
  173. The heap limit applies only when the \fBpcre2_match()\fP or
  174. \fBpcre2_dfa_match()\fP interpreters are used for matching. It does not apply
  175. to JIT. The match limit is used (but in a different way) when JIT is being
  176. used, or when \fBpcre2_dfa_match()\fP is called, to limit computing resource
  177. usage by those matching functions. The depth limit is ignored by JIT but is
  178. relevant for DFA matching, which uses function recursion for recursions within
  179. the pattern and for lookaround assertions and atomic groups. In this case, the
  180. depth limit controls the depth of such recursion.
  181. .
  182. .
  183. .\" HTML <a name="newlines"></a>
  184. .SS "Newline conventions"
  185. .rs
  186. .sp
  187. PCRE2 supports six different conventions for indicating line breaks in
  188. strings: a single CR (carriage return) character, a single LF (linefeed)
  189. character, the two-character sequence CRLF, any of the three preceding, any
  190. Unicode newline sequence, or the NUL character (binary zero). The
  191. .\" HREF
  192. \fBpcre2api\fP
  193. .\"
  194. page has
  195. .\" HTML <a href="pcre2api.html#newlines">
  196. .\" </a>
  197. further discussion
  198. .\"
  199. about newlines, and shows how to set the newline convention when calling
  200. \fBpcre2_compile()\fP.
  201. .P
  202. It is also possible to specify a newline convention by starting a pattern
  203. string with one of the following sequences:
  204. .sp
  205. (*CR) carriage return
  206. (*LF) linefeed
  207. (*CRLF) carriage return, followed by linefeed
  208. (*ANYCRLF) any of the three above
  209. (*ANY) all Unicode newline sequences
  210. (*NUL) the NUL character (binary zero)
  211. .sp
  212. These override the default and the options given to the compiling function. For
  213. example, on a Unix system where LF is the default newline sequence, the pattern
  214. .sp
  215. (*CR)a.b
  216. .sp
  217. changes the convention to CR. That pattern matches "a\enb" because LF is no
  218. longer a newline. If more than one of these settings is present, the last one
  219. is used.
  220. .P
  221. The newline convention affects where the circumflex and dollar assertions are
  222. true. It also affects the interpretation of the dot metacharacter when
  223. PCRE2_DOTALL is not set, and the behaviour of \eN when not followed by an
  224. opening brace. However, it does not affect what the \eR escape sequence
  225. matches. By default, this is any Unicode newline sequence, for Perl
  226. compatibility. However, this can be changed; see the next section and the
  227. description of \eR in the section entitled
  228. .\" HTML <a href="#newlineseq">
  229. .\" </a>
  230. "Newline sequences"
  231. .\"
  232. below. A change of \eR setting can be combined with a change of newline
  233. convention.
  234. .
  235. .
  236. .SS "Specifying what \eR matches"
  237. .rs
  238. .sp
  239. It is possible to restrict \eR to match only CR, LF, or CRLF (instead of the
  240. complete set of Unicode line endings) by setting the option PCRE2_BSR_ANYCRLF
  241. at compile time. This effect can also be achieved by starting a pattern with
  242. (*BSR_ANYCRLF). For completeness, (*BSR_UNICODE) is also recognized,
  243. corresponding to PCRE2_BSR_UNICODE.
  244. .
  245. .
  246. .SH "EBCDIC CHARACTER CODES"
  247. .rs
  248. .sp
  249. PCRE2 can be compiled to run in an environment that uses EBCDIC as its
  250. character code instead of ASCII or Unicode (typically a mainframe system). In
  251. the sections below, character code values are ASCII or Unicode; in an EBCDIC
  252. environment these characters may have different code values, and there are no
  253. code points greater than 255.
  254. .
  255. .
  256. .SH "CHARACTERS AND METACHARACTERS"
  257. .rs
  258. .sp
  259. A regular expression is a pattern that is matched against a subject string from
  260. left to right. Most characters stand for themselves in a pattern, and match the
  261. corresponding characters in the subject. As a trivial example, the pattern
  262. .sp
  263. The quick brown fox
  264. .sp
  265. matches a portion of a subject string that is identical to itself. When
  266. caseless matching is specified (the PCRE2_CASELESS option or (?i) within the
  267. pattern), letters are matched independently of case. Note that there are two
  268. ASCII characters, K and S, that, in addition to their lower case ASCII
  269. equivalents, are case-equivalent with Unicode U+212A (Kelvin sign) and U+017F
  270. (long S) respectively when either PCRE2_UTF or PCRE2_UCP is set.
  271. .P
  272. The power of regular expressions comes from the ability to include wild cards,
  273. character classes, alternatives, and repetitions in the pattern. These are
  274. encoded in the pattern by the use of \fImetacharacters\fP, which do not stand
  275. for themselves but instead are interpreted in some special way.
  276. .P
  277. There are two different sets of metacharacters: those that are recognized
  278. anywhere in the pattern except within square brackets, and those that are
  279. recognized within square brackets. Outside square brackets, the metacharacters
  280. are as follows:
  281. .sp
  282. \e general escape character with several uses
  283. ^ assert start of string (or line, in multiline mode)
  284. $ assert end of string (or line, in multiline mode)
  285. . match any character except newline (by default)
  286. [ start character class definition
  287. | start of alternative branch
  288. ( start group or control verb
  289. ) end group or control verb
  290. * 0 or more quantifier
  291. + 1 or more quantifier; also "possessive quantifier"
  292. ? 0 or 1 quantifier; also quantifier minimizer
  293. { start min/max quantifier
  294. .sp
  295. Part of a pattern that is in square brackets is called a "character class". In
  296. a character class the only metacharacters are:
  297. .sp
  298. \e general escape character
  299. ^ negate the class, but only if the first character
  300. - indicates character range
  301. [ POSIX character class (if followed by POSIX syntax)
  302. ] terminates the character class
  303. .sp
  304. If a pattern is compiled with the PCRE2_EXTENDED option, most white space in
  305. the pattern, other than in a character class, and characters between a #
  306. outside a character class and the next newline, inclusive, are ignored. An
  307. escaping backslash can be used to include a white space or a # character as
  308. part of the pattern. If the PCRE2_EXTENDED_MORE option is set, the same
  309. applies, but in addition unescaped space and horizontal tab characters are
  310. ignored inside a character class. Note: only these two characters are ignored,
  311. not the full set of pattern white space characters that are ignored outside a
  312. character class. Option settings can be changed within a pattern; see the
  313. section entitled
  314. .\" HTML <a href="#internaloptions">
  315. .\" </a>
  316. "Internal Option Setting"
  317. .\"
  318. below.
  319. .P
  320. The following sections describe the use of each of the metacharacters.
  321. .
  322. .
  323. .SH BACKSLASH
  324. .rs
  325. .sp
  326. The backslash character has several uses. Firstly, if it is followed by a
  327. character that is not a digit or a letter, it takes away any special meaning
  328. that character may have. This use of backslash as an escape character applies
  329. both inside and outside character classes.
  330. .P
  331. For example, if you want to match a * character, you must write \e* in the
  332. pattern. This escaping action applies whether or not the following character
  333. would otherwise be interpreted as a metacharacter, so it is always safe to
  334. precede a non-alphanumeric with backslash to specify that it stands for itself.
  335. In particular, if you want to match a backslash, you write \e\e.
  336. .P
  337. Only ASCII digits and letters have any special meaning after a backslash. All
  338. other characters (in particular, those whose code points are greater than 127)
  339. are treated as literals.
  340. .P
  341. If you want to treat all characters in a sequence as literals, you can do so by
  342. putting them between \eQ and \eE. This is different from Perl in that $ and @
  343. are handled as literals in \eQ...\eE sequences in PCRE2, whereas in Perl, $ and
  344. @ cause variable interpolation. Also, Perl does "double-quotish backslash
  345. interpolation" on any backslashes between \eQ and \eE which, its documentation
  346. says, "may lead to confusing results". PCRE2 treats a backslash between \eQ and
  347. \eE just like any other character. Note the following examples:
  348. .sp
  349. Pattern PCRE2 matches Perl matches
  350. .sp
  351. .\" JOIN
  352. \eQabc$xyz\eE abc$xyz abc followed by the
  353. contents of $xyz
  354. \eQabc\e$xyz\eE abc\e$xyz abc\e$xyz
  355. \eQabc\eE\e$\eQxyz\eE abc$xyz abc$xyz
  356. \eQA\eB\eE A\eB A\eB
  357. \eQ\e\eE \e \e\eE
  358. .sp
  359. The \eQ...\eE sequence is recognized both inside and outside character classes.
  360. An isolated \eE that is not preceded by \eQ is ignored. If \eQ is not followed
  361. by \eE later in the pattern, the literal interpretation continues to the end of
  362. the pattern (that is, \eE is assumed at the end). If the isolated \eQ is inside
  363. a character class, this causes an error, because the character class is not
  364. terminated by a closing square bracket.
  365. .
  366. .
  367. .\" HTML <a name="digitsafterbackslash"></a>
  368. .SS "Non-printing characters"
  369. .rs
  370. .sp
  371. A second use of backslash provides a way of encoding non-printing characters
  372. in patterns in a visible manner. There is no restriction on the appearance of
  373. non-printing characters in a pattern, but when a pattern is being prepared by
  374. text editing, it is often easier to use one of the following escape sequences
  375. instead of the binary character it represents. In an ASCII or Unicode
  376. environment, these escapes are as follows:
  377. .sp
  378. \ea alarm, that is, the BEL character (hex 07)
  379. \ecx "control-x", where x is any printable ASCII character
  380. \ee escape (hex 1B)
  381. \ef form feed (hex 0C)
  382. \en linefeed (hex 0A)
  383. \er carriage return (hex 0D) (but see below)
  384. \et tab (hex 09)
  385. \e0dd character with octal code 0dd
  386. \eddd character with octal code ddd, or backreference
  387. \eo{ddd..} character with octal code ddd..
  388. \exhh character with hex code hh
  389. \ex{hhh..} character with hex code hhh..
  390. \eN{U+hhh..} character with Unicode hex code point hhh..
  391. .sp
  392. By default, after \ex that is not followed by {, from zero to two hexadecimal
  393. digits are read (letters can be in upper or lower case). Any number of
  394. hexadecimal digits may appear between \ex{ and }. If a character other than a
  395. hexadecimal digit appears between \ex{ and }, or if there is no terminating },
  396. an error occurs.
  397. .P
  398. Characters whose code points are less than 256 can be defined by either of the
  399. two syntaxes for \ex or by an octal sequence. There is no difference in the way
  400. they are handled. For example, \exdc is exactly the same as \ex{dc} or \e334.
  401. However, using the braced versions does make such sequences easier to read.
  402. .P
  403. Support is available for some ECMAScript (aka JavaScript) escape sequences via
  404. two compile-time options. If PCRE2_ALT_BSUX is set, the sequence \ex followed
  405. by { is not recognized. Only if \ex is followed by two hexadecimal digits is it
  406. recognized as a character escape. Otherwise it is interpreted as a literal "x"
  407. character. In this mode, support for code points greater than 256 is provided
  408. by \eu, which must be followed by four hexadecimal digits; otherwise it is
  409. interpreted as a literal "u" character.
  410. .P
  411. PCRE2_EXTRA_ALT_BSUX has the same effect as PCRE2_ALT_BSUX and, in addition,
  412. \eu{hhh..} is recognized as the character specified by hexadecimal code point.
  413. There may be any number of hexadecimal digits. This syntax is from ECMAScript
  414. 6.
  415. .P
  416. The \eN{U+hhh..} escape sequence is recognized only when PCRE2 is operating in
  417. UTF mode. Perl also uses \eN{name} to specify characters by Unicode name; PCRE2
  418. does not support this. Note that when \eN is not followed by an opening brace
  419. (curly bracket) it has an entirely different meaning, matching any character
  420. that is not a newline.
  421. .P
  422. There are some legacy applications where the escape sequence \er is expected to
  423. match a newline. If the PCRE2_EXTRA_ESCAPED_CR_IS_LF option is set, \er in a
  424. pattern is converted to \en so that it matches a LF (linefeed) instead of a CR
  425. (carriage return) character.
  426. .P
  427. The precise effect of \ecx on ASCII characters is as follows: if x is a lower
  428. case letter, it is converted to upper case. Then bit 6 of the character (hex
  429. 40) is inverted. Thus \ecA to \ecZ become hex 01 to hex 1A (A is 41, Z is 5A),
  430. but \ec{ becomes hex 3B ({ is 7B), and \ec; becomes hex 7B (; is 3B). If the
  431. code unit following \ec has a value less than 32 or greater than 126, a
  432. compile-time error occurs.
  433. .P
  434. When PCRE2 is compiled in EBCDIC mode, \eN{U+hhh..} is not supported. \ea, \ee,
  435. \ef, \en, \er, and \et generate the appropriate EBCDIC code values. The \ec
  436. escape is processed as specified for Perl in the \fBperlebcdic\fP document. The
  437. only characters that are allowed after \ec are A-Z, a-z, or one of @, [, \e, ],
  438. ^, _, or ?. Any other character provokes a compile-time error. The sequence
  439. \ec@ encodes character code 0; after \ec the letters (in either case) encode
  440. characters 1-26 (hex 01 to hex 1A); [, \e, ], ^, and _ encode characters 27-31
  441. (hex 1B to hex 1F), and \ec? becomes either 255 (hex FF) or 95 (hex 5F).
  442. .P
  443. Thus, apart from \ec?, these escapes generate the same character code values as
  444. they do in an ASCII environment, though the meanings of the values mostly
  445. differ. For example, \ecG always generates code value 7, which is BEL in ASCII
  446. but DEL in EBCDIC.
  447. .P
  448. The sequence \ec? generates DEL (127, hex 7F) in an ASCII environment, but
  449. because 127 is not a control character in EBCDIC, Perl makes it generate the
  450. APC character. Unfortunately, there are several variants of EBCDIC. In most of
  451. them the APC character has the value 255 (hex FF), but in the one Perl calls
  452. POSIX-BC its value is 95 (hex 5F). If certain other characters have POSIX-BC
  453. values, PCRE2 makes \ec? generate 95; otherwise it generates 255.
  454. .P
  455. After \e0 up to two further octal digits are read. If there are fewer than two
  456. digits, just those that are present are used. Thus the sequence \e0\ex\e015
  457. specifies two binary zeros followed by a CR character (code value 13). Make
  458. sure you supply two digits after the initial zero if the pattern character that
  459. follows is itself an octal digit.
  460. .P
  461. The escape \eo must be followed by a sequence of octal digits, enclosed in
  462. braces. An error occurs if this is not the case. This escape is a recent
  463. addition to Perl; it provides way of specifying character code points as octal
  464. numbers greater than 0777, and it also allows octal numbers and backreferences
  465. to be unambiguously specified.
  466. .P
  467. For greater clarity and unambiguity, it is best to avoid following \e by a
  468. digit greater than zero. Instead, use \eo{} or \ex{} to specify numerical
  469. character code points, and \eg{} to specify backreferences. The following
  470. paragraphs describe the old, ambiguous syntax.
  471. .P
  472. The handling of a backslash followed by a digit other than 0 is complicated,
  473. and Perl has changed over time, causing PCRE2 also to change.
  474. .P
  475. Outside a character class, PCRE2 reads the digit and any following digits as a
  476. decimal number. If the number is less than 10, begins with the digit 8 or 9, or
  477. if there are at least that many previous capture groups in the expression, the
  478. entire sequence is taken as a \fIbackreference\fP. A description of how this
  479. works is given
  480. .\" HTML <a href="#backreferences">
  481. .\" </a>
  482. later,
  483. .\"
  484. following the discussion of
  485. .\" HTML <a href="#group">
  486. .\" </a>
  487. parenthesized groups.
  488. .\"
  489. Otherwise, up to three octal digits are read to form a character code.
  490. .P
  491. Inside a character class, PCRE2 handles \e8 and \e9 as the literal characters
  492. "8" and "9", and otherwise reads up to three octal digits following the
  493. backslash, using them to generate a data character. Any subsequent digits stand
  494. for themselves. For example, outside a character class:
  495. .sp
  496. \e040 is another way of writing an ASCII space
  497. .\" JOIN
  498. \e40 is the same, provided there are fewer than 40
  499. previous capture groups
  500. \e7 is always a backreference
  501. .\" JOIN
  502. \e11 might be a backreference, or another way of
  503. writing a tab
  504. \e011 is always a tab
  505. \e0113 is a tab followed by the character "3"
  506. .\" JOIN
  507. \e113 might be a backreference, otherwise the
  508. character with octal code 113
  509. .\" JOIN
  510. \e377 might be a backreference, otherwise
  511. the value 255 (decimal)
  512. .\" JOIN
  513. \e81 is always a backreference
  514. .sp
  515. Note that octal values of 100 or greater that are specified using this syntax
  516. must not be introduced by a leading zero, because no more than three octal
  517. digits are ever read.
  518. .
  519. .
  520. .SS "Constraints on character values"
  521. .rs
  522. .sp
  523. Characters that are specified using octal or hexadecimal numbers are
  524. limited to certain values, as follows:
  525. .sp
  526. 8-bit non-UTF mode no greater than 0xff
  527. 16-bit non-UTF mode no greater than 0xffff
  528. 32-bit non-UTF mode no greater than 0xffffffff
  529. All UTF modes no greater than 0x10ffff and a valid code point
  530. .sp
  531. Invalid Unicode code points are all those in the range 0xd800 to 0xdfff (the
  532. so-called "surrogate" code points). The check for these can be disabled by the
  533. caller of \fBpcre2_compile()\fP by setting the option
  534. PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES. However, this is possible only in UTF-8
  535. and UTF-32 modes, because these values are not representable in UTF-16.
  536. .
  537. .
  538. .SS "Escape sequences in character classes"
  539. .rs
  540. .sp
  541. All the sequences that define a single character value can be used both inside
  542. and outside character classes. In addition, inside a character class, \eb is
  543. interpreted as the backspace character (hex 08).
  544. .P
  545. When not followed by an opening brace, \eN is not allowed in a character class.
  546. \eB, \eR, and \eX are not special inside a character class. Like other
  547. unrecognized alphabetic escape sequences, they cause an error. Outside a
  548. character class, these sequences have different meanings.
  549. .
  550. .
  551. .SS "Unsupported escape sequences"
  552. .rs
  553. .sp
  554. In Perl, the sequences \eF, \el, \eL, \eu, and \eU are recognized by its string
  555. handler and used to modify the case of following characters. By default, PCRE2
  556. does not support these escape sequences in patterns. However, if either of the
  557. PCRE2_ALT_BSUX or PCRE2_EXTRA_ALT_BSUX options is set, \eU matches a "U"
  558. character, and \eu can be used to define a character by code point, as
  559. described above.
  560. .
  561. .
  562. .SS "Absolute and relative backreferences"
  563. .rs
  564. .sp
  565. The sequence \eg followed by a signed or unsigned number, optionally enclosed
  566. in braces, is an absolute or relative backreference. A named backreference
  567. can be coded as \eg{name}. Backreferences are discussed
  568. .\" HTML <a href="#backreferences">
  569. .\" </a>
  570. later,
  571. .\"
  572. following the discussion of
  573. .\" HTML <a href="#group">
  574. .\" </a>
  575. parenthesized groups.
  576. .\"
  577. .
  578. .
  579. .SS "Absolute and relative subroutine calls"
  580. .rs
  581. .sp
  582. For compatibility with Oniguruma, the non-Perl syntax \eg followed by a name or
  583. a number enclosed either in angle brackets or single quotes, is an alternative
  584. syntax for referencing a capture group as a subroutine. Details are discussed
  585. .\" HTML <a href="#onigurumasubroutines">
  586. .\" </a>
  587. later.
  588. .\"
  589. Note that \eg{...} (Perl syntax) and \eg<...> (Oniguruma syntax) are \fInot\fP
  590. synonymous. The former is a backreference; the latter is a
  591. .\" HTML <a href="#groupsassubroutines">
  592. .\" </a>
  593. subroutine
  594. .\"
  595. call.
  596. .
  597. .
  598. .\" HTML <a name="genericchartypes"></a>
  599. .SS "Generic character types"
  600. .rs
  601. .sp
  602. Another use of backslash is for specifying generic character types:
  603. .sp
  604. \ed any decimal digit
  605. \eD any character that is not a decimal digit
  606. \eh any horizontal white space character
  607. \eH any character that is not a horizontal white space character
  608. \eN any character that is not a newline
  609. \es any white space character
  610. \eS any character that is not a white space character
  611. \ev any vertical white space character
  612. \eV any character that is not a vertical white space character
  613. \ew any "word" character
  614. \eW any "non-word" character
  615. .sp
  616. The \eN escape sequence has the same meaning as
  617. .\" HTML <a href="#fullstopdot">
  618. .\" </a>
  619. the "." metacharacter
  620. .\"
  621. when PCRE2_DOTALL is not set, but setting PCRE2_DOTALL does not change the
  622. meaning of \eN. Note that when \eN is followed by an opening brace it has a
  623. different meaning. See the section entitled
  624. .\" HTML <a href="#digitsafterbackslash">
  625. .\" </a>
  626. "Non-printing characters"
  627. .\"
  628. above for details. Perl also uses \eN{name} to specify characters by Unicode
  629. name; PCRE2 does not support this.
  630. .P
  631. Each pair of lower and upper case escape sequences partitions the complete set
  632. of characters into two disjoint sets. Any given character matches one, and only
  633. one, of each pair. The sequences can appear both inside and outside character
  634. classes. They each match one character of the appropriate type. If the current
  635. matching point is at the end of the subject string, all of them fail, because
  636. there is no character to match.
  637. .P
  638. The default \es characters are HT (9), LF (10), VT (11), FF (12), CR (13), and
  639. space (32), which are defined as white space in the "C" locale. This list may
  640. vary if locale-specific matching is taking place. For example, in some locales
  641. the "non-breaking space" character (\exA0) is recognized as white space, and in
  642. others the VT character is not.
  643. .P
  644. A "word" character is an underscore or any character that is a letter or digit.
  645. By default, the definition of letters and digits is controlled by PCRE2's
  646. low-valued character tables, and may vary if locale-specific matching is taking
  647. place (see
  648. .\" HTML <a href="pcre2api.html#localesupport">
  649. .\" </a>
  650. "Locale support"
  651. .\"
  652. in the
  653. .\" HREF
  654. \fBpcre2api\fP
  655. .\"
  656. page). For example, in a French locale such as "fr_FR" in Unix-like systems,
  657. or "french" in Windows, some character codes greater than 127 are used for
  658. accented letters, and these are then matched by \ew. The use of locales with
  659. Unicode is discouraged.
  660. .P
  661. By default, characters whose code points are greater than 127 never match \ed,
  662. \es, or \ew, and always match \eD, \eS, and \eW, although this may be different
  663. for characters in the range 128-255 when locale-specific matching is happening.
  664. These escape sequences retain their original meanings from before Unicode
  665. support was available, mainly for efficiency reasons. If the PCRE2_UCP option
  666. is set, the behaviour is changed so that Unicode properties are used to
  667. determine character types, as follows:
  668. .sp
  669. \ed any character that matches \ep{Nd} (decimal digit)
  670. \es any character that matches \ep{Z} or \eh or \ev
  671. \ew any character that matches \ep{L} or \ep{N}, plus underscore
  672. .sp
  673. The upper case escapes match the inverse sets of characters. Note that \ed
  674. matches only decimal digits, whereas \ew matches any Unicode digit, as well as
  675. any Unicode letter, and underscore. Note also that PCRE2_UCP affects \eb, and
  676. \eB because they are defined in terms of \ew and \eW. Matching these sequences
  677. is noticeably slower when PCRE2_UCP is set.
  678. .P
  679. The sequences \eh, \eH, \ev, and \eV, in contrast to the other sequences, which
  680. match only ASCII characters by default, always match a specific list of code
  681. points, whether or not PCRE2_UCP is set. The horizontal space characters are:
  682. .sp
  683. U+0009 Horizontal tab (HT)
  684. U+0020 Space
  685. U+00A0 Non-break space
  686. U+1680 Ogham space mark
  687. U+180E Mongolian vowel separator
  688. U+2000 En quad
  689. U+2001 Em quad
  690. U+2002 En space
  691. U+2003 Em space
  692. U+2004 Three-per-em space
  693. U+2005 Four-per-em space
  694. U+2006 Six-per-em space
  695. U+2007 Figure space
  696. U+2008 Punctuation space
  697. U+2009 Thin space
  698. U+200A Hair space
  699. U+202F Narrow no-break space
  700. U+205F Medium mathematical space
  701. U+3000 Ideographic space
  702. .sp
  703. The vertical space characters are:
  704. .sp
  705. U+000A Linefeed (LF)
  706. U+000B Vertical tab (VT)
  707. U+000C Form feed (FF)
  708. U+000D Carriage return (CR)
  709. U+0085 Next line (NEL)
  710. U+2028 Line separator
  711. U+2029 Paragraph separator
  712. .sp
  713. In 8-bit, non-UTF-8 mode, only the characters with code points less than 256
  714. are relevant.
  715. .
  716. .
  717. .\" HTML <a name="newlineseq"></a>
  718. .SS "Newline sequences"
  719. .rs
  720. .sp
  721. Outside a character class, by default, the escape sequence \eR matches any
  722. Unicode newline sequence. In 8-bit non-UTF-8 mode \eR is equivalent to the
  723. following:
  724. .sp
  725. (?>\er\en|\en|\ex0b|\ef|\er|\ex85)
  726. .sp
  727. This is an example of an "atomic group", details of which are given
  728. .\" HTML <a href="#atomicgroup">
  729. .\" </a>
  730. below.
  731. .\"
  732. This particular group matches either the two-character sequence CR followed by
  733. LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,
  734. U+000B), FF (form feed, U+000C), CR (carriage return, U+000D), or NEL (next
  735. line, U+0085). Because this is an atomic group, the two-character sequence is
  736. treated as a single unit that cannot be split.
  737. .P
  738. In other modes, two additional characters whose code points are greater than 255
  739. are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).
  740. Unicode support is not needed for these characters to be recognized.
  741. .P
  742. It is possible to restrict \eR to match only CR, LF, or CRLF (instead of the
  743. complete set of Unicode line endings) by setting the option PCRE2_BSR_ANYCRLF
  744. at compile time. (BSR is an abbrevation for "backslash R".) This can be made
  745. the default when PCRE2 is built; if this is the case, the other behaviour can
  746. be requested via the PCRE2_BSR_UNICODE option. It is also possible to specify
  747. these settings by starting a pattern string with one of the following
  748. sequences:
  749. .sp
  750. (*BSR_ANYCRLF) CR, LF, or CRLF only
  751. (*BSR_UNICODE) any Unicode newline sequence
  752. .sp
  753. These override the default and the options given to the compiling function.
  754. Note that these special settings, which are not Perl-compatible, are recognized
  755. only at the very start of a pattern, and that they must be in upper case. If
  756. more than one of them is present, the last one is used. They can be combined
  757. with a change of newline convention; for example, a pattern can start with:
  758. .sp
  759. (*ANY)(*BSR_ANYCRLF)
  760. .sp
  761. They can also be combined with the (*UTF) or (*UCP) special sequences. Inside a
  762. character class, \eR is treated as an unrecognized escape sequence, and causes
  763. an error.
  764. .
  765. .
  766. .\" HTML <a name="uniextseq"></a>
  767. .SS Unicode character properties
  768. .rs
  769. .sp
  770. When PCRE2 is built with Unicode support (the default), three additional escape
  771. sequences that match characters with specific properties are available. They
  772. can be used in any mode, though in 8-bit and 16-bit non-UTF modes these
  773. sequences are of course limited to testing characters whose code points are
  774. less than U+0100 and U+10000, respectively. In 32-bit non-UTF mode, code points
  775. greater than 0x10ffff (the Unicode limit) may be encountered. These are all
  776. treated as being in the Unknown script and with an unassigned type. The extra
  777. escape sequences are:
  778. .sp
  779. \ep{\fIxx\fP} a character with the \fIxx\fP property
  780. \eP{\fIxx\fP} a character without the \fIxx\fP property
  781. \eX a Unicode extended grapheme cluster
  782. .sp
  783. The property names represented by \fIxx\fP above are case-sensitive. There is
  784. support for Unicode script names, Unicode general category properties, "Any",
  785. which matches any character (including newline), and some special PCRE2
  786. properties (described in the
  787. .\" HTML <a href="#extraprops">
  788. .\" </a>
  789. next section).
  790. .\"
  791. Other Perl properties such as "InMusicalSymbols" are not supported by PCRE2.
  792. Note that \eP{Any} does not match any characters, so always causes a match
  793. failure.
  794. .P
  795. Sets of Unicode characters are defined as belonging to certain scripts. A
  796. character from one of these sets can be matched using a script name. For
  797. example:
  798. .sp
  799. \ep{Greek}
  800. \eP{Han}
  801. .sp
  802. Unassigned characters (and in non-UTF 32-bit mode, characters with code points
  803. greater than 0x10FFFF) are assigned the "Unknown" script. Others that are not
  804. part of an identified script are lumped together as "Common". The current list
  805. of scripts is:
  806. .P
  807. Adlam,
  808. Ahom,
  809. Anatolian_Hieroglyphs,
  810. Arabic,
  811. Armenian,
  812. Avestan,
  813. Balinese,
  814. Bamum,
  815. Bassa_Vah,
  816. Batak,
  817. Bengali,
  818. Bhaiksuki,
  819. Bopomofo,
  820. Brahmi,
  821. Braille,
  822. Buginese,
  823. Buhid,
  824. Canadian_Aboriginal,
  825. Carian,
  826. Caucasian_Albanian,
  827. Chakma,
  828. Cham,
  829. Cherokee,
  830. Chorasmian,
  831. Common,
  832. Coptic,
  833. Cuneiform,
  834. Cypriot,
  835. Cyrillic,
  836. Deseret,
  837. Devanagari,
  838. Dives_Akuru,
  839. Dogra,
  840. Duployan,
  841. Egyptian_Hieroglyphs,
  842. Elbasan,
  843. Elymaic,
  844. Ethiopic,
  845. Georgian,
  846. Glagolitic,
  847. Gothic,
  848. Grantha,
  849. Greek,
  850. Gujarati,
  851. Gunjala_Gondi,
  852. Gurmukhi,
  853. Han,
  854. Hangul,
  855. Hanifi_Rohingya,
  856. Hanunoo,
  857. Hatran,
  858. Hebrew,
  859. Hiragana,
  860. Imperial_Aramaic,
  861. Inherited,
  862. Inscriptional_Pahlavi,
  863. Inscriptional_Parthian,
  864. Javanese,
  865. Kaithi,
  866. Kannada,
  867. Katakana,
  868. Kayah_Li,
  869. Kharoshthi,
  870. Khitan_Small_Script,
  871. Khmer,
  872. Khojki,
  873. Khudawadi,
  874. Lao,
  875. Latin,
  876. Lepcha,
  877. Limbu,
  878. Linear_A,
  879. Linear_B,
  880. Lisu,
  881. Lycian,
  882. Lydian,
  883. Mahajani,
  884. Makasar,
  885. Malayalam,
  886. Mandaic,
  887. Manichaean,
  888. Marchen,
  889. Masaram_Gondi,
  890. Medefaidrin,
  891. Meetei_Mayek,
  892. Mende_Kikakui,
  893. Meroitic_Cursive,
  894. Meroitic_Hieroglyphs,
  895. Miao,
  896. Modi,
  897. Mongolian,
  898. Mro,
  899. Multani,
  900. Myanmar,
  901. Nabataean,
  902. Nandinagari,
  903. New_Tai_Lue,
  904. Newa,
  905. Nko,
  906. Nushu,
  907. Nyakeng_Puachue_Hmong,
  908. Ogham,
  909. Ol_Chiki,
  910. Old_Hungarian,
  911. Old_Italic,
  912. Old_North_Arabian,
  913. Old_Permic,
  914. Old_Persian,
  915. Old_Sogdian,
  916. Old_South_Arabian,
  917. Old_Turkic,
  918. Oriya,
  919. Osage,
  920. Osmanya,
  921. Pahawh_Hmong,
  922. Palmyrene,
  923. Pau_Cin_Hau,
  924. Phags_Pa,
  925. Phoenician,
  926. Psalter_Pahlavi,
  927. Rejang,
  928. Runic,
  929. Samaritan,
  930. Saurashtra,
  931. Sharada,
  932. Shavian,
  933. Siddham,
  934. SignWriting,
  935. Sinhala,
  936. Sogdian,
  937. Sora_Sompeng,
  938. Soyombo,
  939. Sundanese,
  940. Syloti_Nagri,
  941. Syriac,
  942. Tagalog,
  943. Tagbanwa,
  944. Tai_Le,
  945. Tai_Tham,
  946. Tai_Viet,
  947. Takri,
  948. Tamil,
  949. Tangut,
  950. Telugu,
  951. Thaana,
  952. Thai,
  953. Tibetan,
  954. Tifinagh,
  955. Tirhuta,
  956. Ugaritic,
  957. Unknown,
  958. Vai,
  959. Wancho,
  960. Warang_Citi,
  961. Yezidi,
  962. Yi,
  963. Zanabazar_Square.
  964. .P
  965. Each character has exactly one Unicode general category property, specified by
  966. a two-letter abbreviation. For compatibility with Perl, negation can be
  967. specified by including a circumflex between the opening brace and the property
  968. name. For example, \ep{^Lu} is the same as \eP{Lu}.
  969. .P
  970. If only one letter is specified with \ep or \eP, it includes all the general
  971. category properties that start with that letter. In this case, in the absence
  972. of negation, the curly brackets in the escape sequence are optional; these two
  973. examples have the same effect:
  974. .sp
  975. \ep{L}
  976. \epL
  977. .sp
  978. The following general category property codes are supported:
  979. .sp
  980. C Other
  981. Cc Control
  982. Cf Format
  983. Cn Unassigned
  984. Co Private use
  985. Cs Surrogate
  986. .sp
  987. L Letter
  988. Ll Lower case letter
  989. Lm Modifier letter
  990. Lo Other letter
  991. Lt Title case letter
  992. Lu Upper case letter
  993. .sp
  994. M Mark
  995. Mc Spacing mark
  996. Me Enclosing mark
  997. Mn Non-spacing mark
  998. .sp
  999. N Number
  1000. Nd Decimal number
  1001. Nl Letter number
  1002. No Other number
  1003. .sp
  1004. P Punctuation
  1005. Pc Connector punctuation
  1006. Pd Dash punctuation
  1007. Pe Close punctuation
  1008. Pf Final punctuation
  1009. Pi Initial punctuation
  1010. Po Other punctuation
  1011. Ps Open punctuation
  1012. .sp
  1013. S Symbol
  1014. Sc Currency symbol
  1015. Sk Modifier symbol
  1016. Sm Mathematical symbol
  1017. So Other symbol
  1018. .sp
  1019. Z Separator
  1020. Zl Line separator
  1021. Zp Paragraph separator
  1022. Zs Space separator
  1023. .sp
  1024. The special property L& is also supported: it matches a character that has
  1025. the Lu, Ll, or Lt property, in other words, a letter that is not classified as
  1026. a modifier or "other".
  1027. .P
  1028. The Cs (Surrogate) property applies only to characters whose code points are in
  1029. the range U+D800 to U+DFFF. These characters are no different to any other
  1030. character when PCRE2 is not in UTF mode (using the 16-bit or 32-bit library).
  1031. However, they are not valid in Unicode strings and so cannot be tested by PCRE2
  1032. in UTF mode, unless UTF validity checking has been turned off (see the
  1033. discussion of PCRE2_NO_UTF_CHECK in the
  1034. .\" HREF
  1035. \fBpcre2api\fP
  1036. .\"
  1037. page).
  1038. .P
  1039. The long synonyms for property names that Perl supports (such as \ep{Letter})
  1040. are not supported by PCRE2, nor is it permitted to prefix any of these
  1041. properties with "Is".
  1042. .P
  1043. No character that is in the Unicode table has the Cn (unassigned) property.
  1044. Instead, this property is assumed for any code point that is not in the
  1045. Unicode table.
  1046. .P
  1047. Specifying caseless matching does not affect these escape sequences. For
  1048. example, \ep{Lu} always matches only upper case letters. This is different from
  1049. the behaviour of current versions of Perl.
  1050. .P
  1051. Matching characters by Unicode property is not fast, because PCRE2 has to do a
  1052. multistage table lookup in order to find a character's property. That is why
  1053. the traditional escape sequences such as \ed and \ew do not use Unicode
  1054. properties in PCRE2 by default, though you can make them do so by setting the
  1055. PCRE2_UCP option or by starting the pattern with (*UCP).
  1056. .
  1057. .
  1058. .SS Extended grapheme clusters
  1059. .rs
  1060. .sp
  1061. The \eX escape matches any number of Unicode characters that form an "extended
  1062. grapheme cluster", and treats the sequence as an atomic group
  1063. .\" HTML <a href="#atomicgroup">
  1064. .\" </a>
  1065. (see below).
  1066. .\"
  1067. Unicode supports various kinds of composite character by giving each character
  1068. a grapheme breaking property, and having rules that use these properties to
  1069. define the boundaries of extended grapheme clusters. The rules are defined in
  1070. Unicode Standard Annex 29, "Unicode Text Segmentation". Unicode 11.0.0
  1071. abandoned the use of some previous properties that had been used for emojis.
  1072. Instead it introduced various emoji-specific properties. PCRE2 uses only the
  1073. Extended Pictographic property.
  1074. .P
  1075. \eX always matches at least one character. Then it decides whether to add
  1076. additional characters according to the following rules for ending a cluster:
  1077. .P
  1078. 1. End at the end of the subject string.
  1079. .P
  1080. 2. Do not end between CR and LF; otherwise end after any control character.
  1081. .P
  1082. 3. Do not break Hangul (a Korean script) syllable sequences. Hangul characters
  1083. are of five types: L, V, T, LV, and LVT. An L character may be followed by an
  1084. L, V, LV, or LVT character; an LV or V character may be followed by a V or T
  1085. character; an LVT or T character may be follwed only by a T character.
  1086. .P
  1087. 4. Do not end before extending characters or spacing marks or the "zero-width
  1088. joiner" character. Characters with the "mark" property always have the
  1089. "extend" grapheme breaking property.
  1090. .P
  1091. 5. Do not end after prepend characters.
  1092. .P
  1093. 6. Do not break within emoji modifier sequences or emoji zwj sequences. That
  1094. is, do not break between characters with the Extended_Pictographic property.
  1095. Extend and ZWJ characters are allowed between the characters.
  1096. .P
  1097. 7. Do not break within emoji flag sequences. That is, do not break between
  1098. regional indicator (RI) characters if there are an odd number of RI characters
  1099. before the break point.
  1100. .P
  1101. 8. Otherwise, end the cluster.
  1102. .
  1103. .
  1104. .\" HTML <a name="extraprops"></a>
  1105. .SS PCRE2's additional properties
  1106. .rs
  1107. .sp
  1108. As well as the standard Unicode properties described above, PCRE2 supports four
  1109. more that make it possible to convert traditional escape sequences such as \ew
  1110. and \es to use Unicode properties. PCRE2 uses these non-standard, non-Perl
  1111. properties internally when PCRE2_UCP is set. However, they may also be used
  1112. explicitly. These properties are:
  1113. .sp
  1114. Xan Any alphanumeric character
  1115. Xps Any POSIX space character
  1116. Xsp Any Perl space character
  1117. Xwd Any Perl "word" character
  1118. .sp
  1119. Xan matches characters that have either the L (letter) or the N (number)
  1120. property. Xps matches the characters tab, linefeed, vertical tab, form feed, or
  1121. carriage return, and any other character that has the Z (separator) property.
  1122. Xsp is the same as Xps; in PCRE1 it used to exclude vertical tab, for Perl
  1123. compatibility, but Perl changed. Xwd matches the same characters as Xan, plus
  1124. underscore.
  1125. .P
  1126. There is another non-standard property, Xuc, which matches any character that
  1127. can be represented by a Universal Character Name in C++ and other programming
  1128. languages. These are the characters $, @, ` (grave accent), and all characters
  1129. with Unicode code points greater than or equal to U+00A0, except for the
  1130. surrogates U+D800 to U+DFFF. Note that most base (ASCII) characters are
  1131. excluded. (Universal Character Names are of the form \euHHHH or \eUHHHHHHHH
  1132. where H is a hexadecimal digit. Note that the Xuc property does not match these
  1133. sequences but the characters that they represent.)
  1134. .
  1135. .
  1136. .\" HTML <a name="resetmatchstart"></a>
  1137. .SS "Resetting the match start"
  1138. .rs
  1139. .sp
  1140. In normal use, the escape sequence \eK causes any previously matched characters
  1141. not to be included in the final matched sequence that is returned. For example,
  1142. the pattern:
  1143. .sp
  1144. foo\eKbar
  1145. .sp
  1146. matches "foobar", but reports that it has matched "bar". \eK does not interact
  1147. with anchoring in any way. The pattern:
  1148. .sp
  1149. ^foo\eKbar
  1150. .sp
  1151. matches only when the subject begins with "foobar" (in single line mode),
  1152. though it again reports the matched string as "bar". This feature is similar to
  1153. a lookbehind assertion
  1154. .\" HTML <a href="#lookbehind">
  1155. .\" </a>
  1156. (described below).
  1157. .\"
  1158. However, in this case, the part of the subject before the real match does not
  1159. have to be of fixed length, as lookbehind assertions do. The use of \eK does
  1160. not interfere with the setting of
  1161. .\" HTML <a href="#group">
  1162. .\" </a>
  1163. captured substrings.
  1164. .\"
  1165. For example, when the pattern
  1166. .sp
  1167. (foo)\eKbar
  1168. .sp
  1169. matches "foobar", the first substring is still set to "foo".
  1170. .P
  1171. Perl used to document that the use of \eK within lookaround assertions is "not
  1172. well defined", but from version 5.32.0 Perl does not support this usage at all.
  1173. In PCRE2, \eK is acted upon when it occurs inside positive assertions, but is
  1174. ignored in negative assertions. Note that when a pattern such as (?=ab\eK)
  1175. matches, the reported start of the match can be greater than the end of the
  1176. match. Using \eK in a lookbehind assertion at the start of a pattern can also
  1177. lead to odd effects. For example, consider this pattern:
  1178. .sp
  1179. (?<=\eKfoo)bar
  1180. .sp
  1181. If the subject is "foobar", a call to \fBpcre2_match()\fP with a starting
  1182. offset of 3 succeeds and reports the matching string as "foobar", that is, the
  1183. start of the reported match is earlier than where the match started.
  1184. .
  1185. .
  1186. .\" HTML <a name="smallassertions"></a>
  1187. .SS "Simple assertions"
  1188. .rs
  1189. .sp
  1190. The final use of backslash is for certain simple assertions. An assertion
  1191. specifies a condition that has to be met at a particular point in a match,
  1192. without consuming any characters from the subject string. The use of
  1193. groups for more complicated assertions is described
  1194. .\" HTML <a href="#bigassertions">
  1195. .\" </a>
  1196. below.
  1197. .\"
  1198. The backslashed assertions are:
  1199. .sp
  1200. \eb matches at a word boundary
  1201. \eB matches when not at a word boundary
  1202. \eA matches at the start of the subject
  1203. \eZ matches at the end of the subject
  1204. also matches before a newline at the end of the subject
  1205. \ez matches only at the end of the subject
  1206. \eG matches at the first matching position in the subject
  1207. .sp
  1208. Inside a character class, \eb has a different meaning; it matches the backspace
  1209. character. If any other of these assertions appears in a character class, an
  1210. "invalid escape sequence" error is generated.
  1211. .P
  1212. A word boundary is a position in the subject string where the current character
  1213. and the previous character do not both match \ew or \eW (i.e. one matches
  1214. \ew and the other matches \eW), or the start or end of the string if the
  1215. first or last character matches \ew, respectively. When PCRE2 is built with
  1216. Unicode support, the meanings of \ew and \eW can be changed by setting the
  1217. PCRE2_UCP option. When this is done, it also affects \eb and \eB. Neither PCRE2
  1218. nor Perl has a separate "start of word" or "end of word" metasequence. However,
  1219. whatever follows \eb normally determines which it is. For example, the fragment
  1220. \eba matches "a" at the start of a word.
  1221. .P
  1222. The \eA, \eZ, and \ez assertions differ from the traditional circumflex and
  1223. dollar (described in the next section) in that they only ever match at the very
  1224. start and end of the subject string, whatever options are set. Thus, they are
  1225. independent of multiline mode. These three assertions are not affected by the
  1226. PCRE2_NOTBOL or PCRE2_NOTEOL options, which affect only the behaviour of the
  1227. circumflex and dollar metacharacters. However, if the \fIstartoffset\fP
  1228. argument of \fBpcre2_match()\fP is non-zero, indicating that matching is to
  1229. start at a point other than the beginning of the subject, \eA can never match.
  1230. The difference between \eZ and \ez is that \eZ matches before a newline at the
  1231. end of the string as well as at the very end, whereas \ez matches only at the
  1232. end.
  1233. .P
  1234. The \eG assertion is true only when the current matching position is at the
  1235. start point of the matching process, as specified by the \fIstartoffset\fP
  1236. argument of \fBpcre2_match()\fP. It differs from \eA when the value of
  1237. \fIstartoffset\fP is non-zero. By calling \fBpcre2_match()\fP multiple times
  1238. with appropriate arguments, you can mimic Perl's /g option, and it is in this
  1239. kind of implementation where \eG can be useful.
  1240. .P
  1241. Note, however, that PCRE2's implementation of \eG, being true at the starting
  1242. character of the matching process, is subtly different from Perl's, which
  1243. defines it as true at the end of the previous match. In Perl, these can be
  1244. different when the previously matched string was empty. Because PCRE2 does just
  1245. one match at a time, it cannot reproduce this behaviour.
  1246. .P
  1247. If all the alternatives of a pattern begin with \eG, the expression is anchored
  1248. to the starting match position, and the "anchored" flag is set in the compiled
  1249. regular expression.
  1250. .
  1251. .
  1252. .SH "CIRCUMFLEX AND DOLLAR"
  1253. .rs
  1254. .sp
  1255. The circumflex and dollar metacharacters are zero-width assertions. That is,
  1256. they test for a particular condition being true without consuming any
  1257. characters from the subject string. These two metacharacters are concerned with
  1258. matching the starts and ends of lines. If the newline convention is set so that
  1259. only the two-character sequence CRLF is recognized as a newline, isolated CR
  1260. and LF characters are treated as ordinary data characters, and are not
  1261. recognized as newlines.
  1262. .P
  1263. Outside a character class, in the default matching mode, the circumflex
  1264. character is an assertion that is true only if the current matching point is at
  1265. the start of the subject string. If the \fIstartoffset\fP argument of
  1266. \fBpcre2_match()\fP is non-zero, or if PCRE2_NOTBOL is set, circumflex can
  1267. never match if the PCRE2_MULTILINE option is unset. Inside a character class,
  1268. circumflex has an entirely different meaning
  1269. .\" HTML <a href="#characterclass">
  1270. .\" </a>
  1271. (see below).
  1272. .\"
  1273. .P
  1274. Circumflex need not be the first character of the pattern if a number of
  1275. alternatives are involved, but it should be the first thing in each alternative
  1276. in which it appears if the pattern is ever to match that branch. If all
  1277. possible alternatives start with a circumflex, that is, if the pattern is
  1278. constrained to match only at the start of the subject, it is said to be an
  1279. "anchored" pattern. (There are also other constructs that can cause a pattern
  1280. to be anchored.)
  1281. .P
  1282. The dollar character is an assertion that is true only if the current matching
  1283. point is at the end of the subject string, or immediately before a newline at
  1284. the end of the string (by default), unless PCRE2_NOTEOL is set. Note, however,
  1285. that it does not actually match the newline. Dollar need not be the last
  1286. character of the pattern if a number of alternatives are involved, but it
  1287. should be the last item in any branch in which it appears. Dollar has no
  1288. special meaning in a character class.
  1289. .P
  1290. The meaning of dollar can be changed so that it matches only at the very end of
  1291. the string, by setting the PCRE2_DOLLAR_ENDONLY option at compile time. This
  1292. does not affect the \eZ assertion.
  1293. .P
  1294. The meanings of the circumflex and dollar metacharacters are changed if the
  1295. PCRE2_MULTILINE option is set. When this is the case, a dollar character
  1296. matches before any newlines in the string, as well as at the very end, and a
  1297. circumflex matches immediately after internal newlines as well as at the start
  1298. of the subject string. It does not match after a newline that ends the string,
  1299. for compatibility with Perl. However, this can be changed by setting the
  1300. PCRE2_ALT_CIRCUMFLEX option.
  1301. .P
  1302. For example, the pattern /^abc$/ matches the subject string "def\enabc" (where
  1303. \en represents a newline) in multiline mode, but not otherwise. Consequently,
  1304. patterns that are anchored in single line mode because all branches start with
  1305. ^ are not anchored in multiline mode, and a match for circumflex is possible
  1306. when the \fIstartoffset\fP argument of \fBpcre2_match()\fP is non-zero. The
  1307. PCRE2_DOLLAR_ENDONLY option is ignored if PCRE2_MULTILINE is set.
  1308. .P
  1309. When the newline convention (see
  1310. .\" HTML <a href="#newlines">
  1311. .\" </a>
  1312. "Newline conventions"
  1313. .\"
  1314. below) recognizes the two-character sequence CRLF as a newline, this is
  1315. preferred, even if the single characters CR and LF are also recognized as
  1316. newlines. For example, if the newline convention is "any", a multiline mode
  1317. circumflex matches before "xyz" in the string "abc\er\enxyz" rather than after
  1318. CR, even though CR on its own is a valid newline. (It also matches at the very
  1319. start of the string, of course.)
  1320. .P
  1321. Note that the sequences \eA, \eZ, and \ez can be used to match the start and
  1322. end of the subject in both modes, and if all branches of a pattern start with
  1323. \eA it is always anchored, whether or not PCRE2_MULTILINE is set.
  1324. .
  1325. .
  1326. .\" HTML <a name="fullstopdot"></a>
  1327. .SH "FULL STOP (PERIOD, DOT) AND \eN"
  1328. .rs
  1329. .sp
  1330. Outside a character class, a dot in the pattern matches any one character in
  1331. the subject string except (by default) a character that signifies the end of a
  1332. line.
  1333. .P
  1334. When a line ending is defined as a single character, dot never matches that
  1335. character; when the two-character sequence CRLF is used, dot does not match CR
  1336. if it is immediately followed by LF, but otherwise it matches all characters
  1337. (including isolated CRs and LFs). When any Unicode line endings are being
  1338. recognized, dot does not match CR or LF or any of the other line ending
  1339. characters.
  1340. .P
  1341. The behaviour of dot with regard to newlines can be changed. If the
  1342. PCRE2_DOTALL option is set, a dot matches any one character, without exception.
  1343. If the two-character sequence CRLF is present in the subject string, it takes
  1344. two dots to match it.
  1345. .P
  1346. The handling of dot is entirely independent of the handling of circumflex and
  1347. dollar, the only relationship being that they both involve newlines. Dot has no
  1348. special meaning in a character class.
  1349. .P
  1350. The escape sequence \eN when not followed by an opening brace behaves like a
  1351. dot, except that it is not affected by the PCRE2_DOTALL option. In other words,
  1352. it matches any character except one that signifies the end of a line.
  1353. .P
  1354. When \eN is followed by an opening brace it has a different meaning. See the
  1355. section entitled
  1356. .\" HTML <a href="digitsafterbackslash">
  1357. .\" </a>
  1358. "Non-printing characters"
  1359. .\"
  1360. above for details. Perl also uses \eN{name} to specify characters by Unicode
  1361. name; PCRE2 does not support this.
  1362. .
  1363. .
  1364. .SH "MATCHING A SINGLE CODE UNIT"
  1365. .rs
  1366. .sp
  1367. Outside a character class, the escape sequence \eC matches any one code unit,
  1368. whether or not a UTF mode is set. In the 8-bit library, one code unit is one
  1369. byte; in the 16-bit library it is a 16-bit unit; in the 32-bit library it is a
  1370. 32-bit unit. Unlike a dot, \eC always matches line-ending characters. The
  1371. feature is provided in Perl in order to match individual bytes in UTF-8 mode,
  1372. but it is unclear how it can usefully be used.
  1373. .P
  1374. Because \eC breaks up characters into individual code units, matching one unit
  1375. with \eC in UTF-8 or UTF-16 mode means that the rest of the string may start
  1376. with a malformed UTF character. This has undefined results, because PCRE2
  1377. assumes that it is matching character by character in a valid UTF string (by
  1378. default it checks the subject string's validity at the start of processing
  1379. unless the PCRE2_NO_UTF_CHECK or PCRE2_MATCH_INVALID_UTF option is used).
  1380. .P
  1381. An application can lock out the use of \eC by setting the
  1382. PCRE2_NEVER_BACKSLASH_C option when compiling a pattern. It is also possible to
  1383. build PCRE2 with the use of \eC permanently disabled.
  1384. .P
  1385. PCRE2 does not allow \eC to appear in lookbehind assertions
  1386. .\" HTML <a href="#lookbehind">
  1387. .\" </a>
  1388. (described below)
  1389. .\"
  1390. in UTF-8 or UTF-16 modes, because this would make it impossible to calculate
  1391. the length of the lookbehind. Neither the alternative matching function
  1392. \fBpcre2_dfa_match()\fP nor the JIT optimizer support \eC in these UTF modes.
  1393. The former gives a match-time error; the latter fails to optimize and so the
  1394. match is always run using the interpreter.
  1395. .P
  1396. In the 32-bit library, however, \eC is always supported (when not explicitly
  1397. locked out) because it always matches a single code unit, whether or not UTF-32
  1398. is specified.
  1399. .P
  1400. In general, the \eC escape sequence is best avoided. However, one way of using
  1401. it that avoids the problem of malformed UTF-8 or UTF-16 characters is to use a
  1402. lookahead to check the length of the next character, as in this pattern, which
  1403. could be used with a UTF-8 string (ignore white space and line breaks):
  1404. .sp
  1405. (?| (?=[\ex00-\ex7f])(\eC) |
  1406. (?=[\ex80-\ex{7ff}])(\eC)(\eC) |
  1407. (?=[\ex{800}-\ex{ffff}])(\eC)(\eC)(\eC) |
  1408. (?=[\ex{10000}-\ex{1fffff}])(\eC)(\eC)(\eC)(\eC))
  1409. .sp
  1410. In this example, a group that starts with (?| resets the capturing parentheses
  1411. numbers in each alternative (see
  1412. .\" HTML <a href="#dupgroupnumber">
  1413. .\" </a>
  1414. "Duplicate Group Numbers"
  1415. .\"
  1416. below). The assertions at the start of each branch check the next UTF-8
  1417. character for values whose encoding uses 1, 2, 3, or 4 bytes, respectively. The
  1418. character's individual bytes are then captured by the appropriate number of
  1419. \eC groups.
  1420. .
  1421. .
  1422. .\" HTML <a name="characterclass"></a>
  1423. .SH "SQUARE BRACKETS AND CHARACTER CLASSES"
  1424. .rs
  1425. .sp
  1426. An opening square bracket introduces a character class, terminated by a closing
  1427. square bracket. A closing square bracket on its own is not special by default.
  1428. If a closing square bracket is required as a member of the class, it should be
  1429. the first data character in the class (after an initial circumflex, if present)
  1430. or escaped with a backslash. This means that, by default, an empty class cannot
  1431. be defined. However, if the PCRE2_ALLOW_EMPTY_CLASS option is set, a closing
  1432. square bracket at the start does end the (empty) class.
  1433. .P
  1434. A character class matches a single character in the subject. A matched
  1435. character must be in the set of characters defined by the class, unless the
  1436. first character in the class definition is a circumflex, in which case the
  1437. subject character must not be in the set defined by the class. If a circumflex
  1438. is actually required as a member of the class, ensure it is not the first
  1439. character, or escape it with a backslash.
  1440. .P
  1441. For example, the character class [aeiou] matches any lower case vowel, while
  1442. [^aeiou] matches any character that is not a lower case vowel. Note that a
  1443. circumflex is just a convenient notation for specifying the characters that
  1444. are in the class by enumerating those that are not. A class that starts with a
  1445. circumflex is not an assertion; it still consumes a character from the subject
  1446. string, and therefore it fails if the current pointer is at the end of the
  1447. string.
  1448. .P
  1449. Characters in a class may be specified by their code points using \eo, \ex, or
  1450. \eN{U+hh..} in the usual way. When caseless matching is set, any letters in a
  1451. class represent both their upper case and lower case versions, so for example,
  1452. a caseless [aeiou] matches "A" as well as "a", and a caseless [^aeiou] does not
  1453. match "A", whereas a caseful version would. Note that there are two ASCII
  1454. characters, K and S, that, in addition to their lower case ASCII equivalents,
  1455. are case-equivalent with Unicode U+212A (Kelvin sign) and U+017F (long S)
  1456. respectively when either PCRE2_UTF or PCRE2_UCP is set.
  1457. .P
  1458. Characters that might indicate line breaks are never treated in any special way
  1459. when matching character classes, whatever line-ending sequence is in use, and
  1460. whatever setting of the PCRE2_DOTALL and PCRE2_MULTILINE options is used. A
  1461. class such as [^a] always matches one of these characters.
  1462. .P
  1463. The generic character type escape sequences \ed, \eD, \eh, \eH, \ep, \eP, \es,
  1464. \eS, \ev, \eV, \ew, and \eW may appear in a character class, and add the
  1465. characters that they match to the class. For example, [\edABCDEF] matches any
  1466. hexadecimal digit. In UTF modes, the PCRE2_UCP option affects the meanings of
  1467. \ed, \es, \ew and their upper case partners, just as it does when they appear
  1468. outside a character class, as described in the section entitled
  1469. .\" HTML <a href="#genericchartypes">
  1470. .\" </a>
  1471. "Generic character types"
  1472. .\"
  1473. above. The escape sequence \eb has a different meaning inside a character
  1474. class; it matches the backspace character. The sequences \eB, \eR, and \eX are
  1475. not special inside a character class. Like any other unrecognized escape
  1476. sequences, they cause an error. The same is true for \eN when not followed by
  1477. an opening brace.
  1478. .P
  1479. The minus (hyphen) character can be used to specify a range of characters in a
  1480. character class. For example, [d-m] matches any letter between d and m,
  1481. inclusive. If a minus character is required in a class, it must be escaped with
  1482. a backslash or appear in a position where it cannot be interpreted as
  1483. indicating a range, typically as the first or last character in the class,
  1484. or immediately after a range. For example, [b-d-z] matches letters in the range
  1485. b to d, a hyphen character, or z.
  1486. .P
  1487. Perl treats a hyphen as a literal if it appears before or after a POSIX class
  1488. (see below) or before or after a character type escape such as as \ed or \eH.
  1489. However, unless the hyphen is the last character in the class, Perl outputs a
  1490. warning in its warning mode, as this is most likely a user error. As PCRE2 has
  1491. no facility for warning, an error is given in these cases.
  1492. .P
  1493. It is not possible to have the literal character "]" as the end character of a
  1494. range. A pattern such as [W-]46] is interpreted as a class of two characters
  1495. ("W" and "-") followed by a literal string "46]", so it would match "W46]" or
  1496. "-46]". However, if the "]" is escaped with a backslash it is interpreted as
  1497. the end of range, so [W-\e]46] is interpreted as a class containing a range
  1498. followed by two other characters. The octal or hexadecimal representation of
  1499. "]" can also be used to end a range.
  1500. .P
  1501. Ranges normally include all code points between the start and end characters,
  1502. inclusive. They can also be used for code points specified numerically, for
  1503. example [\e000-\e037]. Ranges can include any characters that are valid for the
  1504. current mode. In any UTF mode, the so-called "surrogate" characters (those
  1505. whose code points lie between 0xd800 and 0xdfff inclusive) may not be specified
  1506. explicitly by default (the PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES option disables
  1507. this check). However, ranges such as [\ex{d7ff}-\ex{e000}], which include the
  1508. surrogates, are always permitted.
  1509. .P
  1510. There is a special case in EBCDIC environments for ranges whose end points are
  1511. both specified as literal letters in the same case. For compatibility with
  1512. Perl, EBCDIC code points within the range that are not letters are omitted. For
  1513. example, [h-k] matches only four characters, even though the codes for h and k
  1514. are 0x88 and 0x92, a range of 11 code points. However, if the range is
  1515. specified numerically, for example, [\ex88-\ex92] or [h-\ex92], all code points
  1516. are included.
  1517. .P
  1518. If a range that includes letters is used when caseless matching is set, it
  1519. matches the letters in either case. For example, [W-c] is equivalent to
  1520. [][\e\e^_`wxyzabc], matched caselessly, and in a non-UTF mode, if character
  1521. tables for a French locale are in use, [\exc8-\excb] matches accented E
  1522. characters in both cases.
  1523. .P
  1524. A circumflex can conveniently be used with the upper case character types to
  1525. specify a more restricted set of characters than the matching lower case type.
  1526. For example, the class [^\eW_] matches any letter or digit, but not underscore,
  1527. whereas [\ew] includes underscore. A positive character class should be read as
  1528. "something OR something OR ..." and a negative class as "NOT something AND NOT
  1529. something AND NOT ...".
  1530. .P
  1531. The only metacharacters that are recognized in character classes are backslash,
  1532. hyphen (only where it can be interpreted as specifying a range), circumflex
  1533. (only at the start), opening square bracket (only when it can be interpreted as
  1534. introducing a POSIX class name, or for a special compatibility feature - see
  1535. the next two sections), and the terminating closing square bracket. However,
  1536. escaping other non-alphanumeric characters does no harm.
  1537. .
  1538. .
  1539. .SH "POSIX CHARACTER CLASSES"
  1540. .rs
  1541. .sp
  1542. Perl supports the POSIX notation for character classes. This uses names
  1543. enclosed by [: and :] within the enclosing square brackets. PCRE2 also supports
  1544. this notation. For example,
  1545. .sp
  1546. [01[:alpha:]%]
  1547. .sp
  1548. matches "0", "1", any alphabetic character, or "%". The supported class names
  1549. are:
  1550. .sp
  1551. alnum letters and digits
  1552. alpha letters
  1553. ascii character codes 0 - 127
  1554. blank space or tab only
  1555. cntrl control characters
  1556. digit decimal digits (same as \ed)
  1557. graph printing characters, excluding space
  1558. lower lower case letters
  1559. print printing characters, including space
  1560. punct printing characters, excluding letters and digits and space
  1561. space white space (the same as \es from PCRE2 8.34)
  1562. upper upper case letters
  1563. word "word" characters (same as \ew)
  1564. xdigit hexadecimal digits
  1565. .sp
  1566. The default "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13),
  1567. and space (32). If locale-specific matching is taking place, the list of space
  1568. characters may be different; there may be fewer or more of them. "Space" and
  1569. \es match the same set of characters.
  1570. .P
  1571. The name "word" is a Perl extension, and "blank" is a GNU extension from Perl
  1572. 5.8. Another Perl extension is negation, which is indicated by a ^ character
  1573. after the colon. For example,
  1574. .sp
  1575. [12[:^digit:]]
  1576. .sp
  1577. matches "1", "2", or any non-digit. PCRE2 (and Perl) also recognize the POSIX
  1578. syntax [.ch.] and [=ch=] where "ch" is a "collating element", but these are not
  1579. supported, and an error is given if they are encountered.
  1580. .P
  1581. By default, characters with values greater than 127 do not match any of the
  1582. POSIX character classes, although this may be different for characters in the
  1583. range 128-255 when locale-specific matching is happening. However, if the
  1584. PCRE2_UCP option is passed to \fBpcre2_compile()\fP, some of the classes are
  1585. changed so that Unicode character properties are used. This is achieved by
  1586. replacing certain POSIX classes with other sequences, as follows:
  1587. .sp
  1588. [:alnum:] becomes \ep{Xan}
  1589. [:alpha:] becomes \ep{L}
  1590. [:blank:] becomes \eh
  1591. [:cntrl:] becomes \ep{Cc}
  1592. [:digit:] becomes \ep{Nd}
  1593. [:lower:] becomes \ep{Ll}
  1594. [:space:] becomes \ep{Xps}
  1595. [:upper:] becomes \ep{Lu}
  1596. [:word:] becomes \ep{Xwd}
  1597. .sp
  1598. Negated versions, such as [:^alpha:] use \eP instead of \ep. Three other POSIX
  1599. classes are handled specially in UCP mode:
  1600. .TP 10
  1601. [:graph:]
  1602. This matches characters that have glyphs that mark the page when printed. In
  1603. Unicode property terms, it matches all characters with the L, M, N, P, S, or Cf
  1604. properties, except for:
  1605. .sp
  1606. U+061C Arabic Letter Mark
  1607. U+180E Mongolian Vowel Separator
  1608. U+2066 - U+2069 Various "isolate"s
  1609. .sp
  1610. .TP 10
  1611. [:print:]
  1612. This matches the same characters as [:graph:] plus space characters that are
  1613. not controls, that is, characters with the Zs property.
  1614. .TP 10
  1615. [:punct:]
  1616. This matches all characters that have the Unicode P (punctuation) property,
  1617. plus those characters with code points less than 256 that have the S (Symbol)
  1618. property.
  1619. .P
  1620. The other POSIX classes are unchanged, and match only characters with code
  1621. points less than 256.
  1622. .
  1623. .
  1624. .SH "COMPATIBILITY FEATURE FOR WORD BOUNDARIES"
  1625. .rs
  1626. .sp
  1627. In the POSIX.2 compliant library that was included in 4.4BSD Unix, the ugly
  1628. syntax [[:<:]] and [[:>:]] is used for matching "start of word" and "end of
  1629. word". PCRE2 treats these items as follows:
  1630. .sp
  1631. [[:<:]] is converted to \eb(?=\ew)
  1632. [[:>:]] is converted to \eb(?<=\ew)
  1633. .sp
  1634. Only these exact character sequences are recognized. A sequence such as
  1635. [a[:<:]b] provokes error for an unrecognized POSIX class name. This support is
  1636. not compatible with Perl. It is provided to help migrations from other
  1637. environments, and is best not used in any new patterns. Note that \eb matches
  1638. at the start and the end of a word (see
  1639. .\" HTML <a href="#smallassertions">
  1640. .\" </a>
  1641. "Simple assertions"
  1642. .\"
  1643. above), and in a Perl-style pattern the preceding or following character
  1644. normally shows which is wanted, without the need for the assertions that are
  1645. used above in order to give exactly the POSIX behaviour.
  1646. .
  1647. .
  1648. .SH "VERTICAL BAR"
  1649. .rs
  1650. .sp
  1651. Vertical bar characters are used to separate alternative patterns. For example,
  1652. the pattern
  1653. .sp
  1654. gilbert|sullivan
  1655. .sp
  1656. matches either "gilbert" or "sullivan". Any number of alternatives may appear,
  1657. and an empty alternative is permitted (matching the empty string). The matching
  1658. process tries each alternative in turn, from left to right, and the first one
  1659. that succeeds is used. If the alternatives are within a group
  1660. .\" HTML <a href="#group">
  1661. .\" </a>
  1662. (defined below),
  1663. .\"
  1664. "succeeds" means matching the rest of the main pattern as well as the
  1665. alternative in the group.
  1666. .
  1667. .
  1668. .\" HTML <a name="internaloptions"></a>
  1669. .SH "INTERNAL OPTION SETTING"
  1670. .rs
  1671. .sp
  1672. The settings of the PCRE2_CASELESS, PCRE2_MULTILINE, PCRE2_DOTALL,
  1673. PCRE2_EXTENDED, PCRE2_EXTENDED_MORE, and PCRE2_NO_AUTO_CAPTURE options can be
  1674. changed from within the pattern by a sequence of letters enclosed between "(?"
  1675. and ")". These options are Perl-compatible, and are described in detail in the
  1676. .\" HREF
  1677. \fBpcre2api\fP
  1678. .\"
  1679. documentation. The option letters are:
  1680. .sp
  1681. i for PCRE2_CASELESS
  1682. m for PCRE2_MULTILINE
  1683. n for PCRE2_NO_AUTO_CAPTURE
  1684. s for PCRE2_DOTALL
  1685. x for PCRE2_EXTENDED
  1686. xx for PCRE2_EXTENDED_MORE
  1687. .sp
  1688. For example, (?im) sets caseless, multiline matching. It is also possible to
  1689. unset these options by preceding the relevant letters with a hyphen, for
  1690. example (?-im). The two "extended" options are not independent; unsetting either
  1691. one cancels the effects of both of them.
  1692. .P
  1693. A combined setting and unsetting such as (?im-sx), which sets PCRE2_CASELESS
  1694. and PCRE2_MULTILINE while unsetting PCRE2_DOTALL and PCRE2_EXTENDED, is also
  1695. permitted. Only one hyphen may appear in the options string. If a letter
  1696. appears both before and after the hyphen, the option is unset. An empty options
  1697. setting "(?)" is allowed. Needless to say, it has no effect.
  1698. .P
  1699. If the first character following (? is a circumflex, it causes all of the above
  1700. options to be unset. Thus, (?^) is equivalent to (?-imnsx). Letters may follow
  1701. the circumflex to cause some options to be re-instated, but a hyphen may not
  1702. appear.
  1703. .P
  1704. The PCRE2-specific options PCRE2_DUPNAMES and PCRE2_UNGREEDY can be changed in
  1705. the same way as the Perl-compatible options by using the characters J and U
  1706. respectively. However, these are not unset by (?^).
  1707. .P
  1708. When one of these option changes occurs at top level (that is, not inside
  1709. group parentheses), the change applies to the remainder of the pattern
  1710. that follows. An option change within a group (see below for a description
  1711. of groups) affects only that part of the group that follows it, so
  1712. .sp
  1713. (a(?i)b)c
  1714. .sp
  1715. matches abc and aBc and no other strings (assuming PCRE2_CASELESS is not used).
  1716. By this means, options can be made to have different settings in different
  1717. parts of the pattern. Any changes made in one alternative do carry on
  1718. into subsequent branches within the same group. For example,
  1719. .sp
  1720. (a(?i)b|c)
  1721. .sp
  1722. matches "ab", "aB", "c", and "C", even though when matching "C" the first
  1723. branch is abandoned before the option setting. This is because the effects of
  1724. option settings happen at compile time. There would be some very weird
  1725. behaviour otherwise.
  1726. .P
  1727. As a convenient shorthand, if any option settings are required at the start of
  1728. a non-capturing group (see the next section), the option letters may
  1729. appear between the "?" and the ":". Thus the two patterns
  1730. .sp
  1731. (?i:saturday|sunday)
  1732. (?:(?i)saturday|sunday)
  1733. .sp
  1734. match exactly the same set of strings.
  1735. .P
  1736. \fBNote:\fP There are other PCRE2-specific options, applying to the whole
  1737. pattern, which can be set by the application when the compiling function is
  1738. called. In addition, the pattern can contain special leading sequences such as
  1739. (*CRLF) to override what the application has set or what has been defaulted.
  1740. Details are given in the section entitled
  1741. .\" HTML <a href="#newlineseq">
  1742. .\" </a>
  1743. "Newline sequences"
  1744. .\"
  1745. above. There are also the (*UTF) and (*UCP) leading sequences that can be used
  1746. to set UTF and Unicode property modes; they are equivalent to setting the
  1747. PCRE2_UTF and PCRE2_UCP options, respectively. However, the application can set
  1748. the PCRE2_NEVER_UTF and PCRE2_NEVER_UCP options, which lock out the use of the
  1749. (*UTF) and (*UCP) sequences.
  1750. .
  1751. .
  1752. .\" HTML <a name="group"></a>
  1753. .SH GROUPS
  1754. .rs
  1755. .sp
  1756. Groups are delimited by parentheses (round brackets), which can be nested.
  1757. Turning part of a pattern into a group does two things:
  1758. .sp
  1759. 1. It localizes a set of alternatives. For example, the pattern
  1760. .sp
  1761. cat(aract|erpillar|)
  1762. .sp
  1763. matches "cataract", "caterpillar", or "cat". Without the parentheses, it would
  1764. match "cataract", "erpillar" or an empty string.
  1765. .sp
  1766. 2. It creates a "capture group". This means that, when the whole pattern
  1767. matches, the portion of the subject string that matched the group is passed
  1768. back to the caller, separately from the portion that matched the whole pattern.
  1769. (This applies only to the traditional matching function; the DFA matching
  1770. function does not support capturing.)
  1771. .P
  1772. Opening parentheses are counted from left to right (starting from 1) to obtain
  1773. numbers for capture groups. For example, if the string "the red king" is
  1774. matched against the pattern
  1775. .sp
  1776. the ((red|white) (king|queen))
  1777. .sp
  1778. the captured substrings are "red king", "red", and "king", and are numbered 1,
  1779. 2, and 3, respectively.
  1780. .P
  1781. The fact that plain parentheses fulfil two functions is not always helpful.
  1782. There are often times when grouping is required without capturing. If an
  1783. opening parenthesis is followed by a question mark and a colon, the group
  1784. does not do any capturing, and is not counted when computing the number of any
  1785. subsequent capture groups. For example, if the string "the white queen"
  1786. is matched against the pattern
  1787. .sp
  1788. the ((?:red|white) (king|queen))
  1789. .sp
  1790. the captured substrings are "white queen" and "queen", and are numbered 1 and
  1791. 2. The maximum number of capture groups is 65535.
  1792. .P
  1793. As a convenient shorthand, if any option settings are required at the start of
  1794. a non-capturing group, the option letters may appear between the "?" and the
  1795. ":". Thus the two patterns
  1796. .sp
  1797. (?i:saturday|sunday)
  1798. (?:(?i)saturday|sunday)
  1799. .sp
  1800. match exactly the same set of strings. Because alternative branches are tried
  1801. from left to right, and options are not reset until the end of the group is
  1802. reached, an option setting in one branch does affect subsequent branches, so
  1803. the above patterns match "SUNDAY" as well as "Saturday".
  1804. .
  1805. .
  1806. .\" HTML <a name="dupgroupnumber"></a>
  1807. .SH "DUPLICATE GROUP NUMBERS"
  1808. .rs
  1809. .sp
  1810. Perl 5.10 introduced a feature whereby each alternative in a group uses the
  1811. same numbers for its capturing parentheses. Such a group starts with (?| and is
  1812. itself a non-capturing group. For example, consider this pattern:
  1813. .sp
  1814. (?|(Sat)ur|(Sun))day
  1815. .sp
  1816. Because the two alternatives are inside a (?| group, both sets of capturing
  1817. parentheses are numbered one. Thus, when the pattern matches, you can look
  1818. at captured substring number one, whichever alternative matched. This construct
  1819. is useful when you want to capture part, but not all, of one of a number of
  1820. alternatives. Inside a (?| group, parentheses are numbered as usual, but the
  1821. number is reset at the start of each branch. The numbers of any capturing
  1822. parentheses that follow the whole group start after the highest number used in
  1823. any branch. The following example is taken from the Perl documentation. The
  1824. numbers underneath show in which buffer the captured content will be stored.
  1825. .sp
  1826. # before ---------------branch-reset----------- after
  1827. / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
  1828. # 1 2 2 3 2 3 4
  1829. .sp
  1830. A backreference to a capture group uses the most recent value that is set for
  1831. the group. The following pattern matches "abcabc" or "defdef":
  1832. .sp
  1833. /(?|(abc)|(def))\e1/
  1834. .sp
  1835. In contrast, a subroutine call to a capture group always refers to the
  1836. first one in the pattern with the given number. The following pattern matches
  1837. "abcabc" or "defabc":
  1838. .sp
  1839. /(?|(abc)|(def))(?1)/
  1840. .sp
  1841. A relative reference such as (?-1) is no different: it is just a convenient way
  1842. of computing an absolute group number.
  1843. .P
  1844. If a
  1845. .\" HTML <a href="#conditions">
  1846. .\" </a>
  1847. condition test
  1848. .\"
  1849. for a group's having matched refers to a non-unique number, the test is
  1850. true if any group with that number has matched.
  1851. .P
  1852. An alternative approach to using this "branch reset" feature is to use
  1853. duplicate named groups, as described in the next section.
  1854. .
  1855. .
  1856. .SH "NAMED CAPTURE GROUPS"
  1857. .rs
  1858. .sp
  1859. Identifying capture groups by number is simple, but it can be very hard to keep
  1860. track of the numbers in complicated patterns. Furthermore, if an expression is
  1861. modified, the numbers may change. To help with this difficulty, PCRE2 supports
  1862. the naming of capture groups. This feature was not added to Perl until release
  1863. 5.10. Python had the feature earlier, and PCRE1 introduced it at release 4.0,
  1864. using the Python syntax. PCRE2 supports both the Perl and the Python syntax.
  1865. .P
  1866. In PCRE2, a capture group can be named in one of three ways: (?<name>...) or
  1867. (?'name'...) as in Perl, or (?P<name>...) as in Python. Names may be up to 32
  1868. code units long. When PCRE2_UTF is not set, they may contain only ASCII
  1869. alphanumeric characters and underscores, but must start with a non-digit. When
  1870. PCRE2_UTF is set, the syntax of group names is extended to allow any Unicode
  1871. letter or Unicode decimal digit. In other words, group names must match one of
  1872. these patterns:
  1873. .sp
  1874. ^[_A-Za-z][_A-Za-z0-9]*\ez when PCRE2_UTF is not set
  1875. ^[_\ep{L}][_\ep{L}\ep{Nd}]*\ez when PCRE2_UTF is set
  1876. .sp
  1877. References to capture groups from other parts of the pattern, such as
  1878. .\" HTML <a href="#backreferences">
  1879. .\" </a>
  1880. backreferences,
  1881. .\"
  1882. .\" HTML <a href="#recursion">
  1883. .\" </a>
  1884. recursion,
  1885. .\"
  1886. and
  1887. .\" HTML <a href="#conditions">
  1888. .\" </a>
  1889. conditions,
  1890. .\"
  1891. can all be made by name as well as by number.
  1892. .P
  1893. Named capture groups are allocated numbers as well as names, exactly as
  1894. if the names were not present. In both PCRE2 and Perl, capture groups
  1895. are primarily identified by numbers; any names are just aliases for these
  1896. numbers. The PCRE2 API provides function calls for extracting the complete
  1897. name-to-number translation table from a compiled pattern, as well as
  1898. convenience functions for extracting captured substrings by name.
  1899. .P
  1900. \fBWarning:\fP When more than one capture group has the same number, as
  1901. described in the previous section, a name given to one of them applies to all
  1902. of them. Perl allows identically numbered groups to have different names.
  1903. Consider this pattern, where there are two capture groups, both numbered 1:
  1904. .sp
  1905. (?|(?<AA>aa)|(?<BB>bb))
  1906. .sp
  1907. Perl allows this, with both names AA and BB as aliases of group 1. Thus, after
  1908. a successful match, both names yield the same value (either "aa" or "bb").
  1909. .P
  1910. In an attempt to reduce confusion, PCRE2 does not allow the same group number
  1911. to be associated with more than one name. The example above provokes a
  1912. compile-time error. However, there is still scope for confusion. Consider this
  1913. pattern:
  1914. .sp
  1915. (?|(?<AA>aa)|(bb))
  1916. .sp
  1917. Although the second group number 1 is not explicitly named, the name AA is
  1918. still an alias for any group 1. Whether the pattern matches "aa" or "bb", a
  1919. reference by name to group AA yields the matched string.
  1920. .P
  1921. By default, a name must be unique within a pattern, except that duplicate names
  1922. are permitted for groups with the same number, for example:
  1923. .sp
  1924. (?|(?<AA>aa)|(?<AA>bb))
  1925. .sp
  1926. The duplicate name constraint can be disabled by setting the PCRE2_DUPNAMES
  1927. option at compile time, or by the use of (?J) within the pattern, as described
  1928. in the section entitled
  1929. .\" HTML <a href="#internaloptions">
  1930. .\" </a>
  1931. "Internal Option Setting"
  1932. .\"
  1933. above.
  1934. .P
  1935. Duplicate names can be useful for patterns where only one instance of the named
  1936. capture group can match. Suppose you want to match the name of a weekday,
  1937. either as a 3-letter abbreviation or as the full name, and in both cases you
  1938. want to extract the abbreviation. This pattern (ignoring the line breaks) does
  1939. the job:
  1940. .sp
  1941. (?J)
  1942. (?<DN>Mon|Fri|Sun)(?:day)?|
  1943. (?<DN>Tue)(?:sday)?|
  1944. (?<DN>Wed)(?:nesday)?|
  1945. (?<DN>Thu)(?:rsday)?|
  1946. (?<DN>Sat)(?:urday)?
  1947. .sp
  1948. There are five capture groups, but only one is ever set after a match. The
  1949. convenience functions for extracting the data by name returns the substring for
  1950. the first (and in this example, the only) group of that name that matched. This
  1951. saves searching to find which numbered group it was. (An alternative way of
  1952. solving this problem is to use a "branch reset" group, as described in the
  1953. previous section.)
  1954. .P
  1955. If you make a backreference to a non-unique named group from elsewhere in the
  1956. pattern, the groups to which the name refers are checked in the order in which
  1957. they appear in the overall pattern. The first one that is set is used for the
  1958. reference. For example, this pattern matches both "foofoo" and "barbar" but not
  1959. "foobar" or "barfoo":
  1960. .sp
  1961. (?J)(?:(?<n>foo)|(?<n>bar))\ek<n>
  1962. .sp
  1963. .P
  1964. If you make a subroutine call to a non-unique named group, the one that
  1965. corresponds to the first occurrence of the name is used. In the absence of
  1966. duplicate numbers this is the one with the lowest number.
  1967. .P
  1968. If you use a named reference in a condition
  1969. test (see the
  1970. .\"
  1971. .\" HTML <a href="#conditions">
  1972. .\" </a>
  1973. section about conditions
  1974. .\"
  1975. below), either to check whether a capture group has matched, or to check for
  1976. recursion, all groups with the same name are tested. If the condition is true
  1977. for any one of them, the overall condition is true. This is the same behaviour
  1978. as testing by number. For further details of the interfaces for handling named
  1979. capture groups, see the
  1980. .\" HREF
  1981. \fBpcre2api\fP
  1982. .\"
  1983. documentation.
  1984. .
  1985. .
  1986. .SH REPETITION
  1987. .rs
  1988. .sp
  1989. Repetition is specified by quantifiers, which can follow any of the following
  1990. items:
  1991. .sp
  1992. a literal data character
  1993. the dot metacharacter
  1994. the \eC escape sequence
  1995. the \eR escape sequence
  1996. the \eX escape sequence
  1997. an escape such as \ed or \epL that matches a single character
  1998. a character class
  1999. a backreference
  2000. a parenthesized group (including lookaround assertions)
  2001. a subroutine call (recursive or otherwise)
  2002. .sp
  2003. The general repetition quantifier specifies a minimum and maximum number of
  2004. permitted matches, by giving the two numbers in curly brackets (braces),
  2005. separated by a comma. The numbers must be less than 65536, and the first must
  2006. be less than or equal to the second. For example,
  2007. .sp
  2008. z{2,4}
  2009. .sp
  2010. matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special
  2011. character. If the second number is omitted, but the comma is present, there is
  2012. no upper limit; if the second number and the comma are both omitted, the
  2013. quantifier specifies an exact number of required matches. Thus
  2014. .sp
  2015. [aeiou]{3,}
  2016. .sp
  2017. matches at least 3 successive vowels, but may match many more, whereas
  2018. .sp
  2019. \ed{8}
  2020. .sp
  2021. matches exactly 8 digits. An opening curly bracket that appears in a position
  2022. where a quantifier is not allowed, or one that does not match the syntax of a
  2023. quantifier, is taken as a literal character. For example, {,6} is not a
  2024. quantifier, but a literal string of four characters.
  2025. .P
  2026. In UTF modes, quantifiers apply to characters rather than to individual code
  2027. units. Thus, for example, \ex{100}{2} matches two characters, each of
  2028. which is represented by a two-byte sequence in a UTF-8 string. Similarly,
  2029. \eX{3} matches three Unicode extended grapheme clusters, each of which may be
  2030. several code units long (and they may be of different lengths).
  2031. .P
  2032. The quantifier {0} is permitted, causing the expression to behave as if the
  2033. previous item and the quantifier were not present. This may be useful for
  2034. capture groups that are referenced as
  2035. .\" HTML <a href="#groupsassubroutines">
  2036. .\" </a>
  2037. subroutines
  2038. .\"
  2039. from elsewhere in the pattern (but see also the section entitled
  2040. .\" HTML <a href="#subdefine">
  2041. .\" </a>
  2042. "Defining capture groups for use by reference only"
  2043. .\"
  2044. below). Except for parenthesized groups, items that have a {0} quantifier are
  2045. omitted from the compiled pattern.
  2046. .P
  2047. For convenience, the three most common quantifiers have single-character
  2048. abbreviations:
  2049. .sp
  2050. * is equivalent to {0,}
  2051. + is equivalent to {1,}
  2052. ? is equivalent to {0,1}
  2053. .sp
  2054. It is possible to construct infinite loops by following a group that can match
  2055. no characters with a quantifier that has no upper limit, for example:
  2056. .sp
  2057. (a?)*
  2058. .sp
  2059. Earlier versions of Perl and PCRE1 used to give an error at compile time for
  2060. such patterns. However, because there are cases where this can be useful, such
  2061. patterns are now accepted, but whenever an iteration of such a group matches no
  2062. characters, matching moves on to the next item in the pattern instead of
  2063. repeatedly matching an empty string. This does not prevent backtracking into
  2064. any of the iterations if a subsequent item fails to match.
  2065. .P
  2066. By default, quantifiers are "greedy", that is, they match as much as possible
  2067. (up to the maximum number of permitted times), without causing the rest of the
  2068. pattern to fail. The classic example of where this gives problems is in trying
  2069. to match comments in C programs. These appear between /* and */ and within the
  2070. comment, individual * and / characters may appear. An attempt to match C
  2071. comments by applying the pattern
  2072. .sp
  2073. /\e*.*\e*/
  2074. .sp
  2075. to the string
  2076. .sp
  2077. /* first comment */ not comment /* second comment */
  2078. .sp
  2079. fails, because it matches the entire string owing to the greediness of the .*
  2080. item. However, if a quantifier is followed by a question mark, it ceases to be
  2081. greedy, and instead matches the minimum number of times possible, so the
  2082. pattern
  2083. .sp
  2084. /\e*.*?\e*/
  2085. .sp
  2086. does the right thing with the C comments. The meaning of the various
  2087. quantifiers is not otherwise changed, just the preferred number of matches.
  2088. Do not confuse this use of question mark with its use as a quantifier in its
  2089. own right. Because it has two uses, it can sometimes appear doubled, as in
  2090. .sp
  2091. \ed??\ed
  2092. .sp
  2093. which matches one digit by preference, but can match two if that is the only
  2094. way the rest of the pattern matches.
  2095. .P
  2096. If the PCRE2_UNGREEDY option is set (an option that is not available in Perl),
  2097. the quantifiers are not greedy by default, but individual ones can be made
  2098. greedy by following them with a question mark. In other words, it inverts the
  2099. default behaviour.
  2100. .P
  2101. When a parenthesized group is quantified with a minimum repeat count that
  2102. is greater than 1 or with a limited maximum, more memory is required for the
  2103. compiled pattern, in proportion to the size of the minimum or maximum.
  2104. .P
  2105. If a pattern starts with .* or .{0,} and the PCRE2_DOTALL option (equivalent
  2106. to Perl's /s) is set, thus allowing the dot to match newlines, the pattern is
  2107. implicitly anchored, because whatever follows will be tried against every
  2108. character position in the subject string, so there is no point in retrying the
  2109. overall match at any position after the first. PCRE2 normally treats such a
  2110. pattern as though it were preceded by \eA.
  2111. .P
  2112. In cases where it is known that the subject string contains no newlines, it is
  2113. worth setting PCRE2_DOTALL in order to obtain this optimization, or
  2114. alternatively, using ^ to indicate anchoring explicitly.
  2115. .P
  2116. However, there are some cases where the optimization cannot be used. When .*
  2117. is inside capturing parentheses that are the subject of a backreference
  2118. elsewhere in the pattern, a match at the start may fail where a later one
  2119. succeeds. Consider, for example:
  2120. .sp
  2121. (.*)abc\e1
  2122. .sp
  2123. If the subject is "xyz123abc123" the match point is the fourth character. For
  2124. this reason, such a pattern is not implicitly anchored.
  2125. .P
  2126. Another case where implicit anchoring is not applied is when the leading .* is
  2127. inside an atomic group. Once again, a match at the start may fail where a later
  2128. one succeeds. Consider this pattern:
  2129. .sp
  2130. (?>.*?a)b
  2131. .sp
  2132. It matches "ab" in the subject "aab". The use of the backtracking control verbs
  2133. (*PRUNE) and (*SKIP) also disable this optimization, and there is an option,
  2134. PCRE2_NO_DOTSTAR_ANCHOR, to do so explicitly.
  2135. .P
  2136. When a capture group is repeated, the value captured is the substring that
  2137. matched the final iteration. For example, after
  2138. .sp
  2139. (tweedle[dume]{3}\es*)+
  2140. .sp
  2141. has matched "tweedledum tweedledee" the value of the captured substring is
  2142. "tweedledee". However, if there are nested capture groups, the corresponding
  2143. captured values may have been set in previous iterations. For example, after
  2144. .sp
  2145. (a|(b))+
  2146. .sp
  2147. matches "aba" the value of the second captured substring is "b".
  2148. .
  2149. .
  2150. .\" HTML <a name="atomicgroup"></a>
  2151. .SH "ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS"
  2152. .rs
  2153. .sp
  2154. With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
  2155. repetition, failure of what follows normally causes the repeated item to be
  2156. re-evaluated to see if a different number of repeats allows the rest of the
  2157. pattern to match. Sometimes it is useful to prevent this, either to change the
  2158. nature of the match, or to cause it fail earlier than it otherwise might, when
  2159. the author of the pattern knows there is no point in carrying on.
  2160. .P
  2161. Consider, for example, the pattern \ed+foo when applied to the subject line
  2162. .sp
  2163. 123456bar
  2164. .sp
  2165. After matching all 6 digits and then failing to match "foo", the normal
  2166. action of the matcher is to try again with only 5 digits matching the \ed+
  2167. item, and then with 4, and so on, before ultimately failing. "Atomic grouping"
  2168. (a term taken from Jeffrey Friedl's book) provides the means for specifying
  2169. that once a group has matched, it is not to be re-evaluated in this way.
  2170. .P
  2171. If we use atomic grouping for the previous example, the matcher gives up
  2172. immediately on failing to match "foo" the first time. The notation is a kind of
  2173. special parenthesis, starting with (?> as in this example:
  2174. .sp
  2175. (?>\ed+)foo
  2176. .sp
  2177. Perl 5.28 introduced an experimental alphabetic form starting with (* which may
  2178. be easier to remember:
  2179. .sp
  2180. (*atomic:\ed+)foo
  2181. .sp
  2182. This kind of parenthesized group "locks up" the part of the pattern it
  2183. contains once it has matched, and a failure further into the pattern is
  2184. prevented from backtracking into it. Backtracking past it to previous items,
  2185. however, works as normal.
  2186. .P
  2187. An alternative description is that a group of this type matches exactly the
  2188. string of characters that an identical standalone pattern would match, if
  2189. anchored at the current point in the subject string.
  2190. .P
  2191. Atomic groups are not capture groups. Simple cases such as the above example
  2192. can be thought of as a maximizing repeat that must swallow everything it can.
  2193. So, while both \ed+ and \ed+? are prepared to adjust the number of digits they
  2194. match in order to make the rest of the pattern match, (?>\ed+) can only match
  2195. an entire sequence of digits.
  2196. .P
  2197. Atomic groups in general can of course contain arbitrarily complicated
  2198. expressions, and can be nested. However, when the contents of an atomic
  2199. group is just a single repeated item, as in the example above, a simpler
  2200. notation, called a "possessive quantifier" can be used. This consists of an
  2201. additional + character following a quantifier. Using this notation, the
  2202. previous example can be rewritten as
  2203. .sp
  2204. \ed++foo
  2205. .sp
  2206. Note that a possessive quantifier can be used with an entire group, for
  2207. example:
  2208. .sp
  2209. (abc|xyz){2,3}+
  2210. .sp
  2211. Possessive quantifiers are always greedy; the setting of the PCRE2_UNGREEDY
  2212. option is ignored. They are a convenient notation for the simpler forms of
  2213. atomic group. However, there is no difference in the meaning of a possessive
  2214. quantifier and the equivalent atomic group, though there may be a performance
  2215. difference; possessive quantifiers should be slightly faster.
  2216. .P
  2217. The possessive quantifier syntax is an extension to the Perl 5.8 syntax.
  2218. Jeffrey Friedl originated the idea (and the name) in the first edition of his
  2219. book. Mike McCloskey liked it, so implemented it when he built Sun's Java
  2220. package, and PCRE1 copied it from there. It found its way into Perl at release
  2221. 5.10.
  2222. .P
  2223. PCRE2 has an optimization that automatically "possessifies" certain simple
  2224. pattern constructs. For example, the sequence A+B is treated as A++B because
  2225. there is no point in backtracking into a sequence of A's when B must follow.
  2226. This feature can be disabled by the PCRE2_NO_AUTOPOSSESS option, or starting
  2227. the pattern with (*NO_AUTO_POSSESS).
  2228. .P
  2229. When a pattern contains an unlimited repeat inside a group that can itself be
  2230. repeated an unlimited number of times, the use of an atomic group is the only
  2231. way to avoid some failing matches taking a very long time indeed. The pattern
  2232. .sp
  2233. (\eD+|<\ed+>)*[!?]
  2234. .sp
  2235. matches an unlimited number of substrings that either consist of non-digits, or
  2236. digits enclosed in <>, followed by either ! or ?. When it matches, it runs
  2237. quickly. However, if it is applied to
  2238. .sp
  2239. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  2240. .sp
  2241. it takes a long time before reporting failure. This is because the string can
  2242. be divided between the internal \eD+ repeat and the external * repeat in a
  2243. large number of ways, and all have to be tried. (The example uses [!?] rather
  2244. than a single character at the end, because both PCRE2 and Perl have an
  2245. optimization that allows for fast failure when a single character is used. They
  2246. remember the last single character that is required for a match, and fail early
  2247. if it is not present in the string.) If the pattern is changed so that it uses
  2248. an atomic group, like this:
  2249. .sp
  2250. ((?>\eD+)|<\ed+>)*[!?]
  2251. .sp
  2252. sequences of non-digits cannot be broken, and failure happens quickly.
  2253. .
  2254. .
  2255. .\" HTML <a name="backreferences"></a>
  2256. .SH "BACKREFERENCES"
  2257. .rs
  2258. .sp
  2259. Outside a character class, a backslash followed by a digit greater than 0 (and
  2260. possibly further digits) is a backreference to a capture group earlier (that
  2261. is, to its left) in the pattern, provided there have been that many previous
  2262. capture groups.
  2263. .P
  2264. However, if the decimal number following the backslash is less than 8, it is
  2265. always taken as a backreference, and causes an error only if there are not that
  2266. many capture groups in the entire pattern. In other words, the group that is
  2267. referenced need not be to the left of the reference for numbers less than 8. A
  2268. "forward backreference" of this type can make sense when a repetition is
  2269. involved and the group to the right has participated in an earlier iteration.
  2270. .P
  2271. It is not possible to have a numerical "forward backreference" to a group whose
  2272. number is 8 or more using this syntax because a sequence such as \e50 is
  2273. interpreted as a character defined in octal. See the subsection entitled
  2274. "Non-printing characters"
  2275. .\" HTML <a href="#digitsafterbackslash">
  2276. .\" </a>
  2277. above
  2278. .\"
  2279. for further details of the handling of digits following a backslash. Other
  2280. forms of backreferencing do not suffer from this restriction. In particular,
  2281. there is no problem when named capture groups are used (see below).
  2282. .P
  2283. Another way of avoiding the ambiguity inherent in the use of digits following a
  2284. backslash is to use the \eg escape sequence. This escape must be followed by a
  2285. signed or unsigned number, optionally enclosed in braces. These examples are
  2286. all identical:
  2287. .sp
  2288. (ring), \e1
  2289. (ring), \eg1
  2290. (ring), \eg{1}
  2291. .sp
  2292. An unsigned number specifies an absolute reference without the ambiguity that
  2293. is present in the older syntax. It is also useful when literal digits follow
  2294. the reference. A signed number is a relative reference. Consider this example:
  2295. .sp
  2296. (abc(def)ghi)\eg{-1}
  2297. .sp
  2298. The sequence \eg{-1} is a reference to the most recently started capture group
  2299. before \eg, that is, is it equivalent to \e2 in this example. Similarly,
  2300. \eg{-2} would be equivalent to \e1. The use of relative references can be
  2301. helpful in long patterns, and also in patterns that are created by joining
  2302. together fragments that contain references within themselves.
  2303. .P
  2304. The sequence \eg{+1} is a reference to the next capture group. This kind of
  2305. forward reference can be useful in patterns that repeat. Perl does not support
  2306. the use of + in this way.
  2307. .P
  2308. A backreference matches whatever actually most recently matched the capture
  2309. group in the current subject string, rather than anything at all that matches
  2310. the group (see
  2311. .\" HTML <a href="#groupsassubroutines">
  2312. .\" </a>
  2313. "Groups as subroutines"
  2314. .\"
  2315. below for a way of doing that). So the pattern
  2316. .sp
  2317. (sens|respons)e and \e1ibility
  2318. .sp
  2319. matches "sense and sensibility" and "response and responsibility", but not
  2320. "sense and responsibility". If caseful matching is in force at the time of the
  2321. backreference, the case of letters is relevant. For example,
  2322. .sp
  2323. ((?i)rah)\es+\e1
  2324. .sp
  2325. matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original
  2326. capture group is matched caselessly.
  2327. .P
  2328. There are several different ways of writing backreferences to named capture
  2329. groups. The .NET syntax \ek{name} and the Perl syntax \ek<name> or \ek'name'
  2330. are supported, as is the Python syntax (?P=name). Perl 5.10's unified
  2331. backreference syntax, in which \eg can be used for both numeric and named
  2332. references, is also supported. We could rewrite the above example in any of the
  2333. following ways:
  2334. .sp
  2335. (?<p1>(?i)rah)\es+\ek<p1>
  2336. (?'p1'(?i)rah)\es+\ek{p1}
  2337. (?P<p1>(?i)rah)\es+(?P=p1)
  2338. (?<p1>(?i)rah)\es+\eg{p1}
  2339. .sp
  2340. A capture group that is referenced by name may appear in the pattern before or
  2341. after the reference.
  2342. .P
  2343. There may be more than one backreference to the same group. If a group has not
  2344. actually been used in a particular match, backreferences to it always fail by
  2345. default. For example, the pattern
  2346. .sp
  2347. (a|(bc))\e2
  2348. .sp
  2349. always fails if it starts to match "a" rather than "bc". However, if the
  2350. PCRE2_MATCH_UNSET_BACKREF option is set at compile time, a backreference to an
  2351. unset value matches an empty string.
  2352. .P
  2353. Because there may be many capture groups in a pattern, all digits following a
  2354. backslash are taken as part of a potential backreference number. If the pattern
  2355. continues with a digit character, some delimiter must be used to terminate the
  2356. backreference. If the PCRE2_EXTENDED or PCRE2_EXTENDED_MORE option is set, this
  2357. can be white space. Otherwise, the \eg{} syntax or an empty comment (see
  2358. .\" HTML <a href="#comments">
  2359. .\" </a>
  2360. "Comments"
  2361. .\"
  2362. below) can be used.
  2363. .
  2364. .
  2365. .SS "Recursive backreferences"
  2366. .rs
  2367. .sp
  2368. A backreference that occurs inside the group to which it refers fails when the
  2369. group is first used, so, for example, (a\e1) never matches. However, such
  2370. references can be useful inside repeated groups. For example, the pattern
  2371. .sp
  2372. (a|b\e1)+
  2373. .sp
  2374. matches any number of "a"s and also "aba", "ababbaa" etc. At each iteration of
  2375. the group, the backreference matches the character string corresponding to the
  2376. previous iteration. In order for this to work, the pattern must be such that
  2377. the first iteration does not need to match the backreference. This can be done
  2378. using alternation, as in the example above, or by a quantifier with a minimum
  2379. of zero.
  2380. .P
  2381. For versions of PCRE2 less than 10.25, backreferences of this type used to
  2382. cause the group that they reference to be treated as an
  2383. .\" HTML <a href="#atomicgroup">
  2384. .\" </a>
  2385. atomic group.
  2386. .\"
  2387. This restriction no longer applies, and backtracking into such groups can occur
  2388. as normal.
  2389. .
  2390. .
  2391. .\" HTML <a name="bigassertions"></a>
  2392. .SH ASSERTIONS
  2393. .rs
  2394. .sp
  2395. An assertion is a test on the characters following or preceding the current
  2396. matching point that does not consume any characters. The simple assertions
  2397. coded as \eb, \eB, \eA, \eG, \eZ, \ez, ^ and $ are described
  2398. .\" HTML <a href="#smallassertions">
  2399. .\" </a>
  2400. above.
  2401. .\"
  2402. .P
  2403. More complicated assertions are coded as parenthesized groups. There are two
  2404. kinds: those that look ahead of the current position in the subject string, and
  2405. those that look behind it, and in each case an assertion may be positive (must
  2406. match for the assertion to be true) or negative (must not match for the
  2407. assertion to be true). An assertion group is matched in the normal way,
  2408. and if it is true, matching continues after it, but with the matching position
  2409. in the subject string reset to what it was before the assertion was processed.
  2410. .P
  2411. The Perl-compatible lookaround assertions are atomic. If an assertion is true,
  2412. but there is a subsequent matching failure, there is no backtracking into the
  2413. assertion. However, there are some cases where non-atomic assertions can be
  2414. useful. PCRE2 has some support for these, described in the section entitled
  2415. .\" HTML <a href="#nonatomicassertions">
  2416. .\" </a>
  2417. "Non-atomic assertions"
  2418. .\"
  2419. below, but they are not Perl-compatible.
  2420. .P
  2421. A lookaround assertion may appear as the condition in a
  2422. .\" HTML <a href="#conditions">
  2423. .\" </a>
  2424. conditional group
  2425. .\"
  2426. (see below). In this case, the result of matching the assertion determines
  2427. which branch of the condition is followed.
  2428. .P
  2429. Assertion groups are not capture groups. If an assertion contains capture
  2430. groups within it, these are counted for the purposes of numbering the capture
  2431. groups in the whole pattern. Within each branch of an assertion, locally
  2432. captured substrings may be referenced in the usual way. For example, a sequence
  2433. such as (.)\eg{-1} can be used to check that two adjacent characters are the
  2434. same.
  2435. .P
  2436. When a branch within an assertion fails to match, any substrings that were
  2437. captured are discarded (as happens with any pattern branch that fails to
  2438. match). A negative assertion is true only when all its branches fail to match;
  2439. this means that no captured substrings are ever retained after a successful
  2440. negative assertion. When an assertion contains a matching branch, what happens
  2441. depends on the type of assertion.
  2442. .P
  2443. For a positive assertion, internally captured substrings in the successful
  2444. branch are retained, and matching continues with the next pattern item after
  2445. the assertion. For a negative assertion, a matching branch means that the
  2446. assertion is not true. If such an assertion is being used as a condition in a
  2447. .\" HTML <a href="#conditions">
  2448. .\" </a>
  2449. conditional group
  2450. .\"
  2451. (see below), captured substrings are retained, because matching continues with
  2452. the "no" branch of the condition. For other failing negative assertions,
  2453. control passes to the previous backtracking point, thus discarding any captured
  2454. strings within the assertion.
  2455. .P
  2456. Most assertion groups may be repeated; though it makes no sense to assert the
  2457. same thing several times, the side effect of capturing in positive assertions
  2458. may occasionally be useful. However, an assertion that forms the condition for
  2459. a conditional group may not be quantified. PCRE2 used to restrict the
  2460. repetition of assertions, but from release 10.35 the only restriction is that
  2461. an unlimited maximum repetition is changed to be one more than the minimum. For
  2462. example, {3,} is treated as {3,4}.
  2463. .
  2464. .
  2465. .SS "Alphabetic assertion names"
  2466. .rs
  2467. .sp
  2468. Traditionally, symbolic sequences such as (?= and (?<= have been used to
  2469. specify lookaround assertions. Perl 5.28 introduced some experimental
  2470. alphabetic alternatives which might be easier to remember. They all start with
  2471. (* instead of (? and must be written using lower case letters. PCRE2 supports
  2472. the following synonyms:
  2473. .sp
  2474. (*positive_lookahead: or (*pla: is the same as (?=
  2475. (*negative_lookahead: or (*nla: is the same as (?!
  2476. (*positive_lookbehind: or (*plb: is the same as (?<=
  2477. (*negative_lookbehind: or (*nlb: is the same as (?<!
  2478. .sp
  2479. For example, (*pla:foo) is the same assertion as (?=foo). In the following
  2480. sections, the various assertions are described using the original symbolic
  2481. forms.
  2482. .
  2483. .
  2484. .SS "Lookahead assertions"
  2485. .rs
  2486. .sp
  2487. Lookahead assertions start with (?= for positive assertions and (?! for
  2488. negative assertions. For example,
  2489. .sp
  2490. \ew+(?=;)
  2491. .sp
  2492. matches a word followed by a semicolon, but does not include the semicolon in
  2493. the match, and
  2494. .sp
  2495. foo(?!bar)
  2496. .sp
  2497. matches any occurrence of "foo" that is not followed by "bar". Note that the
  2498. apparently similar pattern
  2499. .sp
  2500. (?!foo)bar
  2501. .sp
  2502. does not find an occurrence of "bar" that is preceded by something other than
  2503. "foo"; it finds any occurrence of "bar" whatsoever, because the assertion
  2504. (?!foo) is always true when the next three characters are "bar". A
  2505. lookbehind assertion is needed to achieve the other effect.
  2506. .P
  2507. If you want to force a matching failure at some point in a pattern, the most
  2508. convenient way to do it is with (?!) because an empty string always matches, so
  2509. an assertion that requires there not to be an empty string must always fail.
  2510. The backtracking control verb (*FAIL) or (*F) is a synonym for (?!).
  2511. .
  2512. .
  2513. .\" HTML <a name="lookbehind"></a>
  2514. .SS "Lookbehind assertions"
  2515. .rs
  2516. .sp
  2517. Lookbehind assertions start with (?<= for positive assertions and (?<! for
  2518. negative assertions. For example,
  2519. .sp
  2520. (?<!foo)bar
  2521. .sp
  2522. does find an occurrence of "bar" that is not preceded by "foo". The contents of
  2523. a lookbehind assertion are restricted such that all the strings it matches must
  2524. have a fixed length. However, if there are several top-level alternatives, they
  2525. do not all have to have the same fixed length. Thus
  2526. .sp
  2527. (?<=bullock|donkey)
  2528. .sp
  2529. is permitted, but
  2530. .sp
  2531. (?<!dogs?|cats?)
  2532. .sp
  2533. causes an error at compile time. Branches that match different length strings
  2534. are permitted only at the top level of a lookbehind assertion. This is an
  2535. extension compared with Perl, which requires all branches to match the same
  2536. length of string. An assertion such as
  2537. .sp
  2538. (?<=ab(c|de))
  2539. .sp
  2540. is not permitted, because its single top-level branch can match two different
  2541. lengths, but it is acceptable to PCRE2 if rewritten to use two top-level
  2542. branches:
  2543. .sp
  2544. (?<=abc|abde)
  2545. .sp
  2546. In some cases, the escape sequence \eK
  2547. .\" HTML <a href="#resetmatchstart">
  2548. .\" </a>
  2549. (see above)
  2550. .\"
  2551. can be used instead of a lookbehind assertion to get round the fixed-length
  2552. restriction.
  2553. .P
  2554. The implementation of lookbehind assertions is, for each alternative, to
  2555. temporarily move the current position back by the fixed length and then try to
  2556. match. If there are insufficient characters before the current position, the
  2557. assertion fails.
  2558. .P
  2559. In UTF-8 and UTF-16 modes, PCRE2 does not allow the \eC escape (which matches a
  2560. single code unit even in a UTF mode) to appear in lookbehind assertions,
  2561. because it makes it impossible to calculate the length of the lookbehind. The
  2562. \eX and \eR escapes, which can match different numbers of code units, are never
  2563. permitted in lookbehinds.
  2564. .P
  2565. .\" HTML <a href="#groupsassubroutines">
  2566. .\" </a>
  2567. "Subroutine"
  2568. .\"
  2569. calls (see below) such as (?2) or (?&X) are permitted in lookbehinds, as long
  2570. as the called capture group matches a fixed-length string. However,
  2571. .\" HTML <a href="#recursion">
  2572. .\" </a>
  2573. recursion,
  2574. .\"
  2575. that is, a "subroutine" call into a group that is already active,
  2576. is not supported.
  2577. .P
  2578. Perl does not support backreferences in lookbehinds. PCRE2 does support them,
  2579. but only if certain conditions are met. The PCRE2_MATCH_UNSET_BACKREF option
  2580. must not be set, there must be no use of (?| in the pattern (it creates
  2581. duplicate group numbers), and if the backreference is by name, the name
  2582. must be unique. Of course, the referenced group must itself match a fixed
  2583. length substring. The following pattern matches words containing at least two
  2584. characters that begin and end with the same character:
  2585. .sp
  2586. \eb(\ew)\ew++(?<=\e1)
  2587. .P
  2588. Possessive quantifiers can be used in conjunction with lookbehind assertions to
  2589. specify efficient matching of fixed-length strings at the end of subject
  2590. strings. Consider a simple pattern such as
  2591. .sp
  2592. abcd$
  2593. .sp
  2594. when applied to a long string that does not match. Because matching proceeds
  2595. from left to right, PCRE2 will look for each "a" in the subject and then see if
  2596. what follows matches the rest of the pattern. If the pattern is specified as
  2597. .sp
  2598. ^.*abcd$
  2599. .sp
  2600. the initial .* matches the entire string at first, but when this fails (because
  2601. there is no following "a"), it backtracks to match all but the last character,
  2602. then all but the last two characters, and so on. Once again the search for "a"
  2603. covers the entire string, from right to left, so we are no better off. However,
  2604. if the pattern is written as
  2605. .sp
  2606. ^.*+(?<=abcd)
  2607. .sp
  2608. there can be no backtracking for the .*+ item because of the possessive
  2609. quantifier; it can match only the entire string. The subsequent lookbehind
  2610. assertion does a single test on the last four characters. If it fails, the
  2611. match fails immediately. For long strings, this approach makes a significant
  2612. difference to the processing time.
  2613. .
  2614. .
  2615. .SS "Using multiple assertions"
  2616. .rs
  2617. .sp
  2618. Several assertions (of any sort) may occur in succession. For example,
  2619. .sp
  2620. (?<=\ed{3})(?<!999)foo
  2621. .sp
  2622. matches "foo" preceded by three digits that are not "999". Notice that each of
  2623. the assertions is applied independently at the same point in the subject
  2624. string. First there is a check that the previous three characters are all
  2625. digits, and then there is a check that the same three characters are not "999".
  2626. This pattern does \fInot\fP match "foo" preceded by six characters, the first
  2627. of which are digits and the last three of which are not "999". For example, it
  2628. doesn't match "123abcfoo". A pattern to do that is
  2629. .sp
  2630. (?<=\ed{3}...)(?<!999)foo
  2631. .sp
  2632. This time the first assertion looks at the preceding six characters, checking
  2633. that the first three are digits, and then the second assertion checks that the
  2634. preceding three characters are not "999".
  2635. .P
  2636. Assertions can be nested in any combination. For example,
  2637. .sp
  2638. (?<=(?<!foo)bar)baz
  2639. .sp
  2640. matches an occurrence of "baz" that is preceded by "bar" which in turn is not
  2641. preceded by "foo", while
  2642. .sp
  2643. (?<=\ed{3}(?!999)...)foo
  2644. .sp
  2645. is another pattern that matches "foo" preceded by three digits and any three
  2646. characters that are not "999".
  2647. .
  2648. .
  2649. .\" HTML <a name="nonatomicassertions"></a>
  2650. .SH "NON-ATOMIC ASSERTIONS"
  2651. .rs
  2652. .sp
  2653. The traditional Perl-compatible lookaround assertions are atomic. That is, if
  2654. an assertion is true, but there is a subsequent matching failure, there is no
  2655. backtracking into the assertion. However, there are some cases where non-atomic
  2656. positive assertions can be useful. PCRE2 provides these using the following
  2657. syntax:
  2658. .sp
  2659. (*non_atomic_positive_lookahead: or (*napla: or (?*
  2660. (*non_atomic_positive_lookbehind: or (*naplb: or (?<*
  2661. .sp
  2662. Consider the problem of finding the right-most word in a string that also
  2663. appears earlier in the string, that is, it must appear at least twice in total.
  2664. This pattern returns the required result as captured substring 1:
  2665. .sp
  2666. ^(?x)(*napla: .* \eb(\ew++)) (?> .*? \eb\e1\eb ){2}
  2667. .sp
  2668. For a subject such as "word1 word2 word3 word2 word3 word4" the result is
  2669. "word3". How does it work? At the start, ^(?x) anchors the pattern and sets the
  2670. "x" option, which causes white space (introduced for readability) to be
  2671. ignored. Inside the assertion, the greedy .* at first consumes the entire
  2672. string, but then has to backtrack until the rest of the assertion can match a
  2673. word, which is captured by group 1. In other words, when the assertion first
  2674. succeeds, it captures the right-most word in the string.
  2675. .P
  2676. The current matching point is then reset to the start of the subject, and the
  2677. rest of the pattern match checks for two occurrences of the captured word,
  2678. using an ungreedy .*? to scan from the left. If this succeeds, we are done, but
  2679. if the last word in the string does not occur twice, this part of the pattern
  2680. fails. If a traditional atomic lookhead (?= or (*pla: had been used, the
  2681. assertion could not be re-entered, and the whole match would fail. The pattern
  2682. would succeed only if the very last word in the subject was found twice.
  2683. .P
  2684. Using a non-atomic lookahead, however, means that when the last word does not
  2685. occur twice in the string, the lookahead can backtrack and find the second-last
  2686. word, and so on, until either the match succeeds, or all words have been
  2687. tested.
  2688. .P
  2689. Two conditions must be met for a non-atomic assertion to be useful: the
  2690. contents of one or more capturing groups must change after a backtrack into the
  2691. assertion, and there must be a backreference to a changed group later in the
  2692. pattern. If this is not the case, the rest of the pattern match fails exactly
  2693. as before because nothing has changed, so using a non-atomic assertion just
  2694. wastes resources.
  2695. .P
  2696. There is one exception to backtracking into a non-atomic assertion. If an
  2697. (*ACCEPT) control verb is triggered, the assertion succeeds atomically. That
  2698. is, a subsequent match failure cannot backtrack into the assertion.
  2699. .P
  2700. Non-atomic assertions are not supported by the alternative matching function
  2701. \fBpcre2_dfa_match()\fP. They are supported by JIT, but only if they do not
  2702. contain any control verbs such as (*ACCEPT). (This may change in future). Note
  2703. that assertions that appear as conditions for
  2704. .\" HTML <a href="#conditions">
  2705. .\" </a>
  2706. conditional groups
  2707. .\"
  2708. (see below) must be atomic.
  2709. .
  2710. .
  2711. .SH "SCRIPT RUNS"
  2712. .rs
  2713. .sp
  2714. In concept, a script run is a sequence of characters that are all from the same
  2715. Unicode script such as Latin or Greek. However, because some scripts are
  2716. commonly used together, and because some diacritical and other marks are used
  2717. with multiple scripts, it is not that simple. There is a full description of
  2718. the rules that PCRE2 uses in the section entitled
  2719. .\" HTML <a href="pcre2unicode.html#scriptruns">
  2720. .\" </a>
  2721. "Script Runs"
  2722. .\"
  2723. in the
  2724. .\" HREF
  2725. \fBpcre2unicode\fP
  2726. .\"
  2727. documentation.
  2728. .P
  2729. If part of a pattern is enclosed between (*script_run: or (*sr: and a closing
  2730. parenthesis, it fails if the sequence of characters that it matches are not a
  2731. script run. After a failure, normal backtracking occurs. Script runs can be
  2732. used to detect spoofing attacks using characters that look the same, but are
  2733. from different scripts. The string "paypal.com" is an infamous example, where
  2734. the letters could be a mixture of Latin and Cyrillic. This pattern ensures that
  2735. the matched characters in a sequence of non-spaces that follow white space are
  2736. a script run:
  2737. .sp
  2738. \es+(*sr:\eS+)
  2739. .sp
  2740. To be sure that they are all from the Latin script (for example), a lookahead
  2741. can be used:
  2742. .sp
  2743. \es+(?=\ep{Latin})(*sr:\eS+)
  2744. .sp
  2745. This works as long as the first character is expected to be a character in that
  2746. script, and not (for example) punctuation, which is allowed with any script. If
  2747. this is not the case, a more creative lookahead is needed. For example, if
  2748. digits, underscore, and dots are permitted at the start:
  2749. .sp
  2750. \es+(?=[0-9_.]*\ep{Latin})(*sr:\eS+)
  2751. .sp
  2752. .P
  2753. In many cases, backtracking into a script run pattern fragment is not
  2754. desirable. The script run can employ an atomic group to prevent this. Because
  2755. this is a common requirement, a shorthand notation is provided by
  2756. (*atomic_script_run: or (*asr:
  2757. .sp
  2758. (*asr:...) is the same as (*sr:(?>...))
  2759. .sp
  2760. Note that the atomic group is inside the script run. Putting it outside would
  2761. not prevent backtracking into the script run pattern.
  2762. .P
  2763. Support for script runs is not available if PCRE2 is compiled without Unicode
  2764. support. A compile-time error is given if any of the above constructs is
  2765. encountered. Script runs are not supported by the alternate matching function,
  2766. \fBpcre2_dfa_match()\fP because they use the same mechanism as capturing
  2767. parentheses.
  2768. .P
  2769. \fBWarning:\fP The (*ACCEPT) control verb
  2770. .\" HTML <a href="#acceptverb">
  2771. .\" </a>
  2772. (see below)
  2773. .\"
  2774. should not be used within a script run group, because it causes an immediate
  2775. exit from the group, bypassing the script run checking.
  2776. .
  2777. .
  2778. .\" HTML <a name="conditions"></a>
  2779. .SH "CONDITIONAL GROUPS"
  2780. .rs
  2781. .sp
  2782. It is possible to cause the matching process to obey a pattern fragment
  2783. conditionally or to choose between two alternative fragments, depending on
  2784. the result of an assertion, or whether a specific capture group has
  2785. already been matched. The two possible forms of conditional group are:
  2786. .sp
  2787. (?(condition)yes-pattern)
  2788. (?(condition)yes-pattern|no-pattern)
  2789. .sp
  2790. If the condition is satisfied, the yes-pattern is used; otherwise the
  2791. no-pattern (if present) is used. An absent no-pattern is equivalent to an empty
  2792. string (it always matches). If there are more than two alternatives in the
  2793. group, a compile-time error occurs. Each of the two alternatives may itself
  2794. contain nested groups of any form, including conditional groups; the
  2795. restriction to two alternatives applies only at the level of the condition
  2796. itself. This pattern fragment is an example where the alternatives are complex:
  2797. .sp
  2798. (?(1) (A|B|C) | (D | (?(2)E|F) | E) )
  2799. .sp
  2800. .P
  2801. There are five kinds of condition: references to capture groups, references to
  2802. recursion, two pseudo-conditions called DEFINE and VERSION, and assertions.
  2803. .
  2804. .
  2805. .SS "Checking for a used capture group by number"
  2806. .rs
  2807. .sp
  2808. If the text between the parentheses consists of a sequence of digits, the
  2809. condition is true if a capture group of that number has previously matched. If
  2810. there is more than one capture group with the same number (see the earlier
  2811. .\"
  2812. .\" HTML <a href="#recursion">
  2813. .\" </a>
  2814. section about duplicate group numbers),
  2815. .\"
  2816. the condition is true if any of them have matched. An alternative notation is
  2817. to precede the digits with a plus or minus sign. In this case, the group number
  2818. is relative rather than absolute. The most recently opened capture group can be
  2819. referenced by (?(-1), the next most recent by (?(-2), and so on. Inside loops
  2820. it can also make sense to refer to subsequent groups. The next capture group
  2821. can be referenced as (?(+1), and so on. (The value zero in any of these forms
  2822. is not used; it provokes a compile-time error.)
  2823. .P
  2824. Consider the following pattern, which contains non-significant white space to
  2825. make it more readable (assume the PCRE2_EXTENDED option) and to divide it into
  2826. three parts for ease of discussion:
  2827. .sp
  2828. ( \e( )? [^()]+ (?(1) \e) )
  2829. .sp
  2830. The first part matches an optional opening parenthesis, and if that
  2831. character is present, sets it as the first captured substring. The second part
  2832. matches one or more characters that are not parentheses. The third part is a
  2833. conditional group that tests whether or not the first capture group
  2834. matched. If it did, that is, if subject started with an opening parenthesis,
  2835. the condition is true, and so the yes-pattern is executed and a closing
  2836. parenthesis is required. Otherwise, since no-pattern is not present, the
  2837. conditional group matches nothing. In other words, this pattern matches a
  2838. sequence of non-parentheses, optionally enclosed in parentheses.
  2839. .P
  2840. If you were embedding this pattern in a larger one, you could use a relative
  2841. reference:
  2842. .sp
  2843. ...other stuff... ( \e( )? [^()]+ (?(-1) \e) ) ...
  2844. .sp
  2845. This makes the fragment independent of the parentheses in the larger pattern.
  2846. .
  2847. .
  2848. .SS "Checking for a used capture group by name"
  2849. .rs
  2850. .sp
  2851. Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a used
  2852. capture group by name. For compatibility with earlier versions of PCRE1, which
  2853. had this facility before Perl, the syntax (?(name)...) is also recognized.
  2854. Note, however, that undelimited names consisting of the letter R followed by
  2855. digits are ambiguous (see the following section). Rewriting the above example
  2856. to use a named group gives this:
  2857. .sp
  2858. (?<OPEN> \e( )? [^()]+ (?(<OPEN>) \e) )
  2859. .sp
  2860. If the name used in a condition of this kind is a duplicate, the test is
  2861. applied to all groups of the same name, and is true if any one of them has
  2862. matched.
  2863. .
  2864. .
  2865. .SS "Checking for pattern recursion"
  2866. .rs
  2867. .sp
  2868. "Recursion" in this sense refers to any subroutine-like call from one part of
  2869. the pattern to another, whether or not it is actually recursive. See the
  2870. sections entitled
  2871. .\" HTML <a href="#recursion">
  2872. .\" </a>
  2873. "Recursive patterns"
  2874. .\"
  2875. and
  2876. .\" HTML <a href="#groupsassubroutines">
  2877. .\" </a>
  2878. "Groups as subroutines"
  2879. .\"
  2880. below for details of recursion and subroutine calls.
  2881. .P
  2882. If a condition is the string (R), and there is no capture group with the name
  2883. R, the condition is true if matching is currently in a recursion or subroutine
  2884. call to the whole pattern or any capture group. If digits follow the letter R,
  2885. and there is no group with that name, the condition is true if the most recent
  2886. call is into a group with the given number, which must exist somewhere in the
  2887. overall pattern. This is a contrived example that is equivalent to a+b:
  2888. .sp
  2889. ((?(R1)a+|(?1)b))
  2890. .sp
  2891. However, in both cases, if there is a capture group with a matching name, the
  2892. condition tests for its being set, as described in the section above, instead
  2893. of testing for recursion. For example, creating a group with the name R1 by
  2894. adding (?<R1>) to the above pattern completely changes its meaning.
  2895. .P
  2896. If a name preceded by ampersand follows the letter R, for example:
  2897. .sp
  2898. (?(R&name)...)
  2899. .sp
  2900. the condition is true if the most recent recursion is into a group of that name
  2901. (which must exist within the pattern).
  2902. .P
  2903. This condition does not check the entire recursion stack. It tests only the
  2904. current level. If the name used in a condition of this kind is a duplicate, the
  2905. test is applied to all groups of the same name, and is true if any one of
  2906. them is the most recent recursion.
  2907. .P
  2908. At "top level", all these recursion test conditions are false.
  2909. .
  2910. .
  2911. .\" HTML <a name="subdefine"></a>
  2912. .SS "Defining capture groups for use by reference only"
  2913. .rs
  2914. .sp
  2915. If the condition is the string (DEFINE), the condition is always false, even if
  2916. there is a group with the name DEFINE. In this case, there may be only one
  2917. alternative in the rest of the conditional group. It is always skipped if
  2918. control reaches this point in the pattern; the idea of DEFINE is that it can be
  2919. used to define subroutines that can be referenced from elsewhere. (The use of
  2920. .\" HTML <a href="#groupsassubroutines">
  2921. .\" </a>
  2922. subroutines
  2923. .\"
  2924. is described below.) For example, a pattern to match an IPv4 address such as
  2925. "192.168.23.245" could be written like this (ignore white space and line
  2926. breaks):
  2927. .sp
  2928. (?(DEFINE) (?<byte> 2[0-4]\ed | 25[0-5] | 1\ed\ed | [1-9]?\ed) )
  2929. \eb (?&byte) (\e.(?&byte)){3} \eb
  2930. .sp
  2931. The first part of the pattern is a DEFINE group inside which a another group
  2932. named "byte" is defined. This matches an individual component of an IPv4
  2933. address (a number less than 256). When matching takes place, this part of the
  2934. pattern is skipped because DEFINE acts like a false condition. The rest of the
  2935. pattern uses references to the named group to match the four dot-separated
  2936. components of an IPv4 address, insisting on a word boundary at each end.
  2937. .
  2938. .
  2939. .SS "Checking the PCRE2 version"
  2940. .rs
  2941. .sp
  2942. Programs that link with a PCRE2 library can check the version by calling
  2943. \fBpcre2_config()\fP with appropriate arguments. Users of applications that do
  2944. not have access to the underlying code cannot do this. A special "condition"
  2945. called VERSION exists to allow such users to discover which version of PCRE2
  2946. they are dealing with by using this condition to match a string such as
  2947. "yesno". VERSION must be followed either by "=" or ">=" and a version number.
  2948. For example:
  2949. .sp
  2950. (?(VERSION>=10.4)yes|no)
  2951. .sp
  2952. This pattern matches "yes" if the PCRE2 version is greater or equal to 10.4, or
  2953. "no" otherwise. The fractional part of the version number may not contain more
  2954. than two digits.
  2955. .
  2956. .
  2957. .SS "Assertion conditions"
  2958. .rs
  2959. .sp
  2960. If the condition is not in any of the above formats, it must be a parenthesized
  2961. assertion. This may be a positive or negative lookahead or lookbehind
  2962. assertion. However, it must be a traditional atomic assertion, not one of the
  2963. PCRE2-specific
  2964. .\" HTML <a href="#nonatomicassertions">
  2965. .\" </a>
  2966. non-atomic assertions.
  2967. .\"
  2968. .P
  2969. Consider this pattern, again containing non-significant white space, and with
  2970. the two alternatives on the second line:
  2971. .sp
  2972. (?(?=[^a-z]*[a-z])
  2973. \ed{2}-[a-z]{3}-\ed{2} | \ed{2}-\ed{2}-\ed{2} )
  2974. .sp
  2975. The condition is a positive lookahead assertion that matches an optional
  2976. sequence of non-letters followed by a letter. In other words, it tests for the
  2977. presence of at least one letter in the subject. If a letter is found, the
  2978. subject is matched against the first alternative; otherwise it is matched
  2979. against the second. This pattern matches strings in one of the two forms
  2980. dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.
  2981. .P
  2982. When an assertion that is a condition contains capture groups, any
  2983. capturing that occurs in a matching branch is retained afterwards, for both
  2984. positive and negative assertions, because matching always continues after the
  2985. assertion, whether it succeeds or fails. (Compare non-conditional assertions,
  2986. for which captures are retained only for positive assertions that succeed.)
  2987. .
  2988. .
  2989. .\" HTML <a name="comments"></a>
  2990. .SH COMMENTS
  2991. .rs
  2992. .sp
  2993. There are two ways of including comments in patterns that are processed by
  2994. PCRE2. In both cases, the start of the comment must not be in a character
  2995. class, nor in the middle of any other sequence of related characters such as
  2996. (?: or a group name or number. The characters that make up a comment play
  2997. no part in the pattern matching.
  2998. .P
  2999. The sequence (?# marks the start of a comment that continues up to the next
  3000. closing parenthesis. Nested parentheses are not permitted. If the
  3001. PCRE2_EXTENDED or PCRE2_EXTENDED_MORE option is set, an unescaped # character
  3002. also introduces a comment, which in this case continues to immediately after
  3003. the next newline character or character sequence in the pattern. Which
  3004. characters are interpreted as newlines is controlled by an option passed to the
  3005. compiling function or by a special sequence at the start of the pattern, as
  3006. described in the section entitled
  3007. .\" HTML <a href="#newlines">
  3008. .\" </a>
  3009. "Newline conventions"
  3010. .\"
  3011. above. Note that the end of this type of comment is a literal newline sequence
  3012. in the pattern; escape sequences that happen to represent a newline do not
  3013. count. For example, consider this pattern when PCRE2_EXTENDED is set, and the
  3014. default newline convention (a single linefeed character) is in force:
  3015. .sp
  3016. abc #comment \en still comment
  3017. .sp
  3018. On encountering the # character, \fBpcre2_compile()\fP skips along, looking for
  3019. a newline in the pattern. The sequence \en is still literal at this stage, so
  3020. it does not terminate the comment. Only an actual character with the code value
  3021. 0x0a (the default newline) does so.
  3022. .
  3023. .
  3024. .\" HTML <a name="recursion"></a>
  3025. .SH "RECURSIVE PATTERNS"
  3026. .rs
  3027. .sp
  3028. Consider the problem of matching a string in parentheses, allowing for
  3029. unlimited nested parentheses. Without the use of recursion, the best that can
  3030. be done is to use a pattern that matches up to some fixed depth of nesting. It
  3031. is not possible to handle an arbitrary nesting depth.
  3032. .P
  3033. For some time, Perl has provided a facility that allows regular expressions to
  3034. recurse (amongst other things). It does this by interpolating Perl code in the
  3035. expression at run time, and the code can refer to the expression itself. A Perl
  3036. pattern using code interpolation to solve the parentheses problem can be
  3037. created like this:
  3038. .sp
  3039. $re = qr{\e( (?: (?>[^()]+) | (?p{$re}) )* \e)}x;
  3040. .sp
  3041. The (?p{...}) item interpolates Perl code at run time, and in this case refers
  3042. recursively to the pattern in which it appears.
  3043. .P
  3044. Obviously, PCRE2 cannot support the interpolation of Perl code. Instead, it
  3045. supports special syntax for recursion of the entire pattern, and also for
  3046. individual capture group recursion. After its introduction in PCRE1 and Python,
  3047. this kind of recursion was subsequently introduced into Perl at release 5.10.
  3048. .P
  3049. A special item that consists of (? followed by a number greater than zero and a
  3050. closing parenthesis is a recursive subroutine call of the capture group of the
  3051. given number, provided that it occurs inside that group. (If not, it is a
  3052. .\" HTML <a href="#groupsassubroutines">
  3053. .\" </a>
  3054. non-recursive subroutine
  3055. .\"
  3056. call, which is described in the next section.) The special item (?R) or (?0) is
  3057. a recursive call of the entire regular expression.
  3058. .P
  3059. This PCRE2 pattern solves the nested parentheses problem (assume the
  3060. PCRE2_EXTENDED option is set so that white space is ignored):
  3061. .sp
  3062. \e( ( [^()]++ | (?R) )* \e)
  3063. .sp
  3064. First it matches an opening parenthesis. Then it matches any number of
  3065. substrings which can either be a sequence of non-parentheses, or a recursive
  3066. match of the pattern itself (that is, a correctly parenthesized substring).
  3067. Finally there is a closing parenthesis. Note the use of a possessive quantifier
  3068. to avoid backtracking into sequences of non-parentheses.
  3069. .P
  3070. If this were part of a larger pattern, you would not want to recurse the entire
  3071. pattern, so instead you could use this:
  3072. .sp
  3073. ( \e( ( [^()]++ | (?1) )* \e) )
  3074. .sp
  3075. We have put the pattern into parentheses, and caused the recursion to refer to
  3076. them instead of the whole pattern.
  3077. .P
  3078. In a larger pattern, keeping track of parenthesis numbers can be tricky. This
  3079. is made easier by the use of relative references. Instead of (?1) in the
  3080. pattern above you can write (?-2) to refer to the second most recently opened
  3081. parentheses preceding the recursion. In other words, a negative number counts
  3082. capturing parentheses leftwards from the point at which it is encountered.
  3083. .P
  3084. Be aware however, that if
  3085. .\" HTML <a href="#dupgroupnumber">
  3086. .\" </a>
  3087. duplicate capture group numbers
  3088. .\"
  3089. are in use, relative references refer to the earliest group with the
  3090. appropriate number. Consider, for example:
  3091. .sp
  3092. (?|(a)|(b)) (c) (?-2)
  3093. .sp
  3094. The first two capture groups (a) and (b) are both numbered 1, and group (c)
  3095. is number 2. When the reference (?-2) is encountered, the second most recently
  3096. opened parentheses has the number 1, but it is the first such group (the (a)
  3097. group) to which the recursion refers. This would be the same if an absolute
  3098. reference (?1) was used. In other words, relative references are just a
  3099. shorthand for computing a group number.
  3100. .P
  3101. It is also possible to refer to subsequent capture groups, by writing
  3102. references such as (?+2). However, these cannot be recursive because the
  3103. reference is not inside the parentheses that are referenced. They are always
  3104. .\" HTML <a href="#groupsassubroutines">
  3105. .\" </a>
  3106. non-recursive subroutine
  3107. .\"
  3108. calls, as described in the next section.
  3109. .P
  3110. An alternative approach is to use named parentheses. The Perl syntax for this
  3111. is (?&name); PCRE1's earlier syntax (?P>name) is also supported. We could
  3112. rewrite the above example as follows:
  3113. .sp
  3114. (?<pn> \e( ( [^()]++ | (?&pn) )* \e) )
  3115. .sp
  3116. If there is more than one group with the same name, the earliest one is
  3117. used.
  3118. .P
  3119. The example pattern that we have been looking at contains nested unlimited
  3120. repeats, and so the use of a possessive quantifier for matching strings of
  3121. non-parentheses is important when applying the pattern to strings that do not
  3122. match. For example, when this pattern is applied to
  3123. .sp
  3124. (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
  3125. .sp
  3126. it yields "no match" quickly. However, if a possessive quantifier is not used,
  3127. the match runs for a very long time indeed because there are so many different
  3128. ways the + and * repeats can carve up the subject, and all have to be tested
  3129. before failure can be reported.
  3130. .P
  3131. At the end of a match, the values of capturing parentheses are those from
  3132. the outermost level. If you want to obtain intermediate values, a callout
  3133. function can be used (see below and the
  3134. .\" HREF
  3135. \fBpcre2callout\fP
  3136. .\"
  3137. documentation). If the pattern above is matched against
  3138. .sp
  3139. (ab(cd)ef)
  3140. .sp
  3141. the value for the inner capturing parentheses (numbered 2) is "ef", which is
  3142. the last value taken on at the top level. If a capture group is not matched at
  3143. the top level, its final captured value is unset, even if it was (temporarily)
  3144. set at a deeper level during the matching process.
  3145. .P
  3146. Do not confuse the (?R) item with the condition (R), which tests for recursion.
  3147. Consider this pattern, which matches text in angle brackets, allowing for
  3148. arbitrary nesting. Only digits are allowed in nested brackets (that is, when
  3149. recursing), whereas any characters are permitted at the outer level.
  3150. .sp
  3151. < (?: (?(R) \ed++ | [^<>]*+) | (?R)) * >
  3152. .sp
  3153. In this pattern, (?(R) is the start of a conditional group, with two different
  3154. alternatives for the recursive and non-recursive cases. The (?R) item is the
  3155. actual recursive call.
  3156. .
  3157. .
  3158. .\" HTML <a name="recursiondifference"></a>
  3159. .SS "Differences in recursion processing between PCRE2 and Perl"
  3160. .rs
  3161. .sp
  3162. Some former differences between PCRE2 and Perl no longer exist.
  3163. .P
  3164. Before release 10.30, recursion processing in PCRE2 differed from Perl in that
  3165. a recursive subroutine call was always treated as an atomic group. That is,
  3166. once it had matched some of the subject string, it was never re-entered, even
  3167. if it contained untried alternatives and there was a subsequent matching
  3168. failure. (Historical note: PCRE implemented recursion before Perl did.)
  3169. .P
  3170. Starting with release 10.30, recursive subroutine calls are no longer treated
  3171. as atomic. That is, they can be re-entered to try unused alternatives if there
  3172. is a matching failure later in the pattern. This is now compatible with the way
  3173. Perl works. If you want a subroutine call to be atomic, you must explicitly
  3174. enclose it in an atomic group.
  3175. .P
  3176. Supporting backtracking into recursions simplifies certain types of recursive
  3177. pattern. For example, this pattern matches palindromic strings:
  3178. .sp
  3179. ^((.)(?1)\e2|.?)$
  3180. .sp
  3181. The second branch in the group matches a single central character in the
  3182. palindrome when there are an odd number of characters, or nothing when there
  3183. are an even number of characters, but in order to work it has to be able to try
  3184. the second case when the rest of the pattern match fails. If you want to match
  3185. typical palindromic phrases, the pattern has to ignore all non-word characters,
  3186. which can be done like this:
  3187. .sp
  3188. ^\eW*+((.)\eW*+(?1)\eW*+\e2|\eW*+.?)\eW*+$
  3189. .sp
  3190. If run with the PCRE2_CASELESS option, this pattern matches phrases such as "A
  3191. man, a plan, a canal: Panama!". Note the use of the possessive quantifier *+ to
  3192. avoid backtracking into sequences of non-word characters. Without this, PCRE2
  3193. takes a great deal longer (ten times or more) to match typical phrases, and
  3194. Perl takes so long that you think it has gone into a loop.
  3195. .P
  3196. Another way in which PCRE2 and Perl used to differ in their recursion
  3197. processing is in the handling of captured values. Formerly in Perl, when a
  3198. group was called recursively or as a subroutine (see the next section), it
  3199. had no access to any values that were captured outside the recursion, whereas
  3200. in PCRE2 these values can be referenced. Consider this pattern:
  3201. .sp
  3202. ^(.)(\e1|a(?2))
  3203. .sp
  3204. This pattern matches "bab". The first capturing parentheses match "b", then in
  3205. the second group, when the backreference \e1 fails to match "b", the second
  3206. alternative matches "a" and then recurses. In the recursion, \e1 does now match
  3207. "b" and so the whole match succeeds. This match used to fail in Perl, but in
  3208. later versions (I tried 5.024) it now works.
  3209. .
  3210. .
  3211. .\" HTML <a name="groupsassubroutines"></a>
  3212. .SH "GROUPS AS SUBROUTINES"
  3213. .rs
  3214. .sp
  3215. If the syntax for a recursive group call (either by number or by name) is used
  3216. outside the parentheses to which it refers, it operates a bit like a subroutine
  3217. in a programming language. More accurately, PCRE2 treats the referenced group
  3218. as an independent subpattern which it tries to match at the current matching
  3219. position. The called group may be defined before or after the reference. A
  3220. numbered reference can be absolute or relative, as in these examples:
  3221. .sp
  3222. (...(absolute)...)...(?2)...
  3223. (...(relative)...)...(?-1)...
  3224. (...(?+1)...(relative)...
  3225. .sp
  3226. An earlier example pointed out that the pattern
  3227. .sp
  3228. (sens|respons)e and \e1ibility
  3229. .sp
  3230. matches "sense and sensibility" and "response and responsibility", but not
  3231. "sense and responsibility". If instead the pattern
  3232. .sp
  3233. (sens|respons)e and (?1)ibility
  3234. .sp
  3235. is used, it does match "sense and responsibility" as well as the other two
  3236. strings. Another example is given in the discussion of DEFINE above.
  3237. .P
  3238. Like recursions, subroutine calls used to be treated as atomic, but this
  3239. changed at PCRE2 release 10.30, so backtracking into subroutine calls can now
  3240. occur. However, any capturing parentheses that are set during the subroutine
  3241. call revert to their previous values afterwards.
  3242. .P
  3243. Processing options such as case-independence are fixed when a group is
  3244. defined, so if it is used as a subroutine, such options cannot be changed for
  3245. different calls. For example, consider this pattern:
  3246. .sp
  3247. (abc)(?i:(?-1))
  3248. .sp
  3249. It matches "abcabc". It does not match "abcABC" because the change of
  3250. processing option does not affect the called group.
  3251. .P
  3252. The behaviour of
  3253. .\" HTML <a href="#backtrackcontrol">
  3254. .\" </a>
  3255. backtracking control verbs
  3256. .\"
  3257. in groups when called as subroutines is described in the section entitled
  3258. .\" HTML <a href="#btsub">
  3259. .\" </a>
  3260. "Backtracking verbs in subroutines"
  3261. .\"
  3262. below.
  3263. .
  3264. .
  3265. .\" HTML <a name="onigurumasubroutines"></a>
  3266. .SH "ONIGURUMA SUBROUTINE SYNTAX"
  3267. .rs
  3268. .sp
  3269. For compatibility with Oniguruma, the non-Perl syntax \eg followed by a name or
  3270. a number enclosed either in angle brackets or single quotes, is an alternative
  3271. syntax for calling a group as a subroutine, possibly recursively. Here are two
  3272. of the examples used above, rewritten using this syntax:
  3273. .sp
  3274. (?<pn> \e( ( (?>[^()]+) | \eg<pn> )* \e) )
  3275. (sens|respons)e and \eg'1'ibility
  3276. .sp
  3277. PCRE2 supports an extension to Oniguruma: if a number is preceded by a
  3278. plus or a minus sign it is taken as a relative reference. For example:
  3279. .sp
  3280. (abc)(?i:\eg<-1>)
  3281. .sp
  3282. Note that \eg{...} (Perl syntax) and \eg<...> (Oniguruma syntax) are \fInot\fP
  3283. synonymous. The former is a backreference; the latter is a subroutine call.
  3284. .
  3285. .
  3286. .SH CALLOUTS
  3287. .rs
  3288. .sp
  3289. Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl
  3290. code to be obeyed in the middle of matching a regular expression. This makes it
  3291. possible, amongst other things, to extract different substrings that match the
  3292. same pair of parentheses when there is a repetition.
  3293. .P
  3294. PCRE2 provides a similar feature, but of course it cannot obey arbitrary Perl
  3295. code. The feature is called "callout". The caller of PCRE2 provides an external
  3296. function by putting its entry point in a match context using the function
  3297. \fBpcre2_set_callout()\fP, and then passing that context to \fBpcre2_match()\fP
  3298. or \fBpcre2_dfa_match()\fP. If no match context is passed, or if the callout
  3299. entry point is set to NULL, callouts are disabled.
  3300. .P
  3301. Within a regular expression, (?C<arg>) indicates a point at which the external
  3302. function is to be called. There are two kinds of callout: those with a
  3303. numerical argument and those with a string argument. (?C) on its own with no
  3304. argument is treated as (?C0). A numerical argument allows the application to
  3305. distinguish between different callouts. String arguments were added for release
  3306. 10.20 to make it possible for script languages that use PCRE2 to embed short
  3307. scripts within patterns in a similar way to Perl.
  3308. .P
  3309. During matching, when PCRE2 reaches a callout point, the external function is
  3310. called. It is provided with the number or string argument of the callout, the
  3311. position in the pattern, and one item of data that is also set in the match
  3312. block. The callout function may cause matching to proceed, to backtrack, or to
  3313. fail.
  3314. .P
  3315. By default, PCRE2 implements a number of optimizations at matching time, and
  3316. one side-effect is that sometimes callouts are skipped. If you need all
  3317. possible callouts to happen, you need to set options that disable the relevant
  3318. optimizations. More details, including a complete description of the
  3319. programming interface to the callout function, are given in the
  3320. .\" HREF
  3321. \fBpcre2callout\fP
  3322. .\"
  3323. documentation.
  3324. .
  3325. .
  3326. .SS "Callouts with numerical arguments"
  3327. .rs
  3328. .sp
  3329. If you just want to have a means of identifying different callout points, put a
  3330. number less than 256 after the letter C. For example, this pattern has two
  3331. callout points:
  3332. .sp
  3333. (?C1)abc(?C2)def
  3334. .sp
  3335. If the PCRE2_AUTO_CALLOUT flag is passed to \fBpcre2_compile()\fP, numerical
  3336. callouts are automatically installed before each item in the pattern. They are
  3337. all numbered 255. If there is a conditional group in the pattern whose
  3338. condition is an assertion, an additional callout is inserted just before the
  3339. condition. An explicit callout may also be set at this position, as in this
  3340. example:
  3341. .sp
  3342. (?(?C9)(?=a)abc|def)
  3343. .sp
  3344. Note that this applies only to assertion conditions, not to other types of
  3345. condition.
  3346. .
  3347. .
  3348. .SS "Callouts with string arguments"
  3349. .rs
  3350. .sp
  3351. A delimited string may be used instead of a number as a callout argument. The
  3352. starting delimiter must be one of ` ' " ^ % # $ { and the ending delimiter is
  3353. the same as the start, except for {, where the ending delimiter is }. If the
  3354. ending delimiter is needed within the string, it must be doubled. For
  3355. example:
  3356. .sp
  3357. (?C'ab ''c'' d')xyz(?C{any text})pqr
  3358. .sp
  3359. The doubling is removed before the string is passed to the callout function.
  3360. .
  3361. .
  3362. .\" HTML <a name="backtrackcontrol"></a>
  3363. .SH "BACKTRACKING CONTROL"
  3364. .rs
  3365. .sp
  3366. There are a number of special "Backtracking Control Verbs" (to use Perl's
  3367. terminology) that modify the behaviour of backtracking during matching. They
  3368. are generally of the form (*VERB) or (*VERB:NAME). Some verbs take either form,
  3369. and may behave differently depending on whether or not a name argument is
  3370. present. The names are not required to be unique within the pattern.
  3371. .P
  3372. By default, for compatibility with Perl, a name is any sequence of characters
  3373. that does not include a closing parenthesis. The name is not processed in
  3374. any way, and it is not possible to include a closing parenthesis in the name.
  3375. This can be changed by setting the PCRE2_ALT_VERBNAMES option, but the result
  3376. is no longer Perl-compatible.
  3377. .P
  3378. When PCRE2_ALT_VERBNAMES is set, backslash processing is applied to verb names
  3379. and only an unescaped closing parenthesis terminates the name. However, the
  3380. only backslash items that are permitted are \eQ, \eE, and sequences such as
  3381. \ex{100} that define character code points. Character type escapes such as \ed
  3382. are faulted.
  3383. .P
  3384. A closing parenthesis can be included in a name either as \e) or between \eQ
  3385. and \eE. In addition to backslash processing, if the PCRE2_EXTENDED or
  3386. PCRE2_EXTENDED_MORE option is also set, unescaped whitespace in verb names is
  3387. skipped, and #-comments are recognized, exactly as in the rest of the pattern.
  3388. PCRE2_EXTENDED and PCRE2_EXTENDED_MORE do not affect verb names unless
  3389. PCRE2_ALT_VERBNAMES is also set.
  3390. .P
  3391. The maximum length of a name is 255 in the 8-bit library and 65535 in the
  3392. 16-bit and 32-bit libraries. If the name is empty, that is, if the closing
  3393. parenthesis immediately follows the colon, the effect is as if the colon were
  3394. not there. Any number of these verbs may occur in a pattern. Except for
  3395. (*ACCEPT), they may not be quantified.
  3396. .P
  3397. Since these verbs are specifically related to backtracking, most of them can be
  3398. used only when the pattern is to be matched using the traditional matching
  3399. function, because that uses a backtracking algorithm. With the exception of
  3400. (*FAIL), which behaves like a failing negative assertion, the backtracking
  3401. control verbs cause an error if encountered by the DFA matching function.
  3402. .P
  3403. The behaviour of these verbs in
  3404. .\" HTML <a href="#btrepeat">
  3405. .\" </a>
  3406. repeated groups,
  3407. .\"
  3408. .\" HTML <a href="#btassert">
  3409. .\" </a>
  3410. assertions,
  3411. .\"
  3412. and in
  3413. .\" HTML <a href="#btsub">
  3414. .\" </a>
  3415. capture groups called as subroutines
  3416. .\"
  3417. (whether or not recursively) is documented below.
  3418. .
  3419. .
  3420. .\" HTML <a name="nooptimize"></a>
  3421. .SS "Optimizations that affect backtracking verbs"
  3422. .rs
  3423. .sp
  3424. PCRE2 contains some optimizations that are used to speed up matching by running
  3425. some checks at the start of each match attempt. For example, it may know the
  3426. minimum length of matching subject, or that a particular character must be
  3427. present. When one of these optimizations bypasses the running of a match, any
  3428. included backtracking verbs will not, of course, be processed. You can suppress
  3429. the start-of-match optimizations by setting the PCRE2_NO_START_OPTIMIZE option
  3430. when calling \fBpcre2_compile()\fP, or by starting the pattern with
  3431. (*NO_START_OPT). There is more discussion of this option in the section
  3432. entitled
  3433. .\" HTML <a href="pcre2api.html#compiling">
  3434. .\" </a>
  3435. "Compiling a pattern"
  3436. .\"
  3437. in the
  3438. .\" HREF
  3439. \fBpcre2api\fP
  3440. .\"
  3441. documentation.
  3442. .P
  3443. Experiments with Perl suggest that it too has similar optimizations, and like
  3444. PCRE2, turning them off can change the result of a match.
  3445. .
  3446. .
  3447. .\" HTML <a name="acceptverb"></a>
  3448. .SS "Verbs that act immediately"
  3449. .rs
  3450. .sp
  3451. The following verbs act as soon as they are encountered.
  3452. .sp
  3453. (*ACCEPT) or (*ACCEPT:NAME)
  3454. .sp
  3455. This verb causes the match to end successfully, skipping the remainder of the
  3456. pattern. However, when it is inside a capture group that is called as a
  3457. subroutine, only that group is ended successfully. Matching then continues
  3458. at the outer level. If (*ACCEPT) in triggered in a positive assertion, the
  3459. assertion succeeds; in a negative assertion, the assertion fails.
  3460. .P
  3461. If (*ACCEPT) is inside capturing parentheses, the data so far is captured. For
  3462. example:
  3463. .sp
  3464. A((?:A|B(*ACCEPT)|C)D)
  3465. .sp
  3466. This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is captured by
  3467. the outer parentheses.
  3468. .P
  3469. (*ACCEPT) is the only backtracking verb that is allowed to be quantified
  3470. because an ungreedy quantification with a minimum of zero acts only when a
  3471. backtrack happens. Consider, for example,
  3472. .sp
  3473. (A(*ACCEPT)??B)C
  3474. .sp
  3475. where A, B, and C may be complex expressions. After matching "A", the matcher
  3476. processes "BC"; if that fails, causing a backtrack, (*ACCEPT) is triggered and
  3477. the match succeeds. In both cases, all but C is captured. Whereas (*COMMIT)
  3478. (see below) means "fail on backtrack", a repeated (*ACCEPT) of this type means
  3479. "succeed on backtrack".
  3480. .P
  3481. \fBWarning:\fP (*ACCEPT) should not be used within a script run group, because
  3482. it causes an immediate exit from the group, bypassing the script run checking.
  3483. .sp
  3484. (*FAIL) or (*FAIL:NAME)
  3485. .sp
  3486. This verb causes a matching failure, forcing backtracking to occur. It may be
  3487. abbreviated to (*F). It is equivalent to (?!) but easier to read. The Perl
  3488. documentation notes that it is probably useful only when combined with (?{}) or
  3489. (??{}). Those are, of course, Perl features that are not present in PCRE2. The
  3490. nearest equivalent is the callout feature, as for example in this pattern:
  3491. .sp
  3492. a+(?C)(*FAIL)
  3493. .sp
  3494. A match with the string "aaaa" always fails, but the callout is taken before
  3495. each backtrack happens (in this example, 10 times).
  3496. .P
  3497. (*ACCEPT:NAME) and (*FAIL:NAME) behave the same as (*MARK:NAME)(*ACCEPT) and
  3498. (*MARK:NAME)(*FAIL), respectively, that is, a (*MARK) is recorded just before
  3499. the verb acts.
  3500. .
  3501. .
  3502. .SS "Recording which path was taken"
  3503. .rs
  3504. .sp
  3505. There is one verb whose main purpose is to track how a match was arrived at,
  3506. though it also has a secondary use in conjunction with advancing the match
  3507. starting point (see (*SKIP) below).
  3508. .sp
  3509. (*MARK:NAME) or (*:NAME)
  3510. .sp
  3511. A name is always required with this verb. For all the other backtracking
  3512. control verbs, a NAME argument is optional.
  3513. .P
  3514. When a match succeeds, the name of the last-encountered mark name on the
  3515. matching path is passed back to the caller as described in the section entitled
  3516. .\" HTML <a href="pcre2api.html#matchotherdata">
  3517. .\" </a>
  3518. "Other information about the match"
  3519. .\"
  3520. in the
  3521. .\" HREF
  3522. \fBpcre2api\fP
  3523. .\"
  3524. documentation. This applies to all instances of (*MARK) and other verbs,
  3525. including those inside assertions and atomic groups. However, there are
  3526. differences in those cases when (*MARK) is used in conjunction with (*SKIP) as
  3527. described below.
  3528. .P
  3529. The mark name that was last encountered on the matching path is passed back. A
  3530. verb without a NAME argument is ignored for this purpose. Here is an example of
  3531. \fBpcre2test\fP output, where the "mark" modifier requests the retrieval and
  3532. outputting of (*MARK) data:
  3533. .sp
  3534. re> /X(*MARK:A)Y|X(*MARK:B)Z/mark
  3535. data> XY
  3536. 0: XY
  3537. MK: A
  3538. XZ
  3539. 0: XZ
  3540. MK: B
  3541. .sp
  3542. The (*MARK) name is tagged with "MK:" in this output, and in this example it
  3543. indicates which of the two alternatives matched. This is a more efficient way
  3544. of obtaining this information than putting each alternative in its own
  3545. capturing parentheses.
  3546. .P
  3547. If a verb with a name is encountered in a positive assertion that is true, the
  3548. name is recorded and passed back if it is the last-encountered. This does not
  3549. happen for negative assertions or failing positive assertions.
  3550. .P
  3551. After a partial match or a failed match, the last encountered name in the
  3552. entire match process is returned. For example:
  3553. .sp
  3554. re> /X(*MARK:A)Y|X(*MARK:B)Z/mark
  3555. data> XP
  3556. No match, mark = B
  3557. .sp
  3558. Note that in this unanchored example the mark is retained from the match
  3559. attempt that started at the letter "X" in the subject. Subsequent match
  3560. attempts starting at "P" and then with an empty string do not get as far as the
  3561. (*MARK) item, but nevertheless do not reset it.
  3562. .P
  3563. If you are interested in (*MARK) values after failed matches, you should
  3564. probably set the PCRE2_NO_START_OPTIMIZE option
  3565. .\" HTML <a href="#nooptimize">
  3566. .\" </a>
  3567. (see above)
  3568. .\"
  3569. to ensure that the match is always attempted.
  3570. .
  3571. .
  3572. .SS "Verbs that act after backtracking"
  3573. .rs
  3574. .sp
  3575. The following verbs do nothing when they are encountered. Matching continues
  3576. with what follows, but if there is a subsequent match failure, causing a
  3577. backtrack to the verb, a failure is forced. That is, backtracking cannot pass
  3578. to the left of the verb. However, when one of these verbs appears inside an
  3579. atomic group or in a lookaround assertion that is true, its effect is confined
  3580. to that group, because once the group has been matched, there is never any
  3581. backtracking into it. Backtracking from beyond an assertion or an atomic group
  3582. ignores the entire group, and seeks a preceding backtracking point.
  3583. .P
  3584. These verbs differ in exactly what kind of failure occurs when backtracking
  3585. reaches them. The behaviour described below is what happens when the verb is
  3586. not in a subroutine or an assertion. Subsequent sections cover these special
  3587. cases.
  3588. .sp
  3589. (*COMMIT) or (*COMMIT:NAME)
  3590. .sp
  3591. This verb causes the whole match to fail outright if there is a later matching
  3592. failure that causes backtracking to reach it. Even if the pattern is
  3593. unanchored, no further attempts to find a match by advancing the starting point
  3594. take place. If (*COMMIT) is the only backtracking verb that is encountered,
  3595. once it has been passed \fBpcre2_match()\fP is committed to finding a match at
  3596. the current starting point, or not at all. For example:
  3597. .sp
  3598. a+(*COMMIT)b
  3599. .sp
  3600. This matches "xxaab" but not "aacaab". It can be thought of as a kind of
  3601. dynamic anchor, or "I've started, so I must finish."
  3602. .P
  3603. The behaviour of (*COMMIT:NAME) is not the same as (*MARK:NAME)(*COMMIT). It is
  3604. like (*MARK:NAME) in that the name is remembered for passing back to the
  3605. caller. However, (*SKIP:NAME) searches only for names that are set with
  3606. (*MARK), ignoring those set by any of the other backtracking verbs.
  3607. .P
  3608. If there is more than one backtracking verb in a pattern, a different one that
  3609. follows (*COMMIT) may be triggered first, so merely passing (*COMMIT) during a
  3610. match does not always guarantee that a match must be at this starting point.
  3611. .P
  3612. Note that (*COMMIT) at the start of a pattern is not the same as an anchor,
  3613. unless PCRE2's start-of-match optimizations are turned off, as shown in this
  3614. output from \fBpcre2test\fP:
  3615. .sp
  3616. re> /(*COMMIT)abc/
  3617. data> xyzabc
  3618. 0: abc
  3619. data>
  3620. re> /(*COMMIT)abc/no_start_optimize
  3621. data> xyzabc
  3622. No match
  3623. .sp
  3624. For the first pattern, PCRE2 knows that any match must start with "a", so the
  3625. optimization skips along the subject to "a" before applying the pattern to the
  3626. first set of data. The match attempt then succeeds. The second pattern disables
  3627. the optimization that skips along to the first character. The pattern is now
  3628. applied starting at "x", and so the (*COMMIT) causes the match to fail without
  3629. trying any other starting points.
  3630. .sp
  3631. (*PRUNE) or (*PRUNE:NAME)
  3632. .sp
  3633. This verb causes the match to fail at the current starting position in the
  3634. subject if there is a later matching failure that causes backtracking to reach
  3635. it. If the pattern is unanchored, the normal "bumpalong" advance to the next
  3636. starting character then happens. Backtracking can occur as usual to the left of
  3637. (*PRUNE), before it is reached, or when matching to the right of (*PRUNE), but
  3638. if there is no match to the right, backtracking cannot cross (*PRUNE). In
  3639. simple cases, the use of (*PRUNE) is just an alternative to an atomic group or
  3640. possessive quantifier, but there are some uses of (*PRUNE) that cannot be
  3641. expressed in any other way. In an anchored pattern (*PRUNE) has the same effect
  3642. as (*COMMIT).
  3643. .P
  3644. The behaviour of (*PRUNE:NAME) is not the same as (*MARK:NAME)(*PRUNE). It is
  3645. like (*MARK:NAME) in that the name is remembered for passing back to the
  3646. caller. However, (*SKIP:NAME) searches only for names set with (*MARK),
  3647. ignoring those set by other backtracking verbs.
  3648. .sp
  3649. (*SKIP)
  3650. .sp
  3651. This verb, when given without a name, is like (*PRUNE), except that if the
  3652. pattern is unanchored, the "bumpalong" advance is not to the next character,
  3653. but to the position in the subject where (*SKIP) was encountered. (*SKIP)
  3654. signifies that whatever text was matched leading up to it cannot be part of a
  3655. successful match if there is a later mismatch. Consider:
  3656. .sp
  3657. a+(*SKIP)b
  3658. .sp
  3659. If the subject is "aaaac...", after the first match attempt fails (starting at
  3660. the first character in the string), the starting point skips on to start the
  3661. next attempt at "c". Note that a possessive quantifer does not have the same
  3662. effect as this example; although it would suppress backtracking during the
  3663. first match attempt, the second attempt would start at the second character
  3664. instead of skipping on to "c".
  3665. .P
  3666. If (*SKIP) is used to specify a new starting position that is the same as the
  3667. starting position of the current match, or (by being inside a lookbehind)
  3668. earlier, the position specified by (*SKIP) is ignored, and instead the normal
  3669. "bumpalong" occurs.
  3670. .sp
  3671. (*SKIP:NAME)
  3672. .sp
  3673. When (*SKIP) has an associated name, its behaviour is modified. When such a
  3674. (*SKIP) is triggered, the previous path through the pattern is searched for the
  3675. most recent (*MARK) that has the same name. If one is found, the "bumpalong"
  3676. advance is to the subject position that corresponds to that (*MARK) instead of
  3677. to where (*SKIP) was encountered. If no (*MARK) with a matching name is found,
  3678. the (*SKIP) is ignored.
  3679. .P
  3680. The search for a (*MARK) name uses the normal backtracking mechanism, which
  3681. means that it does not see (*MARK) settings that are inside atomic groups or
  3682. assertions, because they are never re-entered by backtracking. Compare the
  3683. following \fBpcre2test\fP examples:
  3684. .sp
  3685. re> /a(?>(*MARK:X))(*SKIP:X)(*F)|(.)/
  3686. data: abc
  3687. 0: a
  3688. 1: a
  3689. data:
  3690. re> /a(?:(*MARK:X))(*SKIP:X)(*F)|(.)/
  3691. data: abc
  3692. 0: b
  3693. 1: b
  3694. .sp
  3695. In the first example, the (*MARK) setting is in an atomic group, so it is not
  3696. seen when (*SKIP:X) triggers, causing the (*SKIP) to be ignored. This allows
  3697. the second branch of the pattern to be tried at the first character position.
  3698. In the second example, the (*MARK) setting is not in an atomic group. This
  3699. allows (*SKIP:X) to find the (*MARK) when it backtracks, and this causes a new
  3700. matching attempt to start at the second character. This time, the (*MARK) is
  3701. never seen because "a" does not match "b", so the matcher immediately jumps to
  3702. the second branch of the pattern.
  3703. .P
  3704. Note that (*SKIP:NAME) searches only for names set by (*MARK:NAME). It ignores
  3705. names that are set by other backtracking verbs.
  3706. .sp
  3707. (*THEN) or (*THEN:NAME)
  3708. .sp
  3709. This verb causes a skip to the next innermost alternative when backtracking
  3710. reaches it. That is, it cancels any further backtracking within the current
  3711. alternative. Its name comes from the observation that it can be used for a
  3712. pattern-based if-then-else block:
  3713. .sp
  3714. ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
  3715. .sp
  3716. If the COND1 pattern matches, FOO is tried (and possibly further items after
  3717. the end of the group if FOO succeeds); on failure, the matcher skips to the
  3718. second alternative and tries COND2, without backtracking into COND1. If that
  3719. succeeds and BAR fails, COND3 is tried. If subsequently BAZ fails, there are no
  3720. more alternatives, so there is a backtrack to whatever came before the entire
  3721. group. If (*THEN) is not inside an alternation, it acts like (*PRUNE).
  3722. .P
  3723. The behaviour of (*THEN:NAME) is not the same as (*MARK:NAME)(*THEN). It is
  3724. like (*MARK:NAME) in that the name is remembered for passing back to the
  3725. caller. However, (*SKIP:NAME) searches only for names set with (*MARK),
  3726. ignoring those set by other backtracking verbs.
  3727. .P
  3728. A group that does not contain a | character is just a part of the enclosing
  3729. alternative; it is not a nested alternation with only one alternative. The
  3730. effect of (*THEN) extends beyond such a group to the enclosing alternative.
  3731. Consider this pattern, where A, B, etc. are complex pattern fragments that do
  3732. not contain any | characters at this level:
  3733. .sp
  3734. A (B(*THEN)C) | D
  3735. .sp
  3736. If A and B are matched, but there is a failure in C, matching does not
  3737. backtrack into A; instead it moves to the next alternative, that is, D.
  3738. However, if the group containing (*THEN) is given an alternative, it
  3739. behaves differently:
  3740. .sp
  3741. A (B(*THEN)C | (*FAIL)) | D
  3742. .sp
  3743. The effect of (*THEN) is now confined to the inner group. After a failure in C,
  3744. matching moves to (*FAIL), which causes the whole group to fail because there
  3745. are no more alternatives to try. In this case, matching does backtrack into A.
  3746. .P
  3747. Note that a conditional group is not considered as having two alternatives,
  3748. because only one is ever used. In other words, the | character in a conditional
  3749. group has a different meaning. Ignoring white space, consider:
  3750. .sp
  3751. ^.*? (?(?=a) a | b(*THEN)c )
  3752. .sp
  3753. If the subject is "ba", this pattern does not match. Because .*? is ungreedy,
  3754. it initially matches zero characters. The condition (?=a) then fails, the
  3755. character "b" is matched, but "c" is not. At this point, matching does not
  3756. backtrack to .*? as might perhaps be expected from the presence of the |
  3757. character. The conditional group is part of the single alternative that
  3758. comprises the whole pattern, and so the match fails. (If there was a backtrack
  3759. into .*?, allowing it to match "b", the match would succeed.)
  3760. .P
  3761. The verbs just described provide four different "strengths" of control when
  3762. subsequent matching fails. (*THEN) is the weakest, carrying on the match at the
  3763. next alternative. (*PRUNE) comes next, failing the match at the current
  3764. starting position, but allowing an advance to the next character (for an
  3765. unanchored pattern). (*SKIP) is similar, except that the advance may be more
  3766. than one character. (*COMMIT) is the strongest, causing the entire match to
  3767. fail.
  3768. .
  3769. .
  3770. .SS "More than one backtracking verb"
  3771. .rs
  3772. .sp
  3773. If more than one backtracking verb is present in a pattern, the one that is
  3774. backtracked onto first acts. For example, consider this pattern, where A, B,
  3775. etc. are complex pattern fragments:
  3776. .sp
  3777. (A(*COMMIT)B(*THEN)C|ABD)
  3778. .sp
  3779. If A matches but B fails, the backtrack to (*COMMIT) causes the entire match to
  3780. fail. However, if A and B match, but C fails, the backtrack to (*THEN) causes
  3781. the next alternative (ABD) to be tried. This behaviour is consistent, but is
  3782. not always the same as Perl's. It means that if two or more backtracking verbs
  3783. appear in succession, all the the last of them has no effect. Consider this
  3784. example:
  3785. .sp
  3786. ...(*COMMIT)(*PRUNE)...
  3787. .sp
  3788. If there is a matching failure to the right, backtracking onto (*PRUNE) causes
  3789. it to be triggered, and its action is taken. There can never be a backtrack
  3790. onto (*COMMIT).
  3791. .
  3792. .
  3793. .\" HTML <a name="btrepeat"></a>
  3794. .SS "Backtracking verbs in repeated groups"
  3795. .rs
  3796. .sp
  3797. PCRE2 sometimes differs from Perl in its handling of backtracking verbs in
  3798. repeated groups. For example, consider:
  3799. .sp
  3800. /(a(*COMMIT)b)+ac/
  3801. .sp
  3802. If the subject is "abac", Perl matches unless its optimizations are disabled,
  3803. but PCRE2 always fails because the (*COMMIT) in the second repeat of the group
  3804. acts.
  3805. .
  3806. .
  3807. .\" HTML <a name="btassert"></a>
  3808. .SS "Backtracking verbs in assertions"
  3809. .rs
  3810. .sp
  3811. (*FAIL) in any assertion has its normal effect: it forces an immediate
  3812. backtrack. The behaviour of the other backtracking verbs depends on whether or
  3813. not the assertion is standalone or acting as the condition in a conditional
  3814. group.
  3815. .P
  3816. (*ACCEPT) in a standalone positive assertion causes the assertion to succeed
  3817. without any further processing; captured strings and a mark name (if set) are
  3818. retained. In a standalone negative assertion, (*ACCEPT) causes the assertion to
  3819. fail without any further processing; captured substrings and any mark name are
  3820. discarded.
  3821. .P
  3822. If the assertion is a condition, (*ACCEPT) causes the condition to be true for
  3823. a positive assertion and false for a negative one; captured substrings are
  3824. retained in both cases.
  3825. .P
  3826. The remaining verbs act only when a later failure causes a backtrack to
  3827. reach them. This means that, for the Perl-compatible assertions, their effect
  3828. is confined to the assertion, because Perl lookaround assertions are atomic. A
  3829. backtrack that occurs after such an assertion is complete does not jump back
  3830. into the assertion. Note in particular that a (*MARK) name that is set in an
  3831. assertion is not "seen" by an instance of (*SKIP:NAME) later in the pattern.
  3832. .P
  3833. PCRE2 now supports non-atomic positive assertions, as described in the section
  3834. entitled
  3835. .\" HTML <a href="#nonatomicassertions">
  3836. .\" </a>
  3837. "Non-atomic assertions"
  3838. .\"
  3839. above. These assertions must be standalone (not used as conditions). They are
  3840. not Perl-compatible. For these assertions, a later backtrack does jump back
  3841. into the assertion, and therefore verbs such as (*COMMIT) can be triggered by
  3842. backtracks from later in the pattern.
  3843. .P
  3844. The effect of (*THEN) is not allowed to escape beyond an assertion. If there
  3845. are no more branches to try, (*THEN) causes a positive assertion to be false,
  3846. and a negative assertion to be true.
  3847. .P
  3848. The other backtracking verbs are not treated specially if they appear in a
  3849. standalone positive assertion. In a conditional positive assertion,
  3850. backtracking (from within the assertion) into (*COMMIT), (*SKIP), or (*PRUNE)
  3851. causes the condition to be false. However, for both standalone and conditional
  3852. negative assertions, backtracking into (*COMMIT), (*SKIP), or (*PRUNE) causes
  3853. the assertion to be true, without considering any further alternative branches.
  3854. .
  3855. .
  3856. .\" HTML <a name="btsub"></a>
  3857. .SS "Backtracking verbs in subroutines"
  3858. .rs
  3859. .sp
  3860. These behaviours occur whether or not the group is called recursively.
  3861. .P
  3862. (*ACCEPT) in a group called as a subroutine causes the subroutine match to
  3863. succeed without any further processing. Matching then continues after the
  3864. subroutine call. Perl documents this behaviour. Perl's treatment of the other
  3865. verbs in subroutines is different in some cases.
  3866. .P
  3867. (*FAIL) in a group called as a subroutine has its normal effect: it forces
  3868. an immediate backtrack.
  3869. .P
  3870. (*COMMIT), (*SKIP), and (*PRUNE) cause the subroutine match to fail when
  3871. triggered by being backtracked to in a group called as a subroutine. There is
  3872. then a backtrack at the outer level.
  3873. .P
  3874. (*THEN), when triggered, skips to the next alternative in the innermost
  3875. enclosing group that has alternatives (its normal behaviour). However, if there
  3876. is no such group within the subroutine's group, the subroutine match fails and
  3877. there is a backtrack at the outer level.
  3878. .
  3879. .
  3880. .SH "SEE ALSO"
  3881. .rs
  3882. .sp
  3883. \fBpcre2api\fP(3), \fBpcre2callout\fP(3), \fBpcre2matching\fP(3),
  3884. \fBpcre2syntax\fP(3), \fBpcre2\fP(3).
  3885. .
  3886. .
  3887. .SH AUTHOR
  3888. .rs
  3889. .sp
  3890. .nf
  3891. Philip Hazel
  3892. University Computing Service
  3893. Cambridge, England.
  3894. .fi
  3895. .
  3896. .
  3897. .SH REVISION
  3898. .rs
  3899. .sp
  3900. .nf
  3901. Last updated: 06 October 2020
  3902. Copyright (c) 1997-2020 University of Cambridge.
  3903. .fi