pcre2pattern.html 163 KB

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