pcre2pattern.3 159 KB

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