pcre2pattern.html 168 KB

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