pcre2api.html 182 KB

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