pcre2api.html 190 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170
  1. <html>
  2. <head>
  3. <title>pcre2api specification</title>
  4. </head>
  5. <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
  6. <h1>pcre2api man page</h1>
  7. <p>
  8. Return to the <a href="index.html">PCRE2 index page</a>.
  9. </p>
  10. <p>
  11. This page is part of the PCRE2 HTML documentation. It was generated
  12. automatically from the original man page. If there is any nonsense in it,
  13. please consult the man page, in case the conversion went wrong.
  14. <br>
  15. <ul>
  16. <li><a name="TOC1" href="#SEC1">PCRE2 NATIVE API BASIC FUNCTIONS</a>
  17. <li><a name="TOC2" href="#SEC2">PCRE2 NATIVE API AUXILIARY MATCH FUNCTIONS</a>
  18. <li><a name="TOC3" href="#SEC3">PCRE2 NATIVE API GENERAL CONTEXT FUNCTIONS</a>
  19. <li><a name="TOC4" href="#SEC4">PCRE2 NATIVE API COMPILE CONTEXT FUNCTIONS</a>
  20. <li><a name="TOC5" href="#SEC5">PCRE2 NATIVE API MATCH CONTEXT FUNCTIONS</a>
  21. <li><a name="TOC6" href="#SEC6">PCRE2 NATIVE API STRING EXTRACTION FUNCTIONS</a>
  22. <li><a name="TOC7" href="#SEC7">PCRE2 NATIVE API STRING SUBSTITUTION FUNCTION</a>
  23. <li><a name="TOC8" href="#SEC8">PCRE2 NATIVE API JIT FUNCTIONS</a>
  24. <li><a name="TOC9" href="#SEC9">PCRE2 NATIVE API SERIALIZATION FUNCTIONS</a>
  25. <li><a name="TOC10" href="#SEC10">PCRE2 NATIVE API AUXILIARY FUNCTIONS</a>
  26. <li><a name="TOC11" href="#SEC11">PCRE2 NATIVE API OBSOLETE FUNCTIONS</a>
  27. <li><a name="TOC12" href="#SEC12">PCRE2 EXPERIMENTAL PATTERN CONVERSION FUNCTIONS</a>
  28. <li><a name="TOC13" href="#SEC13">PCRE2 8-BIT, 16-BIT, AND 32-BIT LIBRARIES</a>
  29. <li><a name="TOC14" href="#SEC14">PCRE2 API OVERVIEW</a>
  30. <li><a name="TOC15" href="#SEC15">STRING LENGTHS AND OFFSETS</a>
  31. <li><a name="TOC16" href="#SEC16">NEWLINES</a>
  32. <li><a name="TOC17" href="#SEC17">MULTITHREADING</a>
  33. <li><a name="TOC18" href="#SEC18">PCRE2 CONTEXTS</a>
  34. <li><a name="TOC19" href="#SEC19">CHECKING BUILD-TIME OPTIONS</a>
  35. <li><a name="TOC20" href="#SEC20">COMPILING A PATTERN</a>
  36. <li><a name="TOC21" href="#SEC21">JUST-IN-TIME (JIT) COMPILATION</a>
  37. <li><a name="TOC22" href="#SEC22">LOCALE SUPPORT</a>
  38. <li><a name="TOC23" href="#SEC23">INFORMATION ABOUT A COMPILED PATTERN</a>
  39. <li><a name="TOC24" href="#SEC24">INFORMATION ABOUT A PATTERN'S CALLOUTS</a>
  40. <li><a name="TOC25" href="#SEC25">SERIALIZATION AND PRECOMPILING</a>
  41. <li><a name="TOC26" href="#SEC26">THE MATCH DATA BLOCK</a>
  42. <li><a name="TOC27" href="#SEC27">MEMORY USE FOR MATCH DATA BLOCKS</a>
  43. <li><a name="TOC28" href="#SEC28">MATCHING A PATTERN: THE TRADITIONAL FUNCTION</a>
  44. <li><a name="TOC29" href="#SEC29">NEWLINE HANDLING WHEN MATCHING</a>
  45. <li><a name="TOC30" href="#SEC30">HOW PCRE2_MATCH() RETURNS A STRING AND CAPTURED SUBSTRINGS</a>
  46. <li><a name="TOC31" href="#SEC31">OTHER INFORMATION ABOUT A MATCH</a>
  47. <li><a name="TOC32" href="#SEC32">ERROR RETURNS FROM <b>pcre2_match()</b></a>
  48. <li><a name="TOC33" href="#SEC33">OBTAINING A TEXTUAL ERROR MESSAGE</a>
  49. <li><a name="TOC34" href="#SEC34">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a>
  50. <li><a name="TOC35" href="#SEC35">EXTRACTING A LIST OF ALL CAPTURED SUBSTRINGS</a>
  51. <li><a name="TOC36" href="#SEC36">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a>
  52. <li><a name="TOC37" href="#SEC37">CREATING A NEW STRING WITH SUBSTITUTIONS</a>
  53. <li><a name="TOC38" href="#SEC38">DUPLICATE CAPTURE GROUP NAMES</a>
  54. <li><a name="TOC39" href="#SEC39">FINDING ALL POSSIBLE MATCHES AT ONE POSITION</a>
  55. <li><a name="TOC40" href="#SEC40">MATCHING A PATTERN: THE ALTERNATIVE FUNCTION</a>
  56. <li><a name="TOC41" href="#SEC41">SEE ALSO</a>
  57. <li><a name="TOC42" href="#SEC42">AUTHOR</a>
  58. <li><a name="TOC43" href="#SEC43">REVISION</a>
  59. </ul>
  60. <P>
  61. <b>#include &#60;pcre2.h&#62;</b>
  62. <br>
  63. <br>
  64. PCRE2 is a new API for PCRE, starting at release 10.0. This document contains a
  65. description of all its native functions. See the
  66. <a href="pcre2.html"><b>pcre2</b></a>
  67. document for an overview of all the PCRE2 documentation.
  68. </P>
  69. <br><a name="SEC1" href="#TOC1">PCRE2 NATIVE API BASIC FUNCTIONS</a><br>
  70. <P>
  71. <b>pcre2_code *pcre2_compile(PCRE2_SPTR <i>pattern</i>, PCRE2_SIZE <i>length</i>,</b>
  72. <b> uint32_t <i>options</i>, int *<i>errorcode</i>, PCRE2_SIZE *<i>erroroffset,</i></b>
  73. <b> pcre2_compile_context *<i>ccontext</i>);</b>
  74. <br>
  75. <br>
  76. <b>void pcre2_code_free(pcre2_code *<i>code</i>);</b>
  77. <br>
  78. <br>
  79. <b>pcre2_match_data *pcre2_match_data_create(uint32_t <i>ovecsize</i>,</b>
  80. <b> pcre2_general_context *<i>gcontext</i>);</b>
  81. <br>
  82. <br>
  83. <b>pcre2_match_data *pcre2_match_data_create_from_pattern(</b>
  84. <b> const pcre2_code *<i>code</i>, pcre2_general_context *<i>gcontext</i>);</b>
  85. <br>
  86. <br>
  87. <b>int pcre2_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  88. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  89. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  90. <b> pcre2_match_context *<i>mcontext</i>);</b>
  91. <br>
  92. <br>
  93. <b>int pcre2_dfa_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  94. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  95. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  96. <b> pcre2_match_context *<i>mcontext</i>,</b>
  97. <b> int *<i>workspace</i>, PCRE2_SIZE <i>wscount</i>);</b>
  98. <br>
  99. <br>
  100. <b>void pcre2_match_data_free(pcre2_match_data *<i>match_data</i>);</b>
  101. </P>
  102. <br><a name="SEC2" href="#TOC1">PCRE2 NATIVE API AUXILIARY MATCH FUNCTIONS</a><br>
  103. <P>
  104. <b>PCRE2_SPTR pcre2_get_mark(pcre2_match_data *<i>match_data</i>);</b>
  105. <br>
  106. <br>
  107. <b>PCRE2_SIZE pcre2_get_match_data_size(pcre2_match_data *<i>match_data</i>);</b>
  108. <br>
  109. <br>
  110. <b>PCRE2_SIZE pcre2_get_match_data_heapframes_size(</b>
  111. <b> pcre2_match_data *<i>match_data</i>);</b>
  112. <br>
  113. <br>
  114. <b>uint32_t pcre2_get_ovector_count(pcre2_match_data *<i>match_data</i>);</b>
  115. <br>
  116. <br>
  117. <b>PCRE2_SIZE *pcre2_get_ovector_pointer(pcre2_match_data *<i>match_data</i>);</b>
  118. <br>
  119. <br>
  120. <b>PCRE2_SIZE pcre2_get_startchar(pcre2_match_data *<i>match_data</i>);</b>
  121. </P>
  122. <br><a name="SEC3" href="#TOC1">PCRE2 NATIVE API GENERAL CONTEXT FUNCTIONS</a><br>
  123. <P>
  124. <b>pcre2_general_context *pcre2_general_context_create(</b>
  125. <b> void *(*<i>private_malloc</i>)(PCRE2_SIZE, void *),</b>
  126. <b> void (*<i>private_free</i>)(void *, void *), void *<i>memory_data</i>);</b>
  127. <br>
  128. <br>
  129. <b>pcre2_general_context *pcre2_general_context_copy(</b>
  130. <b> pcre2_general_context *<i>gcontext</i>);</b>
  131. <br>
  132. <br>
  133. <b>void pcre2_general_context_free(pcre2_general_context *<i>gcontext</i>);</b>
  134. </P>
  135. <br><a name="SEC4" href="#TOC1">PCRE2 NATIVE API COMPILE CONTEXT FUNCTIONS</a><br>
  136. <P>
  137. <b>pcre2_compile_context *pcre2_compile_context_create(</b>
  138. <b> pcre2_general_context *<i>gcontext</i>);</b>
  139. <br>
  140. <br>
  141. <b>pcre2_compile_context *pcre2_compile_context_copy(</b>
  142. <b> pcre2_compile_context *<i>ccontext</i>);</b>
  143. <br>
  144. <br>
  145. <b>void pcre2_compile_context_free(pcre2_compile_context *<i>ccontext</i>);</b>
  146. <br>
  147. <br>
  148. <b>int pcre2_set_bsr(pcre2_compile_context *<i>ccontext</i>,</b>
  149. <b> uint32_t <i>value</i>);</b>
  150. <br>
  151. <br>
  152. <b>int pcre2_set_character_tables(pcre2_compile_context *<i>ccontext</i>,</b>
  153. <b> const uint8_t *<i>tables</i>);</b>
  154. <br>
  155. <br>
  156. <b>int pcre2_set_compile_extra_options(pcre2_compile_context *<i>ccontext</i>,</b>
  157. <b> uint32_t <i>extra_options</i>);</b>
  158. <br>
  159. <br>
  160. <b>int pcre2_set_max_pattern_length(pcre2_compile_context *<i>ccontext</i>,</b>
  161. <b> PCRE2_SIZE <i>value</i>);</b>
  162. <br>
  163. <br>
  164. <b>int pcre2_set_max_varlookbehind(pcre2_compile_contest *<i>ccontext</i>,</b>
  165. <b>" uint32_t <i>value</i>);</b>
  166. <br>
  167. <br>
  168. <b>int pcre2_set_newline(pcre2_compile_context *<i>ccontext</i>,</b>
  169. <b> uint32_t <i>value</i>);</b>
  170. <br>
  171. <br>
  172. <b>int pcre2_set_parens_nest_limit(pcre2_compile_context *<i>ccontext</i>,</b>
  173. <b> uint32_t <i>value</i>);</b>
  174. <br>
  175. <br>
  176. <b>int pcre2_set_compile_recursion_guard(pcre2_compile_context *<i>ccontext</i>,</b>
  177. <b> int (*<i>guard_function</i>)(uint32_t, void *), void *<i>user_data</i>);</b>
  178. </P>
  179. <br><a name="SEC5" href="#TOC1">PCRE2 NATIVE API MATCH CONTEXT FUNCTIONS</a><br>
  180. <P>
  181. <b>pcre2_match_context *pcre2_match_context_create(</b>
  182. <b> pcre2_general_context *<i>gcontext</i>);</b>
  183. <br>
  184. <br>
  185. <b>pcre2_match_context *pcre2_match_context_copy(</b>
  186. <b> pcre2_match_context *<i>mcontext</i>);</b>
  187. <br>
  188. <br>
  189. <b>void pcre2_match_context_free(pcre2_match_context *<i>mcontext</i>);</b>
  190. <br>
  191. <br>
  192. <b>int pcre2_set_callout(pcre2_match_context *<i>mcontext</i>,</b>
  193. <b> int (*<i>callout_function</i>)(pcre2_callout_block *, void *),</b>
  194. <b> void *<i>callout_data</i>);</b>
  195. <br>
  196. <br>
  197. <b>int pcre2_set_substitute_callout(pcre2_match_context *<i>mcontext</i>,</b>
  198. <b> int (*<i>callout_function</i>)(pcre2_substitute_callout_block *, void *),</b>
  199. <b> void *<i>callout_data</i>);</b>
  200. <br>
  201. <br>
  202. <b>int pcre2_set_offset_limit(pcre2_match_context *<i>mcontext</i>,</b>
  203. <b> PCRE2_SIZE <i>value</i>);</b>
  204. <br>
  205. <br>
  206. <b>int pcre2_set_heap_limit(pcre2_match_context *<i>mcontext</i>,</b>
  207. <b> uint32_t <i>value</i>);</b>
  208. <br>
  209. <br>
  210. <b>int pcre2_set_match_limit(pcre2_match_context *<i>mcontext</i>,</b>
  211. <b> uint32_t <i>value</i>);</b>
  212. <br>
  213. <br>
  214. <b>int pcre2_set_depth_limit(pcre2_match_context *<i>mcontext</i>,</b>
  215. <b> uint32_t <i>value</i>);</b>
  216. </P>
  217. <br><a name="SEC6" href="#TOC1">PCRE2 NATIVE API STRING EXTRACTION FUNCTIONS</a><br>
  218. <P>
  219. <b>int pcre2_substring_copy_byname(pcre2_match_data *<i>match_data</i>,</b>
  220. <b> PCRE2_SPTR <i>name</i>, PCRE2_UCHAR *<i>buffer</i>, PCRE2_SIZE *<i>bufflen</i>);</b>
  221. <br>
  222. <br>
  223. <b>int pcre2_substring_copy_bynumber(pcre2_match_data *<i>match_data</i>,</b>
  224. <b> uint32_t <i>number</i>, PCRE2_UCHAR *<i>buffer</i>,</b>
  225. <b> PCRE2_SIZE *<i>bufflen</i>);</b>
  226. <br>
  227. <br>
  228. <b>void pcre2_substring_free(PCRE2_UCHAR *<i>buffer</i>);</b>
  229. <br>
  230. <br>
  231. <b>int pcre2_substring_get_byname(pcre2_match_data *<i>match_data</i>,</b>
  232. <b> PCRE2_SPTR <i>name</i>, PCRE2_UCHAR **<i>bufferptr</i>, PCRE2_SIZE *<i>bufflen</i>);</b>
  233. <br>
  234. <br>
  235. <b>int pcre2_substring_get_bynumber(pcre2_match_data *<i>match_data</i>,</b>
  236. <b> uint32_t <i>number</i>, PCRE2_UCHAR **<i>bufferptr</i>,</b>
  237. <b> PCRE2_SIZE *<i>bufflen</i>);</b>
  238. <br>
  239. <br>
  240. <b>int pcre2_substring_length_byname(pcre2_match_data *<i>match_data</i>,</b>
  241. <b> PCRE2_SPTR <i>name</i>, PCRE2_SIZE *<i>length</i>);</b>
  242. <br>
  243. <br>
  244. <b>int pcre2_substring_length_bynumber(pcre2_match_data *<i>match_data</i>,</b>
  245. <b> uint32_t <i>number</i>, PCRE2_SIZE *<i>length</i>);</b>
  246. <br>
  247. <br>
  248. <b>int pcre2_substring_nametable_scan(const pcre2_code *<i>code</i>,</b>
  249. <b> PCRE2_SPTR <i>name</i>, PCRE2_SPTR *<i>first</i>, PCRE2_SPTR *<i>last</i>);</b>
  250. <br>
  251. <br>
  252. <b>int pcre2_substring_number_from_name(const pcre2_code *<i>code</i>,</b>
  253. <b> PCRE2_SPTR <i>name</i>);</b>
  254. <br>
  255. <br>
  256. <b>void pcre2_substring_list_free(PCRE2_UCHAR **<i>list</i>);</b>
  257. <br>
  258. <br>
  259. <b>int pcre2_substring_list_get(pcre2_match_data *<i>match_data</i>,</b>
  260. <b>" PCRE2_UCHAR ***<i>listptr</i>, PCRE2_SIZE **<i>lengthsptr</i>);</b>
  261. </P>
  262. <br><a name="SEC7" href="#TOC1">PCRE2 NATIVE API STRING SUBSTITUTION FUNCTION</a><br>
  263. <P>
  264. <b>int pcre2_substitute(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  265. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  266. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  267. <b> pcre2_match_context *<i>mcontext</i>, PCRE2_SPTR <i>replacementz</i>,</b>
  268. <b> PCRE2_SIZE <i>rlength</i>, PCRE2_UCHAR *<i>outputbuffer</i>,</b>
  269. <b> PCRE2_SIZE *<i>outlengthptr</i>);</b>
  270. </P>
  271. <br><a name="SEC8" href="#TOC1">PCRE2 NATIVE API JIT FUNCTIONS</a><br>
  272. <P>
  273. <b>int pcre2_jit_compile(pcre2_code *<i>code</i>, uint32_t <i>options</i>);</b>
  274. <br>
  275. <br>
  276. <b>int pcre2_jit_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  277. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  278. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  279. <b> pcre2_match_context *<i>mcontext</i>);</b>
  280. <br>
  281. <br>
  282. <b>void pcre2_jit_free_unused_memory(pcre2_general_context *<i>gcontext</i>);</b>
  283. <br>
  284. <br>
  285. <b>pcre2_jit_stack *pcre2_jit_stack_create(size_t <i>startsize</i>,</b>
  286. <b> size_t <i>maxsize</i>, pcre2_general_context *<i>gcontext</i>);</b>
  287. <br>
  288. <br>
  289. <b>void pcre2_jit_stack_assign(pcre2_match_context *<i>mcontext</i>,</b>
  290. <b> pcre2_jit_callback <i>callback_function</i>, void *<i>callback_data</i>);</b>
  291. <br>
  292. <br>
  293. <b>void pcre2_jit_stack_free(pcre2_jit_stack *<i>jit_stack</i>);</b>
  294. </P>
  295. <br><a name="SEC9" href="#TOC1">PCRE2 NATIVE API SERIALIZATION FUNCTIONS</a><br>
  296. <P>
  297. <b>int32_t pcre2_serialize_decode(pcre2_code **<i>codes</i>,</b>
  298. <b> int32_t <i>number_of_codes</i>, const uint8_t *<i>bytes</i>,</b>
  299. <b> pcre2_general_context *<i>gcontext</i>);</b>
  300. <br>
  301. <br>
  302. <b>int32_t pcre2_serialize_encode(const pcre2_code **<i>codes</i>,</b>
  303. <b> int32_t <i>number_of_codes</i>, uint8_t **<i>serialized_bytes</i>,</b>
  304. <b> PCRE2_SIZE *<i>serialized_size</i>, pcre2_general_context *<i>gcontext</i>);</b>
  305. <br>
  306. <br>
  307. <b>void pcre2_serialize_free(uint8_t *<i>bytes</i>);</b>
  308. <br>
  309. <br>
  310. <b>int32_t pcre2_serialize_get_number_of_codes(const uint8_t *<i>bytes</i>);</b>
  311. </P>
  312. <br><a name="SEC10" href="#TOC1">PCRE2 NATIVE API AUXILIARY FUNCTIONS</a><br>
  313. <P>
  314. <b>pcre2_code *pcre2_code_copy(const pcre2_code *<i>code</i>);</b>
  315. <br>
  316. <br>
  317. <b>pcre2_code *pcre2_code_copy_with_tables(const pcre2_code *<i>code</i>);</b>
  318. <br>
  319. <br>
  320. <b>int pcre2_get_error_message(int <i>errorcode</i>, PCRE2_UCHAR *<i>buffer</i>,</b>
  321. <b> PCRE2_SIZE <i>bufflen</i>);</b>
  322. <br>
  323. <br>
  324. <b>const uint8_t *pcre2_maketables(pcre2_general_context *<i>gcontext</i>);</b>
  325. <br>
  326. <br>
  327. <b>void pcre2_maketables_free(pcre2_general_context *<i>gcontext</i>,</b>
  328. <b> const uint8_t *<i>tables</i>);</b>
  329. <br>
  330. <br>
  331. <b>int pcre2_pattern_info(const pcre2_code *<i>code</i>, uint32_t <i>what</i>,</b>
  332. <b> void *<i>where</i>);</b>
  333. <br>
  334. <br>
  335. <b>int pcre2_callout_enumerate(const pcre2_code *<i>code</i>,</b>
  336. <b> int (*<i>callback</i>)(pcre2_callout_enumerate_block *, void *),</b>
  337. <b> void *<i>user_data</i>);</b>
  338. <br>
  339. <br>
  340. <b>int pcre2_config(uint32_t <i>what</i>, void *<i>where</i>);</b>
  341. </P>
  342. <br><a name="SEC11" href="#TOC1">PCRE2 NATIVE API OBSOLETE FUNCTIONS</a><br>
  343. <P>
  344. <b>int pcre2_set_recursion_limit(pcre2_match_context *<i>mcontext</i>,</b>
  345. <b> uint32_t <i>value</i>);</b>
  346. <br>
  347. <br>
  348. <b>int pcre2_set_recursion_memory_management(</b>
  349. <b> pcre2_match_context *<i>mcontext</i>,</b>
  350. <b> void *(*<i>private_malloc</i>)(size_t, void *),</b>
  351. <b> void (*<i>private_free</i>)(void *, void *), void *<i>memory_data</i>);</b>
  352. <br>
  353. <br>
  354. These functions became obsolete at release 10.30 and are retained only for
  355. backward compatibility. They should not be used in new code. The first is
  356. replaced by <b>pcre2_set_depth_limit()</b>; the second is no longer needed and
  357. has no effect (it always returns zero).
  358. </P>
  359. <br><a name="SEC12" href="#TOC1">PCRE2 EXPERIMENTAL PATTERN CONVERSION FUNCTIONS</a><br>
  360. <P>
  361. <b>pcre2_convert_context *pcre2_convert_context_create(</b>
  362. <b> pcre2_general_context *<i>gcontext</i>);</b>
  363. <br>
  364. <br>
  365. <b>pcre2_convert_context *pcre2_convert_context_copy(</b>
  366. <b> pcre2_convert_context *<i>cvcontext</i>);</b>
  367. <br>
  368. <br>
  369. <b>void pcre2_convert_context_free(pcre2_convert_context *<i>cvcontext</i>);</b>
  370. <br>
  371. <br>
  372. <b>int pcre2_set_glob_escape(pcre2_convert_context *<i>cvcontext</i>,</b>
  373. <b> uint32_t <i>escape_char</i>);</b>
  374. <br>
  375. <br>
  376. <b>int pcre2_set_glob_separator(pcre2_convert_context *<i>cvcontext</i>,</b>
  377. <b> uint32_t <i>separator_char</i>);</b>
  378. <br>
  379. <br>
  380. <b>int pcre2_pattern_convert(PCRE2_SPTR <i>pattern</i>, PCRE2_SIZE <i>length</i>,</b>
  381. <b> uint32_t <i>options</i>, PCRE2_UCHAR **<i>buffer</i>,</b>
  382. <b> PCRE2_SIZE *<i>blength</i>, pcre2_convert_context *<i>cvcontext</i>);</b>
  383. <br>
  384. <br>
  385. <b>void pcre2_converted_pattern_free(PCRE2_UCHAR *<i>converted_pattern</i>);</b>
  386. <br>
  387. <br>
  388. These functions provide a way of converting non-PCRE2 patterns into
  389. patterns that can be processed by <b>pcre2_compile()</b>. This facility is
  390. experimental and may be changed in future releases. At present, "globs" and
  391. POSIX basic and extended patterns can be converted. Details are given in the
  392. <a href="pcre2convert.html"><b>pcre2convert</b></a>
  393. documentation.
  394. </P>
  395. <br><a name="SEC13" href="#TOC1">PCRE2 8-BIT, 16-BIT, AND 32-BIT LIBRARIES</a><br>
  396. <P>
  397. There are three PCRE2 libraries, supporting 8-bit, 16-bit, and 32-bit code
  398. units, respectively. However, there is just one header file, <b>pcre2.h</b>.
  399. This contains the function prototypes and other definitions for all three
  400. libraries. One, two, or all three can be installed simultaneously. On Unix-like
  401. systems the libraries are called <b>libpcre2-8</b>, <b>libpcre2-16</b>, and
  402. <b>libpcre2-32</b>, and they can also co-exist with the original PCRE libraries.
  403. Every PCRE2 function comes in three different forms, one for each library, for
  404. example:
  405. <pre>
  406. <b>pcre2_compile_8()</b>
  407. <b>pcre2_compile_16()</b>
  408. <b>pcre2_compile_32()</b>
  409. </pre>
  410. There are also three different sets of data types:
  411. <pre>
  412. <b>PCRE2_UCHAR8, PCRE2_UCHAR16, PCRE2_UCHAR32</b>
  413. <b>PCRE2_SPTR8, PCRE2_SPTR16, PCRE2_SPTR32</b>
  414. </pre>
  415. The UCHAR types define unsigned code units of the appropriate widths.
  416. For example, PCRE2_UCHAR16 is usually defined as `uint16_t'.
  417. The SPTR types are pointers to constants of the equivalent UCHAR types,
  418. that is, they are pointers to vectors of unsigned code units.
  419. </P>
  420. <P>
  421. Character strings are passed to a PCRE2 library as sequences of unsigned
  422. integers in code units of the appropriate width. The length of a string may
  423. be given as a number of code units, or the string may be specified as
  424. zero-terminated.
  425. </P>
  426. <P>
  427. Many applications use only one code unit width. For their convenience, macros
  428. are defined whose names are the generic forms such as <b>pcre2_compile()</b> and
  429. PCRE2_SPTR. These macros use the value of the macro PCRE2_CODE_UNIT_WIDTH to
  430. generate the appropriate width-specific function and macro names.
  431. PCRE2_CODE_UNIT_WIDTH is not defined by default. An application must define it
  432. to be 8, 16, or 32 before including <b>pcre2.h</b> in order to make use of the
  433. generic names.
  434. </P>
  435. <P>
  436. Applications that use more than one code unit width can be linked with more
  437. than one PCRE2 library, but must define PCRE2_CODE_UNIT_WIDTH to be 0 before
  438. including <b>pcre2.h</b>, and then use the real function names. Any code that is
  439. to be included in an environment where the value of PCRE2_CODE_UNIT_WIDTH is
  440. unknown should also use the real function names. (Unfortunately, it is not
  441. possible in C code to save and restore the value of a macro.)
  442. </P>
  443. <P>
  444. If PCRE2_CODE_UNIT_WIDTH is not defined before including <b>pcre2.h</b>, a
  445. compiler error occurs.
  446. </P>
  447. <P>
  448. When using multiple libraries in an application, you must take care when
  449. processing any particular pattern to use only functions from a single library.
  450. For example, if you want to run a match using a pattern that was compiled with
  451. <b>pcre2_compile_16()</b>, you must do so with <b>pcre2_match_16()</b>, not
  452. <b>pcre2_match_8()</b> or <b>pcre2_match_32()</b>.
  453. </P>
  454. <P>
  455. In the function summaries above, and in the rest of this document and other
  456. PCRE2 documents, functions and data types are described using their generic
  457. names, without the _8, _16, or _32 suffix.
  458. </P>
  459. <br><a name="SEC14" href="#TOC1">PCRE2 API OVERVIEW</a><br>
  460. <P>
  461. PCRE2 has its own native API, which is described in this document. There are
  462. also some wrapper functions for the 8-bit library that correspond to the
  463. POSIX regular expression API, but they do not give access to all the
  464. functionality of PCRE2 and they are not thread-safe. They are described in the
  465. <a href="pcre2posix.html"><b>pcre2posix</b></a>
  466. documentation. Both these APIs define a set of C function calls.
  467. </P>
  468. <P>
  469. The native API C data types, function prototypes, option values, and error
  470. codes are defined in the header file <b>pcre2.h</b>, which also contains
  471. definitions of PCRE2_MAJOR and PCRE2_MINOR, the major and minor release numbers
  472. for the library. Applications can use these to include support for different
  473. releases of PCRE2.
  474. </P>
  475. <P>
  476. In a Windows environment, if you want to statically link an application program
  477. against a non-dll PCRE2 library, you must define PCRE2_STATIC before including
  478. <b>pcre2.h</b>.
  479. </P>
  480. <P>
  481. The functions <b>pcre2_compile()</b> and <b>pcre2_match()</b> are used for
  482. compiling and matching regular expressions in a Perl-compatible manner. A
  483. sample program that demonstrates the simplest way of using them is provided in
  484. the file called <i>pcre2demo.c</i> in the PCRE2 source distribution. A listing
  485. of this program is given in the
  486. <a href="pcre2demo.html"><b>pcre2demo</b></a>
  487. documentation, and the
  488. <a href="pcre2sample.html"><b>pcre2sample</b></a>
  489. documentation describes how to compile and run it.
  490. </P>
  491. <P>
  492. The compiling and matching functions recognize various options that are passed
  493. as bits in an options argument. There are also some more complicated parameters
  494. such as custom memory management functions and resource limits that are passed
  495. in "contexts" (which are just memory blocks, described below). Simple
  496. applications do not need to make use of contexts.
  497. </P>
  498. <P>
  499. Just-in-time (JIT) compiler support is an optional feature of PCRE2 that can be
  500. built in appropriate hardware environments. It greatly speeds up the matching
  501. performance of many patterns. Programs can request that it be used if
  502. available by calling <b>pcre2_jit_compile()</b> after a pattern has been
  503. successfully compiled by <b>pcre2_compile()</b>. This does nothing if JIT
  504. support is not available.
  505. </P>
  506. <P>
  507. More complicated programs might need to make use of the specialist functions
  508. <b>pcre2_jit_stack_create()</b>, <b>pcre2_jit_stack_free()</b>, and
  509. <b>pcre2_jit_stack_assign()</b> in order to control the JIT code's memory usage.
  510. </P>
  511. <P>
  512. JIT matching is automatically used by <b>pcre2_match()</b> if it is available,
  513. unless the PCRE2_NO_JIT option is set. There is also a direct interface for JIT
  514. matching, which gives improved performance at the expense of less sanity
  515. checking. The JIT-specific functions are discussed in the
  516. <a href="pcre2jit.html"><b>pcre2jit</b></a>
  517. documentation.
  518. </P>
  519. <P>
  520. A second matching function, <b>pcre2_dfa_match()</b>, which is not
  521. Perl-compatible, is also provided. This uses a different algorithm for the
  522. matching. The alternative algorithm finds all possible matches (at a given
  523. point in the subject), and scans the subject just once (unless there are
  524. lookaround assertions). However, this algorithm does not return captured
  525. substrings. A description of the two matching algorithms and their advantages
  526. and disadvantages is given in the
  527. <a href="pcre2matching.html"><b>pcre2matching</b></a>
  528. documentation. There is no JIT support for <b>pcre2_dfa_match()</b>.
  529. </P>
  530. <P>
  531. In addition to the main compiling and matching functions, there are convenience
  532. functions for extracting captured substrings from a subject string that has
  533. been matched by <b>pcre2_match()</b>. They are:
  534. <pre>
  535. <b>pcre2_substring_copy_byname()</b>
  536. <b>pcre2_substring_copy_bynumber()</b>
  537. <b>pcre2_substring_get_byname()</b>
  538. <b>pcre2_substring_get_bynumber()</b>
  539. <b>pcre2_substring_list_get()</b>
  540. <b>pcre2_substring_length_byname()</b>
  541. <b>pcre2_substring_length_bynumber()</b>
  542. <b>pcre2_substring_nametable_scan()</b>
  543. <b>pcre2_substring_number_from_name()</b>
  544. </pre>
  545. <b>pcre2_substring_free()</b> and <b>pcre2_substring_list_free()</b> are also
  546. provided, to free memory used for extracted strings. If either of these
  547. functions is called with a NULL argument, the function returns immediately
  548. without doing anything.
  549. </P>
  550. <P>
  551. The function <b>pcre2_substitute()</b> can be called to match a pattern and
  552. return a copy of the subject string with substitutions for parts that were
  553. matched.
  554. </P>
  555. <P>
  556. Functions whose names begin with <b>pcre2_serialize_</b> are used for saving
  557. compiled patterns on disc or elsewhere, and reloading them later.
  558. </P>
  559. <P>
  560. Finally, there are functions for finding out information about a compiled
  561. pattern (<b>pcre2_pattern_info()</b>) and about the configuration with which
  562. PCRE2 was built (<b>pcre2_config()</b>).
  563. </P>
  564. <P>
  565. Functions with names ending with <b>_free()</b> are used for freeing memory
  566. blocks of various sorts. In all cases, if one of these functions is called with
  567. a NULL argument, it does nothing.
  568. </P>
  569. <br><a name="SEC15" href="#TOC1">STRING LENGTHS AND OFFSETS</a><br>
  570. <P>
  571. The PCRE2 API uses string lengths and offsets into strings of code units in
  572. several places. These values are always of type PCRE2_SIZE, which is an
  573. unsigned integer type, currently always defined as <i>size_t</i>. The largest
  574. value that can be stored in such a type (that is ~(PCRE2_SIZE)0) is reserved
  575. as a special indicator for zero-terminated strings and unset offsets.
  576. Therefore, the longest string that can be handled is one less than this
  577. maximum. Note that string lengths are always given in code units. Only in the
  578. 8-bit library is such a length the same as the number of bytes in the string.
  579. <a name="newlines"></a></P>
  580. <br><a name="SEC16" href="#TOC1">NEWLINES</a><br>
  581. <P>
  582. PCRE2 supports five different conventions for indicating line breaks in
  583. strings: a single CR (carriage return) character, a single LF (linefeed)
  584. character, the two-character sequence CRLF, any of the three preceding, or any
  585. Unicode newline sequence. The Unicode newline sequences are the three just
  586. mentioned, plus the single characters VT (vertical tab, U+000B), FF (form feed,
  587. U+000C), NEL (next line, U+0085), LS (line separator, U+2028), and PS
  588. (paragraph separator, U+2029).
  589. </P>
  590. <P>
  591. Each of the first three conventions is used by at least one operating system as
  592. its standard newline sequence. When PCRE2 is built, a default can be specified.
  593. If it is not, the default is set to LF, which is the Unix standard. However,
  594. the newline convention can be changed by an application when calling
  595. <b>pcre2_compile()</b>, or it can be specified by special text at the start of
  596. the pattern itself; this overrides any other settings. See the
  597. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  598. page for details of the special character sequences.
  599. </P>
  600. <P>
  601. In the PCRE2 documentation the word "newline" is used to mean "the character or
  602. pair of characters that indicate a line break". The choice of newline
  603. convention affects the handling of the dot, circumflex, and dollar
  604. metacharacters, the handling of #-comments in /x mode, and, when CRLF is a
  605. recognized line ending sequence, the match position advancement for a
  606. non-anchored pattern. There is more detail about this in the
  607. <a href="#matchoptions">section on <b>pcre2_match()</b> options</a>
  608. below.
  609. </P>
  610. <P>
  611. The choice of newline convention does not affect the interpretation of
  612. the \n or \r escape sequences, nor does it affect what \R matches; this has
  613. its own separate convention.
  614. </P>
  615. <br><a name="SEC17" href="#TOC1">MULTITHREADING</a><br>
  616. <P>
  617. In a multithreaded application it is important to keep thread-specific data
  618. separate from data that can be shared between threads. The PCRE2 library code
  619. itself is thread-safe: it contains no static or global variables. The API is
  620. designed to be fairly simple for non-threaded applications while at the same
  621. time ensuring that multithreaded applications can use it.
  622. </P>
  623. <P>
  624. There are several different blocks of data that are used to pass information
  625. between the application and the PCRE2 libraries.
  626. </P>
  627. <br><b>
  628. The compiled pattern
  629. </b><br>
  630. <P>
  631. A pointer to the compiled form of a pattern is returned to the user when
  632. <b>pcre2_compile()</b> is successful. The data in the compiled pattern is fixed,
  633. and does not change when the pattern is matched. Therefore, it is thread-safe,
  634. that is, the same compiled pattern can be used by more than one thread
  635. simultaneously. For example, an application can compile all its patterns at the
  636. start, before forking off multiple threads that use them. However, if the
  637. just-in-time (JIT) optimization feature is being used, it needs separate memory
  638. stack areas for each thread. See the
  639. <a href="pcre2jit.html"><b>pcre2jit</b></a>
  640. documentation for more details.
  641. </P>
  642. <P>
  643. In a more complicated situation, where patterns are compiled only when they are
  644. first needed, but are still shared between threads, pointers to compiled
  645. patterns must be protected from simultaneous writing by multiple threads. This
  646. is somewhat tricky to do correctly. If you know that writing to a pointer is
  647. atomic in your environment, you can use logic like this:
  648. <pre>
  649. Get a read-only (shared) lock (mutex) for pointer
  650. if (pointer == NULL)
  651. {
  652. Get a write (unique) lock for pointer
  653. if (pointer == NULL) pointer = pcre2_compile(...
  654. }
  655. Release the lock
  656. Use pointer in pcre2_match()
  657. </pre>
  658. Of course, testing for compilation errors should also be included in the code.
  659. </P>
  660. <P>
  661. The reason for checking the pointer a second time is as follows: Several
  662. threads may have acquired the shared lock and tested the pointer for being
  663. NULL, but only one of them will be given the write lock, with the rest kept
  664. waiting. The winning thread will compile the pattern and store the result.
  665. After this thread releases the write lock, another thread will get it, and if
  666. it does not retest pointer for being NULL, will recompile the pattern and
  667. overwrite the pointer, creating a memory leak and possibly causing other
  668. issues.
  669. </P>
  670. <P>
  671. In an environment where writing to a pointer may not be atomic, the above logic
  672. is not sufficient. The thread that is doing the compiling may be descheduled
  673. after writing only part of the pointer, which could cause other threads to use
  674. an invalid value. Instead of checking the pointer itself, a separate "pointer
  675. is valid" flag (that can be updated atomically) must be used:
  676. <pre>
  677. Get a read-only (shared) lock (mutex) for pointer
  678. if (!pointer_is_valid)
  679. {
  680. Get a write (unique) lock for pointer
  681. if (!pointer_is_valid)
  682. {
  683. pointer = pcre2_compile(...
  684. pointer_is_valid = TRUE
  685. }
  686. }
  687. Release the lock
  688. Use pointer in pcre2_match()
  689. </pre>
  690. If JIT is being used, but the JIT compilation is not being done immediately
  691. (perhaps waiting to see if the pattern is used often enough), similar logic is
  692. required. JIT compilation updates a value within the compiled code block, so a
  693. thread must gain unique write access to the pointer before calling
  694. <b>pcre2_jit_compile()</b>. Alternatively, <b>pcre2_code_copy()</b> or
  695. <b>pcre2_code_copy_with_tables()</b> can be used to obtain a private copy of the
  696. compiled code before calling the JIT compiler.
  697. </P>
  698. <br><b>
  699. Context blocks
  700. </b><br>
  701. <P>
  702. The next main section below introduces the idea of "contexts" in which PCRE2
  703. functions are called. A context is nothing more than a collection of parameters
  704. that control the way PCRE2 operates. Grouping a number of parameters together
  705. in a context is a convenient way of passing them to a PCRE2 function without
  706. using lots of arguments. The parameters that are stored in contexts are in some
  707. sense "advanced features" of the API. Many straightforward applications will
  708. not need to use contexts.
  709. </P>
  710. <P>
  711. In a multithreaded application, if the parameters in a context are values that
  712. are never changed, the same context can be used by all the threads. However, if
  713. any thread needs to change any value in a context, it must make its own
  714. thread-specific copy.
  715. </P>
  716. <br><b>
  717. Match blocks
  718. </b><br>
  719. <P>
  720. The matching functions need a block of memory for storing the results of a
  721. match. This includes details of what was matched, as well as additional
  722. information such as the name of a (*MARK) setting. Each thread must provide its
  723. own copy of this memory.
  724. </P>
  725. <br><a name="SEC18" href="#TOC1">PCRE2 CONTEXTS</a><br>
  726. <P>
  727. Some PCRE2 functions have a lot of parameters, many of which are used only by
  728. specialist applications, for example, those that use custom memory management
  729. or non-standard character tables. To keep function argument lists at a
  730. reasonable size, and at the same time to keep the API extensible, "uncommon"
  731. parameters are passed to certain functions in a <b>context</b> instead of
  732. directly. A context is just a block of memory that holds the parameter values.
  733. Applications that do not need to adjust any of the context parameters can pass
  734. NULL when a context pointer is required.
  735. </P>
  736. <P>
  737. There are three different types of context: a general context that is relevant
  738. for several PCRE2 operations, a compile-time context, and a match-time context.
  739. </P>
  740. <br><b>
  741. The general context
  742. </b><br>
  743. <P>
  744. At present, this context just contains pointers to (and data for) external
  745. memory management functions that are called from several places in the PCRE2
  746. library. The context is named `general' rather than specifically `memory'
  747. because in future other fields may be added. If you do not want to supply your
  748. own custom memory management functions, you do not need to bother with a
  749. general context. A general context is created by:
  750. <br>
  751. <br>
  752. <b>pcre2_general_context *pcre2_general_context_create(</b>
  753. <b> void *(*<i>private_malloc</i>)(PCRE2_SIZE, void *),</b>
  754. <b> void (*<i>private_free</i>)(void *, void *), void *<i>memory_data</i>);</b>
  755. <br>
  756. <br>
  757. The two function pointers specify custom memory management functions, whose
  758. prototypes are:
  759. <pre>
  760. <b>void *private_malloc(PCRE2_SIZE, void *);</b>
  761. <b>void private_free(void *, void *);</b>
  762. </pre>
  763. Whenever code in PCRE2 calls these functions, the final argument is the value
  764. of <i>memory_data</i>. Either of the first two arguments of the creation
  765. function may be NULL, in which case the system memory management functions
  766. <i>malloc()</i> and <i>free()</i> are used. (This is not currently useful, as
  767. there are no other fields in a general context, but in future there might be.)
  768. The <i>private_malloc()</i> function is used (if supplied) to obtain memory for
  769. storing the context, and all three values are saved as part of the context.
  770. </P>
  771. <P>
  772. Whenever PCRE2 creates a data block of any kind, the block contains a pointer
  773. to the <i>free()</i> function that matches the <i>malloc()</i> function that was
  774. used. When the time comes to free the block, this function is called.
  775. </P>
  776. <P>
  777. A general context can be copied by calling:
  778. <br>
  779. <br>
  780. <b>pcre2_general_context *pcre2_general_context_copy(</b>
  781. <b> pcre2_general_context *<i>gcontext</i>);</b>
  782. <br>
  783. <br>
  784. The memory used for a general context should be freed by calling:
  785. <br>
  786. <br>
  787. <b>void pcre2_general_context_free(pcre2_general_context *<i>gcontext</i>);</b>
  788. <br>
  789. <br>
  790. If this function is passed a NULL argument, it returns immediately without
  791. doing anything.
  792. <a name="compilecontext"></a></P>
  793. <br><b>
  794. The compile context
  795. </b><br>
  796. <P>
  797. A compile context is required if you want to provide an external function for
  798. stack checking during compilation or to change the default values of any of the
  799. following compile-time parameters:
  800. <pre>
  801. What \R matches (Unicode newlines or CR, LF, CRLF only)
  802. PCRE2's character tables
  803. The newline character sequence
  804. The compile time nested parentheses limit
  805. The maximum length of the pattern string
  806. The extra options bits (none set by default)
  807. </pre>
  808. A compile context is also required if you are using custom memory management.
  809. If none of these apply, just pass NULL as the context argument of
  810. <i>pcre2_compile()</i>.
  811. </P>
  812. <P>
  813. A compile context is created, copied, and freed by the following functions:
  814. <br>
  815. <br>
  816. <b>pcre2_compile_context *pcre2_compile_context_create(</b>
  817. <b> pcre2_general_context *<i>gcontext</i>);</b>
  818. <br>
  819. <br>
  820. <b>pcre2_compile_context *pcre2_compile_context_copy(</b>
  821. <b> pcre2_compile_context *<i>ccontext</i>);</b>
  822. <br>
  823. <br>
  824. <b>void pcre2_compile_context_free(pcre2_compile_context *<i>ccontext</i>);</b>
  825. <br>
  826. <br>
  827. A compile context is created with default values for its parameters. These can
  828. be changed by calling the following functions, which return 0 on success, or
  829. PCRE2_ERROR_BADDATA if invalid data is detected.
  830. <br>
  831. <br>
  832. <b>int pcre2_set_bsr(pcre2_compile_context *<i>ccontext</i>,</b>
  833. <b> uint32_t <i>value</i>);</b>
  834. <br>
  835. <br>
  836. The value must be PCRE2_BSR_ANYCRLF, to specify that \R matches only CR, LF,
  837. or CRLF, or PCRE2_BSR_UNICODE, to specify that \R matches any Unicode line
  838. ending sequence. The value is used by the JIT compiler and by the two
  839. interpreted matching functions, <i>pcre2_match()</i> and
  840. <i>pcre2_dfa_match()</i>.
  841. <br>
  842. <br>
  843. <b>int pcre2_set_character_tables(pcre2_compile_context *<i>ccontext</i>,</b>
  844. <b> const uint8_t *<i>tables</i>);</b>
  845. <br>
  846. <br>
  847. The value must be the result of a call to <b>pcre2_maketables()</b>, whose only
  848. argument is a general context. This function builds a set of character tables
  849. in the current locale.
  850. <br>
  851. <br>
  852. <b>int pcre2_set_compile_extra_options(pcre2_compile_context *<i>ccontext</i>,</b>
  853. <b> uint32_t <i>extra_options</i>);</b>
  854. <br>
  855. <br>
  856. As PCRE2 has developed, almost all the 32 option bits that are available in
  857. the <i>options</i> argument of <b>pcre2_compile()</b> have been used up. To avoid
  858. running out, the compile context contains a set of extra option bits which are
  859. used for some newer, assumed rarer, options. This function sets those bits. It
  860. always sets all the bits (either on or off). It does not modify any existing
  861. setting. The available options are defined in the section entitled "Extra
  862. compile options"
  863. <a href="#extracompileoptions">below.</a>
  864. <br>
  865. <br>
  866. <b>int pcre2_set_max_pattern_length(pcre2_compile_context *<i>ccontext</i>,</b>
  867. <b> PCRE2_SIZE <i>value</i>);</b>
  868. <br>
  869. <br>
  870. This sets a maximum length, in code units, for any pattern string that is
  871. compiled with this context. If the pattern is longer, an error is generated.
  872. This facility is provided so that applications that accept patterns from
  873. external sources can limit their size. The default is the largest number that a
  874. PCRE2_SIZE variable can hold, which is effectively unlimited.
  875. <br>
  876. <br>
  877. <b>int pcre2_set_max_varlookbehind(pcre2_compile_contest *<i>ccontext</i>,</b>
  878. <b>" uint32_t <i>value</i>);</b>
  879. <br>
  880. <br>
  881. This sets a maximum length for the number of characters matched by a
  882. variable-length lookbehind assertion. The default is set when PCRE2 is built,
  883. with the ultimate default being 255, the same as Perl. Lookbehind assertions
  884. without a bounding length are not supported.
  885. <br>
  886. <br>
  887. <b>int pcre2_set_newline(pcre2_compile_context *<i>ccontext</i>,</b>
  888. <b> uint32_t <i>value</i>);</b>
  889. <br>
  890. <br>
  891. This specifies which characters or character sequences are to be recognized as
  892. newlines. The value must be one of PCRE2_NEWLINE_CR (carriage return only),
  893. PCRE2_NEWLINE_LF (linefeed only), PCRE2_NEWLINE_CRLF (the two-character
  894. sequence CR followed by LF), PCRE2_NEWLINE_ANYCRLF (any of the above),
  895. PCRE2_NEWLINE_ANY (any Unicode newline sequence), or PCRE2_NEWLINE_NUL (the
  896. NUL character, that is a binary zero).
  897. </P>
  898. <P>
  899. A pattern can override the value set in the compile context by starting with a
  900. sequence such as (*CRLF). See the
  901. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  902. page for details.
  903. </P>
  904. <P>
  905. When a pattern is compiled with the PCRE2_EXTENDED or PCRE2_EXTENDED_MORE
  906. option, the newline convention affects the recognition of the end of internal
  907. comments starting with #. The value is saved with the compiled pattern for
  908. subsequent use by the JIT compiler and by the two interpreted matching
  909. functions, <i>pcre2_match()</i> and <i>pcre2_dfa_match()</i>.
  910. <br>
  911. <br>
  912. <b>int pcre2_set_parens_nest_limit(pcre2_compile_context *<i>ccontext</i>,</b>
  913. <b> uint32_t <i>value</i>);</b>
  914. <br>
  915. <br>
  916. This parameter adjusts the limit, set when PCRE2 is built (default 250), on the
  917. depth of parenthesis nesting in a pattern. This limit stops rogue patterns
  918. using up too much system stack when being compiled. The limit applies to
  919. parentheses of all kinds, not just capturing parentheses.
  920. <br>
  921. <br>
  922. <b>int pcre2_set_compile_recursion_guard(pcre2_compile_context *<i>ccontext</i>,</b>
  923. <b> int (*<i>guard_function</i>)(uint32_t, void *), void *<i>user_data</i>);</b>
  924. <br>
  925. <br>
  926. There is at least one application that runs PCRE2 in threads with very limited
  927. system stack, where running out of stack is to be avoided at all costs. The
  928. parenthesis limit above cannot take account of how much stack is actually
  929. available during compilation. For a finer control, you can supply a function
  930. that is called whenever <b>pcre2_compile()</b> starts to compile a parenthesized
  931. part of a pattern. This function can check the actual stack size (or anything
  932. else that it wants to, of course).
  933. </P>
  934. <P>
  935. The first argument to the callout function gives the current depth of
  936. nesting, and the second is user data that is set up by the last argument of
  937. <b>pcre2_set_compile_recursion_guard()</b>. The callout function should return
  938. zero if all is well, or non-zero to force an error.
  939. <a name="matchcontext"></a></P>
  940. <br><b>
  941. The match context
  942. </b><br>
  943. <P>
  944. A match context is required if you want to:
  945. <pre>
  946. Set up a callout function
  947. Set an offset limit for matching an unanchored pattern
  948. Change the limit on the amount of heap used when matching
  949. Change the backtracking match limit
  950. Change the backtracking depth limit
  951. Set custom memory management specifically for the match
  952. </pre>
  953. If none of these apply, just pass NULL as the context argument of
  954. <b>pcre2_match()</b>, <b>pcre2_dfa_match()</b>, or <b>pcre2_jit_match()</b>.
  955. </P>
  956. <P>
  957. A match context is created, copied, and freed by the following functions:
  958. <br>
  959. <br>
  960. <b>pcre2_match_context *pcre2_match_context_create(</b>
  961. <b> pcre2_general_context *<i>gcontext</i>);</b>
  962. <br>
  963. <br>
  964. <b>pcre2_match_context *pcre2_match_context_copy(</b>
  965. <b> pcre2_match_context *<i>mcontext</i>);</b>
  966. <br>
  967. <br>
  968. <b>void pcre2_match_context_free(pcre2_match_context *<i>mcontext</i>);</b>
  969. <br>
  970. <br>
  971. A match context is created with default values for its parameters. These can
  972. be changed by calling the following functions, which return 0 on success, or
  973. PCRE2_ERROR_BADDATA if invalid data is detected.
  974. <br>
  975. <br>
  976. <b>int pcre2_set_callout(pcre2_match_context *<i>mcontext</i>,</b>
  977. <b> int (*<i>callout_function</i>)(pcre2_callout_block *, void *),</b>
  978. <b> void *<i>callout_data</i>);</b>
  979. <br>
  980. <br>
  981. This sets up a callout function for PCRE2 to call at specified points
  982. during a matching operation. Details are given in the
  983. <a href="pcre2callout.html"><b>pcre2callout</b></a>
  984. documentation.
  985. <br>
  986. <br>
  987. <b>int pcre2_set_substitute_callout(pcre2_match_context *<i>mcontext</i>,</b>
  988. <b> int (*<i>callout_function</i>)(pcre2_substitute_callout_block *, void *),</b>
  989. <b> void *<i>callout_data</i>);</b>
  990. <br>
  991. <br>
  992. This sets up a callout function for PCRE2 to call after each substitution
  993. made by <b>pcre2_substitute()</b>. Details are given in the section entitled
  994. "Creating a new string with substitutions"
  995. <a href="#substitutions">below.</a>
  996. <br>
  997. <br>
  998. <b>int pcre2_set_offset_limit(pcre2_match_context *<i>mcontext</i>,</b>
  999. <b> PCRE2_SIZE <i>value</i>);</b>
  1000. <br>
  1001. <br>
  1002. The <i>offset_limit</i> parameter limits how far an unanchored search can
  1003. advance in the subject string. The default value is PCRE2_UNSET. The
  1004. <b>pcre2_match()</b> and <b>pcre2_dfa_match()</b> functions return
  1005. PCRE2_ERROR_NOMATCH if a match with a starting point before or at the given
  1006. offset is not found. The <b>pcre2_substitute()</b> function makes no more
  1007. substitutions.
  1008. </P>
  1009. <P>
  1010. For example, if the pattern /abc/ is matched against "123abc" with an offset
  1011. limit less than 3, the result is PCRE2_ERROR_NOMATCH. A match can never be
  1012. found if the <i>startoffset</i> argument of <b>pcre2_match()</b>,
  1013. <b>pcre2_dfa_match()</b>, or <b>pcre2_substitute()</b> is greater than the offset
  1014. limit set in the match context.
  1015. </P>
  1016. <P>
  1017. When using this facility, you must set the PCRE2_USE_OFFSET_LIMIT option when
  1018. calling <b>pcre2_compile()</b> so that when JIT is in use, different code can be
  1019. compiled. If a match is started with a non-default match limit when
  1020. PCRE2_USE_OFFSET_LIMIT is not set, an error is generated.
  1021. </P>
  1022. <P>
  1023. The offset limit facility can be used to track progress when searching large
  1024. subject strings or to limit the extent of global substitutions. See also the
  1025. PCRE2_FIRSTLINE option, which requires a match to start before or at the first
  1026. newline that follows the start of matching in the subject. If this is set with
  1027. an offset limit, a match must occur in the first line and also within the
  1028. offset limit. In other words, whichever limit comes first is used.
  1029. <br>
  1030. <br>
  1031. <b>int pcre2_set_heap_limit(pcre2_match_context *<i>mcontext</i>,</b>
  1032. <b> uint32_t <i>value</i>);</b>
  1033. <br>
  1034. <br>
  1035. The <i>heap_limit</i> parameter specifies, in units of kibibytes (1024 bytes),
  1036. the maximum amount of heap memory that <b>pcre2_match()</b> may use to hold
  1037. backtracking information when running an interpretive match. This limit also
  1038. applies to <b>pcre2_dfa_match()</b>, which may use the heap when processing
  1039. patterns with a lot of nested pattern recursion or lookarounds or atomic
  1040. groups. This limit does not apply to matching with the JIT optimization, which
  1041. has its own memory control arrangements (see the
  1042. <a href="pcre2jit.html"><b>pcre2jit</b></a>
  1043. documentation for more details). If the limit is reached, the negative error
  1044. code PCRE2_ERROR_HEAPLIMIT is returned. The default limit can be set when PCRE2
  1045. is built; if it is not, the default is set very large and is essentially
  1046. unlimited.
  1047. </P>
  1048. <P>
  1049. A value for the heap limit may also be supplied by an item at the start of a
  1050. pattern of the form
  1051. <pre>
  1052. (*LIMIT_HEAP=ddd)
  1053. </pre>
  1054. where ddd is a decimal number. However, such a setting is ignored unless ddd is
  1055. less than the limit set by the caller of <b>pcre2_match()</b> or, if no such
  1056. limit is set, less than the default.
  1057. </P>
  1058. <P>
  1059. The <b>pcre2_match()</b> function always needs some heap memory, so setting a
  1060. value of zero guarantees a "heap limit exceeded" error. Details of how
  1061. <b>pcre2_match()</b> uses the heap are given in the
  1062. <a href="pcre2perform.html"><b>pcre2perform</b></a>
  1063. documentation.
  1064. </P>
  1065. <P>
  1066. For <b>pcre2_dfa_match()</b>, a vector on the system stack is used when
  1067. processing pattern recursions, lookarounds, or atomic groups, and only if this
  1068. is not big enough is heap memory used. In this case, setting a value of zero
  1069. disables the use of the heap.
  1070. <br>
  1071. <br>
  1072. <b>int pcre2_set_match_limit(pcre2_match_context *<i>mcontext</i>,</b>
  1073. <b> uint32_t <i>value</i>);</b>
  1074. <br>
  1075. <br>
  1076. The <i>match_limit</i> parameter provides a means of preventing PCRE2 from using
  1077. up too many computing resources when processing patterns that are not going to
  1078. match, but which have a very large number of possibilities in their search
  1079. trees. The classic example is a pattern that uses nested unlimited repeats.
  1080. </P>
  1081. <P>
  1082. There is an internal counter in <b>pcre2_match()</b> that is incremented each
  1083. time round its main matching loop. If this value reaches the match limit,
  1084. <b>pcre2_match()</b> returns the negative value PCRE2_ERROR_MATCHLIMIT. This has
  1085. the effect of limiting the amount of backtracking that can take place. For
  1086. patterns that are not anchored, the count restarts from zero for each position
  1087. in the subject string. This limit also applies to <b>pcre2_dfa_match()</b>,
  1088. though the counting is done in a different way.
  1089. </P>
  1090. <P>
  1091. When <b>pcre2_match()</b> is called with a pattern that was successfully
  1092. processed by <b>pcre2_jit_compile()</b>, the way in which matching is executed
  1093. is entirely different. However, there is still the possibility of runaway
  1094. matching that goes on for a very long time, and so the <i>match_limit</i> value
  1095. is also used in this case (but in a different way) to limit how long the
  1096. matching can continue.
  1097. </P>
  1098. <P>
  1099. The default value for the limit can be set when PCRE2 is built; the default is
  1100. 10 million, which handles all but the most extreme cases. A value for the match
  1101. limit may also be supplied by an item at the start of a pattern of the form
  1102. <pre>
  1103. (*LIMIT_MATCH=ddd)
  1104. </pre>
  1105. where ddd is a decimal number. However, such a setting is ignored unless ddd is
  1106. less than the limit set by the caller of <b>pcre2_match()</b> or
  1107. <b>pcre2_dfa_match()</b> or, if no such limit is set, less than the default.
  1108. <br>
  1109. <br>
  1110. <b>int pcre2_set_depth_limit(pcre2_match_context *<i>mcontext</i>,</b>
  1111. <b> uint32_t <i>value</i>);</b>
  1112. <br>
  1113. <br>
  1114. This parameter limits the depth of nested backtracking in <b>pcre2_match()</b>.
  1115. Each time a nested backtracking point is passed, a new memory frame is used
  1116. to remember the state of matching at that point. Thus, this parameter
  1117. indirectly limits the amount of memory that is used in a match. However,
  1118. because the size of each memory frame depends on the number of capturing
  1119. parentheses, the actual memory limit varies from pattern to pattern. This limit
  1120. was more useful in versions before 10.30, where function recursion was used for
  1121. backtracking.
  1122. </P>
  1123. <P>
  1124. The depth limit is not relevant, and is ignored, when matching is done using
  1125. JIT compiled code. However, it is supported by <b>pcre2_dfa_match()</b>, which
  1126. uses it to limit the depth of nested internal recursive function calls that
  1127. implement atomic groups, lookaround assertions, and pattern recursions. This
  1128. limits, indirectly, the amount of system stack that is used. It was more useful
  1129. in versions before 10.32, when stack memory was used for local workspace
  1130. vectors for recursive function calls. From version 10.32, only local variables
  1131. are allocated on the stack and as each call uses only a few hundred bytes, even
  1132. a small stack can support quite a lot of recursion.
  1133. </P>
  1134. <P>
  1135. If the depth of internal recursive function calls is great enough, local
  1136. workspace vectors are allocated on the heap from version 10.32 onwards, so the
  1137. depth limit also indirectly limits the amount of heap memory that is used. A
  1138. recursive pattern such as /(.(?2))((?1)|)/, when matched to a very long string
  1139. using <b>pcre2_dfa_match()</b>, can use a great deal of memory. However, it is
  1140. probably better to limit heap usage directly by calling
  1141. <b>pcre2_set_heap_limit()</b>.
  1142. </P>
  1143. <P>
  1144. The default value for the depth limit can be set when PCRE2 is built; if it is
  1145. not, the default is set to the same value as the default for the match limit.
  1146. If the limit is exceeded, <b>pcre2_match()</b> or <b>pcre2_dfa_match()</b>
  1147. returns PCRE2_ERROR_DEPTHLIMIT. A value for the depth limit may also be
  1148. supplied by an item at the start of a pattern of the form
  1149. <pre>
  1150. (*LIMIT_DEPTH=ddd)
  1151. </pre>
  1152. where ddd is a decimal number. However, such a setting is ignored unless ddd is
  1153. less than the limit set by the caller of <b>pcre2_match()</b> or
  1154. <b>pcre2_dfa_match()</b> or, if no such limit is set, less than the default.
  1155. </P>
  1156. <br><a name="SEC19" href="#TOC1">CHECKING BUILD-TIME OPTIONS</a><br>
  1157. <P>
  1158. <b>int pcre2_config(uint32_t <i>what</i>, void *<i>where</i>);</b>
  1159. </P>
  1160. <P>
  1161. The function <b>pcre2_config()</b> makes it possible for a PCRE2 client to find
  1162. the value of certain configuration parameters and to discover which optional
  1163. features have been compiled into the PCRE2 library. The
  1164. <a href="pcre2build.html"><b>pcre2build</b></a>
  1165. documentation has more details about these features.
  1166. </P>
  1167. <P>
  1168. The first argument for <b>pcre2_config()</b> specifies which information is
  1169. required. The second argument is a pointer to memory into which the information
  1170. is placed. If NULL is passed, the function returns the amount of memory that is
  1171. needed for the requested information. For calls that return numerical values,
  1172. the value is in bytes; when requesting these values, <i>where</i> should point
  1173. to appropriately aligned memory. For calls that return strings, the required
  1174. length is given in code units, not counting the terminating zero.
  1175. </P>
  1176. <P>
  1177. When requesting information, the returned value from <b>pcre2_config()</b> is
  1178. non-negative on success, or the negative error code PCRE2_ERROR_BADOPTION if
  1179. the value in the first argument is not recognized. The following information is
  1180. available:
  1181. <pre>
  1182. PCRE2_CONFIG_BSR
  1183. </pre>
  1184. The output is a uint32_t integer whose value indicates what character
  1185. sequences the \R escape sequence matches by default. A value of
  1186. PCRE2_BSR_UNICODE means that \R matches any Unicode line ending sequence; a
  1187. value of PCRE2_BSR_ANYCRLF means that \R matches only CR, LF, or CRLF. The
  1188. default can be overridden when a pattern is compiled.
  1189. <pre>
  1190. PCRE2_CONFIG_COMPILED_WIDTHS
  1191. </pre>
  1192. The output is a uint32_t integer whose lower bits indicate which code unit
  1193. widths were selected when PCRE2 was built. The 1-bit indicates 8-bit support,
  1194. and the 2-bit and 4-bit indicate 16-bit and 32-bit support, respectively.
  1195. <pre>
  1196. PCRE2_CONFIG_DEPTHLIMIT
  1197. </pre>
  1198. The output is a uint32_t integer that gives the default limit for the depth of
  1199. nested backtracking in <b>pcre2_match()</b> or the depth of nested recursions,
  1200. lookarounds, and atomic groups in <b>pcre2_dfa_match()</b>. Further details are
  1201. given with <b>pcre2_set_depth_limit()</b> above.
  1202. <pre>
  1203. PCRE2_CONFIG_HEAPLIMIT
  1204. </pre>
  1205. The output is a uint32_t integer that gives, in kibibytes, the default limit
  1206. for the amount of heap memory used by <b>pcre2_match()</b> or
  1207. <b>pcre2_dfa_match()</b>. Further details are given with
  1208. <b>pcre2_set_heap_limit()</b> above.
  1209. <pre>
  1210. PCRE2_CONFIG_JIT
  1211. </pre>
  1212. The output is a uint32_t integer that is set to one if support for just-in-time
  1213. compiling is included in the library; otherwise it is set to zero. Note that
  1214. having the support in the library does not guarantee that JIT will be used for
  1215. any given match. See the
  1216. <a href="pcre2jit.html"><b>pcre2jit</b></a>
  1217. documentation for more details.
  1218. <pre>
  1219. PCRE2_CONFIG_JITTARGET
  1220. </pre>
  1221. The <i>where</i> argument should point to a buffer that is at least 48 code
  1222. units long. (The exact length required can be found by calling
  1223. <b>pcre2_config()</b> with <b>where</b> set to NULL.) The buffer is filled with a
  1224. string that contains the name of the architecture for which the JIT compiler is
  1225. configured, for example "x86 32bit (little endian + unaligned)". If JIT support
  1226. is not available, PCRE2_ERROR_BADOPTION is returned, otherwise the number of
  1227. code units used is returned. This is the length of the string, plus one unit
  1228. for the terminating zero.
  1229. <pre>
  1230. PCRE2_CONFIG_LINKSIZE
  1231. </pre>
  1232. The output is a uint32_t integer that contains the number of bytes used for
  1233. internal linkage in compiled regular expressions. When PCRE2 is configured, the
  1234. value can be set to 2, 3, or 4, with the default being 2. This is the value
  1235. that is returned by <b>pcre2_config()</b>. However, when the 16-bit library is
  1236. compiled, a value of 3 is rounded up to 4, and when the 32-bit library is
  1237. compiled, internal linkages always use 4 bytes, so the configured value is not
  1238. relevant.
  1239. </P>
  1240. <P>
  1241. The default value of 2 for the 8-bit and 16-bit libraries is sufficient for all
  1242. but the most massive patterns, since it allows the size of the compiled pattern
  1243. to be up to 65535 code units. Larger values allow larger regular expressions to
  1244. be compiled by those two libraries, but at the expense of slower matching.
  1245. <pre>
  1246. PCRE2_CONFIG_MATCHLIMIT
  1247. </pre>
  1248. The output is a uint32_t integer that gives the default match limit for
  1249. <b>pcre2_match()</b>. Further details are given with
  1250. <b>pcre2_set_match_limit()</b> above.
  1251. <pre>
  1252. PCRE2_CONFIG_NEWLINE
  1253. </pre>
  1254. The output is a uint32_t integer whose value specifies the default character
  1255. sequence that is recognized as meaning "newline". The values are:
  1256. <pre>
  1257. PCRE2_NEWLINE_CR Carriage return (CR)
  1258. PCRE2_NEWLINE_LF Linefeed (LF)
  1259. PCRE2_NEWLINE_CRLF Carriage return, linefeed (CRLF)
  1260. PCRE2_NEWLINE_ANY Any Unicode line ending
  1261. PCRE2_NEWLINE_ANYCRLF Any of CR, LF, or CRLF
  1262. PCRE2_NEWLINE_NUL The NUL character (binary zero)
  1263. </pre>
  1264. The default should normally correspond to the standard sequence for your
  1265. operating system.
  1266. <pre>
  1267. PCRE2_CONFIG_NEVER_BACKSLASH_C
  1268. </pre>
  1269. The output is a uint32_t integer that is set to one if the use of \C was
  1270. permanently disabled when PCRE2 was built; otherwise it is set to zero.
  1271. <pre>
  1272. PCRE2_CONFIG_PARENSLIMIT
  1273. </pre>
  1274. The output is a uint32_t integer that gives the maximum depth of nesting
  1275. of parentheses (of any kind) in a pattern. This limit is imposed to cap the
  1276. amount of system stack used when a pattern is compiled. It is specified when
  1277. PCRE2 is built; the default is 250. This limit does not take into account the
  1278. stack that may already be used by the calling application. For finer control
  1279. over compilation stack usage, see <b>pcre2_set_compile_recursion_guard()</b>.
  1280. <pre>
  1281. PCRE2_CONFIG_STACKRECURSE
  1282. </pre>
  1283. This parameter is obsolete and should not be used in new code. The output is a
  1284. uint32_t integer that is always set to zero.
  1285. <pre>
  1286. PCRE2_CONFIG_TABLES_LENGTH
  1287. </pre>
  1288. The output is a uint32_t integer that gives the length of PCRE2's character
  1289. processing tables in bytes. For details of these tables see the
  1290. <a href="#localesupport">section on locale support</a>
  1291. below.
  1292. <pre>
  1293. PCRE2_CONFIG_UNICODE_VERSION
  1294. </pre>
  1295. The <i>where</i> argument should point to a buffer that is at least 24 code
  1296. units long. (The exact length required can be found by calling
  1297. <b>pcre2_config()</b> with <b>where</b> set to NULL.) If PCRE2 has been compiled
  1298. without Unicode support, the buffer is filled with the text "Unicode not
  1299. supported". Otherwise, the Unicode version string (for example, "8.0.0") is
  1300. inserted. The number of code units used is returned. This is the length of the
  1301. string plus one unit for the terminating zero.
  1302. <pre>
  1303. PCRE2_CONFIG_UNICODE
  1304. </pre>
  1305. The output is a uint32_t integer that is set to one if Unicode support is
  1306. available; otherwise it is set to zero. Unicode support implies UTF support.
  1307. <pre>
  1308. PCRE2_CONFIG_VERSION
  1309. </pre>
  1310. The <i>where</i> argument should point to a buffer that is at least 24 code
  1311. units long. (The exact length required can be found by calling
  1312. <b>pcre2_config()</b> with <b>where</b> set to NULL.) The buffer is filled with
  1313. the PCRE2 version string, zero-terminated. The number of code units used is
  1314. returned. This is the length of the string plus one unit for the terminating
  1315. zero.
  1316. <a name="compiling"></a></P>
  1317. <br><a name="SEC20" href="#TOC1">COMPILING A PATTERN</a><br>
  1318. <P>
  1319. <b>pcre2_code *pcre2_compile(PCRE2_SPTR <i>pattern</i>, PCRE2_SIZE <i>length</i>,</b>
  1320. <b> uint32_t <i>options</i>, int *<i>errorcode</i>, PCRE2_SIZE *<i>erroroffset,</i></b>
  1321. <b> pcre2_compile_context *<i>ccontext</i>);</b>
  1322. <br>
  1323. <br>
  1324. <b>void pcre2_code_free(pcre2_code *<i>code</i>);</b>
  1325. <br>
  1326. <br>
  1327. <b>pcre2_code *pcre2_code_copy(const pcre2_code *<i>code</i>);</b>
  1328. <br>
  1329. <br>
  1330. <b>pcre2_code *pcre2_code_copy_with_tables(const pcre2_code *<i>code</i>);</b>
  1331. </P>
  1332. <P>
  1333. The <b>pcre2_compile()</b> function compiles a pattern into an internal form.
  1334. The pattern is defined by a pointer to a string of code units and a length in
  1335. code units. If the pattern is zero-terminated, the length can be specified as
  1336. PCRE2_ZERO_TERMINATED. A NULL pattern pointer with a length of zero is treated
  1337. as an empty string (NULL with a non-zero length causes an error return). The
  1338. function returns a pointer to a block of memory that contains the compiled
  1339. pattern and related data, or NULL if an error occurred.
  1340. </P>
  1341. <P>
  1342. If the compile context argument <i>ccontext</i> is NULL, memory for the compiled
  1343. pattern is obtained by calling <b>malloc()</b>. Otherwise, it is obtained from
  1344. the same memory function that was used for the compile context. The caller must
  1345. free the memory by calling <b>pcre2_code_free()</b> when it is no longer needed.
  1346. If <b>pcre2_code_free()</b> is called with a NULL argument, it returns
  1347. immediately, without doing anything.
  1348. </P>
  1349. <P>
  1350. The function <b>pcre2_code_copy()</b> makes a copy of the compiled code in new
  1351. memory, using the same memory allocator as was used for the original. However,
  1352. if the code has been processed by the JIT compiler (see
  1353. <a href="#jitcompiling">below),</a>
  1354. the JIT information cannot be copied (because it is position-dependent).
  1355. The new copy can initially be used only for non-JIT matching, though it can be
  1356. passed to <b>pcre2_jit_compile()</b> if required. If <b>pcre2_code_copy()</b> is
  1357. called with a NULL argument, it returns NULL.
  1358. </P>
  1359. <P>
  1360. The <b>pcre2_code_copy()</b> function provides a way for individual threads in a
  1361. multithreaded application to acquire a private copy of shared compiled code.
  1362. However, it does not make a copy of the character tables used by the compiled
  1363. pattern; the new pattern code points to the same tables as the original code.
  1364. (See
  1365. <a href="#jitcompiling">"Locale Support"</a>
  1366. below for details of these character tables.) In many applications the same
  1367. tables are used throughout, so this behaviour is appropriate. Nevertheless,
  1368. there are occasions when a copy of a compiled pattern and the relevant tables
  1369. are needed. The <b>pcre2_code_copy_with_tables()</b> provides this facility.
  1370. Copies of both the code and the tables are made, with the new code pointing to
  1371. the new tables. The memory for the new tables is automatically freed when
  1372. <b>pcre2_code_free()</b> is called for the new copy of the compiled code. If
  1373. <b>pcre2_code_copy_with_tables()</b> is called with a NULL argument, it returns
  1374. NULL.
  1375. </P>
  1376. <P>
  1377. NOTE: When one of the matching functions is called, pointers to the compiled
  1378. pattern and the subject string are set in the match data block so that they can
  1379. be referenced by the substring extraction functions after a successful match.
  1380. After running a match, you must not free a compiled pattern or a subject string
  1381. until after all operations on the
  1382. <a href="#matchdatablock">match data block</a>
  1383. have taken place, unless, in the case of the subject string, you have used the
  1384. PCRE2_COPY_MATCHED_SUBJECT option, which is described in the section entitled
  1385. "Option bits for <b>pcre2_match()</b>"
  1386. <a href="#matchoptions>">below.</a>
  1387. </P>
  1388. <P>
  1389. The <i>options</i> argument for <b>pcre2_compile()</b> contains various bit
  1390. settings that affect the compilation. It should be zero if none of them are
  1391. required. The available options are described below. Some of them (in
  1392. particular, those that are compatible with Perl, but some others as well) can
  1393. also be set and unset from within the pattern (see the detailed description in
  1394. the
  1395. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  1396. documentation).
  1397. </P>
  1398. <P>
  1399. For those options that can be different in different parts of the pattern, the
  1400. contents of the <i>options</i> argument specifies their settings at the start of
  1401. compilation. The PCRE2_ANCHORED, PCRE2_ENDANCHORED, and PCRE2_NO_UTF_CHECK
  1402. options can be set at the time of matching as well as at compile time.
  1403. </P>
  1404. <P>
  1405. Some additional options and less frequently required compile-time parameters
  1406. (for example, the newline setting) can be provided in a compile context (as
  1407. described
  1408. <a href="#compilecontext">above).</a>
  1409. </P>
  1410. <P>
  1411. If <i>errorcode</i> or <i>erroroffset</i> is NULL, <b>pcre2_compile()</b> returns
  1412. NULL immediately. Otherwise, the variables to which these point are set to an
  1413. error code and an offset (number of code units) within the pattern,
  1414. respectively, when <b>pcre2_compile()</b> returns NULL because a compilation
  1415. error has occurred.
  1416. </P>
  1417. <P>
  1418. There are nearly 100 positive error codes that <b>pcre2_compile()</b> may return
  1419. if it finds an error in the pattern. There are also some negative error codes
  1420. that are used for invalid UTF strings when validity checking is in force. These
  1421. are the same as given by <b>pcre2_match()</b> and <b>pcre2_dfa_match()</b>, and
  1422. are described in the
  1423. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  1424. documentation. There is no separate documentation for the positive error codes,
  1425. because the textual error messages that are obtained by calling the
  1426. <b>pcre2_get_error_message()</b> function (see "Obtaining a textual error
  1427. message"
  1428. <a href="#geterrormessage">below)</a>
  1429. should be self-explanatory. Macro names starting with PCRE2_ERROR_ are defined
  1430. for both positive and negative error codes in <b>pcre2.h</b>. When compilation
  1431. is successful <i>errorcode</i> is set to a value that returns the message "no
  1432. error" if passed to <b>pcre2_get_error_message()</b>.
  1433. </P>
  1434. <P>
  1435. The value returned in <i>erroroffset</i> is an indication of where in the
  1436. pattern an error occurred. When there is no error, zero is returned. A non-zero
  1437. value is not necessarily the furthest point in the pattern that was read. For
  1438. example, after the error "lookbehind assertion is not fixed length", the error
  1439. offset points to the start of the failing assertion. For an invalid UTF-8 or
  1440. UTF-16 string, the offset is that of the first code unit of the failing
  1441. character.
  1442. </P>
  1443. <P>
  1444. Some errors are not detected until the whole pattern has been scanned; in these
  1445. cases, the offset passed back is the length of the pattern. Note that the
  1446. offset is in code units, not characters, even in a UTF mode. It may sometimes
  1447. point into the middle of a UTF-8 or UTF-16 character.
  1448. </P>
  1449. <P>
  1450. This code fragment shows a typical straightforward call to
  1451. <b>pcre2_compile()</b>:
  1452. <pre>
  1453. pcre2_code *re;
  1454. PCRE2_SIZE erroffset;
  1455. int errorcode;
  1456. re = pcre2_compile(
  1457. "^A.*Z", /* the pattern */
  1458. PCRE2_ZERO_TERMINATED, /* the pattern is zero-terminated */
  1459. 0, /* default options */
  1460. &errorcode, /* for error code */
  1461. &erroffset, /* for error offset */
  1462. NULL); /* no compile context */
  1463. </PRE>
  1464. </P>
  1465. <br><b>
  1466. Main compile options
  1467. </b><br>
  1468. <P>
  1469. The following names for option bits are defined in the <b>pcre2.h</b> header
  1470. file:
  1471. <pre>
  1472. PCRE2_ANCHORED
  1473. </pre>
  1474. If this bit is set, the pattern is forced to be "anchored", that is, it is
  1475. constrained to match only at the first matching point in the string that is
  1476. being searched (the "subject string"). This effect can also be achieved by
  1477. appropriate constructs in the pattern itself, which is the only way to do it in
  1478. Perl.
  1479. <pre>
  1480. PCRE2_ALLOW_EMPTY_CLASS
  1481. </pre>
  1482. By default, for compatibility with Perl, a closing square bracket that
  1483. immediately follows an opening one is treated as a data character for the
  1484. class. When PCRE2_ALLOW_EMPTY_CLASS is set, it terminates the class, which
  1485. therefore contains no characters and so can never match.
  1486. <pre>
  1487. PCRE2_ALT_BSUX
  1488. </pre>
  1489. This option request alternative handling of three escape sequences, which
  1490. makes PCRE2's behaviour more like ECMAscript (aka JavaScript). When it is set:
  1491. </P>
  1492. <P>
  1493. (1) \U matches an upper case "U" character; by default \U causes a compile
  1494. time error (Perl uses \U to upper case subsequent characters).
  1495. </P>
  1496. <P>
  1497. (2) \u matches a lower case "u" character unless it is followed by four
  1498. hexadecimal digits, in which case the hexadecimal number defines the code point
  1499. to match. By default, \u causes a compile time error (Perl uses it to upper
  1500. case the following character).
  1501. </P>
  1502. <P>
  1503. (3) \x matches a lower case "x" character unless it is followed by two
  1504. hexadecimal digits, in which case the hexadecimal number defines the code point
  1505. to match. By default, as in Perl, a hexadecimal number is always expected after
  1506. \x, but it may have zero, one, or two digits (so, for example, \xz matches a
  1507. binary zero character followed by z).
  1508. </P>
  1509. <P>
  1510. ECMAscript 6 added additional functionality to \u. This can be accessed using
  1511. the PCRE2_EXTRA_ALT_BSUX extra option (see "Extra compile options"
  1512. <a href="#extracompileoptions">below).</a>
  1513. Note that this alternative escape handling applies only to patterns. Neither of
  1514. these options affects the processing of replacement strings passed to
  1515. <b>pcre2_substitute()</b>.
  1516. <pre>
  1517. PCRE2_ALT_CIRCUMFLEX
  1518. </pre>
  1519. In multiline mode (when PCRE2_MULTILINE is set), the circumflex metacharacter
  1520. matches at the start of the subject (unless PCRE2_NOTBOL is set), and also
  1521. after any internal newline. However, it does not match after a newline at the
  1522. end of the subject, for compatibility with Perl. If you want a multiline
  1523. circumflex also to match after a terminating newline, you must set
  1524. PCRE2_ALT_CIRCUMFLEX.
  1525. <pre>
  1526. PCRE2_ALT_VERBNAMES
  1527. </pre>
  1528. By default, for compatibility with Perl, the name in any verb sequence such as
  1529. (*MARK:NAME) is any sequence of characters that does not include a closing
  1530. parenthesis. The name is not processed in any way, and it is not possible to
  1531. include a closing parenthesis in the name. However, if the PCRE2_ALT_VERBNAMES
  1532. option is set, normal backslash processing is applied to verb names and only an
  1533. unescaped closing parenthesis terminates the name. A closing parenthesis can be
  1534. included in a name either as \) or between \Q and \E. If the PCRE2_EXTENDED
  1535. or PCRE2_EXTENDED_MORE option is set with PCRE2_ALT_VERBNAMES, unescaped
  1536. whitespace in verb names is skipped and #-comments are recognized, exactly as
  1537. in the rest of the pattern.
  1538. <pre>
  1539. PCRE2_AUTO_CALLOUT
  1540. </pre>
  1541. If this bit is set, <b>pcre2_compile()</b> automatically inserts callout items,
  1542. all with number 255, before each pattern item, except immediately before or
  1543. after an explicit callout in the pattern. For discussion of the callout
  1544. facility, see the
  1545. <a href="pcre2callout.html"><b>pcre2callout</b></a>
  1546. documentation.
  1547. <pre>
  1548. PCRE2_CASELESS
  1549. </pre>
  1550. If this bit is set, letters in the pattern match both upper and lower case
  1551. letters in the subject. It is equivalent to Perl's /i option, and it can be
  1552. changed within a pattern by a (?i) option setting. If either PCRE2_UTF or
  1553. PCRE2_UCP is set, Unicode properties are used for all characters with more than
  1554. one other case, and for all characters whose code points are greater than
  1555. U+007F. Note that there are two ASCII characters, K and S, that, in addition to
  1556. their lower case ASCII equivalents, are case-equivalent with U+212A (Kelvin
  1557. sign) and U+017F (long S) respectively. If you do not want this case
  1558. equivalence, you can suppress it by setting PCRE2_EXTRA_CASELESS_RESTRICT.
  1559. </P>
  1560. <P>
  1561. For lower valued characters with only one other case, a lookup table is used
  1562. for speed. When neither PCRE2_UTF nor PCRE2_UCP is set, a lookup table is used
  1563. for all code points less than 256, and higher code points (available only in
  1564. 16-bit or 32-bit mode) are treated as not having another case.
  1565. <pre>
  1566. PCRE2_DOLLAR_ENDONLY
  1567. </pre>
  1568. If this bit is set, a dollar metacharacter in the pattern matches only at the
  1569. end of the subject string. Without this option, a dollar also matches
  1570. immediately before a newline at the end of the string (but not before any other
  1571. newlines). The PCRE2_DOLLAR_ENDONLY option is ignored if PCRE2_MULTILINE is
  1572. set. There is no equivalent to this option in Perl, and no way to set it within
  1573. a pattern.
  1574. <pre>
  1575. PCRE2_DOTALL
  1576. </pre>
  1577. If this bit is set, a dot metacharacter in the pattern matches any character,
  1578. including one that indicates a newline. However, it only ever matches one
  1579. character, even if newlines are coded as CRLF. Without this option, a dot does
  1580. not match when the current position in the subject is at a newline. This option
  1581. is equivalent to Perl's /s option, and it can be changed within a pattern by a
  1582. (?s) option setting. A negative class such as [^a] always matches newline
  1583. characters, and the \N escape sequence always matches a non-newline character,
  1584. independent of the setting of PCRE2_DOTALL.
  1585. <pre>
  1586. PCRE2_DUPNAMES
  1587. </pre>
  1588. If this bit is set, names used to identify capture groups need not be unique.
  1589. This can be helpful for certain types of pattern when it is known that only one
  1590. instance of the named group can ever be matched. There are more details of
  1591. named capture groups below; see also the
  1592. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  1593. documentation.
  1594. <pre>
  1595. PCRE2_ENDANCHORED
  1596. </pre>
  1597. If this bit is set, the end of any pattern match must be right at the end of
  1598. the string being searched (the "subject string"). If the pattern match
  1599. succeeds by reaching (*ACCEPT), but does not reach the end of the subject, the
  1600. match fails at the current starting point. For unanchored patterns, a new match
  1601. is then tried at the next starting point. However, if the match succeeds by
  1602. reaching the end of the pattern, but not the end of the subject, backtracking
  1603. occurs and an alternative match may be found. Consider these two patterns:
  1604. <pre>
  1605. .(*ACCEPT)|..
  1606. .|..
  1607. </pre>
  1608. If matched against "abc" with PCRE2_ENDANCHORED set, the first matches "c"
  1609. whereas the second matches "bc". The effect of PCRE2_ENDANCHORED can also be
  1610. achieved by appropriate constructs in the pattern itself, which is the only way
  1611. to do it in Perl.
  1612. </P>
  1613. <P>
  1614. For DFA matching with <b>pcre2_dfa_match()</b>, PCRE2_ENDANCHORED applies only
  1615. to the first (that is, the longest) matched string. Other parallel matches,
  1616. which are necessarily substrings of the first one, must obviously end before
  1617. the end of the subject.
  1618. <pre>
  1619. PCRE2_EXTENDED
  1620. </pre>
  1621. If this bit is set, most white space characters in the pattern are totally
  1622. ignored except when escaped, inside a character class, or inside a \Q...\E
  1623. sequence. However, white space is not allowed within sequences such as (?&#62; that
  1624. introduce various parenthesized groups, nor within numerical quantifiers such
  1625. as {1,3}. Ignorable white space is permitted between an item and a following
  1626. quantifier and between a quantifier and a following + that indicates
  1627. possessiveness. PCRE2_EXTENDED is equivalent to Perl's /x option, and it can be
  1628. changed within a pattern by a (?x) option setting.
  1629. </P>
  1630. <P>
  1631. When PCRE2 is compiled without Unicode support, PCRE2_EXTENDED recognizes as
  1632. white space only those characters with code points less than 256 that are
  1633. flagged as white space in its low-character table. The table is normally
  1634. created by
  1635. <a href="pcre2_maketables.html"><b>pcre2_maketables()</b>,</a>
  1636. which uses the <b>isspace()</b> function to identify space characters. In most
  1637. ASCII environments, the relevant characters are those with code points 0x0009
  1638. (tab), 0x000A (linefeed), 0x000B (vertical tab), 0x000C (formfeed), 0x000D
  1639. (carriage return), and 0x0020 (space).
  1640. </P>
  1641. <P>
  1642. When PCRE2 is compiled with Unicode support, in addition to these characters,
  1643. five more Unicode "Pattern White Space" characters are recognized by
  1644. PCRE2_EXTENDED. These are U+0085 (next line), U+200E (left-to-right mark),
  1645. U+200F (right-to-left mark), U+2028 (line separator), and U+2029 (paragraph
  1646. separator). This set of characters is the same as recognized by Perl's /x
  1647. option. Note that the horizontal and vertical space characters that are matched
  1648. by the \h and \v escapes in patterns are a much bigger set.
  1649. </P>
  1650. <P>
  1651. As well as ignoring most white space, PCRE2_EXTENDED also causes characters
  1652. between an unescaped # outside a character class and the next newline,
  1653. inclusive, to be ignored, which makes it possible to include comments inside
  1654. complicated patterns. Note that the end of this type of comment is a literal
  1655. newline sequence in the pattern; escape sequences that happen to represent a
  1656. newline do not count.
  1657. </P>
  1658. <P>
  1659. Which characters are interpreted as newlines can be specified by a setting in
  1660. the compile context that is passed to <b>pcre2_compile()</b> or by a special
  1661. sequence at the start of the pattern, as described in the section entitled
  1662. <a href="pcre2pattern.html#newlines">"Newline conventions"</a>
  1663. in the <b>pcre2pattern</b> documentation. A default is defined when PCRE2 is
  1664. built.
  1665. <pre>
  1666. PCRE2_EXTENDED_MORE
  1667. </pre>
  1668. This option has the effect of PCRE2_EXTENDED, but, in addition, unescaped space
  1669. and horizontal tab characters are ignored inside a character class. Note: only
  1670. these two characters are ignored, not the full set of pattern white space
  1671. characters that are ignored outside a character class. PCRE2_EXTENDED_MORE is
  1672. equivalent to Perl's /xx option, and it can be changed within a pattern by a
  1673. (?xx) option setting.
  1674. <pre>
  1675. PCRE2_FIRSTLINE
  1676. </pre>
  1677. If this option is set, the start of an unanchored pattern match must be before
  1678. or at the first newline in the subject string following the start of matching,
  1679. though the matched text may continue over the newline. If <i>startoffset</i> is
  1680. non-zero, the limiting newline is not necessarily the first newline in the
  1681. subject. For example, if the subject string is "abc\nxyz" (where \n
  1682. represents a single-character newline) a pattern match for "yz" succeeds with
  1683. PCRE2_FIRSTLINE if <i>startoffset</i> is greater than 3. See also
  1684. PCRE2_USE_OFFSET_LIMIT, which provides a more general limiting facility. If
  1685. PCRE2_FIRSTLINE is set with an offset limit, a match must occur in the first
  1686. line and also within the offset limit. In other words, whichever limit comes
  1687. first is used. This option has no effect for anchored patterns.
  1688. <pre>
  1689. PCRE2_LITERAL
  1690. </pre>
  1691. If this option is set, all meta-characters in the pattern are disabled, and it
  1692. is treated as a literal string. Matching literal strings with a regular
  1693. expression engine is not the most efficient way of doing it. If you are doing a
  1694. lot of literal matching and are worried about efficiency, you should consider
  1695. using other approaches. The only other main options that are allowed with
  1696. PCRE2_LITERAL are: PCRE2_ANCHORED, PCRE2_ENDANCHORED, PCRE2_AUTO_CALLOUT,
  1697. PCRE2_CASELESS, PCRE2_FIRSTLINE, PCRE2_MATCH_INVALID_UTF,
  1698. PCRE2_NO_START_OPTIMIZE, PCRE2_NO_UTF_CHECK, PCRE2_UTF, and
  1699. PCRE2_USE_OFFSET_LIMIT. The extra options PCRE2_EXTRA_MATCH_LINE and
  1700. PCRE2_EXTRA_MATCH_WORD are also supported. Any other options cause an error.
  1701. <pre>
  1702. PCRE2_MATCH_INVALID_UTF
  1703. </pre>
  1704. This option forces PCRE2_UTF (see below) and also enables support for matching
  1705. by <b>pcre2_match()</b> in subject strings that contain invalid UTF sequences.
  1706. Note, however, that the 16-bit and 32-bit PCRE2 libraries process strings as
  1707. sequences of uint16_t or uint32_t code points. They cannot find valid UTF
  1708. sequences within an arbitrary string of bytes unless such sequences are
  1709. suitably aligned. This facility is not supported for DFA matching. For details,
  1710. see the
  1711. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  1712. documentation.
  1713. <pre>
  1714. PCRE2_MATCH_UNSET_BACKREF
  1715. </pre>
  1716. If this option is set, a backreference to an unset capture group matches an
  1717. empty string (by default this causes the current matching alternative to fail).
  1718. A pattern such as (\1)(a) succeeds when this option is set (assuming it can
  1719. find an "a" in the subject), whereas it fails by default, for Perl
  1720. compatibility. Setting this option makes PCRE2 behave more like ECMAscript (aka
  1721. JavaScript).
  1722. <pre>
  1723. PCRE2_MULTILINE
  1724. </pre>
  1725. By default, for the purposes of matching "start of line" and "end of line",
  1726. PCRE2 treats the subject string as consisting of a single line of characters,
  1727. even if it actually contains newlines. The "start of line" metacharacter (^)
  1728. matches only at the start of the string, and the "end of line" metacharacter
  1729. ($) matches only at the end of the string, or before a terminating newline
  1730. (except when PCRE2_DOLLAR_ENDONLY is set). Note, however, that unless
  1731. PCRE2_DOTALL is set, the "any character" metacharacter (.) does not match at a
  1732. newline. This behaviour (for ^, $, and dot) is the same as Perl.
  1733. </P>
  1734. <P>
  1735. When PCRE2_MULTILINE it is set, the "start of line" and "end of line"
  1736. constructs match immediately following or immediately before internal newlines
  1737. in the subject string, respectively, as well as at the very start and end. This
  1738. is equivalent to Perl's /m option, and it can be changed within a pattern by a
  1739. (?m) option setting. Note that the "start of line" metacharacter does not match
  1740. after a newline at the end of the subject, for compatibility with Perl.
  1741. However, you can change this by setting the PCRE2_ALT_CIRCUMFLEX option. If
  1742. there are no newlines in a subject string, or no occurrences of ^ or $ in a
  1743. pattern, setting PCRE2_MULTILINE has no effect.
  1744. <pre>
  1745. PCRE2_NEVER_BACKSLASH_C
  1746. </pre>
  1747. This option locks out the use of \C in the pattern that is being compiled.
  1748. This escape can cause unpredictable behaviour in UTF-8 or UTF-16 modes, because
  1749. it may leave the current matching point in the middle of a multi-code-unit
  1750. character. This option may be useful in applications that process patterns from
  1751. external sources. Note that there is also a build-time option that permanently
  1752. locks out the use of \C.
  1753. <pre>
  1754. PCRE2_NEVER_UCP
  1755. </pre>
  1756. This option locks out the use of Unicode properties for handling \B, \b, \D,
  1757. \d, \S, \s, \W, \w, and some of the POSIX character classes, as described
  1758. for the PCRE2_UCP option below. In particular, it prevents the creator of the
  1759. pattern from enabling this facility by starting the pattern with (*UCP). This
  1760. option may be useful in applications that process patterns from external
  1761. sources. The option combination PCRE_UCP and PCRE_NEVER_UCP causes an error.
  1762. <pre>
  1763. PCRE2_NEVER_UTF
  1764. </pre>
  1765. This option locks out interpretation of the pattern as UTF-8, UTF-16, or
  1766. UTF-32, depending on which library is in use. In particular, it prevents the
  1767. creator of the pattern from switching to UTF interpretation by starting the
  1768. pattern with (*UTF). This option may be useful in applications that process
  1769. patterns from external sources. The combination of PCRE2_UTF and
  1770. PCRE2_NEVER_UTF causes an error.
  1771. <pre>
  1772. PCRE2_NO_AUTO_CAPTURE
  1773. </pre>
  1774. If this option is set, it disables the use of numbered capturing parentheses in
  1775. the pattern. Any opening parenthesis that is not followed by ? behaves as if it
  1776. were followed by ?: but named parentheses can still be used for capturing (and
  1777. they acquire numbers in the usual way). This is the same as Perl's /n option.
  1778. Note that, when this option is set, references to capture groups
  1779. (backreferences or recursion/subroutine calls) may only refer to named groups,
  1780. though the reference can be by name or by number.
  1781. <pre>
  1782. PCRE2_NO_AUTO_POSSESS
  1783. </pre>
  1784. If this option is set, it disables "auto-possessification", which is an
  1785. optimization that, for example, turns a+b into a++b in order to avoid
  1786. backtracks into a+ that can never be successful. However, if callouts are in
  1787. use, auto-possessification means that some callouts are never taken. You can
  1788. set this option if you want the matching functions to do a full unoptimized
  1789. search and run all the callouts, but it is mainly provided for testing
  1790. purposes.
  1791. <pre>
  1792. PCRE2_NO_DOTSTAR_ANCHOR
  1793. </pre>
  1794. If this option is set, it disables an optimization that is applied when .* is
  1795. the first significant item in a top-level branch of a pattern, and all the
  1796. other branches also start with .* or with \A or \G or ^. The optimization is
  1797. automatically disabled for .* if it is inside an atomic group or a capture
  1798. group that is the subject of a backreference, or if the pattern contains
  1799. (*PRUNE) or (*SKIP). When the optimization is not disabled, such a pattern is
  1800. automatically anchored if PCRE2_DOTALL is set for all the .* items and
  1801. PCRE2_MULTILINE is not set for any ^ items. Otherwise, the fact that any match
  1802. must start either at the start of the subject or following a newline is
  1803. remembered. Like other optimizations, this can cause callouts to be skipped.
  1804. <pre>
  1805. PCRE2_NO_START_OPTIMIZE
  1806. </pre>
  1807. This is an option whose main effect is at matching time. It does not change
  1808. what <b>pcre2_compile()</b> generates, but it does affect the output of the JIT
  1809. compiler.
  1810. </P>
  1811. <P>
  1812. There are a number of optimizations that may occur at the start of a match, in
  1813. order to speed up the process. For example, if it is known that an unanchored
  1814. match must start with a specific code unit value, the matching code searches
  1815. the subject for that value, and fails immediately if it cannot find it, without
  1816. actually running the main matching function. This means that a special item
  1817. such as (*COMMIT) at the start of a pattern is not considered until after a
  1818. suitable starting point for the match has been found. Also, when callouts or
  1819. (*MARK) items are in use, these "start-up" optimizations can cause them to be
  1820. skipped if the pattern is never actually used. The start-up optimizations are
  1821. in effect a pre-scan of the subject that takes place before the pattern is run.
  1822. </P>
  1823. <P>
  1824. The PCRE2_NO_START_OPTIMIZE option disables the start-up optimizations,
  1825. possibly causing performance to suffer, but ensuring that in cases where the
  1826. result is "no match", the callouts do occur, and that items such as (*COMMIT)
  1827. and (*MARK) are considered at every possible starting position in the subject
  1828. string.
  1829. </P>
  1830. <P>
  1831. Setting PCRE2_NO_START_OPTIMIZE may change the outcome of a matching operation.
  1832. Consider the pattern
  1833. <pre>
  1834. (*COMMIT)ABC
  1835. </pre>
  1836. When this is compiled, PCRE2 records the fact that a match must start with the
  1837. character "A". Suppose the subject string is "DEFABC". The start-up
  1838. optimization scans along the subject, finds "A" and runs the first match
  1839. attempt from there. The (*COMMIT) item means that the pattern must match the
  1840. current starting position, which in this case, it does. However, if the same
  1841. match is run with PCRE2_NO_START_OPTIMIZE set, the initial scan along the
  1842. subject string does not happen. The first match attempt is run starting from
  1843. "D" and when this fails, (*COMMIT) prevents any further matches being tried, so
  1844. the overall result is "no match".
  1845. </P>
  1846. <P>
  1847. As another start-up optimization makes use of a minimum length for a matching
  1848. subject, which is recorded when possible. Consider the pattern
  1849. <pre>
  1850. (*MARK:1)B(*MARK:2)(X|Y)
  1851. </pre>
  1852. The minimum length for a match is two characters. If the subject is "XXBB", the
  1853. "starting character" optimization skips "XX", then tries to match "BB", which
  1854. is long enough. In the process, (*MARK:2) is encountered and remembered. When
  1855. the match attempt fails, the next "B" is found, but there is only one character
  1856. left, so there are no more attempts, and "no match" is returned with the "last
  1857. mark seen" set to "2". If NO_START_OPTIMIZE is set, however, matches are tried
  1858. at every possible starting position, including at the end of the subject, where
  1859. (*MARK:1) is encountered, but there is no "B", so the "last mark seen" that is
  1860. returned is "1". In this case, the optimizations do not affect the overall
  1861. match result, which is still "no match", but they do affect the auxiliary
  1862. information that is returned.
  1863. <pre>
  1864. PCRE2_NO_UTF_CHECK
  1865. </pre>
  1866. When PCRE2_UTF is set, the validity of the pattern as a UTF string is
  1867. automatically checked. There are discussions about the validity of
  1868. <a href="pcre2unicode.html#utf8strings">UTF-8 strings,</a>
  1869. <a href="pcre2unicode.html#utf16strings">UTF-16 strings,</a>
  1870. and
  1871. <a href="pcre2unicode.html#utf32strings">UTF-32 strings</a>
  1872. in the
  1873. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  1874. document. If an invalid UTF sequence is found, <b>pcre2_compile()</b> returns a
  1875. negative error code.
  1876. </P>
  1877. <P>
  1878. If you know that your pattern is a valid UTF string, and you want to skip this
  1879. check for performance reasons, you can set the PCRE2_NO_UTF_CHECK option. When
  1880. it is set, the effect of passing an invalid UTF string as a pattern is
  1881. undefined. It may cause your program to crash or loop.
  1882. </P>
  1883. <P>
  1884. Note that this option can also be passed to <b>pcre2_match()</b> and
  1885. <b>pcre2_dfa_match()</b>, to suppress UTF validity checking of the subject
  1886. string.
  1887. </P>
  1888. <P>
  1889. Note also that setting PCRE2_NO_UTF_CHECK at compile time does not disable the
  1890. error that is given if an escape sequence for an invalid Unicode code point is
  1891. encountered in the pattern. In particular, the so-called "surrogate" code
  1892. points (0xd800 to 0xdfff) are invalid. If you want to allow escape sequences
  1893. such as \x{d800} you can set the PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES extra
  1894. option, as described in the section entitled "Extra compile options"
  1895. <a href="#extracompileoptions">below.</a>
  1896. However, this is possible only in UTF-8 and UTF-32 modes, because these values
  1897. are not representable in UTF-16.
  1898. <pre>
  1899. PCRE2_UCP
  1900. </pre>
  1901. This option has two effects. Firstly, it change the way PCRE2 processes \B,
  1902. \b, \D, \d, \S, \s, \W, \w, and some of the POSIX character classes. By
  1903. default, only ASCII characters are recognized, but if PCRE2_UCP is set, Unicode
  1904. properties are used to classify characters. There are some PCRE2_EXTRA
  1905. options (see below) that add finer control to this behaviour. More details are
  1906. given in the section on
  1907. <a href="pcre2pattern.html#genericchartypes">generic character types</a>
  1908. in the
  1909. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  1910. page.
  1911. </P>
  1912. <P>
  1913. The second effect of PCRE2_UCP is to force the use of Unicode properties for
  1914. upper/lower casing operations, even when PCRE2_UTF is not set. This makes it
  1915. possible to process strings in the 16-bit UCS-2 code. This option is available
  1916. only if PCRE2 has been compiled with Unicode support (which is the default).
  1917. The PCRE2_EXTRA_CASELESS_RESTRICT option (see below) restricts caseless
  1918. matching such that ASCII characters match only ASCII characters and non-ASCII
  1919. characters match only non-ASCII characters.
  1920. <pre>
  1921. PCRE2_UNGREEDY
  1922. </pre>
  1923. This option inverts the "greediness" of the quantifiers so that they are not
  1924. greedy by default, but become greedy if followed by "?". It is not compatible
  1925. with Perl. It can also be set by a (?U) option setting within the pattern.
  1926. <pre>
  1927. PCRE2_USE_OFFSET_LIMIT
  1928. </pre>
  1929. This option must be set for <b>pcre2_compile()</b> if
  1930. <b>pcre2_set_offset_limit()</b> is going to be used to set a non-default offset
  1931. limit in a match context for matches that use this pattern. An error is
  1932. generated if an offset limit is set without this option. For more details, see
  1933. the description of <b>pcre2_set_offset_limit()</b> in the
  1934. <a href="#matchcontext">section</a>
  1935. that describes match contexts. See also the PCRE2_FIRSTLINE
  1936. option above.
  1937. <pre>
  1938. PCRE2_UTF
  1939. </pre>
  1940. This option causes PCRE2 to regard both the pattern and the subject strings
  1941. that are subsequently processed as strings of UTF characters instead of
  1942. single-code-unit strings. It is available when PCRE2 is built to include
  1943. Unicode support (which is the default). If Unicode support is not available,
  1944. the use of this option provokes an error. Details of how PCRE2_UTF changes the
  1945. behaviour of PCRE2 are given in the
  1946. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  1947. page. In particular, note that it changes the way PCRE2_CASELESS works.
  1948. <a name="extracompileoptions"></a></P>
  1949. <br><b>
  1950. Extra compile options
  1951. </b><br>
  1952. <P>
  1953. The option bits that can be set in a compile context by calling the
  1954. <b>pcre2_set_compile_extra_options()</b> function are as follows:
  1955. <pre>
  1956. PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK
  1957. </pre>
  1958. Since release 10.38 PCRE2 has forbidden the use of \K within lookaround
  1959. assertions, following Perl's lead. This option is provided to re-enable the
  1960. previous behaviour (act in positive lookarounds, ignore in negative ones) in
  1961. case anybody is relying on it.
  1962. <pre>
  1963. PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES
  1964. </pre>
  1965. This option applies when compiling a pattern in UTF-8 or UTF-32 mode. It is
  1966. forbidden in UTF-16 mode, and ignored in non-UTF modes. Unicode "surrogate"
  1967. code points in the range 0xd800 to 0xdfff are used in pairs in UTF-16 to encode
  1968. code points with values in the range 0x10000 to 0x10ffff. The surrogates cannot
  1969. therefore be represented in UTF-16. They can be represented in UTF-8 and
  1970. UTF-32, but are defined as invalid code points, and cause errors if encountered
  1971. in a UTF-8 or UTF-32 string that is being checked for validity by PCRE2.
  1972. </P>
  1973. <P>
  1974. These values also cause errors if encountered in escape sequences such as
  1975. \x{d912} within a pattern. However, it seems that some applications, when
  1976. using PCRE2 to check for unwanted characters in UTF-8 strings, explicitly test
  1977. for the surrogates using escape sequences. The PCRE2_NO_UTF_CHECK option does
  1978. not disable the error that occurs, because it applies only to the testing of
  1979. input strings for UTF validity.
  1980. </P>
  1981. <P>
  1982. If the extra option PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES is set, surrogate code
  1983. point values in UTF-8 and UTF-32 patterns no longer provoke errors and are
  1984. incorporated in the compiled pattern. However, they can only match subject
  1985. characters if the matching function is called with PCRE2_NO_UTF_CHECK set.
  1986. <pre>
  1987. PCRE2_EXTRA_ALT_BSUX
  1988. </pre>
  1989. The original option PCRE2_ALT_BSUX causes PCRE2 to process \U, \u, and \x in
  1990. the way that ECMAscript (aka JavaScript) does. Additional functionality was
  1991. defined by ECMAscript 6; setting PCRE2_EXTRA_ALT_BSUX has the effect of
  1992. PCRE2_ALT_BSUX, but in addition it recognizes \u{hhh..} as a hexadecimal
  1993. character code, where hhh.. is any number of hexadecimal digits.
  1994. <pre>
  1995. PCRE2_EXTRA_ASCII_BSD
  1996. </pre>
  1997. This option forces \d to match only ASCII digits, even when PCRE2_UCP is set.
  1998. It can be changed within a pattern by means of the (?aD) option setting.
  1999. <pre>
  2000. PCRE2_EXTRA_ASCII_BSS
  2001. </pre>
  2002. This option forces \s to match only ASCII space characters, even when
  2003. PCRE2_UCP is set. It can be changed within a pattern by means of the (?aS)
  2004. option setting.
  2005. <pre>
  2006. PCRE2_EXTRA_ASCII_BSW
  2007. </pre>
  2008. This option forces \w to match only ASCII word characters, even when PCRE2_UCP
  2009. is set. It can be changed within a pattern by means of the (?aW) option
  2010. setting.
  2011. <pre>
  2012. PCRE2_EXTRA_ASCII_DIGIT
  2013. </pre>
  2014. This option forces the POSIX character classes [:digit:] and [:xdigit:] to
  2015. match only ASCII digits, even when PCRE2_UCP is set. It can be changed within
  2016. a pattern by means of the (?aT) option setting.
  2017. <pre>
  2018. PCRE2_EXTRA_ASCII_POSIX
  2019. </pre>
  2020. This option forces all the POSIX character classes, including [:digit:] and
  2021. [:xdigit:], to match only ASCII characters, even when PCRE2_UCP is set. It can
  2022. be changed within a pattern by means of the (?aP) option setting, but note that
  2023. this also sets PCRE2_EXTRA_ASCII_DIGIT in order to ensure that (?-aP) unsets
  2024. all ASCII restrictions for POSIX classes.
  2025. <pre>
  2026. PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL
  2027. </pre>
  2028. This is a dangerous option. Use with care. By default, an unrecognized escape
  2029. such as \j or a malformed one such as \x{2z} causes a compile-time error when
  2030. detected by <b>pcre2_compile()</b>. Perl is somewhat inconsistent in handling
  2031. such items: for example, \j is treated as a literal "j", and non-hexadecimal
  2032. digits in \x{} are just ignored, though warnings are given in both cases if
  2033. Perl's warning switch is enabled. However, a malformed octal number after \o{
  2034. always causes an error in Perl.
  2035. </P>
  2036. <P>
  2037. If the PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL extra option is passed to
  2038. <b>pcre2_compile()</b>, all unrecognized or malformed escape sequences are
  2039. treated as single-character escapes. For example, \j is a literal "j" and
  2040. \x{2z} is treated as the literal string "x{2z}". Setting this option means
  2041. that typos in patterns may go undetected and have unexpected results. Also note
  2042. that a sequence such as [\N{] is interpreted as a malformed attempt at
  2043. [\N{...}] and so is treated as [N{] whereas [\N] gives an error because an
  2044. unqualified \N is a valid escape sequence but is not supported in a character
  2045. class. To reiterate: this is a dangerous option. Use with great care.
  2046. <pre>
  2047. PCRE2_EXTRA_CASELESS_RESTRICT
  2048. </pre>
  2049. When either PCRE2_UCP or PCRE2_UTF is set, caseless matching follows Unicode
  2050. rules, which allow for more than two cases per character. There are two
  2051. case-equivalent character sets that contain both ASCII and non-ASCII
  2052. characters. The ASCII letter S is case-equivalent to U+017f (long S) and the
  2053. ASCII letter K is case-equivalent to U+212a (Kelvin sign). This option disables
  2054. recognition of case-equivalences that cross the ASCII/non-ASCII boundary. In a
  2055. caseless match, both characters must either be ASCII or non-ASCII. The option
  2056. can be changed with a pattern by the (?r) option setting.
  2057. <pre>
  2058. PCRE2_EXTRA_ESCAPED_CR_IS_LF
  2059. </pre>
  2060. There are some legacy applications where the escape sequence \r in a pattern
  2061. is expected to match a newline. If this option is set, \r in a pattern is
  2062. converted to \n so that it matches a LF (linefeed) instead of a CR (carriage
  2063. return) character. The option does not affect a literal CR in the pattern, nor
  2064. does it affect CR specified as an explicit code point such as \x{0D}.
  2065. <pre>
  2066. PCRE2_EXTRA_MATCH_LINE
  2067. </pre>
  2068. This option is provided for use by the <b>-x</b> option of <b>pcre2grep</b>. It
  2069. causes the pattern only to match complete lines. This is achieved by
  2070. automatically inserting the code for "^(?:" at the start of the compiled
  2071. pattern and ")$" at the end. Thus, when PCRE2_MULTILINE is set, the matched
  2072. line may be in the middle of the subject string. This option can be used with
  2073. PCRE2_LITERAL.
  2074. <pre>
  2075. PCRE2_EXTRA_MATCH_WORD
  2076. </pre>
  2077. This option is provided for use by the <b>-w</b> option of <b>pcre2grep</b>. It
  2078. causes the pattern only to match strings that have a word boundary at the start
  2079. and the end. This is achieved by automatically inserting the code for "\b(?:"
  2080. at the start of the compiled pattern and ")\b" at the end. The option may be
  2081. used with PCRE2_LITERAL. However, it is ignored if PCRE2_EXTRA_MATCH_LINE is
  2082. also set.
  2083. <a name="jitcompiling"></a></P>
  2084. <br><a name="SEC21" href="#TOC1">JUST-IN-TIME (JIT) COMPILATION</a><br>
  2085. <P>
  2086. <b>int pcre2_jit_compile(pcre2_code *<i>code</i>, uint32_t <i>options</i>);</b>
  2087. <br>
  2088. <br>
  2089. <b>int pcre2_jit_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  2090. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  2091. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  2092. <b> pcre2_match_context *<i>mcontext</i>);</b>
  2093. <br>
  2094. <br>
  2095. <b>void pcre2_jit_free_unused_memory(pcre2_general_context *<i>gcontext</i>);</b>
  2096. <br>
  2097. <br>
  2098. <b>pcre2_jit_stack *pcre2_jit_stack_create(size_t <i>startsize</i>,</b>
  2099. <b> size_t <i>maxsize</i>, pcre2_general_context *<i>gcontext</i>);</b>
  2100. <br>
  2101. <br>
  2102. <b>void pcre2_jit_stack_assign(pcre2_match_context *<i>mcontext</i>,</b>
  2103. <b> pcre2_jit_callback <i>callback_function</i>, void *<i>callback_data</i>);</b>
  2104. <br>
  2105. <br>
  2106. <b>void pcre2_jit_stack_free(pcre2_jit_stack *<i>jit_stack</i>);</b>
  2107. </P>
  2108. <P>
  2109. These functions provide support for JIT compilation, which, if the just-in-time
  2110. compiler is available, further processes a compiled pattern into machine code
  2111. that executes much faster than the <b>pcre2_match()</b> interpretive matching
  2112. function. Full details are given in the
  2113. <a href="pcre2jit.html"><b>pcre2jit</b></a>
  2114. documentation.
  2115. </P>
  2116. <P>
  2117. JIT compilation is a heavyweight optimization. It can take some time for
  2118. patterns to be analyzed, and for one-off matches and simple patterns the
  2119. benefit of faster execution might be offset by a much slower compilation time.
  2120. Most (but not all) patterns can be optimized by the JIT compiler.
  2121. <a name="localesupport"></a></P>
  2122. <br><a name="SEC22" href="#TOC1">LOCALE SUPPORT</a><br>
  2123. <P>
  2124. <b>const uint8_t *pcre2_maketables(pcre2_general_context *<i>gcontext</i>);</b>
  2125. <br>
  2126. <br>
  2127. <b>void pcre2_maketables_free(pcre2_general_context *<i>gcontext</i>,</b>
  2128. <b> const uint8_t *<i>tables</i>);</b>
  2129. </P>
  2130. <P>
  2131. PCRE2 handles caseless matching, and determines whether characters are letters,
  2132. digits, or whatever, by reference to a set of tables, indexed by character code
  2133. point. However, this applies only to characters whose code points are less than
  2134. 256. By default, higher-valued code points never match escapes such as \w or
  2135. \d.
  2136. </P>
  2137. <P>
  2138. When PCRE2 is built with Unicode support (the default), certain Unicode
  2139. character properties can be tested with \p and \P, or, alternatively, the
  2140. PCRE2_UCP option can be set when a pattern is compiled; this causes \w and
  2141. friends to use Unicode property support instead of the built-in tables.
  2142. PCRE2_UCP also causes upper/lower casing operations on characters with code
  2143. points greater than 127 to use Unicode properties. These effects apply even
  2144. when PCRE2_UTF is not set. There are, however, some PCRE2_EXTRA options (see
  2145. above) that can be used to modify or suppress them.
  2146. </P>
  2147. <P>
  2148. The use of locales with Unicode is discouraged. If you are handling characters
  2149. with code points greater than 127, you should either use Unicode support, or
  2150. use locales, but not try to mix the two.
  2151. </P>
  2152. <P>
  2153. PCRE2 contains a built-in set of character tables that are used by default.
  2154. These are sufficient for many applications. Normally, the internal tables
  2155. recognize only ASCII characters. However, when PCRE2 is built, it is possible
  2156. to cause the internal tables to be rebuilt in the default "C" locale of the
  2157. local system, which may cause them to be different.
  2158. </P>
  2159. <P>
  2160. The built-in tables can be overridden by tables supplied by the application
  2161. that calls PCRE2. These may be created in a different locale from the default.
  2162. As more and more applications change to using Unicode, the need for this locale
  2163. support is expected to die away.
  2164. </P>
  2165. <P>
  2166. External tables are built by calling the <b>pcre2_maketables()</b> function, in
  2167. the relevant locale. The only argument to this function is a general context,
  2168. which can be used to pass a custom memory allocator. If the argument is NULL,
  2169. the system <b>malloc()</b> is used. The result can be passed to
  2170. <b>pcre2_compile()</b> as often as necessary, by creating a compile context and
  2171. calling <b>pcre2_set_character_tables()</b> to set the tables pointer therein.
  2172. </P>
  2173. <P>
  2174. For example, to build and use tables that are appropriate for the French locale
  2175. (where accented characters with values greater than 127 are treated as
  2176. letters), the following code could be used:
  2177. <pre>
  2178. setlocale(LC_CTYPE, "fr_FR");
  2179. tables = pcre2_maketables(NULL);
  2180. ccontext = pcre2_compile_context_create(NULL);
  2181. pcre2_set_character_tables(ccontext, tables);
  2182. re = pcre2_compile(..., ccontext);
  2183. </pre>
  2184. The locale name "fr_FR" is used on Linux and other Unix-like systems; if you
  2185. are using Windows, the name for the French locale is "french".
  2186. </P>
  2187. <P>
  2188. The pointer that is passed (via the compile context) to <b>pcre2_compile()</b>
  2189. is saved with the compiled pattern, and the same tables are used by the
  2190. matching functions. Thus, for any single pattern, compilation and matching both
  2191. happen in the same locale, but different patterns can be processed in different
  2192. locales.
  2193. </P>
  2194. <P>
  2195. It is the caller's responsibility to ensure that the memory containing the
  2196. tables remains available while they are still in use. When they are no longer
  2197. needed, you can discard them using <b>pcre2_maketables_free()</b>, which should
  2198. pass as its first parameter the same global context that was used to create the
  2199. tables.
  2200. </P>
  2201. <br><b>
  2202. Saving locale tables
  2203. </b><br>
  2204. <P>
  2205. The tables described above are just a sequence of binary bytes, which makes
  2206. them independent of hardware characteristics such as endianness or whether the
  2207. processor is 32-bit or 64-bit. A copy of the result of <b>pcre2_maketables()</b>
  2208. can therefore be saved in a file or elsewhere and re-used later, even in a
  2209. different program or on another computer. The size of the tables (number of
  2210. bytes) must be obtained by calling <b>pcre2_config()</b> with the
  2211. PCRE2_CONFIG_TABLES_LENGTH option because <b>pcre2_maketables()</b> does not
  2212. return this value. Note that the <b>pcre2_dftables</b> program, which is part of
  2213. the PCRE2 build system, can be used stand-alone to create a file that contains
  2214. a set of binary tables. See the
  2215. <a href="pcre2build.html#createtables"><b>pcre2build</b></a>
  2216. documentation for details.
  2217. <a name="infoaboutpattern"></a></P>
  2218. <br><a name="SEC23" href="#TOC1">INFORMATION ABOUT A COMPILED PATTERN</a><br>
  2219. <P>
  2220. <b>int pcre2_pattern_info(const pcre2 *<i>code</i>, uint32_t <i>what</i>, void *<i>where</i>);</b>
  2221. </P>
  2222. <P>
  2223. The <b>pcre2_pattern_info()</b> function returns general information about a
  2224. compiled pattern. For information about callouts, see the
  2225. <a href="#infoaboutcallouts">next section.</a>
  2226. The first argument for <b>pcre2_pattern_info()</b> is a pointer to the compiled
  2227. pattern. The second argument specifies which piece of information is required,
  2228. and the third argument is a pointer to a variable to receive the data. If the
  2229. third argument is NULL, the first argument is ignored, and the function returns
  2230. the size in bytes of the variable that is required for the information
  2231. requested. Otherwise, the yield of the function is zero for success, or one of
  2232. the following negative numbers:
  2233. <pre>
  2234. PCRE2_ERROR_NULL the argument <i>code</i> was NULL
  2235. PCRE2_ERROR_BADMAGIC the "magic number" was not found
  2236. PCRE2_ERROR_BADOPTION the value of <i>what</i> was invalid
  2237. PCRE2_ERROR_UNSET the requested field is not set
  2238. </pre>
  2239. The "magic number" is placed at the start of each compiled pattern as a simple
  2240. check against passing an arbitrary memory pointer. Here is a typical call of
  2241. <b>pcre2_pattern_info()</b>, to obtain the length of the compiled pattern:
  2242. <pre>
  2243. int rc;
  2244. size_t length;
  2245. rc = pcre2_pattern_info(
  2246. re, /* result of pcre2_compile() */
  2247. PCRE2_INFO_SIZE, /* what is required */
  2248. &length); /* where to put the data */
  2249. </pre>
  2250. The possible values for the second argument are defined in <b>pcre2.h</b>, and
  2251. are as follows:
  2252. <pre>
  2253. PCRE2_INFO_ALLOPTIONS
  2254. PCRE2_INFO_ARGOPTIONS
  2255. PCRE2_INFO_EXTRAOPTIONS
  2256. </pre>
  2257. Return copies of the pattern's options. The third argument should point to a
  2258. <b>uint32_t</b> variable. PCRE2_INFO_ARGOPTIONS returns exactly the options that
  2259. were passed to <b>pcre2_compile()</b>, whereas PCRE2_INFO_ALLOPTIONS returns
  2260. the compile options as modified by any top-level (*XXX) option settings such as
  2261. (*UTF) at the start of the pattern itself. PCRE2_INFO_EXTRAOPTIONS returns the
  2262. extra options that were set in the compile context by calling the
  2263. pcre2_set_compile_extra_options() function.
  2264. </P>
  2265. <P>
  2266. For example, if the pattern /(*UTF)abc/ is compiled with the PCRE2_EXTENDED
  2267. option, the result for PCRE2_INFO_ALLOPTIONS is PCRE2_EXTENDED and PCRE2_UTF.
  2268. Option settings such as (?i) that can change within a pattern do not affect the
  2269. result of PCRE2_INFO_ALLOPTIONS, even if they appear right at the start of the
  2270. pattern. (This was different in some earlier releases.)
  2271. </P>
  2272. <P>
  2273. A pattern compiled without PCRE2_ANCHORED is automatically anchored by PCRE2 if
  2274. the first significant item in every top-level branch is one of the following:
  2275. <pre>
  2276. ^ unless PCRE2_MULTILINE is set
  2277. \A always
  2278. \G always
  2279. .* sometimes - see below
  2280. </pre>
  2281. When .* is the first significant item, anchoring is possible only when all the
  2282. following are true:
  2283. <pre>
  2284. .* is not in an atomic group
  2285. .* is not in a capture group that is the subject of a backreference
  2286. PCRE2_DOTALL is in force for .*
  2287. Neither (*PRUNE) nor (*SKIP) appears in the pattern
  2288. PCRE2_NO_DOTSTAR_ANCHOR is not set
  2289. </pre>
  2290. For patterns that are auto-anchored, the PCRE2_ANCHORED bit is set in the
  2291. options returned for PCRE2_INFO_ALLOPTIONS.
  2292. <pre>
  2293. PCRE2_INFO_BACKREFMAX
  2294. </pre>
  2295. Return the number of the highest backreference in the pattern. The third
  2296. argument should point to a <b>uint32_t</b> variable. Named capture groups
  2297. acquire numbers as well as names, and these count towards the highest
  2298. backreference. Backreferences such as \4 or \g{12} match the captured
  2299. characters of the given group, but in addition, the check that a capture
  2300. group is set in a conditional group such as (?(3)a|b) is also a backreference.
  2301. Zero is returned if there are no backreferences.
  2302. <pre>
  2303. PCRE2_INFO_BSR
  2304. </pre>
  2305. The output is a uint32_t integer whose value indicates what character sequences
  2306. the \R escape sequence matches. A value of PCRE2_BSR_UNICODE means that \R
  2307. matches any Unicode line ending sequence; a value of PCRE2_BSR_ANYCRLF means
  2308. that \R matches only CR, LF, or CRLF.
  2309. <pre>
  2310. PCRE2_INFO_CAPTURECOUNT
  2311. </pre>
  2312. Return the highest capture group number in the pattern. In patterns where (?|
  2313. is not used, this is also the total number of capture groups. The third
  2314. argument should point to a <b>uint32_t</b> variable.
  2315. <pre>
  2316. PCRE2_INFO_DEPTHLIMIT
  2317. </pre>
  2318. If the pattern set a backtracking depth limit by including an item of the form
  2319. (*LIMIT_DEPTH=nnnn) at the start, the value is returned. The third argument
  2320. should point to a uint32_t integer. If no such value has been set, the call to
  2321. <b>pcre2_pattern_info()</b> returns the error PCRE2_ERROR_UNSET. Note that this
  2322. limit will only be used during matching if it is less than the limit set or
  2323. defaulted by the caller of the match function.
  2324. <pre>
  2325. PCRE2_INFO_FIRSTBITMAP
  2326. </pre>
  2327. In the absence of a single first code unit for a non-anchored pattern,
  2328. <b>pcre2_compile()</b> may construct a 256-bit table that defines a fixed set of
  2329. values for the first code unit in any match. For example, a pattern that starts
  2330. with [abc] results in a table with three bits set. When code unit values
  2331. greater than 255 are supported, the flag bit for 255 means "any code unit of
  2332. value 255 or above". If such a table was constructed, a pointer to it is
  2333. returned. Otherwise NULL is returned. The third argument should point to a
  2334. <b>const uint8_t *</b> variable.
  2335. <pre>
  2336. PCRE2_INFO_FIRSTCODETYPE
  2337. </pre>
  2338. Return information about the first code unit of any matched string, for a
  2339. non-anchored pattern. The third argument should point to a <b>uint32_t</b>
  2340. variable. If there is a fixed first value, for example, the letter "c" from a
  2341. pattern such as (cat|cow|coyote), 1 is returned, and the value can be retrieved
  2342. using PCRE2_INFO_FIRSTCODEUNIT. If there is no fixed first value, but it is
  2343. known that a match can occur only at the start of the subject or following a
  2344. newline in the subject, 2 is returned. Otherwise, and for anchored patterns, 0
  2345. is returned.
  2346. <pre>
  2347. PCRE2_INFO_FIRSTCODEUNIT
  2348. </pre>
  2349. Return the value of the first code unit of any matched string for a pattern
  2350. where PCRE2_INFO_FIRSTCODETYPE returns 1; otherwise return 0. The third
  2351. argument should point to a <b>uint32_t</b> variable. In the 8-bit library, the
  2352. value is always less than 256. In the 16-bit library the value can be up to
  2353. 0xffff. In the 32-bit library in UTF-32 mode the value can be up to 0x10ffff,
  2354. and up to 0xffffffff when not using UTF-32 mode.
  2355. <pre>
  2356. PCRE2_INFO_FRAMESIZE
  2357. </pre>
  2358. Return the size (in bytes) of the data frames that are used to remember
  2359. backtracking positions when the pattern is processed by <b>pcre2_match()</b>
  2360. without the use of JIT. The third argument should point to a <b>size_t</b>
  2361. variable. The frame size depends on the number of capturing parentheses in the
  2362. pattern. Each additional capture group adds two PCRE2_SIZE variables.
  2363. <pre>
  2364. PCRE2_INFO_HASBACKSLASHC
  2365. </pre>
  2366. Return 1 if the pattern contains any instances of \C, otherwise 0. The third
  2367. argument should point to a <b>uint32_t</b> variable.
  2368. <pre>
  2369. PCRE2_INFO_HASCRORLF
  2370. </pre>
  2371. Return 1 if the pattern contains any explicit matches for CR or LF characters,
  2372. otherwise 0. The third argument should point to a <b>uint32_t</b> variable. An
  2373. explicit match is either a literal CR or LF character, or \r or \n or one of
  2374. the equivalent hexadecimal or octal escape sequences.
  2375. <pre>
  2376. PCRE2_INFO_HEAPLIMIT
  2377. </pre>
  2378. If the pattern set a heap memory limit by including an item of the form
  2379. (*LIMIT_HEAP=nnnn) at the start, the value is returned. The third argument
  2380. should point to a uint32_t integer. If no such value has been set, the call to
  2381. <b>pcre2_pattern_info()</b> returns the error PCRE2_ERROR_UNSET. Note that this
  2382. limit will only be used during matching if it is less than the limit set or
  2383. defaulted by the caller of the match function.
  2384. <pre>
  2385. PCRE2_INFO_JCHANGED
  2386. </pre>
  2387. Return 1 if the (?J) or (?-J) option setting is used in the pattern, otherwise
  2388. 0. The third argument should point to a <b>uint32_t</b> variable. (?J) and
  2389. (?-J) set and unset the local PCRE2_DUPNAMES option, respectively.
  2390. <pre>
  2391. PCRE2_INFO_JITSIZE
  2392. </pre>
  2393. If the compiled pattern was successfully processed by
  2394. <b>pcre2_jit_compile()</b>, return the size of the JIT compiled code, otherwise
  2395. return zero. The third argument should point to a <b>size_t</b> variable.
  2396. <pre>
  2397. PCRE2_INFO_LASTCODETYPE
  2398. </pre>
  2399. Returns 1 if there is a rightmost literal code unit that must exist in any
  2400. matched string, other than at its start. The third argument should point to a
  2401. <b>uint32_t</b> variable. If there is no such value, 0 is returned. When 1 is
  2402. returned, the code unit value itself can be retrieved using
  2403. PCRE2_INFO_LASTCODEUNIT. For anchored patterns, a last literal value is
  2404. recorded only if it follows something of variable length. For example, for the
  2405. pattern /^a\d+z\d+/ the returned value is 1 (with "z" returned from
  2406. PCRE2_INFO_LASTCODEUNIT), but for /^a\dz\d/ the returned value is 0.
  2407. <pre>
  2408. PCRE2_INFO_LASTCODEUNIT
  2409. </pre>
  2410. Return the value of the rightmost literal code unit that must exist in any
  2411. matched string, other than at its start, for a pattern where
  2412. PCRE2_INFO_LASTCODETYPE returns 1. Otherwise, return 0. The third argument
  2413. should point to a <b>uint32_t</b> variable.
  2414. <pre>
  2415. PCRE2_INFO_MATCHEMPTY
  2416. </pre>
  2417. Return 1 if the pattern might match an empty string, otherwise 0. The third
  2418. argument should point to a <b>uint32_t</b> variable. When a pattern contains
  2419. recursive subroutine calls it is not always possible to determine whether or
  2420. not it can match an empty string. PCRE2 takes a cautious approach and returns 1
  2421. in such cases.
  2422. <pre>
  2423. PCRE2_INFO_MATCHLIMIT
  2424. </pre>
  2425. If the pattern set a match limit by including an item of the form
  2426. (*LIMIT_MATCH=nnnn) at the start, the value is returned. The third argument
  2427. should point to a uint32_t integer. If no such value has been set, the call to
  2428. <b>pcre2_pattern_info()</b> returns the error PCRE2_ERROR_UNSET. Note that this
  2429. limit will only be used during matching if it is less than the limit set or
  2430. defaulted by the caller of the match function.
  2431. <pre>
  2432. PCRE2_INFO_MAXLOOKBEHIND
  2433. </pre>
  2434. A lookbehind assertion moves back a certain number of characters (not code
  2435. units) when it starts to process each of its branches. This request returns the
  2436. largest of these backward moves. The third argument should point to a uint32_t
  2437. integer. The simple assertions \b and \B require a one-character lookbehind
  2438. and cause PCRE2_INFO_MAXLOOKBEHIND to return 1 in the absence of anything
  2439. longer. \A also registers a one-character lookbehind, though it does not
  2440. actually inspect the previous character.
  2441. </P>
  2442. <P>
  2443. Note that this information is useful for multi-segment matching only
  2444. if the pattern contains no nested lookbehinds. For example, the pattern
  2445. (?&#60;=a(?&#60;=ba)c) returns a maximum lookbehind of 2, but when it is processed, the
  2446. first lookbehind moves back by two characters, matches one character, then the
  2447. nested lookbehind also moves back by two characters. This puts the matching
  2448. point three characters earlier than it was at the start.
  2449. PCRE2_INFO_MAXLOOKBEHIND is really only useful as a debugging tool. See the
  2450. <a href="pcre2partial.html"><b>pcre2partial</b></a>
  2451. documentation for a discussion of multi-segment matching.
  2452. <pre>
  2453. PCRE2_INFO_MINLENGTH
  2454. </pre>
  2455. If a minimum length for matching subject strings was computed, its value is
  2456. returned. Otherwise the returned value is 0. This value is not computed when
  2457. PCRE2_NO_START_OPTIMIZE is set. The value is a number of characters, which in
  2458. UTF mode may be different from the number of code units. The third argument
  2459. should point to a <b>uint32_t</b> variable. The value is a lower bound to the
  2460. length of any matching string. There may not be any strings of that length that
  2461. do actually match, but every string that does match is at least that long.
  2462. <pre>
  2463. PCRE2_INFO_NAMECOUNT
  2464. PCRE2_INFO_NAMEENTRYSIZE
  2465. PCRE2_INFO_NAMETABLE
  2466. </pre>
  2467. PCRE2 supports the use of named as well as numbered capturing parentheses. The
  2468. names are just an additional way of identifying the parentheses, which still
  2469. acquire numbers. Several convenience functions such as
  2470. <b>pcre2_substring_get_byname()</b> are provided for extracting captured
  2471. substrings by name. It is also possible to extract the data directly, by first
  2472. converting the name to a number in order to access the correct pointers in the
  2473. output vector (described with <b>pcre2_match()</b> below). To do the conversion,
  2474. you need to use the name-to-number map, which is described by these three
  2475. values.
  2476. </P>
  2477. <P>
  2478. The map consists of a number of fixed-size entries. PCRE2_INFO_NAMECOUNT gives
  2479. the number of entries, and PCRE2_INFO_NAMEENTRYSIZE gives the size of each
  2480. entry in code units; both of these return a <b>uint32_t</b> value. The entry
  2481. size depends on the length of the longest name.
  2482. </P>
  2483. <P>
  2484. PCRE2_INFO_NAMETABLE returns a pointer to the first entry of the table. This is
  2485. a PCRE2_SPTR pointer to a block of code units. In the 8-bit library, the first
  2486. two bytes of each entry are the number of the capturing parenthesis, most
  2487. significant byte first. In the 16-bit library, the pointer points to 16-bit
  2488. code units, the first of which contains the parenthesis number. In the 32-bit
  2489. library, the pointer points to 32-bit code units, the first of which contains
  2490. the parenthesis number. The rest of the entry is the corresponding name, zero
  2491. terminated.
  2492. </P>
  2493. <P>
  2494. The names are in alphabetical order. If (?| is used to create multiple capture
  2495. groups with the same number, as described in the
  2496. <a href="pcre2pattern.html#dupgroupnumber">section on duplicate group numbers</a>
  2497. in the
  2498. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  2499. page, the groups may be given the same name, but there is only one entry in the
  2500. table. Different names for groups of the same number are not permitted.
  2501. </P>
  2502. <P>
  2503. Duplicate names for capture groups with different numbers are permitted, but
  2504. only if PCRE2_DUPNAMES is set. They appear in the table in the order in which
  2505. they were found in the pattern. In the absence of (?| this is the order of
  2506. increasing number; when (?| is used this is not necessarily the case because
  2507. later capture groups may have lower numbers.
  2508. </P>
  2509. <P>
  2510. As a simple example of the name/number table, consider the following pattern
  2511. after compilation by the 8-bit library (assume PCRE2_EXTENDED is set, so white
  2512. space - including newlines - is ignored):
  2513. <pre>
  2514. (?&#60;date&#62; (?&#60;year&#62;(\d\d)?\d\d) - (?&#60;month&#62;\d\d) - (?&#60;day&#62;\d\d) )
  2515. </pre>
  2516. There are four named capture groups, so the table has four entries, and each
  2517. entry in the table is eight bytes long. The table is as follows, with
  2518. non-printing bytes shows in hexadecimal, and undefined bytes shown as ??:
  2519. <pre>
  2520. 00 01 d a t e 00 ??
  2521. 00 05 d a y 00 ?? ??
  2522. 00 04 m o n t h 00
  2523. 00 02 y e a r 00 ??
  2524. </pre>
  2525. When writing code to extract data from named capture groups using the
  2526. name-to-number map, remember that the length of the entries is likely to be
  2527. different for each compiled pattern.
  2528. <pre>
  2529. PCRE2_INFO_NEWLINE
  2530. </pre>
  2531. The output is one of the following <b>uint32_t</b> values:
  2532. <pre>
  2533. PCRE2_NEWLINE_CR Carriage return (CR)
  2534. PCRE2_NEWLINE_LF Linefeed (LF)
  2535. PCRE2_NEWLINE_CRLF Carriage return, linefeed (CRLF)
  2536. PCRE2_NEWLINE_ANY Any Unicode line ending
  2537. PCRE2_NEWLINE_ANYCRLF Any of CR, LF, or CRLF
  2538. PCRE2_NEWLINE_NUL The NUL character (binary zero)
  2539. </pre>
  2540. This identifies the character sequence that will be recognized as meaning
  2541. "newline" while matching.
  2542. <pre>
  2543. PCRE2_INFO_SIZE
  2544. </pre>
  2545. Return the size of the compiled pattern in bytes (for all three libraries). The
  2546. third argument should point to a <b>size_t</b> variable. This value includes the
  2547. size of the general data block that precedes the code units of the compiled
  2548. pattern itself. The value that is used when <b>pcre2_compile()</b> is getting
  2549. memory in which to place the compiled pattern may be slightly larger than the
  2550. value returned by this option, because there are cases where the code that
  2551. calculates the size has to over-estimate. Processing a pattern with the JIT
  2552. compiler does not alter the value returned by this option.
  2553. <a name="infoaboutcallouts"></a></P>
  2554. <br><a name="SEC24" href="#TOC1">INFORMATION ABOUT A PATTERN'S CALLOUTS</a><br>
  2555. <P>
  2556. <b>int pcre2_callout_enumerate(const pcre2_code *<i>code</i>,</b>
  2557. <b> int (*<i>callback</i>)(pcre2_callout_enumerate_block *, void *),</b>
  2558. <b> void *<i>user_data</i>);</b>
  2559. <br>
  2560. <br>
  2561. A script language that supports the use of string arguments in callouts might
  2562. like to scan all the callouts in a pattern before running the match. This can
  2563. be done by calling <b>pcre2_callout_enumerate()</b>. The first argument is a
  2564. pointer to a compiled pattern, the second points to a callback function, and
  2565. the third is arbitrary user data. The callback function is called for every
  2566. callout in the pattern in the order in which they appear. Its first argument is
  2567. a pointer to a callout enumeration block, and its second argument is the
  2568. <i>user_data</i> value that was passed to <b>pcre2_callout_enumerate()</b>. The
  2569. contents of the callout enumeration block are described in the
  2570. <a href="pcre2callout.html"><b>pcre2callout</b></a>
  2571. documentation, which also gives further details about callouts.
  2572. </P>
  2573. <br><a name="SEC25" href="#TOC1">SERIALIZATION AND PRECOMPILING</a><br>
  2574. <P>
  2575. It is possible to save compiled patterns on disc or elsewhere, and reload them
  2576. later, subject to a number of restrictions. The host on which the patterns are
  2577. reloaded must be running the same version of PCRE2, with the same code unit
  2578. width, and must also have the same endianness, pointer width, and PCRE2_SIZE
  2579. type. Before compiled patterns can be saved, they must be converted to a
  2580. "serialized" form, which in the case of PCRE2 is really just a bytecode dump.
  2581. The functions whose names begin with <b>pcre2_serialize_</b> are used for
  2582. converting to and from the serialized form. They are described in the
  2583. <a href="pcre2serialize.html"><b>pcre2serialize</b></a>
  2584. documentation. Note that PCRE2 serialization does not convert compiled patterns
  2585. to an abstract format like Java or .NET serialization.
  2586. <a name="matchdatablock"></a></P>
  2587. <br><a name="SEC26" href="#TOC1">THE MATCH DATA BLOCK</a><br>
  2588. <P>
  2589. <b>pcre2_match_data *pcre2_match_data_create(uint32_t <i>ovecsize</i>,</b>
  2590. <b> pcre2_general_context *<i>gcontext</i>);</b>
  2591. <br>
  2592. <br>
  2593. <b>pcre2_match_data *pcre2_match_data_create_from_pattern(</b>
  2594. <b> const pcre2_code *<i>code</i>, pcre2_general_context *<i>gcontext</i>);</b>
  2595. <br>
  2596. <br>
  2597. <b>void pcre2_match_data_free(pcre2_match_data *<i>match_data</i>);</b>
  2598. </P>
  2599. <P>
  2600. Information about a successful or unsuccessful match is placed in a match
  2601. data block, which is an opaque structure that is accessed by function calls. In
  2602. particular, the match data block contains a vector of offsets into the subject
  2603. string that define the matched parts of the subject. This is known as the
  2604. <i>ovector</i>.
  2605. </P>
  2606. <P>
  2607. Before calling <b>pcre2_match()</b>, <b>pcre2_dfa_match()</b>, or
  2608. <b>pcre2_jit_match()</b> you must create a match data block by calling one of
  2609. the creation functions above. For <b>pcre2_match_data_create()</b>, the first
  2610. argument is the number of pairs of offsets in the <i>ovector</i>.
  2611. </P>
  2612. <P>
  2613. When using <b>pcre2_match()</b>, one pair of offsets is required to identify the
  2614. string that matched the whole pattern, with an additional pair for each
  2615. captured substring. For example, a value of 4 creates enough space to record
  2616. the matched portion of the subject plus three captured substrings.
  2617. </P>
  2618. <P>
  2619. When using <b>pcre2_dfa_match()</b> there may be multiple matched substrings of
  2620. different lengths at the same point in the subject. The ovector should be made
  2621. large enough to hold as many as are expected.
  2622. </P>
  2623. <P>
  2624. A minimum of at least 1 pair is imposed by <b>pcre2_match_data_create()</b>, so
  2625. it is always possible to return the overall matched string in the case of
  2626. <b>pcre2_match()</b> or the longest match in the case of
  2627. <b>pcre2_dfa_match()</b>. The maximum number of pairs is 65535; if the first
  2628. argument of <b>pcre2_match_data_create()</b> is greater than this, 65535 is
  2629. used.
  2630. </P>
  2631. <P>
  2632. The second argument of <b>pcre2_match_data_create()</b> is a pointer to a
  2633. general context, which can specify custom memory management for obtaining the
  2634. memory for the match data block. If you are not using custom memory management,
  2635. pass NULL, which causes <b>malloc()</b> to be used.
  2636. </P>
  2637. <P>
  2638. For <b>pcre2_match_data_create_from_pattern()</b>, the first argument is a
  2639. pointer to a compiled pattern. The ovector is created to be exactly the right
  2640. size to hold all the substrings a pattern might capture when matched using
  2641. <b>pcre2_match()</b>. You should not use this call when matching with
  2642. <b>pcre2_dfa_match()</b>. The second argument is again a pointer to a general
  2643. context, but in this case if NULL is passed, the memory is obtained using the
  2644. same allocator that was used for the compiled pattern (custom or default).
  2645. </P>
  2646. <P>
  2647. A match data block can be used many times, with the same or different compiled
  2648. patterns. You can extract information from a match data block after a match
  2649. operation has finished, using functions that are described in the sections on
  2650. <a href="#matchedstrings">matched strings</a>
  2651. and
  2652. <a href="#matchotherdata">other match data</a>
  2653. below.
  2654. </P>
  2655. <P>
  2656. When a call of <b>pcre2_match()</b> fails, valid data is available in the match
  2657. block only when the error is PCRE2_ERROR_NOMATCH, PCRE2_ERROR_PARTIAL, or one
  2658. of the error codes for an invalid UTF string. Exactly what is available depends
  2659. on the error, and is detailed below.
  2660. </P>
  2661. <P>
  2662. When one of the matching functions is called, pointers to the compiled pattern
  2663. and the subject string are set in the match data block so that they can be
  2664. referenced by the extraction functions after a successful match. After running
  2665. a match, you must not free a compiled pattern or a subject string until after
  2666. all operations on the match data block (for that match) have taken place,
  2667. unless, in the case of the subject string, you have used the
  2668. PCRE2_COPY_MATCHED_SUBJECT option, which is described in the section entitled
  2669. "Option bits for <b>pcre2_match()</b>"
  2670. <a href="#matchoptions>">below.</a>
  2671. </P>
  2672. <P>
  2673. When a match data block itself is no longer needed, it should be freed by
  2674. calling <b>pcre2_match_data_free()</b>. If this function is called with a NULL
  2675. argument, it returns immediately, without doing anything.
  2676. </P>
  2677. <br><a name="SEC27" href="#TOC1">MEMORY USE FOR MATCH DATA BLOCKS</a><br>
  2678. <P>
  2679. <b>PCRE2_SIZE pcre2_get_match_data_size(pcre2_match_data *<i>match_data</i>);</b>
  2680. <br>
  2681. <br>
  2682. <b>PCRE2_SIZE pcre2_get_match_data_heapframes_size(</b>
  2683. <b> pcre2_match_data *<i>match_data</i>);</b>
  2684. </P>
  2685. <P>
  2686. The size of a match data block depends on the size of the ovector that it
  2687. contains. The function <b>pcre2_get_match_data_size()</b> returns the size, in
  2688. bytes, of the block that is its argument.
  2689. </P>
  2690. <P>
  2691. When <b>pcre2_match()</b> runs interpretively (that is, without using JIT), it
  2692. makes use of a vector of data frames for remembering backtracking positions.
  2693. The size of each individual frame depends on the number of capturing
  2694. parentheses in the pattern and can be obtained by calling
  2695. <b>pcre2_pattern_info()</b> with the PCRE2_INFO_FRAMESIZE option (see the
  2696. section entitled "Information about a compiled pattern"
  2697. <a href="#infoaboutpattern>">above).</a>
  2698. </P>
  2699. <P>
  2700. Heap memory is used for the frames vector; if the initial memory block turns
  2701. out to be too small during matching, it is automatically expanded. When
  2702. <b>pcre2_match()</b> returns, the memory is not freed, but remains attached to
  2703. the match data block, for use by any subsequent matches that use the same
  2704. block. It is automatically freed when the match data block itself is freed.
  2705. </P>
  2706. <P>
  2707. You can find the current size of the frames vector that a match data block owns
  2708. by calling <b>pcre2_get_match_data_heapframes_size()</b>. For a newly created
  2709. match data block the size will be zero. Some types of match may require a lot
  2710. of frames and thus a large vector; applications that run in environments where
  2711. memory is constrained can check this and free the match data block if the heap
  2712. frames vector has become too big.
  2713. </P>
  2714. <br><a name="SEC28" href="#TOC1">MATCHING A PATTERN: THE TRADITIONAL FUNCTION</a><br>
  2715. <P>
  2716. <b>int pcre2_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  2717. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  2718. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  2719. <b> pcre2_match_context *<i>mcontext</i>);</b>
  2720. </P>
  2721. <P>
  2722. The function <b>pcre2_match()</b> is called to match a subject string against a
  2723. compiled pattern, which is passed in the <i>code</i> argument. You can call
  2724. <b>pcre2_match()</b> with the same <i>code</i> argument as many times as you
  2725. like, in order to find multiple matches in the subject string or to match
  2726. different subject strings with the same pattern.
  2727. </P>
  2728. <P>
  2729. This function is the main matching facility of the library, and it operates in
  2730. a Perl-like manner. For specialist use there is also an alternative matching
  2731. function, which is described
  2732. <a href="#dfamatch">below</a>
  2733. in the section about the <b>pcre2_dfa_match()</b> function.
  2734. </P>
  2735. <P>
  2736. Here is an example of a simple call to <b>pcre2_match()</b>:
  2737. <pre>
  2738. pcre2_match_data *md = pcre2_match_data_create(4, NULL);
  2739. int rc = pcre2_match(
  2740. re, /* result of pcre2_compile() */
  2741. "some string", /* the subject string */
  2742. 11, /* the length of the subject string */
  2743. 0, /* start at offset 0 in the subject */
  2744. 0, /* default options */
  2745. md, /* the match data block */
  2746. NULL); /* a match context; NULL means use defaults */
  2747. </pre>
  2748. If the subject string is zero-terminated, the length can be given as
  2749. PCRE2_ZERO_TERMINATED. A match context must be provided if certain less common
  2750. matching parameters are to be changed. For details, see the section on
  2751. <a href="#matchcontext">the match context</a>
  2752. above.
  2753. </P>
  2754. <br><b>
  2755. The string to be matched by <b>pcre2_match()</b>
  2756. </b><br>
  2757. <P>
  2758. The subject string is passed to <b>pcre2_match()</b> as a pointer in
  2759. <i>subject</i>, a length in <i>length</i>, and a starting offset in
  2760. <i>startoffset</i>. The length and offset are in code units, not characters.
  2761. That is, they are in bytes for the 8-bit library, 16-bit code units for the
  2762. 16-bit library, and 32-bit code units for the 32-bit library, whether or not
  2763. UTF processing is enabled. As a special case, if <i>subject</i> is NULL and
  2764. <i>length</i> is zero, the subject is assumed to be an empty string. If
  2765. <i>length</i> is non-zero, an error occurs if <i>subject</i> is NULL.
  2766. </P>
  2767. <P>
  2768. If <i>startoffset</i> is greater than the length of the subject,
  2769. <b>pcre2_match()</b> returns PCRE2_ERROR_BADOFFSET. When the starting offset is
  2770. zero, the search for a match starts at the beginning of the subject, and this
  2771. is by far the most common case. In UTF-8 or UTF-16 mode, the starting offset
  2772. must point to the start of a character, or to the end of the subject (in UTF-32
  2773. mode, one code unit equals one character, so all offsets are valid). Like the
  2774. pattern string, the subject may contain binary zeros.
  2775. </P>
  2776. <P>
  2777. A non-zero starting offset is useful when searching for another match in the
  2778. same subject by calling <b>pcre2_match()</b> again after a previous success.
  2779. Setting <i>startoffset</i> differs from passing over a shortened string and
  2780. setting PCRE2_NOTBOL in the case of a pattern that begins with any kind of
  2781. lookbehind. For example, consider the pattern
  2782. <pre>
  2783. \Biss\B
  2784. </pre>
  2785. which finds occurrences of "iss" in the middle of words. (\B matches only if
  2786. the current position in the subject is not a word boundary.) When applied to
  2787. the string "Mississippi" the first call to <b>pcre2_match()</b> finds the first
  2788. occurrence. If <b>pcre2_match()</b> is called again with just the remainder of
  2789. the subject, namely "issippi", it does not match, because \B is always false
  2790. at the start of the subject, which is deemed to be a word boundary. However, if
  2791. <b>pcre2_match()</b> is passed the entire string again, but with
  2792. <i>startoffset</i> set to 4, it finds the second occurrence of "iss" because it
  2793. is able to look behind the starting point to discover that it is preceded by a
  2794. letter.
  2795. </P>
  2796. <P>
  2797. Finding all the matches in a subject is tricky when the pattern can match an
  2798. empty string. It is possible to emulate Perl's /g behaviour by first trying the
  2799. match again at the same offset, with the PCRE2_NOTEMPTY_ATSTART and
  2800. PCRE2_ANCHORED options, and then if that fails, advancing the starting offset
  2801. and trying an ordinary match again. There is some code that demonstrates how to
  2802. do this in the
  2803. <a href="pcre2demo.html"><b>pcre2demo</b></a>
  2804. sample program. In the most general case, you have to check to see if the
  2805. newline convention recognizes CRLF as a newline, and if so, and the current
  2806. character is CR followed by LF, advance the starting offset by two characters
  2807. instead of one.
  2808. </P>
  2809. <P>
  2810. If a non-zero starting offset is passed when the pattern is anchored, a single
  2811. attempt to match at the given offset is made. This can only succeed if the
  2812. pattern does not require the match to be at the start of the subject. In other
  2813. words, the anchoring must be the result of setting the PCRE2_ANCHORED option or
  2814. the use of .* with PCRE2_DOTALL, not by starting the pattern with ^ or \A.
  2815. <a name="matchoptions"></a></P>
  2816. <br><b>
  2817. Option bits for <b>pcre2_match()</b>
  2818. </b><br>
  2819. <P>
  2820. The unused bits of the <i>options</i> argument for <b>pcre2_match()</b> must be
  2821. zero. The only bits that may be set are PCRE2_ANCHORED,
  2822. PCRE2_COPY_MATCHED_SUBJECT, PCRE2_DISABLE_RECURSELOOP_CHECK, PCRE2_ENDANCHORED,
  2823. PCRE2_NOTBOL, PCRE2_NOTEOL, PCRE2_NOTEMPTY, PCRE2_NOTEMPTY_ATSTART,
  2824. PCRE2_NO_JIT, PCRE2_NO_UTF_CHECK, PCRE2_PARTIAL_HARD, and PCRE2_PARTIAL_SOFT.
  2825. Their action is described below.
  2826. </P>
  2827. <P>
  2828. Setting PCRE2_ANCHORED or PCRE2_ENDANCHORED at match time is not supported by
  2829. the just-in-time (JIT) compiler. If it is set, JIT matching is disabled and the
  2830. interpretive code in <b>pcre2_match()</b> is run.
  2831. PCRE2_DISABLE_RECURSELOOP_CHECK is ignored by JIT, but apart from PCRE2_NO_JIT
  2832. (obviously), the remaining options are supported for JIT matching.
  2833. <pre>
  2834. PCRE2_ANCHORED
  2835. </pre>
  2836. The PCRE2_ANCHORED option limits <b>pcre2_match()</b> to matching at the first
  2837. matching position. If a pattern was compiled with PCRE2_ANCHORED, or turned out
  2838. to be anchored by virtue of its contents, it cannot be made unachored at
  2839. matching time. Note that setting the option at match time disables JIT
  2840. matching.
  2841. <pre>
  2842. PCRE2_COPY_MATCHED_SUBJECT
  2843. </pre>
  2844. By default, a pointer to the subject is remembered in the match data block so
  2845. that, after a successful match, it can be referenced by the substring
  2846. extraction functions. This means that the subject's memory must not be freed
  2847. until all such operations are complete. For some applications where the
  2848. lifetime of the subject string is not guaranteed, it may be necessary to make a
  2849. copy of the subject string, but it is wasteful to do this unless the match is
  2850. successful. After a successful match, if PCRE2_COPY_MATCHED_SUBJECT is set, the
  2851. subject is copied and the new pointer is remembered in the match data block
  2852. instead of the original subject pointer. The memory allocator that was used for
  2853. the match block itself is used. The copy is automatically freed when
  2854. <b>pcre2_match_data_free()</b> is called to free the match data block. It is also
  2855. automatically freed if the match data block is re-used for another match
  2856. operation.
  2857. <pre>
  2858. PCRE2_DISABLE_RECURSELOOP_CHECK
  2859. </pre>
  2860. This option is relevant only to <b>pcre2_match()</b> for interpretive matching.
  2861. It is ignored when JIT is used, and is forbidden for <b>pcre2_dfa_match()</b>.
  2862. </P>
  2863. <P>
  2864. The use of recursion in patterns can lead to infinite loops. In the
  2865. interpretive matcher these would be eventually caught by the match or heap
  2866. limits, but this could take a long time and/or use a lot of memory if the
  2867. limits are large. There is therefore a check at the start of each recursion.
  2868. If the same group is still active from a previous call, and the current subject
  2869. pointer is the same as it was at the start of that group, and the furthest
  2870. inspected character of the subject has not changed, an error is generated.
  2871. </P>
  2872. <P>
  2873. There are rare cases of matches that would complete, but nevertheless trigger
  2874. this error. This option disables the check. It is provided mainly for testing
  2875. when comparing JIT and interpretive behaviour.
  2876. <pre>
  2877. PCRE2_ENDANCHORED
  2878. </pre>
  2879. If the PCRE2_ENDANCHORED option is set, any string that <b>pcre2_match()</b>
  2880. matches must be right at the end of the subject string. Note that setting the
  2881. option at match time disables JIT matching.
  2882. <pre>
  2883. PCRE2_NOTBOL
  2884. </pre>
  2885. This option specifies that first character of the subject string is not the
  2886. beginning of a line, so the circumflex metacharacter should not match before
  2887. it. Setting this without having set PCRE2_MULTILINE at compile time causes
  2888. circumflex never to match. This option affects only the behaviour of the
  2889. circumflex metacharacter. It does not affect \A.
  2890. <pre>
  2891. PCRE2_NOTEOL
  2892. </pre>
  2893. This option specifies that the end of the subject string is not the end of a
  2894. line, so the dollar metacharacter should not match it nor (except in multiline
  2895. mode) a newline immediately before it. Setting this without having set
  2896. PCRE2_MULTILINE at compile time causes dollar never to match. This option
  2897. affects only the behaviour of the dollar metacharacter. It does not affect \Z
  2898. or \z.
  2899. <pre>
  2900. PCRE2_NOTEMPTY
  2901. </pre>
  2902. An empty string is not considered to be a valid match if this option is set. If
  2903. there are alternatives in the pattern, they are tried. If all the alternatives
  2904. match the empty string, the entire match fails. For example, if the pattern
  2905. <pre>
  2906. a?b?
  2907. </pre>
  2908. is applied to a string not beginning with "a" or "b", it matches an empty
  2909. string at the start of the subject. With PCRE2_NOTEMPTY set, this match is not
  2910. valid, so <b>pcre2_match()</b> searches further into the string for occurrences
  2911. of "a" or "b".
  2912. <pre>
  2913. PCRE2_NOTEMPTY_ATSTART
  2914. </pre>
  2915. This is like PCRE2_NOTEMPTY, except that it locks out an empty string match
  2916. only at the first matching position, that is, at the start of the subject plus
  2917. the starting offset. An empty string match later in the subject is permitted.
  2918. If the pattern is anchored, such a match can occur only if the pattern contains
  2919. \K.
  2920. <pre>
  2921. PCRE2_NO_JIT
  2922. </pre>
  2923. By default, if a pattern has been successfully processed by
  2924. <b>pcre2_jit_compile()</b>, JIT is automatically used when <b>pcre2_match()</b>
  2925. is called with options that JIT supports. Setting PCRE2_NO_JIT disables the use
  2926. of JIT; it forces matching to be done by the interpreter.
  2927. <pre>
  2928. PCRE2_NO_UTF_CHECK
  2929. </pre>
  2930. When PCRE2_UTF is set at compile time, the validity of the subject as a UTF
  2931. string is checked unless PCRE2_NO_UTF_CHECK is passed to <b>pcre2_match()</b> or
  2932. PCRE2_MATCH_INVALID_UTF was passed to <b>pcre2_compile()</b>. The latter special
  2933. case is discussed in detail in the
  2934. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  2935. documentation.
  2936. </P>
  2937. <P>
  2938. In the default case, if a non-zero starting offset is given, the check is
  2939. applied only to that part of the subject that could be inspected during
  2940. matching, and there is a check that the starting offset points to the first
  2941. code unit of a character or to the end of the subject. If there are no
  2942. lookbehind assertions in the pattern, the check starts at the starting offset.
  2943. Otherwise, it starts at the length of the longest lookbehind before the
  2944. starting offset, or at the start of the subject if there are not that many
  2945. characters before the starting offset. Note that the sequences \b and \B are
  2946. one-character lookbehinds.
  2947. </P>
  2948. <P>
  2949. The check is carried out before any other processing takes place, and a
  2950. negative error code is returned if the check fails. There are several UTF error
  2951. codes for each code unit width, corresponding to different problems with the
  2952. code unit sequence. There are discussions about the validity of
  2953. <a href="pcre2unicode.html#utf8strings">UTF-8 strings,</a>
  2954. <a href="pcre2unicode.html#utf16strings">UTF-16 strings,</a>
  2955. and
  2956. <a href="pcre2unicode.html#utf32strings">UTF-32 strings</a>
  2957. in the
  2958. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  2959. documentation.
  2960. </P>
  2961. <P>
  2962. If you know that your subject is valid, and you want to skip this check for
  2963. performance reasons, you can set the PCRE2_NO_UTF_CHECK option when calling
  2964. <b>pcre2_match()</b>. You might want to do this for the second and subsequent
  2965. calls to <b>pcre2_match()</b> if you are making repeated calls to find multiple
  2966. matches in the same subject string.
  2967. </P>
  2968. <P>
  2969. <b>Warning:</b> Unless PCRE2_MATCH_INVALID_UTF was set at compile time, when
  2970. PCRE2_NO_UTF_CHECK is set at match time the effect of passing an invalid
  2971. string as a subject, or an invalid value of <i>startoffset</i>, is undefined.
  2972. Your program may crash or loop indefinitely or give wrong results.
  2973. <pre>
  2974. PCRE2_PARTIAL_HARD
  2975. PCRE2_PARTIAL_SOFT
  2976. </pre>
  2977. These options turn on the partial matching feature. A partial match occurs if
  2978. the end of the subject string is reached successfully, but there are not enough
  2979. subject characters to complete the match. In addition, either at least one
  2980. character must have been inspected or the pattern must contain a lookbehind, or
  2981. the pattern must be one that could match an empty string.
  2982. </P>
  2983. <P>
  2984. If this situation arises when PCRE2_PARTIAL_SOFT (but not PCRE2_PARTIAL_HARD)
  2985. is set, matching continues by testing any remaining alternatives. Only if no
  2986. complete match can be found is PCRE2_ERROR_PARTIAL returned instead of
  2987. PCRE2_ERROR_NOMATCH. In other words, PCRE2_PARTIAL_SOFT specifies that the
  2988. caller is prepared to handle a partial match, but only if no complete match can
  2989. be found.
  2990. </P>
  2991. <P>
  2992. If PCRE2_PARTIAL_HARD is set, it overrides PCRE2_PARTIAL_SOFT. In this case, if
  2993. a partial match is found, <b>pcre2_match()</b> immediately returns
  2994. PCRE2_ERROR_PARTIAL, without considering any other alternatives. In other
  2995. words, when PCRE2_PARTIAL_HARD is set, a partial match is considered to be more
  2996. important that an alternative complete match.
  2997. </P>
  2998. <P>
  2999. There is a more detailed discussion of partial and multi-segment matching, with
  3000. examples, in the
  3001. <a href="pcre2partial.html"><b>pcre2partial</b></a>
  3002. documentation.
  3003. </P>
  3004. <br><a name="SEC29" href="#TOC1">NEWLINE HANDLING WHEN MATCHING</a><br>
  3005. <P>
  3006. When PCRE2 is built, a default newline convention is set; this is usually the
  3007. standard convention for the operating system. The default can be overridden in
  3008. a
  3009. <a href="#compilecontext">compile context</a>
  3010. by calling <b>pcre2_set_newline()</b>. It can also be overridden by starting a
  3011. pattern string with, for example, (*CRLF), as described in the
  3012. <a href="pcre2pattern.html#newlines">section on newline conventions</a>
  3013. in the
  3014. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  3015. page. During matching, the newline choice affects the behaviour of the dot,
  3016. circumflex, and dollar metacharacters. It may also alter the way the match
  3017. starting position is advanced after a match failure for an unanchored pattern.
  3018. </P>
  3019. <P>
  3020. When PCRE2_NEWLINE_CRLF, PCRE2_NEWLINE_ANYCRLF, or PCRE2_NEWLINE_ANY is set as
  3021. the newline convention, and a match attempt for an unanchored pattern fails
  3022. when the current starting position is at a CRLF sequence, and the pattern
  3023. contains no explicit matches for CR or LF characters, the match position is
  3024. advanced by two characters instead of one, in other words, to after the CRLF.
  3025. </P>
  3026. <P>
  3027. The above rule is a compromise that makes the most common cases work as
  3028. expected. For example, if the pattern is .+A (and the PCRE2_DOTALL option is
  3029. not set), it does not match the string "\r\nA" because, after failing at the
  3030. start, it skips both the CR and the LF before retrying. However, the pattern
  3031. [\r\n]A does match that string, because it contains an explicit CR or LF
  3032. reference, and so advances only by one character after the first failure.
  3033. </P>
  3034. <P>
  3035. An explicit match for CR of LF is either a literal appearance of one of those
  3036. characters in the pattern, or one of the \r or \n or equivalent octal or
  3037. hexadecimal escape sequences. Implicit matches such as [^X] do not count, nor
  3038. does \s, even though it includes CR and LF in the characters that it matches.
  3039. </P>
  3040. <P>
  3041. Notwithstanding the above, anomalous effects may still occur when CRLF is a
  3042. valid newline sequence and explicit \r or \n escapes appear in the pattern.
  3043. <a name="matchedstrings"></a></P>
  3044. <br><a name="SEC30" href="#TOC1">HOW PCRE2_MATCH() RETURNS A STRING AND CAPTURED SUBSTRINGS</a><br>
  3045. <P>
  3046. <b>uint32_t pcre2_get_ovector_count(pcre2_match_data *<i>match_data</i>);</b>
  3047. <br>
  3048. <br>
  3049. <b>PCRE2_SIZE *pcre2_get_ovector_pointer(pcre2_match_data *<i>match_data</i>);</b>
  3050. </P>
  3051. <P>
  3052. In general, a pattern matches a certain portion of the subject, and in
  3053. addition, further substrings from the subject may be picked out by
  3054. parenthesized parts of the pattern. Following the usage in Jeffrey Friedl's
  3055. book, this is called "capturing" in what follows, and the phrase "capture
  3056. group" (Perl terminology) is used for a fragment of a pattern that picks out a
  3057. substring. PCRE2 supports several other kinds of parenthesized group that do
  3058. not cause substrings to be captured. The <b>pcre2_pattern_info()</b> function
  3059. can be used to find out how many capture groups there are in a compiled
  3060. pattern.
  3061. </P>
  3062. <P>
  3063. You can use auxiliary functions for accessing captured substrings
  3064. <a href="#extractbynumber">by number</a>
  3065. or
  3066. <a href="#extractbyname">by name,</a>
  3067. as described in sections below.
  3068. </P>
  3069. <P>
  3070. Alternatively, you can make direct use of the vector of PCRE2_SIZE values,
  3071. called the <b>ovector</b>, which contains the offsets of captured strings. It is
  3072. part of the
  3073. <a href="#matchdatablock">match data block.</a>
  3074. The function <b>pcre2_get_ovector_pointer()</b> returns the address of the
  3075. ovector, and <b>pcre2_get_ovector_count()</b> returns the number of pairs of
  3076. values it contains.
  3077. </P>
  3078. <P>
  3079. Within the ovector, the first in each pair of values is set to the offset of
  3080. the first code unit of a substring, and the second is set to the offset of the
  3081. first code unit after the end of a substring. These values are always code unit
  3082. offsets, not character offsets. That is, they are byte offsets in the 8-bit
  3083. library, 16-bit offsets in the 16-bit library, and 32-bit offsets in the 32-bit
  3084. library.
  3085. </P>
  3086. <P>
  3087. After a partial match (error return PCRE2_ERROR_PARTIAL), only the first pair
  3088. of offsets (that is, <i>ovector[0]</i> and <i>ovector[1]</i>) are set. They
  3089. identify the part of the subject that was partially matched. See the
  3090. <a href="pcre2partial.html"><b>pcre2partial</b></a>
  3091. documentation for details of partial matching.
  3092. </P>
  3093. <P>
  3094. After a fully successful match, the first pair of offsets identifies the
  3095. portion of the subject string that was matched by the entire pattern. The next
  3096. pair is used for the first captured substring, and so on. The value returned by
  3097. <b>pcre2_match()</b> is one more than the highest numbered pair that has been
  3098. set. For example, if two substrings have been captured, the returned value is
  3099. 3. If there are no captured substrings, the return value from a successful
  3100. match is 1, indicating that just the first pair of offsets has been set.
  3101. </P>
  3102. <P>
  3103. If a pattern uses the \K escape sequence within a positive assertion, the
  3104. reported start of a successful match can be greater than the end of the match.
  3105. For example, if the pattern (?=ab\K) is matched against "ab", the start and
  3106. end offset values for the match are 2 and 0.
  3107. </P>
  3108. <P>
  3109. If a capture group is matched repeatedly within a single match operation, it is
  3110. the last portion of the subject that it matched that is returned.
  3111. </P>
  3112. <P>
  3113. If the ovector is too small to hold all the captured substring offsets, as much
  3114. as possible is filled in, and the function returns a value of zero. If captured
  3115. substrings are not of interest, <b>pcre2_match()</b> may be called with a match
  3116. data block whose ovector is of minimum length (that is, one pair).
  3117. </P>
  3118. <P>
  3119. It is possible for capture group number <i>n+1</i> to match some part of the
  3120. subject when group <i>n</i> has not been used at all. For example, if the string
  3121. "abc" is matched against the pattern (a|(z))(bc) the return from the function
  3122. is 4, and groups 1 and 3 are matched, but 2 is not. When this happens, both
  3123. values in the offset pairs corresponding to unused groups are set to
  3124. PCRE2_UNSET.
  3125. </P>
  3126. <P>
  3127. Offset values that correspond to unused groups at the end of the expression are
  3128. also set to PCRE2_UNSET. For example, if the string "abc" is matched against
  3129. the pattern (abc)(x(yz)?)? groups 2 and 3 are not matched. The return from the
  3130. function is 2, because the highest used capture group number is 1. The offsets
  3131. for the second and third capture groups (assuming the vector is large enough,
  3132. of course) are set to PCRE2_UNSET.
  3133. </P>
  3134. <P>
  3135. Elements in the ovector that do not correspond to capturing parentheses in the
  3136. pattern are never changed. That is, if a pattern contains <i>n</i> capturing
  3137. parentheses, no more than <i>ovector[0]</i> to <i>ovector[2n+1]</i> are set by
  3138. <b>pcre2_match()</b>. The other elements retain whatever values they previously
  3139. had. After a failed match attempt, the contents of the ovector are unchanged.
  3140. <a name="matchotherdata"></a></P>
  3141. <br><a name="SEC31" href="#TOC1">OTHER INFORMATION ABOUT A MATCH</a><br>
  3142. <P>
  3143. <b>PCRE2_SPTR pcre2_get_mark(pcre2_match_data *<i>match_data</i>);</b>
  3144. <br>
  3145. <br>
  3146. <b>PCRE2_SIZE pcre2_get_startchar(pcre2_match_data *<i>match_data</i>);</b>
  3147. </P>
  3148. <P>
  3149. As well as the offsets in the ovector, other information about a match is
  3150. retained in the match data block and can be retrieved by the above functions in
  3151. appropriate circumstances. If they are called at other times, the result is
  3152. undefined.
  3153. </P>
  3154. <P>
  3155. After a successful match, a partial match (PCRE2_ERROR_PARTIAL), or a failure
  3156. to match (PCRE2_ERROR_NOMATCH), a mark name may be available. The function
  3157. <b>pcre2_get_mark()</b> can be called to access this name, which can be
  3158. specified in the pattern by any of the backtracking control verbs, not just
  3159. (*MARK). The same function applies to all the verbs. It returns a pointer to
  3160. the zero-terminated name, which is within the compiled pattern. If no name is
  3161. available, NULL is returned. The length of the name (excluding the terminating
  3162. zero) is stored in the code unit that precedes the name. You should use this
  3163. length instead of relying on the terminating zero if the name might contain a
  3164. binary zero.
  3165. </P>
  3166. <P>
  3167. After a successful match, the name that is returned is the last mark name
  3168. encountered on the matching path through the pattern. Instances of backtracking
  3169. verbs without names do not count. Thus, for example, if the matching path
  3170. contains (*MARK:A)(*PRUNE), the name "A" is returned. After a "no match" or a
  3171. partial match, the last encountered name is returned. For example, consider
  3172. this pattern:
  3173. <pre>
  3174. ^(*MARK:A)((*MARK:B)a|b)c
  3175. </pre>
  3176. When it matches "bc", the returned name is A. The B mark is "seen" in the first
  3177. branch of the group, but it is not on the matching path. On the other hand,
  3178. when this pattern fails to match "bx", the returned name is B.
  3179. </P>
  3180. <P>
  3181. <b>Warning:</b> By default, certain start-of-match optimizations are used to
  3182. give a fast "no match" result in some situations. For example, if the anchoring
  3183. is removed from the pattern above, there is an initial check for the presence
  3184. of "c" in the subject before running the matching engine. This check fails for
  3185. "bx", causing a match failure without seeing any marks. You can disable the
  3186. start-of-match optimizations by setting the PCRE2_NO_START_OPTIMIZE option for
  3187. <b>pcre2_compile()</b> or by starting the pattern with (*NO_START_OPT).
  3188. </P>
  3189. <P>
  3190. After a successful match, a partial match, or one of the invalid UTF errors
  3191. (for example, PCRE2_ERROR_UTF8_ERR5), <b>pcre2_get_startchar()</b> can be
  3192. called. After a successful or partial match it returns the code unit offset of
  3193. the character at which the match started. For a non-partial match, this can be
  3194. different to the value of <i>ovector[0]</i> if the pattern contains the \K
  3195. escape sequence. After a partial match, however, this value is always the same
  3196. as <i>ovector[0]</i> because \K does not affect the result of a partial match.
  3197. </P>
  3198. <P>
  3199. After a UTF check failure, <b>pcre2_get_startchar()</b> can be used to obtain
  3200. the code unit offset of the invalid UTF character. Details are given in the
  3201. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  3202. page.
  3203. <a name="errorlist"></a></P>
  3204. <br><a name="SEC32" href="#TOC1">ERROR RETURNS FROM <b>pcre2_match()</b></a><br>
  3205. <P>
  3206. If <b>pcre2_match()</b> fails, it returns a negative number. This can be
  3207. converted to a text string by calling the <b>pcre2_get_error_message()</b>
  3208. function (see "Obtaining a textual error message"
  3209. <a href="#geterrormessage">below).</a>
  3210. Negative error codes are also returned by other functions, and are documented
  3211. with them. The codes are given names in the header file. If UTF checking is in
  3212. force and an invalid UTF subject string is detected, one of a number of
  3213. UTF-specific negative error codes is returned. Details are given in the
  3214. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  3215. page. The following are the other errors that may be returned by
  3216. <b>pcre2_match()</b>:
  3217. <pre>
  3218. PCRE2_ERROR_NOMATCH
  3219. </pre>
  3220. The subject string did not match the pattern.
  3221. <pre>
  3222. PCRE2_ERROR_PARTIAL
  3223. </pre>
  3224. The subject string did not match, but it did match partially. See the
  3225. <a href="pcre2partial.html"><b>pcre2partial</b></a>
  3226. documentation for details of partial matching.
  3227. <pre>
  3228. PCRE2_ERROR_BADMAGIC
  3229. </pre>
  3230. PCRE2 stores a 4-byte "magic number" at the start of the compiled code, to
  3231. catch the case when it is passed a junk pointer. This is the error that is
  3232. returned when the magic number is not present.
  3233. <pre>
  3234. PCRE2_ERROR_BADMODE
  3235. </pre>
  3236. This error is given when a compiled pattern is passed to a function in a
  3237. library of a different code unit width, for example, a pattern compiled by
  3238. the 8-bit library is passed to a 16-bit or 32-bit library function.
  3239. <pre>
  3240. PCRE2_ERROR_BADOFFSET
  3241. </pre>
  3242. The value of <i>startoffset</i> was greater than the length of the subject.
  3243. <pre>
  3244. PCRE2_ERROR_BADOPTION
  3245. </pre>
  3246. An unrecognized bit was set in the <i>options</i> argument.
  3247. <pre>
  3248. PCRE2_ERROR_BADUTFOFFSET
  3249. </pre>
  3250. The UTF code unit sequence that was passed as a subject was checked and found
  3251. to be valid (the PCRE2_NO_UTF_CHECK option was not set), but the value of
  3252. <i>startoffset</i> did not point to the beginning of a UTF character or the end
  3253. of the subject.
  3254. <pre>
  3255. PCRE2_ERROR_CALLOUT
  3256. </pre>
  3257. This error is never generated by <b>pcre2_match()</b> itself. It is provided for
  3258. use by callout functions that want to cause <b>pcre2_match()</b> or
  3259. <b>pcre2_callout_enumerate()</b> to return a distinctive error code. See the
  3260. <a href="pcre2callout.html"><b>pcre2callout</b></a>
  3261. documentation for details.
  3262. <pre>
  3263. PCRE2_ERROR_DEPTHLIMIT
  3264. </pre>
  3265. The nested backtracking depth limit was reached.
  3266. <pre>
  3267. PCRE2_ERROR_HEAPLIMIT
  3268. </pre>
  3269. The heap limit was reached.
  3270. <pre>
  3271. PCRE2_ERROR_INTERNAL
  3272. </pre>
  3273. An unexpected internal error has occurred. This error could be caused by a bug
  3274. in PCRE2 or by overwriting of the compiled pattern.
  3275. <pre>
  3276. PCRE2_ERROR_JIT_STACKLIMIT
  3277. </pre>
  3278. This error is returned when a pattern that was successfully studied using JIT
  3279. is being matched, but the memory available for the just-in-time processing
  3280. stack is not large enough. See the
  3281. <a href="pcre2jit.html"><b>pcre2jit</b></a>
  3282. documentation for more details.
  3283. <pre>
  3284. PCRE2_ERROR_MATCHLIMIT
  3285. </pre>
  3286. The backtracking match limit was reached.
  3287. <pre>
  3288. PCRE2_ERROR_NOMEMORY
  3289. </pre>
  3290. Heap memory is used to remember backtracking points. This error is given when
  3291. the memory allocation function (default or custom) fails. Note that a different
  3292. error, PCRE2_ERROR_HEAPLIMIT, is given if the amount of memory needed exceeds
  3293. the heap limit. PCRE2_ERROR_NOMEMORY is also returned if
  3294. PCRE2_COPY_MATCHED_SUBJECT is set and memory allocation fails.
  3295. <pre>
  3296. PCRE2_ERROR_NULL
  3297. </pre>
  3298. Either the <i>code</i>, <i>subject</i>, or <i>match_data</i> argument was passed
  3299. as NULL.
  3300. <pre>
  3301. PCRE2_ERROR_RECURSELOOP
  3302. </pre>
  3303. This error is returned when <b>pcre2_match()</b> detects a recursion loop within
  3304. the pattern. Specifically, it means that either the whole pattern or a
  3305. capture group has been called recursively for the second time at the same
  3306. position in the subject string. Some simple patterns that might do this are
  3307. detected and faulted at compile time, but more complicated cases, in particular
  3308. mutual recursions between two different groups, cannot be detected until
  3309. matching is attempted.
  3310. <a name="geterrormessage"></a></P>
  3311. <br><a name="SEC33" href="#TOC1">OBTAINING A TEXTUAL ERROR MESSAGE</a><br>
  3312. <P>
  3313. <b>int pcre2_get_error_message(int <i>errorcode</i>, PCRE2_UCHAR *<i>buffer</i>,</b>
  3314. <b> PCRE2_SIZE <i>bufflen</i>);</b>
  3315. </P>
  3316. <P>
  3317. A text message for an error code from any PCRE2 function (compile, match, or
  3318. auxiliary) can be obtained by calling <b>pcre2_get_error_message()</b>. The code
  3319. is passed as the first argument, with the remaining two arguments specifying a
  3320. code unit buffer and its length in code units, into which the text message is
  3321. placed. The message is returned in code units of the appropriate width for the
  3322. library that is being used.
  3323. </P>
  3324. <P>
  3325. The returned message is terminated with a trailing zero, and the function
  3326. returns the number of code units used, excluding the trailing zero. If the
  3327. error number is unknown, the negative error code PCRE2_ERROR_BADDATA is
  3328. returned. If the buffer is too small, the message is truncated (but still with
  3329. a trailing zero), and the negative error code PCRE2_ERROR_NOMEMORY is returned.
  3330. None of the messages are very long; a buffer size of 120 code units is ample.
  3331. <a name="extractbynumber"></a></P>
  3332. <br><a name="SEC34" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NUMBER</a><br>
  3333. <P>
  3334. <b>int pcre2_substring_length_bynumber(pcre2_match_data *<i>match_data</i>,</b>
  3335. <b> uint32_t <i>number</i>, PCRE2_SIZE *<i>length</i>);</b>
  3336. <br>
  3337. <br>
  3338. <b>int pcre2_substring_copy_bynumber(pcre2_match_data *<i>match_data</i>,</b>
  3339. <b> uint32_t <i>number</i>, PCRE2_UCHAR *<i>buffer</i>,</b>
  3340. <b> PCRE2_SIZE *<i>bufflen</i>);</b>
  3341. <br>
  3342. <br>
  3343. <b>int pcre2_substring_get_bynumber(pcre2_match_data *<i>match_data</i>,</b>
  3344. <b> uint32_t <i>number</i>, PCRE2_UCHAR **<i>bufferptr</i>,</b>
  3345. <b> PCRE2_SIZE *<i>bufflen</i>);</b>
  3346. <br>
  3347. <br>
  3348. <b>void pcre2_substring_free(PCRE2_UCHAR *<i>buffer</i>);</b>
  3349. </P>
  3350. <P>
  3351. Captured substrings can be accessed directly by using the ovector as described
  3352. <a href="#matchedstrings">above.</a>
  3353. For convenience, auxiliary functions are provided for extracting captured
  3354. substrings as new, separate, zero-terminated strings. A substring that contains
  3355. a binary zero is correctly extracted and has a further zero added on the end,
  3356. but the result is not, of course, a C string.
  3357. </P>
  3358. <P>
  3359. The functions in this section identify substrings by number. The number zero
  3360. refers to the entire matched substring, with higher numbers referring to
  3361. substrings captured by parenthesized groups. After a partial match, only
  3362. substring zero is available. An attempt to extract any other substring gives
  3363. the error PCRE2_ERROR_PARTIAL. The next section describes similar functions for
  3364. extracting captured substrings by name.
  3365. </P>
  3366. <P>
  3367. If a pattern uses the \K escape sequence within a positive assertion, the
  3368. reported start of a successful match can be greater than the end of the match.
  3369. For example, if the pattern (?=ab\K) is matched against "ab", the start and
  3370. end offset values for the match are 2 and 0. In this situation, calling these
  3371. functions with a zero substring number extracts a zero-length empty string.
  3372. </P>
  3373. <P>
  3374. You can find the length in code units of a captured substring without
  3375. extracting it by calling <b>pcre2_substring_length_bynumber()</b>. The first
  3376. argument is a pointer to the match data block, the second is the group number,
  3377. and the third is a pointer to a variable into which the length is placed. If
  3378. you just want to know whether or not the substring has been captured, you can
  3379. pass the third argument as NULL.
  3380. </P>
  3381. <P>
  3382. The <b>pcre2_substring_copy_bynumber()</b> function copies a captured substring
  3383. into a supplied buffer, whereas <b>pcre2_substring_get_bynumber()</b> copies it
  3384. into new memory, obtained using the same memory allocation function that was
  3385. used for the match data block. The first two arguments of these functions are a
  3386. pointer to the match data block and a capture group number.
  3387. </P>
  3388. <P>
  3389. The final arguments of <b>pcre2_substring_copy_bynumber()</b> are a pointer to
  3390. the buffer and a pointer to a variable that contains its length in code units.
  3391. This is updated to contain the actual number of code units used for the
  3392. extracted substring, excluding the terminating zero.
  3393. </P>
  3394. <P>
  3395. For <b>pcre2_substring_get_bynumber()</b> the third and fourth arguments point
  3396. to variables that are updated with a pointer to the new memory and the number
  3397. of code units that comprise the substring, again excluding the terminating
  3398. zero. When the substring is no longer needed, the memory should be freed by
  3399. calling <b>pcre2_substring_free()</b>.
  3400. </P>
  3401. <P>
  3402. The return value from all these functions is zero for success, or a negative
  3403. error code. If the pattern match failed, the match failure code is returned.
  3404. If a substring number greater than zero is used after a partial match,
  3405. PCRE2_ERROR_PARTIAL is returned. Other possible error codes are:
  3406. <pre>
  3407. PCRE2_ERROR_NOMEMORY
  3408. </pre>
  3409. The buffer was too small for <b>pcre2_substring_copy_bynumber()</b>, or the
  3410. attempt to get memory failed for <b>pcre2_substring_get_bynumber()</b>.
  3411. <pre>
  3412. PCRE2_ERROR_NOSUBSTRING
  3413. </pre>
  3414. There is no substring with that number in the pattern, that is, the number is
  3415. greater than the number of capturing parentheses.
  3416. <pre>
  3417. PCRE2_ERROR_UNAVAILABLE
  3418. </pre>
  3419. The substring number, though not greater than the number of captures in the
  3420. pattern, is greater than the number of slots in the ovector, so the substring
  3421. could not be captured.
  3422. <pre>
  3423. PCRE2_ERROR_UNSET
  3424. </pre>
  3425. The substring did not participate in the match. For example, if the pattern is
  3426. (abc)|(def) and the subject is "def", and the ovector contains at least two
  3427. capturing slots, substring number 1 is unset.
  3428. </P>
  3429. <br><a name="SEC35" href="#TOC1">EXTRACTING A LIST OF ALL CAPTURED SUBSTRINGS</a><br>
  3430. <P>
  3431. <b>int pcre2_substring_list_get(pcre2_match_data *<i>match_data</i>,</b>
  3432. <b>" PCRE2_UCHAR ***<i>listptr</i>, PCRE2_SIZE **<i>lengthsptr</i>);</b>
  3433. <br>
  3434. <br>
  3435. <b>void pcre2_substring_list_free(PCRE2_UCHAR **<i>list</i>);</b>
  3436. </P>
  3437. <P>
  3438. The <b>pcre2_substring_list_get()</b> function extracts all available substrings
  3439. and builds a list of pointers to them. It also (optionally) builds a second
  3440. list that contains their lengths (in code units), excluding a terminating zero
  3441. that is added to each of them. All this is done in a single block of memory
  3442. that is obtained using the same memory allocation function that was used to get
  3443. the match data block.
  3444. </P>
  3445. <P>
  3446. This function must be called only after a successful match. If called after a
  3447. partial match, the error code PCRE2_ERROR_PARTIAL is returned.
  3448. </P>
  3449. <P>
  3450. The address of the memory block is returned via <i>listptr</i>, which is also
  3451. the start of the list of string pointers. The end of the list is marked by a
  3452. NULL pointer. The address of the list of lengths is returned via
  3453. <i>lengthsptr</i>. If your strings do not contain binary zeros and you do not
  3454. therefore need the lengths, you may supply NULL as the <b>lengthsptr</b>
  3455. argument to disable the creation of a list of lengths. The yield of the
  3456. function is zero if all went well, or PCRE2_ERROR_NOMEMORY if the memory block
  3457. could not be obtained. When the list is no longer needed, it should be freed by
  3458. calling <b>pcre2_substring_list_free()</b>.
  3459. </P>
  3460. <P>
  3461. If this function encounters a substring that is unset, which can happen when
  3462. capture group number <i>n+1</i> matches some part of the subject, but group
  3463. <i>n</i> has not been used at all, it returns an empty string. This can be
  3464. distinguished from a genuine zero-length substring by inspecting the
  3465. appropriate offset in the ovector, which contain PCRE2_UNSET for unset
  3466. substrings, or by calling <b>pcre2_substring_length_bynumber()</b>.
  3467. <a name="extractbyname"></a></P>
  3468. <br><a name="SEC36" href="#TOC1">EXTRACTING CAPTURED SUBSTRINGS BY NAME</a><br>
  3469. <P>
  3470. <b>int pcre2_substring_number_from_name(const pcre2_code *<i>code</i>,</b>
  3471. <b> PCRE2_SPTR <i>name</i>);</b>
  3472. <br>
  3473. <br>
  3474. <b>int pcre2_substring_length_byname(pcre2_match_data *<i>match_data</i>,</b>
  3475. <b> PCRE2_SPTR <i>name</i>, PCRE2_SIZE *<i>length</i>);</b>
  3476. <br>
  3477. <br>
  3478. <b>int pcre2_substring_copy_byname(pcre2_match_data *<i>match_data</i>,</b>
  3479. <b> PCRE2_SPTR <i>name</i>, PCRE2_UCHAR *<i>buffer</i>, PCRE2_SIZE *<i>bufflen</i>);</b>
  3480. <br>
  3481. <br>
  3482. <b>int pcre2_substring_get_byname(pcre2_match_data *<i>match_data</i>,</b>
  3483. <b> PCRE2_SPTR <i>name</i>, PCRE2_UCHAR **<i>bufferptr</i>, PCRE2_SIZE *<i>bufflen</i>);</b>
  3484. <br>
  3485. <br>
  3486. <b>void pcre2_substring_free(PCRE2_UCHAR *<i>buffer</i>);</b>
  3487. </P>
  3488. <P>
  3489. To extract a substring by name, you first have to find associated number.
  3490. For example, for this pattern:
  3491. <pre>
  3492. (a+)b(?&#60;xxx&#62;\d+)...
  3493. </pre>
  3494. the number of the capture group called "xxx" is 2. If the name is known to be
  3495. unique (PCRE2_DUPNAMES was not set), you can find the number from the name by
  3496. calling <b>pcre2_substring_number_from_name()</b>. The first argument is the
  3497. compiled pattern, and the second is the name. The yield of the function is the
  3498. group number, PCRE2_ERROR_NOSUBSTRING if there is no group with that name, or
  3499. PCRE2_ERROR_NOUNIQUESUBSTRING if there is more than one group with that name.
  3500. Given the number, you can extract the substring directly from the ovector, or
  3501. use one of the "bynumber" functions described above.
  3502. </P>
  3503. <P>
  3504. For convenience, there are also "byname" functions that correspond to the
  3505. "bynumber" functions, the only difference being that the second argument is a
  3506. name instead of a number. If PCRE2_DUPNAMES is set and there are duplicate
  3507. names, these functions scan all the groups with the given name, and return the
  3508. captured substring from the first named group that is set.
  3509. </P>
  3510. <P>
  3511. If there are no groups with the given name, PCRE2_ERROR_NOSUBSTRING is
  3512. returned. If all groups with the name have numbers that are greater than the
  3513. number of slots in the ovector, PCRE2_ERROR_UNAVAILABLE is returned. If there
  3514. is at least one group with a slot in the ovector, but no group is found to be
  3515. set, PCRE2_ERROR_UNSET is returned.
  3516. </P>
  3517. <P>
  3518. <b>Warning:</b> If the pattern uses the (?| feature to set up multiple
  3519. capture groups with the same number, as described in the
  3520. <a href="pcre2pattern.html#dupgroupnumber">section on duplicate group numbers</a>
  3521. in the
  3522. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  3523. page, you cannot use names to distinguish the different capture groups, because
  3524. names are not included in the compiled code. The matching process uses only
  3525. numbers. For this reason, the use of different names for groups with the
  3526. same number causes an error at compile time.
  3527. <a name="substitutions"></a></P>
  3528. <br><a name="SEC37" href="#TOC1">CREATING A NEW STRING WITH SUBSTITUTIONS</a><br>
  3529. <P>
  3530. <b>int pcre2_substitute(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  3531. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  3532. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  3533. <b> pcre2_match_context *<i>mcontext</i>, PCRE2_SPTR <i>replacement</i>,</b>
  3534. <b> PCRE2_SIZE <i>rlength</i>, PCRE2_UCHAR *<i>outputbuffer</i>,</b>
  3535. <b> PCRE2_SIZE *<i>outlengthptr</i>);</b>
  3536. </P>
  3537. <P>
  3538. This function optionally calls <b>pcre2_match()</b> and then makes a copy of the
  3539. subject string in <i>outputbuffer</i>, replacing parts that were matched with
  3540. the <i>replacement</i> string, whose length is supplied in <b>rlength</b>, which
  3541. can be given as PCRE2_ZERO_TERMINATED for a zero-terminated string. As a
  3542. special case, if <i>replacement</i> is NULL and <i>rlength</i> is zero, the
  3543. replacement is assumed to be an empty string. If <i>rlength</i> is non-zero, an
  3544. error occurs if <i>replacement</i> is NULL.
  3545. </P>
  3546. <P>
  3547. There is an option (see PCRE2_SUBSTITUTE_REPLACEMENT_ONLY below) to return just
  3548. the replacement string(s). The default action is to perform just one
  3549. replacement if the pattern matches, but there is an option that requests
  3550. multiple replacements (see PCRE2_SUBSTITUTE_GLOBAL below).
  3551. </P>
  3552. <P>
  3553. If successful, <b>pcre2_substitute()</b> returns the number of substitutions
  3554. that were carried out. This may be zero if no match was found, and is never
  3555. greater than one unless PCRE2_SUBSTITUTE_GLOBAL is set. A negative value is
  3556. returned if an error is detected.
  3557. </P>
  3558. <P>
  3559. Matches in which a \K item in a lookahead in the pattern causes the match to
  3560. end before it starts are not supported, and give rise to an error return. For
  3561. global replacements, matches in which \K in a lookbehind causes the match to
  3562. start earlier than the point that was reached in the previous iteration are
  3563. also not supported.
  3564. </P>
  3565. <P>
  3566. The first seven arguments of <b>pcre2_substitute()</b> are the same as for
  3567. <b>pcre2_match()</b>, except that the partial matching options are not
  3568. permitted, and <i>match_data</i> may be passed as NULL, in which case a match
  3569. data block is obtained and freed within this function, using memory management
  3570. functions from the match context, if provided, or else those that were used to
  3571. allocate memory for the compiled code.
  3572. </P>
  3573. <P>
  3574. If <i>match_data</i> is not NULL and PCRE2_SUBSTITUTE_MATCHED is not set, the
  3575. provided block is used for all calls to <b>pcre2_match()</b>, and its contents
  3576. afterwards are the result of the final call. For global changes, this will
  3577. always be a no-match error. The contents of the ovector within the match data
  3578. block may or may not have been changed.
  3579. </P>
  3580. <P>
  3581. As well as the usual options for <b>pcre2_match()</b>, a number of additional
  3582. options can be set in the <i>options</i> argument of <b>pcre2_substitute()</b>.
  3583. One such option is PCRE2_SUBSTITUTE_MATCHED. When this is set, an external
  3584. <i>match_data</i> block must be provided, and it must have already been used for
  3585. an external call to <b>pcre2_match()</b> with the same pattern and subject
  3586. arguments. The data in the <i>match_data</i> block (return code, offset vector)
  3587. is then used for the first substitution instead of calling <b>pcre2_match()</b>
  3588. from within <b>pcre2_substitute()</b>. This allows an application to check for a
  3589. match before choosing to substitute, without having to repeat the match.
  3590. </P>
  3591. <P>
  3592. The contents of the externally supplied match data block are not changed when
  3593. PCRE2_SUBSTITUTE_MATCHED is set. If PCRE2_SUBSTITUTE_GLOBAL is also set,
  3594. <b>pcre2_match()</b> is called after the first substitution to check for further
  3595. matches, but this is done using an internally obtained match data block, thus
  3596. always leaving the external block unchanged.
  3597. </P>
  3598. <P>
  3599. The <i>code</i> argument is not used for matching before the first substitution
  3600. when PCRE2_SUBSTITUTE_MATCHED is set, but it must be provided, even when
  3601. PCRE2_SUBSTITUTE_GLOBAL is not set, because it contains information such as the
  3602. UTF setting and the number of capturing parentheses in the pattern.
  3603. </P>
  3604. <P>
  3605. The default action of <b>pcre2_substitute()</b> is to return a copy of the
  3606. subject string with matched substrings replaced. However, if
  3607. PCRE2_SUBSTITUTE_REPLACEMENT_ONLY is set, only the replacement substrings are
  3608. returned. In the global case, multiple replacements are concatenated in the
  3609. output buffer. Substitution callouts (see
  3610. <a href="#subcallouts">below)</a>
  3611. can be used to separate them if necessary.
  3612. </P>
  3613. <P>
  3614. The <i>outlengthptr</i> argument of <b>pcre2_substitute()</b> must point to a
  3615. variable that contains the length, in code units, of the output buffer. If the
  3616. function is successful, the value is updated to contain the length in code
  3617. units of the new string, excluding the trailing zero that is automatically
  3618. added.
  3619. </P>
  3620. <P>
  3621. If the function is not successful, the value set via <i>outlengthptr</i> depends
  3622. on the type of error. For syntax errors in the replacement string, the value is
  3623. the offset in the replacement string where the error was detected. For other
  3624. errors, the value is PCRE2_UNSET by default. This includes the case of the
  3625. output buffer being too small, unless PCRE2_SUBSTITUTE_OVERFLOW_LENGTH is set.
  3626. </P>
  3627. <P>
  3628. PCRE2_SUBSTITUTE_OVERFLOW_LENGTH changes what happens when the output buffer is
  3629. too small. The default action is to return PCRE2_ERROR_NOMEMORY immediately. If
  3630. this option is set, however, <b>pcre2_substitute()</b> continues to go through
  3631. the motions of matching and substituting (without, of course, writing anything)
  3632. in order to compute the size of buffer that is needed. This value is passed
  3633. back via the <i>outlengthptr</i> variable, with the result of the function still
  3634. being PCRE2_ERROR_NOMEMORY.
  3635. </P>
  3636. <P>
  3637. Passing a buffer size of zero is a permitted way of finding out how much memory
  3638. is needed for given substitution. However, this does mean that the entire
  3639. operation is carried out twice. Depending on the application, it may be more
  3640. efficient to allocate a large buffer and free the excess afterwards, instead of
  3641. using PCRE2_SUBSTITUTE_OVERFLOW_LENGTH.
  3642. </P>
  3643. <P>
  3644. The replacement string, which is interpreted as a UTF string in UTF mode, is
  3645. checked for UTF validity unless PCRE2_NO_UTF_CHECK is set. An invalid UTF
  3646. replacement string causes an immediate return with the relevant UTF error code.
  3647. </P>
  3648. <P>
  3649. If PCRE2_SUBSTITUTE_LITERAL is set, the replacement string is not interpreted
  3650. in any way. By default, however, a dollar character is an escape character that
  3651. can specify the insertion of characters from capture groups and names from
  3652. (*MARK) or other control verbs in the pattern. Dollar is the only escape
  3653. character (backslash is treated as literal). The following forms are always
  3654. recognized:
  3655. <pre>
  3656. $$ insert a dollar character
  3657. $&#60;n&#62; or ${&#60;n&#62;} insert the contents of group &#60;n&#62;
  3658. $*MARK or ${*MARK} insert a control verb name
  3659. </pre>
  3660. Either a group number or a group name can be given for &#60;n&#62;. Curly brackets are
  3661. required only if the following character would be interpreted as part of the
  3662. number or name. The number may be zero to include the entire matched string.
  3663. For example, if the pattern a(b)c is matched with "=abc=" and the replacement
  3664. string "+$1$0$1+", the result is "=+babcb+=".
  3665. </P>
  3666. <P>
  3667. $*MARK inserts the name from the last encountered backtracking control verb on
  3668. the matching path that has a name. (*MARK) must always include a name, but the
  3669. other verbs need not. For example, in the case of (*MARK:A)(*PRUNE) the name
  3670. inserted is "A", but for (*MARK:A)(*PRUNE:B) the relevant name is "B". This
  3671. facility can be used to perform simple simultaneous substitutions, as this
  3672. <b>pcre2test</b> example shows:
  3673. <pre>
  3674. /(*MARK:pear)apple|(*MARK:orange)lemon/g,replace=${*MARK}
  3675. apple lemon
  3676. 2: pear orange
  3677. </pre>
  3678. PCRE2_SUBSTITUTE_GLOBAL causes the function to iterate over the subject string,
  3679. replacing every matching substring. If this option is not set, only the first
  3680. matching substring is replaced. The search for matches takes place in the
  3681. original subject string (that is, previous replacements do not affect it).
  3682. Iteration is implemented by advancing the <i>startoffset</i> value for each
  3683. search, which is always passed the entire subject string. If an offset limit is
  3684. set in the match context, searching stops when that limit is reached.
  3685. </P>
  3686. <P>
  3687. You can restrict the effect of a global substitution to a portion of the
  3688. subject string by setting either or both of <i>startoffset</i> and an offset
  3689. limit. Here is a <b>pcre2test</b> example:
  3690. <pre>
  3691. /B/g,replace=!,use_offset_limit
  3692. ABC ABC ABC ABC\=offset=3,offset_limit=12
  3693. 2: ABC A!C A!C ABC
  3694. </pre>
  3695. When continuing with global substitutions after matching a substring with zero
  3696. length, an attempt to find a non-empty match at the same offset is performed.
  3697. If this is not successful, the offset is advanced by one character except when
  3698. CRLF is a valid newline sequence and the next two characters are CR, LF. In
  3699. this case, the offset is advanced by two characters.
  3700. </P>
  3701. <P>
  3702. PCRE2_SUBSTITUTE_UNKNOWN_UNSET causes references to capture groups that do
  3703. not appear in the pattern to be treated as unset groups. This option should be
  3704. used with care, because it means that a typo in a group name or number no
  3705. longer causes the PCRE2_ERROR_NOSUBSTRING error.
  3706. </P>
  3707. <P>
  3708. PCRE2_SUBSTITUTE_UNSET_EMPTY causes unset capture groups (including unknown
  3709. groups when PCRE2_SUBSTITUTE_UNKNOWN_UNSET is set) to be treated as empty
  3710. strings when inserted as described above. If this option is not set, an attempt
  3711. to insert an unset group causes the PCRE2_ERROR_UNSET error. This option does
  3712. not influence the extended substitution syntax described below.
  3713. </P>
  3714. <P>
  3715. PCRE2_SUBSTITUTE_EXTENDED causes extra processing to be applied to the
  3716. replacement string. Without this option, only the dollar character is special,
  3717. and only the group insertion forms listed above are valid. When
  3718. PCRE2_SUBSTITUTE_EXTENDED is set, two things change:
  3719. </P>
  3720. <P>
  3721. Firstly, backslash in a replacement string is interpreted as an escape
  3722. character. The usual forms such as \n or \x{ddd} can be used to specify
  3723. particular character codes, and backslash followed by any non-alphanumeric
  3724. character quotes that character. Extended quoting can be coded using \Q...\E,
  3725. exactly as in pattern strings.
  3726. </P>
  3727. <P>
  3728. There are also four escape sequences for forcing the case of inserted letters.
  3729. The insertion mechanism has three states: no case forcing, force upper case,
  3730. and force lower case. The escape sequences change the current state: \U and
  3731. \L change to upper or lower case forcing, respectively, and \E (when not
  3732. terminating a \Q quoted sequence) reverts to no case forcing. The sequences
  3733. \u and \l force the next character (if it is a letter) to upper or lower
  3734. case, respectively, and then the state automatically reverts to no case
  3735. forcing. Case forcing applies to all inserted characters, including those from
  3736. capture groups and letters within \Q...\E quoted sequences. If either
  3737. PCRE2_UTF or PCRE2_UCP was set when the pattern was compiled, Unicode
  3738. properties are used for case forcing characters whose code points are greater
  3739. than 127.
  3740. </P>
  3741. <P>
  3742. Note that case forcing sequences such as \U...\E do not nest. For example,
  3743. the result of processing "\Uaa\LBB\Ecc\E" is "AAbbcc"; the final \E has no
  3744. effect. Note also that the PCRE2_ALT_BSUX and PCRE2_EXTRA_ALT_BSUX options do
  3745. not apply to replacement strings.
  3746. </P>
  3747. <P>
  3748. The second effect of setting PCRE2_SUBSTITUTE_EXTENDED is to add more
  3749. flexibility to capture group substitution. The syntax is similar to that used
  3750. by Bash:
  3751. <pre>
  3752. ${&#60;n&#62;:-&#60;string&#62;}
  3753. ${&#60;n&#62;:+&#60;string1&#62;:&#60;string2&#62;}
  3754. </pre>
  3755. As before, &#60;n&#62; may be a group number or a name. The first form specifies a
  3756. default value. If group &#60;n&#62; is set, its value is inserted; if not, &#60;string&#62; is
  3757. expanded and the result inserted. The second form specifies strings that are
  3758. expanded and inserted when group &#60;n&#62; is set or unset, respectively. The first
  3759. form is just a convenient shorthand for
  3760. <pre>
  3761. ${&#60;n&#62;:+${&#60;n&#62;}:&#60;string&#62;}
  3762. </pre>
  3763. Backslash can be used to escape colons and closing curly brackets in the
  3764. replacement strings. A change of the case forcing state within a replacement
  3765. string remains in force afterwards, as shown in this <b>pcre2test</b> example:
  3766. <pre>
  3767. /(some)?(body)/substitute_extended,replace=${1:+\U:\L}HeLLo
  3768. body
  3769. 1: hello
  3770. somebody
  3771. 1: HELLO
  3772. </pre>
  3773. The PCRE2_SUBSTITUTE_UNSET_EMPTY option does not affect these extended
  3774. substitutions. However, PCRE2_SUBSTITUTE_UNKNOWN_UNSET does cause unknown
  3775. groups in the extended syntax forms to be treated as unset.
  3776. </P>
  3777. <P>
  3778. If PCRE2_SUBSTITUTE_LITERAL is set, PCRE2_SUBSTITUTE_UNKNOWN_UNSET,
  3779. PCRE2_SUBSTITUTE_UNSET_EMPTY, and PCRE2_SUBSTITUTE_EXTENDED are irrelevant and
  3780. are ignored.
  3781. </P>
  3782. <br><b>
  3783. Substitution errors
  3784. </b><br>
  3785. <P>
  3786. In the event of an error, <b>pcre2_substitute()</b> returns a negative error
  3787. code. Except for PCRE2_ERROR_NOMATCH (which is never returned), errors from
  3788. <b>pcre2_match()</b> are passed straight back.
  3789. </P>
  3790. <P>
  3791. PCRE2_ERROR_NOSUBSTRING is returned for a non-existent substring insertion,
  3792. unless PCRE2_SUBSTITUTE_UNKNOWN_UNSET is set.
  3793. </P>
  3794. <P>
  3795. PCRE2_ERROR_UNSET is returned for an unset substring insertion (including an
  3796. unknown substring when PCRE2_SUBSTITUTE_UNKNOWN_UNSET is set) when the simple
  3797. (non-extended) syntax is used and PCRE2_SUBSTITUTE_UNSET_EMPTY is not set.
  3798. </P>
  3799. <P>
  3800. PCRE2_ERROR_NOMEMORY is returned if the output buffer is not big enough. If the
  3801. PCRE2_SUBSTITUTE_OVERFLOW_LENGTH option is set, the size of buffer that is
  3802. needed is returned via <i>outlengthptr</i>. Note that this does not happen by
  3803. default.
  3804. </P>
  3805. <P>
  3806. PCRE2_ERROR_NULL is returned if PCRE2_SUBSTITUTE_MATCHED is set but the
  3807. <i>match_data</i> argument is NULL or if the <i>subject</i> or <i>replacement</i>
  3808. arguments are NULL. For backward compatibility reasons an exception is made for
  3809. the <i>replacement</i> argument if the <i>rlength</i> argument is also 0.
  3810. </P>
  3811. <P>
  3812. PCRE2_ERROR_BADREPLACEMENT is used for miscellaneous syntax errors in the
  3813. replacement string, with more particular errors being PCRE2_ERROR_BADREPESCAPE
  3814. (invalid escape sequence), PCRE2_ERROR_REPMISSINGBRACE (closing curly bracket
  3815. not found), PCRE2_ERROR_BADSUBSTITUTION (syntax error in extended group
  3816. substitution), and PCRE2_ERROR_BADSUBSPATTERN (the pattern match ended before
  3817. it started or the match started earlier than the current position in the
  3818. subject, which can happen if \K is used in an assertion).
  3819. </P>
  3820. <P>
  3821. As for all PCRE2 errors, a text message that describes the error can be
  3822. obtained by calling the <b>pcre2_get_error_message()</b> function (see
  3823. "Obtaining a textual error message"
  3824. <a href="#geterrormessage">above).</a>
  3825. <a name="subcallouts"></a></P>
  3826. <br><b>
  3827. Substitution callouts
  3828. </b><br>
  3829. <P>
  3830. <b>int pcre2_set_substitute_callout(pcre2_match_context *<i>mcontext</i>,</b>
  3831. <b> int (*<i>callout_function</i>)(pcre2_substitute_callout_block *, void *),</b>
  3832. <b> void *<i>callout_data</i>);</b>
  3833. <br>
  3834. <br>
  3835. The <b>pcre2_set_substitution_callout()</b> function can be used to specify a
  3836. callout function for <b>pcre2_substitute()</b>. This information is passed in
  3837. a match context. The callout function is called after each substitution has
  3838. been processed, but it can cause the replacement not to happen. The callout
  3839. function is not called for simulated substitutions that happen as a result of
  3840. the PCRE2_SUBSTITUTE_OVERFLOW_LENGTH option.
  3841. </P>
  3842. <P>
  3843. The first argument of the callout function is a pointer to a substitute callout
  3844. block structure, which contains the following fields, not necessarily in this
  3845. order:
  3846. <pre>
  3847. uint32_t <i>version</i>;
  3848. uint32_t <i>subscount</i>;
  3849. PCRE2_SPTR <i>input</i>;
  3850. PCRE2_SPTR <i>output</i>;
  3851. PCRE2_SIZE <i>*ovector</i>;
  3852. uint32_t <i>oveccount</i>;
  3853. PCRE2_SIZE <i>output_offsets[2]</i>;
  3854. </pre>
  3855. The <i>version</i> field contains the version number of the block format. The
  3856. current version is 0. The version number will increase in future if more fields
  3857. are added, but the intention is never to remove any of the existing fields.
  3858. </P>
  3859. <P>
  3860. The <i>subscount</i> field is the number of the current match. It is 1 for the
  3861. first callout, 2 for the second, and so on. The <i>input</i> and <i>output</i>
  3862. pointers are copies of the values passed to <b>pcre2_substitute()</b>.
  3863. </P>
  3864. <P>
  3865. The <i>ovector</i> field points to the ovector, which contains the result of the
  3866. most recent match. The <i>oveccount</i> field contains the number of pairs that
  3867. are set in the ovector, and is always greater than zero.
  3868. </P>
  3869. <P>
  3870. The <i>output_offsets</i> vector contains the offsets of the replacement in the
  3871. output string. This has already been processed for dollar and (if requested)
  3872. backslash substitutions as described above.
  3873. </P>
  3874. <P>
  3875. The second argument of the callout function is the value passed as
  3876. <i>callout_data</i> when the function was registered. The value returned by the
  3877. callout function is interpreted as follows:
  3878. </P>
  3879. <P>
  3880. If the value is zero, the replacement is accepted, and, if
  3881. PCRE2_SUBSTITUTE_GLOBAL is set, processing continues with a search for the next
  3882. match. If the value is not zero, the current replacement is not accepted. If
  3883. the value is greater than zero, processing continues when
  3884. PCRE2_SUBSTITUTE_GLOBAL is set. Otherwise (the value is less than zero or
  3885. PCRE2_SUBSTITUTE_GLOBAL is not set), the rest of the input is copied to the
  3886. output and the call to <b>pcre2_substitute()</b> exits, returning the number of
  3887. matches so far.
  3888. </P>
  3889. <br><a name="SEC38" href="#TOC1">DUPLICATE CAPTURE GROUP NAMES</a><br>
  3890. <P>
  3891. <b>int pcre2_substring_nametable_scan(const pcre2_code *<i>code</i>,</b>
  3892. <b> PCRE2_SPTR <i>name</i>, PCRE2_SPTR *<i>first</i>, PCRE2_SPTR *<i>last</i>);</b>
  3893. </P>
  3894. <P>
  3895. When a pattern is compiled with the PCRE2_DUPNAMES option, names for capture
  3896. groups are not required to be unique. Duplicate names are always allowed for
  3897. groups with the same number, created by using the (?| feature. Indeed, if such
  3898. groups are named, they are required to use the same names.
  3899. </P>
  3900. <P>
  3901. Normally, patterns that use duplicate names are such that in any one match,
  3902. only one of each set of identically-named groups participates. An example is
  3903. shown in the
  3904. <a href="pcre2pattern.html"><b>pcre2pattern</b></a>
  3905. documentation.
  3906. </P>
  3907. <P>
  3908. When duplicates are present, <b>pcre2_substring_copy_byname()</b> and
  3909. <b>pcre2_substring_get_byname()</b> return the first substring corresponding to
  3910. the given name that is set. Only if none are set is PCRE2_ERROR_UNSET is
  3911. returned. The <b>pcre2_substring_number_from_name()</b> function returns the
  3912. error PCRE2_ERROR_NOUNIQUESUBSTRING when there are duplicate names.
  3913. </P>
  3914. <P>
  3915. If you want to get full details of all captured substrings for a given name,
  3916. you must use the <b>pcre2_substring_nametable_scan()</b> function. The first
  3917. argument is the compiled pattern, and the second is the name. If the third and
  3918. fourth arguments are NULL, the function returns a group number for a unique
  3919. name, or PCRE2_ERROR_NOUNIQUESUBSTRING otherwise.
  3920. </P>
  3921. <P>
  3922. When the third and fourth arguments are not NULL, they must be pointers to
  3923. variables that are updated by the function. After it has run, they point to the
  3924. first and last entries in the name-to-number table for the given name, and the
  3925. function returns the length of each entry in code units. In both cases,
  3926. PCRE2_ERROR_NOSUBSTRING is returned if there are no entries for the given name.
  3927. </P>
  3928. <P>
  3929. The format of the name table is described
  3930. <a href="#infoaboutpattern">above</a>
  3931. in the section entitled <i>Information about a pattern</i>. Given all the
  3932. relevant entries for the name, you can extract each of their numbers, and hence
  3933. the captured data.
  3934. </P>
  3935. <br><a name="SEC39" href="#TOC1">FINDING ALL POSSIBLE MATCHES AT ONE POSITION</a><br>
  3936. <P>
  3937. The traditional matching function uses a similar algorithm to Perl, which stops
  3938. when it finds the first match at a given point in the subject. If you want to
  3939. find all possible matches, or the longest possible match at a given position,
  3940. consider using the alternative matching function (see below) instead. If you
  3941. cannot use the alternative function, you can kludge it up by making use of the
  3942. callout facility, which is described in the
  3943. <a href="pcre2callout.html"><b>pcre2callout</b></a>
  3944. documentation.
  3945. </P>
  3946. <P>
  3947. What you have to do is to insert a callout right at the end of the pattern.
  3948. When your callout function is called, extract and save the current matched
  3949. substring. Then return 1, which forces <b>pcre2_match()</b> to backtrack and try
  3950. other alternatives. Ultimately, when it runs out of matches,
  3951. <b>pcre2_match()</b> will yield PCRE2_ERROR_NOMATCH.
  3952. <a name="dfamatch"></a></P>
  3953. <br><a name="SEC40" href="#TOC1">MATCHING A PATTERN: THE ALTERNATIVE FUNCTION</a><br>
  3954. <P>
  3955. <b>int pcre2_dfa_match(const pcre2_code *<i>code</i>, PCRE2_SPTR <i>subject</i>,</b>
  3956. <b> PCRE2_SIZE <i>length</i>, PCRE2_SIZE <i>startoffset</i>,</b>
  3957. <b> uint32_t <i>options</i>, pcre2_match_data *<i>match_data</i>,</b>
  3958. <b> pcre2_match_context *<i>mcontext</i>,</b>
  3959. <b> int *<i>workspace</i>, PCRE2_SIZE <i>wscount</i>);</b>
  3960. </P>
  3961. <P>
  3962. The function <b>pcre2_dfa_match()</b> is called to match a subject string
  3963. against a compiled pattern, using a matching algorithm that scans the subject
  3964. string just once (not counting lookaround assertions), and does not backtrack
  3965. (except when processing lookaround assertions). This has different
  3966. characteristics to the normal algorithm, and is not compatible with Perl. Some
  3967. of the features of PCRE2 patterns are not supported. Nevertheless, there are
  3968. times when this kind of matching can be useful. For a discussion of the two
  3969. matching algorithms, and a list of features that <b>pcre2_dfa_match()</b> does
  3970. not support, see the
  3971. <a href="pcre2matching.html"><b>pcre2matching</b></a>
  3972. documentation.
  3973. </P>
  3974. <P>
  3975. The arguments for the <b>pcre2_dfa_match()</b> function are the same as for
  3976. <b>pcre2_match()</b>, plus two extras. The ovector within the match data block
  3977. is used in a different way, and this is described below. The other common
  3978. arguments are used in the same way as for <b>pcre2_match()</b>, so their
  3979. description is not repeated here.
  3980. </P>
  3981. <P>
  3982. The two additional arguments provide workspace for the function. The workspace
  3983. vector should contain at least 20 elements. It is used for keeping track of
  3984. multiple paths through the pattern tree. More workspace is needed for patterns
  3985. and subjects where there are a lot of potential matches.
  3986. </P>
  3987. <P>
  3988. Here is an example of a simple call to <b>pcre2_dfa_match()</b>:
  3989. <pre>
  3990. int wspace[20];
  3991. pcre2_match_data *md = pcre2_match_data_create(4, NULL);
  3992. int rc = pcre2_dfa_match(
  3993. re, /* result of pcre2_compile() */
  3994. "some string", /* the subject string */
  3995. 11, /* the length of the subject string */
  3996. 0, /* start at offset 0 in the subject */
  3997. 0, /* default options */
  3998. md, /* the match data block */
  3999. NULL, /* a match context; NULL means use defaults */
  4000. wspace, /* working space vector */
  4001. 20); /* number of elements (NOT size in bytes) */
  4002. </PRE>
  4003. </P>
  4004. <br><b>
  4005. Option bits for <b>pcre2_dfa_match()</b>
  4006. </b><br>
  4007. <P>
  4008. The unused bits of the <i>options</i> argument for <b>pcre2_dfa_match()</b> must
  4009. be zero. The only bits that may be set are PCRE2_ANCHORED,
  4010. PCRE2_COPY_MATCHED_SUBJECT, PCRE2_ENDANCHORED, PCRE2_NOTBOL, PCRE2_NOTEOL,
  4011. PCRE2_NOTEMPTY, PCRE2_NOTEMPTY_ATSTART, PCRE2_NO_UTF_CHECK, PCRE2_PARTIAL_HARD,
  4012. PCRE2_PARTIAL_SOFT, PCRE2_DFA_SHORTEST, and PCRE2_DFA_RESTART. All but the last
  4013. four of these are exactly the same as for <b>pcre2_match()</b>, so their
  4014. description is not repeated here.
  4015. <pre>
  4016. PCRE2_PARTIAL_HARD
  4017. PCRE2_PARTIAL_SOFT
  4018. </pre>
  4019. These have the same general effect as they do for <b>pcre2_match()</b>, but the
  4020. details are slightly different. When PCRE2_PARTIAL_HARD is set for
  4021. <b>pcre2_dfa_match()</b>, it returns PCRE2_ERROR_PARTIAL if the end of the
  4022. subject is reached and there is still at least one matching possibility that
  4023. requires additional characters. This happens even if some complete matches have
  4024. already been found. When PCRE2_PARTIAL_SOFT is set, the return code
  4025. PCRE2_ERROR_NOMATCH is converted into PCRE2_ERROR_PARTIAL if the end of the
  4026. subject is reached, there have been no complete matches, but there is still at
  4027. least one matching possibility. The portion of the string that was inspected
  4028. when the longest partial match was found is set as the first matching string in
  4029. both cases. There is a more detailed discussion of partial and multi-segment
  4030. matching, with examples, in the
  4031. <a href="pcre2partial.html"><b>pcre2partial</b></a>
  4032. documentation.
  4033. <pre>
  4034. PCRE2_DFA_SHORTEST
  4035. </pre>
  4036. Setting the PCRE2_DFA_SHORTEST option causes the matching algorithm to stop as
  4037. soon as it has found one match. Because of the way the alternative algorithm
  4038. works, this is necessarily the shortest possible match at the first possible
  4039. matching point in the subject string.
  4040. <pre>
  4041. PCRE2_DFA_RESTART
  4042. </pre>
  4043. When <b>pcre2_dfa_match()</b> returns a partial match, it is possible to call it
  4044. again, with additional subject characters, and have it continue with the same
  4045. match. The PCRE2_DFA_RESTART option requests this action; when it is set, the
  4046. <i>workspace</i> and <i>wscount</i> options must reference the same vector as
  4047. before because data about the match so far is left in them after a partial
  4048. match. There is more discussion of this facility in the
  4049. <a href="pcre2partial.html"><b>pcre2partial</b></a>
  4050. documentation.
  4051. </P>
  4052. <br><b>
  4053. Successful returns from <b>pcre2_dfa_match()</b>
  4054. </b><br>
  4055. <P>
  4056. When <b>pcre2_dfa_match()</b> succeeds, it may have matched more than one
  4057. substring in the subject. Note, however, that all the matches from one run of
  4058. the function start at the same point in the subject. The shorter matches are
  4059. all initial substrings of the longer matches. For example, if the pattern
  4060. <pre>
  4061. &#60;.*&#62;
  4062. </pre>
  4063. is matched against the string
  4064. <pre>
  4065. This is &#60;something&#62; &#60;something else&#62; &#60;something further&#62; no more
  4066. </pre>
  4067. the three matched strings are
  4068. <pre>
  4069. &#60;something&#62; &#60;something else&#62; &#60;something further&#62;
  4070. &#60;something&#62; &#60;something else&#62;
  4071. &#60;something&#62;
  4072. </pre>
  4073. On success, the yield of the function is a number greater than zero, which is
  4074. the number of matched substrings. The offsets of the substrings are returned in
  4075. the ovector, and can be extracted by number in the same way as for
  4076. <b>pcre2_match()</b>, but the numbers bear no relation to any capture groups
  4077. that may exist in the pattern, because DFA matching does not support capturing.
  4078. </P>
  4079. <P>
  4080. Calls to the convenience functions that extract substrings by name
  4081. return the error PCRE2_ERROR_DFA_UFUNC (unsupported function) if used after a
  4082. DFA match. The convenience functions that extract substrings by number never
  4083. return PCRE2_ERROR_NOSUBSTRING.
  4084. </P>
  4085. <P>
  4086. The matched strings are stored in the ovector in reverse order of length; that
  4087. is, the longest matching string is first. If there were too many matches to fit
  4088. into the ovector, the yield of the function is zero, and the vector is filled
  4089. with the longest matches.
  4090. </P>
  4091. <P>
  4092. NOTE: PCRE2's "auto-possessification" optimization usually applies to character
  4093. repeats at the end of a pattern (as well as internally). For example, the
  4094. pattern "a\d+" is compiled as if it were "a\d++". For DFA matching, this
  4095. means that only one possible match is found. If you really do want multiple
  4096. matches in such cases, either use an ungreedy repeat such as "a\d+?" or set
  4097. the PCRE2_NO_AUTO_POSSESS option when compiling.
  4098. </P>
  4099. <br><b>
  4100. Error returns from <b>pcre2_dfa_match()</b>
  4101. </b><br>
  4102. <P>
  4103. The <b>pcre2_dfa_match()</b> function returns a negative number when it fails.
  4104. Many of the errors are the same as for <b>pcre2_match()</b>, as described
  4105. <a href="#errorlist">above.</a>
  4106. There are in addition the following errors that are specific to
  4107. <b>pcre2_dfa_match()</b>:
  4108. <pre>
  4109. PCRE2_ERROR_DFA_UITEM
  4110. </pre>
  4111. This return is given if <b>pcre2_dfa_match()</b> encounters an item in the
  4112. pattern that it does not support, for instance, the use of \C in a UTF mode or
  4113. a backreference.
  4114. <pre>
  4115. PCRE2_ERROR_DFA_UCOND
  4116. </pre>
  4117. This return is given if <b>pcre2_dfa_match()</b> encounters a condition item
  4118. that uses a backreference for the condition, or a test for recursion in a
  4119. specific capture group. These are not supported.
  4120. <pre>
  4121. PCRE2_ERROR_DFA_UINVALID_UTF
  4122. </pre>
  4123. This return is given if <b>pcre2_dfa_match()</b> is called for a pattern that
  4124. was compiled with PCRE2_MATCH_INVALID_UTF. This is not supported for DFA
  4125. matching.
  4126. <pre>
  4127. PCRE2_ERROR_DFA_WSSIZE
  4128. </pre>
  4129. This return is given if <b>pcre2_dfa_match()</b> runs out of space in the
  4130. <i>workspace</i> vector.
  4131. <pre>
  4132. PCRE2_ERROR_DFA_RECURSE
  4133. </pre>
  4134. When a recursion or subroutine call is processed, the matching function calls
  4135. itself recursively, using private memory for the ovector and <i>workspace</i>.
  4136. This error is given if the internal ovector is not large enough. This should be
  4137. extremely rare, as a vector of size 1000 is used.
  4138. <pre>
  4139. PCRE2_ERROR_DFA_BADRESTART
  4140. </pre>
  4141. When <b>pcre2_dfa_match()</b> is called with the <b>PCRE2_DFA_RESTART</b> option,
  4142. some plausibility checks are made on the contents of the workspace, which
  4143. should contain data about the previous partial match. If any of these checks
  4144. fail, this error is given.
  4145. </P>
  4146. <br><a name="SEC41" href="#TOC1">SEE ALSO</a><br>
  4147. <P>
  4148. <b>pcre2build</b>(3), <b>pcre2callout</b>(3), <b>pcre2demo(3)</b>,
  4149. <b>pcre2matching</b>(3), <b>pcre2partial</b>(3), <b>pcre2posix</b>(3),
  4150. <b>pcre2sample</b>(3), <b>pcre2unicode</b>(3).
  4151. </P>
  4152. <br><a name="SEC42" href="#TOC1">AUTHOR</a><br>
  4153. <P>
  4154. Philip Hazel
  4155. <br>
  4156. Retired from University Computing Service
  4157. <br>
  4158. Cambridge, England.
  4159. <br>
  4160. </P>
  4161. <br><a name="SEC43" href="#TOC1">REVISION</a><br>
  4162. <P>
  4163. Last updated: 27 January 2024
  4164. <br>
  4165. Copyright &copy; 1997-2024 University of Cambridge.
  4166. <br>
  4167. <p>
  4168. Return to the <a href="index.html">PCRE2 index page</a>.
  4169. </p>