pcre.txt 494 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455
  1. -----------------------------------------------------------------------------
  2. This file contains a concatenation of the PCRE man pages, converted to plain
  3. text format for ease of searching with a text editor, or for use on systems
  4. that do not have a man page processor. The small individual files that give
  5. synopses of each function in the library have not been included. Neither has
  6. the pcredemo program. There are separate text files for the pcregrep and
  7. pcretest commands.
  8. -----------------------------------------------------------------------------
  9. PCRE(3) Library Functions Manual PCRE(3)
  10. NAME
  11. PCRE - Perl-compatible regular expressions (original API)
  12. PLEASE TAKE NOTE
  13. This document relates to PCRE releases that use the original API, with
  14. library names libpcre, libpcre16, and libpcre32. January 2015 saw the
  15. first release of a new API, known as PCRE2, with release numbers start-
  16. ing at 10.00 and library names libpcre2-8, libpcre2-16, and
  17. libpcre2-32. The old libraries (now called PCRE1) are still being main-
  18. tained for bug fixes, but there will be no new development. New
  19. projects are advised to use the new PCRE2 libraries.
  20. INTRODUCTION
  21. The PCRE library is a set of functions that implement regular expres-
  22. sion pattern matching using the same syntax and semantics as Perl, with
  23. just a few differences. Some features that appeared in Python and PCRE
  24. before they appeared in Perl are also available using the Python syn-
  25. tax, there is some support for one or two .NET and Oniguruma syntax
  26. items, and there is an option for requesting some minor changes that
  27. give better JavaScript compatibility.
  28. Starting with release 8.30, it is possible to compile two separate PCRE
  29. libraries: the original, which supports 8-bit character strings
  30. (including UTF-8 strings), and a second library that supports 16-bit
  31. character strings (including UTF-16 strings). The build process allows
  32. either one or both to be built. The majority of the work to make this
  33. possible was done by Zoltan Herczeg.
  34. Starting with release 8.32 it is possible to compile a third separate
  35. PCRE library that supports 32-bit character strings (including UTF-32
  36. strings). The build process allows any combination of the 8-, 16- and
  37. 32-bit libraries. The work to make this possible was done by Christian
  38. Persch.
  39. The three libraries contain identical sets of functions, except that
  40. the names in the 16-bit library start with pcre16_ instead of pcre_,
  41. and the names in the 32-bit library start with pcre32_ instead of
  42. pcre_. To avoid over-complication and reduce the documentation mainte-
  43. nance load, most of the documentation describes the 8-bit library, with
  44. the differences for the 16-bit and 32-bit libraries described sepa-
  45. rately in the pcre16 and pcre32 pages. References to functions or
  46. structures of the form pcre[16|32]_xxx should be read as meaning
  47. "pcre_xxx when using the 8-bit library, pcre16_xxx when using the
  48. 16-bit library, or pcre32_xxx when using the 32-bit library".
  49. The current implementation of PCRE corresponds approximately with Perl
  50. 5.12, including support for UTF-8/16/32 encoded strings and Unicode
  51. general category properties. However, UTF-8/16/32 and Unicode support
  52. has to be explicitly enabled; it is not the default. The Unicode tables
  53. correspond to Unicode release 6.3.0.
  54. In addition to the Perl-compatible matching function, PCRE contains an
  55. alternative function that matches the same compiled patterns in a dif-
  56. ferent way. In certain circumstances, the alternative function has some
  57. advantages. For a discussion of the two matching algorithms, see the
  58. pcrematching page.
  59. PCRE is written in C and released as a C library. A number of people
  60. have written wrappers and interfaces of various kinds. In particular,
  61. Google Inc. have provided a comprehensive C++ wrapper for the 8-bit
  62. library. This is now included as part of the PCRE distribution. The
  63. pcrecpp page has details of this interface. Other people's contribu-
  64. tions can be found in the Contrib directory at the primary FTP site,
  65. which is:
  66. ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre
  67. Details of exactly which Perl regular expression features are and are
  68. not supported by PCRE are given in separate documents. See the pcrepat-
  69. tern and pcrecompat pages. There is a syntax summary in the pcresyntax
  70. page.
  71. Some features of PCRE can be included, excluded, or changed when the
  72. library is built. The pcre_config() function makes it possible for a
  73. client to discover which features are available. The features them-
  74. selves are described in the pcrebuild page. Documentation about build-
  75. ing PCRE for various operating systems can be found in the README and
  76. NON-AUTOTOOLS_BUILD files in the source distribution.
  77. The libraries contains a number of undocumented internal functions and
  78. data tables that are used by more than one of the exported external
  79. functions, but which are not intended for use by external callers.
  80. Their names all begin with "_pcre_" or "_pcre16_" or "_pcre32_", which
  81. hopefully will not provoke any name clashes. In some environments, it
  82. is possible to control which external symbols are exported when a
  83. shared library is built, and in these cases the undocumented symbols
  84. are not exported.
  85. SECURITY CONSIDERATIONS
  86. If you are using PCRE in a non-UTF application that permits users to
  87. supply arbitrary patterns for compilation, you should be aware of a
  88. feature that allows users to turn on UTF support from within a pattern,
  89. provided that PCRE was built with UTF support. For example, an 8-bit
  90. pattern that begins with "(*UTF8)" or "(*UTF)" turns on UTF-8 mode,
  91. which interprets patterns and subjects as strings of UTF-8 characters
  92. instead of individual 8-bit characters. This causes both the pattern
  93. and any data against which it is matched to be checked for UTF-8 valid-
  94. ity. If the data string is very long, such a check might use suffi-
  95. ciently many resources as to cause your application to lose perfor-
  96. mance.
  97. One way of guarding against this possibility is to use the
  98. pcre_fullinfo() function to check the compiled pattern's options for
  99. UTF. Alternatively, from release 8.33, you can set the PCRE_NEVER_UTF
  100. option at compile time. This causes an compile time error if a pattern
  101. contains a UTF-setting sequence.
  102. If your application is one that supports UTF, be aware that validity
  103. checking can take time. If the same data string is to be matched many
  104. times, you can use the PCRE_NO_UTF[8|16|32]_CHECK option for the second
  105. and subsequent matches to save redundant checks.
  106. Another way that performance can be hit is by running a pattern that
  107. has a very large search tree against a string that will never match.
  108. Nested unlimited repeats in a pattern are a common example. PCRE pro-
  109. vides some protection against this: see the PCRE_EXTRA_MATCH_LIMIT fea-
  110. ture in the pcreapi page.
  111. USER DOCUMENTATION
  112. The user documentation for PCRE comprises a number of different sec-
  113. tions. In the "man" format, each of these is a separate "man page". In
  114. the HTML format, each is a separate page, linked from the index page.
  115. In the plain text format, the descriptions of the pcregrep and pcretest
  116. programs are in files called pcregrep.txt and pcretest.txt, respec-
  117. tively. The remaining sections, except for the pcredemo section (which
  118. is a program listing), are concatenated in pcre.txt, for ease of
  119. searching. The sections are as follows:
  120. pcre this document
  121. pcre-config show PCRE installation configuration information
  122. pcre16 details of the 16-bit library
  123. pcre32 details of the 32-bit library
  124. pcreapi details of PCRE's native C API
  125. pcrebuild building PCRE
  126. pcrecallout details of the callout feature
  127. pcrecompat discussion of Perl compatibility
  128. pcrecpp details of the C++ wrapper for the 8-bit library
  129. pcredemo a demonstration C program that uses PCRE
  130. pcregrep description of the pcregrep command (8-bit only)
  131. pcrejit discussion of the just-in-time optimization support
  132. pcrelimits details of size and other limits
  133. pcrematching discussion of the two matching algorithms
  134. pcrepartial details of the partial matching facility
  135. pcrepattern syntax and semantics of supported
  136. regular expressions
  137. pcreperform discussion of performance issues
  138. pcreposix the POSIX-compatible C API for the 8-bit library
  139. pcreprecompile details of saving and re-using precompiled patterns
  140. pcresample discussion of the pcredemo program
  141. pcrestack discussion of stack usage
  142. pcresyntax quick syntax reference
  143. pcretest description of the pcretest testing command
  144. pcreunicode discussion of Unicode and UTF-8/16/32 support
  145. In the "man" and HTML formats, there is also a short page for each C
  146. library function, listing its arguments and results.
  147. AUTHOR
  148. Philip Hazel
  149. University Computing Service
  150. Cambridge CB2 3QH, England.
  151. Putting an actual email address here seems to have been a spam magnet,
  152. so I've taken it away. If you want to email me, use my two initials,
  153. followed by the two digits 10, at the domain cam.ac.uk.
  154. REVISION
  155. Last updated: 10 February 2015
  156. Copyright (c) 1997-2015 University of Cambridge.
  157. ------------------------------------------------------------------------------
  158. PCRE(3) Library Functions Manual PCRE(3)
  159. NAME
  160. PCRE - Perl-compatible regular expressions
  161. #include <pcre.h>
  162. PCRE 16-BIT API BASIC FUNCTIONS
  163. pcre16 *pcre16_compile(PCRE_SPTR16 pattern, int options,
  164. const char **errptr, int *erroffset,
  165. const unsigned char *tableptr);
  166. pcre16 *pcre16_compile2(PCRE_SPTR16 pattern, int options,
  167. int *errorcodeptr,
  168. const char **errptr, int *erroffset,
  169. const unsigned char *tableptr);
  170. pcre16_extra *pcre16_study(const pcre16 *code, int options,
  171. const char **errptr);
  172. void pcre16_free_study(pcre16_extra *extra);
  173. int pcre16_exec(const pcre16 *code, const pcre16_extra *extra,
  174. PCRE_SPTR16 subject, int length, int startoffset,
  175. int options, int *ovector, int ovecsize);
  176. int pcre16_dfa_exec(const pcre16 *code, const pcre16_extra *extra,
  177. PCRE_SPTR16 subject, int length, int startoffset,
  178. int options, int *ovector, int ovecsize,
  179. int *workspace, int wscount);
  180. PCRE 16-BIT API STRING EXTRACTION FUNCTIONS
  181. int pcre16_copy_named_substring(const pcre16 *code,
  182. PCRE_SPTR16 subject, int *ovector,
  183. int stringcount, PCRE_SPTR16 stringname,
  184. PCRE_UCHAR16 *buffer, int buffersize);
  185. int pcre16_copy_substring(PCRE_SPTR16 subject, int *ovector,
  186. int stringcount, int stringnumber, PCRE_UCHAR16 *buffer,
  187. int buffersize);
  188. int pcre16_get_named_substring(const pcre16 *code,
  189. PCRE_SPTR16 subject, int *ovector,
  190. int stringcount, PCRE_SPTR16 stringname,
  191. PCRE_SPTR16 *stringptr);
  192. int pcre16_get_stringnumber(const pcre16 *code,
  193. PCRE_SPTR16 name);
  194. int pcre16_get_stringtable_entries(const pcre16 *code,
  195. PCRE_SPTR16 name, PCRE_UCHAR16 **first, PCRE_UCHAR16 **last);
  196. int pcre16_get_substring(PCRE_SPTR16 subject, int *ovector,
  197. int stringcount, int stringnumber,
  198. PCRE_SPTR16 *stringptr);
  199. int pcre16_get_substring_list(PCRE_SPTR16 subject,
  200. int *ovector, int stringcount, PCRE_SPTR16 **listptr);
  201. void pcre16_free_substring(PCRE_SPTR16 stringptr);
  202. void pcre16_free_substring_list(PCRE_SPTR16 *stringptr);
  203. PCRE 16-BIT API AUXILIARY FUNCTIONS
  204. pcre16_jit_stack *pcre16_jit_stack_alloc(int startsize, int maxsize);
  205. void pcre16_jit_stack_free(pcre16_jit_stack *stack);
  206. void pcre16_assign_jit_stack(pcre16_extra *extra,
  207. pcre16_jit_callback callback, void *data);
  208. const unsigned char *pcre16_maketables(void);
  209. int pcre16_fullinfo(const pcre16 *code, const pcre16_extra *extra,
  210. int what, void *where);
  211. int pcre16_refcount(pcre16 *code, int adjust);
  212. int pcre16_config(int what, void *where);
  213. const char *pcre16_version(void);
  214. int pcre16_pattern_to_host_byte_order(pcre16 *code,
  215. pcre16_extra *extra, const unsigned char *tables);
  216. PCRE 16-BIT API INDIRECTED FUNCTIONS
  217. void *(*pcre16_malloc)(size_t);
  218. void (*pcre16_free)(void *);
  219. void *(*pcre16_stack_malloc)(size_t);
  220. void (*pcre16_stack_free)(void *);
  221. int (*pcre16_callout)(pcre16_callout_block *);
  222. PCRE 16-BIT API 16-BIT-ONLY FUNCTION
  223. int pcre16_utf16_to_host_byte_order(PCRE_UCHAR16 *output,
  224. PCRE_SPTR16 input, int length, int *byte_order,
  225. int keep_boms);
  226. THE PCRE 16-BIT LIBRARY
  227. Starting with release 8.30, it is possible to compile a PCRE library
  228. that supports 16-bit character strings, including UTF-16 strings, as
  229. well as or instead of the original 8-bit library. The majority of the
  230. work to make this possible was done by Zoltan Herczeg. The two
  231. libraries contain identical sets of functions, used in exactly the same
  232. way. Only the names of the functions and the data types of their argu-
  233. ments and results are different. To avoid over-complication and reduce
  234. the documentation maintenance load, most of the PCRE documentation
  235. describes the 8-bit library, with only occasional references to the
  236. 16-bit library. This page describes what is different when you use the
  237. 16-bit library.
  238. WARNING: A single application can be linked with both libraries, but
  239. you must take care when processing any particular pattern to use func-
  240. tions from just one library. For example, if you want to study a pat-
  241. tern that was compiled with pcre16_compile(), you must do so with
  242. pcre16_study(), not pcre_study(), and you must free the study data with
  243. pcre16_free_study().
  244. THE HEADER FILE
  245. There is only one header file, pcre.h. It contains prototypes for all
  246. the functions in all libraries, as well as definitions of flags, struc-
  247. tures, error codes, etc.
  248. THE LIBRARY NAME
  249. In Unix-like systems, the 16-bit library is called libpcre16, and can
  250. normally be accesss by adding -lpcre16 to the command for linking an
  251. application that uses PCRE.
  252. STRING TYPES
  253. In the 8-bit library, strings are passed to PCRE library functions as
  254. vectors of bytes with the C type "char *". In the 16-bit library,
  255. strings are passed as vectors of unsigned 16-bit quantities. The macro
  256. PCRE_UCHAR16 specifies an appropriate data type, and PCRE_SPTR16 is
  257. defined as "const PCRE_UCHAR16 *". In very many environments, "short
  258. int" is a 16-bit data type. When PCRE is built, it defines PCRE_UCHAR16
  259. as "unsigned short int", but checks that it really is a 16-bit data
  260. type. If it is not, the build fails with an error message telling the
  261. maintainer to modify the definition appropriately.
  262. STRUCTURE TYPES
  263. The types of the opaque structures that are used for compiled 16-bit
  264. patterns and JIT stacks are pcre16 and pcre16_jit_stack respectively.
  265. The type of the user-accessible structure that is returned by
  266. pcre16_study() is pcre16_extra, and the type of the structure that is
  267. used for passing data to a callout function is pcre16_callout_block.
  268. These structures contain the same fields, with the same names, as their
  269. 8-bit counterparts. The only difference is that pointers to character
  270. strings are 16-bit instead of 8-bit types.
  271. 16-BIT FUNCTIONS
  272. For every function in the 8-bit library there is a corresponding func-
  273. tion in the 16-bit library with a name that starts with pcre16_ instead
  274. of pcre_. The prototypes are listed above. In addition, there is one
  275. extra function, pcre16_utf16_to_host_byte_order(). This is a utility
  276. function that converts a UTF-16 character string to host byte order if
  277. necessary. The other 16-bit functions expect the strings they are
  278. passed to be in host byte order.
  279. The input and output arguments of pcre16_utf16_to_host_byte_order() may
  280. point to the same address, that is, conversion in place is supported.
  281. The output buffer must be at least as long as the input.
  282. The length argument specifies the number of 16-bit data units in the
  283. input string; a negative value specifies a zero-terminated string.
  284. If byte_order is NULL, it is assumed that the string starts off in host
  285. byte order. This may be changed by byte-order marks (BOMs) anywhere in
  286. the string (commonly as the first character).
  287. If byte_order is not NULL, a non-zero value of the integer to which it
  288. points means that the input starts off in host byte order, otherwise
  289. the opposite order is assumed. Again, BOMs in the string can change
  290. this. The final byte order is passed back at the end of processing.
  291. If keep_boms is not zero, byte-order mark characters (0xfeff) are
  292. copied into the output string. Otherwise they are discarded.
  293. The result of the function is the number of 16-bit units placed into
  294. the output buffer, including the zero terminator if the string was
  295. zero-terminated.
  296. SUBJECT STRING OFFSETS
  297. The lengths and starting offsets of subject strings must be specified
  298. in 16-bit data units, and the offsets within subject strings that are
  299. returned by the matching functions are in also 16-bit units rather than
  300. bytes.
  301. NAMED SUBPATTERNS
  302. The name-to-number translation table that is maintained for named sub-
  303. patterns uses 16-bit characters. The pcre16_get_stringtable_entries()
  304. function returns the length of each entry in the table as the number of
  305. 16-bit data units.
  306. OPTION NAMES
  307. There are two new general option names, PCRE_UTF16 and
  308. PCRE_NO_UTF16_CHECK, which correspond to PCRE_UTF8 and
  309. PCRE_NO_UTF8_CHECK in the 8-bit library. In fact, these new options
  310. define the same bits in the options word. There is a discussion about
  311. the validity of UTF-16 strings in the pcreunicode page.
  312. For the pcre16_config() function there is an option PCRE_CONFIG_UTF16
  313. that returns 1 if UTF-16 support is configured, otherwise 0. If this
  314. option is given to pcre_config() or pcre32_config(), or if the
  315. PCRE_CONFIG_UTF8 or PCRE_CONFIG_UTF32 option is given to pcre16_con-
  316. fig(), the result is the PCRE_ERROR_BADOPTION error.
  317. CHARACTER CODES
  318. In 16-bit mode, when PCRE_UTF16 is not set, character values are
  319. treated in the same way as in 8-bit, non UTF-8 mode, except, of course,
  320. that they can range from 0 to 0xffff instead of 0 to 0xff. Character
  321. types for characters less than 0xff can therefore be influenced by the
  322. locale in the same way as before. Characters greater than 0xff have
  323. only one case, and no "type" (such as letter or digit).
  324. In UTF-16 mode, the character code is Unicode, in the range 0 to
  325. 0x10ffff, with the exception of values in the range 0xd800 to 0xdfff
  326. because those are "surrogate" values that are used in pairs to encode
  327. values greater than 0xffff.
  328. A UTF-16 string can indicate its endianness by special code knows as a
  329. byte-order mark (BOM). The PCRE functions do not handle this, expecting
  330. strings to be in host byte order. A utility function called
  331. pcre16_utf16_to_host_byte_order() is provided to help with this (see
  332. above).
  333. ERROR NAMES
  334. The errors PCRE_ERROR_BADUTF16_OFFSET and PCRE_ERROR_SHORTUTF16 corre-
  335. spond to their 8-bit counterparts. The error PCRE_ERROR_BADMODE is
  336. given when a compiled pattern is passed to a function that processes
  337. patterns in the other mode, for example, if a pattern compiled with
  338. pcre_compile() is passed to pcre16_exec().
  339. There are new error codes whose names begin with PCRE_UTF16_ERR for
  340. invalid UTF-16 strings, corresponding to the PCRE_UTF8_ERR codes for
  341. UTF-8 strings that are described in the section entitled "Reason codes
  342. for invalid UTF-8 strings" in the main pcreapi page. The UTF-16 errors
  343. are:
  344. PCRE_UTF16_ERR1 Missing low surrogate at end of string
  345. PCRE_UTF16_ERR2 Invalid low surrogate follows high surrogate
  346. PCRE_UTF16_ERR3 Isolated low surrogate
  347. PCRE_UTF16_ERR4 Non-character
  348. ERROR TEXTS
  349. If there is an error while compiling a pattern, the error text that is
  350. passed back by pcre16_compile() or pcre16_compile2() is still an 8-bit
  351. character string, zero-terminated.
  352. CALLOUTS
  353. The subject and mark fields in the callout block that is passed to a
  354. callout function point to 16-bit vectors.
  355. TESTING
  356. The pcretest program continues to operate with 8-bit input and output
  357. files, but it can be used for testing the 16-bit library. If it is run
  358. with the command line option -16, patterns and subject strings are con-
  359. verted from 8-bit to 16-bit before being passed to PCRE, and the 16-bit
  360. library functions are used instead of the 8-bit ones. Returned 16-bit
  361. strings are converted to 8-bit for output. If both the 8-bit and the
  362. 32-bit libraries were not compiled, pcretest defaults to 16-bit and the
  363. -16 option is ignored.
  364. When PCRE is being built, the RunTest script that is called by "make
  365. check" uses the pcretest -C option to discover which of the 8-bit,
  366. 16-bit and 32-bit libraries has been built, and runs the tests appro-
  367. priately.
  368. NOT SUPPORTED IN 16-BIT MODE
  369. Not all the features of the 8-bit library are available with the 16-bit
  370. library. The C++ and POSIX wrapper functions support only the 8-bit
  371. library, and the pcregrep program is at present 8-bit only.
  372. AUTHOR
  373. Philip Hazel
  374. University Computing Service
  375. Cambridge CB2 3QH, England.
  376. REVISION
  377. Last updated: 12 May 2013
  378. Copyright (c) 1997-2013 University of Cambridge.
  379. ------------------------------------------------------------------------------
  380. PCRE(3) Library Functions Manual PCRE(3)
  381. NAME
  382. PCRE - Perl-compatible regular expressions
  383. #include <pcre.h>
  384. PCRE 32-BIT API BASIC FUNCTIONS
  385. pcre32 *pcre32_compile(PCRE_SPTR32 pattern, int options,
  386. const char **errptr, int *erroffset,
  387. const unsigned char *tableptr);
  388. pcre32 *pcre32_compile2(PCRE_SPTR32 pattern, int options,
  389. int *errorcodeptr,
  390. const unsigned char *tableptr);
  391. pcre32_extra *pcre32_study(const pcre32 *code, int options,
  392. const char **errptr);
  393. void pcre32_free_study(pcre32_extra *extra);
  394. int pcre32_exec(const pcre32 *code, const pcre32_extra *extra,
  395. PCRE_SPTR32 subject, int length, int startoffset,
  396. int options, int *ovector, int ovecsize);
  397. int pcre32_dfa_exec(const pcre32 *code, const pcre32_extra *extra,
  398. PCRE_SPTR32 subject, int length, int startoffset,
  399. int options, int *ovector, int ovecsize,
  400. int *workspace, int wscount);
  401. PCRE 32-BIT API STRING EXTRACTION FUNCTIONS
  402. int pcre32_copy_named_substring(const pcre32 *code,
  403. PCRE_SPTR32 subject, int *ovector,
  404. int stringcount, PCRE_SPTR32 stringname,
  405. PCRE_UCHAR32 *buffer, int buffersize);
  406. int pcre32_copy_substring(PCRE_SPTR32 subject, int *ovector,
  407. int stringcount, int stringnumber, PCRE_UCHAR32 *buffer,
  408. int buffersize);
  409. int pcre32_get_named_substring(const pcre32 *code,
  410. PCRE_SPTR32 subject, int *ovector,
  411. int stringcount, PCRE_SPTR32 stringname,
  412. PCRE_SPTR32 *stringptr);
  413. int pcre32_get_stringnumber(const pcre32 *code,
  414. PCRE_SPTR32 name);
  415. int pcre32_get_stringtable_entries(const pcre32 *code,
  416. PCRE_SPTR32 name, PCRE_UCHAR32 **first, PCRE_UCHAR32 **last);
  417. int pcre32_get_substring(PCRE_SPTR32 subject, int *ovector,
  418. int stringcount, int stringnumber,
  419. PCRE_SPTR32 *stringptr);
  420. int pcre32_get_substring_list(PCRE_SPTR32 subject,
  421. int *ovector, int stringcount, PCRE_SPTR32 **listptr);
  422. void pcre32_free_substring(PCRE_SPTR32 stringptr);
  423. void pcre32_free_substring_list(PCRE_SPTR32 *stringptr);
  424. PCRE 32-BIT API AUXILIARY FUNCTIONS
  425. pcre32_jit_stack *pcre32_jit_stack_alloc(int startsize, int maxsize);
  426. void pcre32_jit_stack_free(pcre32_jit_stack *stack);
  427. void pcre32_assign_jit_stack(pcre32_extra *extra,
  428. pcre32_jit_callback callback, void *data);
  429. const unsigned char *pcre32_maketables(void);
  430. int pcre32_fullinfo(const pcre32 *code, const pcre32_extra *extra,
  431. int what, void *where);
  432. int pcre32_refcount(pcre32 *code, int adjust);
  433. int pcre32_config(int what, void *where);
  434. const char *pcre32_version(void);
  435. int pcre32_pattern_to_host_byte_order(pcre32 *code,
  436. pcre32_extra *extra, const unsigned char *tables);
  437. PCRE 32-BIT API INDIRECTED FUNCTIONS
  438. void *(*pcre32_malloc)(size_t);
  439. void (*pcre32_free)(void *);
  440. void *(*pcre32_stack_malloc)(size_t);
  441. void (*pcre32_stack_free)(void *);
  442. int (*pcre32_callout)(pcre32_callout_block *);
  443. PCRE 32-BIT API 32-BIT-ONLY FUNCTION
  444. int pcre32_utf32_to_host_byte_order(PCRE_UCHAR32 *output,
  445. PCRE_SPTR32 input, int length, int *byte_order,
  446. int keep_boms);
  447. THE PCRE 32-BIT LIBRARY
  448. Starting with release 8.32, it is possible to compile a PCRE library
  449. that supports 32-bit character strings, including UTF-32 strings, as
  450. well as or instead of the original 8-bit library. This work was done by
  451. Christian Persch, based on the work done by Zoltan Herczeg for the
  452. 16-bit library. All three libraries contain identical sets of func-
  453. tions, used in exactly the same way. Only the names of the functions
  454. and the data types of their arguments and results are different. To
  455. avoid over-complication and reduce the documentation maintenance load,
  456. most of the PCRE documentation describes the 8-bit library, with only
  457. occasional references to the 16-bit and 32-bit libraries. This page
  458. describes what is different when you use the 32-bit library.
  459. WARNING: A single application can be linked with all or any of the
  460. three libraries, but you must take care when processing any particular
  461. pattern to use functions from just one library. For example, if you
  462. want to study a pattern that was compiled with pcre32_compile(), you
  463. must do so with pcre32_study(), not pcre_study(), and you must free the
  464. study data with pcre32_free_study().
  465. THE HEADER FILE
  466. There is only one header file, pcre.h. It contains prototypes for all
  467. the functions in all libraries, as well as definitions of flags, struc-
  468. tures, error codes, etc.
  469. THE LIBRARY NAME
  470. In Unix-like systems, the 32-bit library is called libpcre32, and can
  471. normally be accesss by adding -lpcre32 to the command for linking an
  472. application that uses PCRE.
  473. STRING TYPES
  474. In the 8-bit library, strings are passed to PCRE library functions as
  475. vectors of bytes with the C type "char *". In the 32-bit library,
  476. strings are passed as vectors of unsigned 32-bit quantities. The macro
  477. PCRE_UCHAR32 specifies an appropriate data type, and PCRE_SPTR32 is
  478. defined as "const PCRE_UCHAR32 *". In very many environments, "unsigned
  479. int" is a 32-bit data type. When PCRE is built, it defines PCRE_UCHAR32
  480. as "unsigned int", but checks that it really is a 32-bit data type. If
  481. it is not, the build fails with an error message telling the maintainer
  482. to modify the definition appropriately.
  483. STRUCTURE TYPES
  484. The types of the opaque structures that are used for compiled 32-bit
  485. patterns and JIT stacks are pcre32 and pcre32_jit_stack respectively.
  486. The type of the user-accessible structure that is returned by
  487. pcre32_study() is pcre32_extra, and the type of the structure that is
  488. used for passing data to a callout function is pcre32_callout_block.
  489. These structures contain the same fields, with the same names, as their
  490. 8-bit counterparts. The only difference is that pointers to character
  491. strings are 32-bit instead of 8-bit types.
  492. 32-BIT FUNCTIONS
  493. For every function in the 8-bit library there is a corresponding func-
  494. tion in the 32-bit library with a name that starts with pcre32_ instead
  495. of pcre_. The prototypes are listed above. In addition, there is one
  496. extra function, pcre32_utf32_to_host_byte_order(). This is a utility
  497. function that converts a UTF-32 character string to host byte order if
  498. necessary. The other 32-bit functions expect the strings they are
  499. passed to be in host byte order.
  500. The input and output arguments of pcre32_utf32_to_host_byte_order() may
  501. point to the same address, that is, conversion in place is supported.
  502. The output buffer must be at least as long as the input.
  503. The length argument specifies the number of 32-bit data units in the
  504. input string; a negative value specifies a zero-terminated string.
  505. If byte_order is NULL, it is assumed that the string starts off in host
  506. byte order. This may be changed by byte-order marks (BOMs) anywhere in
  507. the string (commonly as the first character).
  508. If byte_order is not NULL, a non-zero value of the integer to which it
  509. points means that the input starts off in host byte order, otherwise
  510. the opposite order is assumed. Again, BOMs in the string can change
  511. this. The final byte order is passed back at the end of processing.
  512. If keep_boms is not zero, byte-order mark characters (0xfeff) are
  513. copied into the output string. Otherwise they are discarded.
  514. The result of the function is the number of 32-bit units placed into
  515. the output buffer, including the zero terminator if the string was
  516. zero-terminated.
  517. SUBJECT STRING OFFSETS
  518. The lengths and starting offsets of subject strings must be specified
  519. in 32-bit data units, and the offsets within subject strings that are
  520. returned by the matching functions are in also 32-bit units rather than
  521. bytes.
  522. NAMED SUBPATTERNS
  523. The name-to-number translation table that is maintained for named sub-
  524. patterns uses 32-bit characters. The pcre32_get_stringtable_entries()
  525. function returns the length of each entry in the table as the number of
  526. 32-bit data units.
  527. OPTION NAMES
  528. There are two new general option names, PCRE_UTF32 and
  529. PCRE_NO_UTF32_CHECK, which correspond to PCRE_UTF8 and
  530. PCRE_NO_UTF8_CHECK in the 8-bit library. In fact, these new options
  531. define the same bits in the options word. There is a discussion about
  532. the validity of UTF-32 strings in the pcreunicode page.
  533. For the pcre32_config() function there is an option PCRE_CONFIG_UTF32
  534. that returns 1 if UTF-32 support is configured, otherwise 0. If this
  535. option is given to pcre_config() or pcre16_config(), or if the
  536. PCRE_CONFIG_UTF8 or PCRE_CONFIG_UTF16 option is given to pcre32_con-
  537. fig(), the result is the PCRE_ERROR_BADOPTION error.
  538. CHARACTER CODES
  539. In 32-bit mode, when PCRE_UTF32 is not set, character values are
  540. treated in the same way as in 8-bit, non UTF-8 mode, except, of course,
  541. that they can range from 0 to 0x7fffffff instead of 0 to 0xff. Charac-
  542. ter types for characters less than 0xff can therefore be influenced by
  543. the locale in the same way as before. Characters greater than 0xff
  544. have only one case, and no "type" (such as letter or digit).
  545. In UTF-32 mode, the character code is Unicode, in the range 0 to
  546. 0x10ffff, with the exception of values in the range 0xd800 to 0xdfff
  547. because those are "surrogate" values that are ill-formed in UTF-32.
  548. A UTF-32 string can indicate its endianness by special code knows as a
  549. byte-order mark (BOM). The PCRE functions do not handle this, expecting
  550. strings to be in host byte order. A utility function called
  551. pcre32_utf32_to_host_byte_order() is provided to help with this (see
  552. above).
  553. ERROR NAMES
  554. The error PCRE_ERROR_BADUTF32 corresponds to its 8-bit counterpart.
  555. The error PCRE_ERROR_BADMODE is given when a compiled pattern is passed
  556. to a function that processes patterns in the other mode, for example,
  557. if a pattern compiled with pcre_compile() is passed to pcre32_exec().
  558. There are new error codes whose names begin with PCRE_UTF32_ERR for
  559. invalid UTF-32 strings, corresponding to the PCRE_UTF8_ERR codes for
  560. UTF-8 strings that are described in the section entitled "Reason codes
  561. for invalid UTF-8 strings" in the main pcreapi page. The UTF-32 errors
  562. are:
  563. PCRE_UTF32_ERR1 Surrogate character (range from 0xd800 to 0xdfff)
  564. PCRE_UTF32_ERR2 Non-character
  565. PCRE_UTF32_ERR3 Character > 0x10ffff
  566. ERROR TEXTS
  567. If there is an error while compiling a pattern, the error text that is
  568. passed back by pcre32_compile() or pcre32_compile2() is still an 8-bit
  569. character string, zero-terminated.
  570. CALLOUTS
  571. The subject and mark fields in the callout block that is passed to a
  572. callout function point to 32-bit vectors.
  573. TESTING
  574. The pcretest program continues to operate with 8-bit input and output
  575. files, but it can be used for testing the 32-bit library. If it is run
  576. with the command line option -32, patterns and subject strings are con-
  577. verted from 8-bit to 32-bit before being passed to PCRE, and the 32-bit
  578. library functions are used instead of the 8-bit ones. Returned 32-bit
  579. strings are converted to 8-bit for output. If both the 8-bit and the
  580. 16-bit libraries were not compiled, pcretest defaults to 32-bit and the
  581. -32 option is ignored.
  582. When PCRE is being built, the RunTest script that is called by "make
  583. check" uses the pcretest -C option to discover which of the 8-bit,
  584. 16-bit and 32-bit libraries has been built, and runs the tests appro-
  585. priately.
  586. NOT SUPPORTED IN 32-BIT MODE
  587. Not all the features of the 8-bit library are available with the 32-bit
  588. library. The C++ and POSIX wrapper functions support only the 8-bit
  589. library, and the pcregrep program is at present 8-bit only.
  590. AUTHOR
  591. Philip Hazel
  592. University Computing Service
  593. Cambridge CB2 3QH, England.
  594. REVISION
  595. Last updated: 12 May 2013
  596. Copyright (c) 1997-2013 University of Cambridge.
  597. ------------------------------------------------------------------------------
  598. PCREBUILD(3) Library Functions Manual PCREBUILD(3)
  599. NAME
  600. PCRE - Perl-compatible regular expressions
  601. BUILDING PCRE
  602. PCRE is distributed with a configure script that can be used to build
  603. the library in Unix-like environments using the applications known as
  604. Autotools. Also in the distribution are files to support building
  605. using CMake instead of configure. The text file README contains general
  606. information about building with Autotools (some of which is repeated
  607. below), and also has some comments about building on various operating
  608. systems. There is a lot more information about building PCRE without
  609. using Autotools (including information about using CMake and building
  610. "by hand") in the text file called NON-AUTOTOOLS-BUILD. You should
  611. consult this file as well as the README file if you are building in a
  612. non-Unix-like environment.
  613. PCRE BUILD-TIME OPTIONS
  614. The rest of this document describes the optional features of PCRE that
  615. can be selected when the library is compiled. It assumes use of the
  616. configure script, where the optional features are selected or dese-
  617. lected by providing options to configure before running the make com-
  618. mand. However, the same options can be selected in both Unix-like and
  619. non-Unix-like environments using the GUI facility of cmake-gui if you
  620. are using CMake instead of configure to build PCRE.
  621. If you are not using Autotools or CMake, option selection can be done
  622. by editing the config.h file, or by passing parameter settings to the
  623. compiler, as described in NON-AUTOTOOLS-BUILD.
  624. The complete list of options for configure (which includes the standard
  625. ones such as the selection of the installation directory) can be
  626. obtained by running
  627. ./configure --help
  628. The following sections include descriptions of options whose names
  629. begin with --enable or --disable. These settings specify changes to the
  630. defaults for the configure command. Because of the way that configure
  631. works, --enable and --disable always come in pairs, so the complemen-
  632. tary option always exists as well, but as it specifies the default, it
  633. is not described.
  634. BUILDING 8-BIT, 16-BIT AND 32-BIT LIBRARIES
  635. By default, a library called libpcre is built, containing functions
  636. that take string arguments contained in vectors of bytes, either as
  637. single-byte characters, or interpreted as UTF-8 strings. You can also
  638. build a separate library, called libpcre16, in which strings are con-
  639. tained in vectors of 16-bit data units and interpreted either as sin-
  640. gle-unit characters or UTF-16 strings, by adding
  641. --enable-pcre16
  642. to the configure command. You can also build yet another separate
  643. library, called libpcre32, in which strings are contained in vectors of
  644. 32-bit data units and interpreted either as single-unit characters or
  645. UTF-32 strings, by adding
  646. --enable-pcre32
  647. to the configure command. If you do not want the 8-bit library, add
  648. --disable-pcre8
  649. as well. At least one of the three libraries must be built. Note that
  650. the C++ and POSIX wrappers are for the 8-bit library only, and that
  651. pcregrep is an 8-bit program. None of these are built if you select
  652. only the 16-bit or 32-bit libraries.
  653. BUILDING SHARED AND STATIC LIBRARIES
  654. The Autotools PCRE building process uses libtool to build both shared
  655. and static libraries by default. You can suppress one of these by
  656. adding one of
  657. --disable-shared
  658. --disable-static
  659. to the configure command, as required.
  660. C++ SUPPORT
  661. By default, if the 8-bit library is being built, the configure script
  662. will search for a C++ compiler and C++ header files. If it finds them,
  663. it automatically builds the C++ wrapper library (which supports only
  664. 8-bit strings). You can disable this by adding
  665. --disable-cpp
  666. to the configure command.
  667. UTF-8, UTF-16 AND UTF-32 SUPPORT
  668. To build PCRE with support for UTF Unicode character strings, add
  669. --enable-utf
  670. to the configure command. This setting applies to all three libraries,
  671. adding support for UTF-8 to the 8-bit library, support for UTF-16 to
  672. the 16-bit library, and support for UTF-32 to the to the 32-bit
  673. library. There are no separate options for enabling UTF-8, UTF-16 and
  674. UTF-32 independently because that would allow ridiculous settings such
  675. as requesting UTF-16 support while building only the 8-bit library. It
  676. is not possible to build one library with UTF support and another with-
  677. out in the same configuration. (For backwards compatibility, --enable-
  678. utf8 is a synonym of --enable-utf.)
  679. Of itself, this setting does not make PCRE treat strings as UTF-8,
  680. UTF-16 or UTF-32. As well as compiling PCRE with this option, you also
  681. have have to set the PCRE_UTF8, PCRE_UTF16 or PCRE_UTF32 option (as
  682. appropriate) when you call one of the pattern compiling functions.
  683. If you set --enable-utf when compiling in an EBCDIC environment, PCRE
  684. expects its input to be either ASCII or UTF-8 (depending on the run-
  685. time option). It is not possible to support both EBCDIC and UTF-8 codes
  686. in the same version of the library. Consequently, --enable-utf and
  687. --enable-ebcdic are mutually exclusive.
  688. UNICODE CHARACTER PROPERTY SUPPORT
  689. UTF support allows the libraries to process character codepoints up to
  690. 0x10ffff in the strings that they handle. On its own, however, it does
  691. not provide any facilities for accessing the properties of such charac-
  692. ters. If you want to be able to use the pattern escapes \P, \p, and \X,
  693. which refer to Unicode character properties, you must add
  694. --enable-unicode-properties
  695. to the configure command. This implies UTF support, even if you have
  696. not explicitly requested it.
  697. Including Unicode property support adds around 30K of tables to the
  698. PCRE library. Only the general category properties such as Lu and Nd
  699. are supported. Details are given in the pcrepattern documentation.
  700. JUST-IN-TIME COMPILER SUPPORT
  701. Just-in-time compiler support is included in the build by specifying
  702. --enable-jit
  703. This support is available only for certain hardware architectures. If
  704. this option is set for an unsupported architecture, a compile time
  705. error occurs. See the pcrejit documentation for a discussion of JIT
  706. usage. When JIT support is enabled, pcregrep automatically makes use of
  707. it, unless you add
  708. --disable-pcregrep-jit
  709. to the "configure" command.
  710. CODE VALUE OF NEWLINE
  711. By default, PCRE interprets the linefeed (LF) character as indicating
  712. the end of a line. This is the normal newline character on Unix-like
  713. systems. You can compile PCRE to use carriage return (CR) instead, by
  714. adding
  715. --enable-newline-is-cr
  716. to the configure command. There is also a --enable-newline-is-lf
  717. option, which explicitly specifies linefeed as the newline character.
  718. Alternatively, you can specify that line endings are to be indicated by
  719. the two character sequence CRLF. If you want this, add
  720. --enable-newline-is-crlf
  721. to the configure command. There is a fourth option, specified by
  722. --enable-newline-is-anycrlf
  723. which causes PCRE to recognize any of the three sequences CR, LF, or
  724. CRLF as indicating a line ending. Finally, a fifth option, specified by
  725. --enable-newline-is-any
  726. causes PCRE to recognize any Unicode newline sequence.
  727. Whatever line ending convention is selected when PCRE is built can be
  728. overridden when the library functions are called. At build time it is
  729. conventional to use the standard for your operating system.
  730. WHAT \R MATCHES
  731. By default, the sequence \R in a pattern matches any Unicode newline
  732. sequence, whatever has been selected as the line ending sequence. If
  733. you specify
  734. --enable-bsr-anycrlf
  735. the default is changed so that \R matches only CR, LF, or CRLF. What-
  736. ever is selected when PCRE is built can be overridden when the library
  737. functions are called.
  738. POSIX MALLOC USAGE
  739. When the 8-bit library is called through the POSIX interface (see the
  740. pcreposix documentation), additional working storage is required for
  741. holding the pointers to capturing substrings, because PCRE requires
  742. three integers per substring, whereas the POSIX interface provides only
  743. two. If the number of expected substrings is small, the wrapper func-
  744. tion uses space on the stack, because this is faster than using mal-
  745. loc() for each call. The default threshold above which the stack is no
  746. longer used is 10; it can be changed by adding a setting such as
  747. --with-posix-malloc-threshold=20
  748. to the configure command.
  749. HANDLING VERY LARGE PATTERNS
  750. Within a compiled pattern, offset values are used to point from one
  751. part to another (for example, from an opening parenthesis to an alter-
  752. nation metacharacter). By default, in the 8-bit and 16-bit libraries,
  753. two-byte values are used for these offsets, leading to a maximum size
  754. for a compiled pattern of around 64K. This is sufficient to handle all
  755. but the most gigantic patterns. Nevertheless, some people do want to
  756. process truly enormous patterns, so it is possible to compile PCRE to
  757. use three-byte or four-byte offsets by adding a setting such as
  758. --with-link-size=3
  759. to the configure command. The value given must be 2, 3, or 4. For the
  760. 16-bit library, a value of 3 is rounded up to 4. In these libraries,
  761. using longer offsets slows down the operation of PCRE because it has to
  762. load additional data when handling them. For the 32-bit library the
  763. value is always 4 and cannot be overridden; the value of --with-link-
  764. size is ignored.
  765. AVOIDING EXCESSIVE STACK USAGE
  766. When matching with the pcre_exec() function, PCRE implements backtrack-
  767. ing by making recursive calls to an internal function called match().
  768. In environments where the size of the stack is limited, this can se-
  769. verely limit PCRE's operation. (The Unix environment does not usually
  770. suffer from this problem, but it may sometimes be necessary to increase
  771. the maximum stack size. There is a discussion in the pcrestack docu-
  772. mentation.) An alternative approach to recursion that uses memory from
  773. the heap to remember data, instead of using recursive function calls,
  774. has been implemented to work round the problem of limited stack size.
  775. If you want to build a version of PCRE that works this way, add
  776. --disable-stack-for-recursion
  777. to the configure command. With this configuration, PCRE will use the
  778. pcre_stack_malloc and pcre_stack_free variables to call memory manage-
  779. ment functions. By default these point to malloc() and free(), but you
  780. can replace the pointers so that your own functions are used instead.
  781. Separate functions are provided rather than using pcre_malloc and
  782. pcre_free because the usage is very predictable: the block sizes
  783. requested are always the same, and the blocks are always freed in
  784. reverse order. A calling program might be able to implement optimized
  785. functions that perform better than malloc() and free(). PCRE runs
  786. noticeably more slowly when built in this way. This option affects only
  787. the pcre_exec() function; it is not relevant for pcre_dfa_exec().
  788. LIMITING PCRE RESOURCE USAGE
  789. Internally, PCRE has a function called match(), which it calls repeat-
  790. edly (sometimes recursively) when matching a pattern with the
  791. pcre_exec() function. By controlling the maximum number of times this
  792. function may be called during a single matching operation, a limit can
  793. be placed on the resources used by a single call to pcre_exec(). The
  794. limit can be changed at run time, as described in the pcreapi documen-
  795. tation. The default is 10 million, but this can be changed by adding a
  796. setting such as
  797. --with-match-limit=500000
  798. to the configure command. This setting has no effect on the
  799. pcre_dfa_exec() matching function.
  800. In some environments it is desirable to limit the depth of recursive
  801. calls of match() more strictly than the total number of calls, in order
  802. to restrict the maximum amount of stack (or heap, if --disable-stack-
  803. for-recursion is specified) that is used. A second limit controls this;
  804. it defaults to the value that is set for --with-match-limit, which
  805. imposes no additional constraints. However, you can set a lower limit
  806. by adding, for example,
  807. --with-match-limit-recursion=10000
  808. to the configure command. This value can also be overridden at run
  809. time.
  810. CREATING CHARACTER TABLES AT BUILD TIME
  811. PCRE uses fixed tables for processing characters whose code values are
  812. less than 256. By default, PCRE is built with a set of tables that are
  813. distributed in the file pcre_chartables.c.dist. These tables are for
  814. ASCII codes only. If you add
  815. --enable-rebuild-chartables
  816. to the configure command, the distributed tables are no longer used.
  817. Instead, a program called dftables is compiled and run. This outputs
  818. the source for new set of tables, created in the default locale of your
  819. C run-time system. (This method of replacing the tables does not work
  820. if you are cross compiling, because dftables is run on the local host.
  821. If you need to create alternative tables when cross compiling, you will
  822. have to do so "by hand".)
  823. USING EBCDIC CODE
  824. PCRE assumes by default that it will run in an environment where the
  825. character code is ASCII (or Unicode, which is a superset of ASCII).
  826. This is the case for most computer operating systems. PCRE can, how-
  827. ever, be compiled to run in an EBCDIC environment by adding
  828. --enable-ebcdic
  829. to the configure command. This setting implies --enable-rebuild-charta-
  830. bles. You should only use it if you know that you are in an EBCDIC
  831. environment (for example, an IBM mainframe operating system). The
  832. --enable-ebcdic option is incompatible with --enable-utf.
  833. The EBCDIC character that corresponds to an ASCII LF is assumed to have
  834. the value 0x15 by default. However, in some EBCDIC environments, 0x25
  835. is used. In such an environment you should use
  836. --enable-ebcdic-nl25
  837. as well as, or instead of, --enable-ebcdic. The EBCDIC character for CR
  838. has the same value as in ASCII, namely, 0x0d. Whichever of 0x15 and
  839. 0x25 is not chosen as LF is made to correspond to the Unicode NEL char-
  840. acter (which, in Unicode, is 0x85).
  841. The options that select newline behaviour, such as --enable-newline-is-
  842. cr, and equivalent run-time options, refer to these character values in
  843. an EBCDIC environment.
  844. PCREGREP OPTIONS FOR COMPRESSED FILE SUPPORT
  845. By default, pcregrep reads all files as plain text. You can build it so
  846. that it recognizes files whose names end in .gz or .bz2, and reads them
  847. with libz or libbz2, respectively, by adding one or both of
  848. --enable-pcregrep-libz
  849. --enable-pcregrep-libbz2
  850. to the configure command. These options naturally require that the rel-
  851. evant libraries are installed on your system. Configuration will fail
  852. if they are not.
  853. PCREGREP BUFFER SIZE
  854. pcregrep uses an internal buffer to hold a "window" on the file it is
  855. scanning, in order to be able to output "before" and "after" lines when
  856. it finds a match. The size of the buffer is controlled by a parameter
  857. whose default value is 20K. The buffer itself is three times this size,
  858. but because of the way it is used for holding "before" lines, the long-
  859. est line that is guaranteed to be processable is the parameter size.
  860. You can change the default parameter value by adding, for example,
  861. --with-pcregrep-bufsize=50K
  862. to the configure command. The caller of pcregrep can, however, override
  863. this value by specifying a run-time option.
  864. PCRETEST OPTION FOR LIBREADLINE SUPPORT
  865. If you add
  866. --enable-pcretest-libreadline
  867. to the configure command, pcretest is linked with the libreadline
  868. library, and when its input is from a terminal, it reads it using the
  869. readline() function. This provides line-editing and history facilities.
  870. Note that libreadline is GPL-licensed, so if you distribute a binary of
  871. pcretest linked in this way, there may be licensing issues.
  872. Setting this option causes the -lreadline option to be added to the
  873. pcretest build. In many operating environments with a system-installed
  874. libreadline this is sufficient. However, in some environments (e.g. if
  875. an unmodified distribution version of readline is in use), some extra
  876. configuration may be necessary. The INSTALL file for libreadline says
  877. this:
  878. "Readline uses the termcap functions, but does not link with the
  879. termcap or curses library itself, allowing applications which link
  880. with readline the to choose an appropriate library."
  881. If your environment has not been set up so that an appropriate library
  882. is automatically included, you may need to add something like
  883. LIBS="-ncurses"
  884. immediately before the configure command.
  885. DEBUGGING WITH VALGRIND SUPPORT
  886. By adding the
  887. --enable-valgrind
  888. option to to the configure command, PCRE will use valgrind annotations
  889. to mark certain memory regions as unaddressable. This allows it to
  890. detect invalid memory accesses, and is mostly useful for debugging PCRE
  891. itself.
  892. CODE COVERAGE REPORTING
  893. If your C compiler is gcc, you can build a version of PCRE that can
  894. generate a code coverage report for its test suite. To enable this, you
  895. must install lcov version 1.6 or above. Then specify
  896. --enable-coverage
  897. to the configure command and build PCRE in the usual way.
  898. Note that using ccache (a caching C compiler) is incompatible with code
  899. coverage reporting. If you have configured ccache to run automatically
  900. on your system, you must set the environment variable
  901. CCACHE_DISABLE=1
  902. before running make to build PCRE, so that ccache is not used.
  903. When --enable-coverage is used, the following addition targets are
  904. added to the Makefile:
  905. make coverage
  906. This creates a fresh coverage report for the PCRE test suite. It is
  907. equivalent to running "make coverage-reset", "make coverage-baseline",
  908. "make check", and then "make coverage-report".
  909. make coverage-reset
  910. This zeroes the coverage counters, but does nothing else.
  911. make coverage-baseline
  912. This captures baseline coverage information.
  913. make coverage-report
  914. This creates the coverage report.
  915. make coverage-clean-report
  916. This removes the generated coverage report without cleaning the cover-
  917. age data itself.
  918. make coverage-clean-data
  919. This removes the captured coverage data without removing the coverage
  920. files created at compile time (*.gcno).
  921. make coverage-clean
  922. This cleans all coverage data including the generated coverage report.
  923. For more information about code coverage, see the gcov and lcov docu-
  924. mentation.
  925. SEE ALSO
  926. pcreapi(3), pcre16, pcre32, pcre_config(3).
  927. AUTHOR
  928. Philip Hazel
  929. University Computing Service
  930. Cambridge CB2 3QH, England.
  931. REVISION
  932. Last updated: 12 May 2013
  933. Copyright (c) 1997-2013 University of Cambridge.
  934. ------------------------------------------------------------------------------
  935. PCREMATCHING(3) Library Functions Manual PCREMATCHING(3)
  936. NAME
  937. PCRE - Perl-compatible regular expressions
  938. PCRE MATCHING ALGORITHMS
  939. This document describes the two different algorithms that are available
  940. in PCRE for matching a compiled regular expression against a given sub-
  941. ject string. The "standard" algorithm is the one provided by the
  942. pcre_exec(), pcre16_exec() and pcre32_exec() functions. These work in
  943. the same as as Perl's matching function, and provide a Perl-compatible
  944. matching operation. The just-in-time (JIT) optimization that is
  945. described in the pcrejit documentation is compatible with these func-
  946. tions.
  947. An alternative algorithm is provided by the pcre_dfa_exec(),
  948. pcre16_dfa_exec() and pcre32_dfa_exec() functions; they operate in a
  949. different way, and are not Perl-compatible. This alternative has advan-
  950. tages and disadvantages compared with the standard algorithm, and these
  951. are described below.
  952. When there is only one possible way in which a given subject string can
  953. match a pattern, the two algorithms give the same answer. A difference
  954. arises, however, when there are multiple possibilities. For example, if
  955. the pattern
  956. ^<.*>
  957. is matched against the string
  958. <something> <something else> <something further>
  959. there are three possible answers. The standard algorithm finds only one
  960. of them, whereas the alternative algorithm finds all three.
  961. REGULAR EXPRESSIONS AS TREES
  962. The set of strings that are matched by a regular expression can be rep-
  963. resented as a tree structure. An unlimited repetition in the pattern
  964. makes the tree of infinite size, but it is still a tree. Matching the
  965. pattern to a given subject string (from a given starting point) can be
  966. thought of as a search of the tree. There are two ways to search a
  967. tree: depth-first and breadth-first, and these correspond to the two
  968. matching algorithms provided by PCRE.
  969. THE STANDARD MATCHING ALGORITHM
  970. In the terminology of Jeffrey Friedl's book "Mastering Regular Expres-
  971. sions", the standard algorithm is an "NFA algorithm". It conducts a
  972. depth-first search of the pattern tree. That is, it proceeds along a
  973. single path through the tree, checking that the subject matches what is
  974. required. When there is a mismatch, the algorithm tries any alterna-
  975. tives at the current point, and if they all fail, it backs up to the
  976. previous branch point in the tree, and tries the next alternative
  977. branch at that level. This often involves backing up (moving to the
  978. left) in the subject string as well. The order in which repetition
  979. branches are tried is controlled by the greedy or ungreedy nature of
  980. the quantifier.
  981. If a leaf node is reached, a matching string has been found, and at
  982. that point the algorithm stops. Thus, if there is more than one possi-
  983. ble match, this algorithm returns the first one that it finds. Whether
  984. this is the shortest, the longest, or some intermediate length depends
  985. on the way the greedy and ungreedy repetition quantifiers are specified
  986. in the pattern.
  987. Because it ends up with a single path through the tree, it is rela-
  988. tively straightforward for this algorithm to keep track of the sub-
  989. strings that are matched by portions of the pattern in parentheses.
  990. This provides support for capturing parentheses and back references.
  991. THE ALTERNATIVE MATCHING ALGORITHM
  992. This algorithm conducts a breadth-first search of the tree. Starting
  993. from the first matching point in the subject, it scans the subject
  994. string from left to right, once, character by character, and as it does
  995. this, it remembers all the paths through the tree that represent valid
  996. matches. In Friedl's terminology, this is a kind of "DFA algorithm",
  997. though it is not implemented as a traditional finite state machine (it
  998. keeps multiple states active simultaneously).
  999. Although the general principle of this matching algorithm is that it
  1000. scans the subject string only once, without backtracking, there is one
  1001. exception: when a lookaround assertion is encountered, the characters
  1002. following or preceding the current point have to be independently
  1003. inspected.
  1004. The scan continues until either the end of the subject is reached, or
  1005. there are no more unterminated paths. At this point, terminated paths
  1006. represent the different matching possibilities (if there are none, the
  1007. match has failed). Thus, if there is more than one possible match,
  1008. this algorithm finds all of them, and in particular, it finds the long-
  1009. est. The matches are returned in decreasing order of length. There is
  1010. an option to stop the algorithm after the first match (which is neces-
  1011. sarily the shortest) is found.
  1012. Note that all the matches that are found start at the same point in the
  1013. subject. If the pattern
  1014. cat(er(pillar)?)?
  1015. is matched against the string "the caterpillar catchment", the result
  1016. will be the three strings "caterpillar", "cater", and "cat" that start
  1017. at the fifth character of the subject. The algorithm does not automati-
  1018. cally move on to find matches that start at later positions.
  1019. PCRE's "auto-possessification" optimization usually applies to charac-
  1020. ter repeats at the end of a pattern (as well as internally). For exam-
  1021. ple, the pattern "a\d+" is compiled as if it were "a\d++" because there
  1022. is no point even considering the possibility of backtracking into the
  1023. repeated digits. For DFA matching, this means that only one possible
  1024. match is found. If you really do want multiple matches in such cases,
  1025. either use an ungreedy repeat ("a\d+?") or set the PCRE_NO_AUTO_POSSESS
  1026. option when compiling.
  1027. There are a number of features of PCRE regular expressions that are not
  1028. supported by the alternative matching algorithm. They are as follows:
  1029. 1. Because the algorithm finds all possible matches, the greedy or
  1030. ungreedy nature of repetition quantifiers is not relevant. Greedy and
  1031. ungreedy quantifiers are treated in exactly the same way. However, pos-
  1032. sessive quantifiers can make a difference when what follows could also
  1033. match what is quantified, for example in a pattern like this:
  1034. ^a++\w!
  1035. This pattern matches "aaab!" but not "aaa!", which would be matched by
  1036. a non-possessive quantifier. Similarly, if an atomic group is present,
  1037. it is matched as if it were a standalone pattern at the current point,
  1038. and the longest match is then "locked in" for the rest of the overall
  1039. pattern.
  1040. 2. When dealing with multiple paths through the tree simultaneously, it
  1041. is not straightforward to keep track of captured substrings for the
  1042. different matching possibilities, and PCRE's implementation of this
  1043. algorithm does not attempt to do this. This means that no captured sub-
  1044. strings are available.
  1045. 3. Because no substrings are captured, back references within the pat-
  1046. tern are not supported, and cause errors if encountered.
  1047. 4. For the same reason, conditional expressions that use a backrefer-
  1048. ence as the condition or test for a specific group recursion are not
  1049. supported.
  1050. 5. Because many paths through the tree may be active, the \K escape
  1051. sequence, which resets the start of the match when encountered (but may
  1052. be on some paths and not on others), is not supported. It causes an
  1053. error if encountered.
  1054. 6. Callouts are supported, but the value of the capture_top field is
  1055. always 1, and the value of the capture_last field is always -1.
  1056. 7. The \C escape sequence, which (in the standard algorithm) always
  1057. matches a single data unit, even in UTF-8, UTF-16 or UTF-32 modes, is
  1058. not supported in these modes, because the alternative algorithm moves
  1059. through the subject string one character (not data unit) at a time, for
  1060. all active paths through the tree.
  1061. 8. Except for (*FAIL), the backtracking control verbs such as (*PRUNE)
  1062. are not supported. (*FAIL) is supported, and behaves like a failing
  1063. negative assertion.
  1064. ADVANTAGES OF THE ALTERNATIVE ALGORITHM
  1065. Using the alternative matching algorithm provides the following advan-
  1066. tages:
  1067. 1. All possible matches (at a single point in the subject) are automat-
  1068. ically found, and in particular, the longest match is found. To find
  1069. more than one match using the standard algorithm, you have to do kludgy
  1070. things with callouts.
  1071. 2. Because the alternative algorithm scans the subject string just
  1072. once, and never needs to backtrack (except for lookbehinds), it is pos-
  1073. sible to pass very long subject strings to the matching function in
  1074. several pieces, checking for partial matching each time. Although it is
  1075. possible to do multi-segment matching using the standard algorithm by
  1076. retaining partially matched substrings, it is more complicated. The
  1077. pcrepartial documentation gives details of partial matching and dis-
  1078. cusses multi-segment matching.
  1079. DISADVANTAGES OF THE ALTERNATIVE ALGORITHM
  1080. The alternative algorithm suffers from a number of disadvantages:
  1081. 1. It is substantially slower than the standard algorithm. This is
  1082. partly because it has to search for all possible matches, but is also
  1083. because it is less susceptible to optimization.
  1084. 2. Capturing parentheses and back references are not supported.
  1085. 3. Although atomic groups are supported, their use does not provide the
  1086. performance advantage that it does for the standard algorithm.
  1087. AUTHOR
  1088. Philip Hazel
  1089. University Computing Service
  1090. Cambridge CB2 3QH, England.
  1091. REVISION
  1092. Last updated: 12 November 2013
  1093. Copyright (c) 1997-2012 University of Cambridge.
  1094. ------------------------------------------------------------------------------
  1095. PCREAPI(3) Library Functions Manual PCREAPI(3)
  1096. NAME
  1097. PCRE - Perl-compatible regular expressions
  1098. #include <pcre.h>
  1099. PCRE NATIVE API BASIC FUNCTIONS
  1100. pcre *pcre_compile(const char *pattern, int options,
  1101. const char **errptr, int *erroffset,
  1102. const unsigned char *tableptr);
  1103. pcre *pcre_compile2(const char *pattern, int options,
  1104. int *errorcodeptr,
  1105. const char **errptr, int *erroffset,
  1106. const unsigned char *tableptr);
  1107. pcre_extra *pcre_study(const pcre *code, int options,
  1108. const char **errptr);
  1109. void pcre_free_study(pcre_extra *extra);
  1110. int pcre_exec(const pcre *code, const pcre_extra *extra,
  1111. const char *subject, int length, int startoffset,
  1112. int options, int *ovector, int ovecsize);
  1113. int pcre_dfa_exec(const pcre *code, const pcre_extra *extra,
  1114. const char *subject, int length, int startoffset,
  1115. int options, int *ovector, int ovecsize,
  1116. int *workspace, int wscount);
  1117. PCRE NATIVE API STRING EXTRACTION FUNCTIONS
  1118. int pcre_copy_named_substring(const pcre *code,
  1119. const char *subject, int *ovector,
  1120. int stringcount, const char *stringname,
  1121. char *buffer, int buffersize);
  1122. int pcre_copy_substring(const char *subject, int *ovector,
  1123. int stringcount, int stringnumber, char *buffer,
  1124. int buffersize);
  1125. int pcre_get_named_substring(const pcre *code,
  1126. const char *subject, int *ovector,
  1127. int stringcount, const char *stringname,
  1128. const char **stringptr);
  1129. int pcre_get_stringnumber(const pcre *code,
  1130. const char *name);
  1131. int pcre_get_stringtable_entries(const pcre *code,
  1132. const char *name, char **first, char **last);
  1133. int pcre_get_substring(const char *subject, int *ovector,
  1134. int stringcount, int stringnumber,
  1135. const char **stringptr);
  1136. int pcre_get_substring_list(const char *subject,
  1137. int *ovector, int stringcount, const char ***listptr);
  1138. void pcre_free_substring(const char *stringptr);
  1139. void pcre_free_substring_list(const char **stringptr);
  1140. PCRE NATIVE API AUXILIARY FUNCTIONS
  1141. int pcre_jit_exec(const pcre *code, const pcre_extra *extra,
  1142. const char *subject, int length, int startoffset,
  1143. int options, int *ovector, int ovecsize,
  1144. pcre_jit_stack *jstack);
  1145. pcre_jit_stack *pcre_jit_stack_alloc(int startsize, int maxsize);
  1146. void pcre_jit_stack_free(pcre_jit_stack *stack);
  1147. void pcre_assign_jit_stack(pcre_extra *extra,
  1148. pcre_jit_callback callback, void *data);
  1149. const unsigned char *pcre_maketables(void);
  1150. int pcre_fullinfo(const pcre *code, const pcre_extra *extra,
  1151. int what, void *where);
  1152. int pcre_refcount(pcre *code, int adjust);
  1153. int pcre_config(int what, void *where);
  1154. const char *pcre_version(void);
  1155. int pcre_pattern_to_host_byte_order(pcre *code,
  1156. pcre_extra *extra, const unsigned char *tables);
  1157. PCRE NATIVE API INDIRECTED FUNCTIONS
  1158. void *(*pcre_malloc)(size_t);
  1159. void (*pcre_free)(void *);
  1160. void *(*pcre_stack_malloc)(size_t);
  1161. void (*pcre_stack_free)(void *);
  1162. int (*pcre_callout)(pcre_callout_block *);
  1163. int (*pcre_stack_guard)(void);
  1164. PCRE 8-BIT, 16-BIT, AND 32-BIT LIBRARIES
  1165. As well as support for 8-bit character strings, PCRE also supports
  1166. 16-bit strings (from release 8.30) and 32-bit strings (from release
  1167. 8.32), by means of two additional libraries. They can be built as well
  1168. as, or instead of, the 8-bit library. To avoid too much complication,
  1169. this document describes the 8-bit versions of the functions, with only
  1170. occasional references to the 16-bit and 32-bit libraries.
  1171. The 16-bit and 32-bit functions operate in the same way as their 8-bit
  1172. counterparts; they just use different data types for their arguments
  1173. and results, and their names start with pcre16_ or pcre32_ instead of
  1174. pcre_. For every option that has UTF8 in its name (for example,
  1175. PCRE_UTF8), there are corresponding 16-bit and 32-bit names with UTF8
  1176. replaced by UTF16 or UTF32, respectively. This facility is in fact just
  1177. cosmetic; the 16-bit and 32-bit option names define the same bit val-
  1178. ues.
  1179. References to bytes and UTF-8 in this document should be read as refer-
  1180. ences to 16-bit data units and UTF-16 when using the 16-bit library, or
  1181. 32-bit data units and UTF-32 when using the 32-bit library, unless
  1182. specified otherwise. More details of the specific differences for the
  1183. 16-bit and 32-bit libraries are given in the pcre16 and pcre32 pages.
  1184. PCRE API OVERVIEW
  1185. PCRE has its own native API, which is described in this document. There
  1186. are also some wrapper functions (for the 8-bit library only) that cor-
  1187. respond to the POSIX regular expression API, but they do not give
  1188. access to all the functionality. They are described in the pcreposix
  1189. documentation. Both of these APIs define a set of C function calls. A
  1190. C++ wrapper (again for the 8-bit library only) is also distributed with
  1191. PCRE. It is documented in the pcrecpp page.
  1192. The native API C function prototypes are defined in the header file
  1193. pcre.h, and on Unix-like systems the (8-bit) library itself is called
  1194. libpcre. It can normally be accessed by adding -lpcre to the command
  1195. for linking an application that uses PCRE. The header file defines the
  1196. macros PCRE_MAJOR and PCRE_MINOR to contain the major and minor release
  1197. numbers for the library. Applications can use these to include support
  1198. for different releases of PCRE.
  1199. In a Windows environment, if you want to statically link an application
  1200. program against a non-dll pcre.a file, you must define PCRE_STATIC
  1201. before including pcre.h or pcrecpp.h, because otherwise the pcre_mal-
  1202. loc() and pcre_free() exported functions will be declared
  1203. __declspec(dllimport), with unwanted results.
  1204. The functions pcre_compile(), pcre_compile2(), pcre_study(), and
  1205. pcre_exec() are used for compiling and matching regular expressions in
  1206. a Perl-compatible manner. A sample program that demonstrates the sim-
  1207. plest way of using them is provided in the file called pcredemo.c in
  1208. the PCRE source distribution. A listing of this program is given in the
  1209. pcredemo documentation, and the pcresample documentation describes how
  1210. to compile and run it.
  1211. Just-in-time compiler support is an optional feature of PCRE that can
  1212. be built in appropriate hardware environments. It greatly speeds up the
  1213. matching performance of many patterns. Simple programs can easily
  1214. request that it be used if available, by setting an option that is
  1215. ignored when it is not relevant. More complicated programs might need
  1216. to make use of the functions pcre_jit_stack_alloc(),
  1217. pcre_jit_stack_free(), and pcre_assign_jit_stack() in order to control
  1218. the JIT code's memory usage.
  1219. From release 8.32 there is also a direct interface for JIT execution,
  1220. which gives improved performance. The JIT-specific functions are dis-
  1221. cussed in the pcrejit documentation.
  1222. A second matching function, pcre_dfa_exec(), which is not Perl-compati-
  1223. ble, is also provided. This uses a different algorithm for the match-
  1224. ing. The alternative algorithm finds all possible matches (at a given
  1225. point in the subject), and scans the subject just once (unless there
  1226. are lookbehind assertions). However, this algorithm does not return
  1227. captured substrings. A description of the two matching algorithms and
  1228. their advantages and disadvantages is given in the pcrematching docu-
  1229. mentation.
  1230. In addition to the main compiling and matching functions, there are
  1231. convenience functions for extracting captured substrings from a subject
  1232. string that is matched by pcre_exec(). They are:
  1233. pcre_copy_substring()
  1234. pcre_copy_named_substring()
  1235. pcre_get_substring()
  1236. pcre_get_named_substring()
  1237. pcre_get_substring_list()
  1238. pcre_get_stringnumber()
  1239. pcre_get_stringtable_entries()
  1240. pcre_free_substring() and pcre_free_substring_list() are also provided,
  1241. to free the memory used for extracted strings.
  1242. The function pcre_maketables() is used to build a set of character
  1243. tables in the current locale for passing to pcre_compile(),
  1244. pcre_exec(), or pcre_dfa_exec(). This is an optional facility that is
  1245. provided for specialist use. Most commonly, no special tables are
  1246. passed, in which case internal tables that are generated when PCRE is
  1247. built are used.
  1248. The function pcre_fullinfo() is used to find out information about a
  1249. compiled pattern. The function pcre_version() returns a pointer to a
  1250. string containing the version of PCRE and its date of release.
  1251. The function pcre_refcount() maintains a reference count in a data
  1252. block containing a compiled pattern. This is provided for the benefit
  1253. of object-oriented applications.
  1254. The global variables pcre_malloc and pcre_free initially contain the
  1255. entry points of the standard malloc() and free() functions, respec-
  1256. tively. PCRE calls the memory management functions via these variables,
  1257. so a calling program can replace them if it wishes to intercept the
  1258. calls. This should be done before calling any PCRE functions.
  1259. The global variables pcre_stack_malloc and pcre_stack_free are also
  1260. indirections to memory management functions. These special functions
  1261. are used only when PCRE is compiled to use the heap for remembering
  1262. data, instead of recursive function calls, when running the pcre_exec()
  1263. function. See the pcrebuild documentation for details of how to do
  1264. this. It is a non-standard way of building PCRE, for use in environ-
  1265. ments that have limited stacks. Because of the greater use of memory
  1266. management, it runs more slowly. Separate functions are provided so
  1267. that special-purpose external code can be used for this case. When
  1268. used, these functions are always called in a stack-like manner (last
  1269. obtained, first freed), and always for memory blocks of the same size.
  1270. There is a discussion about PCRE's stack usage in the pcrestack docu-
  1271. mentation.
  1272. The global variable pcre_callout initially contains NULL. It can be set
  1273. by the caller to a "callout" function, which PCRE will then call at
  1274. specified points during a matching operation. Details are given in the
  1275. pcrecallout documentation.
  1276. The global variable pcre_stack_guard initially contains NULL. It can be
  1277. set by the caller to a function that is called by PCRE whenever it
  1278. starts to compile a parenthesized part of a pattern. When parentheses
  1279. are nested, PCRE uses recursive function calls, which use up the system
  1280. stack. This function is provided so that applications with restricted
  1281. stacks can force a compilation error if the stack runs out. The func-
  1282. tion should return zero if all is well, or non-zero to force an error.
  1283. NEWLINES
  1284. PCRE supports five different conventions for indicating line breaks in
  1285. strings: a single CR (carriage return) character, a single LF (line-
  1286. feed) character, the two-character sequence CRLF, any of the three pre-
  1287. ceding, or any Unicode newline sequence. The Unicode newline sequences
  1288. are the three just mentioned, plus the single characters VT (vertical
  1289. tab, U+000B), FF (form feed, U+000C), NEL (next line, U+0085), LS (line
  1290. separator, U+2028), and PS (paragraph separator, U+2029).
  1291. Each of the first three conventions is used by at least one operating
  1292. system as its standard newline sequence. When PCRE is built, a default
  1293. can be specified. The default default is LF, which is the Unix stan-
  1294. dard. When PCRE is run, the default can be overridden, either when a
  1295. pattern is compiled, or when it is matched.
  1296. At compile time, the newline convention can be specified by the options
  1297. argument of pcre_compile(), or it can be specified by special text at
  1298. the start of the pattern itself; this overrides any other settings. See
  1299. the pcrepattern page for details of the special character sequences.
  1300. In the PCRE documentation the word "newline" is used to mean "the char-
  1301. acter or pair of characters that indicate a line break". The choice of
  1302. newline convention affects the handling of the dot, circumflex, and
  1303. dollar metacharacters, the handling of #-comments in /x mode, and, when
  1304. CRLF is a recognized line ending sequence, the match position advance-
  1305. ment for a non-anchored pattern. There is more detail about this in the
  1306. section on pcre_exec() options below.
  1307. The choice of newline convention does not affect the interpretation of
  1308. the \n or \r escape sequences, nor does it affect what \R matches,
  1309. which is controlled in a similar way, but by separate options.
  1310. MULTITHREADING
  1311. The PCRE functions can be used in multi-threading applications, with
  1312. the proviso that the memory management functions pointed to by
  1313. pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the
  1314. callout and stack-checking functions pointed to by pcre_callout and
  1315. pcre_stack_guard, are shared by all threads.
  1316. The compiled form of a regular expression is not altered during match-
  1317. ing, so the same compiled pattern can safely be used by several threads
  1318. at once.
  1319. If the just-in-time optimization feature is being used, it needs sepa-
  1320. rate memory stack areas for each thread. See the pcrejit documentation
  1321. for more details.
  1322. SAVING PRECOMPILED PATTERNS FOR LATER USE
  1323. The compiled form of a regular expression can be saved and re-used at a
  1324. later time, possibly by a different program, and even on a host other
  1325. than the one on which it was compiled. Details are given in the
  1326. pcreprecompile documentation, which includes a description of the
  1327. pcre_pattern_to_host_byte_order() function. However, compiling a regu-
  1328. lar expression with one version of PCRE for use with a different ver-
  1329. sion is not guaranteed to work and may cause crashes.
  1330. CHECKING BUILD-TIME OPTIONS
  1331. int pcre_config(int what, void *where);
  1332. The function pcre_config() makes it possible for a PCRE client to dis-
  1333. cover which optional features have been compiled into the PCRE library.
  1334. The pcrebuild documentation has more details about these optional fea-
  1335. tures.
  1336. The first argument for pcre_config() is an integer, specifying which
  1337. information is required; the second argument is a pointer to a variable
  1338. into which the information is placed. The returned value is zero on
  1339. success, or the negative error code PCRE_ERROR_BADOPTION if the value
  1340. in the first argument is not recognized. The following information is
  1341. available:
  1342. PCRE_CONFIG_UTF8
  1343. The output is an integer that is set to one if UTF-8 support is avail-
  1344. able; otherwise it is set to zero. This value should normally be given
  1345. to the 8-bit version of this function, pcre_config(). If it is given to
  1346. the 16-bit or 32-bit version of this function, the result is
  1347. PCRE_ERROR_BADOPTION.
  1348. PCRE_CONFIG_UTF16
  1349. The output is an integer that is set to one if UTF-16 support is avail-
  1350. able; otherwise it is set to zero. This value should normally be given
  1351. to the 16-bit version of this function, pcre16_config(). If it is given
  1352. to the 8-bit or 32-bit version of this function, the result is
  1353. PCRE_ERROR_BADOPTION.
  1354. PCRE_CONFIG_UTF32
  1355. The output is an integer that is set to one if UTF-32 support is avail-
  1356. able; otherwise it is set to zero. This value should normally be given
  1357. to the 32-bit version of this function, pcre32_config(). If it is given
  1358. to the 8-bit or 16-bit version of this function, the result is
  1359. PCRE_ERROR_BADOPTION.
  1360. PCRE_CONFIG_UNICODE_PROPERTIES
  1361. The output is an integer that is set to one if support for Unicode
  1362. character properties is available; otherwise it is set to zero.
  1363. PCRE_CONFIG_JIT
  1364. The output is an integer that is set to one if support for just-in-time
  1365. compiling is available; otherwise it is set to zero.
  1366. PCRE_CONFIG_JITTARGET
  1367. The output is a pointer to a zero-terminated "const char *" string. If
  1368. JIT support is available, the string contains the name of the architec-
  1369. ture for which the JIT compiler is configured, for example "x86 32bit
  1370. (little endian + unaligned)". If JIT support is not available, the
  1371. result is NULL.
  1372. PCRE_CONFIG_NEWLINE
  1373. The output is an integer whose value specifies the default character
  1374. sequence that is recognized as meaning "newline". The values that are
  1375. supported in ASCII/Unicode environments are: 10 for LF, 13 for CR, 3338
  1376. for CRLF, -2 for ANYCRLF, and -1 for ANY. In EBCDIC environments, CR,
  1377. ANYCRLF, and ANY yield the same values. However, the value for LF is
  1378. normally 21, though some EBCDIC environments use 37. The corresponding
  1379. values for CRLF are 3349 and 3365. The default should normally corre-
  1380. spond to the standard sequence for your operating system.
  1381. PCRE_CONFIG_BSR
  1382. The output is an integer whose value indicates what character sequences
  1383. the \R escape sequence matches by default. A value of 0 means that \R
  1384. matches any Unicode line ending sequence; a value of 1 means that \R
  1385. matches only CR, LF, or CRLF. The default can be overridden when a pat-
  1386. tern is compiled or matched.
  1387. PCRE_CONFIG_LINK_SIZE
  1388. The output is an integer that contains the number of bytes used for
  1389. internal linkage in compiled regular expressions. For the 8-bit
  1390. library, the value can be 2, 3, or 4. For the 16-bit library, the value
  1391. is either 2 or 4 and is still a number of bytes. For the 32-bit
  1392. library, the value is either 2 or 4 and is still a number of bytes. The
  1393. default value of 2 is sufficient for all but the most massive patterns,
  1394. since it allows the compiled pattern to be up to 64K in size. Larger
  1395. values allow larger regular expressions to be compiled, at the expense
  1396. of slower matching.
  1397. PCRE_CONFIG_POSIX_MALLOC_THRESHOLD
  1398. The output is an integer that contains the threshold above which the
  1399. POSIX interface uses malloc() for output vectors. Further details are
  1400. given in the pcreposix documentation.
  1401. PCRE_CONFIG_PARENS_LIMIT
  1402. The output is a long integer that gives the maximum depth of nesting of
  1403. parentheses (of any kind) in a pattern. This limit is imposed to cap
  1404. the amount of system stack used when a pattern is compiled. It is spec-
  1405. ified when PCRE is built; the default is 250. This limit does not take
  1406. into account the stack that may already be used by the calling applica-
  1407. tion. For finer control over compilation stack usage, you can set a
  1408. pointer to an external checking function in pcre_stack_guard.
  1409. PCRE_CONFIG_MATCH_LIMIT
  1410. The output is a long integer that gives the default limit for the num-
  1411. ber of internal matching function calls in a pcre_exec() execution.
  1412. Further details are given with pcre_exec() below.
  1413. PCRE_CONFIG_MATCH_LIMIT_RECURSION
  1414. The output is a long integer that gives the default limit for the depth
  1415. of recursion when calling the internal matching function in a
  1416. pcre_exec() execution. Further details are given with pcre_exec()
  1417. below.
  1418. PCRE_CONFIG_STACKRECURSE
  1419. The output is an integer that is set to one if internal recursion when
  1420. running pcre_exec() is implemented by recursive function calls that use
  1421. the stack to remember their state. This is the usual way that PCRE is
  1422. compiled. The output is zero if PCRE was compiled to use blocks of data
  1423. on the heap instead of recursive function calls. In this case,
  1424. pcre_stack_malloc and pcre_stack_free are called to manage memory
  1425. blocks on the heap, thus avoiding the use of the stack.
  1426. COMPILING A PATTERN
  1427. pcre *pcre_compile(const char *pattern, int options,
  1428. const char **errptr, int *erroffset,
  1429. const unsigned char *tableptr);
  1430. pcre *pcre_compile2(const char *pattern, int options,
  1431. int *errorcodeptr,
  1432. const char **errptr, int *erroffset,
  1433. const unsigned char *tableptr);
  1434. Either of the functions pcre_compile() or pcre_compile2() can be called
  1435. to compile a pattern into an internal form. The only difference between
  1436. the two interfaces is that pcre_compile2() has an additional argument,
  1437. errorcodeptr, via which a numerical error code can be returned. To
  1438. avoid too much repetition, we refer just to pcre_compile() below, but
  1439. the information applies equally to pcre_compile2().
  1440. The pattern is a C string terminated by a binary zero, and is passed in
  1441. the pattern argument. A pointer to a single block of memory that is
  1442. obtained via pcre_malloc is returned. This contains the compiled code
  1443. and related data. The pcre type is defined for the returned block; this
  1444. is a typedef for a structure whose contents are not externally defined.
  1445. It is up to the caller to free the memory (via pcre_free) when it is no
  1446. longer required.
  1447. Although the compiled code of a PCRE regex is relocatable, that is, it
  1448. does not depend on memory location, the complete pcre data block is not
  1449. fully relocatable, because it may contain a copy of the tableptr argu-
  1450. ment, which is an address (see below).
  1451. The options argument contains various bit settings that affect the com-
  1452. pilation. It should be zero if no options are required. The available
  1453. options are described below. Some of them (in particular, those that
  1454. are compatible with Perl, but some others as well) can also be set and
  1455. unset from within the pattern (see the detailed description in the
  1456. pcrepattern documentation). For those options that can be different in
  1457. different parts of the pattern, the contents of the options argument
  1458. specifies their settings at the start of compilation and execution. The
  1459. PCRE_ANCHORED, PCRE_BSR_xxx, PCRE_NEWLINE_xxx, PCRE_NO_UTF8_CHECK, and
  1460. PCRE_NO_START_OPTIMIZE options can be set at the time of matching as
  1461. well as at compile time.
  1462. If errptr is NULL, pcre_compile() returns NULL immediately. Otherwise,
  1463. if compilation of a pattern fails, pcre_compile() returns NULL, and
  1464. sets the variable pointed to by errptr to point to a textual error mes-
  1465. sage. This is a static string that is part of the library. You must not
  1466. try to free it. Normally, the offset from the start of the pattern to
  1467. the data unit that was being processed when the error was discovered is
  1468. placed in the variable pointed to by erroffset, which must not be NULL
  1469. (if it is, an immediate error is given). However, for an invalid UTF-8
  1470. or UTF-16 string, the offset is that of the first data unit of the
  1471. failing character.
  1472. Some errors are not detected until the whole pattern has been scanned;
  1473. in these cases, the offset passed back is the length of the pattern.
  1474. Note that the offset is in data units, not characters, even in a UTF
  1475. mode. It may sometimes point into the middle of a UTF-8 or UTF-16 char-
  1476. acter.
  1477. If pcre_compile2() is used instead of pcre_compile(), and the error-
  1478. codeptr argument is not NULL, a non-zero error code number is returned
  1479. via this argument in the event of an error. This is in addition to the
  1480. textual error message. Error codes and messages are listed below.
  1481. If the final argument, tableptr, is NULL, PCRE uses a default set of
  1482. character tables that are built when PCRE is compiled, using the
  1483. default C locale. Otherwise, tableptr must be an address that is the
  1484. result of a call to pcre_maketables(). This value is stored with the
  1485. compiled pattern, and used again by pcre_exec() and pcre_dfa_exec()
  1486. when the pattern is matched. For more discussion, see the section on
  1487. locale support below.
  1488. This code fragment shows a typical straightforward call to pcre_com-
  1489. pile():
  1490. pcre *re;
  1491. const char *error;
  1492. int erroffset;
  1493. re = pcre_compile(
  1494. "^A.*Z", /* the pattern */
  1495. 0, /* default options */
  1496. &error, /* for error message */
  1497. &erroffset, /* for error offset */
  1498. NULL); /* use default character tables */
  1499. The following names for option bits are defined in the pcre.h header
  1500. file:
  1501. PCRE_ANCHORED
  1502. If this bit is set, the pattern is forced to be "anchored", that is, it
  1503. is constrained to match only at the first matching point in the string
  1504. that is being searched (the "subject string"). This effect can also be
  1505. achieved by appropriate constructs in the pattern itself, which is the
  1506. only way to do it in Perl.
  1507. PCRE_AUTO_CALLOUT
  1508. If this bit is set, pcre_compile() automatically inserts callout items,
  1509. all with number 255, before each pattern item. For discussion of the
  1510. callout facility, see the pcrecallout documentation.
  1511. PCRE_BSR_ANYCRLF
  1512. PCRE_BSR_UNICODE
  1513. These options (which are mutually exclusive) control what the \R escape
  1514. sequence matches. The choice is either to match only CR, LF, or CRLF,
  1515. or to match any Unicode newline sequence. The default is specified when
  1516. PCRE is built. It can be overridden from within the pattern, or by set-
  1517. ting an option when a compiled pattern is matched.
  1518. PCRE_CASELESS
  1519. If this bit is set, letters in the pattern match both upper and lower
  1520. case letters. It is equivalent to Perl's /i option, and it can be
  1521. changed within a pattern by a (?i) option setting. In UTF-8 mode, PCRE
  1522. always understands the concept of case for characters whose values are
  1523. less than 128, so caseless matching is always possible. For characters
  1524. with higher values, the concept of case is supported if PCRE is com-
  1525. piled with Unicode property support, but not otherwise. If you want to
  1526. use caseless matching for characters 128 and above, you must ensure
  1527. that PCRE is compiled with Unicode property support as well as with
  1528. UTF-8 support.
  1529. PCRE_DOLLAR_ENDONLY
  1530. If this bit is set, a dollar metacharacter in the pattern matches only
  1531. at the end of the subject string. Without this option, a dollar also
  1532. matches immediately before a newline at the end of the string (but not
  1533. before any other newlines). The PCRE_DOLLAR_ENDONLY option is ignored
  1534. if PCRE_MULTILINE is set. There is no equivalent to this option in
  1535. Perl, and no way to set it within a pattern.
  1536. PCRE_DOTALL
  1537. If this bit is set, a dot metacharacter in the pattern matches a char-
  1538. acter of any value, including one that indicates a newline. However, it
  1539. only ever matches one character, even if newlines are coded as CRLF.
  1540. Without this option, a dot does not match when the current position is
  1541. at a newline. This option is equivalent to Perl's /s option, and it can
  1542. be changed within a pattern by a (?s) option setting. A negative class
  1543. such as [^a] always matches newline characters, independent of the set-
  1544. ting of this option.
  1545. PCRE_DUPNAMES
  1546. If this bit is set, names used to identify capturing subpatterns need
  1547. not be unique. This can be helpful for certain types of pattern when it
  1548. is known that only one instance of the named subpattern can ever be
  1549. matched. There are more details of named subpatterns below; see also
  1550. the pcrepattern documentation.
  1551. PCRE_EXTENDED
  1552. If this bit is set, most white space characters in the pattern are
  1553. totally ignored except when escaped or inside a character class. How-
  1554. ever, white space is not allowed within sequences such as (?> that
  1555. introduce various parenthesized subpatterns, nor within a numerical
  1556. quantifier such as {1,3}. However, ignorable white space is permitted
  1557. between an item and a following quantifier and between a quantifier and
  1558. a following + that indicates possessiveness.
  1559. White space did not used to include the VT character (code 11), because
  1560. Perl did not treat this character as white space. However, Perl changed
  1561. at release 5.18, so PCRE followed at release 8.34, and VT is now
  1562. treated as white space.
  1563. PCRE_EXTENDED also causes characters between an unescaped # outside a
  1564. character class and the next newline, inclusive, to be ignored.
  1565. PCRE_EXTENDED is equivalent to Perl's /x option, and it can be changed
  1566. within a pattern by a (?x) option setting.
  1567. Which characters are interpreted as newlines is controlled by the
  1568. options passed to pcre_compile() or by a special sequence at the start
  1569. of the pattern, as described in the section entitled "Newline conven-
  1570. tions" in the pcrepattern documentation. Note that the end of this type
  1571. of comment is a literal newline sequence in the pattern; escape
  1572. sequences that happen to represent a newline do not count.
  1573. This option makes it possible to include comments inside complicated
  1574. patterns. Note, however, that this applies only to data characters.
  1575. White space characters may never appear within special character
  1576. sequences in a pattern, for example within the sequence (?( that intro-
  1577. duces a conditional subpattern.
  1578. PCRE_EXTRA
  1579. This option was invented in order to turn on additional functionality
  1580. of PCRE that is incompatible with Perl, but it is currently of very
  1581. little use. When set, any backslash in a pattern that is followed by a
  1582. letter that has no special meaning causes an error, thus reserving
  1583. these combinations for future expansion. By default, as in Perl, a
  1584. backslash followed by a letter with no special meaning is treated as a
  1585. literal. (Perl can, however, be persuaded to give an error for this, by
  1586. running it with the -w option.) There are at present no other features
  1587. controlled by this option. It can also be set by a (?X) option setting
  1588. within a pattern.
  1589. PCRE_FIRSTLINE
  1590. If this option is set, an unanchored pattern is required to match
  1591. before or at the first newline in the subject string, though the
  1592. matched text may continue over the newline.
  1593. PCRE_JAVASCRIPT_COMPAT
  1594. If this option is set, PCRE's behaviour is changed in some ways so that
  1595. it is compatible with JavaScript rather than Perl. The changes are as
  1596. follows:
  1597. (1) A lone closing square bracket in a pattern causes a compile-time
  1598. error, because this is illegal in JavaScript (by default it is treated
  1599. as a data character). Thus, the pattern AB]CD becomes illegal when this
  1600. option is set.
  1601. (2) At run time, a back reference to an unset subpattern group matches
  1602. an empty string (by default this causes the current matching alterna-
  1603. tive to fail). A pattern such as (\1)(a) succeeds when this option is
  1604. set (assuming it can find an "a" in the subject), whereas it fails by
  1605. default, for Perl compatibility.
  1606. (3) \U matches an upper case "U" character; by default \U causes a com-
  1607. pile time error (Perl uses \U to upper case subsequent characters).
  1608. (4) \u matches a lower case "u" character unless it is followed by four
  1609. hexadecimal digits, in which case the hexadecimal number defines the
  1610. code point to match. By default, \u causes a compile time error (Perl
  1611. uses it to upper case the following character).
  1612. (5) \x matches a lower case "x" character unless it is followed by two
  1613. hexadecimal digits, in which case the hexadecimal number defines the
  1614. code point to match. By default, as in Perl, a hexadecimal number is
  1615. always expected after \x, but it may have zero, one, or two digits (so,
  1616. for example, \xz matches a binary zero character followed by z).
  1617. PCRE_MULTILINE
  1618. By default, for the purposes of matching "start of line" and "end of
  1619. line", PCRE treats the subject string as consisting of a single line of
  1620. characters, even if it actually contains newlines. The "start of line"
  1621. metacharacter (^) matches only at the start of the string, and the "end
  1622. of line" metacharacter ($) matches only at the end of the string, or
  1623. before a terminating newline (except when PCRE_DOLLAR_ENDONLY is set).
  1624. Note, however, that unless PCRE_DOTALL is set, the "any character"
  1625. metacharacter (.) does not match at a newline. This behaviour (for ^,
  1626. $, and dot) is the same as Perl.
  1627. When PCRE_MULTILINE it is set, the "start of line" and "end of line"
  1628. constructs match immediately following or immediately before internal
  1629. newlines in the subject string, respectively, as well as at the very
  1630. start and end. This is equivalent to Perl's /m option, and it can be
  1631. changed within a pattern by a (?m) option setting. If there are no new-
  1632. lines in a subject string, or no occurrences of ^ or $ in a pattern,
  1633. setting PCRE_MULTILINE has no effect.
  1634. PCRE_NEVER_UTF
  1635. This option locks out interpretation of the pattern as UTF-8 (or UTF-16
  1636. or UTF-32 in the 16-bit and 32-bit libraries). In particular, it pre-
  1637. vents the creator of the pattern from switching to UTF interpretation
  1638. by starting the pattern with (*UTF). This may be useful in applications
  1639. that process patterns from external sources. The combination of
  1640. PCRE_UTF8 and PCRE_NEVER_UTF also causes an error.
  1641. PCRE_NEWLINE_CR
  1642. PCRE_NEWLINE_LF
  1643. PCRE_NEWLINE_CRLF
  1644. PCRE_NEWLINE_ANYCRLF
  1645. PCRE_NEWLINE_ANY
  1646. These options override the default newline definition that was chosen
  1647. when PCRE was built. Setting the first or the second specifies that a
  1648. newline is indicated by a single character (CR or LF, respectively).
  1649. Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by the
  1650. two-character CRLF sequence. Setting PCRE_NEWLINE_ANYCRLF specifies
  1651. that any of the three preceding sequences should be recognized. Setting
  1652. PCRE_NEWLINE_ANY specifies that any Unicode newline sequence should be
  1653. recognized.
  1654. In an ASCII/Unicode environment, the Unicode newline sequences are the
  1655. three just mentioned, plus the single characters VT (vertical tab,
  1656. U+000B), FF (form feed, U+000C), NEL (next line, U+0085), LS (line sep-
  1657. arator, U+2028), and PS (paragraph separator, U+2029). For the 8-bit
  1658. library, the last two are recognized only in UTF-8 mode.
  1659. When PCRE is compiled to run in an EBCDIC (mainframe) environment, the
  1660. code for CR is 0x0d, the same as ASCII. However, the character code for
  1661. LF is normally 0x15, though in some EBCDIC environments 0x25 is used.
  1662. Whichever of these is not LF is made to correspond to Unicode's NEL
  1663. character. EBCDIC codes are all less than 256. For more details, see
  1664. the pcrebuild documentation.
  1665. The newline setting in the options word uses three bits that are
  1666. treated as a number, giving eight possibilities. Currently only six are
  1667. used (default plus the five values above). This means that if you set
  1668. more than one newline option, the combination may or may not be sensi-
  1669. ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to
  1670. PCRE_NEWLINE_CRLF, but other combinations may yield unused numbers and
  1671. cause an error.
  1672. The only time that a line break in a pattern is specially recognized
  1673. when compiling is when PCRE_EXTENDED is set. CR and LF are white space
  1674. characters, and so are ignored in this mode. Also, an unescaped # out-
  1675. side a character class indicates a comment that lasts until after the
  1676. next line break sequence. In other circumstances, line break sequences
  1677. in patterns are treated as literal data.
  1678. The newline option that is set at compile time becomes the default that
  1679. is used for pcre_exec() and pcre_dfa_exec(), but it can be overridden.
  1680. PCRE_NO_AUTO_CAPTURE
  1681. If this option is set, it disables the use of numbered capturing paren-
  1682. theses in the pattern. Any opening parenthesis that is not followed by
  1683. ? behaves as if it were followed by ?: but named parentheses can still
  1684. be used for capturing (and they acquire numbers in the usual way).
  1685. There is no equivalent of this option in Perl.
  1686. PCRE_NO_AUTO_POSSESS
  1687. If this option is set, it disables "auto-possessification". This is an
  1688. optimization that, for example, turns a+b into a++b in order to avoid
  1689. backtracks into a+ that can never be successful. However, if callouts
  1690. are in use, auto-possessification means that some of them are never
  1691. taken. You can set this option if you want the matching functions to do
  1692. a full unoptimized search and run all the callouts, but it is mainly
  1693. provided for testing purposes.
  1694. PCRE_NO_START_OPTIMIZE
  1695. This is an option that acts at matching time; that is, it is really an
  1696. option for pcre_exec() or pcre_dfa_exec(). If it is set at compile
  1697. time, it is remembered with the compiled pattern and assumed at match-
  1698. ing time. This is necessary if you want to use JIT execution, because
  1699. the JIT compiler needs to know whether or not this option is set. For
  1700. details see the discussion of PCRE_NO_START_OPTIMIZE below.
  1701. PCRE_UCP
  1702. This option changes the way PCRE processes \B, \b, \D, \d, \S, \s, \W,
  1703. \w, and some of the POSIX character classes. By default, only ASCII
  1704. characters are recognized, but if PCRE_UCP is set, Unicode properties
  1705. are used instead to classify characters. More details are given in the
  1706. section on generic character types in the pcrepattern page. If you set
  1707. PCRE_UCP, matching one of the items it affects takes much longer. The
  1708. option is available only if PCRE has been compiled with Unicode prop-
  1709. erty support.
  1710. PCRE_UNGREEDY
  1711. This option inverts the "greediness" of the quantifiers so that they
  1712. are not greedy by default, but become greedy if followed by "?". It is
  1713. not compatible with Perl. It can also be set by a (?U) option setting
  1714. within the pattern.
  1715. PCRE_UTF8
  1716. This option causes PCRE to regard both the pattern and the subject as
  1717. strings of UTF-8 characters instead of single-byte strings. However, it
  1718. is available only when PCRE is built to include UTF support. If not,
  1719. the use of this option provokes an error. Details of how this option
  1720. changes the behaviour of PCRE are given in the pcreunicode page.
  1721. PCRE_NO_UTF8_CHECK
  1722. When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is
  1723. automatically checked. There is a discussion about the validity of
  1724. UTF-8 strings in the pcreunicode page. If an invalid UTF-8 sequence is
  1725. found, pcre_compile() returns an error. If you already know that your
  1726. pattern is valid, and you want to skip this check for performance rea-
  1727. sons, you can set the PCRE_NO_UTF8_CHECK option. When it is set, the
  1728. effect of passing an invalid UTF-8 string as a pattern is undefined. It
  1729. may cause your program to crash or loop. Note that this option can also
  1730. be passed to pcre_exec() and pcre_dfa_exec(), to suppress the validity
  1731. checking of subject strings only. If the same string is being matched
  1732. many times, the option can be safely set for the second and subsequent
  1733. matchings to improve performance.
  1734. COMPILATION ERROR CODES
  1735. The following table lists the error codes than may be returned by
  1736. pcre_compile2(), along with the error messages that may be returned by
  1737. both compiling functions. Note that error messages are always 8-bit
  1738. ASCII strings, even in 16-bit or 32-bit mode. As PCRE has developed,
  1739. some error codes have fallen out of use. To avoid confusion, they have
  1740. not been re-used.
  1741. 0 no error
  1742. 1 \ at end of pattern
  1743. 2 \c at end of pattern
  1744. 3 unrecognized character follows \
  1745. 4 numbers out of order in {} quantifier
  1746. 5 number too big in {} quantifier
  1747. 6 missing terminating ] for character class
  1748. 7 invalid escape sequence in character class
  1749. 8 range out of order in character class
  1750. 9 nothing to repeat
  1751. 10 [this code is not in use]
  1752. 11 internal error: unexpected repeat
  1753. 12 unrecognized character after (? or (?-
  1754. 13 POSIX named classes are supported only within a class
  1755. 14 missing )
  1756. 15 reference to non-existent subpattern
  1757. 16 erroffset passed as NULL
  1758. 17 unknown option bit(s) set
  1759. 18 missing ) after comment
  1760. 19 [this code is not in use]
  1761. 20 regular expression is too large
  1762. 21 failed to get memory
  1763. 22 unmatched parentheses
  1764. 23 internal error: code overflow
  1765. 24 unrecognized character after (?<
  1766. 25 lookbehind assertion is not fixed length
  1767. 26 malformed number or name after (?(
  1768. 27 conditional group contains more than two branches
  1769. 28 assertion expected after (?(
  1770. 29 (?R or (?[+-]digits must be followed by )
  1771. 30 unknown POSIX class name
  1772. 31 POSIX collating elements are not supported
  1773. 32 this version of PCRE is compiled without UTF support
  1774. 33 [this code is not in use]
  1775. 34 character value in \x{} or \o{} is too large
  1776. 35 invalid condition (?(0)
  1777. 36 \C not allowed in lookbehind assertion
  1778. 37 PCRE does not support \L, \l, \N{name}, \U, or \u
  1779. 38 number after (?C is > 255
  1780. 39 closing ) for (?C expected
  1781. 40 recursive call could loop indefinitely
  1782. 41 unrecognized character after (?P
  1783. 42 syntax error in subpattern name (missing terminator)
  1784. 43 two named subpatterns have the same name
  1785. 44 invalid UTF-8 string (specifically UTF-8)
  1786. 45 support for \P, \p, and \X has not been compiled
  1787. 46 malformed \P or \p sequence
  1788. 47 unknown property name after \P or \p
  1789. 48 subpattern name is too long (maximum 32 characters)
  1790. 49 too many named subpatterns (maximum 10000)
  1791. 50 [this code is not in use]
  1792. 51 octal value is greater than \377 in 8-bit non-UTF-8 mode
  1793. 52 internal error: overran compiling workspace
  1794. 53 internal error: previously-checked referenced subpattern
  1795. not found
  1796. 54 DEFINE group contains more than one branch
  1797. 55 repeating a DEFINE group is not allowed
  1798. 56 inconsistent NEWLINE options
  1799. 57 \g is not followed by a braced, angle-bracketed, or quoted
  1800. name/number or by a plain number
  1801. 58 a numbered reference must not be zero
  1802. 59 an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)
  1803. 60 (*VERB) not recognized or malformed
  1804. 61 number is too big
  1805. 62 subpattern name expected
  1806. 63 digit expected after (?+
  1807. 64 ] is an invalid data character in JavaScript compatibility mode
  1808. 65 different names for subpatterns of the same number are
  1809. not allowed
  1810. 66 (*MARK) must have an argument
  1811. 67 this version of PCRE is not compiled with Unicode property
  1812. support
  1813. 68 \c must be followed by an ASCII character
  1814. 69 \k is not followed by a braced, angle-bracketed, or quoted name
  1815. 70 internal error: unknown opcode in find_fixedlength()
  1816. 71 \N is not supported in a class
  1817. 72 too many forward references
  1818. 73 disallowed Unicode code point (>= 0xd800 && <= 0xdfff)
  1819. 74 invalid UTF-16 string (specifically UTF-16)
  1820. 75 name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)
  1821. 76 character value in \u.... sequence is too large
  1822. 77 invalid UTF-32 string (specifically UTF-32)
  1823. 78 setting UTF is disabled by the application
  1824. 79 non-hex character in \x{} (closing brace missing?)
  1825. 80 non-octal character in \o{} (closing brace missing?)
  1826. 81 missing opening brace after \o
  1827. 82 parentheses are too deeply nested
  1828. 83 invalid range in character class
  1829. 84 group name must start with a non-digit
  1830. 85 parentheses are too deeply nested (stack check)
  1831. The numbers 32 and 10000 in errors 48 and 49 are defaults; different
  1832. values may be used if the limits were changed when PCRE was built.
  1833. STUDYING A PATTERN
  1834. pcre_extra *pcre_study(const pcre *code, int options,
  1835. const char **errptr);
  1836. If a compiled pattern is going to be used several times, it is worth
  1837. spending more time analyzing it in order to speed up the time taken for
  1838. matching. The function pcre_study() takes a pointer to a compiled pat-
  1839. tern as its first argument. If studying the pattern produces additional
  1840. information that will help speed up matching, pcre_study() returns a
  1841. pointer to a pcre_extra block, in which the study_data field points to
  1842. the results of the study.
  1843. The returned value from pcre_study() can be passed directly to
  1844. pcre_exec() or pcre_dfa_exec(). However, a pcre_extra block also con-
  1845. tains other fields that can be set by the caller before the block is
  1846. passed; these are described below in the section on matching a pattern.
  1847. If studying the pattern does not produce any useful information,
  1848. pcre_study() returns NULL by default. In that circumstance, if the
  1849. calling program wants to pass any of the other fields to pcre_exec() or
  1850. pcre_dfa_exec(), it must set up its own pcre_extra block. However, if
  1851. pcre_study() is called with the PCRE_STUDY_EXTRA_NEEDED option, it
  1852. returns a pcre_extra block even if studying did not find any additional
  1853. information. It may still return NULL, however, if an error occurs in
  1854. pcre_study().
  1855. The second argument of pcre_study() contains option bits. There are
  1856. three further options in addition to PCRE_STUDY_EXTRA_NEEDED:
  1857. PCRE_STUDY_JIT_COMPILE
  1858. PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
  1859. PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
  1860. If any of these are set, and the just-in-time compiler is available,
  1861. the pattern is further compiled into machine code that executes much
  1862. faster than the pcre_exec() interpretive matching function. If the
  1863. just-in-time compiler is not available, these options are ignored. All
  1864. undefined bits in the options argument must be zero.
  1865. JIT compilation is a heavyweight optimization. It can take some time
  1866. for patterns to be analyzed, and for one-off matches and simple pat-
  1867. terns the benefit of faster execution might be offset by a much slower
  1868. study time. Not all patterns can be optimized by the JIT compiler. For
  1869. those that cannot be handled, matching automatically falls back to the
  1870. pcre_exec() interpreter. For more details, see the pcrejit documenta-
  1871. tion.
  1872. The third argument for pcre_study() is a pointer for an error message.
  1873. If studying succeeds (even if no data is returned), the variable it
  1874. points to is set to NULL. Otherwise it is set to point to a textual
  1875. error message. This is a static string that is part of the library. You
  1876. must not try to free it. You should test the error pointer for NULL
  1877. after calling pcre_study(), to be sure that it has run successfully.
  1878. When you are finished with a pattern, you can free the memory used for
  1879. the study data by calling pcre_free_study(). This function was added to
  1880. the API for release 8.20. For earlier versions, the memory could be
  1881. freed with pcre_free(), just like the pattern itself. This will still
  1882. work in cases where JIT optimization is not used, but it is advisable
  1883. to change to the new function when convenient.
  1884. This is a typical way in which pcre_study() is used (except that in a
  1885. real application there should be tests for errors):
  1886. int rc;
  1887. pcre *re;
  1888. pcre_extra *sd;
  1889. re = pcre_compile("pattern", 0, &error, &erroroffset, NULL);
  1890. sd = pcre_study(
  1891. re, /* result of pcre_compile() */
  1892. 0, /* no options */
  1893. &error); /* set to NULL or points to a message */
  1894. rc = pcre_exec( /* see below for details of pcre_exec() options */
  1895. re, sd, "subject", 7, 0, 0, ovector, 30);
  1896. ...
  1897. pcre_free_study(sd);
  1898. pcre_free(re);
  1899. Studying a pattern does two things: first, a lower bound for the length
  1900. of subject string that is needed to match the pattern is computed. This
  1901. does not mean that there are any strings of that length that match, but
  1902. it does guarantee that no shorter strings match. The value is used to
  1903. avoid wasting time by trying to match strings that are shorter than the
  1904. lower bound. You can find out the value in a calling program via the
  1905. pcre_fullinfo() function.
  1906. Studying a pattern is also useful for non-anchored patterns that do not
  1907. have a single fixed starting character. A bitmap of possible starting
  1908. bytes is created. This speeds up finding a position in the subject at
  1909. which to start matching. (In 16-bit mode, the bitmap is used for 16-bit
  1910. values less than 256. In 32-bit mode, the bitmap is used for 32-bit
  1911. values less than 256.)
  1912. These two optimizations apply to both pcre_exec() and pcre_dfa_exec(),
  1913. and the information is also used by the JIT compiler. The optimiza-
  1914. tions can be disabled by setting the PCRE_NO_START_OPTIMIZE option.
  1915. You might want to do this if your pattern contains callouts or (*MARK)
  1916. and you want to make use of these facilities in cases where matching
  1917. fails.
  1918. PCRE_NO_START_OPTIMIZE can be specified at either compile time or exe-
  1919. cution time. However, if PCRE_NO_START_OPTIMIZE is passed to
  1920. pcre_exec(), (that is, after any JIT compilation has happened) JIT exe-
  1921. cution is disabled. For JIT execution to work with PCRE_NO_START_OPTI-
  1922. MIZE, the option must be set at compile time.
  1923. There is a longer discussion of PCRE_NO_START_OPTIMIZE below.
  1924. LOCALE SUPPORT
  1925. PCRE handles caseless matching, and determines whether characters are
  1926. letters, digits, or whatever, by reference to a set of tables, indexed
  1927. by character code point. When running in UTF-8 mode, or in the 16- or
  1928. 32-bit libraries, this applies only to characters with code points less
  1929. than 256. By default, higher-valued code points never match escapes
  1930. such as \w or \d. However, if PCRE is built with Unicode property sup-
  1931. port, all characters can be tested with \p and \P, or, alternatively,
  1932. the PCRE_UCP option can be set when a pattern is compiled; this causes
  1933. \w and friends to use Unicode property support instead of the built-in
  1934. tables.
  1935. The use of locales with Unicode is discouraged. If you are handling
  1936. characters with code points greater than 128, you should either use
  1937. Unicode support, or use locales, but not try to mix the two.
  1938. PCRE contains an internal set of tables that are used when the final
  1939. argument of pcre_compile() is NULL. These are sufficient for many
  1940. applications. Normally, the internal tables recognize only ASCII char-
  1941. acters. However, when PCRE is built, it is possible to cause the inter-
  1942. nal tables to be rebuilt in the default "C" locale of the local system,
  1943. which may cause them to be different.
  1944. The internal tables can always be overridden by tables supplied by the
  1945. application that calls PCRE. These may be created in a different locale
  1946. from the default. As more and more applications change to using Uni-
  1947. code, the need for this locale support is expected to die away.
  1948. External tables are built by calling the pcre_maketables() function,
  1949. which has no arguments, in the relevant locale. The result can then be
  1950. passed to pcre_compile() as often as necessary. For example, to build
  1951. and use tables that are appropriate for the French locale (where
  1952. accented characters with values greater than 128 are treated as let-
  1953. ters), the following code could be used:
  1954. setlocale(LC_CTYPE, "fr_FR");
  1955. tables = pcre_maketables();
  1956. re = pcre_compile(..., tables);
  1957. The locale name "fr_FR" is used on Linux and other Unix-like systems;
  1958. if you are using Windows, the name for the French locale is "french".
  1959. When pcre_maketables() runs, the tables are built in memory that is
  1960. obtained via pcre_malloc. It is the caller's responsibility to ensure
  1961. that the memory containing the tables remains available for as long as
  1962. it is needed.
  1963. The pointer that is passed to pcre_compile() is saved with the compiled
  1964. pattern, and the same tables are used via this pointer by pcre_study()
  1965. and also by pcre_exec() and pcre_dfa_exec(). Thus, for any single pat-
  1966. tern, compilation, studying and matching all happen in the same locale,
  1967. but different patterns can be processed in different locales.
  1968. It is possible to pass a table pointer or NULL (indicating the use of
  1969. the internal tables) to pcre_exec() or pcre_dfa_exec() (see the discus-
  1970. sion below in the section on matching a pattern). This facility is pro-
  1971. vided for use with pre-compiled patterns that have been saved and
  1972. reloaded. Character tables are not saved with patterns, so if a non-
  1973. standard table was used at compile time, it must be provided again when
  1974. the reloaded pattern is matched. Attempting to use this facility to
  1975. match a pattern in a different locale from the one in which it was com-
  1976. piled is likely to lead to anomalous (usually incorrect) results.
  1977. INFORMATION ABOUT A PATTERN
  1978. int pcre_fullinfo(const pcre *code, const pcre_extra *extra,
  1979. int what, void *where);
  1980. The pcre_fullinfo() function returns information about a compiled pat-
  1981. tern. It replaces the pcre_info() function, which was removed from the
  1982. library at version 8.30, after more than 10 years of obsolescence.
  1983. The first argument for pcre_fullinfo() is a pointer to the compiled
  1984. pattern. The second argument is the result of pcre_study(), or NULL if
  1985. the pattern was not studied. The third argument specifies which piece
  1986. of information is required, and the fourth argument is a pointer to a
  1987. variable to receive the data. The yield of the function is zero for
  1988. success, or one of the following negative numbers:
  1989. PCRE_ERROR_NULL the argument code was NULL
  1990. the argument where was NULL
  1991. PCRE_ERROR_BADMAGIC the "magic number" was not found
  1992. PCRE_ERROR_BADENDIANNESS the pattern was compiled with different
  1993. endianness
  1994. PCRE_ERROR_BADOPTION the value of what was invalid
  1995. PCRE_ERROR_UNSET the requested field is not set
  1996. The "magic number" is placed at the start of each compiled pattern as
  1997. an simple check against passing an arbitrary memory pointer. The endi-
  1998. anness error can occur if a compiled pattern is saved and reloaded on a
  1999. different host. Here is a typical call of pcre_fullinfo(), to obtain
  2000. the length of the compiled pattern:
  2001. int rc;
  2002. size_t length;
  2003. rc = pcre_fullinfo(
  2004. re, /* result of pcre_compile() */
  2005. sd, /* result of pcre_study(), or NULL */
  2006. PCRE_INFO_SIZE, /* what is required */
  2007. &length); /* where to put the data */
  2008. The possible values for the third argument are defined in pcre.h, and
  2009. are as follows:
  2010. PCRE_INFO_BACKREFMAX
  2011. Return the number of the highest back reference in the pattern. The
  2012. fourth argument should point to an int variable. Zero is returned if
  2013. there are no back references.
  2014. PCRE_INFO_CAPTURECOUNT
  2015. Return the number of capturing subpatterns in the pattern. The fourth
  2016. argument should point to an int variable.
  2017. PCRE_INFO_DEFAULT_TABLES
  2018. Return a pointer to the internal default character tables within PCRE.
  2019. The fourth argument should point to an unsigned char * variable. This
  2020. information call is provided for internal use by the pcre_study() func-
  2021. tion. External callers can cause PCRE to use its internal tables by
  2022. passing a NULL table pointer.
  2023. PCRE_INFO_FIRSTBYTE (deprecated)
  2024. Return information about the first data unit of any matched string, for
  2025. a non-anchored pattern. The name of this option refers to the 8-bit
  2026. library, where data units are bytes. The fourth argument should point
  2027. to an int variable. Negative values are used for special cases. How-
  2028. ever, this means that when the 32-bit library is in non-UTF-32 mode,
  2029. the full 32-bit range of characters cannot be returned. For this rea-
  2030. son, this value is deprecated; use PCRE_INFO_FIRSTCHARACTERFLAGS and
  2031. PCRE_INFO_FIRSTCHARACTER instead.
  2032. If there is a fixed first value, for example, the letter "c" from a
  2033. pattern such as (cat|cow|coyote), its value is returned. In the 8-bit
  2034. library, the value is always less than 256. In the 16-bit library the
  2035. value can be up to 0xffff. In the 32-bit library the value can be up to
  2036. 0x10ffff.
  2037. If there is no fixed first value, and if either
  2038. (a) the pattern was compiled with the PCRE_MULTILINE option, and every
  2039. branch starts with "^", or
  2040. (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not
  2041. set (if it were set, the pattern would be anchored),
  2042. -1 is returned, indicating that the pattern matches only at the start
  2043. of a subject string or after any newline within the string. Otherwise
  2044. -2 is returned. For anchored patterns, -2 is returned.
  2045. PCRE_INFO_FIRSTCHARACTER
  2046. Return the value of the first data unit (non-UTF character) of any
  2047. matched string in the situation where PCRE_INFO_FIRSTCHARACTERFLAGS
  2048. returns 1; otherwise return 0. The fourth argument should point to an
  2049. uint_t variable.
  2050. In the 8-bit library, the value is always less than 256. In the 16-bit
  2051. library the value can be up to 0xffff. In the 32-bit library in UTF-32
  2052. mode the value can be up to 0x10ffff, and up to 0xffffffff when not
  2053. using UTF-32 mode.
  2054. PCRE_INFO_FIRSTCHARACTERFLAGS
  2055. Return information about the first data unit of any matched string, for
  2056. a non-anchored pattern. The fourth argument should point to an int
  2057. variable.
  2058. If there is a fixed first value, for example, the letter "c" from a
  2059. pattern such as (cat|cow|coyote), 1 is returned, and the character
  2060. value can be retrieved using PCRE_INFO_FIRSTCHARACTER. If there is no
  2061. fixed first value, and if either
  2062. (a) the pattern was compiled with the PCRE_MULTILINE option, and every
  2063. branch starts with "^", or
  2064. (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not
  2065. set (if it were set, the pattern would be anchored),
  2066. 2 is returned, indicating that the pattern matches only at the start of
  2067. a subject string or after any newline within the string. Otherwise 0 is
  2068. returned. For anchored patterns, 0 is returned.
  2069. PCRE_INFO_FIRSTTABLE
  2070. If the pattern was studied, and this resulted in the construction of a
  2071. 256-bit table indicating a fixed set of values for the first data unit
  2072. in any matching string, a pointer to the table is returned. Otherwise
  2073. NULL is returned. The fourth argument should point to an unsigned char
  2074. * variable.
  2075. PCRE_INFO_HASCRORLF
  2076. Return 1 if the pattern contains any explicit matches for CR or LF
  2077. characters, otherwise 0. The fourth argument should point to an int
  2078. variable. An explicit match is either a literal CR or LF character, or
  2079. \r or \n.
  2080. PCRE_INFO_JCHANGED
  2081. Return 1 if the (?J) or (?-J) option setting is used in the pattern,
  2082. otherwise 0. The fourth argument should point to an int variable. (?J)
  2083. and (?-J) set and unset the local PCRE_DUPNAMES option, respectively.
  2084. PCRE_INFO_JIT
  2085. Return 1 if the pattern was studied with one of the JIT options, and
  2086. just-in-time compiling was successful. The fourth argument should point
  2087. to an int variable. A return value of 0 means that JIT support is not
  2088. available in this version of PCRE, or that the pattern was not studied
  2089. with a JIT option, or that the JIT compiler could not handle this par-
  2090. ticular pattern. See the pcrejit documentation for details of what can
  2091. and cannot be handled.
  2092. PCRE_INFO_JITSIZE
  2093. If the pattern was successfully studied with a JIT option, return the
  2094. size of the JIT compiled code, otherwise return zero. The fourth argu-
  2095. ment should point to a size_t variable.
  2096. PCRE_INFO_LASTLITERAL
  2097. Return the value of the rightmost literal data unit that must exist in
  2098. any matched string, other than at its start, if such a value has been
  2099. recorded. The fourth argument should point to an int variable. If there
  2100. is no such value, -1 is returned. For anchored patterns, a last literal
  2101. value is recorded only if it follows something of variable length. For
  2102. example, for the pattern /^a\d+z\d+/ the returned value is "z", but for
  2103. /^a\dz\d/ the returned value is -1.
  2104. Since for the 32-bit library using the non-UTF-32 mode, this function
  2105. is unable to return the full 32-bit range of characters, this value is
  2106. deprecated; instead the PCRE_INFO_REQUIREDCHARFLAGS and
  2107. PCRE_INFO_REQUIREDCHAR values should be used.
  2108. PCRE_INFO_MATCH_EMPTY
  2109. Return 1 if the pattern can match an empty string, otherwise 0. The
  2110. fourth argument should point to an int variable.
  2111. PCRE_INFO_MATCHLIMIT
  2112. If the pattern set a match limit by including an item of the form
  2113. (*LIMIT_MATCH=nnnn) at the start, the value is returned. The fourth
  2114. argument should point to an unsigned 32-bit integer. If no such value
  2115. has been set, the call to pcre_fullinfo() returns the error
  2116. PCRE_ERROR_UNSET.
  2117. PCRE_INFO_MAXLOOKBEHIND
  2118. Return the number of characters (NB not data units) in the longest
  2119. lookbehind assertion in the pattern. This information is useful when
  2120. doing multi-segment matching using the partial matching facilities.
  2121. Note that the simple assertions \b and \B require a one-character look-
  2122. behind. \A also registers a one-character lookbehind, though it does
  2123. not actually inspect the previous character. This is to ensure that at
  2124. least one character from the old segment is retained when a new segment
  2125. is processed. Otherwise, if there are no lookbehinds in the pattern, \A
  2126. might match incorrectly at the start of a new segment.
  2127. PCRE_INFO_MINLENGTH
  2128. If the pattern was studied and a minimum length for matching subject
  2129. strings was computed, its value is returned. Otherwise the returned
  2130. value is -1. The value is a number of characters, which in UTF mode may
  2131. be different from the number of data units. The fourth argument should
  2132. point to an int variable. A non-negative value is a lower bound to the
  2133. length of any matching string. There may not be any strings of that
  2134. length that do actually match, but every string that does match is at
  2135. least that long.
  2136. PCRE_INFO_NAMECOUNT
  2137. PCRE_INFO_NAMEENTRYSIZE
  2138. PCRE_INFO_NAMETABLE
  2139. PCRE supports the use of named as well as numbered capturing parenthe-
  2140. ses. The names are just an additional way of identifying the parenthe-
  2141. ses, which still acquire numbers. Several convenience functions such as
  2142. pcre_get_named_substring() are provided for extracting captured sub-
  2143. strings by name. It is also possible to extract the data directly, by
  2144. first converting the name to a number in order to access the correct
  2145. pointers in the output vector (described with pcre_exec() below). To do
  2146. the conversion, you need to use the name-to-number map, which is
  2147. described by these three values.
  2148. The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT
  2149. gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size
  2150. of each entry; both of these return an int value. The entry size
  2151. depends on the length of the longest name. PCRE_INFO_NAMETABLE returns
  2152. a pointer to the first entry of the table. This is a pointer to char in
  2153. the 8-bit library, where the first two bytes of each entry are the num-
  2154. ber of the capturing parenthesis, most significant byte first. In the
  2155. 16-bit library, the pointer points to 16-bit data units, the first of
  2156. which contains the parenthesis number. In the 32-bit library, the
  2157. pointer points to 32-bit data units, the first of which contains the
  2158. parenthesis number. The rest of the entry is the corresponding name,
  2159. zero terminated.
  2160. The names are in alphabetical order. If (?| is used to create multiple
  2161. groups with the same number, as described in the section on duplicate
  2162. subpattern numbers in the pcrepattern page, the groups may be given the
  2163. same name, but there is only one entry in the table. Different names
  2164. for groups of the same number are not permitted. Duplicate names for
  2165. subpatterns with different numbers are permitted, but only if PCRE_DUP-
  2166. NAMES is set. They appear in the table in the order in which they were
  2167. found in the pattern. In the absence of (?| this is the order of
  2168. increasing number; when (?| is used this is not necessarily the case
  2169. because later subpatterns may have lower numbers.
  2170. As a simple example of the name/number table, consider the following
  2171. pattern after compilation by the 8-bit library (assume PCRE_EXTENDED is
  2172. set, so white space - including newlines - is ignored):
  2173. (?<date> (?<year>(\d\d)?\d\d) -
  2174. (?<month>\d\d) - (?<day>\d\d) )
  2175. There are four named subpatterns, so the table has four entries, and
  2176. each entry in the table is eight bytes long. The table is as follows,
  2177. with non-printing bytes shows in hexadecimal, and undefined bytes shown
  2178. as ??:
  2179. 00 01 d a t e 00 ??
  2180. 00 05 d a y 00 ?? ??
  2181. 00 04 m o n t h 00
  2182. 00 02 y e a r 00 ??
  2183. When writing code to extract data from named subpatterns using the
  2184. name-to-number map, remember that the length of the entries is likely
  2185. to be different for each compiled pattern.
  2186. PCRE_INFO_OKPARTIAL
  2187. Return 1 if the pattern can be used for partial matching with
  2188. pcre_exec(), otherwise 0. The fourth argument should point to an int
  2189. variable. From release 8.00, this always returns 1, because the
  2190. restrictions that previously applied to partial matching have been
  2191. lifted. The pcrepartial documentation gives details of partial match-
  2192. ing.
  2193. PCRE_INFO_OPTIONS
  2194. Return a copy of the options with which the pattern was compiled. The
  2195. fourth argument should point to an unsigned long int variable. These
  2196. option bits are those specified in the call to pcre_compile(), modified
  2197. by any top-level option settings at the start of the pattern itself. In
  2198. other words, they are the options that will be in force when matching
  2199. starts. For example, if the pattern /(?im)abc(?-i)d/ is compiled with
  2200. the PCRE_EXTENDED option, the result is PCRE_CASELESS, PCRE_MULTILINE,
  2201. and PCRE_EXTENDED.
  2202. A pattern is automatically anchored by PCRE if all of its top-level
  2203. alternatives begin with one of the following:
  2204. ^ unless PCRE_MULTILINE is set
  2205. \A always
  2206. \G always
  2207. .* if PCRE_DOTALL is set and there are no back
  2208. references to the subpattern in which .* appears
  2209. For such patterns, the PCRE_ANCHORED bit is set in the options returned
  2210. by pcre_fullinfo().
  2211. PCRE_INFO_RECURSIONLIMIT
  2212. If the pattern set a recursion limit by including an item of the form
  2213. (*LIMIT_RECURSION=nnnn) at the start, the value is returned. The fourth
  2214. argument should point to an unsigned 32-bit integer. If no such value
  2215. has been set, the call to pcre_fullinfo() returns the error
  2216. PCRE_ERROR_UNSET.
  2217. PCRE_INFO_SIZE
  2218. Return the size of the compiled pattern in bytes (for all three
  2219. libraries). The fourth argument should point to a size_t variable. This
  2220. value does not include the size of the pcre structure that is returned
  2221. by pcre_compile(). The value that is passed as the argument to
  2222. pcre_malloc() when pcre_compile() is getting memory in which to place
  2223. the compiled data is the value returned by this option plus the size of
  2224. the pcre structure. Studying a compiled pattern, with or without JIT,
  2225. does not alter the value returned by this option.
  2226. PCRE_INFO_STUDYSIZE
  2227. Return the size in bytes (for all three libraries) of the data block
  2228. pointed to by the study_data field in a pcre_extra block. If pcre_extra
  2229. is NULL, or there is no study data, zero is returned. The fourth argu-
  2230. ment should point to a size_t variable. The study_data field is set by
  2231. pcre_study() to record information that will speed up matching (see the
  2232. section entitled "Studying a pattern" above). The format of the
  2233. study_data block is private, but its length is made available via this
  2234. option so that it can be saved and restored (see the pcreprecompile
  2235. documentation for details).
  2236. PCRE_INFO_REQUIREDCHARFLAGS
  2237. Returns 1 if there is a rightmost literal data unit that must exist in
  2238. any matched string, other than at its start. The fourth argument should
  2239. point to an int variable. If there is no such value, 0 is returned. If
  2240. returning 1, the character value itself can be retrieved using
  2241. PCRE_INFO_REQUIREDCHAR.
  2242. For anchored patterns, a last literal value is recorded only if it fol-
  2243. lows something of variable length. For example, for the pattern
  2244. /^a\d+z\d+/ the returned value 1 (with "z" returned from
  2245. PCRE_INFO_REQUIREDCHAR), but for /^a\dz\d/ the returned value is 0.
  2246. PCRE_INFO_REQUIREDCHAR
  2247. Return the value of the rightmost literal data unit that must exist in
  2248. any matched string, other than at its start, if such a value has been
  2249. recorded. The fourth argument should point to an uint32_t variable. If
  2250. there is no such value, 0 is returned.
  2251. REFERENCE COUNTS
  2252. int pcre_refcount(pcre *code, int adjust);
  2253. The pcre_refcount() function is used to maintain a reference count in
  2254. the data block that contains a compiled pattern. It is provided for the
  2255. benefit of applications that operate in an object-oriented manner,
  2256. where different parts of the application may be using the same compiled
  2257. pattern, but you want to free the block when they are all done.
  2258. When a pattern is compiled, the reference count field is initialized to
  2259. zero. It is changed only by calling this function, whose action is to
  2260. add the adjust value (which may be positive or negative) to it. The
  2261. yield of the function is the new value. However, the value of the count
  2262. is constrained to lie between 0 and 65535, inclusive. If the new value
  2263. is outside these limits, it is forced to the appropriate limit value.
  2264. Except when it is zero, the reference count is not correctly preserved
  2265. if a pattern is compiled on one host and then transferred to a host
  2266. whose byte-order is different. (This seems a highly unlikely scenario.)
  2267. MATCHING A PATTERN: THE TRADITIONAL FUNCTION
  2268. int pcre_exec(const pcre *code, const pcre_extra *extra,
  2269. const char *subject, int length, int startoffset,
  2270. int options, int *ovector, int ovecsize);
  2271. The function pcre_exec() is called to match a subject string against a
  2272. compiled pattern, which is passed in the code argument. If the pattern
  2273. was studied, the result of the study should be passed in the extra
  2274. argument. You can call pcre_exec() with the same code and extra argu-
  2275. ments as many times as you like, in order to match different subject
  2276. strings with the same pattern.
  2277. This function is the main matching facility of the library, and it
  2278. operates in a Perl-like manner. For specialist use there is also an
  2279. alternative matching function, which is described below in the section
  2280. about the pcre_dfa_exec() function.
  2281. In most applications, the pattern will have been compiled (and option-
  2282. ally studied) in the same process that calls pcre_exec(). However, it
  2283. is possible to save compiled patterns and study data, and then use them
  2284. later in different processes, possibly even on different hosts. For a
  2285. discussion about this, see the pcreprecompile documentation.
  2286. Here is an example of a simple call to pcre_exec():
  2287. int rc;
  2288. int ovector[30];
  2289. rc = pcre_exec(
  2290. re, /* result of pcre_compile() */
  2291. NULL, /* we didn't study the pattern */
  2292. "some string", /* the subject string */
  2293. 11, /* the length of the subject string */
  2294. 0, /* start at offset 0 in the subject */
  2295. 0, /* default options */
  2296. ovector, /* vector of integers for substring information */
  2297. 30); /* number of elements (NOT size in bytes) */
  2298. Extra data for pcre_exec()
  2299. If the extra argument is not NULL, it must point to a pcre_extra data
  2300. block. The pcre_study() function returns such a block (when it doesn't
  2301. return NULL), but you can also create one for yourself, and pass addi-
  2302. tional information in it. The pcre_extra block contains the following
  2303. fields (not necessarily in this order):
  2304. unsigned long int flags;
  2305. void *study_data;
  2306. void *executable_jit;
  2307. unsigned long int match_limit;
  2308. unsigned long int match_limit_recursion;
  2309. void *callout_data;
  2310. const unsigned char *tables;
  2311. unsigned char **mark;
  2312. In the 16-bit version of this structure, the mark field has type
  2313. "PCRE_UCHAR16 **".
  2314. In the 32-bit version of this structure, the mark field has type
  2315. "PCRE_UCHAR32 **".
  2316. The flags field is used to specify which of the other fields are set.
  2317. The flag bits are:
  2318. PCRE_EXTRA_CALLOUT_DATA
  2319. PCRE_EXTRA_EXECUTABLE_JIT
  2320. PCRE_EXTRA_MARK
  2321. PCRE_EXTRA_MATCH_LIMIT
  2322. PCRE_EXTRA_MATCH_LIMIT_RECURSION
  2323. PCRE_EXTRA_STUDY_DATA
  2324. PCRE_EXTRA_TABLES
  2325. Other flag bits should be set to zero. The study_data field and some-
  2326. times the executable_jit field are set in the pcre_extra block that is
  2327. returned by pcre_study(), together with the appropriate flag bits. You
  2328. should not set these yourself, but you may add to the block by setting
  2329. other fields and their corresponding flag bits.
  2330. The match_limit field provides a means of preventing PCRE from using up
  2331. a vast amount of resources when running patterns that are not going to
  2332. match, but which have a very large number of possibilities in their
  2333. search trees. The classic example is a pattern that uses nested unlim-
  2334. ited repeats.
  2335. Internally, pcre_exec() uses a function called match(), which it calls
  2336. repeatedly (sometimes recursively). The limit set by match_limit is
  2337. imposed on the number of times this function is called during a match,
  2338. which has the effect of limiting the amount of backtracking that can
  2339. take place. For patterns that are not anchored, the count restarts from
  2340. zero for each position in the subject string.
  2341. When pcre_exec() is called with a pattern that was successfully studied
  2342. with a JIT option, the way that the matching is executed is entirely
  2343. different. However, there is still the possibility of runaway matching
  2344. that goes on for a very long time, and so the match_limit value is also
  2345. used in this case (but in a different way) to limit how long the match-
  2346. ing can continue.
  2347. The default value for the limit can be set when PCRE is built; the
  2348. default default is 10 million, which handles all but the most extreme
  2349. cases. You can override the default by suppling pcre_exec() with a
  2350. pcre_extra block in which match_limit is set, and
  2351. PCRE_EXTRA_MATCH_LIMIT is set in the flags field. If the limit is
  2352. exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT.
  2353. A value for the match limit may also be supplied by an item at the
  2354. start of a pattern of the form
  2355. (*LIMIT_MATCH=d)
  2356. where d is a decimal number. However, such a setting is ignored unless
  2357. d is less than the limit set by the caller of pcre_exec() or, if no
  2358. such limit is set, less than the default.
  2359. The match_limit_recursion field is similar to match_limit, but instead
  2360. of limiting the total number of times that match() is called, it limits
  2361. the depth of recursion. The recursion depth is a smaller number than
  2362. the total number of calls, because not all calls to match() are recur-
  2363. sive. This limit is of use only if it is set smaller than match_limit.
  2364. Limiting the recursion depth limits the amount of machine stack that
  2365. can be used, or, when PCRE has been compiled to use memory on the heap
  2366. instead of the stack, the amount of heap memory that can be used. This
  2367. limit is not relevant, and is ignored, when matching is done using JIT
  2368. compiled code.
  2369. The default value for match_limit_recursion can be set when PCRE is
  2370. built; the default default is the same value as the default for
  2371. match_limit. You can override the default by suppling pcre_exec() with
  2372. a pcre_extra block in which match_limit_recursion is set, and
  2373. PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in the flags field. If the
  2374. limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT.
  2375. A value for the recursion limit may also be supplied by an item at the
  2376. start of a pattern of the form
  2377. (*LIMIT_RECURSION=d)
  2378. where d is a decimal number. However, such a setting is ignored unless
  2379. d is less than the limit set by the caller of pcre_exec() or, if no
  2380. such limit is set, less than the default.
  2381. The callout_data field is used in conjunction with the "callout" fea-
  2382. ture, and is described in the pcrecallout documentation.
  2383. The tables field is provided for use with patterns that have been pre-
  2384. compiled using custom character tables, saved to disc or elsewhere, and
  2385. then reloaded, because the tables that were used to compile a pattern
  2386. are not saved with it. See the pcreprecompile documentation for a dis-
  2387. cussion of saving compiled patterns for later use. If NULL is passed
  2388. using this mechanism, it forces PCRE's internal tables to be used.
  2389. Warning: The tables that pcre_exec() uses must be the same as those
  2390. that were used when the pattern was compiled. If this is not the case,
  2391. the behaviour of pcre_exec() is undefined. Therefore, when a pattern is
  2392. compiled and matched in the same process, this field should never be
  2393. set. In this (the most common) case, the correct table pointer is auto-
  2394. matically passed with the compiled pattern from pcre_compile() to
  2395. pcre_exec().
  2396. If PCRE_EXTRA_MARK is set in the flags field, the mark field must be
  2397. set to point to a suitable variable. If the pattern contains any back-
  2398. tracking control verbs such as (*MARK:NAME), and the execution ends up
  2399. with a name to pass back, a pointer to the name string (zero termi-
  2400. nated) is placed in the variable pointed to by the mark field. The
  2401. names are within the compiled pattern; if you wish to retain such a
  2402. name you must copy it before freeing the memory of a compiled pattern.
  2403. If there is no name to pass back, the variable pointed to by the mark
  2404. field is set to NULL. For details of the backtracking control verbs,
  2405. see the section entitled "Backtracking control" in the pcrepattern doc-
  2406. umentation.
  2407. Option bits for pcre_exec()
  2408. The unused bits of the options argument for pcre_exec() must be zero.
  2409. The only bits that may be set are PCRE_ANCHORED, PCRE_NEWLINE_xxx,
  2410. PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART,
  2411. PCRE_NO_START_OPTIMIZE, PCRE_NO_UTF8_CHECK, PCRE_PARTIAL_HARD, and
  2412. PCRE_PARTIAL_SOFT.
  2413. If the pattern was successfully studied with one of the just-in-time
  2414. (JIT) compile options, the only supported options for JIT execution are
  2415. PCRE_NO_UTF8_CHECK, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY,
  2416. PCRE_NOTEMPTY_ATSTART, PCRE_PARTIAL_HARD, and PCRE_PARTIAL_SOFT. If an
  2417. unsupported option is used, JIT execution is disabled and the normal
  2418. interpretive code in pcre_exec() is run.
  2419. PCRE_ANCHORED
  2420. The PCRE_ANCHORED option limits pcre_exec() to matching at the first
  2421. matching position. If a pattern was compiled with PCRE_ANCHORED, or
  2422. turned out to be anchored by virtue of its contents, it cannot be made
  2423. unachored at matching time.
  2424. PCRE_BSR_ANYCRLF
  2425. PCRE_BSR_UNICODE
  2426. These options (which are mutually exclusive) control what the \R escape
  2427. sequence matches. The choice is either to match only CR, LF, or CRLF,
  2428. or to match any Unicode newline sequence. These options override the
  2429. choice that was made or defaulted when the pattern was compiled.
  2430. PCRE_NEWLINE_CR
  2431. PCRE_NEWLINE_LF
  2432. PCRE_NEWLINE_CRLF
  2433. PCRE_NEWLINE_ANYCRLF
  2434. PCRE_NEWLINE_ANY
  2435. These options override the newline definition that was chosen or
  2436. defaulted when the pattern was compiled. For details, see the descrip-
  2437. tion of pcre_compile() above. During matching, the newline choice
  2438. affects the behaviour of the dot, circumflex, and dollar metacharac-
  2439. ters. It may also alter the way the match position is advanced after a
  2440. match failure for an unanchored pattern.
  2441. When PCRE_NEWLINE_CRLF, PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is
  2442. set, and a match attempt for an unanchored pattern fails when the cur-
  2443. rent position is at a CRLF sequence, and the pattern contains no
  2444. explicit matches for CR or LF characters, the match position is
  2445. advanced by two characters instead of one, in other words, to after the
  2446. CRLF.
  2447. The above rule is a compromise that makes the most common cases work as
  2448. expected. For example, if the pattern is .+A (and the PCRE_DOTALL
  2449. option is not set), it does not match the string "\r\nA" because, after
  2450. failing at the start, it skips both the CR and the LF before retrying.
  2451. However, the pattern [\r\n]A does match that string, because it con-
  2452. tains an explicit CR or LF reference, and so advances only by one char-
  2453. acter after the first failure.
  2454. An explicit match for CR of LF is either a literal appearance of one of
  2455. those characters, or one of the \r or \n escape sequences. Implicit
  2456. matches such as [^X] do not count, nor does \s (which includes CR and
  2457. LF in the characters that it matches).
  2458. Notwithstanding the above, anomalous effects may still occur when CRLF
  2459. is a valid newline sequence and explicit \r or \n escapes appear in the
  2460. pattern.
  2461. PCRE_NOTBOL
  2462. This option specifies that first character of the subject string is not
  2463. the beginning of a line, so the circumflex metacharacter should not
  2464. match before it. Setting this without PCRE_MULTILINE (at compile time)
  2465. causes circumflex never to match. This option affects only the behav-
  2466. iour of the circumflex metacharacter. It does not affect \A.
  2467. PCRE_NOTEOL
  2468. This option specifies that the end of the subject string is not the end
  2469. of a line, so the dollar metacharacter should not match it nor (except
  2470. in multiline mode) a newline immediately before it. Setting this with-
  2471. out PCRE_MULTILINE (at compile time) causes dollar never to match. This
  2472. option affects only the behaviour of the dollar metacharacter. It does
  2473. not affect \Z or \z.
  2474. PCRE_NOTEMPTY
  2475. An empty string is not considered to be a valid match if this option is
  2476. set. If there are alternatives in the pattern, they are tried. If all
  2477. the alternatives match the empty string, the entire match fails. For
  2478. example, if the pattern
  2479. a?b?
  2480. is applied to a string not beginning with "a" or "b", it matches an
  2481. empty string at the start of the subject. With PCRE_NOTEMPTY set, this
  2482. match is not valid, so PCRE searches further into the string for occur-
  2483. rences of "a" or "b".
  2484. PCRE_NOTEMPTY_ATSTART
  2485. This is like PCRE_NOTEMPTY, except that an empty string match that is
  2486. not at the start of the subject is permitted. If the pattern is
  2487. anchored, such a match can occur only if the pattern contains \K.
  2488. Perl has no direct equivalent of PCRE_NOTEMPTY or
  2489. PCRE_NOTEMPTY_ATSTART, but it does make a special case of a pattern
  2490. match of the empty string within its split() function, and when using
  2491. the /g modifier. It is possible to emulate Perl's behaviour after
  2492. matching a null string by first trying the match again at the same off-
  2493. set with PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED, and then if that
  2494. fails, by advancing the starting offset (see below) and trying an ordi-
  2495. nary match again. There is some code that demonstrates how to do this
  2496. in the pcredemo sample program. In the most general case, you have to
  2497. check to see if the newline convention recognizes CRLF as a newline,
  2498. and if so, and the current character is CR followed by LF, advance the
  2499. starting offset by two characters instead of one.
  2500. PCRE_NO_START_OPTIMIZE
  2501. There are a number of optimizations that pcre_exec() uses at the start
  2502. of a match, in order to speed up the process. For example, if it is
  2503. known that an unanchored match must start with a specific character, it
  2504. searches the subject for that character, and fails immediately if it
  2505. cannot find it, without actually running the main matching function.
  2506. This means that a special item such as (*COMMIT) at the start of a pat-
  2507. tern is not considered until after a suitable starting point for the
  2508. match has been found. Also, when callouts or (*MARK) items are in use,
  2509. these "start-up" optimizations can cause them to be skipped if the pat-
  2510. tern is never actually used. The start-up optimizations are in effect a
  2511. pre-scan of the subject that takes place before the pattern is run.
  2512. The PCRE_NO_START_OPTIMIZE option disables the start-up optimizations,
  2513. possibly causing performance to suffer, but ensuring that in cases
  2514. where the result is "no match", the callouts do occur, and that items
  2515. such as (*COMMIT) and (*MARK) are considered at every possible starting
  2516. position in the subject string. If PCRE_NO_START_OPTIMIZE is set at
  2517. compile time, it cannot be unset at matching time. The use of
  2518. PCRE_NO_START_OPTIMIZE at matching time (that is, passing it to
  2519. pcre_exec()) disables JIT execution; in this situation, matching is
  2520. always done using interpretively.
  2521. Setting PCRE_NO_START_OPTIMIZE can change the outcome of a matching
  2522. operation. Consider the pattern
  2523. (*COMMIT)ABC
  2524. When this is compiled, PCRE records the fact that a match must start
  2525. with the character "A". Suppose the subject string is "DEFABC". The
  2526. start-up optimization scans along the subject, finds "A" and runs the
  2527. first match attempt from there. The (*COMMIT) item means that the pat-
  2528. tern must match the current starting position, which in this case, it
  2529. does. However, if the same match is run with PCRE_NO_START_OPTIMIZE
  2530. set, the initial scan along the subject string does not happen. The
  2531. first match attempt is run starting from "D" and when this fails,
  2532. (*COMMIT) prevents any further matches being tried, so the overall
  2533. result is "no match". If the pattern is studied, more start-up opti-
  2534. mizations may be used. For example, a minimum length for the subject
  2535. may be recorded. Consider the pattern
  2536. (*MARK:A)(X|Y)
  2537. The minimum length for a match is one character. If the subject is
  2538. "ABC", there will be attempts to match "ABC", "BC", "C", and then
  2539. finally an empty string. If the pattern is studied, the final attempt
  2540. does not take place, because PCRE knows that the subject is too short,
  2541. and so the (*MARK) is never encountered. In this case, studying the
  2542. pattern does not affect the overall match result, which is still "no
  2543. match", but it does affect the auxiliary information that is returned.
  2544. PCRE_NO_UTF8_CHECK
  2545. When PCRE_UTF8 is set at compile time, the validity of the subject as a
  2546. UTF-8 string is automatically checked when pcre_exec() is subsequently
  2547. called. The entire string is checked before any other processing takes
  2548. place. The value of startoffset is also checked to ensure that it
  2549. points to the start of a UTF-8 character. There is a discussion about
  2550. the validity of UTF-8 strings in the pcreunicode page. If an invalid
  2551. sequence of bytes is found, pcre_exec() returns the error
  2552. PCRE_ERROR_BADUTF8 or, if PCRE_PARTIAL_HARD is set and the problem is a
  2553. truncated character at the end of the subject, PCRE_ERROR_SHORTUTF8. In
  2554. both cases, information about the precise nature of the error may also
  2555. be returned (see the descriptions of these errors in the section enti-
  2556. tled Error return values from pcre_exec() below). If startoffset con-
  2557. tains a value that does not point to the start of a UTF-8 character (or
  2558. to the end of the subject), PCRE_ERROR_BADUTF8_OFFSET is returned.
  2559. If you already know that your subject is valid, and you want to skip
  2560. these checks for performance reasons, you can set the
  2561. PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might want to
  2562. do this for the second and subsequent calls to pcre_exec() if you are
  2563. making repeated calls to find all the matches in a single subject
  2564. string. However, you should be sure that the value of startoffset
  2565. points to the start of a character (or the end of the subject). When
  2566. PCRE_NO_UTF8_CHECK is set, the effect of passing an invalid string as a
  2567. subject or an invalid value of startoffset is undefined. Your program
  2568. may crash or loop.
  2569. PCRE_PARTIAL_HARD
  2570. PCRE_PARTIAL_SOFT
  2571. These options turn on the partial matching feature. For backwards com-
  2572. patibility, PCRE_PARTIAL is a synonym for PCRE_PARTIAL_SOFT. A partial
  2573. match occurs if the end of the subject string is reached successfully,
  2574. but there are not enough subject characters to complete the match. If
  2575. this happens when PCRE_PARTIAL_SOFT (but not PCRE_PARTIAL_HARD) is set,
  2576. matching continues by testing any remaining alternatives. Only if no
  2577. complete match can be found is PCRE_ERROR_PARTIAL returned instead of
  2578. PCRE_ERROR_NOMATCH. In other words, PCRE_PARTIAL_SOFT says that the
  2579. caller is prepared to handle a partial match, but only if no complete
  2580. match can be found.
  2581. If PCRE_PARTIAL_HARD is set, it overrides PCRE_PARTIAL_SOFT. In this
  2582. case, if a partial match is found, pcre_exec() immediately returns
  2583. PCRE_ERROR_PARTIAL, without considering any other alternatives. In
  2584. other words, when PCRE_PARTIAL_HARD is set, a partial match is consid-
  2585. ered to be more important that an alternative complete match.
  2586. In both cases, the portion of the string that was inspected when the
  2587. partial match was found is set as the first matching string. There is a
  2588. more detailed discussion of partial and multi-segment matching, with
  2589. examples, in the pcrepartial documentation.
  2590. The string to be matched by pcre_exec()
  2591. The subject string is passed to pcre_exec() as a pointer in subject, a
  2592. length in length, and a starting offset in startoffset. The units for
  2593. length and startoffset are bytes for the 8-bit library, 16-bit data
  2594. items for the 16-bit library, and 32-bit data items for the 32-bit
  2595. library.
  2596. If startoffset is negative or greater than the length of the subject,
  2597. pcre_exec() returns PCRE_ERROR_BADOFFSET. When the starting offset is
  2598. zero, the search for a match starts at the beginning of the subject,
  2599. and this is by far the most common case. In UTF-8 or UTF-16 mode, the
  2600. offset must point to the start of a character, or the end of the sub-
  2601. ject (in UTF-32 mode, one data unit equals one character, so all off-
  2602. sets are valid). Unlike the pattern string, the subject may contain
  2603. binary zeroes.
  2604. A non-zero starting offset is useful when searching for another match
  2605. in the same subject by calling pcre_exec() again after a previous suc-
  2606. cess. Setting startoffset differs from just passing over a shortened
  2607. string and setting PCRE_NOTBOL in the case of a pattern that begins
  2608. with any kind of lookbehind. For example, consider the pattern
  2609. \Biss\B
  2610. which finds occurrences of "iss" in the middle of words. (\B matches
  2611. only if the current position in the subject is not a word boundary.)
  2612. When applied to the string "Mississipi" the first call to pcre_exec()
  2613. finds the first occurrence. If pcre_exec() is called again with just
  2614. the remainder of the subject, namely "issipi", it does not match,
  2615. because \B is always false at the start of the subject, which is deemed
  2616. to be a word boundary. However, if pcre_exec() is passed the entire
  2617. string again, but with startoffset set to 4, it finds the second occur-
  2618. rence of "iss" because it is able to look behind the starting point to
  2619. discover that it is preceded by a letter.
  2620. Finding all the matches in a subject is tricky when the pattern can
  2621. match an empty string. It is possible to emulate Perl's /g behaviour by
  2622. first trying the match again at the same offset, with the
  2623. PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED options, and then if that
  2624. fails, advancing the starting offset and trying an ordinary match
  2625. again. There is some code that demonstrates how to do this in the pcre-
  2626. demo sample program. In the most general case, you have to check to see
  2627. if the newline convention recognizes CRLF as a newline, and if so, and
  2628. the current character is CR followed by LF, advance the starting offset
  2629. by two characters instead of one.
  2630. If a non-zero starting offset is passed when the pattern is anchored,
  2631. one attempt to match at the given offset is made. This can only succeed
  2632. if the pattern does not require the match to be at the start of the
  2633. subject.
  2634. How pcre_exec() returns captured substrings
  2635. In general, a pattern matches a certain portion of the subject, and in
  2636. addition, further substrings from the subject may be picked out by
  2637. parts of the pattern. Following the usage in Jeffrey Friedl's book,
  2638. this is called "capturing" in what follows, and the phrase "capturing
  2639. subpattern" is used for a fragment of a pattern that picks out a sub-
  2640. string. PCRE supports several other kinds of parenthesized subpattern
  2641. that do not cause substrings to be captured.
  2642. Captured substrings are returned to the caller via a vector of integers
  2643. whose address is passed in ovector. The number of elements in the vec-
  2644. tor is passed in ovecsize, which must be a non-negative number. Note:
  2645. this argument is NOT the size of ovector in bytes.
  2646. The first two-thirds of the vector is used to pass back captured sub-
  2647. strings, each substring using a pair of integers. The remaining third
  2648. of the vector is used as workspace by pcre_exec() while matching cap-
  2649. turing subpatterns, and is not available for passing back information.
  2650. The number passed in ovecsize should always be a multiple of three. If
  2651. it is not, it is rounded down.
  2652. When a match is successful, information about captured substrings is
  2653. returned in pairs of integers, starting at the beginning of ovector,
  2654. and continuing up to two-thirds of its length at the most. The first
  2655. element of each pair is set to the offset of the first character in a
  2656. substring, and the second is set to the offset of the first character
  2657. after the end of a substring. These values are always data unit off-
  2658. sets, even in UTF mode. They are byte offsets in the 8-bit library,
  2659. 16-bit data item offsets in the 16-bit library, and 32-bit data item
  2660. offsets in the 32-bit library. Note: they are not character counts.
  2661. The first pair of integers, ovector[0] and ovector[1], identify the
  2662. portion of the subject string matched by the entire pattern. The next
  2663. pair is used for the first capturing subpattern, and so on. The value
  2664. returned by pcre_exec() is one more than the highest numbered pair that
  2665. has been set. For example, if two substrings have been captured, the
  2666. returned value is 3. If there are no capturing subpatterns, the return
  2667. value from a successful match is 1, indicating that just the first pair
  2668. of offsets has been set.
  2669. If a capturing subpattern is matched repeatedly, it is the last portion
  2670. of the string that it matched that is returned.
  2671. If the vector is too small to hold all the captured substring offsets,
  2672. it is used as far as possible (up to two-thirds of its length), and the
  2673. function returns a value of zero. If neither the actual string matched
  2674. nor any captured substrings are of interest, pcre_exec() may be called
  2675. with ovector passed as NULL and ovecsize as zero. However, if the pat-
  2676. tern contains back references and the ovector is not big enough to
  2677. remember the related substrings, PCRE has to get additional memory for
  2678. use during matching. Thus it is usually advisable to supply an ovector
  2679. of reasonable size.
  2680. There are some cases where zero is returned (indicating vector over-
  2681. flow) when in fact the vector is exactly the right size for the final
  2682. match. For example, consider the pattern
  2683. (a)(?:(b)c|bd)
  2684. If a vector of 6 elements (allowing for only 1 captured substring) is
  2685. given with subject string "abd", pcre_exec() will try to set the second
  2686. captured string, thereby recording a vector overflow, before failing to
  2687. match "c" and backing up to try the second alternative. The zero
  2688. return, however, does correctly indicate that the maximum number of
  2689. slots (namely 2) have been filled. In similar cases where there is tem-
  2690. porary overflow, but the final number of used slots is actually less
  2691. than the maximum, a non-zero value is returned.
  2692. The pcre_fullinfo() function can be used to find out how many capturing
  2693. subpatterns there are in a compiled pattern. The smallest size for
  2694. ovector that will allow for n captured substrings, in addition to the
  2695. offsets of the substring matched by the whole pattern, is (n+1)*3.
  2696. It is possible for capturing subpattern number n+1 to match some part
  2697. of the subject when subpattern n has not been used at all. For example,
  2698. if the string "abc" is matched against the pattern (a|(z))(bc) the
  2699. return from the function is 4, and subpatterns 1 and 3 are matched, but
  2700. 2 is not. When this happens, both values in the offset pairs corre-
  2701. sponding to unused subpatterns are set to -1.
  2702. Offset values that correspond to unused subpatterns at the end of the
  2703. expression are also set to -1. For example, if the string "abc" is
  2704. matched against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not
  2705. matched. The return from the function is 2, because the highest used
  2706. capturing subpattern number is 1, and the offsets for for the second
  2707. and third capturing subpatterns (assuming the vector is large enough,
  2708. of course) are set to -1.
  2709. Note: Elements in the first two-thirds of ovector that do not corre-
  2710. spond to capturing parentheses in the pattern are never changed. That
  2711. is, if a pattern contains n capturing parentheses, no more than ovec-
  2712. tor[0] to ovector[2n+1] are set by pcre_exec(). The other elements (in
  2713. the first two-thirds) retain whatever values they previously had.
  2714. Some convenience functions are provided for extracting the captured
  2715. substrings as separate strings. These are described below.
  2716. Error return values from pcre_exec()
  2717. If pcre_exec() fails, it returns a negative number. The following are
  2718. defined in the header file:
  2719. PCRE_ERROR_NOMATCH (-1)
  2720. The subject string did not match the pattern.
  2721. PCRE_ERROR_NULL (-2)
  2722. Either code or subject was passed as NULL, or ovector was NULL and
  2723. ovecsize was not zero.
  2724. PCRE_ERROR_BADOPTION (-3)
  2725. An unrecognized bit was set in the options argument.
  2726. PCRE_ERROR_BADMAGIC (-4)
  2727. PCRE stores a 4-byte "magic number" at the start of the compiled code,
  2728. to catch the case when it is passed a junk pointer and to detect when a
  2729. pattern that was compiled in an environment of one endianness is run in
  2730. an environment with the other endianness. This is the error that PCRE
  2731. gives when the magic number is not present.
  2732. PCRE_ERROR_UNKNOWN_OPCODE (-5)
  2733. While running the pattern match, an unknown item was encountered in the
  2734. compiled pattern. This error could be caused by a bug in PCRE or by
  2735. overwriting of the compiled pattern.
  2736. PCRE_ERROR_NOMEMORY (-6)
  2737. If a pattern contains back references, but the ovector that is passed
  2738. to pcre_exec() is not big enough to remember the referenced substrings,
  2739. PCRE gets a block of memory at the start of matching to use for this
  2740. purpose. If the call via pcre_malloc() fails, this error is given. The
  2741. memory is automatically freed at the end of matching.
  2742. This error is also given if pcre_stack_malloc() fails in pcre_exec().
  2743. This can happen only when PCRE has been compiled with --disable-stack-
  2744. for-recursion.
  2745. PCRE_ERROR_NOSUBSTRING (-7)
  2746. This error is used by the pcre_copy_substring(), pcre_get_substring(),
  2747. and pcre_get_substring_list() functions (see below). It is never
  2748. returned by pcre_exec().
  2749. PCRE_ERROR_MATCHLIMIT (-8)
  2750. The backtracking limit, as specified by the match_limit field in a
  2751. pcre_extra structure (or defaulted) was reached. See the description
  2752. above.
  2753. PCRE_ERROR_CALLOUT (-9)
  2754. This error is never generated by pcre_exec() itself. It is provided for
  2755. use by callout functions that want to yield a distinctive error code.
  2756. See the pcrecallout documentation for details.
  2757. PCRE_ERROR_BADUTF8 (-10)
  2758. A string that contains an invalid UTF-8 byte sequence was passed as a
  2759. subject, and the PCRE_NO_UTF8_CHECK option was not set. If the size of
  2760. the output vector (ovecsize) is at least 2, the byte offset to the
  2761. start of the the invalid UTF-8 character is placed in the first ele-
  2762. ment, and a reason code is placed in the second element. The reason
  2763. codes are listed in the following section. For backward compatibility,
  2764. if PCRE_PARTIAL_HARD is set and the problem is a truncated UTF-8 char-
  2765. acter at the end of the subject (reason codes 1 to 5),
  2766. PCRE_ERROR_SHORTUTF8 is returned instead of PCRE_ERROR_BADUTF8.
  2767. PCRE_ERROR_BADUTF8_OFFSET (-11)
  2768. The UTF-8 byte sequence that was passed as a subject was checked and
  2769. found to be valid (the PCRE_NO_UTF8_CHECK option was not set), but the
  2770. value of startoffset did not point to the beginning of a UTF-8 charac-
  2771. ter or the end of the subject.
  2772. PCRE_ERROR_PARTIAL (-12)
  2773. The subject string did not match, but it did match partially. See the
  2774. pcrepartial documentation for details of partial matching.
  2775. PCRE_ERROR_BADPARTIAL (-13)
  2776. This code is no longer in use. It was formerly returned when the
  2777. PCRE_PARTIAL option was used with a compiled pattern containing items
  2778. that were not supported for partial matching. From release 8.00
  2779. onwards, there are no restrictions on partial matching.
  2780. PCRE_ERROR_INTERNAL (-14)
  2781. An unexpected internal error has occurred. This error could be caused
  2782. by a bug in PCRE or by overwriting of the compiled pattern.
  2783. PCRE_ERROR_BADCOUNT (-15)
  2784. This error is given if the value of the ovecsize argument is negative.
  2785. PCRE_ERROR_RECURSIONLIMIT (-21)
  2786. The internal recursion limit, as specified by the match_limit_recursion
  2787. field in a pcre_extra structure (or defaulted) was reached. See the
  2788. description above.
  2789. PCRE_ERROR_BADNEWLINE (-23)
  2790. An invalid combination of PCRE_NEWLINE_xxx options was given.
  2791. PCRE_ERROR_BADOFFSET (-24)
  2792. The value of startoffset was negative or greater than the length of the
  2793. subject, that is, the value in length.
  2794. PCRE_ERROR_SHORTUTF8 (-25)
  2795. This error is returned instead of PCRE_ERROR_BADUTF8 when the subject
  2796. string ends with a truncated UTF-8 character and the PCRE_PARTIAL_HARD
  2797. option is set. Information about the failure is returned as for
  2798. PCRE_ERROR_BADUTF8. It is in fact sufficient to detect this case, but
  2799. this special error code for PCRE_PARTIAL_HARD precedes the implementa-
  2800. tion of returned information; it is retained for backwards compatibil-
  2801. ity.
  2802. PCRE_ERROR_RECURSELOOP (-26)
  2803. This error is returned when pcre_exec() detects a recursion loop within
  2804. the pattern. Specifically, it means that either the whole pattern or a
  2805. subpattern has been called recursively for the second time at the same
  2806. position in the subject string. Some simple patterns that might do this
  2807. are detected and faulted at compile time, but more complicated cases,
  2808. in particular mutual recursions between two different subpatterns, can-
  2809. not be detected until run time.
  2810. PCRE_ERROR_JIT_STACKLIMIT (-27)
  2811. This error is returned when a pattern that was successfully studied
  2812. using a JIT compile option is being matched, but the memory available
  2813. for the just-in-time processing stack is not large enough. See the
  2814. pcrejit documentation for more details.
  2815. PCRE_ERROR_BADMODE (-28)
  2816. This error is given if a pattern that was compiled by the 8-bit library
  2817. is passed to a 16-bit or 32-bit library function, or vice versa.
  2818. PCRE_ERROR_BADENDIANNESS (-29)
  2819. This error is given if a pattern that was compiled and saved is
  2820. reloaded on a host with different endianness. The utility function
  2821. pcre_pattern_to_host_byte_order() can be used to convert such a pattern
  2822. so that it runs on the new host.
  2823. PCRE_ERROR_JIT_BADOPTION
  2824. This error is returned when a pattern that was successfully studied
  2825. using a JIT compile option is being matched, but the matching mode
  2826. (partial or complete match) does not correspond to any JIT compilation
  2827. mode. When the JIT fast path function is used, this error may be also
  2828. given for invalid options. See the pcrejit documentation for more
  2829. details.
  2830. PCRE_ERROR_BADLENGTH (-32)
  2831. This error is given if pcre_exec() is called with a negative value for
  2832. the length argument.
  2833. Error numbers -16 to -20, -22, and 30 are not used by pcre_exec().
  2834. Reason codes for invalid UTF-8 strings
  2835. This section applies only to the 8-bit library. The corresponding
  2836. information for the 16-bit and 32-bit libraries is given in the pcre16
  2837. and pcre32 pages.
  2838. When pcre_exec() returns either PCRE_ERROR_BADUTF8 or PCRE_ERROR_SHORT-
  2839. UTF8, and the size of the output vector (ovecsize) is at least 2, the
  2840. offset of the start of the invalid UTF-8 character is placed in the
  2841. first output vector element (ovector[0]) and a reason code is placed in
  2842. the second element (ovector[1]). The reason codes are given names in
  2843. the pcre.h header file:
  2844. PCRE_UTF8_ERR1
  2845. PCRE_UTF8_ERR2
  2846. PCRE_UTF8_ERR3
  2847. PCRE_UTF8_ERR4
  2848. PCRE_UTF8_ERR5
  2849. The string ends with a truncated UTF-8 character; the code specifies
  2850. how many bytes are missing (1 to 5). Although RFC 3629 restricts UTF-8
  2851. characters to be no longer than 4 bytes, the encoding scheme (origi-
  2852. nally defined by RFC 2279) allows for up to 6 bytes, and this is
  2853. checked first; hence the possibility of 4 or 5 missing bytes.
  2854. PCRE_UTF8_ERR6
  2855. PCRE_UTF8_ERR7
  2856. PCRE_UTF8_ERR8
  2857. PCRE_UTF8_ERR9
  2858. PCRE_UTF8_ERR10
  2859. The two most significant bits of the 2nd, 3rd, 4th, 5th, or 6th byte of
  2860. the character do not have the binary value 0b10 (that is, either the
  2861. most significant bit is 0, or the next bit is 1).
  2862. PCRE_UTF8_ERR11
  2863. PCRE_UTF8_ERR12
  2864. A character that is valid by the RFC 2279 rules is either 5 or 6 bytes
  2865. long; these code points are excluded by RFC 3629.
  2866. PCRE_UTF8_ERR13
  2867. A 4-byte character has a value greater than 0x10fff; these code points
  2868. are excluded by RFC 3629.
  2869. PCRE_UTF8_ERR14
  2870. A 3-byte character has a value in the range 0xd800 to 0xdfff; this
  2871. range of code points are reserved by RFC 3629 for use with UTF-16, and
  2872. so are excluded from UTF-8.
  2873. PCRE_UTF8_ERR15
  2874. PCRE_UTF8_ERR16
  2875. PCRE_UTF8_ERR17
  2876. PCRE_UTF8_ERR18
  2877. PCRE_UTF8_ERR19
  2878. A 2-, 3-, 4-, 5-, or 6-byte character is "overlong", that is, it codes
  2879. for a value that can be represented by fewer bytes, which is invalid.
  2880. For example, the two bytes 0xc0, 0xae give the value 0x2e, whose cor-
  2881. rect coding uses just one byte.
  2882. PCRE_UTF8_ERR20
  2883. The two most significant bits of the first byte of a character have the
  2884. binary value 0b10 (that is, the most significant bit is 1 and the sec-
  2885. ond is 0). Such a byte can only validly occur as the second or subse-
  2886. quent byte of a multi-byte character.
  2887. PCRE_UTF8_ERR21
  2888. The first byte of a character has the value 0xfe or 0xff. These values
  2889. can never occur in a valid UTF-8 string.
  2890. PCRE_UTF8_ERR22
  2891. This error code was formerly used when the presence of a so-called
  2892. "non-character" caused an error. Unicode corrigendum #9 makes it clear
  2893. that such characters should not cause a string to be rejected, and so
  2894. this code is no longer in use and is never returned.
  2895. EXTRACTING CAPTURED SUBSTRINGS BY NUMBER
  2896. int pcre_copy_substring(const char *subject, int *ovector,
  2897. int stringcount, int stringnumber, char *buffer,
  2898. int buffersize);
  2899. int pcre_get_substring(const char *subject, int *ovector,
  2900. int stringcount, int stringnumber,
  2901. const char **stringptr);
  2902. int pcre_get_substring_list(const char *subject,
  2903. int *ovector, int stringcount, const char ***listptr);
  2904. Captured substrings can be accessed directly by using the offsets
  2905. returned by pcre_exec() in ovector. For convenience, the functions
  2906. pcre_copy_substring(), pcre_get_substring(), and pcre_get_sub-
  2907. string_list() are provided for extracting captured substrings as new,
  2908. separate, zero-terminated strings. These functions identify substrings
  2909. by number. The next section describes functions for extracting named
  2910. substrings.
  2911. A substring that contains a binary zero is correctly extracted and has
  2912. a further zero added on the end, but the result is not, of course, a C
  2913. string. However, you can process such a string by referring to the
  2914. length that is returned by pcre_copy_substring() and pcre_get_sub-
  2915. string(). Unfortunately, the interface to pcre_get_substring_list() is
  2916. not adequate for handling strings containing binary zeros, because the
  2917. end of the final string is not independently indicated.
  2918. The first three arguments are the same for all three of these func-
  2919. tions: subject is the subject string that has just been successfully
  2920. matched, ovector is a pointer to the vector of integer offsets that was
  2921. passed to pcre_exec(), and stringcount is the number of substrings that
  2922. were captured by the match, including the substring that matched the
  2923. entire regular expression. This is the value returned by pcre_exec() if
  2924. it is greater than zero. If pcre_exec() returned zero, indicating that
  2925. it ran out of space in ovector, the value passed as stringcount should
  2926. be the number of elements in the vector divided by three.
  2927. The functions pcre_copy_substring() and pcre_get_substring() extract a
  2928. single substring, whose number is given as stringnumber. A value of
  2929. zero extracts the substring that matched the entire pattern, whereas
  2930. higher values extract the captured substrings. For pcre_copy_sub-
  2931. string(), the string is placed in buffer, whose length is given by
  2932. buffersize, while for pcre_get_substring() a new block of memory is
  2933. obtained via pcre_malloc, and its address is returned via stringptr.
  2934. The yield of the function is the length of the string, not including
  2935. the terminating zero, or one of these error codes:
  2936. PCRE_ERROR_NOMEMORY (-6)
  2937. The buffer was too small for pcre_copy_substring(), or the attempt to
  2938. get memory failed for pcre_get_substring().
  2939. PCRE_ERROR_NOSUBSTRING (-7)
  2940. There is no substring whose number is stringnumber.
  2941. The pcre_get_substring_list() function extracts all available sub-
  2942. strings and builds a list of pointers to them. All this is done in a
  2943. single block of memory that is obtained via pcre_malloc. The address of
  2944. the memory block is returned via listptr, which is also the start of
  2945. the list of string pointers. The end of the list is marked by a NULL
  2946. pointer. The yield of the function is zero if all went well, or the
  2947. error code
  2948. PCRE_ERROR_NOMEMORY (-6)
  2949. if the attempt to get the memory block failed.
  2950. When any of these functions encounter a substring that is unset, which
  2951. can happen when capturing subpattern number n+1 matches some part of
  2952. the subject, but subpattern n has not been used at all, they return an
  2953. empty string. This can be distinguished from a genuine zero-length sub-
  2954. string by inspecting the appropriate offset in ovector, which is nega-
  2955. tive for unset substrings.
  2956. The two convenience functions pcre_free_substring() and pcre_free_sub-
  2957. string_list() can be used to free the memory returned by a previous
  2958. call of pcre_get_substring() or pcre_get_substring_list(), respec-
  2959. tively. They do nothing more than call the function pointed to by
  2960. pcre_free, which of course could be called directly from a C program.
  2961. However, PCRE is used in some situations where it is linked via a spe-
  2962. cial interface to another programming language that cannot use
  2963. pcre_free directly; it is for these cases that the functions are pro-
  2964. vided.
  2965. EXTRACTING CAPTURED SUBSTRINGS BY NAME
  2966. int pcre_get_stringnumber(const pcre *code,
  2967. const char *name);
  2968. int pcre_copy_named_substring(const pcre *code,
  2969. const char *subject, int *ovector,
  2970. int stringcount, const char *stringname,
  2971. char *buffer, int buffersize);
  2972. int pcre_get_named_substring(const pcre *code,
  2973. const char *subject, int *ovector,
  2974. int stringcount, const char *stringname,
  2975. const char **stringptr);
  2976. To extract a substring by name, you first have to find associated num-
  2977. ber. For example, for this pattern
  2978. (a+)b(?<xxx>\d+)...
  2979. the number of the subpattern called "xxx" is 2. If the name is known to
  2980. be unique (PCRE_DUPNAMES was not set), you can find the number from the
  2981. name by calling pcre_get_stringnumber(). The first argument is the com-
  2982. piled pattern, and the second is the name. The yield of the function is
  2983. the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if there is no
  2984. subpattern of that name.
  2985. Given the number, you can extract the substring directly, or use one of
  2986. the functions described in the previous section. For convenience, there
  2987. are also two functions that do the whole job.
  2988. Most of the arguments of pcre_copy_named_substring() and
  2989. pcre_get_named_substring() are the same as those for the similarly
  2990. named functions that extract by number. As these are described in the
  2991. previous section, they are not re-described here. There are just two
  2992. differences:
  2993. First, instead of a substring number, a substring name is given. Sec-
  2994. ond, there is an extra argument, given at the start, which is a pointer
  2995. to the compiled pattern. This is needed in order to gain access to the
  2996. name-to-number translation table.
  2997. These functions call pcre_get_stringnumber(), and if it succeeds, they
  2998. then call pcre_copy_substring() or pcre_get_substring(), as appropri-
  2999. ate. NOTE: If PCRE_DUPNAMES is set and there are duplicate names, the
  3000. behaviour may not be what you want (see the next section).
  3001. Warning: If the pattern uses the (?| feature to set up multiple subpat-
  3002. terns with the same number, as described in the section on duplicate
  3003. subpattern numbers in the pcrepattern page, you cannot use names to
  3004. distinguish the different subpatterns, because names are not included
  3005. in the compiled code. The matching process uses only numbers. For this
  3006. reason, the use of different names for subpatterns of the same number
  3007. causes an error at compile time.
  3008. DUPLICATE SUBPATTERN NAMES
  3009. int pcre_get_stringtable_entries(const pcre *code,
  3010. const char *name, char **first, char **last);
  3011. When a pattern is compiled with the PCRE_DUPNAMES option, names for
  3012. subpatterns are not required to be unique. (Duplicate names are always
  3013. allowed for subpatterns with the same number, created by using the (?|
  3014. feature. Indeed, if such subpatterns are named, they are required to
  3015. use the same names.)
  3016. Normally, patterns with duplicate names are such that in any one match,
  3017. only one of the named subpatterns participates. An example is shown in
  3018. the pcrepattern documentation.
  3019. When duplicates are present, pcre_copy_named_substring() and
  3020. pcre_get_named_substring() return the first substring corresponding to
  3021. the given name that is set. If none are set, PCRE_ERROR_NOSUBSTRING
  3022. (-7) is returned; no data is returned. The pcre_get_stringnumber()
  3023. function returns one of the numbers that are associated with the name,
  3024. but it is not defined which it is.
  3025. If you want to get full details of all captured substrings for a given
  3026. name, you must use the pcre_get_stringtable_entries() function. The
  3027. first argument is the compiled pattern, and the second is the name. The
  3028. third and fourth are pointers to variables which are updated by the
  3029. function. After it has run, they point to the first and last entries in
  3030. the name-to-number table for the given name. The function itself
  3031. returns the length of each entry, or PCRE_ERROR_NOSUBSTRING (-7) if
  3032. there are none. The format of the table is described above in the sec-
  3033. tion entitled Information about a pattern above. Given all the rele-
  3034. vant entries for the name, you can extract each of their numbers, and
  3035. hence the captured data, if any.
  3036. FINDING ALL POSSIBLE MATCHES
  3037. The traditional matching function uses a similar algorithm to Perl,
  3038. which stops when it finds the first match, starting at a given point in
  3039. the subject. If you want to find all possible matches, or the longest
  3040. possible match, consider using the alternative matching function (see
  3041. below) instead. If you cannot use the alternative function, but still
  3042. need to find all possible matches, you can kludge it up by making use
  3043. of the callout facility, which is described in the pcrecallout documen-
  3044. tation.
  3045. What you have to do is to insert a callout right at the end of the pat-
  3046. tern. When your callout function is called, extract and save the cur-
  3047. rent matched substring. Then return 1, which forces pcre_exec() to
  3048. backtrack and try other alternatives. Ultimately, when it runs out of
  3049. matches, pcre_exec() will yield PCRE_ERROR_NOMATCH.
  3050. OBTAINING AN ESTIMATE OF STACK USAGE
  3051. Matching certain patterns using pcre_exec() can use a lot of process
  3052. stack, which in certain environments can be rather limited in size.
  3053. Some users find it helpful to have an estimate of the amount of stack
  3054. that is used by pcre_exec(), to help them set recursion limits, as
  3055. described in the pcrestack documentation. The estimate that is output
  3056. by pcretest when called with the -m and -C options is obtained by call-
  3057. ing pcre_exec with the values NULL, NULL, NULL, -999, and -999 for its
  3058. first five arguments.
  3059. Normally, if its first argument is NULL, pcre_exec() immediately
  3060. returns the negative error code PCRE_ERROR_NULL, but with this special
  3061. combination of arguments, it returns instead a negative number whose
  3062. absolute value is the approximate stack frame size in bytes. (A nega-
  3063. tive number is used so that it is clear that no match has happened.)
  3064. The value is approximate because in some cases, recursive calls to
  3065. pcre_exec() occur when there are one or two additional variables on the
  3066. stack.
  3067. If PCRE has been compiled to use the heap instead of the stack for
  3068. recursion, the value returned is the size of each block that is
  3069. obtained from the heap.
  3070. MATCHING A PATTERN: THE ALTERNATIVE FUNCTION
  3071. int pcre_dfa_exec(const pcre *code, const pcre_extra *extra,
  3072. const char *subject, int length, int startoffset,
  3073. int options, int *ovector, int ovecsize,
  3074. int *workspace, int wscount);
  3075. The function pcre_dfa_exec() is called to match a subject string
  3076. against a compiled pattern, using a matching algorithm that scans the
  3077. subject string just once, and does not backtrack. This has different
  3078. characteristics to the normal algorithm, and is not compatible with
  3079. Perl. Some of the features of PCRE patterns are not supported. Never-
  3080. theless, there are times when this kind of matching can be useful. For
  3081. a discussion of the two matching algorithms, and a list of features
  3082. that pcre_dfa_exec() does not support, see the pcrematching documenta-
  3083. tion.
  3084. The arguments for the pcre_dfa_exec() function are the same as for
  3085. pcre_exec(), plus two extras. The ovector argument is used in a differ-
  3086. ent way, and this is described below. The other common arguments are
  3087. used in the same way as for pcre_exec(), so their description is not
  3088. repeated here.
  3089. The two additional arguments provide workspace for the function. The
  3090. workspace vector should contain at least 20 elements. It is used for
  3091. keeping track of multiple paths through the pattern tree. More
  3092. workspace will be needed for patterns and subjects where there are a
  3093. lot of potential matches.
  3094. Here is an example of a simple call to pcre_dfa_exec():
  3095. int rc;
  3096. int ovector[10];
  3097. int wspace[20];
  3098. rc = pcre_dfa_exec(
  3099. re, /* result of pcre_compile() */
  3100. NULL, /* we didn't study the pattern */
  3101. "some string", /* the subject string */
  3102. 11, /* the length of the subject string */
  3103. 0, /* start at offset 0 in the subject */
  3104. 0, /* default options */
  3105. ovector, /* vector of integers for substring information */
  3106. 10, /* number of elements (NOT size in bytes) */
  3107. wspace, /* working space vector */
  3108. 20); /* number of elements (NOT size in bytes) */
  3109. Option bits for pcre_dfa_exec()
  3110. The unused bits of the options argument for pcre_dfa_exec() must be
  3111. zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NEW-
  3112. LINE_xxx, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY,
  3113. PCRE_NOTEMPTY_ATSTART, PCRE_NO_UTF8_CHECK, PCRE_BSR_ANYCRLF,
  3114. PCRE_BSR_UNICODE, PCRE_NO_START_OPTIMIZE, PCRE_PARTIAL_HARD, PCRE_PAR-
  3115. TIAL_SOFT, PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last
  3116. four of these are exactly the same as for pcre_exec(), so their
  3117. description is not repeated here.
  3118. PCRE_PARTIAL_HARD
  3119. PCRE_PARTIAL_SOFT
  3120. These have the same general effect as they do for pcre_exec(), but the
  3121. details are slightly different. When PCRE_PARTIAL_HARD is set for
  3122. pcre_dfa_exec(), it returns PCRE_ERROR_PARTIAL if the end of the sub-
  3123. ject is reached and there is still at least one matching possibility
  3124. that requires additional characters. This happens even if some complete
  3125. matches have also been found. When PCRE_PARTIAL_SOFT is set, the return
  3126. code PCRE_ERROR_NOMATCH is converted into PCRE_ERROR_PARTIAL if the end
  3127. of the subject is reached, there have been no complete matches, but
  3128. there is still at least one matching possibility. The portion of the
  3129. string that was inspected when the longest partial match was found is
  3130. set as the first matching string in both cases. There is a more
  3131. detailed discussion of partial and multi-segment matching, with exam-
  3132. ples, in the pcrepartial documentation.
  3133. PCRE_DFA_SHORTEST
  3134. Setting the PCRE_DFA_SHORTEST option causes the matching algorithm to
  3135. stop as soon as it has found one match. Because of the way the alterna-
  3136. tive algorithm works, this is necessarily the shortest possible match
  3137. at the first possible matching point in the subject string.
  3138. PCRE_DFA_RESTART
  3139. When pcre_dfa_exec() returns a partial match, it is possible to call it
  3140. again, with additional subject characters, and have it continue with
  3141. the same match. The PCRE_DFA_RESTART option requests this action; when
  3142. it is set, the workspace and wscount options must reference the same
  3143. vector as before because data about the match so far is left in them
  3144. after a partial match. There is more discussion of this facility in the
  3145. pcrepartial documentation.
  3146. Successful returns from pcre_dfa_exec()
  3147. When pcre_dfa_exec() succeeds, it may have matched more than one sub-
  3148. string in the subject. Note, however, that all the matches from one run
  3149. of the function start at the same point in the subject. The shorter
  3150. matches are all initial substrings of the longer matches. For example,
  3151. if the pattern
  3152. <.*>
  3153. is matched against the string
  3154. This is <something> <something else> <something further> no more
  3155. the three matched strings are
  3156. <something>
  3157. <something> <something else>
  3158. <something> <something else> <something further>
  3159. On success, the yield of the function is a number greater than zero,
  3160. which is the number of matched substrings. The substrings themselves
  3161. are returned in ovector. Each string uses two elements; the first is
  3162. the offset to the start, and the second is the offset to the end. In
  3163. fact, all the strings have the same start offset. (Space could have
  3164. been saved by giving this only once, but it was decided to retain some
  3165. compatibility with the way pcre_exec() returns data, even though the
  3166. meaning of the strings is different.)
  3167. The strings are returned in reverse order of length; that is, the long-
  3168. est matching string is given first. If there were too many matches to
  3169. fit into ovector, the yield of the function is zero, and the vector is
  3170. filled with the longest matches. Unlike pcre_exec(), pcre_dfa_exec()
  3171. can use the entire ovector for returning matched strings.
  3172. NOTE: PCRE's "auto-possessification" optimization usually applies to
  3173. character repeats at the end of a pattern (as well as internally). For
  3174. example, the pattern "a\d+" is compiled as if it were "a\d++" because
  3175. there is no point even considering the possibility of backtracking into
  3176. the repeated digits. For DFA matching, this means that only one possi-
  3177. ble match is found. If you really do want multiple matches in such
  3178. cases, either use an ungreedy repeat ("a\d+?") or set the
  3179. PCRE_NO_AUTO_POSSESS option when compiling.
  3180. Error returns from pcre_dfa_exec()
  3181. The pcre_dfa_exec() function returns a negative number when it fails.
  3182. Many of the errors are the same as for pcre_exec(), and these are
  3183. described above. There are in addition the following errors that are
  3184. specific to pcre_dfa_exec():
  3185. PCRE_ERROR_DFA_UITEM (-16)
  3186. This return is given if pcre_dfa_exec() encounters an item in the pat-
  3187. tern that it does not support, for instance, the use of \C or a back
  3188. reference.
  3189. PCRE_ERROR_DFA_UCOND (-17)
  3190. This return is given if pcre_dfa_exec() encounters a condition item
  3191. that uses a back reference for the condition, or a test for recursion
  3192. in a specific group. These are not supported.
  3193. PCRE_ERROR_DFA_UMLIMIT (-18)
  3194. This return is given if pcre_dfa_exec() is called with an extra block
  3195. that contains a setting of the match_limit or match_limit_recursion
  3196. fields. This is not supported (these fields are meaningless for DFA
  3197. matching).
  3198. PCRE_ERROR_DFA_WSSIZE (-19)
  3199. This return is given if pcre_dfa_exec() runs out of space in the
  3200. workspace vector.
  3201. PCRE_ERROR_DFA_RECURSE (-20)
  3202. When a recursive subpattern is processed, the matching function calls
  3203. itself recursively, using private vectors for ovector and workspace.
  3204. This error is given if the output vector is not large enough. This
  3205. should be extremely rare, as a vector of size 1000 is used.
  3206. PCRE_ERROR_DFA_BADRESTART (-30)
  3207. When pcre_dfa_exec() is called with the PCRE_DFA_RESTART option, some
  3208. plausibility checks are made on the contents of the workspace, which
  3209. should contain data about the previous partial match. If any of these
  3210. checks fail, this error is given.
  3211. SEE ALSO
  3212. pcre16(3), pcre32(3), pcrebuild(3), pcrecallout(3), pcrecpp(3)(3),
  3213. pcrematching(3), pcrepartial(3), pcreposix(3), pcreprecompile(3), pcre-
  3214. sample(3), pcrestack(3).
  3215. AUTHOR
  3216. Philip Hazel
  3217. University Computing Service
  3218. Cambridge CB2 3QH, England.
  3219. REVISION
  3220. Last updated: 09 February 2014
  3221. Copyright (c) 1997-2014 University of Cambridge.
  3222. ------------------------------------------------------------------------------
  3223. PCRECALLOUT(3) Library Functions Manual PCRECALLOUT(3)
  3224. NAME
  3225. PCRE - Perl-compatible regular expressions
  3226. SYNOPSIS
  3227. #include <pcre.h>
  3228. int (*pcre_callout)(pcre_callout_block *);
  3229. int (*pcre16_callout)(pcre16_callout_block *);
  3230. int (*pcre32_callout)(pcre32_callout_block *);
  3231. DESCRIPTION
  3232. PCRE provides a feature called "callout", which is a means of temporar-
  3233. ily passing control to the caller of PCRE in the middle of pattern
  3234. matching. The caller of PCRE provides an external function by putting
  3235. its entry point in the global variable pcre_callout (pcre16_callout for
  3236. the 16-bit library, pcre32_callout for the 32-bit library). By default,
  3237. this variable contains NULL, which disables all calling out.
  3238. Within a regular expression, (?C) indicates the points at which the
  3239. external function is to be called. Different callout points can be
  3240. identified by putting a number less than 256 after the letter C. The
  3241. default value is zero. For example, this pattern has two callout
  3242. points:
  3243. (?C1)abc(?C2)def
  3244. If the PCRE_AUTO_CALLOUT option bit is set when a pattern is compiled,
  3245. PCRE automatically inserts callouts, all with number 255, before each
  3246. item in the pattern. For example, if PCRE_AUTO_CALLOUT is used with the
  3247. pattern
  3248. A(\d{2}|--)
  3249. it is processed as if it were
  3250. (?C255)A(?C255)((?C255)\d{2}(?C255)|(?C255)-(?C255)-(?C255))(?C255)
  3251. Notice that there is a callout before and after each parenthesis and
  3252. alternation bar. If the pattern contains a conditional group whose con-
  3253. dition is an assertion, an automatic callout is inserted immediately
  3254. before the condition. Such a callout may also be inserted explicitly,
  3255. for example:
  3256. (?(?C9)(?=a)ab|de)
  3257. This applies only to assertion conditions (because they are themselves
  3258. independent groups).
  3259. Automatic callouts can be used for tracking the progress of pattern
  3260. matching. The pcretest program has a pattern qualifier (/C) that sets
  3261. automatic callouts; when it is used, the output indicates how the pat-
  3262. tern is being matched. This is useful information when you are trying
  3263. to optimize the performance of a particular pattern.
  3264. MISSING CALLOUTS
  3265. You should be aware that, because of optimizations in the way PCRE com-
  3266. piles and matches patterns, callouts sometimes do not happen exactly as
  3267. you might expect.
  3268. At compile time, PCRE "auto-possessifies" repeated items when it knows
  3269. that what follows cannot be part of the repeat. For example, a+[bc] is
  3270. compiled as if it were a++[bc]. The pcretest output when this pattern
  3271. is anchored and then applied with automatic callouts to the string
  3272. "aaaa" is:
  3273. --->aaaa
  3274. +0 ^ ^
  3275. +1 ^ a+
  3276. +3 ^ ^ [bc]
  3277. No match
  3278. This indicates that when matching [bc] fails, there is no backtracking
  3279. into a+ and therefore the callouts that would be taken for the back-
  3280. tracks do not occur. You can disable the auto-possessify feature by
  3281. passing PCRE_NO_AUTO_POSSESS to pcre_compile(), or starting the pattern
  3282. with (*NO_AUTO_POSSESS). If this is done in pcretest (using the /O
  3283. qualifier), the output changes to this:
  3284. --->aaaa
  3285. +0 ^ ^
  3286. +1 ^ a+
  3287. +3 ^ ^ [bc]
  3288. +3 ^ ^ [bc]
  3289. +3 ^ ^ [bc]
  3290. +3 ^^ [bc]
  3291. No match
  3292. This time, when matching [bc] fails, the matcher backtracks into a+ and
  3293. tries again, repeatedly, until a+ itself fails.
  3294. Other optimizations that provide fast "no match" results also affect
  3295. callouts. For example, if the pattern is
  3296. ab(?C4)cd
  3297. PCRE knows that any matching string must contain the letter "d". If the
  3298. subject string is "abyz", the lack of "d" means that matching doesn't
  3299. ever start, and the callout is never reached. However, with "abyd",
  3300. though the result is still no match, the callout is obeyed.
  3301. If the pattern is studied, PCRE knows the minimum length of a matching
  3302. string, and will immediately give a "no match" return without actually
  3303. running a match if the subject is not long enough, or, for unanchored
  3304. patterns, if it has been scanned far enough.
  3305. You can disable these optimizations by passing the PCRE_NO_START_OPTI-
  3306. MIZE option to the matching function, or by starting the pattern with
  3307. (*NO_START_OPT). This slows down the matching process, but does ensure
  3308. that callouts such as the example above are obeyed.
  3309. THE CALLOUT INTERFACE
  3310. During matching, when PCRE reaches a callout point, the external func-
  3311. tion defined by pcre_callout or pcre[16|32]_callout is called (if it is
  3312. set). This applies to both normal and DFA matching. The only argument
  3313. to the callout function is a pointer to a pcre_callout or
  3314. pcre[16|32]_callout block. These structures contains the following
  3315. fields:
  3316. int version;
  3317. int callout_number;
  3318. int *offset_vector;
  3319. const char *subject; (8-bit version)
  3320. PCRE_SPTR16 subject; (16-bit version)
  3321. PCRE_SPTR32 subject; (32-bit version)
  3322. int subject_length;
  3323. int start_match;
  3324. int current_position;
  3325. int capture_top;
  3326. int capture_last;
  3327. void *callout_data;
  3328. int pattern_position;
  3329. int next_item_length;
  3330. const unsigned char *mark; (8-bit version)
  3331. const PCRE_UCHAR16 *mark; (16-bit version)
  3332. const PCRE_UCHAR32 *mark; (32-bit version)
  3333. The version field is an integer containing the version number of the
  3334. block format. The initial version was 0; the current version is 2. The
  3335. version number will change again in future if additional fields are
  3336. added, but the intention is never to remove any of the existing fields.
  3337. The callout_number field contains the number of the callout, as com-
  3338. piled into the pattern (that is, the number after ?C for manual call-
  3339. outs, and 255 for automatically generated callouts).
  3340. The offset_vector field is a pointer to the vector of offsets that was
  3341. passed by the caller to the matching function. When pcre_exec() or
  3342. pcre[16|32]_exec() is used, the contents can be inspected, in order to
  3343. extract substrings that have been matched so far, in the same way as
  3344. for extracting substrings after a match has completed. For the DFA
  3345. matching functions, this field is not useful.
  3346. The subject and subject_length fields contain copies of the values that
  3347. were passed to the matching function.
  3348. The start_match field normally contains the offset within the subject
  3349. at which the current match attempt started. However, if the escape
  3350. sequence \K has been encountered, this value is changed to reflect the
  3351. modified starting point. If the pattern is not anchored, the callout
  3352. function may be called several times from the same point in the pattern
  3353. for different starting points in the subject.
  3354. The current_position field contains the offset within the subject of
  3355. the current match pointer.
  3356. When the pcre_exec() or pcre[16|32]_exec() is used, the capture_top
  3357. field contains one more than the number of the highest numbered cap-
  3358. tured substring so far. If no substrings have been captured, the value
  3359. of capture_top is one. This is always the case when the DFA functions
  3360. are used, because they do not support captured substrings.
  3361. The capture_last field contains the number of the most recently cap-
  3362. tured substring. However, when a recursion exits, the value reverts to
  3363. what it was outside the recursion, as do the values of all captured
  3364. substrings. If no substrings have been captured, the value of cap-
  3365. ture_last is -1. This is always the case for the DFA matching func-
  3366. tions.
  3367. The callout_data field contains a value that is passed to a matching
  3368. function specifically so that it can be passed back in callouts. It is
  3369. passed in the callout_data field of a pcre_extra or pcre[16|32]_extra
  3370. data structure. If no such data was passed, the value of callout_data
  3371. in a callout block is NULL. There is a description of the pcre_extra
  3372. structure in the pcreapi documentation.
  3373. The pattern_position field is present from version 1 of the callout
  3374. structure. It contains the offset to the next item to be matched in the
  3375. pattern string.
  3376. The next_item_length field is present from version 1 of the callout
  3377. structure. It contains the length of the next item to be matched in the
  3378. pattern string. When the callout immediately precedes an alternation
  3379. bar, a closing parenthesis, or the end of the pattern, the length is
  3380. zero. When the callout precedes an opening parenthesis, the length is
  3381. that of the entire subpattern.
  3382. The pattern_position and next_item_length fields are intended to help
  3383. in distinguishing between different automatic callouts, which all have
  3384. the same callout number. However, they are set for all callouts.
  3385. The mark field is present from version 2 of the callout structure. In
  3386. callouts from pcre_exec() or pcre[16|32]_exec() it contains a pointer
  3387. to the zero-terminated name of the most recently passed (*MARK),
  3388. (*PRUNE), or (*THEN) item in the match, or NULL if no such items have
  3389. been passed. Instances of (*PRUNE) or (*THEN) without a name do not
  3390. obliterate a previous (*MARK). In callouts from the DFA matching func-
  3391. tions this field always contains NULL.
  3392. RETURN VALUES
  3393. The external callout function returns an integer to PCRE. If the value
  3394. is zero, matching proceeds as normal. If the value is greater than
  3395. zero, matching fails at the current point, but the testing of other
  3396. matching possibilities goes ahead, just as if a lookahead assertion had
  3397. failed. If the value is less than zero, the match is abandoned, the
  3398. matching function returns the negative value.
  3399. Negative values should normally be chosen from the set of
  3400. PCRE_ERROR_xxx values. In particular, PCRE_ERROR_NOMATCH forces a stan-
  3401. dard "no match" failure. The error number PCRE_ERROR_CALLOUT is
  3402. reserved for use by callout functions; it will never be used by PCRE
  3403. itself.
  3404. AUTHOR
  3405. Philip Hazel
  3406. University Computing Service
  3407. Cambridge CB2 3QH, England.
  3408. REVISION
  3409. Last updated: 12 November 2013
  3410. Copyright (c) 1997-2013 University of Cambridge.
  3411. ------------------------------------------------------------------------------
  3412. PCRECOMPAT(3) Library Functions Manual PCRECOMPAT(3)
  3413. NAME
  3414. PCRE - Perl-compatible regular expressions
  3415. DIFFERENCES BETWEEN PCRE AND PERL
  3416. This document describes the differences in the ways that PCRE and Perl
  3417. handle regular expressions. The differences described here are with
  3418. respect to Perl versions 5.10 and above.
  3419. 1. PCRE has only a subset of Perl's Unicode support. Details of what it
  3420. does have are given in the pcreunicode page.
  3421. 2. PCRE allows repeat quantifiers only on parenthesized assertions, but
  3422. they do not mean what you might think. For example, (?!a){3} does not
  3423. assert that the next three characters are not "a". It just asserts that
  3424. the next character is not "a" three times (in principle: PCRE optimizes
  3425. this to run the assertion just once). Perl allows repeat quantifiers on
  3426. other assertions such as \b, but these do not seem to have any use.
  3427. 3. Capturing subpatterns that occur inside negative lookahead asser-
  3428. tions are counted, but their entries in the offsets vector are never
  3429. set. Perl sometimes (but not always) sets its numerical variables from
  3430. inside negative assertions.
  3431. 4. Though binary zero characters are supported in the subject string,
  3432. they are not allowed in a pattern string because it is passed as a nor-
  3433. mal C string, terminated by zero. The escape sequence \0 can be used in
  3434. the pattern to represent a binary zero.
  3435. 5. The following Perl escape sequences are not supported: \l, \u, \L,
  3436. \U, and \N when followed by a character name or Unicode value. (\N on
  3437. its own, matching a non-newline character, is supported.) In fact these
  3438. are implemented by Perl's general string-handling and are not part of
  3439. its pattern matching engine. If any of these are encountered by PCRE,
  3440. an error is generated by default. However, if the PCRE_JAVASCRIPT_COM-
  3441. PAT option is set, \U and \u are interpreted as JavaScript interprets
  3442. them.
  3443. 6. The Perl escape sequences \p, \P, and \X are supported only if PCRE
  3444. is built with Unicode character property support. The properties that
  3445. can be tested with \p and \P are limited to the general category prop-
  3446. erties such as Lu and Nd, script names such as Greek or Han, and the
  3447. derived properties Any and L&. PCRE does support the Cs (surrogate)
  3448. property, which Perl does not; the Perl documentation says "Because
  3449. Perl hides the need for the user to understand the internal representa-
  3450. tion of Unicode characters, there is no need to implement the somewhat
  3451. messy concept of surrogates."
  3452. 7. PCRE does support the \Q...\E escape for quoting substrings. Charac-
  3453. ters in between are treated as literals. This is slightly different
  3454. from Perl in that $ and @ are also handled as literals inside the
  3455. quotes. In Perl, they cause variable interpolation (but of course PCRE
  3456. does not have variables). Note the following examples:
  3457. Pattern PCRE matches Perl matches
  3458. \Qabc$xyz\E abc$xyz abc followed by the
  3459. contents of $xyz
  3460. \Qabc\$xyz\E abc\$xyz abc\$xyz
  3461. \Qabc\E\$\Qxyz\E abc$xyz abc$xyz
  3462. The \Q...\E sequence is recognized both inside and outside character
  3463. classes.
  3464. 8. Fairly obviously, PCRE does not support the (?{code}) and (??{code})
  3465. constructions. However, there is support for recursive patterns. This
  3466. is not available in Perl 5.8, but it is in Perl 5.10. Also, the PCRE
  3467. "callout" feature allows an external function to be called during pat-
  3468. tern matching. See the pcrecallout documentation for details.
  3469. 9. Subpatterns that are called as subroutines (whether or not recur-
  3470. sively) are always treated as atomic groups in PCRE. This is like
  3471. Python, but unlike Perl. Captured values that are set outside a sub-
  3472. routine call can be reference from inside in PCRE, but not in Perl.
  3473. There is a discussion that explains these differences in more detail in
  3474. the section on recursion differences from Perl in the pcrepattern page.
  3475. 10. If any of the backtracking control verbs are used in a subpattern
  3476. that is called as a subroutine (whether or not recursively), their
  3477. effect is confined to that subpattern; it does not extend to the sur-
  3478. rounding pattern. This is not always the case in Perl. In particular,
  3479. if (*THEN) is present in a group that is called as a subroutine, its
  3480. action is limited to that group, even if the group does not contain any
  3481. | characters. Note that such subpatterns are processed as anchored at
  3482. the point where they are tested.
  3483. 11. If a pattern contains more than one backtracking control verb, the
  3484. first one that is backtracked onto acts. For example, in the pattern
  3485. A(*COMMIT)B(*PRUNE)C a failure in B triggers (*COMMIT), but a failure
  3486. in C triggers (*PRUNE). Perl's behaviour is more complex; in many cases
  3487. it is the same as PCRE, but there are examples where it differs.
  3488. 12. Most backtracking verbs in assertions have their normal actions.
  3489. They are not confined to the assertion.
  3490. 13. There are some differences that are concerned with the settings of
  3491. captured strings when part of a pattern is repeated. For example,
  3492. matching "aba" against the pattern /^(a(b)?)+$/ in Perl leaves $2
  3493. unset, but in PCRE it is set to "b".
  3494. 14. PCRE's handling of duplicate subpattern numbers and duplicate sub-
  3495. pattern names is not as general as Perl's. This is a consequence of the
  3496. fact the PCRE works internally just with numbers, using an external ta-
  3497. ble to translate between numbers and names. In particular, a pattern
  3498. such as (?|(?<a>A)|(?<b)B), where the two capturing parentheses have
  3499. the same number but different names, is not supported, and causes an
  3500. error at compile time. If it were allowed, it would not be possible to
  3501. distinguish which parentheses matched, because both names map to cap-
  3502. turing subpattern number 1. To avoid this confusing situation, an error
  3503. is given at compile time.
  3504. 15. Perl recognizes comments in some places that PCRE does not, for
  3505. example, between the ( and ? at the start of a subpattern. If the /x
  3506. modifier is set, Perl allows white space between ( and ? (though cur-
  3507. rent Perls warn that this is deprecated) but PCRE never does, even if
  3508. the PCRE_EXTENDED option is set.
  3509. 16. Perl, when in warning mode, gives warnings for character classes
  3510. such as [A-\d] or [a-[:digit:]]. It then treats the hyphens as liter-
  3511. als. PCRE has no warning features, so it gives an error in these cases
  3512. because they are almost certainly user mistakes.
  3513. 17. In PCRE, the upper/lower case character properties Lu and Ll are
  3514. not affected when case-independent matching is specified. For example,
  3515. \p{Lu} always matches an upper case letter. I think Perl has changed in
  3516. this respect; in the release at the time of writing (5.16), \p{Lu} and
  3517. \p{Ll} match all letters, regardless of case, when case independence is
  3518. specified.
  3519. 18. PCRE provides some extensions to the Perl regular expression facil-
  3520. ities. Perl 5.10 includes new features that are not in earlier ver-
  3521. sions of Perl, some of which (such as named parentheses) have been in
  3522. PCRE for some time. This list is with respect to Perl 5.10:
  3523. (a) Although lookbehind assertions in PCRE must match fixed length
  3524. strings, each alternative branch of a lookbehind assertion can match a
  3525. different length of string. Perl requires them all to have the same
  3526. length.
  3527. (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $
  3528. meta-character matches only at the very end of the string.
  3529. (c) If PCRE_EXTRA is set, a backslash followed by a letter with no spe-
  3530. cial meaning is faulted. Otherwise, like Perl, the backslash is quietly
  3531. ignored. (Perl can be made to issue a warning.)
  3532. (d) If PCRE_UNGREEDY is set, the greediness of the repetition quanti-
  3533. fiers is inverted, that is, by default they are not greedy, but if fol-
  3534. lowed by a question mark they are.
  3535. (e) PCRE_ANCHORED can be used at matching time to force a pattern to be
  3536. tried only at the first matching position in the subject string.
  3537. (f) The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART,
  3538. and PCRE_NO_AUTO_CAPTURE options for pcre_exec() have no Perl equiva-
  3539. lents.
  3540. (g) The \R escape sequence can be restricted to match only CR, LF, or
  3541. CRLF by the PCRE_BSR_ANYCRLF option.
  3542. (h) The callout facility is PCRE-specific.
  3543. (i) The partial matching facility is PCRE-specific.
  3544. (j) Patterns compiled by PCRE can be saved and re-used at a later time,
  3545. even on different hosts that have the other endianness. However, this
  3546. does not apply to optimized data created by the just-in-time compiler.
  3547. (k) The alternative matching functions (pcre_dfa_exec(),
  3548. pcre16_dfa_exec() and pcre32_dfa_exec(),) match in a different way and
  3549. are not Perl-compatible.
  3550. (l) PCRE recognizes some special sequences such as (*CR) at the start
  3551. of a pattern that set overall options that cannot be changed within the
  3552. pattern.
  3553. AUTHOR
  3554. Philip Hazel
  3555. University Computing Service
  3556. Cambridge CB2 3QH, England.
  3557. REVISION
  3558. Last updated: 10 November 2013
  3559. Copyright (c) 1997-2013 University of Cambridge.
  3560. ------------------------------------------------------------------------------
  3561. PCREPATTERN(3) Library Functions Manual PCREPATTERN(3)
  3562. NAME
  3563. PCRE - Perl-compatible regular expressions
  3564. PCRE REGULAR EXPRESSION DETAILS
  3565. The syntax and semantics of the regular expressions that are supported
  3566. by PCRE are described in detail below. There is a quick-reference syn-
  3567. tax summary in the pcresyntax page. PCRE tries to match Perl syntax and
  3568. semantics as closely as it can. PCRE also supports some alternative
  3569. regular expression syntax (which does not conflict with the Perl syn-
  3570. tax) in order to provide some compatibility with regular expressions in
  3571. Python, .NET, and Oniguruma.
  3572. Perl's regular expressions are described in its own documentation, and
  3573. regular expressions in general are covered in a number of books, some
  3574. of which have copious examples. Jeffrey Friedl's "Mastering Regular
  3575. Expressions", published by O'Reilly, covers regular expressions in
  3576. great detail. This description of PCRE's regular expressions is
  3577. intended as reference material.
  3578. This document discusses the patterns that are supported by PCRE when
  3579. one its main matching functions, pcre_exec() (8-bit) or
  3580. pcre[16|32]_exec() (16- or 32-bit), is used. PCRE also has alternative
  3581. matching functions, pcre_dfa_exec() and pcre[16|32_dfa_exec(), which
  3582. match using a different algorithm that is not Perl-compatible. Some of
  3583. the features discussed below are not available when DFA matching is
  3584. used. The advantages and disadvantages of the alternative functions,
  3585. and how they differ from the normal functions, are discussed in the
  3586. pcrematching page.
  3587. SPECIAL START-OF-PATTERN ITEMS
  3588. A number of options that can be passed to pcre_compile() can also be
  3589. set by special items at the start of a pattern. These are not Perl-com-
  3590. patible, but are provided to make these options accessible to pattern
  3591. writers who are not able to change the program that processes the pat-
  3592. tern. Any number of these items may appear, but they must all be
  3593. together right at the start of the pattern string, and the letters must
  3594. be in upper case.
  3595. UTF support
  3596. The original operation of PCRE was on strings of one-byte characters.
  3597. However, there is now also support for UTF-8 strings in the original
  3598. library, an extra library that supports 16-bit and UTF-16 character
  3599. strings, and a third library that supports 32-bit and UTF-32 character
  3600. strings. To use these features, PCRE must be built to include appropri-
  3601. ate support. When using UTF strings you must either call the compiling
  3602. function with the PCRE_UTF8, PCRE_UTF16, or PCRE_UTF32 option, or the
  3603. pattern must start with one of these special sequences:
  3604. (*UTF8)
  3605. (*UTF16)
  3606. (*UTF32)
  3607. (*UTF)
  3608. (*UTF) is a generic sequence that can be used with any of the
  3609. libraries. Starting a pattern with such a sequence is equivalent to
  3610. setting the relevant option. How setting a UTF mode affects pattern
  3611. matching is mentioned in several places below. There is also a summary
  3612. of features in the pcreunicode page.
  3613. Some applications that allow their users to supply patterns may wish to
  3614. restrict them to non-UTF data for security reasons. If the
  3615. PCRE_NEVER_UTF option is set at compile time, (*UTF) etc. are not
  3616. allowed, and their appearance causes an error.
  3617. Unicode property support
  3618. Another special sequence that may appear at the start of a pattern is
  3619. (*UCP). This has the same effect as setting the PCRE_UCP option: it
  3620. causes sequences such as \d and \w to use Unicode properties to deter-
  3621. mine character types, instead of recognizing only characters with codes
  3622. less than 128 via a lookup table.
  3623. Disabling auto-possessification
  3624. If a pattern starts with (*NO_AUTO_POSSESS), it has the same effect as
  3625. setting the PCRE_NO_AUTO_POSSESS option at compile time. This stops
  3626. PCRE from making quantifiers possessive when what follows cannot match
  3627. the repeated item. For example, by default a+b is treated as a++b. For
  3628. more details, see the pcreapi documentation.
  3629. Disabling start-up optimizations
  3630. If a pattern starts with (*NO_START_OPT), it has the same effect as
  3631. setting the PCRE_NO_START_OPTIMIZE option either at compile or matching
  3632. time. This disables several optimizations for quickly reaching "no
  3633. match" results. For more details, see the pcreapi documentation.
  3634. Newline conventions
  3635. PCRE supports five different conventions for indicating line breaks in
  3636. strings: a single CR (carriage return) character, a single LF (line-
  3637. feed) character, the two-character sequence CRLF, any of the three pre-
  3638. ceding, or any Unicode newline sequence. The pcreapi page has further
  3639. discussion about newlines, and shows how to set the newline convention
  3640. in the options arguments for the compiling and matching functions.
  3641. It is also possible to specify a newline convention by starting a pat-
  3642. tern string with one of the following five sequences:
  3643. (*CR) carriage return
  3644. (*LF) linefeed
  3645. (*CRLF) carriage return, followed by linefeed
  3646. (*ANYCRLF) any of the three above
  3647. (*ANY) all Unicode newline sequences
  3648. These override the default and the options given to the compiling func-
  3649. tion. For example, on a Unix system where LF is the default newline
  3650. sequence, the pattern
  3651. (*CR)a.b
  3652. changes the convention to CR. That pattern matches "a\nb" because LF is
  3653. no longer a newline. If more than one of these settings is present, the
  3654. last one is used.
  3655. The newline convention affects where the circumflex and dollar asser-
  3656. tions are true. It also affects the interpretation of the dot metachar-
  3657. acter when PCRE_DOTALL is not set, and the behaviour of \N. However, it
  3658. does not affect what the \R escape sequence matches. By default, this
  3659. is any Unicode newline sequence, for Perl compatibility. However, this
  3660. can be changed; see the description of \R in the section entitled "New-
  3661. line sequences" below. A change of \R setting can be combined with a
  3662. change of newline convention.
  3663. Setting match and recursion limits
  3664. The caller of pcre_exec() can set a limit on the number of times the
  3665. internal match() function is called and on the maximum depth of recur-
  3666. sive calls. These facilities are provided to catch runaway matches that
  3667. are provoked by patterns with huge matching trees (a typical example is
  3668. a pattern with nested unlimited repeats) and to avoid running out of
  3669. system stack by too much recursion. When one of these limits is
  3670. reached, pcre_exec() gives an error return. The limits can also be set
  3671. by items at the start of the pattern of the form
  3672. (*LIMIT_MATCH=d)
  3673. (*LIMIT_RECURSION=d)
  3674. where d is any number of decimal digits. However, the value of the set-
  3675. ting must be less than the value set (or defaulted) by the caller of
  3676. pcre_exec() for it to have any effect. In other words, the pattern
  3677. writer can lower the limits set by the programmer, but not raise them.
  3678. If there is more than one setting of one of these limits, the lower
  3679. value is used.
  3680. EBCDIC CHARACTER CODES
  3681. PCRE can be compiled to run in an environment that uses EBCDIC as its
  3682. character code rather than ASCII or Unicode (typically a mainframe sys-
  3683. tem). In the sections below, character code values are ASCII or Uni-
  3684. code; in an EBCDIC environment these characters may have different code
  3685. values, and there are no code points greater than 255.
  3686. CHARACTERS AND METACHARACTERS
  3687. A regular expression is a pattern that is matched against a subject
  3688. string from left to right. Most characters stand for themselves in a
  3689. pattern, and match the corresponding characters in the subject. As a
  3690. trivial example, the pattern
  3691. The quick brown fox
  3692. matches a portion of a subject string that is identical to itself. When
  3693. caseless matching is specified (the PCRE_CASELESS option), letters are
  3694. matched independently of case. In a UTF mode, PCRE always understands
  3695. the concept of case for characters whose values are less than 128, so
  3696. caseless matching is always possible. For characters with higher val-
  3697. ues, the concept of case is supported if PCRE is compiled with Unicode
  3698. property support, but not otherwise. If you want to use caseless
  3699. matching for characters 128 and above, you must ensure that PCRE is
  3700. compiled with Unicode property support as well as with UTF support.
  3701. The power of regular expressions comes from the ability to include
  3702. alternatives and repetitions in the pattern. These are encoded in the
  3703. pattern by the use of metacharacters, which do not stand for themselves
  3704. but instead are interpreted in some special way.
  3705. There are two different sets of metacharacters: those that are recog-
  3706. nized anywhere in the pattern except within square brackets, and those
  3707. that are recognized within square brackets. Outside square brackets,
  3708. the metacharacters are as follows:
  3709. \ general escape character with several uses
  3710. ^ assert start of string (or line, in multiline mode)
  3711. $ assert end of string (or line, in multiline mode)
  3712. . match any character except newline (by default)
  3713. [ start character class definition
  3714. | start of alternative branch
  3715. ( start subpattern
  3716. ) end subpattern
  3717. ? extends the meaning of (
  3718. also 0 or 1 quantifier
  3719. also quantifier minimizer
  3720. * 0 or more quantifier
  3721. + 1 or more quantifier
  3722. also "possessive quantifier"
  3723. { start min/max quantifier
  3724. Part of a pattern that is in square brackets is called a "character
  3725. class". In a character class the only metacharacters are:
  3726. \ general escape character
  3727. ^ negate the class, but only if the first character
  3728. - indicates character range
  3729. [ POSIX character class (only if followed by POSIX
  3730. syntax)
  3731. ] terminates the character class
  3732. The following sections describe the use of each of the metacharacters.
  3733. BACKSLASH
  3734. The backslash character has several uses. Firstly, if it is followed by
  3735. a character that is not a number or a letter, it takes away any special
  3736. meaning that character may have. This use of backslash as an escape
  3737. character applies both inside and outside character classes.
  3738. For example, if you want to match a * character, you write \* in the
  3739. pattern. This escaping action applies whether or not the following
  3740. character would otherwise be interpreted as a metacharacter, so it is
  3741. always safe to precede a non-alphanumeric with backslash to specify
  3742. that it stands for itself. In particular, if you want to match a back-
  3743. slash, you write \\.
  3744. In a UTF mode, only ASCII numbers and letters have any special meaning
  3745. after a backslash. All other characters (in particular, those whose
  3746. codepoints are greater than 127) are treated as literals.
  3747. If a pattern is compiled with the PCRE_EXTENDED option, most white
  3748. space in the pattern (other than in a character class), and characters
  3749. between a # outside a character class and the next newline, inclusive,
  3750. are ignored. An escaping backslash can be used to include a white space
  3751. or # character as part of the pattern.
  3752. If you want to remove the special meaning from a sequence of charac-
  3753. ters, you can do so by putting them between \Q and \E. This is differ-
  3754. ent from Perl in that $ and @ are handled as literals in \Q...\E
  3755. sequences in PCRE, whereas in Perl, $ and @ cause variable interpola-
  3756. tion. Note the following examples:
  3757. Pattern PCRE matches Perl matches
  3758. \Qabc$xyz\E abc$xyz abc followed by the
  3759. contents of $xyz
  3760. \Qabc\$xyz\E abc\$xyz abc\$xyz
  3761. \Qabc\E\$\Qxyz\E abc$xyz abc$xyz
  3762. The \Q...\E sequence is recognized both inside and outside character
  3763. classes. An isolated \E that is not preceded by \Q is ignored. If \Q
  3764. is not followed by \E later in the pattern, the literal interpretation
  3765. continues to the end of the pattern (that is, \E is assumed at the
  3766. end). If the isolated \Q is inside a character class, this causes an
  3767. error, because the character class is not terminated.
  3768. Non-printing characters
  3769. A second use of backslash provides a way of encoding non-printing char-
  3770. acters in patterns in a visible manner. There is no restriction on the
  3771. appearance of non-printing characters, apart from the binary zero that
  3772. terminates a pattern, but when a pattern is being prepared by text
  3773. editing, it is often easier to use one of the following escape
  3774. sequences than the binary character it represents. In an ASCII or Uni-
  3775. code environment, these escapes are as follows:
  3776. \a alarm, that is, the BEL character (hex 07)
  3777. \cx "control-x", where x is any ASCII character
  3778. \e escape (hex 1B)
  3779. \f form feed (hex 0C)
  3780. \n linefeed (hex 0A)
  3781. \r carriage return (hex 0D)
  3782. \t tab (hex 09)
  3783. \0dd character with octal code 0dd
  3784. \ddd character with octal code ddd, or back reference
  3785. \o{ddd..} character with octal code ddd..
  3786. \xhh character with hex code hh
  3787. \x{hhh..} character with hex code hhh.. (non-JavaScript mode)
  3788. \uhhhh character with hex code hhhh (JavaScript mode only)
  3789. The precise effect of \cx on ASCII characters is as follows: if x is a
  3790. lower case letter, it is converted to upper case. Then bit 6 of the
  3791. character (hex 40) is inverted. Thus \cA to \cZ become hex 01 to hex 1A
  3792. (A is 41, Z is 5A), but \c{ becomes hex 3B ({ is 7B), and \c; becomes
  3793. hex 7B (; is 3B). If the data item (byte or 16-bit value) following \c
  3794. has a value greater than 127, a compile-time error occurs. This locks
  3795. out non-ASCII characters in all modes.
  3796. When PCRE is compiled in EBCDIC mode, \a, \e, \f, \n, \r, and \t gener-
  3797. ate the appropriate EBCDIC code values. The \c escape is processed as
  3798. specified for Perl in the perlebcdic document. The only characters that
  3799. are allowed after \c are A-Z, a-z, or one of @, [, \, ], ^, _, or ?.
  3800. Any other character provokes a compile-time error. The sequence \@
  3801. encodes character code 0; the letters (in either case) encode charac-
  3802. ters 1-26 (hex 01 to hex 1A); [, \, ], ^, and _ encode characters 27-31
  3803. (hex 1B to hex 1F), and \? becomes either 255 (hex FF) or 95 (hex 5F).
  3804. Thus, apart from \?, these escapes generate the same character code
  3805. values as they do in an ASCII environment, though the meanings of the
  3806. values mostly differ. For example, \G always generates code value 7,
  3807. which is BEL in ASCII but DEL in EBCDIC.
  3808. The sequence \? generates DEL (127, hex 7F) in an ASCII environment,
  3809. but because 127 is not a control character in EBCDIC, Perl makes it
  3810. generate the APC character. Unfortunately, there are several variants
  3811. of EBCDIC. In most of them the APC character has the value 255 (hex
  3812. FF), but in the one Perl calls POSIX-BC its value is 95 (hex 5F). If
  3813. certain other characters have POSIX-BC values, PCRE makes \? generate
  3814. 95; otherwise it generates 255.
  3815. After \0 up to two further octal digits are read. If there are fewer
  3816. than two digits, just those that are present are used. Thus the
  3817. sequence \0\x\015 specifies two binary zeros followed by a CR character
  3818. (code value 13). Make sure you supply two digits after the initial zero
  3819. if the pattern character that follows is itself an octal digit.
  3820. The escape \o must be followed by a sequence of octal digits, enclosed
  3821. in braces. An error occurs if this is not the case. This escape is a
  3822. recent addition to Perl; it provides way of specifying character code
  3823. points as octal numbers greater than 0777, and it also allows octal
  3824. numbers and back references to be unambiguously specified.
  3825. For greater clarity and unambiguity, it is best to avoid following \ by
  3826. a digit greater than zero. Instead, use \o{} or \x{} to specify charac-
  3827. ter numbers, and \g{} to specify back references. The following para-
  3828. graphs describe the old, ambiguous syntax.
  3829. The handling of a backslash followed by a digit other than 0 is compli-
  3830. cated, and Perl has changed in recent releases, causing PCRE also to
  3831. change. Outside a character class, PCRE reads the digit and any follow-
  3832. ing digits as a decimal number. If the number is less than 8, or if
  3833. there have been at least that many previous capturing left parentheses
  3834. in the expression, the entire sequence is taken as a back reference. A
  3835. description of how this works is given later, following the discussion
  3836. of parenthesized subpatterns.
  3837. Inside a character class, or if the decimal number following \ is
  3838. greater than 7 and there have not been that many capturing subpatterns,
  3839. PCRE handles \8 and \9 as the literal characters "8" and "9", and oth-
  3840. erwise re-reads up to three octal digits following the backslash, using
  3841. them to generate a data character. Any subsequent digits stand for
  3842. themselves. For example:
  3843. \040 is another way of writing an ASCII space
  3844. \40 is the same, provided there are fewer than 40
  3845. previous capturing subpatterns
  3846. \7 is always a back reference
  3847. \11 might be a back reference, or another way of
  3848. writing a tab
  3849. \011 is always a tab
  3850. \0113 is a tab followed by the character "3"
  3851. \113 might be a back reference, otherwise the
  3852. character with octal code 113
  3853. \377 might be a back reference, otherwise
  3854. the value 255 (decimal)
  3855. \81 is either a back reference, or the two
  3856. characters "8" and "1"
  3857. Note that octal values of 100 or greater that are specified using this
  3858. syntax must not be introduced by a leading zero, because no more than
  3859. three octal digits are ever read.
  3860. By default, after \x that is not followed by {, from zero to two hexa-
  3861. decimal digits are read (letters can be in upper or lower case). Any
  3862. number of hexadecimal digits may appear between \x{ and }. If a charac-
  3863. ter other than a hexadecimal digit appears between \x{ and }, or if
  3864. there is no terminating }, an error occurs.
  3865. If the PCRE_JAVASCRIPT_COMPAT option is set, the interpretation of \x
  3866. is as just described only when it is followed by two hexadecimal dig-
  3867. its. Otherwise, it matches a literal "x" character. In JavaScript
  3868. mode, support for code points greater than 256 is provided by \u, which
  3869. must be followed by four hexadecimal digits; otherwise it matches a
  3870. literal "u" character.
  3871. Characters whose value is less than 256 can be defined by either of the
  3872. two syntaxes for \x (or by \u in JavaScript mode). There is no differ-
  3873. ence in the way they are handled. For example, \xdc is exactly the same
  3874. as \x{dc} (or \u00dc in JavaScript mode).
  3875. Constraints on character values
  3876. Characters that are specified using octal or hexadecimal numbers are
  3877. limited to certain values, as follows:
  3878. 8-bit non-UTF mode less than 0x100
  3879. 8-bit UTF-8 mode less than 0x10ffff and a valid codepoint
  3880. 16-bit non-UTF mode less than 0x10000
  3881. 16-bit UTF-16 mode less than 0x10ffff and a valid codepoint
  3882. 32-bit non-UTF mode less than 0x100000000
  3883. 32-bit UTF-32 mode less than 0x10ffff and a valid codepoint
  3884. Invalid Unicode codepoints are the range 0xd800 to 0xdfff (the so-
  3885. called "surrogate" codepoints), and 0xffef.
  3886. Escape sequences in character classes
  3887. All the sequences that define a single character value can be used both
  3888. inside and outside character classes. In addition, inside a character
  3889. class, \b is interpreted as the backspace character (hex 08).
  3890. \N is not allowed in a character class. \B, \R, and \X are not special
  3891. inside a character class. Like other unrecognized escape sequences,
  3892. they are treated as the literal characters "B", "R", and "X" by
  3893. default, but cause an error if the PCRE_EXTRA option is set. Outside a
  3894. character class, these sequences have different meanings.
  3895. Unsupported escape sequences
  3896. In Perl, the sequences \l, \L, \u, and \U are recognized by its string
  3897. handler and used to modify the case of following characters. By
  3898. default, PCRE does not support these escape sequences. However, if the
  3899. PCRE_JAVASCRIPT_COMPAT option is set, \U matches a "U" character, and
  3900. \u can be used to define a character by code point, as described in the
  3901. previous section.
  3902. Absolute and relative back references
  3903. The sequence \g followed by an unsigned or a negative number, option-
  3904. ally enclosed in braces, is an absolute or relative back reference. A
  3905. named back reference can be coded as \g{name}. Back references are dis-
  3906. cussed later, following the discussion of parenthesized subpatterns.
  3907. Absolute and relative subroutine calls
  3908. For compatibility with Oniguruma, the non-Perl syntax \g followed by a
  3909. name or a number enclosed either in angle brackets or single quotes, is
  3910. an alternative syntax for referencing a subpattern as a "subroutine".
  3911. Details are discussed later. Note that \g{...} (Perl syntax) and
  3912. \g<...> (Oniguruma syntax) are not synonymous. The former is a back
  3913. reference; the latter is a subroutine call.
  3914. Generic character types
  3915. Another use of backslash is for specifying generic character types:
  3916. \d any decimal digit
  3917. \D any character that is not a decimal digit
  3918. \h any horizontal white space character
  3919. \H any character that is not a horizontal white space character
  3920. \s any white space character
  3921. \S any character that is not a white space character
  3922. \v any vertical white space character
  3923. \V any character that is not a vertical white space character
  3924. \w any "word" character
  3925. \W any "non-word" character
  3926. There is also the single sequence \N, which matches a non-newline char-
  3927. acter. This is the same as the "." metacharacter when PCRE_DOTALL is
  3928. not set. Perl also uses \N to match characters by name; PCRE does not
  3929. support this.
  3930. Each pair of lower and upper case escape sequences partitions the com-
  3931. plete set of characters into two disjoint sets. Any given character
  3932. matches one, and only one, of each pair. The sequences can appear both
  3933. inside and outside character classes. They each match one character of
  3934. the appropriate type. If the current matching point is at the end of
  3935. the subject string, all of them fail, because there is no character to
  3936. match.
  3937. For compatibility with Perl, \s did not used to match the VT character
  3938. (code 11), which made it different from the the POSIX "space" class.
  3939. However, Perl added VT at release 5.18, and PCRE followed suit at
  3940. release 8.34. The default \s characters are now HT (9), LF (10), VT
  3941. (11), FF (12), CR (13), and space (32), which are defined as white
  3942. space in the "C" locale. This list may vary if locale-specific matching
  3943. is taking place. For example, in some locales the "non-breaking space"
  3944. character (\xA0) is recognized as white space, and in others the VT
  3945. character is not.
  3946. A "word" character is an underscore or any character that is a letter
  3947. or digit. By default, the definition of letters and digits is con-
  3948. trolled by PCRE's low-valued character tables, and may vary if locale-
  3949. specific matching is taking place (see "Locale support" in the pcreapi
  3950. page). For example, in a French locale such as "fr_FR" in Unix-like
  3951. systems, or "french" in Windows, some character codes greater than 127
  3952. are used for accented letters, and these are then matched by \w. The
  3953. use of locales with Unicode is discouraged.
  3954. By default, characters whose code points are greater than 127 never
  3955. match \d, \s, or \w, and always match \D, \S, and \W, although this may
  3956. vary for characters in the range 128-255 when locale-specific matching
  3957. is happening. These escape sequences retain their original meanings
  3958. from before Unicode support was available, mainly for efficiency rea-
  3959. sons. If PCRE is compiled with Unicode property support, and the
  3960. PCRE_UCP option is set, the behaviour is changed so that Unicode prop-
  3961. erties are used to determine character types, as follows:
  3962. \d any character that matches \p{Nd} (decimal digit)
  3963. \s any character that matches \p{Z} or \h or \v
  3964. \w any character that matches \p{L} or \p{N}, plus underscore
  3965. The upper case escapes match the inverse sets of characters. Note that
  3966. \d matches only decimal digits, whereas \w matches any Unicode digit,
  3967. as well as any Unicode letter, and underscore. Note also that PCRE_UCP
  3968. affects \b, and \B because they are defined in terms of \w and \W.
  3969. Matching these sequences is noticeably slower when PCRE_UCP is set.
  3970. The sequences \h, \H, \v, and \V are features that were added to Perl
  3971. at release 5.10. In contrast to the other sequences, which match only
  3972. ASCII characters by default, these always match certain high-valued
  3973. code points, whether or not PCRE_UCP is set. The horizontal space char-
  3974. acters are:
  3975. U+0009 Horizontal tab (HT)
  3976. U+0020 Space
  3977. U+00A0 Non-break space
  3978. U+1680 Ogham space mark
  3979. U+180E Mongolian vowel separator
  3980. U+2000 En quad
  3981. U+2001 Em quad
  3982. U+2002 En space
  3983. U+2003 Em space
  3984. U+2004 Three-per-em space
  3985. U+2005 Four-per-em space
  3986. U+2006 Six-per-em space
  3987. U+2007 Figure space
  3988. U+2008 Punctuation space
  3989. U+2009 Thin space
  3990. U+200A Hair space
  3991. U+202F Narrow no-break space
  3992. U+205F Medium mathematical space
  3993. U+3000 Ideographic space
  3994. The vertical space characters are:
  3995. U+000A Linefeed (LF)
  3996. U+000B Vertical tab (VT)
  3997. U+000C Form feed (FF)
  3998. U+000D Carriage return (CR)
  3999. U+0085 Next line (NEL)
  4000. U+2028 Line separator
  4001. U+2029 Paragraph separator
  4002. In 8-bit, non-UTF-8 mode, only the characters with codepoints less than
  4003. 256 are relevant.
  4004. Newline sequences
  4005. Outside a character class, by default, the escape sequence \R matches
  4006. any Unicode newline sequence. In 8-bit non-UTF-8 mode \R is equivalent
  4007. to the following:
  4008. (?>\r\n|\n|\x0b|\f|\r|\x85)
  4009. This is an example of an "atomic group", details of which are given
  4010. below. This particular group matches either the two-character sequence
  4011. CR followed by LF, or one of the single characters LF (linefeed,
  4012. U+000A), VT (vertical tab, U+000B), FF (form feed, U+000C), CR (car-
  4013. riage return, U+000D), or NEL (next line, U+0085). The two-character
  4014. sequence is treated as a single unit that cannot be split.
  4015. In other modes, two additional characters whose codepoints are greater
  4016. than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa-
  4017. rator, U+2029). Unicode character property support is not needed for
  4018. these characters to be recognized.
  4019. It is possible to restrict \R to match only CR, LF, or CRLF (instead of
  4020. the complete set of Unicode line endings) by setting the option
  4021. PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched.
  4022. (BSR is an abbrevation for "backslash R".) This can be made the default
  4023. when PCRE is built; if this is the case, the other behaviour can be
  4024. requested via the PCRE_BSR_UNICODE option. It is also possible to
  4025. specify these settings by starting a pattern string with one of the
  4026. following sequences:
  4027. (*BSR_ANYCRLF) CR, LF, or CRLF only
  4028. (*BSR_UNICODE) any Unicode newline sequence
  4029. These override the default and the options given to the compiling func-
  4030. tion, but they can themselves be overridden by options given to a
  4031. matching function. Note that these special settings, which are not
  4032. Perl-compatible, are recognized only at the very start of a pattern,
  4033. and that they must be in upper case. If more than one of them is
  4034. present, the last one is used. They can be combined with a change of
  4035. newline convention; for example, a pattern can start with:
  4036. (*ANY)(*BSR_ANYCRLF)
  4037. They can also be combined with the (*UTF8), (*UTF16), (*UTF32), (*UTF)
  4038. or (*UCP) special sequences. Inside a character class, \R is treated as
  4039. an unrecognized escape sequence, and so matches the letter "R" by
  4040. default, but causes an error if PCRE_EXTRA is set.
  4041. Unicode character properties
  4042. When PCRE is built with Unicode character property support, three addi-
  4043. tional escape sequences that match characters with specific properties
  4044. are available. When in 8-bit non-UTF-8 mode, these sequences are of
  4045. course limited to testing characters whose codepoints are less than
  4046. 256, but they do work in this mode. The extra escape sequences are:
  4047. \p{xx} a character with the xx property
  4048. \P{xx} a character without the xx property
  4049. \X a Unicode extended grapheme cluster
  4050. The property names represented by xx above are limited to the Unicode
  4051. script names, the general category properties, "Any", which matches any
  4052. character (including newline), and some special PCRE properties
  4053. (described in the next section). Other Perl properties such as "InMu-
  4054. sicalSymbols" are not currently supported by PCRE. Note that \P{Any}
  4055. does not match any characters, so always causes a match failure.
  4056. Sets of Unicode characters are defined as belonging to certain scripts.
  4057. A character from one of these sets can be matched using a script name.
  4058. For example:
  4059. \p{Greek}
  4060. \P{Han}
  4061. Those that are not part of an identified script are lumped together as
  4062. "Common". The current list of scripts is:
  4063. Arabic, Armenian, Avestan, Balinese, Bamum, Bassa_Vah, Batak, Bengali,
  4064. Bopomofo, Brahmi, Braille, Buginese, Buhid, Canadian_Aboriginal, Car-
  4065. ian, Caucasian_Albanian, Chakma, Cham, Cherokee, Common, Coptic, Cunei-
  4066. form, Cypriot, Cyrillic, Deseret, Devanagari, Duployan, Egyptian_Hiero-
  4067. glyphs, Elbasan, Ethiopic, Georgian, Glagolitic, Gothic, Grantha,
  4068. Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hiragana,
  4069. Imperial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscrip-
  4070. tional_Parthian, Javanese, Kaithi, Kannada, Katakana, Kayah_Li,
  4071. Kharoshthi, Khmer, Khojki, Khudawadi, Lao, Latin, Lepcha, Limbu, Lin-
  4072. ear_A, Linear_B, Lisu, Lycian, Lydian, Mahajani, Malayalam, Mandaic,
  4073. Manichaean, Meetei_Mayek, Mende_Kikakui, Meroitic_Cursive,
  4074. Meroitic_Hieroglyphs, Miao, Modi, Mongolian, Mro, Myanmar, Nabataean,
  4075. New_Tai_Lue, Nko, Ogham, Ol_Chiki, Old_Italic, Old_North_Arabian,
  4076. Old_Permic, Old_Persian, Old_South_Arabian, Old_Turkic, Oriya, Osmanya,
  4077. Pahawh_Hmong, Palmyrene, Pau_Cin_Hau, Phags_Pa, Phoenician,
  4078. Psalter_Pahlavi, Rejang, Runic, Samaritan, Saurashtra, Sharada, Sha-
  4079. vian, Siddham, Sinhala, Sora_Sompeng, Sundanese, Syloti_Nagri, Syriac,
  4080. Tagalog, Tagbanwa, Tai_Le, Tai_Tham, Tai_Viet, Takri, Tamil, Telugu,
  4081. Thaana, Thai, Tibetan, Tifinagh, Tirhuta, Ugaritic, Vai, Warang_Citi,
  4082. Yi.
  4083. Each character has exactly one Unicode general category property, spec-
  4084. ified by a two-letter abbreviation. For compatibility with Perl, nega-
  4085. tion can be specified by including a circumflex between the opening
  4086. brace and the property name. For example, \p{^Lu} is the same as
  4087. \P{Lu}.
  4088. If only one letter is specified with \p or \P, it includes all the gen-
  4089. eral category properties that start with that letter. In this case, in
  4090. the absence of negation, the curly brackets in the escape sequence are
  4091. optional; these two examples have the same effect:
  4092. \p{L}
  4093. \pL
  4094. The following general category property codes are supported:
  4095. C Other
  4096. Cc Control
  4097. Cf Format
  4098. Cn Unassigned
  4099. Co Private use
  4100. Cs Surrogate
  4101. L Letter
  4102. Ll Lower case letter
  4103. Lm Modifier letter
  4104. Lo Other letter
  4105. Lt Title case letter
  4106. Lu Upper case letter
  4107. M Mark
  4108. Mc Spacing mark
  4109. Me Enclosing mark
  4110. Mn Non-spacing mark
  4111. N Number
  4112. Nd Decimal number
  4113. Nl Letter number
  4114. No Other number
  4115. P Punctuation
  4116. Pc Connector punctuation
  4117. Pd Dash punctuation
  4118. Pe Close punctuation
  4119. Pf Final punctuation
  4120. Pi Initial punctuation
  4121. Po Other punctuation
  4122. Ps Open punctuation
  4123. S Symbol
  4124. Sc Currency symbol
  4125. Sk Modifier symbol
  4126. Sm Mathematical symbol
  4127. So Other symbol
  4128. Z Separator
  4129. Zl Line separator
  4130. Zp Paragraph separator
  4131. Zs Space separator
  4132. The special property L& is also supported: it matches a character that
  4133. has the Lu, Ll, or Lt property, in other words, a letter that is not
  4134. classified as a modifier or "other".
  4135. The Cs (Surrogate) property applies only to characters in the range
  4136. U+D800 to U+DFFF. Such characters are not valid in Unicode strings and
  4137. so cannot be tested by PCRE, unless UTF validity checking has been
  4138. turned off (see the discussion of PCRE_NO_UTF8_CHECK,
  4139. PCRE_NO_UTF16_CHECK and PCRE_NO_UTF32_CHECK in the pcreapi page). Perl
  4140. does not support the Cs property.
  4141. The long synonyms for property names that Perl supports (such as
  4142. \p{Letter}) are not supported by PCRE, nor is it permitted to prefix
  4143. any of these properties with "Is".
  4144. No character that is in the Unicode table has the Cn (unassigned) prop-
  4145. erty. Instead, this property is assumed for any code point that is not
  4146. in the Unicode table.
  4147. Specifying caseless matching does not affect these escape sequences.
  4148. For example, \p{Lu} always matches only upper case letters. This is
  4149. different from the behaviour of current versions of Perl.
  4150. Matching characters by Unicode property is not fast, because PCRE has
  4151. to do a multistage table lookup in order to find a character's prop-
  4152. erty. That is why the traditional escape sequences such as \d and \w do
  4153. not use Unicode properties in PCRE by default, though you can make them
  4154. do so by setting the PCRE_UCP option or by starting the pattern with
  4155. (*UCP).
  4156. Extended grapheme clusters
  4157. The \X escape matches any number of Unicode characters that form an
  4158. "extended grapheme cluster", and treats the sequence as an atomic group
  4159. (see below). Up to and including release 8.31, PCRE matched an ear-
  4160. lier, simpler definition that was equivalent to
  4161. (?>\PM\pM*)
  4162. That is, it matched a character without the "mark" property, followed
  4163. by zero or more characters with the "mark" property. Characters with
  4164. the "mark" property are typically non-spacing accents that affect the
  4165. preceding character.
  4166. This simple definition was extended in Unicode to include more compli-
  4167. cated kinds of composite character by giving each character a grapheme
  4168. breaking property, and creating rules that use these properties to
  4169. define the boundaries of extended grapheme clusters. In releases of
  4170. PCRE later than 8.31, \X matches one of these clusters.
  4171. \X always matches at least one character. Then it decides whether to
  4172. add additional characters according to the following rules for ending a
  4173. cluster:
  4174. 1. End at the end of the subject string.
  4175. 2. Do not end between CR and LF; otherwise end after any control char-
  4176. acter.
  4177. 3. Do not break Hangul (a Korean script) syllable sequences. Hangul
  4178. characters are of five types: L, V, T, LV, and LVT. An L character may
  4179. be followed by an L, V, LV, or LVT character; an LV or V character may
  4180. be followed by a V or T character; an LVT or T character may be follwed
  4181. only by a T character.
  4182. 4. Do not end before extending characters or spacing marks. Characters
  4183. with the "mark" property always have the "extend" grapheme breaking
  4184. property.
  4185. 5. Do not end after prepend characters.
  4186. 6. Otherwise, end the cluster.
  4187. PCRE's additional properties
  4188. As well as the standard Unicode properties described above, PCRE sup-
  4189. ports four more that make it possible to convert traditional escape
  4190. sequences such as \w and \s to use Unicode properties. PCRE uses these
  4191. non-standard, non-Perl properties internally when PCRE_UCP is set. How-
  4192. ever, they may also be used explicitly. These properties are:
  4193. Xan Any alphanumeric character
  4194. Xps Any POSIX space character
  4195. Xsp Any Perl space character
  4196. Xwd Any Perl "word" character
  4197. Xan matches characters that have either the L (letter) or the N (num-
  4198. ber) property. Xps matches the characters tab, linefeed, vertical tab,
  4199. form feed, or carriage return, and any other character that has the Z
  4200. (separator) property. Xsp is the same as Xps; it used to exclude ver-
  4201. tical tab, for Perl compatibility, but Perl changed, and so PCRE fol-
  4202. lowed at release 8.34. Xwd matches the same characters as Xan, plus
  4203. underscore.
  4204. There is another non-standard property, Xuc, which matches any charac-
  4205. ter that can be represented by a Universal Character Name in C++ and
  4206. other programming languages. These are the characters $, @, ` (grave
  4207. accent), and all characters with Unicode code points greater than or
  4208. equal to U+00A0, except for the surrogates U+D800 to U+DFFF. Note that
  4209. most base (ASCII) characters are excluded. (Universal Character Names
  4210. are of the form \uHHHH or \UHHHHHHHH where H is a hexadecimal digit.
  4211. Note that the Xuc property does not match these sequences but the char-
  4212. acters that they represent.)
  4213. Resetting the match start
  4214. The escape sequence \K causes any previously matched characters not to
  4215. be included in the final matched sequence. For example, the pattern:
  4216. foo\Kbar
  4217. matches "foobar", but reports that it has matched "bar". This feature
  4218. is similar to a lookbehind assertion (described below). However, in
  4219. this case, the part of the subject before the real match does not have
  4220. to be of fixed length, as lookbehind assertions do. The use of \K does
  4221. not interfere with the setting of captured substrings. For example,
  4222. when the pattern
  4223. (foo)\Kbar
  4224. matches "foobar", the first substring is still set to "foo".
  4225. Perl documents that the use of \K within assertions is "not well
  4226. defined". In PCRE, \K is acted upon when it occurs inside positive
  4227. assertions, but is ignored in negative assertions. Note that when a
  4228. pattern such as (?=ab\K) matches, the reported start of the match can
  4229. be greater than the end of the match.
  4230. Simple assertions
  4231. The final use of backslash is for certain simple assertions. An asser-
  4232. tion specifies a condition that has to be met at a particular point in
  4233. a match, without consuming any characters from the subject string. The
  4234. use of subpatterns for more complicated assertions is described below.
  4235. The backslashed assertions are:
  4236. \b matches at a word boundary
  4237. \B matches when not at a word boundary
  4238. \A matches at the start of the subject
  4239. \Z matches at the end of the subject
  4240. also matches before a newline at the end of the subject
  4241. \z matches only at the end of the subject
  4242. \G matches at the first matching position in the subject
  4243. Inside a character class, \b has a different meaning; it matches the
  4244. backspace character. If any other of these assertions appears in a
  4245. character class, by default it matches the corresponding literal char-
  4246. acter (for example, \B matches the letter B). However, if the
  4247. PCRE_EXTRA option is set, an "invalid escape sequence" error is gener-
  4248. ated instead.
  4249. A word boundary is a position in the subject string where the current
  4250. character and the previous character do not both match \w or \W (i.e.
  4251. one matches \w and the other matches \W), or the start or end of the
  4252. string if the first or last character matches \w, respectively. In a
  4253. UTF mode, the meanings of \w and \W can be changed by setting the
  4254. PCRE_UCP option. When this is done, it also affects \b and \B. Neither
  4255. PCRE nor Perl has a separate "start of word" or "end of word" metase-
  4256. quence. However, whatever follows \b normally determines which it is.
  4257. For example, the fragment \ba matches "a" at the start of a word.
  4258. The \A, \Z, and \z assertions differ from the traditional circumflex
  4259. and dollar (described in the next section) in that they only ever match
  4260. at the very start and end of the subject string, whatever options are
  4261. set. Thus, they are independent of multiline mode. These three asser-
  4262. tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which
  4263. affect only the behaviour of the circumflex and dollar metacharacters.
  4264. However, if the startoffset argument of pcre_exec() is non-zero, indi-
  4265. cating that matching is to start at a point other than the beginning of
  4266. the subject, \A can never match. The difference between \Z and \z is
  4267. that \Z matches before a newline at the end of the string as well as at
  4268. the very end, whereas \z matches only at the end.
  4269. The \G assertion is true only when the current matching position is at
  4270. the start point of the match, as specified by the startoffset argument
  4271. of pcre_exec(). It differs from \A when the value of startoffset is
  4272. non-zero. By calling pcre_exec() multiple times with appropriate argu-
  4273. ments, you can mimic Perl's /g option, and it is in this kind of imple-
  4274. mentation where \G can be useful.
  4275. Note, however, that PCRE's interpretation of \G, as the start of the
  4276. current match, is subtly different from Perl's, which defines it as the
  4277. end of the previous match. In Perl, these can be different when the
  4278. previously matched string was empty. Because PCRE does just one match
  4279. at a time, it cannot reproduce this behaviour.
  4280. If all the alternatives of a pattern begin with \G, the expression is
  4281. anchored to the starting match position, and the "anchored" flag is set
  4282. in the compiled regular expression.
  4283. CIRCUMFLEX AND DOLLAR
  4284. The circumflex and dollar metacharacters are zero-width assertions.
  4285. That is, they test for a particular condition being true without con-
  4286. suming any characters from the subject string.
  4287. Outside a character class, in the default matching mode, the circumflex
  4288. character is an assertion that is true only if the current matching
  4289. point is at the start of the subject string. If the startoffset argu-
  4290. ment of pcre_exec() is non-zero, circumflex can never match if the
  4291. PCRE_MULTILINE option is unset. Inside a character class, circumflex
  4292. has an entirely different meaning (see below).
  4293. Circumflex need not be the first character of the pattern if a number
  4294. of alternatives are involved, but it should be the first thing in each
  4295. alternative in which it appears if the pattern is ever to match that
  4296. branch. If all possible alternatives start with a circumflex, that is,
  4297. if the pattern is constrained to match only at the start of the sub-
  4298. ject, it is said to be an "anchored" pattern. (There are also other
  4299. constructs that can cause a pattern to be anchored.)
  4300. The dollar character is an assertion that is true only if the current
  4301. matching point is at the end of the subject string, or immediately
  4302. before a newline at the end of the string (by default). Note, however,
  4303. that it does not actually match the newline. Dollar need not be the
  4304. last character of the pattern if a number of alternatives are involved,
  4305. but it should be the last item in any branch in which it appears. Dol-
  4306. lar has no special meaning in a character class.
  4307. The meaning of dollar can be changed so that it matches only at the
  4308. very end of the string, by setting the PCRE_DOLLAR_ENDONLY option at
  4309. compile time. This does not affect the \Z assertion.
  4310. The meanings of the circumflex and dollar characters are changed if the
  4311. PCRE_MULTILINE option is set. When this is the case, a circumflex
  4312. matches immediately after internal newlines as well as at the start of
  4313. the subject string. It does not match after a newline that ends the
  4314. string. A dollar matches before any newlines in the string, as well as
  4315. at the very end, when PCRE_MULTILINE is set. When newline is specified
  4316. as the two-character sequence CRLF, isolated CR and LF characters do
  4317. not indicate newlines.
  4318. For example, the pattern /^abc$/ matches the subject string "def\nabc"
  4319. (where \n represents a newline) in multiline mode, but not otherwise.
  4320. Consequently, patterns that are anchored in single line mode because
  4321. all branches start with ^ are not anchored in multiline mode, and a
  4322. match for circumflex is possible when the startoffset argument of
  4323. pcre_exec() is non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if
  4324. PCRE_MULTILINE is set.
  4325. Note that the sequences \A, \Z, and \z can be used to match the start
  4326. and end of the subject in both modes, and if all branches of a pattern
  4327. start with \A it is always anchored, whether or not PCRE_MULTILINE is
  4328. set.
  4329. FULL STOP (PERIOD, DOT) AND \N
  4330. Outside a character class, a dot in the pattern matches any one charac-
  4331. ter in the subject string except (by default) a character that signi-
  4332. fies the end of a line.
  4333. When a line ending is defined as a single character, dot never matches
  4334. that character; when the two-character sequence CRLF is used, dot does
  4335. not match CR if it is immediately followed by LF, but otherwise it
  4336. matches all characters (including isolated CRs and LFs). When any Uni-
  4337. code line endings are being recognized, dot does not match CR or LF or
  4338. any of the other line ending characters.
  4339. The behaviour of dot with regard to newlines can be changed. If the
  4340. PCRE_DOTALL option is set, a dot matches any one character, without
  4341. exception. If the two-character sequence CRLF is present in the subject
  4342. string, it takes two dots to match it.
  4343. The handling of dot is entirely independent of the handling of circum-
  4344. flex and dollar, the only relationship being that they both involve
  4345. newlines. Dot has no special meaning in a character class.
  4346. The escape sequence \N behaves like a dot, except that it is not
  4347. affected by the PCRE_DOTALL option. In other words, it matches any
  4348. character except one that signifies the end of a line. Perl also uses
  4349. \N to match characters by name; PCRE does not support this.
  4350. MATCHING A SINGLE DATA UNIT
  4351. Outside a character class, the escape sequence \C matches any one data
  4352. unit, whether or not a UTF mode is set. In the 8-bit library, one data
  4353. unit is one byte; in the 16-bit library it is a 16-bit unit; in the
  4354. 32-bit library it is a 32-bit unit. Unlike a dot, \C always matches
  4355. line-ending characters. The feature is provided in Perl in order to
  4356. match individual bytes in UTF-8 mode, but it is unclear how it can use-
  4357. fully be used. Because \C breaks up characters into individual data
  4358. units, matching one unit with \C in a UTF mode means that the rest of
  4359. the string may start with a malformed UTF character. This has undefined
  4360. results, because PCRE assumes that it is dealing with valid UTF strings
  4361. (and by default it checks this at the start of processing unless the
  4362. PCRE_NO_UTF8_CHECK, PCRE_NO_UTF16_CHECK or PCRE_NO_UTF32_CHECK option
  4363. is used).
  4364. PCRE does not allow \C to appear in lookbehind assertions (described
  4365. below) in a UTF mode, because this would make it impossible to calcu-
  4366. late the length of the lookbehind.
  4367. In general, the \C escape sequence is best avoided. However, one way of
  4368. using it that avoids the problem of malformed UTF characters is to use
  4369. a lookahead to check the length of the next character, as in this pat-
  4370. tern, which could be used with a UTF-8 string (ignore white space and
  4371. line breaks):
  4372. (?| (?=[\x00-\x7f])(\C) |
  4373. (?=[\x80-\x{7ff}])(\C)(\C) |
  4374. (?=[\x{800}-\x{ffff}])(\C)(\C)(\C) |
  4375. (?=[\x{10000}-\x{1fffff}])(\C)(\C)(\C)(\C))
  4376. A group that starts with (?| resets the capturing parentheses numbers
  4377. in each alternative (see "Duplicate Subpattern Numbers" below). The
  4378. assertions at the start of each branch check the next UTF-8 character
  4379. for values whose encoding uses 1, 2, 3, or 4 bytes, respectively. The
  4380. character's individual bytes are then captured by the appropriate num-
  4381. ber of groups.
  4382. SQUARE BRACKETS AND CHARACTER CLASSES
  4383. An opening square bracket introduces a character class, terminated by a
  4384. closing square bracket. A closing square bracket on its own is not spe-
  4385. cial by default. However, if the PCRE_JAVASCRIPT_COMPAT option is set,
  4386. a lone closing square bracket causes a compile-time error. If a closing
  4387. square bracket is required as a member of the class, it should be the
  4388. first data character in the class (after an initial circumflex, if
  4389. present) or escaped with a backslash.
  4390. A character class matches a single character in the subject. In a UTF
  4391. mode, the character may be more than one data unit long. A matched
  4392. character must be in the set of characters defined by the class, unless
  4393. the first character in the class definition is a circumflex, in which
  4394. case the subject character must not be in the set defined by the class.
  4395. If a circumflex is actually required as a member of the class, ensure
  4396. it is not the first character, or escape it with a backslash.
  4397. For example, the character class [aeiou] matches any lower case vowel,
  4398. while [^aeiou] matches any character that is not a lower case vowel.
  4399. Note that a circumflex is just a convenient notation for specifying the
  4400. characters that are in the class by enumerating those that are not. A
  4401. class that starts with a circumflex is not an assertion; it still con-
  4402. sumes a character from the subject string, and therefore it fails if
  4403. the current pointer is at the end of the string.
  4404. In UTF-8 (UTF-16, UTF-32) mode, characters with values greater than 255
  4405. (0xffff) can be included in a class as a literal string of data units,
  4406. or by using the \x{ escaping mechanism.
  4407. When caseless matching is set, any letters in a class represent both
  4408. their upper case and lower case versions, so for example, a caseless
  4409. [aeiou] matches "A" as well as "a", and a caseless [^aeiou] does not
  4410. match "A", whereas a caseful version would. In a UTF mode, PCRE always
  4411. understands the concept of case for characters whose values are less
  4412. than 128, so caseless matching is always possible. For characters with
  4413. higher values, the concept of case is supported if PCRE is compiled
  4414. with Unicode property support, but not otherwise. If you want to use
  4415. caseless matching in a UTF mode for characters 128 and above, you must
  4416. ensure that PCRE is compiled with Unicode property support as well as
  4417. with UTF support.
  4418. Characters that might indicate line breaks are never treated in any
  4419. special way when matching character classes, whatever line-ending
  4420. sequence is in use, and whatever setting of the PCRE_DOTALL and
  4421. PCRE_MULTILINE options is used. A class such as [^a] always matches one
  4422. of these characters.
  4423. The minus (hyphen) character can be used to specify a range of charac-
  4424. ters in a character class. For example, [d-m] matches any letter
  4425. between d and m, inclusive. If a minus character is required in a
  4426. class, it must be escaped with a backslash or appear in a position
  4427. where it cannot be interpreted as indicating a range, typically as the
  4428. first or last character in the class, or immediately after a range. For
  4429. example, [b-d-z] matches letters in the range b to d, a hyphen charac-
  4430. ter, or z.
  4431. It is not possible to have the literal character "]" as the end charac-
  4432. ter of a range. A pattern such as [W-]46] is interpreted as a class of
  4433. two characters ("W" and "-") followed by a literal string "46]", so it
  4434. would match "W46]" or "-46]". However, if the "]" is escaped with a
  4435. backslash it is interpreted as the end of range, so [W-\]46] is inter-
  4436. preted as a class containing a range followed by two other characters.
  4437. The octal or hexadecimal representation of "]" can also be used to end
  4438. a range.
  4439. An error is generated if a POSIX character class (see below) or an
  4440. escape sequence other than one that defines a single character appears
  4441. at a point where a range ending character is expected. For example,
  4442. [z-\xff] is valid, but [A-\d] and [A-[:digit:]] are not.
  4443. Ranges operate in the collating sequence of character values. They can
  4444. also be used for characters specified numerically, for example
  4445. [\000-\037]. Ranges can include any characters that are valid for the
  4446. current mode.
  4447. If a range that includes letters is used when caseless matching is set,
  4448. it matches the letters in either case. For example, [W-c] is equivalent
  4449. to [][\\^_`wxyzabc], matched caselessly, and in a non-UTF mode, if
  4450. character tables for a French locale are in use, [\xc8-\xcb] matches
  4451. accented E characters in both cases. In UTF modes, PCRE supports the
  4452. concept of case for characters with values greater than 128 only when
  4453. it is compiled with Unicode property support.
  4454. The character escape sequences \d, \D, \h, \H, \p, \P, \s, \S, \v, \V,
  4455. \w, and \W may appear in a character class, and add the characters that
  4456. they match to the class. For example, [\dABCDEF] matches any hexadeci-
  4457. mal digit. In UTF modes, the PCRE_UCP option affects the meanings of
  4458. \d, \s, \w and their upper case partners, just as it does when they
  4459. appear outside a character class, as described in the section entitled
  4460. "Generic character types" above. The escape sequence \b has a different
  4461. meaning inside a character class; it matches the backspace character.
  4462. The sequences \B, \N, \R, and \X are not special inside a character
  4463. class. Like any other unrecognized escape sequences, they are treated
  4464. as the literal characters "B", "N", "R", and "X" by default, but cause
  4465. an error if the PCRE_EXTRA option is set.
  4466. A circumflex can conveniently be used with the upper case character
  4467. types to specify a more restricted set of characters than the matching
  4468. lower case type. For example, the class [^\W_] matches any letter or
  4469. digit, but not underscore, whereas [\w] includes underscore. A positive
  4470. character class should be read as "something OR something OR ..." and a
  4471. negative class as "NOT something AND NOT something AND NOT ...".
  4472. The only metacharacters that are recognized in character classes are
  4473. backslash, hyphen (only where it can be interpreted as specifying a
  4474. range), circumflex (only at the start), opening square bracket (only
  4475. when it can be interpreted as introducing a POSIX class name, or for a
  4476. special compatibility feature - see the next two sections), and the
  4477. terminating closing square bracket. However, escaping other non-
  4478. alphanumeric characters does no harm.
  4479. POSIX CHARACTER CLASSES
  4480. Perl supports the POSIX notation for character classes. This uses names
  4481. enclosed by [: and :] within the enclosing square brackets. PCRE also
  4482. supports this notation. For example,
  4483. [01[:alpha:]%]
  4484. matches "0", "1", any alphabetic character, or "%". The supported class
  4485. names are:
  4486. alnum letters and digits
  4487. alpha letters
  4488. ascii character codes 0 - 127
  4489. blank space or tab only
  4490. cntrl control characters
  4491. digit decimal digits (same as \d)
  4492. graph printing characters, excluding space
  4493. lower lower case letters
  4494. print printing characters, including space
  4495. punct printing characters, excluding letters and digits and space
  4496. space white space (the same as \s from PCRE 8.34)
  4497. upper upper case letters
  4498. word "word" characters (same as \w)
  4499. xdigit hexadecimal digits
  4500. The default "space" characters are HT (9), LF (10), VT (11), FF (12),
  4501. CR (13), and space (32). If locale-specific matching is taking place,
  4502. the list of space characters may be different; there may be fewer or
  4503. more of them. "Space" used to be different to \s, which did not include
  4504. VT, for Perl compatibility. However, Perl changed at release 5.18, and
  4505. PCRE followed at release 8.34. "Space" and \s now match the same set
  4506. of characters.
  4507. The name "word" is a Perl extension, and "blank" is a GNU extension
  4508. from Perl 5.8. Another Perl extension is negation, which is indicated
  4509. by a ^ character after the colon. For example,
  4510. [12[:^digit:]]
  4511. matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the
  4512. POSIX syntax [.ch.] and [=ch=] where "ch" is a "collating element", but
  4513. these are not supported, and an error is given if they are encountered.
  4514. By default, characters with values greater than 128 do not match any of
  4515. the POSIX character classes. However, if the PCRE_UCP option is passed
  4516. to pcre_compile(), some of the classes are changed so that Unicode
  4517. character properties are used. This is achieved by replacing certain
  4518. POSIX classes by other sequences, as follows:
  4519. [:alnum:] becomes \p{Xan}
  4520. [:alpha:] becomes \p{L}
  4521. [:blank:] becomes \h
  4522. [:digit:] becomes \p{Nd}
  4523. [:lower:] becomes \p{Ll}
  4524. [:space:] becomes \p{Xps}
  4525. [:upper:] becomes \p{Lu}
  4526. [:word:] becomes \p{Xwd}
  4527. Negated versions, such as [:^alpha:] use \P instead of \p. Three other
  4528. POSIX classes are handled specially in UCP mode:
  4529. [:graph:] This matches characters that have glyphs that mark the page
  4530. when printed. In Unicode property terms, it matches all char-
  4531. acters with the L, M, N, P, S, or Cf properties, except for:
  4532. U+061C Arabic Letter Mark
  4533. U+180E Mongolian Vowel Separator
  4534. U+2066 - U+2069 Various "isolate"s
  4535. [:print:] This matches the same characters as [:graph:] plus space
  4536. characters that are not controls, that is, characters with
  4537. the Zs property.
  4538. [:punct:] This matches all characters that have the Unicode P (punctua-
  4539. tion) property, plus those characters whose code points are
  4540. less than 128 that have the S (Symbol) property.
  4541. The other POSIX classes are unchanged, and match only characters with
  4542. code points less than 128.
  4543. COMPATIBILITY FEATURE FOR WORD BOUNDARIES
  4544. In the POSIX.2 compliant library that was included in 4.4BSD Unix, the
  4545. ugly syntax [[:<:]] and [[:>:]] is used for matching "start of word"
  4546. and "end of word". PCRE treats these items as follows:
  4547. [[:<:]] is converted to \b(?=\w)
  4548. [[:>:]] is converted to \b(?<=\w)
  4549. Only these exact character sequences are recognized. A sequence such as
  4550. [a[:<:]b] provokes error for an unrecognized POSIX class name. This
  4551. support is not compatible with Perl. It is provided to help migrations
  4552. from other environments, and is best not used in any new patterns. Note
  4553. that \b matches at the start and the end of a word (see "Simple asser-
  4554. tions" above), and in a Perl-style pattern the preceding or following
  4555. character normally shows which is wanted, without the need for the
  4556. assertions that are used above in order to give exactly the POSIX be-
  4557. haviour.
  4558. VERTICAL BAR
  4559. Vertical bar characters are used to separate alternative patterns. For
  4560. example, the pattern
  4561. gilbert|sullivan
  4562. matches either "gilbert" or "sullivan". Any number of alternatives may
  4563. appear, and an empty alternative is permitted (matching the empty
  4564. string). The matching process tries each alternative in turn, from left
  4565. to right, and the first one that succeeds is used. If the alternatives
  4566. are within a subpattern (defined below), "succeeds" means matching the
  4567. rest of the main pattern as well as the alternative in the subpattern.
  4568. INTERNAL OPTION SETTING
  4569. The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
  4570. PCRE_EXTENDED options (which are Perl-compatible) can be changed from
  4571. within the pattern by a sequence of Perl option letters enclosed
  4572. between "(?" and ")". The option letters are
  4573. i for PCRE_CASELESS
  4574. m for PCRE_MULTILINE
  4575. s for PCRE_DOTALL
  4576. x for PCRE_EXTENDED
  4577. For example, (?im) sets caseless, multiline matching. It is also possi-
  4578. ble to unset these options by preceding the letter with a hyphen, and a
  4579. combined setting and unsetting such as (?im-sx), which sets PCRE_CASE-
  4580. LESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED,
  4581. is also permitted. If a letter appears both before and after the
  4582. hyphen, the option is unset.
  4583. The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA
  4584. can be changed in the same way as the Perl-compatible options by using
  4585. the characters J, U and X respectively.
  4586. When one of these option changes occurs at top level (that is, not
  4587. inside subpattern parentheses), the change applies to the remainder of
  4588. the pattern that follows. If the change is placed right at the start of
  4589. a pattern, PCRE extracts it into the global options (and it will there-
  4590. fore show up in data extracted by the pcre_fullinfo() function).
  4591. An option change within a subpattern (see below for a description of
  4592. subpatterns) affects only that part of the subpattern that follows it,
  4593. so
  4594. (a(?i)b)c
  4595. matches abc and aBc and no other strings (assuming PCRE_CASELESS is not
  4596. used). By this means, options can be made to have different settings
  4597. in different parts of the pattern. Any changes made in one alternative
  4598. do carry on into subsequent branches within the same subpattern. For
  4599. example,
  4600. (a(?i)b|c)
  4601. matches "ab", "aB", "c", and "C", even though when matching "C" the
  4602. first branch is abandoned before the option setting. This is because
  4603. the effects of option settings happen at compile time. There would be
  4604. some very weird behaviour otherwise.
  4605. Note: There are other PCRE-specific options that can be set by the
  4606. application when the compiling or matching functions are called. In
  4607. some cases the pattern can contain special leading sequences such as
  4608. (*CRLF) to override what the application has set or what has been
  4609. defaulted. Details are given in the section entitled "Newline
  4610. sequences" above. There are also the (*UTF8), (*UTF16),(*UTF32), and
  4611. (*UCP) leading sequences that can be used to set UTF and Unicode prop-
  4612. erty modes; they are equivalent to setting the PCRE_UTF8, PCRE_UTF16,
  4613. PCRE_UTF32 and the PCRE_UCP options, respectively. The (*UTF) sequence
  4614. is a generic version that can be used with any of the libraries. How-
  4615. ever, the application can set the PCRE_NEVER_UTF option, which locks
  4616. out the use of the (*UTF) sequences.
  4617. SUBPATTERNS
  4618. Subpatterns are delimited by parentheses (round brackets), which can be
  4619. nested. Turning part of a pattern into a subpattern does two things:
  4620. 1. It localizes a set of alternatives. For example, the pattern
  4621. cat(aract|erpillar|)
  4622. matches "cataract", "caterpillar", or "cat". Without the parentheses,
  4623. it would match "cataract", "erpillar" or an empty string.
  4624. 2. It sets up the subpattern as a capturing subpattern. This means
  4625. that, when the whole pattern matches, that portion of the subject
  4626. string that matched the subpattern is passed back to the caller via the
  4627. ovector argument of the matching function. (This applies only to the
  4628. traditional matching functions; the DFA matching functions do not sup-
  4629. port capturing.)
  4630. Opening parentheses are counted from left to right (starting from 1) to
  4631. obtain numbers for the capturing subpatterns. For example, if the
  4632. string "the red king" is matched against the pattern
  4633. the ((red|white) (king|queen))
  4634. the captured substrings are "red king", "red", and "king", and are num-
  4635. bered 1, 2, and 3, respectively.
  4636. The fact that plain parentheses fulfil two functions is not always
  4637. helpful. There are often times when a grouping subpattern is required
  4638. without a capturing requirement. If an opening parenthesis is followed
  4639. by a question mark and a colon, the subpattern does not do any captur-
  4640. ing, and is not counted when computing the number of any subsequent
  4641. capturing subpatterns. For example, if the string "the white queen" is
  4642. matched against the pattern
  4643. the ((?:red|white) (king|queen))
  4644. the captured substrings are "white queen" and "queen", and are numbered
  4645. 1 and 2. The maximum number of capturing subpatterns is 65535.
  4646. As a convenient shorthand, if any option settings are required at the
  4647. start of a non-capturing subpattern, the option letters may appear
  4648. between the "?" and the ":". Thus the two patterns
  4649. (?i:saturday|sunday)
  4650. (?:(?i)saturday|sunday)
  4651. match exactly the same set of strings. Because alternative branches are
  4652. tried from left to right, and options are not reset until the end of
  4653. the subpattern is reached, an option setting in one branch does affect
  4654. subsequent branches, so the above patterns match "SUNDAY" as well as
  4655. "Saturday".
  4656. DUPLICATE SUBPATTERN NUMBERS
  4657. Perl 5.10 introduced a feature whereby each alternative in a subpattern
  4658. uses the same numbers for its capturing parentheses. Such a subpattern
  4659. starts with (?| and is itself a non-capturing subpattern. For example,
  4660. consider this pattern:
  4661. (?|(Sat)ur|(Sun))day
  4662. Because the two alternatives are inside a (?| group, both sets of cap-
  4663. turing parentheses are numbered one. Thus, when the pattern matches,
  4664. you can look at captured substring number one, whichever alternative
  4665. matched. This construct is useful when you want to capture part, but
  4666. not all, of one of a number of alternatives. Inside a (?| group, paren-
  4667. theses are numbered as usual, but the number is reset at the start of
  4668. each branch. The numbers of any capturing parentheses that follow the
  4669. subpattern start after the highest number used in any branch. The fol-
  4670. lowing example is taken from the Perl documentation. The numbers under-
  4671. neath show in which buffer the captured content will be stored.
  4672. # before ---------------branch-reset----------- after
  4673. / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
  4674. # 1 2 2 3 2 3 4
  4675. A back reference to a numbered subpattern uses the most recent value
  4676. that is set for that number by any subpattern. The following pattern
  4677. matches "abcabc" or "defdef":
  4678. /(?|(abc)|(def))\1/
  4679. In contrast, a subroutine call to a numbered subpattern always refers
  4680. to the first one in the pattern with the given number. The following
  4681. pattern matches "abcabc" or "defabc":
  4682. /(?|(abc)|(def))(?1)/
  4683. If a condition test for a subpattern's having matched refers to a non-
  4684. unique number, the test is true if any of the subpatterns of that num-
  4685. ber have matched.
  4686. An alternative approach to using this "branch reset" feature is to use
  4687. duplicate named subpatterns, as described in the next section.
  4688. NAMED SUBPATTERNS
  4689. Identifying capturing parentheses by number is simple, but it can be
  4690. very hard to keep track of the numbers in complicated regular expres-
  4691. sions. Furthermore, if an expression is modified, the numbers may
  4692. change. To help with this difficulty, PCRE supports the naming of sub-
  4693. patterns. This feature was not added to Perl until release 5.10. Python
  4694. had the feature earlier, and PCRE introduced it at release 4.0, using
  4695. the Python syntax. PCRE now supports both the Perl and the Python syn-
  4696. tax. Perl allows identically numbered subpatterns to have different
  4697. names, but PCRE does not.
  4698. In PCRE, a subpattern can be named in one of three ways: (?<name>...)
  4699. or (?'name'...) as in Perl, or (?P<name>...) as in Python. References
  4700. to capturing parentheses from other parts of the pattern, such as back
  4701. references, recursion, and conditions, can be made by name as well as
  4702. by number.
  4703. Names consist of up to 32 alphanumeric characters and underscores, but
  4704. must start with a non-digit. Named capturing parentheses are still
  4705. allocated numbers as well as names, exactly as if the names were not
  4706. present. The PCRE API provides function calls for extracting the name-
  4707. to-number translation table from a compiled pattern. There is also a
  4708. convenience function for extracting a captured substring by name.
  4709. By default, a name must be unique within a pattern, but it is possible
  4710. to relax this constraint by setting the PCRE_DUPNAMES option at compile
  4711. time. (Duplicate names are also always permitted for subpatterns with
  4712. the same number, set up as described in the previous section.) Dupli-
  4713. cate names can be useful for patterns where only one instance of the
  4714. named parentheses can match. Suppose you want to match the name of a
  4715. weekday, either as a 3-letter abbreviation or as the full name, and in
  4716. both cases you want to extract the abbreviation. This pattern (ignoring
  4717. the line breaks) does the job:
  4718. (?<DN>Mon|Fri|Sun)(?:day)?|
  4719. (?<DN>Tue)(?:sday)?|
  4720. (?<DN>Wed)(?:nesday)?|
  4721. (?<DN>Thu)(?:rsday)?|
  4722. (?<DN>Sat)(?:urday)?
  4723. There are five capturing substrings, but only one is ever set after a
  4724. match. (An alternative way of solving this problem is to use a "branch
  4725. reset" subpattern, as described in the previous section.)
  4726. The convenience function for extracting the data by name returns the
  4727. substring for the first (and in this example, the only) subpattern of
  4728. that name that matched. This saves searching to find which numbered
  4729. subpattern it was.
  4730. If you make a back reference to a non-unique named subpattern from
  4731. elsewhere in the pattern, the subpatterns to which the name refers are
  4732. checked in the order in which they appear in the overall pattern. The
  4733. first one that is set is used for the reference. For example, this pat-
  4734. tern matches both "foofoo" and "barbar" but not "foobar" or "barfoo":
  4735. (?:(?<n>foo)|(?<n>bar))\k<n>
  4736. If you make a subroutine call to a non-unique named subpattern, the one
  4737. that corresponds to the first occurrence of the name is used. In the
  4738. absence of duplicate numbers (see the previous section) this is the one
  4739. with the lowest number.
  4740. If you use a named reference in a condition test (see the section about
  4741. conditions below), either to check whether a subpattern has matched, or
  4742. to check for recursion, all subpatterns with the same name are tested.
  4743. If the condition is true for any one of them, the overall condition is
  4744. true. This is the same behaviour as testing by number. For further
  4745. details of the interfaces for handling named subpatterns, see the
  4746. pcreapi documentation.
  4747. Warning: You cannot use different names to distinguish between two sub-
  4748. patterns with the same number because PCRE uses only the numbers when
  4749. matching. For this reason, an error is given at compile time if differ-
  4750. ent names are given to subpatterns with the same number. However, you
  4751. can always give the same name to subpatterns with the same number, even
  4752. when PCRE_DUPNAMES is not set.
  4753. REPETITION
  4754. Repetition is specified by quantifiers, which can follow any of the
  4755. following items:
  4756. a literal data character
  4757. the dot metacharacter
  4758. the \C escape sequence
  4759. the \X escape sequence
  4760. the \R escape sequence
  4761. an escape such as \d or \pL that matches a single character
  4762. a character class
  4763. a back reference (see next section)
  4764. a parenthesized subpattern (including assertions)
  4765. a subroutine call to a subpattern (recursive or otherwise)
  4766. The general repetition quantifier specifies a minimum and maximum num-
  4767. ber of permitted matches, by giving the two numbers in curly brackets
  4768. (braces), separated by a comma. The numbers must be less than 65536,
  4769. and the first must be less than or equal to the second. For example:
  4770. z{2,4}
  4771. matches "zz", "zzz", or "zzzz". A closing brace on its own is not a
  4772. special character. If the second number is omitted, but the comma is
  4773. present, there is no upper limit; if the second number and the comma
  4774. are both omitted, the quantifier specifies an exact number of required
  4775. matches. Thus
  4776. [aeiou]{3,}
  4777. matches at least 3 successive vowels, but may match many more, while
  4778. \d{8}
  4779. matches exactly 8 digits. An opening curly bracket that appears in a
  4780. position where a quantifier is not allowed, or one that does not match
  4781. the syntax of a quantifier, is taken as a literal character. For exam-
  4782. ple, {,6} is not a quantifier, but a literal string of four characters.
  4783. In UTF modes, quantifiers apply to characters rather than to individual
  4784. data units. Thus, for example, \x{100}{2} matches two characters, each
  4785. of which is represented by a two-byte sequence in a UTF-8 string. Simi-
  4786. larly, \X{3} matches three Unicode extended grapheme clusters, each of
  4787. which may be several data units long (and they may be of different
  4788. lengths).
  4789. The quantifier {0} is permitted, causing the expression to behave as if
  4790. the previous item and the quantifier were not present. This may be use-
  4791. ful for subpatterns that are referenced as subroutines from elsewhere
  4792. in the pattern (but see also the section entitled "Defining subpatterns
  4793. for use by reference only" below). Items other than subpatterns that
  4794. have a {0} quantifier are omitted from the compiled pattern.
  4795. For convenience, the three most common quantifiers have single-charac-
  4796. ter abbreviations:
  4797. * is equivalent to {0,}
  4798. + is equivalent to {1,}
  4799. ? is equivalent to {0,1}
  4800. It is possible to construct infinite loops by following a subpattern
  4801. that can match no characters with a quantifier that has no upper limit,
  4802. for example:
  4803. (a?)*
  4804. Earlier versions of Perl and PCRE used to give an error at compile time
  4805. for such patterns. However, because there are cases where this can be
  4806. useful, such patterns are now accepted, but if any repetition of the
  4807. subpattern does in fact match no characters, the loop is forcibly bro-
  4808. ken.
  4809. By default, the quantifiers are "greedy", that is, they match as much
  4810. as possible (up to the maximum number of permitted times), without
  4811. causing the rest of the pattern to fail. The classic example of where
  4812. this gives problems is in trying to match comments in C programs. These
  4813. appear between /* and */ and within the comment, individual * and /
  4814. characters may appear. An attempt to match C comments by applying the
  4815. pattern
  4816. /\*.*\*/
  4817. to the string
  4818. /* first comment */ not comment /* second comment */
  4819. fails, because it matches the entire string owing to the greediness of
  4820. the .* item.
  4821. However, if a quantifier is followed by a question mark, it ceases to
  4822. be greedy, and instead matches the minimum number of times possible, so
  4823. the pattern
  4824. /\*.*?\*/
  4825. does the right thing with the C comments. The meaning of the various
  4826. quantifiers is not otherwise changed, just the preferred number of
  4827. matches. Do not confuse this use of question mark with its use as a
  4828. quantifier in its own right. Because it has two uses, it can sometimes
  4829. appear doubled, as in
  4830. \d??\d
  4831. which matches one digit by preference, but can match two if that is the
  4832. only way the rest of the pattern matches.
  4833. If the PCRE_UNGREEDY option is set (an option that is not available in
  4834. Perl), the quantifiers are not greedy by default, but individual ones
  4835. can be made greedy by following them with a question mark. In other
  4836. words, it inverts the default behaviour.
  4837. When a parenthesized subpattern is quantified with a minimum repeat
  4838. count that is greater than 1 or with a limited maximum, more memory is
  4839. required for the compiled pattern, in proportion to the size of the
  4840. minimum or maximum.
  4841. If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv-
  4842. alent to Perl's /s) is set, thus allowing the dot to match newlines,
  4843. the pattern is implicitly anchored, because whatever follows will be
  4844. tried against every character position in the subject string, so there
  4845. is no point in retrying the overall match at any position after the
  4846. first. PCRE normally treats such a pattern as though it were preceded
  4847. by \A.
  4848. In cases where it is known that the subject string contains no new-
  4849. lines, it is worth setting PCRE_DOTALL in order to obtain this opti-
  4850. mization, or alternatively using ^ to indicate anchoring explicitly.
  4851. However, there are some cases where the optimization cannot be used.
  4852. When .* is inside capturing parentheses that are the subject of a back
  4853. reference elsewhere in the pattern, a match at the start may fail where
  4854. a later one succeeds. Consider, for example:
  4855. (.*)abc\1
  4856. If the subject is "xyz123abc123" the match point is the fourth charac-
  4857. ter. For this reason, such a pattern is not implicitly anchored.
  4858. Another case where implicit anchoring is not applied is when the lead-
  4859. ing .* is inside an atomic group. Once again, a match at the start may
  4860. fail where a later one succeeds. Consider this pattern:
  4861. (?>.*?a)b
  4862. It matches "ab" in the subject "aab". The use of the backtracking con-
  4863. trol verbs (*PRUNE) and (*SKIP) also disable this optimization.
  4864. When a capturing subpattern is repeated, the value captured is the sub-
  4865. string that matched the final iteration. For example, after
  4866. (tweedle[dume]{3}\s*)+
  4867. has matched "tweedledum tweedledee" the value of the captured substring
  4868. is "tweedledee". However, if there are nested capturing subpatterns,
  4869. the corresponding captured values may have been set in previous itera-
  4870. tions. For example, after
  4871. /(a|(b))+/
  4872. matches "aba" the value of the second captured substring is "b".
  4873. ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS
  4874. With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
  4875. repetition, failure of what follows normally causes the repeated item
  4876. to be re-evaluated to see if a different number of repeats allows the
  4877. rest of the pattern to match. Sometimes it is useful to prevent this,
  4878. either to change the nature of the match, or to cause it fail earlier
  4879. than it otherwise might, when the author of the pattern knows there is
  4880. no point in carrying on.
  4881. Consider, for example, the pattern \d+foo when applied to the subject
  4882. line
  4883. 123456bar
  4884. After matching all 6 digits and then failing to match "foo", the normal
  4885. action of the matcher is to try again with only 5 digits matching the
  4886. \d+ item, and then with 4, and so on, before ultimately failing.
  4887. "Atomic grouping" (a term taken from Jeffrey Friedl's book) provides
  4888. the means for specifying that once a subpattern has matched, it is not
  4889. to be re-evaluated in this way.
  4890. If we use atomic grouping for the previous example, the matcher gives
  4891. up immediately on failing to match "foo" the first time. The notation
  4892. is a kind of special parenthesis, starting with (?> as in this example:
  4893. (?>\d+)foo
  4894. This kind of parenthesis "locks up" the part of the pattern it con-
  4895. tains once it has matched, and a failure further into the pattern is
  4896. prevented from backtracking into it. Backtracking past it to previous
  4897. items, however, works as normal.
  4898. An alternative description is that a subpattern of this type matches
  4899. the string of characters that an identical standalone pattern would
  4900. match, if anchored at the current point in the subject string.
  4901. Atomic grouping subpatterns are not capturing subpatterns. Simple cases
  4902. such as the above example can be thought of as a maximizing repeat that
  4903. must swallow everything it can. So, while both \d+ and \d+? are pre-
  4904. pared to adjust the number of digits they match in order to make the
  4905. rest of the pattern match, (?>\d+) can only match an entire sequence of
  4906. digits.
  4907. Atomic groups in general can of course contain arbitrarily complicated
  4908. subpatterns, and can be nested. However, when the subpattern for an
  4909. atomic group is just a single repeated item, as in the example above, a
  4910. simpler notation, called a "possessive quantifier" can be used. This
  4911. consists of an additional + character following a quantifier. Using
  4912. this notation, the previous example can be rewritten as
  4913. \d++foo
  4914. Note that a possessive quantifier can be used with an entire group, for
  4915. example:
  4916. (abc|xyz){2,3}+
  4917. Possessive quantifiers are always greedy; the setting of the
  4918. PCRE_UNGREEDY option is ignored. They are a convenient notation for the
  4919. simpler forms of atomic group. However, there is no difference in the
  4920. meaning of a possessive quantifier and the equivalent atomic group,
  4921. though there may be a performance difference; possessive quantifiers
  4922. should be slightly faster.
  4923. The possessive quantifier syntax is an extension to the Perl 5.8 syn-
  4924. tax. Jeffrey Friedl originated the idea (and the name) in the first
  4925. edition of his book. Mike McCloskey liked it, so implemented it when he
  4926. built Sun's Java package, and PCRE copied it from there. It ultimately
  4927. found its way into Perl at release 5.10.
  4928. PCRE has an optimization that automatically "possessifies" certain sim-
  4929. ple pattern constructs. For example, the sequence A+B is treated as
  4930. A++B because there is no point in backtracking into a sequence of A's
  4931. when B must follow.
  4932. When a pattern contains an unlimited repeat inside a subpattern that
  4933. can itself be repeated an unlimited number of times, the use of an
  4934. atomic group is the only way to avoid some failing matches taking a
  4935. very long time indeed. The pattern
  4936. (\D+|<\d+>)*[!?]
  4937. matches an unlimited number of substrings that either consist of non-
  4938. digits, or digits enclosed in <>, followed by either ! or ?. When it
  4939. matches, it runs quickly. However, if it is applied to
  4940. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  4941. it takes a long time before reporting failure. This is because the
  4942. string can be divided between the internal \D+ repeat and the external
  4943. * repeat in a large number of ways, and all have to be tried. (The
  4944. example uses [!?] rather than a single character at the end, because
  4945. both PCRE and Perl have an optimization that allows for fast failure
  4946. when a single character is used. They remember the last single charac-
  4947. ter that is required for a match, and fail early if it is not present
  4948. in the string.) If the pattern is changed so that it uses an atomic
  4949. group, like this:
  4950. ((?>\D+)|<\d+>)*[!?]
  4951. sequences of non-digits cannot be broken, and failure happens quickly.
  4952. BACK REFERENCES
  4953. Outside a character class, a backslash followed by a digit greater than
  4954. 0 (and possibly further digits) is a back reference to a capturing sub-
  4955. pattern earlier (that is, to its left) in the pattern, provided there
  4956. have been that many previous capturing left parentheses.
  4957. However, if the decimal number following the backslash is less than 10,
  4958. it is always taken as a back reference, and causes an error only if
  4959. there are not that many capturing left parentheses in the entire pat-
  4960. tern. In other words, the parentheses that are referenced need not be
  4961. to the left of the reference for numbers less than 10. A "forward back
  4962. reference" of this type can make sense when a repetition is involved
  4963. and the subpattern to the right has participated in an earlier itera-
  4964. tion.
  4965. It is not possible to have a numerical "forward back reference" to a
  4966. subpattern whose number is 10 or more using this syntax because a
  4967. sequence such as \50 is interpreted as a character defined in octal.
  4968. See the subsection entitled "Non-printing characters" above for further
  4969. details of the handling of digits following a backslash. There is no
  4970. such problem when named parentheses are used. A back reference to any
  4971. subpattern is possible using named parentheses (see below).
  4972. Another way of avoiding the ambiguity inherent in the use of digits
  4973. following a backslash is to use the \g escape sequence. This escape
  4974. must be followed by an unsigned number or a negative number, optionally
  4975. enclosed in braces. These examples are all identical:
  4976. (ring), \1
  4977. (ring), \g1
  4978. (ring), \g{1}
  4979. An unsigned number specifies an absolute reference without the ambigu-
  4980. ity that is present in the older syntax. It is also useful when literal
  4981. digits follow the reference. A negative number is a relative reference.
  4982. Consider this example:
  4983. (abc(def)ghi)\g{-1}
  4984. The sequence \g{-1} is a reference to the most recently started captur-
  4985. ing subpattern before \g, that is, is it equivalent to \2 in this exam-
  4986. ple. Similarly, \g{-2} would be equivalent to \1. The use of relative
  4987. references can be helpful in long patterns, and also in patterns that
  4988. are created by joining together fragments that contain references
  4989. within themselves.
  4990. A back reference matches whatever actually matched the capturing sub-
  4991. pattern in the current subject string, rather than anything matching
  4992. the subpattern itself (see "Subpatterns as subroutines" below for a way
  4993. of doing that). So the pattern
  4994. (sens|respons)e and \1ibility
  4995. matches "sense and sensibility" and "response and responsibility", but
  4996. not "sense and responsibility". If caseful matching is in force at the
  4997. time of the back reference, the case of letters is relevant. For exam-
  4998. ple,
  4999. ((?i)rah)\s+\1
  5000. matches "rah rah" and "RAH RAH", but not "RAH rah", even though the
  5001. original capturing subpattern is matched caselessly.
  5002. There are several different ways of writing back references to named
  5003. subpatterns. The .NET syntax \k{name} and the Perl syntax \k<name> or
  5004. \k'name' are supported, as is the Python syntax (?P=name). Perl 5.10's
  5005. unified back reference syntax, in which \g can be used for both numeric
  5006. and named references, is also supported. We could rewrite the above
  5007. example in any of the following ways:
  5008. (?<p1>(?i)rah)\s+\k<p1>
  5009. (?'p1'(?i)rah)\s+\k{p1}
  5010. (?P<p1>(?i)rah)\s+(?P=p1)
  5011. (?<p1>(?i)rah)\s+\g{p1}
  5012. A subpattern that is referenced by name may appear in the pattern
  5013. before or after the reference.
  5014. There may be more than one back reference to the same subpattern. If a
  5015. subpattern has not actually been used in a particular match, any back
  5016. references to it always fail by default. For example, the pattern
  5017. (a|(bc))\2
  5018. always fails if it starts to match "a" rather than "bc". However, if
  5019. the PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back refer-
  5020. ence to an unset value matches an empty string.
  5021. Because there may be many capturing parentheses in a pattern, all dig-
  5022. its following a backslash are taken as part of a potential back refer-
  5023. ence number. If the pattern continues with a digit character, some
  5024. delimiter must be used to terminate the back reference. If the
  5025. PCRE_EXTENDED option is set, this can be white space. Otherwise, the
  5026. \g{ syntax or an empty comment (see "Comments" below) can be used.
  5027. Recursive back references
  5028. A back reference that occurs inside the parentheses to which it refers
  5029. fails when the subpattern is first used, so, for example, (a\1) never
  5030. matches. However, such references can be useful inside repeated sub-
  5031. patterns. For example, the pattern
  5032. (a|b\1)+
  5033. matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-
  5034. ation of the subpattern, the back reference matches the character
  5035. string corresponding to the previous iteration. In order for this to
  5036. work, the pattern must be such that the first iteration does not need
  5037. to match the back reference. This can be done using alternation, as in
  5038. the example above, or by a quantifier with a minimum of zero.
  5039. Back references of this type cause the group that they reference to be
  5040. treated as an atomic group. Once the whole group has been matched, a
  5041. subsequent matching failure cannot cause backtracking into the middle
  5042. of the group.
  5043. ASSERTIONS
  5044. An assertion is a test on the characters following or preceding the
  5045. current matching point that does not actually consume any characters.
  5046. The simple assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are
  5047. described above.
  5048. More complicated assertions are coded as subpatterns. There are two
  5049. kinds: those that look ahead of the current position in the subject
  5050. string, and those that look behind it. An assertion subpattern is
  5051. matched in the normal way, except that it does not cause the current
  5052. matching position to be changed.
  5053. Assertion subpatterns are not capturing subpatterns. If such an asser-
  5054. tion contains capturing subpatterns within it, these are counted for
  5055. the purposes of numbering the capturing subpatterns in the whole pat-
  5056. tern. However, substring capturing is carried out only for positive
  5057. assertions. (Perl sometimes, but not always, does do capturing in nega-
  5058. tive assertions.)
  5059. For compatibility with Perl, assertion subpatterns may be repeated;
  5060. though it makes no sense to assert the same thing several times, the
  5061. side effect of capturing parentheses may occasionally be useful. In
  5062. practice, there only three cases:
  5063. (1) If the quantifier is {0}, the assertion is never obeyed during
  5064. matching. However, it may contain internal capturing parenthesized
  5065. groups that are called from elsewhere via the subroutine mechanism.
  5066. (2) If quantifier is {0,n} where n is greater than zero, it is treated
  5067. as if it were {0,1}. At run time, the rest of the pattern match is
  5068. tried with and without the assertion, the order depending on the greed-
  5069. iness of the quantifier.
  5070. (3) If the minimum repetition is greater than zero, the quantifier is
  5071. ignored. The assertion is obeyed just once when encountered during
  5072. matching.
  5073. Lookahead assertions
  5074. Lookahead assertions start with (?= for positive assertions and (?! for
  5075. negative assertions. For example,
  5076. \w+(?=;)
  5077. matches a word followed by a semicolon, but does not include the semi-
  5078. colon in the match, and
  5079. foo(?!bar)
  5080. matches any occurrence of "foo" that is not followed by "bar". Note
  5081. that the apparently similar pattern
  5082. (?!foo)bar
  5083. does not find an occurrence of "bar" that is preceded by something
  5084. other than "foo"; it finds any occurrence of "bar" whatsoever, because
  5085. the assertion (?!foo) is always true when the next three characters are
  5086. "bar". A lookbehind assertion is needed to achieve the other effect.
  5087. If you want to force a matching failure at some point in a pattern, the
  5088. most convenient way to do it is with (?!) because an empty string
  5089. always matches, so an assertion that requires there not to be an empty
  5090. string must always fail. The backtracking control verb (*FAIL) or (*F)
  5091. is a synonym for (?!).
  5092. Lookbehind assertions
  5093. Lookbehind assertions start with (?<= for positive assertions and (?<!
  5094. for negative assertions. For example,
  5095. (?<!foo)bar
  5096. does find an occurrence of "bar" that is not preceded by "foo". The
  5097. contents of a lookbehind assertion are restricted such that all the
  5098. strings it matches must have a fixed length. However, if there are sev-
  5099. eral top-level alternatives, they do not all have to have the same
  5100. fixed length. Thus
  5101. (?<=bullock|donkey)
  5102. is permitted, but
  5103. (?<!dogs?|cats?)
  5104. causes an error at compile time. Branches that match different length
  5105. strings are permitted only at the top level of a lookbehind assertion.
  5106. This is an extension compared with Perl, which requires all branches to
  5107. match the same length of string. An assertion such as
  5108. (?<=ab(c|de))
  5109. is not permitted, because its single top-level branch can match two
  5110. different lengths, but it is acceptable to PCRE if rewritten to use two
  5111. top-level branches:
  5112. (?<=abc|abde)
  5113. In some cases, the escape sequence \K (see above) can be used instead
  5114. of a lookbehind assertion to get round the fixed-length restriction.
  5115. The implementation of lookbehind assertions is, for each alternative,
  5116. to temporarily move the current position back by the fixed length and
  5117. then try to match. If there are insufficient characters before the cur-
  5118. rent position, the assertion fails.
  5119. In a UTF mode, PCRE does not allow the \C escape (which matches a sin-
  5120. gle data unit even in a UTF mode) to appear in lookbehind assertions,
  5121. because it makes it impossible to calculate the length of the lookbe-
  5122. hind. The \X and \R escapes, which can match different numbers of data
  5123. units, are also not permitted.
  5124. "Subroutine" calls (see below) such as (?2) or (?&X) are permitted in
  5125. lookbehinds, as long as the subpattern matches a fixed-length string.
  5126. Recursion, however, is not supported.
  5127. Possessive quantifiers can be used in conjunction with lookbehind
  5128. assertions to specify efficient matching of fixed-length strings at the
  5129. end of subject strings. Consider a simple pattern such as
  5130. abcd$
  5131. when applied to a long string that does not match. Because matching
  5132. proceeds from left to right, PCRE will look for each "a" in the subject
  5133. and then see if what follows matches the rest of the pattern. If the
  5134. pattern is specified as
  5135. ^.*abcd$
  5136. the initial .* matches the entire string at first, but when this fails
  5137. (because there is no following "a"), it backtracks to match all but the
  5138. last character, then all but the last two characters, and so on. Once
  5139. again the search for "a" covers the entire string, from right to left,
  5140. so we are no better off. However, if the pattern is written as
  5141. ^.*+(?<=abcd)
  5142. there can be no backtracking for the .*+ item; it can match only the
  5143. entire string. The subsequent lookbehind assertion does a single test
  5144. on the last four characters. If it fails, the match fails immediately.
  5145. For long strings, this approach makes a significant difference to the
  5146. processing time.
  5147. Using multiple assertions
  5148. Several assertions (of any sort) may occur in succession. For example,
  5149. (?<=\d{3})(?<!999)foo
  5150. matches "foo" preceded by three digits that are not "999". Notice that
  5151. each of the assertions is applied independently at the same point in
  5152. the subject string. First there is a check that the previous three
  5153. characters are all digits, and then there is a check that the same
  5154. three characters are not "999". This pattern does not match "foo" pre-
  5155. ceded by six characters, the first of which are digits and the last
  5156. three of which are not "999". For example, it doesn't match "123abc-
  5157. foo". A pattern to do that is
  5158. (?<=\d{3}...)(?<!999)foo
  5159. This time the first assertion looks at the preceding six characters,
  5160. checking that the first three are digits, and then the second assertion
  5161. checks that the preceding three characters are not "999".
  5162. Assertions can be nested in any combination. For example,
  5163. (?<=(?<!foo)bar)baz
  5164. matches an occurrence of "baz" that is preceded by "bar" which in turn
  5165. is not preceded by "foo", while
  5166. (?<=\d{3}(?!999)...)foo
  5167. is another pattern that matches "foo" preceded by three digits and any
  5168. three characters that are not "999".
  5169. CONDITIONAL SUBPATTERNS
  5170. It is possible to cause the matching process to obey a subpattern con-
  5171. ditionally or to choose between two alternative subpatterns, depending
  5172. on the result of an assertion, or whether a specific capturing subpat-
  5173. tern has already been matched. The two possible forms of conditional
  5174. subpattern are:
  5175. (?(condition)yes-pattern)
  5176. (?(condition)yes-pattern|no-pattern)
  5177. If the condition is satisfied, the yes-pattern is used; otherwise the
  5178. no-pattern (if present) is used. If there are more than two alterna-
  5179. tives in the subpattern, a compile-time error occurs. Each of the two
  5180. alternatives may itself contain nested subpatterns of any form, includ-
  5181. ing conditional subpatterns; the restriction to two alternatives
  5182. applies only at the level of the condition. This pattern fragment is an
  5183. example where the alternatives are complex:
  5184. (?(1) (A|B|C) | (D | (?(2)E|F) | E) )
  5185. There are four kinds of condition: references to subpatterns, refer-
  5186. ences to recursion, a pseudo-condition called DEFINE, and assertions.
  5187. Checking for a used subpattern by number
  5188. If the text between the parentheses consists of a sequence of digits,
  5189. the condition is true if a capturing subpattern of that number has pre-
  5190. viously matched. If there is more than one capturing subpattern with
  5191. the same number (see the earlier section about duplicate subpattern
  5192. numbers), the condition is true if any of them have matched. An alter-
  5193. native notation is to precede the digits with a plus or minus sign. In
  5194. this case, the subpattern number is relative rather than absolute. The
  5195. most recently opened parentheses can be referenced by (?(-1), the next
  5196. most recent by (?(-2), and so on. Inside loops it can also make sense
  5197. to refer to subsequent groups. The next parentheses to be opened can be
  5198. referenced as (?(+1), and so on. (The value zero in any of these forms
  5199. is not used; it provokes a compile-time error.)
  5200. Consider the following pattern, which contains non-significant white
  5201. space to make it more readable (assume the PCRE_EXTENDED option) and to
  5202. divide it into three parts for ease of discussion:
  5203. ( \( )? [^()]+ (?(1) \) )
  5204. The first part matches an optional opening parenthesis, and if that
  5205. character is present, sets it as the first captured substring. The sec-
  5206. ond part matches one or more characters that are not parentheses. The
  5207. third part is a conditional subpattern that tests whether or not the
  5208. first set of parentheses matched. If they did, that is, if subject
  5209. started with an opening parenthesis, the condition is true, and so the
  5210. yes-pattern is executed and a closing parenthesis is required. Other-
  5211. wise, since no-pattern is not present, the subpattern matches nothing.
  5212. In other words, this pattern matches a sequence of non-parentheses,
  5213. optionally enclosed in parentheses.
  5214. If you were embedding this pattern in a larger one, you could use a
  5215. relative reference:
  5216. ...other stuff... ( \( )? [^()]+ (?(-1) \) ) ...
  5217. This makes the fragment independent of the parentheses in the larger
  5218. pattern.
  5219. Checking for a used subpattern by name
  5220. Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a
  5221. used subpattern by name. For compatibility with earlier versions of
  5222. PCRE, which had this facility before Perl, the syntax (?(name)...) is
  5223. also recognized.
  5224. Rewriting the above example to use a named subpattern gives this:
  5225. (?<OPEN> \( )? [^()]+ (?(<OPEN>) \) )
  5226. If the name used in a condition of this kind is a duplicate, the test
  5227. is applied to all subpatterns of the same name, and is true if any one
  5228. of them has matched.
  5229. Checking for pattern recursion
  5230. If the condition is the string (R), and there is no subpattern with the
  5231. name R, the condition is true if a recursive call to the whole pattern
  5232. or any subpattern has been made. If digits or a name preceded by amper-
  5233. sand follow the letter R, for example:
  5234. (?(R3)...) or (?(R&name)...)
  5235. the condition is true if the most recent recursion is into a subpattern
  5236. whose number or name is given. This condition does not check the entire
  5237. recursion stack. If the name used in a condition of this kind is a
  5238. duplicate, the test is applied to all subpatterns of the same name, and
  5239. is true if any one of them is the most recent recursion.
  5240. At "top level", all these recursion test conditions are false. The
  5241. syntax for recursive patterns is described below.
  5242. Defining subpatterns for use by reference only
  5243. If the condition is the string (DEFINE), and there is no subpattern
  5244. with the name DEFINE, the condition is always false. In this case,
  5245. there may be only one alternative in the subpattern. It is always
  5246. skipped if control reaches this point in the pattern; the idea of
  5247. DEFINE is that it can be used to define subroutines that can be refer-
  5248. enced from elsewhere. (The use of subroutines is described below.) For
  5249. example, a pattern to match an IPv4 address such as "192.168.23.245"
  5250. could be written like this (ignore white space and line breaks):
  5251. (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
  5252. \b (?&byte) (\.(?&byte)){3} \b
  5253. The first part of the pattern is a DEFINE group inside which a another
  5254. group named "byte" is defined. This matches an individual component of
  5255. an IPv4 address (a number less than 256). When matching takes place,
  5256. this part of the pattern is skipped because DEFINE acts like a false
  5257. condition. The rest of the pattern uses references to the named group
  5258. to match the four dot-separated components of an IPv4 address, insist-
  5259. ing on a word boundary at each end.
  5260. Assertion conditions
  5261. If the condition is not in any of the above formats, it must be an
  5262. assertion. This may be a positive or negative lookahead or lookbehind
  5263. assertion. Consider this pattern, again containing non-significant
  5264. white space, and with the two alternatives on the second line:
  5265. (?(?=[^a-z]*[a-z])
  5266. \d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} )
  5267. The condition is a positive lookahead assertion that matches an
  5268. optional sequence of non-letters followed by a letter. In other words,
  5269. it tests for the presence of at least one letter in the subject. If a
  5270. letter is found, the subject is matched against the first alternative;
  5271. otherwise it is matched against the second. This pattern matches
  5272. strings in one of the two forms dd-aaa-dd or dd-dd-dd, where aaa are
  5273. letters and dd are digits.
  5274. COMMENTS
  5275. There are two ways of including comments in patterns that are processed
  5276. by PCRE. In both cases, the start of the comment must not be in a char-
  5277. acter class, nor in the middle of any other sequence of related charac-
  5278. ters such as (?: or a subpattern name or number. The characters that
  5279. make up a comment play no part in the pattern matching.
  5280. The sequence (?# marks the start of a comment that continues up to the
  5281. next closing parenthesis. Nested parentheses are not permitted. If the
  5282. PCRE_EXTENDED option is set, an unescaped # character also introduces a
  5283. comment, which in this case continues to immediately after the next
  5284. newline character or character sequence in the pattern. Which charac-
  5285. ters are interpreted as newlines is controlled by the options passed to
  5286. a compiling function or by a special sequence at the start of the pat-
  5287. tern, as described in the section entitled "Newline conventions" above.
  5288. Note that the end of this type of comment is a literal newline sequence
  5289. in the pattern; escape sequences that happen to represent a newline do
  5290. not count. For example, consider this pattern when PCRE_EXTENDED is
  5291. set, and the default newline convention is in force:
  5292. abc #comment \n still comment
  5293. On encountering the # character, pcre_compile() skips along, looking
  5294. for a newline in the pattern. The sequence \n is still literal at this
  5295. stage, so it does not terminate the comment. Only an actual character
  5296. with the code value 0x0a (the default newline) does so.
  5297. RECURSIVE PATTERNS
  5298. Consider the problem of matching a string in parentheses, allowing for
  5299. unlimited nested parentheses. Without the use of recursion, the best
  5300. that can be done is to use a pattern that matches up to some fixed
  5301. depth of nesting. It is not possible to handle an arbitrary nesting
  5302. depth.
  5303. For some time, Perl has provided a facility that allows regular expres-
  5304. sions to recurse (amongst other things). It does this by interpolating
  5305. Perl code in the expression at run time, and the code can refer to the
  5306. expression itself. A Perl pattern using code interpolation to solve the
  5307. parentheses problem can be created like this:
  5308. $re = qr{\( (?: (?>[^()]+) | (?p{$re}) )* \)}x;
  5309. The (?p{...}) item interpolates Perl code at run time, and in this case
  5310. refers recursively to the pattern in which it appears.
  5311. Obviously, PCRE cannot support the interpolation of Perl code. Instead,
  5312. it supports special syntax for recursion of the entire pattern, and
  5313. also for individual subpattern recursion. After its introduction in
  5314. PCRE and Python, this kind of recursion was subsequently introduced
  5315. into Perl at release 5.10.
  5316. A special item that consists of (? followed by a number greater than
  5317. zero and a closing parenthesis is a recursive subroutine call of the
  5318. subpattern of the given number, provided that it occurs inside that
  5319. subpattern. (If not, it is a non-recursive subroutine call, which is
  5320. described in the next section.) The special item (?R) or (?0) is a
  5321. recursive call of the entire regular expression.
  5322. This PCRE pattern solves the nested parentheses problem (assume the
  5323. PCRE_EXTENDED option is set so that white space is ignored):
  5324. \( ( [^()]++ | (?R) )* \)
  5325. First it matches an opening parenthesis. Then it matches any number of
  5326. substrings which can either be a sequence of non-parentheses, or a
  5327. recursive match of the pattern itself (that is, a correctly parenthe-
  5328. sized substring). Finally there is a closing parenthesis. Note the use
  5329. of a possessive quantifier to avoid backtracking into sequences of non-
  5330. parentheses.
  5331. If this were part of a larger pattern, you would not want to recurse
  5332. the entire pattern, so instead you could use this:
  5333. ( \( ( [^()]++ | (?1) )* \) )
  5334. We have put the pattern into parentheses, and caused the recursion to
  5335. refer to them instead of the whole pattern.
  5336. In a larger pattern, keeping track of parenthesis numbers can be
  5337. tricky. This is made easier by the use of relative references. Instead
  5338. of (?1) in the pattern above you can write (?-2) to refer to the second
  5339. most recently opened parentheses preceding the recursion. In other
  5340. words, a negative number counts capturing parentheses leftwards from
  5341. the point at which it is encountered.
  5342. It is also possible to refer to subsequently opened parentheses, by
  5343. writing references such as (?+2). However, these cannot be recursive
  5344. because the reference is not inside the parentheses that are refer-
  5345. enced. They are always non-recursive subroutine calls, as described in
  5346. the next section.
  5347. An alternative approach is to use named parentheses instead. The Perl
  5348. syntax for this is (?&name); PCRE's earlier syntax (?P>name) is also
  5349. supported. We could rewrite the above example as follows:
  5350. (?<pn> \( ( [^()]++ | (?&pn) )* \) )
  5351. If there is more than one subpattern with the same name, the earliest
  5352. one is used.
  5353. This particular example pattern that we have been looking at contains
  5354. nested unlimited repeats, and so the use of a possessive quantifier for
  5355. matching strings of non-parentheses is important when applying the pat-
  5356. tern to strings that do not match. For example, when this pattern is
  5357. applied to
  5358. (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
  5359. it yields "no match" quickly. However, if a possessive quantifier is
  5360. not used, the match runs for a very long time indeed because there are
  5361. so many different ways the + and * repeats can carve up the subject,
  5362. and all have to be tested before failure can be reported.
  5363. At the end of a match, the values of capturing parentheses are those
  5364. from the outermost level. If you want to obtain intermediate values, a
  5365. callout function can be used (see below and the pcrecallout documenta-
  5366. tion). If the pattern above is matched against
  5367. (ab(cd)ef)
  5368. the value for the inner capturing parentheses (numbered 2) is "ef",
  5369. which is the last value taken on at the top level. If a capturing sub-
  5370. pattern is not matched at the top level, its final captured value is
  5371. unset, even if it was (temporarily) set at a deeper level during the
  5372. matching process.
  5373. If there are more than 15 capturing parentheses in a pattern, PCRE has
  5374. to obtain extra memory to store data during a recursion, which it does
  5375. by using pcre_malloc, freeing it via pcre_free afterwards. If no memory
  5376. can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.
  5377. Do not confuse the (?R) item with the condition (R), which tests for
  5378. recursion. Consider this pattern, which matches text in angle brack-
  5379. ets, allowing for arbitrary nesting. Only digits are allowed in nested
  5380. brackets (that is, when recursing), whereas any characters are permit-
  5381. ted at the outer level.
  5382. < (?: (?(R) \d++ | [^<>]*+) | (?R)) * >
  5383. In this pattern, (?(R) is the start of a conditional subpattern, with
  5384. two different alternatives for the recursive and non-recursive cases.
  5385. The (?R) item is the actual recursive call.
  5386. Differences in recursion processing between PCRE and Perl
  5387. Recursion processing in PCRE differs from Perl in two important ways.
  5388. In PCRE (like Python, but unlike Perl), a recursive subpattern call is
  5389. always treated as an atomic group. That is, once it has matched some of
  5390. the subject string, it is never re-entered, even if it contains untried
  5391. alternatives and there is a subsequent matching failure. This can be
  5392. illustrated by the following pattern, which purports to match a palin-
  5393. dromic string that contains an odd number of characters (for example,
  5394. "a", "aba", "abcba", "abcdcba"):
  5395. ^(.|(.)(?1)\2)$
  5396. The idea is that it either matches a single character, or two identical
  5397. characters surrounding a sub-palindrome. In Perl, this pattern works;
  5398. in PCRE it does not if the pattern is longer than three characters.
  5399. Consider the subject string "abcba":
  5400. At the top level, the first character is matched, but as it is not at
  5401. the end of the string, the first alternative fails; the second alterna-
  5402. tive is taken and the recursion kicks in. The recursive call to subpat-
  5403. tern 1 successfully matches the next character ("b"). (Note that the
  5404. beginning and end of line tests are not part of the recursion).
  5405. Back at the top level, the next character ("c") is compared with what
  5406. subpattern 2 matched, which was "a". This fails. Because the recursion
  5407. is treated as an atomic group, there are now no backtracking points,
  5408. and so the entire match fails. (Perl is able, at this point, to re-
  5409. enter the recursion and try the second alternative.) However, if the
  5410. pattern is written with the alternatives in the other order, things are
  5411. different:
  5412. ^((.)(?1)\2|.)$
  5413. This time, the recursing alternative is tried first, and continues to
  5414. recurse until it runs out of characters, at which point the recursion
  5415. fails. But this time we do have another alternative to try at the
  5416. higher level. That is the big difference: in the previous case the
  5417. remaining alternative is at a deeper recursion level, which PCRE cannot
  5418. use.
  5419. To change the pattern so that it matches all palindromic strings, not
  5420. just those with an odd number of characters, it is tempting to change
  5421. the pattern to this:
  5422. ^((.)(?1)\2|.?)$
  5423. Again, this works in Perl, but not in PCRE, and for the same reason.
  5424. When a deeper recursion has matched a single character, it cannot be
  5425. entered again in order to match an empty string. The solution is to
  5426. separate the two cases, and write out the odd and even cases as alter-
  5427. natives at the higher level:
  5428. ^(?:((.)(?1)\2|)|((.)(?3)\4|.))
  5429. If you want to match typical palindromic phrases, the pattern has to
  5430. ignore all non-word characters, which can be done like this:
  5431. ^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$
  5432. If run with the PCRE_CASELESS option, this pattern matches phrases such
  5433. as "A man, a plan, a canal: Panama!" and it works well in both PCRE and
  5434. Perl. Note the use of the possessive quantifier *+ to avoid backtrack-
  5435. ing into sequences of non-word characters. Without this, PCRE takes a
  5436. great deal longer (ten times or more) to match typical phrases, and
  5437. Perl takes so long that you think it has gone into a loop.
  5438. WARNING: The palindrome-matching patterns above work only if the sub-
  5439. ject string does not start with a palindrome that is shorter than the
  5440. entire string. For example, although "abcba" is correctly matched, if
  5441. the subject is "ababa", PCRE finds the palindrome "aba" at the start,
  5442. then fails at top level because the end of the string does not follow.
  5443. Once again, it cannot jump back into the recursion to try other alter-
  5444. natives, so the entire match fails.
  5445. The second way in which PCRE and Perl differ in their recursion pro-
  5446. cessing is in the handling of captured values. In Perl, when a subpat-
  5447. tern is called recursively or as a subpattern (see the next section),
  5448. it has no access to any values that were captured outside the recur-
  5449. sion, whereas in PCRE these values can be referenced. Consider this
  5450. pattern:
  5451. ^(.)(\1|a(?2))
  5452. In PCRE, this pattern matches "bab". The first capturing parentheses
  5453. match "b", then in the second group, when the back reference \1 fails
  5454. to match "b", the second alternative matches "a" and then recurses. In
  5455. the recursion, \1 does now match "b" and so the whole match succeeds.
  5456. In Perl, the pattern fails to match because inside the recursive call
  5457. \1 cannot access the externally set value.
  5458. SUBPATTERNS AS SUBROUTINES
  5459. If the syntax for a recursive subpattern call (either by number or by
  5460. name) is used outside the parentheses to which it refers, it operates
  5461. like a subroutine in a programming language. The called subpattern may
  5462. be defined before or after the reference. A numbered reference can be
  5463. absolute or relative, as in these examples:
  5464. (...(absolute)...)...(?2)...
  5465. (...(relative)...)...(?-1)...
  5466. (...(?+1)...(relative)...
  5467. An earlier example pointed out that the pattern
  5468. (sens|respons)e and \1ibility
  5469. matches "sense and sensibility" and "response and responsibility", but
  5470. not "sense and responsibility". If instead the pattern
  5471. (sens|respons)e and (?1)ibility
  5472. is used, it does match "sense and responsibility" as well as the other
  5473. two strings. Another example is given in the discussion of DEFINE
  5474. above.
  5475. All subroutine calls, whether recursive or not, are always treated as
  5476. atomic groups. That is, once a subroutine has matched some of the sub-
  5477. ject string, it is never re-entered, even if it contains untried alter-
  5478. natives and there is a subsequent matching failure. Any capturing
  5479. parentheses that are set during the subroutine call revert to their
  5480. previous values afterwards.
  5481. Processing options such as case-independence are fixed when a subpat-
  5482. tern is defined, so if it is used as a subroutine, such options cannot
  5483. be changed for different calls. For example, consider this pattern:
  5484. (abc)(?i:(?-1))
  5485. It matches "abcabc". It does not match "abcABC" because the change of
  5486. processing option does not affect the called subpattern.
  5487. ONIGURUMA SUBROUTINE SYNTAX
  5488. For compatibility with Oniguruma, the non-Perl syntax \g followed by a
  5489. name or a number enclosed either in angle brackets or single quotes, is
  5490. an alternative syntax for referencing a subpattern as a subroutine,
  5491. possibly recursively. Here are two of the examples used above, rewrit-
  5492. ten using this syntax:
  5493. (?<pn> \( ( (?>[^()]+) | \g<pn> )* \) )
  5494. (sens|respons)e and \g'1'ibility
  5495. PCRE supports an extension to Oniguruma: if a number is preceded by a
  5496. plus or a minus sign it is taken as a relative reference. For example:
  5497. (abc)(?i:\g<-1>)
  5498. Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are not
  5499. synonymous. The former is a back reference; the latter is a subroutine
  5500. call.
  5501. CALLOUTS
  5502. Perl has a feature whereby using the sequence (?{...}) causes arbitrary
  5503. Perl code to be obeyed in the middle of matching a regular expression.
  5504. This makes it possible, amongst other things, to extract different sub-
  5505. strings that match the same pair of parentheses when there is a repeti-
  5506. tion.
  5507. PCRE provides a similar feature, but of course it cannot obey arbitrary
  5508. Perl code. The feature is called "callout". The caller of PCRE provides
  5509. an external function by putting its entry point in the global variable
  5510. pcre_callout (8-bit library) or pcre[16|32]_callout (16-bit or 32-bit
  5511. library). By default, this variable contains NULL, which disables all
  5512. calling out.
  5513. Within a regular expression, (?C) indicates the points at which the
  5514. external function is to be called. If you want to identify different
  5515. callout points, you can put a number less than 256 after the letter C.
  5516. The default value is zero. For example, this pattern has two callout
  5517. points:
  5518. (?C1)abc(?C2)def
  5519. If the PCRE_AUTO_CALLOUT flag is passed to a compiling function, call-
  5520. outs are automatically installed before each item in the pattern. They
  5521. are all numbered 255. If there is a conditional group in the pattern
  5522. whose condition is an assertion, an additional callout is inserted just
  5523. before the condition. An explicit callout may also be set at this posi-
  5524. tion, as in this example:
  5525. (?(?C9)(?=a)abc|def)
  5526. Note that this applies only to assertion conditions, not to other types
  5527. of condition.
  5528. During matching, when PCRE reaches a callout point, the external func-
  5529. tion is called. It is provided with the number of the callout, the
  5530. position in the pattern, and, optionally, one item of data originally
  5531. supplied by the caller of the matching function. The callout function
  5532. may cause matching to proceed, to backtrack, or to fail altogether.
  5533. By default, PCRE implements a number of optimizations at compile time
  5534. and matching time, and one side-effect is that sometimes callouts are
  5535. skipped. If you need all possible callouts to happen, you need to set
  5536. options that disable the relevant optimizations. More details, and a
  5537. complete description of the interface to the callout function, are
  5538. given in the pcrecallout documentation.
  5539. BACKTRACKING CONTROL
  5540. Perl 5.10 introduced a number of "Special Backtracking Control Verbs",
  5541. which are still described in the Perl documentation as "experimental
  5542. and subject to change or removal in a future version of Perl". It goes
  5543. on to say: "Their usage in production code should be noted to avoid
  5544. problems during upgrades." The same remarks apply to the PCRE features
  5545. described in this section.
  5546. The new verbs make use of what was previously invalid syntax: an open-
  5547. ing parenthesis followed by an asterisk. They are generally of the form
  5548. (*VERB) or (*VERB:NAME). Some may take either form, possibly behaving
  5549. differently depending on whether or not a name is present. A name is
  5550. any sequence of characters that does not include a closing parenthesis.
  5551. The maximum length of name is 255 in the 8-bit library and 65535 in the
  5552. 16-bit and 32-bit libraries. If the name is empty, that is, if the
  5553. closing parenthesis immediately follows the colon, the effect is as if
  5554. the colon were not there. Any number of these verbs may occur in a
  5555. pattern.
  5556. Since these verbs are specifically related to backtracking, most of
  5557. them can be used only when the pattern is to be matched using one of
  5558. the traditional matching functions, because these use a backtracking
  5559. algorithm. With the exception of (*FAIL), which behaves like a failing
  5560. negative assertion, the backtracking control verbs cause an error if
  5561. encountered by a DFA matching function.
  5562. The behaviour of these verbs in repeated groups, assertions, and in
  5563. subpatterns called as subroutines (whether or not recursively) is docu-
  5564. mented below.
  5565. Optimizations that affect backtracking verbs
  5566. PCRE contains some optimizations that are used to speed up matching by
  5567. running some checks at the start of each match attempt. For example, it
  5568. may know the minimum length of matching subject, or that a particular
  5569. character must be present. When one of these optimizations bypasses the
  5570. running of a match, any included backtracking verbs will not, of
  5571. course, be processed. You can suppress the start-of-match optimizations
  5572. by setting the PCRE_NO_START_OPTIMIZE option when calling pcre_com-
  5573. pile() or pcre_exec(), or by starting the pattern with (*NO_START_OPT).
  5574. There is more discussion of this option in the section entitled "Option
  5575. bits for pcre_exec()" in the pcreapi documentation.
  5576. Experiments with Perl suggest that it too has similar optimizations,
  5577. sometimes leading to anomalous results.
  5578. Verbs that act immediately
  5579. The following verbs act as soon as they are encountered. They may not
  5580. be followed by a name.
  5581. (*ACCEPT)
  5582. This verb causes the match to end successfully, skipping the remainder
  5583. of the pattern. However, when it is inside a subpattern that is called
  5584. as a subroutine, only that subpattern is ended successfully. Matching
  5585. then continues at the outer level. If (*ACCEPT) in triggered in a posi-
  5586. tive assertion, the assertion succeeds; in a negative assertion, the
  5587. assertion fails.
  5588. If (*ACCEPT) is inside capturing parentheses, the data so far is cap-
  5589. tured. For example:
  5590. A((?:A|B(*ACCEPT)|C)D)
  5591. This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is cap-
  5592. tured by the outer parentheses.
  5593. (*FAIL) or (*F)
  5594. This verb causes a matching failure, forcing backtracking to occur. It
  5595. is equivalent to (?!) but easier to read. The Perl documentation notes
  5596. that it is probably useful only when combined with (?{}) or (??{}).
  5597. Those are, of course, Perl features that are not present in PCRE. The
  5598. nearest equivalent is the callout feature, as for example in this pat-
  5599. tern:
  5600. a+(?C)(*FAIL)
  5601. A match with the string "aaaa" always fails, but the callout is taken
  5602. before each backtrack happens (in this example, 10 times).
  5603. Recording which path was taken
  5604. There is one verb whose main purpose is to track how a match was
  5605. arrived at, though it also has a secondary use in conjunction with
  5606. advancing the match starting point (see (*SKIP) below).
  5607. (*MARK:NAME) or (*:NAME)
  5608. A name is always required with this verb. There may be as many
  5609. instances of (*MARK) as you like in a pattern, and their names do not
  5610. have to be unique.
  5611. When a match succeeds, the name of the last-encountered (*MARK:NAME),
  5612. (*PRUNE:NAME), or (*THEN:NAME) on the matching path is passed back to
  5613. the caller as described in the section entitled "Extra data for
  5614. pcre_exec()" in the pcreapi documentation. Here is an example of
  5615. pcretest output, where the /K modifier requests the retrieval and out-
  5616. putting of (*MARK) data:
  5617. re> /X(*MARK:A)Y|X(*MARK:B)Z/K
  5618. data> XY
  5619. 0: XY
  5620. MK: A
  5621. XZ
  5622. 0: XZ
  5623. MK: B
  5624. The (*MARK) name is tagged with "MK:" in this output, and in this exam-
  5625. ple it indicates which of the two alternatives matched. This is a more
  5626. efficient way of obtaining this information than putting each alterna-
  5627. tive in its own capturing parentheses.
  5628. If a verb with a name is encountered in a positive assertion that is
  5629. true, the name is recorded and passed back if it is the last-encoun-
  5630. tered. This does not happen for negative assertions or failing positive
  5631. assertions.
  5632. After a partial match or a failed match, the last encountered name in
  5633. the entire match process is returned. For example:
  5634. re> /X(*MARK:A)Y|X(*MARK:B)Z/K
  5635. data> XP
  5636. No match, mark = B
  5637. Note that in this unanchored example the mark is retained from the
  5638. match attempt that started at the letter "X" in the subject. Subsequent
  5639. match attempts starting at "P" and then with an empty string do not get
  5640. as far as the (*MARK) item, but nevertheless do not reset it.
  5641. If you are interested in (*MARK) values after failed matches, you
  5642. should probably set the PCRE_NO_START_OPTIMIZE option (see above) to
  5643. ensure that the match is always attempted.
  5644. Verbs that act after backtracking
  5645. The following verbs do nothing when they are encountered. Matching con-
  5646. tinues with what follows, but if there is no subsequent match, causing
  5647. a backtrack to the verb, a failure is forced. That is, backtracking
  5648. cannot pass to the left of the verb. However, when one of these verbs
  5649. appears inside an atomic group or an assertion that is true, its effect
  5650. is confined to that group, because once the group has been matched,
  5651. there is never any backtracking into it. In this situation, backtrack-
  5652. ing can "jump back" to the left of the entire atomic group or asser-
  5653. tion. (Remember also, as stated above, that this localization also
  5654. applies in subroutine calls.)
  5655. These verbs differ in exactly what kind of failure occurs when back-
  5656. tracking reaches them. The behaviour described below is what happens
  5657. when the verb is not in a subroutine or an assertion. Subsequent sec-
  5658. tions cover these special cases.
  5659. (*COMMIT)
  5660. This verb, which may not be followed by a name, causes the whole match
  5661. to fail outright if there is a later matching failure that causes back-
  5662. tracking to reach it. Even if the pattern is unanchored, no further
  5663. attempts to find a match by advancing the starting point take place. If
  5664. (*COMMIT) is the only backtracking verb that is encountered, once it
  5665. has been passed pcre_exec() is committed to finding a match at the cur-
  5666. rent starting point, or not at all. For example:
  5667. a+(*COMMIT)b
  5668. This matches "xxaab" but not "aacaab". It can be thought of as a kind
  5669. of dynamic anchor, or "I've started, so I must finish." The name of the
  5670. most recently passed (*MARK) in the path is passed back when (*COMMIT)
  5671. forces a match failure.
  5672. If there is more than one backtracking verb in a pattern, a different
  5673. one that follows (*COMMIT) may be triggered first, so merely passing
  5674. (*COMMIT) during a match does not always guarantee that a match must be
  5675. at this starting point.
  5676. Note that (*COMMIT) at the start of a pattern is not the same as an
  5677. anchor, unless PCRE's start-of-match optimizations are turned off, as
  5678. shown in this output from pcretest:
  5679. re> /(*COMMIT)abc/
  5680. data> xyzabc
  5681. 0: abc
  5682. data> xyzabc\Y
  5683. No match
  5684. For this pattern, PCRE knows that any match must start with "a", so the
  5685. optimization skips along the subject to "a" before applying the pattern
  5686. to the first set of data. The match attempt then succeeds. In the sec-
  5687. ond set of data, the escape sequence \Y is interpreted by the pcretest
  5688. program. It causes the PCRE_NO_START_OPTIMIZE option to be set when
  5689. pcre_exec() is called. This disables the optimization that skips along
  5690. to the first character. The pattern is now applied starting at "x", and
  5691. so the (*COMMIT) causes the match to fail without trying any other
  5692. starting points.
  5693. (*PRUNE) or (*PRUNE:NAME)
  5694. This verb causes the match to fail at the current starting position in
  5695. the subject if there is a later matching failure that causes backtrack-
  5696. ing to reach it. If the pattern is unanchored, the normal "bumpalong"
  5697. advance to the next starting character then happens. Backtracking can
  5698. occur as usual to the left of (*PRUNE), before it is reached, or when
  5699. matching to the right of (*PRUNE), but if there is no match to the
  5700. right, backtracking cannot cross (*PRUNE). In simple cases, the use of
  5701. (*PRUNE) is just an alternative to an atomic group or possessive quan-
  5702. tifier, but there are some uses of (*PRUNE) that cannot be expressed in
  5703. any other way. In an anchored pattern (*PRUNE) has the same effect as
  5704. (*COMMIT).
  5705. The behaviour of (*PRUNE:NAME) is the not the same as
  5706. (*MARK:NAME)(*PRUNE). It is like (*MARK:NAME) in that the name is
  5707. remembered for passing back to the caller. However, (*SKIP:NAME)
  5708. searches only for names set with (*MARK).
  5709. (*SKIP)
  5710. This verb, when given without a name, is like (*PRUNE), except that if
  5711. the pattern is unanchored, the "bumpalong" advance is not to the next
  5712. character, but to the position in the subject where (*SKIP) was encoun-
  5713. tered. (*SKIP) signifies that whatever text was matched leading up to
  5714. it cannot be part of a successful match. Consider:
  5715. a+(*SKIP)b
  5716. If the subject is "aaaac...", after the first match attempt fails
  5717. (starting at the first character in the string), the starting point
  5718. skips on to start the next attempt at "c". Note that a possessive quan-
  5719. tifer does not have the same effect as this example; although it would
  5720. suppress backtracking during the first match attempt, the second
  5721. attempt would start at the second character instead of skipping on to
  5722. "c".
  5723. (*SKIP:NAME)
  5724. When (*SKIP) has an associated name, its behaviour is modified. When it
  5725. is triggered, the previous path through the pattern is searched for the
  5726. most recent (*MARK) that has the same name. If one is found, the
  5727. "bumpalong" advance is to the subject position that corresponds to that
  5728. (*MARK) instead of to where (*SKIP) was encountered. If no (*MARK) with
  5729. a matching name is found, the (*SKIP) is ignored.
  5730. Note that (*SKIP:NAME) searches only for names set by (*MARK:NAME). It
  5731. ignores names that are set by (*PRUNE:NAME) or (*THEN:NAME).
  5732. (*THEN) or (*THEN:NAME)
  5733. This verb causes a skip to the next innermost alternative when back-
  5734. tracking reaches it. That is, it cancels any further backtracking
  5735. within the current alternative. Its name comes from the observation
  5736. that it can be used for a pattern-based if-then-else block:
  5737. ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
  5738. If the COND1 pattern matches, FOO is tried (and possibly further items
  5739. after the end of the group if FOO succeeds); on failure, the matcher
  5740. skips to the second alternative and tries COND2, without backtracking
  5741. into COND1. If that succeeds and BAR fails, COND3 is tried. If subse-
  5742. quently BAZ fails, there are no more alternatives, so there is a back-
  5743. track to whatever came before the entire group. If (*THEN) is not
  5744. inside an alternation, it acts like (*PRUNE).
  5745. The behaviour of (*THEN:NAME) is the not the same as
  5746. (*MARK:NAME)(*THEN). It is like (*MARK:NAME) in that the name is
  5747. remembered for passing back to the caller. However, (*SKIP:NAME)
  5748. searches only for names set with (*MARK).
  5749. A subpattern that does not contain a | character is just a part of the
  5750. enclosing alternative; it is not a nested alternation with only one
  5751. alternative. The effect of (*THEN) extends beyond such a subpattern to
  5752. the enclosing alternative. Consider this pattern, where A, B, etc. are
  5753. complex pattern fragments that do not contain any | characters at this
  5754. level:
  5755. A (B(*THEN)C) | D
  5756. If A and B are matched, but there is a failure in C, matching does not
  5757. backtrack into A; instead it moves to the next alternative, that is, D.
  5758. However, if the subpattern containing (*THEN) is given an alternative,
  5759. it behaves differently:
  5760. A (B(*THEN)C | (*FAIL)) | D
  5761. The effect of (*THEN) is now confined to the inner subpattern. After a
  5762. failure in C, matching moves to (*FAIL), which causes the whole subpat-
  5763. tern to fail because there are no more alternatives to try. In this
  5764. case, matching does now backtrack into A.
  5765. Note that a conditional subpattern is not considered as having two
  5766. alternatives, because only one is ever used. In other words, the |
  5767. character in a conditional subpattern has a different meaning. Ignoring
  5768. white space, consider:
  5769. ^.*? (?(?=a) a | b(*THEN)c )
  5770. If the subject is "ba", this pattern does not match. Because .*? is
  5771. ungreedy, it initially matches zero characters. The condition (?=a)
  5772. then fails, the character "b" is matched, but "c" is not. At this
  5773. point, matching does not backtrack to .*? as might perhaps be expected
  5774. from the presence of the | character. The conditional subpattern is
  5775. part of the single alternative that comprises the whole pattern, and so
  5776. the match fails. (If there was a backtrack into .*?, allowing it to
  5777. match "b", the match would succeed.)
  5778. The verbs just described provide four different "strengths" of control
  5779. when subsequent matching fails. (*THEN) is the weakest, carrying on the
  5780. match at the next alternative. (*PRUNE) comes next, failing the match
  5781. at the current starting position, but allowing an advance to the next
  5782. character (for an unanchored pattern). (*SKIP) is similar, except that
  5783. the advance may be more than one character. (*COMMIT) is the strongest,
  5784. causing the entire match to fail.
  5785. More than one backtracking verb
  5786. If more than one backtracking verb is present in a pattern, the one
  5787. that is backtracked onto first acts. For example, consider this pat-
  5788. tern, where A, B, etc. are complex pattern fragments:
  5789. (A(*COMMIT)B(*THEN)C|ABD)
  5790. If A matches but B fails, the backtrack to (*COMMIT) causes the entire
  5791. match to fail. However, if A and B match, but C fails, the backtrack to
  5792. (*THEN) causes the next alternative (ABD) to be tried. This behaviour
  5793. is consistent, but is not always the same as Perl's. It means that if
  5794. two or more backtracking verbs appear in succession, all the the last
  5795. of them has no effect. Consider this example:
  5796. ...(*COMMIT)(*PRUNE)...
  5797. If there is a matching failure to the right, backtracking onto (*PRUNE)
  5798. causes it to be triggered, and its action is taken. There can never be
  5799. a backtrack onto (*COMMIT).
  5800. Backtracking verbs in repeated groups
  5801. PCRE differs from Perl in its handling of backtracking verbs in
  5802. repeated groups. For example, consider:
  5803. /(a(*COMMIT)b)+ac/
  5804. If the subject is "abac", Perl matches, but PCRE fails because the
  5805. (*COMMIT) in the second repeat of the group acts.
  5806. Backtracking verbs in assertions
  5807. (*FAIL) in an assertion has its normal effect: it forces an immediate
  5808. backtrack.
  5809. (*ACCEPT) in a positive assertion causes the assertion to succeed with-
  5810. out any further processing. In a negative assertion, (*ACCEPT) causes
  5811. the assertion to fail without any further processing.
  5812. The other backtracking verbs are not treated specially if they appear
  5813. in a positive assertion. In particular, (*THEN) skips to the next
  5814. alternative in the innermost enclosing group that has alternations,
  5815. whether or not this is within the assertion.
  5816. Negative assertions are, however, different, in order to ensure that
  5817. changing a positive assertion into a negative assertion changes its
  5818. result. Backtracking into (*COMMIT), (*SKIP), or (*PRUNE) causes a neg-
  5819. ative assertion to be true, without considering any further alternative
  5820. branches in the assertion. Backtracking into (*THEN) causes it to skip
  5821. to the next enclosing alternative within the assertion (the normal be-
  5822. haviour), but if the assertion does not have such an alternative,
  5823. (*THEN) behaves like (*PRUNE).
  5824. Backtracking verbs in subroutines
  5825. These behaviours occur whether or not the subpattern is called recur-
  5826. sively. Perl's treatment of subroutines is different in some cases.
  5827. (*FAIL) in a subpattern called as a subroutine has its normal effect:
  5828. it forces an immediate backtrack.
  5829. (*ACCEPT) in a subpattern called as a subroutine causes the subroutine
  5830. match to succeed without any further processing. Matching then contin-
  5831. ues after the subroutine call.
  5832. (*COMMIT), (*SKIP), and (*PRUNE) in a subpattern called as a subroutine
  5833. cause the subroutine match to fail.
  5834. (*THEN) skips to the next alternative in the innermost enclosing group
  5835. within the subpattern that has alternatives. If there is no such group
  5836. within the subpattern, (*THEN) causes the subroutine match to fail.
  5837. SEE ALSO
  5838. pcreapi(3), pcrecallout(3), pcrematching(3), pcresyntax(3), pcre(3),
  5839. pcre16(3), pcre32(3).
  5840. AUTHOR
  5841. Philip Hazel
  5842. University Computing Service
  5843. Cambridge CB2 3QH, England.
  5844. REVISION
  5845. Last updated: 14 June 2015
  5846. Copyright (c) 1997-2015 University of Cambridge.
  5847. ------------------------------------------------------------------------------
  5848. PCRESYNTAX(3) Library Functions Manual PCRESYNTAX(3)
  5849. NAME
  5850. PCRE - Perl-compatible regular expressions
  5851. PCRE REGULAR EXPRESSION SYNTAX SUMMARY
  5852. The full syntax and semantics of the regular expressions that are sup-
  5853. ported by PCRE are described in the pcrepattern documentation. This
  5854. document contains a quick-reference summary of the syntax.
  5855. QUOTING
  5856. \x where x is non-alphanumeric is a literal x
  5857. \Q...\E treat enclosed characters as literal
  5858. CHARACTERS
  5859. \a alarm, that is, the BEL character (hex 07)
  5860. \cx "control-x", where x is any ASCII character
  5861. \e escape (hex 1B)
  5862. \f form feed (hex 0C)
  5863. \n newline (hex 0A)
  5864. \r carriage return (hex 0D)
  5865. \t tab (hex 09)
  5866. \0dd character with octal code 0dd
  5867. \ddd character with octal code ddd, or backreference
  5868. \o{ddd..} character with octal code ddd..
  5869. \xhh character with hex code hh
  5870. \x{hhh..} character with hex code hhh..
  5871. Note that \0dd is always an octal code, and that \8 and \9 are the lit-
  5872. eral characters "8" and "9".
  5873. CHARACTER TYPES
  5874. . any character except newline;
  5875. in dotall mode, any character whatsoever
  5876. \C one data unit, even in UTF mode (best avoided)
  5877. \d a decimal digit
  5878. \D a character that is not a decimal digit
  5879. \h a horizontal white space character
  5880. \H a character that is not a horizontal white space character
  5881. \N a character that is not a newline
  5882. \p{xx} a character with the xx property
  5883. \P{xx} a character without the xx property
  5884. \R a newline sequence
  5885. \s a white space character
  5886. \S a character that is not a white space character
  5887. \v a vertical white space character
  5888. \V a character that is not a vertical white space character
  5889. \w a "word" character
  5890. \W a "non-word" character
  5891. \X a Unicode extended grapheme cluster
  5892. By default, \d, \s, and \w match only ASCII characters, even in UTF-8
  5893. mode or in the 16- bit and 32-bit libraries. However, if locale-spe-
  5894. cific matching is happening, \s and \w may also match characters with
  5895. code points in the range 128-255. If the PCRE_UCP option is set, the
  5896. behaviour of these escape sequences is changed to use Unicode proper-
  5897. ties and they match many more characters.
  5898. GENERAL CATEGORY PROPERTIES FOR \p and \P
  5899. C Other
  5900. Cc Control
  5901. Cf Format
  5902. Cn Unassigned
  5903. Co Private use
  5904. Cs Surrogate
  5905. L Letter
  5906. Ll Lower case letter
  5907. Lm Modifier letter
  5908. Lo Other letter
  5909. Lt Title case letter
  5910. Lu Upper case letter
  5911. L& Ll, Lu, or Lt
  5912. M Mark
  5913. Mc Spacing mark
  5914. Me Enclosing mark
  5915. Mn Non-spacing mark
  5916. N Number
  5917. Nd Decimal number
  5918. Nl Letter number
  5919. No Other number
  5920. P Punctuation
  5921. Pc Connector punctuation
  5922. Pd Dash punctuation
  5923. Pe Close punctuation
  5924. Pf Final punctuation
  5925. Pi Initial punctuation
  5926. Po Other punctuation
  5927. Ps Open punctuation
  5928. S Symbol
  5929. Sc Currency symbol
  5930. Sk Modifier symbol
  5931. Sm Mathematical symbol
  5932. So Other symbol
  5933. Z Separator
  5934. Zl Line separator
  5935. Zp Paragraph separator
  5936. Zs Space separator
  5937. PCRE SPECIAL CATEGORY PROPERTIES FOR \p and \P
  5938. Xan Alphanumeric: union of properties L and N
  5939. Xps POSIX space: property Z or tab, NL, VT, FF, CR
  5940. Xsp Perl space: property Z or tab, NL, VT, FF, CR
  5941. Xuc Univerally-named character: one that can be
  5942. represented by a Universal Character Name
  5943. Xwd Perl word: property Xan or underscore
  5944. Perl and POSIX space are now the same. Perl added VT to its space char-
  5945. acter set at release 5.18 and PCRE changed at release 8.34.
  5946. SCRIPT NAMES FOR \p AND \P
  5947. Arabic, Armenian, Avestan, Balinese, Bamum, Bassa_Vah, Batak, Bengali,
  5948. Bopomofo, Brahmi, Braille, Buginese, Buhid, Canadian_Aboriginal, Car-
  5949. ian, Caucasian_Albanian, Chakma, Cham, Cherokee, Common, Coptic, Cunei-
  5950. form, Cypriot, Cyrillic, Deseret, Devanagari, Duployan, Egyptian_Hiero-
  5951. glyphs, Elbasan, Ethiopic, Georgian, Glagolitic, Gothic, Grantha,
  5952. Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hiragana,
  5953. Imperial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscrip-
  5954. tional_Parthian, Javanese, Kaithi, Kannada, Katakana, Kayah_Li,
  5955. Kharoshthi, Khmer, Khojki, Khudawadi, Lao, Latin, Lepcha, Limbu, Lin-
  5956. ear_A, Linear_B, Lisu, Lycian, Lydian, Mahajani, Malayalam, Mandaic,
  5957. Manichaean, Meetei_Mayek, Mende_Kikakui, Meroitic_Cursive,
  5958. Meroitic_Hieroglyphs, Miao, Modi, Mongolian, Mro, Myanmar, Nabataean,
  5959. New_Tai_Lue, Nko, Ogham, Ol_Chiki, Old_Italic, Old_North_Arabian,
  5960. Old_Permic, Old_Persian, Old_South_Arabian, Old_Turkic, Oriya, Osmanya,
  5961. Pahawh_Hmong, Palmyrene, Pau_Cin_Hau, Phags_Pa, Phoenician,
  5962. Psalter_Pahlavi, Rejang, Runic, Samaritan, Saurashtra, Sharada, Sha-
  5963. vian, Siddham, Sinhala, Sora_Sompeng, Sundanese, Syloti_Nagri, Syriac,
  5964. Tagalog, Tagbanwa, Tai_Le, Tai_Tham, Tai_Viet, Takri, Tamil, Telugu,
  5965. Thaana, Thai, Tibetan, Tifinagh, Tirhuta, Ugaritic, Vai, Warang_Citi,
  5966. Yi.
  5967. CHARACTER CLASSES
  5968. [...] positive character class
  5969. [^...] negative character class
  5970. [x-y] range (can be used for hex characters)
  5971. [[:xxx:]] positive POSIX named set
  5972. [[:^xxx:]] negative POSIX named set
  5973. alnum alphanumeric
  5974. alpha alphabetic
  5975. ascii 0-127
  5976. blank space or tab
  5977. cntrl control character
  5978. digit decimal digit
  5979. graph printing, excluding space
  5980. lower lower case letter
  5981. print printing, including space
  5982. punct printing, excluding alphanumeric
  5983. space white space
  5984. upper upper case letter
  5985. word same as \w
  5986. xdigit hexadecimal digit
  5987. In PCRE, POSIX character set names recognize only ASCII characters by
  5988. default, but some of them use Unicode properties if PCRE_UCP is set.
  5989. You can use \Q...\E inside a character class.
  5990. QUANTIFIERS
  5991. ? 0 or 1, greedy
  5992. ?+ 0 or 1, possessive
  5993. ?? 0 or 1, lazy
  5994. * 0 or more, greedy
  5995. *+ 0 or more, possessive
  5996. *? 0 or more, lazy
  5997. + 1 or more, greedy
  5998. ++ 1 or more, possessive
  5999. +? 1 or more, lazy
  6000. {n} exactly n
  6001. {n,m} at least n, no more than m, greedy
  6002. {n,m}+ at least n, no more than m, possessive
  6003. {n,m}? at least n, no more than m, lazy
  6004. {n,} n or more, greedy
  6005. {n,}+ n or more, possessive
  6006. {n,}? n or more, lazy
  6007. ANCHORS AND SIMPLE ASSERTIONS
  6008. \b word boundary
  6009. \B not a word boundary
  6010. ^ start of subject
  6011. also after internal newline in multiline mode
  6012. \A start of subject
  6013. $ end of subject
  6014. also before newline at end of subject
  6015. also before internal newline in multiline mode
  6016. \Z end of subject
  6017. also before newline at end of subject
  6018. \z end of subject
  6019. \G first matching position in subject
  6020. MATCH POINT RESET
  6021. \K reset start of match
  6022. \K is honoured in positive assertions, but ignored in negative ones.
  6023. ALTERNATION
  6024. expr|expr|expr...
  6025. CAPTURING
  6026. (...) capturing group
  6027. (?<name>...) named capturing group (Perl)
  6028. (?'name'...) named capturing group (Perl)
  6029. (?P<name>...) named capturing group (Python)
  6030. (?:...) non-capturing group
  6031. (?|...) non-capturing group; reset group numbers for
  6032. capturing groups in each alternative
  6033. ATOMIC GROUPS
  6034. (?>...) atomic, non-capturing group
  6035. COMMENT
  6036. (?#....) comment (not nestable)
  6037. OPTION SETTING
  6038. (?i) caseless
  6039. (?J) allow duplicate names
  6040. (?m) multiline
  6041. (?s) single line (dotall)
  6042. (?U) default ungreedy (lazy)
  6043. (?x) extended (ignore white space)
  6044. (?-...) unset option(s)
  6045. The following are recognized only at the very start of a pattern or
  6046. after one of the newline or \R options with similar syntax. More than
  6047. one of them may appear.
  6048. (*LIMIT_MATCH=d) set the match limit to d (decimal number)
  6049. (*LIMIT_RECURSION=d) set the recursion limit to d (decimal number)
  6050. (*NO_AUTO_POSSESS) no auto-possessification (PCRE_NO_AUTO_POSSESS)
  6051. (*NO_START_OPT) no start-match optimization (PCRE_NO_START_OPTIMIZE)
  6052. (*UTF8) set UTF-8 mode: 8-bit library (PCRE_UTF8)
  6053. (*UTF16) set UTF-16 mode: 16-bit library (PCRE_UTF16)
  6054. (*UTF32) set UTF-32 mode: 32-bit library (PCRE_UTF32)
  6055. (*UTF) set appropriate UTF mode for the library in use
  6056. (*UCP) set PCRE_UCP (use Unicode properties for \d etc)
  6057. Note that LIMIT_MATCH and LIMIT_RECURSION can only reduce the value of
  6058. the limits set by the caller of pcre_exec(), not increase them.
  6059. NEWLINE CONVENTION
  6060. These are recognized only at the very start of the pattern or after
  6061. option settings with a similar syntax.
  6062. (*CR) carriage return only
  6063. (*LF) linefeed only
  6064. (*CRLF) carriage return followed by linefeed
  6065. (*ANYCRLF) all three of the above
  6066. (*ANY) any Unicode newline sequence
  6067. WHAT \R MATCHES
  6068. These are recognized only at the very start of the pattern or after
  6069. option setting with a similar syntax.
  6070. (*BSR_ANYCRLF) CR, LF, or CRLF
  6071. (*BSR_UNICODE) any Unicode newline sequence
  6072. LOOKAHEAD AND LOOKBEHIND ASSERTIONS
  6073. (?=...) positive look ahead
  6074. (?!...) negative look ahead
  6075. (?<=...) positive look behind
  6076. (?<!...) negative look behind
  6077. Each top-level branch of a look behind must be of a fixed length.
  6078. BACKREFERENCES
  6079. \n reference by number (can be ambiguous)
  6080. \gn reference by number
  6081. \g{n} reference by number
  6082. \g{-n} relative reference by number
  6083. \k<name> reference by name (Perl)
  6084. \k'name' reference by name (Perl)
  6085. \g{name} reference by name (Perl)
  6086. \k{name} reference by name (.NET)
  6087. (?P=name) reference by name (Python)
  6088. SUBROUTINE REFERENCES (POSSIBLY RECURSIVE)
  6089. (?R) recurse whole pattern
  6090. (?n) call subpattern by absolute number
  6091. (?+n) call subpattern by relative number
  6092. (?-n) call subpattern by relative number
  6093. (?&name) call subpattern by name (Perl)
  6094. (?P>name) call subpattern by name (Python)
  6095. \g<name> call subpattern by name (Oniguruma)
  6096. \g'name' call subpattern by name (Oniguruma)
  6097. \g<n> call subpattern by absolute number (Oniguruma)
  6098. \g'n' call subpattern by absolute number (Oniguruma)
  6099. \g<+n> call subpattern by relative number (PCRE extension)
  6100. \g'+n' call subpattern by relative number (PCRE extension)
  6101. \g<-n> call subpattern by relative number (PCRE extension)
  6102. \g'-n' call subpattern by relative number (PCRE extension)
  6103. CONDITIONAL PATTERNS
  6104. (?(condition)yes-pattern)
  6105. (?(condition)yes-pattern|no-pattern)
  6106. (?(n)... absolute reference condition
  6107. (?(+n)... relative reference condition
  6108. (?(-n)... relative reference condition
  6109. (?(<name>)... named reference condition (Perl)
  6110. (?('name')... named reference condition (Perl)
  6111. (?(name)... named reference condition (PCRE)
  6112. (?(R)... overall recursion condition
  6113. (?(Rn)... specific group recursion condition
  6114. (?(R&name)... specific recursion condition
  6115. (?(DEFINE)... define subpattern for reference
  6116. (?(assert)... assertion condition
  6117. BACKTRACKING CONTROL
  6118. The following act immediately they are reached:
  6119. (*ACCEPT) force successful match
  6120. (*FAIL) force backtrack; synonym (*F)
  6121. (*MARK:NAME) set name to be passed back; synonym (*:NAME)
  6122. The following act only when a subsequent match failure causes a back-
  6123. track to reach them. They all force a match failure, but they differ in
  6124. what happens afterwards. Those that advance the start-of-match point do
  6125. so only if the pattern is not anchored.
  6126. (*COMMIT) overall failure, no advance of starting point
  6127. (*PRUNE) advance to next starting character
  6128. (*PRUNE:NAME) equivalent to (*MARK:NAME)(*PRUNE)
  6129. (*SKIP) advance to current matching position
  6130. (*SKIP:NAME) advance to position corresponding to an earlier
  6131. (*MARK:NAME); if not found, the (*SKIP) is ignored
  6132. (*THEN) local failure, backtrack to next alternation
  6133. (*THEN:NAME) equivalent to (*MARK:NAME)(*THEN)
  6134. CALLOUTS
  6135. (?C) callout
  6136. (?Cn) callout with data n
  6137. SEE ALSO
  6138. pcrepattern(3), pcreapi(3), pcrecallout(3), pcrematching(3), pcre(3).
  6139. AUTHOR
  6140. Philip Hazel
  6141. University Computing Service
  6142. Cambridge CB2 3QH, England.
  6143. REVISION
  6144. Last updated: 08 January 2014
  6145. Copyright (c) 1997-2014 University of Cambridge.
  6146. ------------------------------------------------------------------------------
  6147. PCREUNICODE(3) Library Functions Manual PCREUNICODE(3)
  6148. NAME
  6149. PCRE - Perl-compatible regular expressions
  6150. UTF-8, UTF-16, UTF-32, AND UNICODE PROPERTY SUPPORT
  6151. As well as UTF-8 support, PCRE also supports UTF-16 (from release 8.30)
  6152. and UTF-32 (from release 8.32), by means of two additional libraries.
  6153. They can be built as well as, or instead of, the 8-bit library.
  6154. UTF-8 SUPPORT
  6155. In order process UTF-8 strings, you must build PCRE's 8-bit library
  6156. with UTF support, and, in addition, you must call pcre_compile() with
  6157. the PCRE_UTF8 option flag, or the pattern must start with the sequence
  6158. (*UTF8) or (*UTF). When either of these is the case, both the pattern
  6159. and any subject strings that are matched against it are treated as
  6160. UTF-8 strings instead of strings of individual 1-byte characters.
  6161. UTF-16 AND UTF-32 SUPPORT
  6162. In order process UTF-16 or UTF-32 strings, you must build PCRE's 16-bit
  6163. or 32-bit library with UTF support, and, in addition, you must call
  6164. pcre16_compile() or pcre32_compile() with the PCRE_UTF16 or PCRE_UTF32
  6165. option flag, as appropriate. Alternatively, the pattern must start with
  6166. the sequence (*UTF16), (*UTF32), as appropriate, or (*UTF), which can
  6167. be used with either library. When UTF mode is set, both the pattern and
  6168. any subject strings that are matched against it are treated as UTF-16
  6169. or UTF-32 strings instead of strings of individual 16-bit or 32-bit
  6170. characters.
  6171. UTF SUPPORT OVERHEAD
  6172. If you compile PCRE with UTF support, but do not use it at run time,
  6173. the library will be a bit bigger, but the additional run time overhead
  6174. is limited to testing the PCRE_UTF[8|16|32] flag occasionally, so
  6175. should not be very big.
  6176. UNICODE PROPERTY SUPPORT
  6177. If PCRE is built with Unicode character property support (which implies
  6178. UTF support), the escape sequences \p{..}, \P{..}, and \X can be used.
  6179. The available properties that can be tested are limited to the general
  6180. category properties such as Lu for an upper case letter or Nd for a
  6181. decimal number, the Unicode script names such as Arabic or Han, and the
  6182. derived properties Any and L&. Full lists is given in the pcrepattern
  6183. and pcresyntax documentation. Only the short names for properties are
  6184. supported. For example, \p{L} matches a letter. Its Perl synonym,
  6185. \p{Letter}, is not supported. Furthermore, in Perl, many properties
  6186. may optionally be prefixed by "Is", for compatibility with Perl 5.6.
  6187. PCRE does not support this.
  6188. Validity of UTF-8 strings
  6189. When you set the PCRE_UTF8 flag, the byte strings passed as patterns
  6190. and subjects are (by default) checked for validity on entry to the rel-
  6191. evant functions. The entire string is checked before any other process-
  6192. ing takes place. From release 7.3 of PCRE, the check is according the
  6193. rules of RFC 3629, which are themselves derived from the Unicode speci-
  6194. fication. Earlier releases of PCRE followed the rules of RFC 2279,
  6195. which allows the full range of 31-bit values (0 to 0x7FFFFFFF). The
  6196. current check allows only values in the range U+0 to U+10FFFF, exclud-
  6197. ing the surrogate area. (From release 8.33 the so-called "non-charac-
  6198. ter" code points are no longer excluded because Unicode corrigendum #9
  6199. makes it clear that they should not be.)
  6200. Characters in the "Surrogate Area" of Unicode are reserved for use by
  6201. UTF-16, where they are used in pairs to encode codepoints with values
  6202. greater than 0xFFFF. The code points that are encoded by UTF-16 pairs
  6203. are available independently in the UTF-8 and UTF-32 encodings. (In
  6204. other words, the whole surrogate thing is a fudge for UTF-16 which
  6205. unfortunately messes up UTF-8 and UTF-32.)
  6206. If an invalid UTF-8 string is passed to PCRE, an error return is given.
  6207. At compile time, the only additional information is the offset to the
  6208. first byte of the failing character. The run-time functions pcre_exec()
  6209. and pcre_dfa_exec() also pass back this information, as well as a more
  6210. detailed reason code if the caller has provided memory in which to do
  6211. this.
  6212. In some situations, you may already know that your strings are valid,
  6213. and therefore want to skip these checks in order to improve perfor-
  6214. mance, for example in the case of a long subject string that is being
  6215. scanned repeatedly. If you set the PCRE_NO_UTF8_CHECK flag at compile
  6216. time or at run time, PCRE assumes that the pattern or subject it is
  6217. given (respectively) contains only valid UTF-8 codes. In this case, it
  6218. does not diagnose an invalid UTF-8 string.
  6219. Note that passing PCRE_NO_UTF8_CHECK to pcre_compile() just disables
  6220. the check for the pattern; it does not also apply to subject strings.
  6221. If you want to disable the check for a subject string you must pass
  6222. this option to pcre_exec() or pcre_dfa_exec().
  6223. If you pass an invalid UTF-8 string when PCRE_NO_UTF8_CHECK is set, the
  6224. result is undefined and your program may crash.
  6225. Validity of UTF-16 strings
  6226. When you set the PCRE_UTF16 flag, the strings of 16-bit data units that
  6227. are passed as patterns and subjects are (by default) checked for valid-
  6228. ity on entry to the relevant functions. Values other than those in the
  6229. surrogate range U+D800 to U+DFFF are independent code points. Values in
  6230. the surrogate range must be used in pairs in the correct manner.
  6231. If an invalid UTF-16 string is passed to PCRE, an error return is
  6232. given. At compile time, the only additional information is the offset
  6233. to the first data unit of the failing character. The run-time functions
  6234. pcre16_exec() and pcre16_dfa_exec() also pass back this information, as
  6235. well as a more detailed reason code if the caller has provided memory
  6236. in which to do this.
  6237. In some situations, you may already know that your strings are valid,
  6238. and therefore want to skip these checks in order to improve perfor-
  6239. mance. If you set the PCRE_NO_UTF16_CHECK flag at compile time or at
  6240. run time, PCRE assumes that the pattern or subject it is given (respec-
  6241. tively) contains only valid UTF-16 sequences. In this case, it does not
  6242. diagnose an invalid UTF-16 string. However, if an invalid string is
  6243. passed, the result is undefined.
  6244. Validity of UTF-32 strings
  6245. When you set the PCRE_UTF32 flag, the strings of 32-bit data units that
  6246. are passed as patterns and subjects are (by default) checked for valid-
  6247. ity on entry to the relevant functions. This check allows only values
  6248. in the range U+0 to U+10FFFF, excluding the surrogate area U+D800 to
  6249. U+DFFF.
  6250. If an invalid UTF-32 string is passed to PCRE, an error return is
  6251. given. At compile time, the only additional information is the offset
  6252. to the first data unit of the failing character. The run-time functions
  6253. pcre32_exec() and pcre32_dfa_exec() also pass back this information, as
  6254. well as a more detailed reason code if the caller has provided memory
  6255. in which to do this.
  6256. In some situations, you may already know that your strings are valid,
  6257. and therefore want to skip these checks in order to improve perfor-
  6258. mance. If you set the PCRE_NO_UTF32_CHECK flag at compile time or at
  6259. run time, PCRE assumes that the pattern or subject it is given (respec-
  6260. tively) contains only valid UTF-32 sequences. In this case, it does not
  6261. diagnose an invalid UTF-32 string. However, if an invalid string is
  6262. passed, the result is undefined.
  6263. General comments about UTF modes
  6264. 1. Codepoints less than 256 can be specified in patterns by either
  6265. braced or unbraced hexadecimal escape sequences (for example, \x{b3} or
  6266. \xb3). Larger values have to use braced sequences.
  6267. 2. Octal numbers up to \777 are recognized, and in UTF-8 mode they
  6268. match two-byte characters for values greater than \177.
  6269. 3. Repeat quantifiers apply to complete UTF characters, not to individ-
  6270. ual data units, for example: \x{100}{3}.
  6271. 4. The dot metacharacter matches one UTF character instead of a single
  6272. data unit.
  6273. 5. The escape sequence \C can be used to match a single byte in UTF-8
  6274. mode, or a single 16-bit data unit in UTF-16 mode, or a single 32-bit
  6275. data unit in UTF-32 mode, but its use can lead to some strange effects
  6276. because it breaks up multi-unit characters (see the description of \C
  6277. in the pcrepattern documentation). The use of \C is not supported in
  6278. the alternative matching function pcre[16|32]_dfa_exec(), nor is it
  6279. supported in UTF mode by the JIT optimization of pcre[16|32]_exec(). If
  6280. JIT optimization is requested for a UTF pattern that contains \C, it
  6281. will not succeed, and so the matching will be carried out by the normal
  6282. interpretive function.
  6283. 6. The character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly
  6284. test characters of any code value, but, by default, the characters that
  6285. PCRE recognizes as digits, spaces, or word characters remain the same
  6286. set as in non-UTF mode, all with values less than 256. This remains
  6287. true even when PCRE is built to include Unicode property support,
  6288. because to do otherwise would slow down PCRE in many common cases. Note
  6289. in particular that this applies to \b and \B, because they are defined
  6290. in terms of \w and \W. If you really want to test for a wider sense of,
  6291. say, "digit", you can use explicit Unicode property tests such as
  6292. \p{Nd}. Alternatively, if you set the PCRE_UCP option, the way that the
  6293. character escapes work is changed so that Unicode properties are used
  6294. to determine which characters match. There are more details in the sec-
  6295. tion on generic character types in the pcrepattern documentation.
  6296. 7. Similarly, characters that match the POSIX named character classes
  6297. are all low-valued characters, unless the PCRE_UCP option is set.
  6298. 8. However, the horizontal and vertical white space matching escapes
  6299. (\h, \H, \v, and \V) do match all the appropriate Unicode characters,
  6300. whether or not PCRE_UCP is set.
  6301. 9. Case-insensitive matching applies only to characters whose values
  6302. are less than 128, unless PCRE is built with Unicode property support.
  6303. A few Unicode characters such as Greek sigma have more than two code-
  6304. points that are case-equivalent. Up to and including PCRE release 8.31,
  6305. only one-to-one case mappings were supported, but later releases (with
  6306. Unicode property support) do treat as case-equivalent all versions of
  6307. characters such as Greek sigma.
  6308. AUTHOR
  6309. Philip Hazel
  6310. University Computing Service
  6311. Cambridge CB2 3QH, England.
  6312. REVISION
  6313. Last updated: 27 February 2013
  6314. Copyright (c) 1997-2013 University of Cambridge.
  6315. ------------------------------------------------------------------------------
  6316. PCREJIT(3) Library Functions Manual PCREJIT(3)
  6317. NAME
  6318. PCRE - Perl-compatible regular expressions
  6319. PCRE JUST-IN-TIME COMPILER SUPPORT
  6320. Just-in-time compiling is a heavyweight optimization that can greatly
  6321. speed up pattern matching. However, it comes at the cost of extra pro-
  6322. cessing before the match is performed. Therefore, it is of most benefit
  6323. when the same pattern is going to be matched many times. This does not
  6324. necessarily mean many calls of a matching function; if the pattern is
  6325. not anchored, matching attempts may take place many times at various
  6326. positions in the subject, even for a single call. Therefore, if the
  6327. subject string is very long, it may still pay to use JIT for one-off
  6328. matches.
  6329. JIT support applies only to the traditional Perl-compatible matching
  6330. function. It does not apply when the DFA matching function is being
  6331. used. The code for this support was written by Zoltan Herczeg.
  6332. 8-BIT, 16-BIT AND 32-BIT SUPPORT
  6333. JIT support is available for all of the 8-bit, 16-bit and 32-bit PCRE
  6334. libraries. To keep this documentation simple, only the 8-bit interface
  6335. is described in what follows. If you are using the 16-bit library, sub-
  6336. stitute the 16-bit functions and 16-bit structures (for example,
  6337. pcre16_jit_stack instead of pcre_jit_stack). If you are using the
  6338. 32-bit library, substitute the 32-bit functions and 32-bit structures
  6339. (for example, pcre32_jit_stack instead of pcre_jit_stack).
  6340. AVAILABILITY OF JIT SUPPORT
  6341. JIT support is an optional feature of PCRE. The "configure" option
  6342. --enable-jit (or equivalent CMake option) must be set when PCRE is
  6343. built if you want to use JIT. The support is limited to the following
  6344. hardware platforms:
  6345. ARM v5, v7, and Thumb2
  6346. Intel x86 32-bit and 64-bit
  6347. MIPS 32-bit
  6348. Power PC 32-bit and 64-bit
  6349. SPARC 32-bit (experimental)
  6350. If --enable-jit is set on an unsupported platform, compilation fails.
  6351. A program that is linked with PCRE 8.20 or later can tell if JIT sup-
  6352. port is available by calling pcre_config() with the PCRE_CONFIG_JIT
  6353. option. The result is 1 when JIT is available, and 0 otherwise. How-
  6354. ever, a simple program does not need to check this in order to use JIT.
  6355. The normal API is implemented in a way that falls back to the interpre-
  6356. tive code if JIT is not available. For programs that need the best pos-
  6357. sible performance, there is also a "fast path" API that is JIT-spe-
  6358. cific.
  6359. If your program may sometimes be linked with versions of PCRE that are
  6360. older than 8.20, but you want to use JIT when it is available, you can
  6361. test the values of PCRE_MAJOR and PCRE_MINOR, or the existence of a JIT
  6362. macro such as PCRE_CONFIG_JIT, for compile-time control of your code.
  6363. SIMPLE USE OF JIT
  6364. You have to do two things to make use of the JIT support in the sim-
  6365. plest way:
  6366. (1) Call pcre_study() with the PCRE_STUDY_JIT_COMPILE option for
  6367. each compiled pattern, and pass the resulting pcre_extra block to
  6368. pcre_exec().
  6369. (2) Use pcre_free_study() to free the pcre_extra block when it is
  6370. no longer needed, instead of just freeing it yourself. This
  6371. ensures that
  6372. any JIT data is also freed.
  6373. For a program that may be linked with pre-8.20 versions of PCRE, you
  6374. can insert
  6375. #ifndef PCRE_STUDY_JIT_COMPILE
  6376. #define PCRE_STUDY_JIT_COMPILE 0
  6377. #endif
  6378. so that no option is passed to pcre_study(), and then use something
  6379. like this to free the study data:
  6380. #ifdef PCRE_CONFIG_JIT
  6381. pcre_free_study(study_ptr);
  6382. #else
  6383. pcre_free(study_ptr);
  6384. #endif
  6385. PCRE_STUDY_JIT_COMPILE requests the JIT compiler to generate code for
  6386. complete matches. If you want to run partial matches using the
  6387. PCRE_PARTIAL_HARD or PCRE_PARTIAL_SOFT options of pcre_exec(), you
  6388. should set one or both of the following options in addition to, or
  6389. instead of, PCRE_STUDY_JIT_COMPILE when you call pcre_study():
  6390. PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
  6391. PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
  6392. The JIT compiler generates different optimized code for each of the
  6393. three modes (normal, soft partial, hard partial). When pcre_exec() is
  6394. called, the appropriate code is run if it is available. Otherwise, the
  6395. pattern is matched using interpretive code.
  6396. In some circumstances you may need to call additional functions. These
  6397. are described in the section entitled "Controlling the JIT stack"
  6398. below.
  6399. If JIT support is not available, PCRE_STUDY_JIT_COMPILE etc. are
  6400. ignored, and no JIT data is created. Otherwise, the compiled pattern is
  6401. passed to the JIT compiler, which turns it into machine code that exe-
  6402. cutes much faster than the normal interpretive code. When pcre_exec()
  6403. is passed a pcre_extra block containing a pointer to JIT code of the
  6404. appropriate mode (normal or hard/soft partial), it obeys that code
  6405. instead of running the interpreter. The result is identical, but the
  6406. compiled JIT code runs much faster.
  6407. There are some pcre_exec() options that are not supported for JIT exe-
  6408. cution. There are also some pattern items that JIT cannot handle.
  6409. Details are given below. In both cases, execution automatically falls
  6410. back to the interpretive code. If you want to know whether JIT was
  6411. actually used for a particular match, you should arrange for a JIT
  6412. callback function to be set up as described in the section entitled
  6413. "Controlling the JIT stack" below, even if you do not need to supply a
  6414. non-default JIT stack. Such a callback function is called whenever JIT
  6415. code is about to be obeyed. If the execution options are not right for
  6416. JIT execution, the callback function is not obeyed.
  6417. If the JIT compiler finds an unsupported item, no JIT data is gener-
  6418. ated. You can find out if JIT execution is available after studying a
  6419. pattern by calling pcre_fullinfo() with the PCRE_INFO_JIT option. A
  6420. result of 1 means that JIT compilation was successful. A result of 0
  6421. means that JIT support is not available, or the pattern was not studied
  6422. with PCRE_STUDY_JIT_COMPILE etc., or the JIT compiler was not able to
  6423. handle the pattern.
  6424. Once a pattern has been studied, with or without JIT, it can be used as
  6425. many times as you like for matching different subject strings.
  6426. UNSUPPORTED OPTIONS AND PATTERN ITEMS
  6427. The only pcre_exec() options that are supported for JIT execution are
  6428. PCRE_NO_UTF8_CHECK, PCRE_NO_UTF16_CHECK, PCRE_NO_UTF32_CHECK, PCRE_NOT-
  6429. BOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART, PCRE_PAR-
  6430. TIAL_HARD, and PCRE_PARTIAL_SOFT.
  6431. The only unsupported pattern items are \C (match a single data unit)
  6432. when running in a UTF mode, and a callout immediately before an asser-
  6433. tion condition in a conditional group.
  6434. RETURN VALUES FROM JIT EXECUTION
  6435. When a pattern is matched using JIT execution, the return values are
  6436. the same as those given by the interpretive pcre_exec() code, with the
  6437. addition of one new error code: PCRE_ERROR_JIT_STACKLIMIT. This means
  6438. that the memory used for the JIT stack was insufficient. See "Control-
  6439. ling the JIT stack" below for a discussion of JIT stack usage. For com-
  6440. patibility with the interpretive pcre_exec() code, no more than two-
  6441. thirds of the ovector argument is used for passing back captured sub-
  6442. strings.
  6443. The error code PCRE_ERROR_MATCHLIMIT is returned by the JIT code if
  6444. searching a very large pattern tree goes on for too long, as it is in
  6445. the same circumstance when JIT is not used, but the details of exactly
  6446. what is counted are not the same. The PCRE_ERROR_RECURSIONLIMIT error
  6447. code is never returned by JIT execution.
  6448. SAVING AND RESTORING COMPILED PATTERNS
  6449. The code that is generated by the JIT compiler is architecture-spe-
  6450. cific, and is also position dependent. For those reasons it cannot be
  6451. saved (in a file or database) and restored later like the bytecode and
  6452. other data of a compiled pattern. Saving and restoring compiled pat-
  6453. terns is not something many people do. More detail about this facility
  6454. is given in the pcreprecompile documentation. It should be possible to
  6455. run pcre_study() on a saved and restored pattern, and thereby recreate
  6456. the JIT data, but because JIT compilation uses significant resources,
  6457. it is probably not worth doing this; you might as well recompile the
  6458. original pattern.
  6459. CONTROLLING THE JIT STACK
  6460. When the compiled JIT code runs, it needs a block of memory to use as a
  6461. stack. By default, it uses 32K on the machine stack. However, some
  6462. large or complicated patterns need more than this. The error
  6463. PCRE_ERROR_JIT_STACKLIMIT is given when there is not enough stack.
  6464. Three functions are provided for managing blocks of memory for use as
  6465. JIT stacks. There is further discussion about the use of JIT stacks in
  6466. the section entitled "JIT stack FAQ" below.
  6467. The pcre_jit_stack_alloc() function creates a JIT stack. Its arguments
  6468. are a starting size and a maximum size, and it returns a pointer to an
  6469. opaque structure of type pcre_jit_stack, or NULL if there is an error.
  6470. The pcre_jit_stack_free() function can be used to free a stack that is
  6471. no longer needed. (For the technically minded: the address space is
  6472. allocated by mmap or VirtualAlloc.)
  6473. JIT uses far less memory for recursion than the interpretive code, and
  6474. a maximum stack size of 512K to 1M should be more than enough for any
  6475. pattern.
  6476. The pcre_assign_jit_stack() function specifies which stack JIT code
  6477. should use. Its arguments are as follows:
  6478. pcre_extra *extra
  6479. pcre_jit_callback callback
  6480. void *data
  6481. The extra argument must be the result of studying a pattern with
  6482. PCRE_STUDY_JIT_COMPILE etc. There are three cases for the values of the
  6483. other two options:
  6484. (1) If callback is NULL and data is NULL, an internal 32K block
  6485. on the machine stack is used.
  6486. (2) If callback is NULL and data is not NULL, data must be
  6487. a valid JIT stack, the result of calling pcre_jit_stack_alloc().
  6488. (3) If callback is not NULL, it must point to a function that is
  6489. called with data as an argument at the start of matching, in
  6490. order to set up a JIT stack. If the return from the callback
  6491. function is NULL, the internal 32K stack is used; otherwise the
  6492. return value must be a valid JIT stack, the result of calling
  6493. pcre_jit_stack_alloc().
  6494. A callback function is obeyed whenever JIT code is about to be run; it
  6495. is not obeyed when pcre_exec() is called with options that are incom-
  6496. patible for JIT execution. A callback function can therefore be used to
  6497. determine whether a match operation was executed by JIT or by the
  6498. interpreter.
  6499. You may safely use the same JIT stack for more than one pattern (either
  6500. by assigning directly or by callback), as long as the patterns are all
  6501. matched sequentially in the same thread. In a multithread application,
  6502. if you do not specify a JIT stack, or if you assign or pass back NULL
  6503. from a callback, that is thread-safe, because each thread has its own
  6504. machine stack. However, if you assign or pass back a non-NULL JIT
  6505. stack, this must be a different stack for each thread so that the
  6506. application is thread-safe.
  6507. Strictly speaking, even more is allowed. You can assign the same non-
  6508. NULL stack to any number of patterns as long as they are not used for
  6509. matching by multiple threads at the same time. For example, you can
  6510. assign the same stack to all compiled patterns, and use a global mutex
  6511. in the callback to wait until the stack is available for use. However,
  6512. this is an inefficient solution, and not recommended.
  6513. This is a suggestion for how a multithreaded program that needs to set
  6514. up non-default JIT stacks might operate:
  6515. During thread initalization
  6516. thread_local_var = pcre_jit_stack_alloc(...)
  6517. During thread exit
  6518. pcre_jit_stack_free(thread_local_var)
  6519. Use a one-line callback function
  6520. return thread_local_var
  6521. All the functions described in this section do nothing if JIT is not
  6522. available, and pcre_assign_jit_stack() does nothing unless the extra
  6523. argument is non-NULL and points to a pcre_extra block that is the
  6524. result of a successful study with PCRE_STUDY_JIT_COMPILE etc.
  6525. JIT STACK FAQ
  6526. (1) Why do we need JIT stacks?
  6527. PCRE (and JIT) is a recursive, depth-first engine, so it needs a stack
  6528. where the local data of the current node is pushed before checking its
  6529. child nodes. Allocating real machine stack on some platforms is diffi-
  6530. cult. For example, the stack chain needs to be updated every time if we
  6531. extend the stack on PowerPC. Although it is possible, its updating
  6532. time overhead decreases performance. So we do the recursion in memory.
  6533. (2) Why don't we simply allocate blocks of memory with malloc()?
  6534. Modern operating systems have a nice feature: they can reserve an
  6535. address space instead of allocating memory. We can safely allocate mem-
  6536. ory pages inside this address space, so the stack could grow without
  6537. moving memory data (this is important because of pointers). Thus we can
  6538. allocate 1M address space, and use only a single memory page (usually
  6539. 4K) if that is enough. However, we can still grow up to 1M anytime if
  6540. needed.
  6541. (3) Who "owns" a JIT stack?
  6542. The owner of the stack is the user program, not the JIT studied pattern
  6543. or anything else. The user program must ensure that if a stack is used
  6544. by pcre_exec(), (that is, it is assigned to the pattern currently run-
  6545. ning), that stack must not be used by any other threads (to avoid over-
  6546. writing the same memory area). The best practice for multithreaded pro-
  6547. grams is to allocate a stack for each thread, and return this stack
  6548. through the JIT callback function.
  6549. (4) When should a JIT stack be freed?
  6550. You can free a JIT stack at any time, as long as it will not be used by
  6551. pcre_exec() again. When you assign the stack to a pattern, only a
  6552. pointer is set. There is no reference counting or any other magic. You
  6553. can free the patterns and stacks in any order, anytime. Just do not
  6554. call pcre_exec() with a pattern pointing to an already freed stack, as
  6555. that will cause SEGFAULT. (Also, do not free a stack currently used by
  6556. pcre_exec() in another thread). You can also replace the stack for a
  6557. pattern at any time. You can even free the previous stack before
  6558. assigning a replacement.
  6559. (5) Should I allocate/free a stack every time before/after calling
  6560. pcre_exec()?
  6561. No, because this is too costly in terms of resources. However, you
  6562. could implement some clever idea which release the stack if it is not
  6563. used in let's say two minutes. The JIT callback can help to achieve
  6564. this without keeping a list of the currently JIT studied patterns.
  6565. (6) OK, the stack is for long term memory allocation. But what happens
  6566. if a pattern causes stack overflow with a stack of 1M? Is that 1M kept
  6567. until the stack is freed?
  6568. Especially on embedded sytems, it might be a good idea to release mem-
  6569. ory sometimes without freeing the stack. There is no API for this at
  6570. the moment. Probably a function call which returns with the currently
  6571. allocated memory for any stack and another which allows releasing mem-
  6572. ory (shrinking the stack) would be a good idea if someone needs this.
  6573. (7) This is too much of a headache. Isn't there any better solution for
  6574. JIT stack handling?
  6575. No, thanks to Windows. If POSIX threads were used everywhere, we could
  6576. throw out this complicated API.
  6577. EXAMPLE CODE
  6578. This is a single-threaded example that specifies a JIT stack without
  6579. using a callback.
  6580. int rc;
  6581. int ovector[30];
  6582. pcre *re;
  6583. pcre_extra *extra;
  6584. pcre_jit_stack *jit_stack;
  6585. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  6586. /* Check for errors */
  6587. extra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &error);
  6588. jit_stack = pcre_jit_stack_alloc(32*1024, 512*1024);
  6589. /* Check for error (NULL) */
  6590. pcre_assign_jit_stack(extra, NULL, jit_stack);
  6591. rc = pcre_exec(re, extra, subject, length, 0, 0, ovector, 30);
  6592. /* Check results */
  6593. pcre_free(re);
  6594. pcre_free_study(extra);
  6595. pcre_jit_stack_free(jit_stack);
  6596. JIT FAST PATH API
  6597. Because the API described above falls back to interpreted execution
  6598. when JIT is not available, it is convenient for programs that are writ-
  6599. ten for general use in many environments. However, calling JIT via
  6600. pcre_exec() does have a performance impact. Programs that are written
  6601. for use where JIT is known to be available, and which need the best
  6602. possible performance, can instead use a "fast path" API to call JIT
  6603. execution directly instead of calling pcre_exec() (obviously only for
  6604. patterns that have been successfully studied by JIT).
  6605. The fast path function is called pcre_jit_exec(), and it takes exactly
  6606. the same arguments as pcre_exec(), plus one additional argument that
  6607. must point to a JIT stack. The JIT stack arrangements described above
  6608. do not apply. The return values are the same as for pcre_exec().
  6609. When you call pcre_exec(), as well as testing for invalid options, a
  6610. number of other sanity checks are performed on the arguments. For exam-
  6611. ple, if the subject pointer is NULL, or its length is negative, an
  6612. immediate error is given. Also, unless PCRE_NO_UTF[8|16|32] is set, a
  6613. UTF subject string is tested for validity. In the interests of speed,
  6614. these checks do not happen on the JIT fast path, and if invalid data is
  6615. passed, the result is undefined.
  6616. Bypassing the sanity checks and the pcre_exec() wrapping can give
  6617. speedups of more than 10%.
  6618. SEE ALSO
  6619. pcreapi(3)
  6620. AUTHOR
  6621. Philip Hazel (FAQ by Zoltan Herczeg)
  6622. University Computing Service
  6623. Cambridge CB2 3QH, England.
  6624. REVISION
  6625. Last updated: 17 March 2013
  6626. Copyright (c) 1997-2013 University of Cambridge.
  6627. ------------------------------------------------------------------------------
  6628. PCREPARTIAL(3) Library Functions Manual PCREPARTIAL(3)
  6629. NAME
  6630. PCRE - Perl-compatible regular expressions
  6631. PARTIAL MATCHING IN PCRE
  6632. In normal use of PCRE, if the subject string that is passed to a match-
  6633. ing function matches as far as it goes, but is too short to match the
  6634. entire pattern, PCRE_ERROR_NOMATCH is returned. There are circumstances
  6635. where it might be helpful to distinguish this case from other cases in
  6636. which there is no match.
  6637. Consider, for example, an application where a human is required to type
  6638. in data for a field with specific formatting requirements. An example
  6639. might be a date in the form ddmmmyy, defined by this pattern:
  6640. ^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$
  6641. If the application sees the user's keystrokes one by one, and can check
  6642. that what has been typed so far is potentially valid, it is able to
  6643. raise an error as soon as a mistake is made, by beeping and not
  6644. reflecting the character that has been typed, for example. This immedi-
  6645. ate feedback is likely to be a better user interface than a check that
  6646. is delayed until the entire string has been entered. Partial matching
  6647. can also be useful when the subject string is very long and is not all
  6648. available at once.
  6649. PCRE supports partial matching by means of the PCRE_PARTIAL_SOFT and
  6650. PCRE_PARTIAL_HARD options, which can be set when calling any of the
  6651. matching functions. For backwards compatibility, PCRE_PARTIAL is a syn-
  6652. onym for PCRE_PARTIAL_SOFT. The essential difference between the two
  6653. options is whether or not a partial match is preferred to an alterna-
  6654. tive complete match, though the details differ between the two types of
  6655. matching function. If both options are set, PCRE_PARTIAL_HARD takes
  6656. precedence.
  6657. If you want to use partial matching with just-in-time optimized code,
  6658. you must call pcre_study(), pcre16_study() or pcre32_study() with one
  6659. or both of these options:
  6660. PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
  6661. PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
  6662. PCRE_STUDY_JIT_COMPILE should also be set if you are going to run non-
  6663. partial matches on the same pattern. If the appropriate JIT study mode
  6664. has not been set for a match, the interpretive matching code is used.
  6665. Setting a partial matching option disables two of PCRE's standard opti-
  6666. mizations. PCRE remembers the last literal data unit in a pattern, and
  6667. abandons matching immediately if it is not present in the subject
  6668. string. This optimization cannot be used for a subject string that
  6669. might match only partially. If the pattern was studied, PCRE knows the
  6670. minimum length of a matching string, and does not bother to run the
  6671. matching function on shorter strings. This optimization is also dis-
  6672. abled for partial matching.
  6673. PARTIAL MATCHING USING pcre_exec() OR pcre[16|32]_exec()
  6674. A partial match occurs during a call to pcre_exec() or
  6675. pcre[16|32]_exec() when the end of the subject string is reached suc-
  6676. cessfully, but matching cannot continue because more characters are
  6677. needed. However, at least one character in the subject must have been
  6678. inspected. This character need not form part of the final matched
  6679. string; lookbehind assertions and the \K escape sequence provide ways
  6680. of inspecting characters before the start of a matched substring. The
  6681. requirement for inspecting at least one character exists because an
  6682. empty string can always be matched; without such a restriction there
  6683. would always be a partial match of an empty string at the end of the
  6684. subject.
  6685. If there are at least two slots in the offsets vector when a partial
  6686. match is returned, the first slot is set to the offset of the earliest
  6687. character that was inspected. For convenience, the second offset points
  6688. to the end of the subject so that a substring can easily be identified.
  6689. If there are at least three slots in the offsets vector, the third slot
  6690. is set to the offset of the character where matching started.
  6691. For the majority of patterns, the contents of the first and third slots
  6692. will be the same. However, for patterns that contain lookbehind asser-
  6693. tions, or begin with \b or \B, characters before the one where matching
  6694. started may have been inspected while carrying out the match. For exam-
  6695. ple, consider this pattern:
  6696. /(?<=abc)123/
  6697. This pattern matches "123", but only if it is preceded by "abc". If the
  6698. subject string is "xyzabc12", the first two offsets after a partial
  6699. match are for the substring "abc12", because all these characters were
  6700. inspected. However, the third offset is set to 6, because that is the
  6701. offset where matching began.
  6702. What happens when a partial match is identified depends on which of the
  6703. two partial matching options are set.
  6704. PCRE_PARTIAL_SOFT WITH pcre_exec() OR pcre[16|32]_exec()
  6705. If PCRE_PARTIAL_SOFT is set when pcre_exec() or pcre[16|32]_exec()
  6706. identifies a partial match, the partial match is remembered, but match-
  6707. ing continues as normal, and other alternatives in the pattern are
  6708. tried. If no complete match can be found, PCRE_ERROR_PARTIAL is
  6709. returned instead of PCRE_ERROR_NOMATCH.
  6710. This option is "soft" because it prefers a complete match over a par-
  6711. tial match. All the various matching items in a pattern behave as if
  6712. the subject string is potentially complete. For example, \z, \Z, and $
  6713. match at the end of the subject, as normal, and for \b and \B the end
  6714. of the subject is treated as a non-alphanumeric.
  6715. If there is more than one partial match, the first one that was found
  6716. provides the data that is returned. Consider this pattern:
  6717. /123\w+X|dogY/
  6718. If this is matched against the subject string "abc123dog", both alter-
  6719. natives fail to match, but the end of the subject is reached during
  6720. matching, so PCRE_ERROR_PARTIAL is returned. The offsets are set to 3
  6721. and 9, identifying "123dog" as the first partial match that was found.
  6722. (In this example, there are two partial matches, because "dog" on its
  6723. own partially matches the second alternative.)
  6724. PCRE_PARTIAL_HARD WITH pcre_exec() OR pcre[16|32]_exec()
  6725. If PCRE_PARTIAL_HARD is set for pcre_exec() or pcre[16|32]_exec(),
  6726. PCRE_ERROR_PARTIAL is returned as soon as a partial match is found,
  6727. without continuing to search for possible complete matches. This option
  6728. is "hard" because it prefers an earlier partial match over a later com-
  6729. plete match. For this reason, the assumption is made that the end of
  6730. the supplied subject string may not be the true end of the available
  6731. data, and so, if \z, \Z, \b, \B, or $ are encountered at the end of the
  6732. subject, the result is PCRE_ERROR_PARTIAL, provided that at least one
  6733. character in the subject has been inspected.
  6734. Setting PCRE_PARTIAL_HARD also affects the way UTF-8 and UTF-16 subject
  6735. strings are checked for validity. Normally, an invalid sequence causes
  6736. the error PCRE_ERROR_BADUTF8 or PCRE_ERROR_BADUTF16. However, in the
  6737. special case of a truncated character at the end of the subject,
  6738. PCRE_ERROR_SHORTUTF8 or PCRE_ERROR_SHORTUTF16 is returned when
  6739. PCRE_PARTIAL_HARD is set.
  6740. Comparing hard and soft partial matching
  6741. The difference between the two partial matching options can be illus-
  6742. trated by a pattern such as:
  6743. /dog(sbody)?/
  6744. This matches either "dog" or "dogsbody", greedily (that is, it prefers
  6745. the longer string if possible). If it is matched against the string
  6746. "dog" with PCRE_PARTIAL_SOFT, it yields a complete match for "dog".
  6747. However, if PCRE_PARTIAL_HARD is set, the result is PCRE_ERROR_PARTIAL.
  6748. On the other hand, if the pattern is made ungreedy the result is dif-
  6749. ferent:
  6750. /dog(sbody)??/
  6751. In this case the result is always a complete match because that is
  6752. found first, and matching never continues after finding a complete
  6753. match. It might be easier to follow this explanation by thinking of the
  6754. two patterns like this:
  6755. /dog(sbody)?/ is the same as /dogsbody|dog/
  6756. /dog(sbody)??/ is the same as /dog|dogsbody/
  6757. The second pattern will never match "dogsbody", because it will always
  6758. find the shorter match first.
  6759. PARTIAL MATCHING USING pcre_dfa_exec() OR pcre[16|32]_dfa_exec()
  6760. The DFA functions move along the subject string character by character,
  6761. without backtracking, searching for all possible matches simultane-
  6762. ously. If the end of the subject is reached before the end of the pat-
  6763. tern, there is the possibility of a partial match, again provided that
  6764. at least one character has been inspected.
  6765. When PCRE_PARTIAL_SOFT is set, PCRE_ERROR_PARTIAL is returned only if
  6766. there have been no complete matches. Otherwise, the complete matches
  6767. are returned. However, if PCRE_PARTIAL_HARD is set, a partial match
  6768. takes precedence over any complete matches. The portion of the string
  6769. that was inspected when the longest partial match was found is set as
  6770. the first matching string, provided there are at least two slots in the
  6771. offsets vector.
  6772. Because the DFA functions always search for all possible matches, and
  6773. there is no difference between greedy and ungreedy repetition, their
  6774. behaviour is different from the standard functions when PCRE_PAR-
  6775. TIAL_HARD is set. Consider the string "dog" matched against the
  6776. ungreedy pattern shown above:
  6777. /dog(sbody)??/
  6778. Whereas the standard functions stop as soon as they find the complete
  6779. match for "dog", the DFA functions also find the partial match for
  6780. "dogsbody", and so return that when PCRE_PARTIAL_HARD is set.
  6781. PARTIAL MATCHING AND WORD BOUNDARIES
  6782. If a pattern ends with one of sequences \b or \B, which test for word
  6783. boundaries, partial matching with PCRE_PARTIAL_SOFT can give counter-
  6784. intuitive results. Consider this pattern:
  6785. /\bcat\b/
  6786. This matches "cat", provided there is a word boundary at either end. If
  6787. the subject string is "the cat", the comparison of the final "t" with a
  6788. following character cannot take place, so a partial match is found.
  6789. However, normal matching carries on, and \b matches at the end of the
  6790. subject when the last character is a letter, so a complete match is
  6791. found. The result, therefore, is not PCRE_ERROR_PARTIAL. Using
  6792. PCRE_PARTIAL_HARD in this case does yield PCRE_ERROR_PARTIAL, because
  6793. then the partial match takes precedence.
  6794. FORMERLY RESTRICTED PATTERNS
  6795. For releases of PCRE prior to 8.00, because of the way certain internal
  6796. optimizations were implemented in the pcre_exec() function, the
  6797. PCRE_PARTIAL option (predecessor of PCRE_PARTIAL_SOFT) could not be
  6798. used with all patterns. From release 8.00 onwards, the restrictions no
  6799. longer apply, and partial matching with can be requested for any pat-
  6800. tern.
  6801. Items that were formerly restricted were repeated single characters and
  6802. repeated metasequences. If PCRE_PARTIAL was set for a pattern that did
  6803. not conform to the restrictions, pcre_exec() returned the error code
  6804. PCRE_ERROR_BADPARTIAL (-13). This error code is no longer in use. The
  6805. PCRE_INFO_OKPARTIAL call to pcre_fullinfo() to find out if a compiled
  6806. pattern can be used for partial matching now always returns 1.
  6807. EXAMPLE OF PARTIAL MATCHING USING PCRETEST
  6808. If the escape sequence \P is present in a pcretest data line, the
  6809. PCRE_PARTIAL_SOFT option is used for the match. Here is a run of
  6810. pcretest that uses the date example quoted above:
  6811. re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/
  6812. data> 25jun04\P
  6813. 0: 25jun04
  6814. 1: jun
  6815. data> 25dec3\P
  6816. Partial match: 23dec3
  6817. data> 3ju\P
  6818. Partial match: 3ju
  6819. data> 3juj\P
  6820. No match
  6821. data> j\P
  6822. No match
  6823. The first data string is matched completely, so pcretest shows the
  6824. matched substrings. The remaining four strings do not match the com-
  6825. plete pattern, but the first two are partial matches. Similar output is
  6826. obtained if DFA matching is used.
  6827. If the escape sequence \P is present more than once in a pcretest data
  6828. line, the PCRE_PARTIAL_HARD option is set for the match.
  6829. MULTI-SEGMENT MATCHING WITH pcre_dfa_exec() OR pcre[16|32]_dfa_exec()
  6830. When a partial match has been found using a DFA matching function, it
  6831. is possible to continue the match by providing additional subject data
  6832. and calling the function again with the same compiled regular expres-
  6833. sion, this time setting the PCRE_DFA_RESTART option. You must pass the
  6834. same working space as before, because this is where details of the pre-
  6835. vious partial match are stored. Here is an example using pcretest,
  6836. using the \R escape sequence to set the PCRE_DFA_RESTART option (\D
  6837. specifies the use of the DFA matching function):
  6838. re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/
  6839. data> 23ja\P\D
  6840. Partial match: 23ja
  6841. data> n05\R\D
  6842. 0: n05
  6843. The first call has "23ja" as the subject, and requests partial match-
  6844. ing; the second call has "n05" as the subject for the continued
  6845. (restarted) match. Notice that when the match is complete, only the
  6846. last part is shown; PCRE does not retain the previously partially-
  6847. matched string. It is up to the calling program to do that if it needs
  6848. to.
  6849. That means that, for an unanchored pattern, if a continued match fails,
  6850. it is not possible to try again at a new starting point. All this
  6851. facility is capable of doing is continuing with the previous match
  6852. attempt. In the previous example, if the second set of data is "ug23"
  6853. the result is no match, even though there would be a match for "aug23"
  6854. if the entire string were given at once. Depending on the application,
  6855. this may or may not be what you want. The only way to allow for start-
  6856. ing again at the next character is to retain the matched part of the
  6857. subject and try a new complete match.
  6858. You can set the PCRE_PARTIAL_SOFT or PCRE_PARTIAL_HARD options with
  6859. PCRE_DFA_RESTART to continue partial matching over multiple segments.
  6860. This facility can be used to pass very long subject strings to the DFA
  6861. matching functions.
  6862. MULTI-SEGMENT MATCHING WITH pcre_exec() OR pcre[16|32]_exec()
  6863. From release 8.00, the standard matching functions can also be used to
  6864. do multi-segment matching. Unlike the DFA functions, it is not possible
  6865. to restart the previous match with a new segment of data. Instead, new
  6866. data must be added to the previous subject string, and the entire match
  6867. re-run, starting from the point where the partial match occurred. Ear-
  6868. lier data can be discarded.
  6869. It is best to use PCRE_PARTIAL_HARD in this situation, because it does
  6870. not treat the end of a segment as the end of the subject when matching
  6871. \z, \Z, \b, \B, and $. Consider an unanchored pattern that matches
  6872. dates:
  6873. re> /\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d/
  6874. data> The date is 23ja\P\P
  6875. Partial match: 23ja
  6876. At this stage, an application could discard the text preceding "23ja",
  6877. add on text from the next segment, and call the matching function
  6878. again. Unlike the DFA matching functions, the entire matching string
  6879. must always be available, and the complete matching process occurs for
  6880. each call, so more memory and more processing time is needed.
  6881. Note: If the pattern contains lookbehind assertions, or \K, or starts
  6882. with \b or \B, the string that is returned for a partial match includes
  6883. characters that precede the start of what would be returned for a com-
  6884. plete match, because it contains all the characters that were inspected
  6885. during the partial match.
  6886. ISSUES WITH MULTI-SEGMENT MATCHING
  6887. Certain types of pattern may give problems with multi-segment matching,
  6888. whichever matching function is used.
  6889. 1. If the pattern contains a test for the beginning of a line, you need
  6890. to pass the PCRE_NOTBOL option when the subject string for any call
  6891. does start at the beginning of a line. There is also a PCRE_NOTEOL
  6892. option, but in practice when doing multi-segment matching you should be
  6893. using PCRE_PARTIAL_HARD, which includes the effect of PCRE_NOTEOL.
  6894. 2. Lookbehind assertions that have already been obeyed are catered for
  6895. in the offsets that are returned for a partial match. However a lookbe-
  6896. hind assertion later in the pattern could require even earlier charac-
  6897. ters to be inspected. You can handle this case by using the
  6898. PCRE_INFO_MAXLOOKBEHIND option of the pcre_fullinfo() or
  6899. pcre[16|32]_fullinfo() functions to obtain the length of the longest
  6900. lookbehind in the pattern. This length is given in characters, not
  6901. bytes. If you always retain at least that many characters before the
  6902. partially matched string, all should be well. (Of course, near the
  6903. start of the subject, fewer characters may be present; in that case all
  6904. characters should be retained.)
  6905. From release 8.33, there is a more accurate way of deciding which char-
  6906. acters to retain. Instead of subtracting the length of the longest
  6907. lookbehind from the earliest inspected character (offsets[0]), the
  6908. match start position (offsets[2]) should be used, and the next match
  6909. attempt started at the offsets[2] character by setting the startoffset
  6910. argument of pcre_exec() or pcre_dfa_exec().
  6911. For example, if the pattern "(?<=123)abc" is partially matched against
  6912. the string "xx123a", the three offset values returned are 2, 6, and 5.
  6913. This indicates that the matching process that gave a partial match
  6914. started at offset 5, but the characters "123a" were all inspected. The
  6915. maximum lookbehind for that pattern is 3, so taking that away from 5
  6916. shows that we need only keep "123a", and the next match attempt can be
  6917. started at offset 3 (that is, at "a") when further characters have been
  6918. added. When the match start is not the earliest inspected character,
  6919. pcretest shows it explicitly:
  6920. re> "(?<=123)abc"
  6921. data> xx123a\P\P
  6922. Partial match at offset 5: 123a
  6923. 3. Because a partial match must always contain at least one character,
  6924. what might be considered a partial match of an empty string actually
  6925. gives a "no match" result. For example:
  6926. re> /c(?<=abc)x/
  6927. data> ab\P
  6928. No match
  6929. If the next segment begins "cx", a match should be found, but this will
  6930. only happen if characters from the previous segment are retained. For
  6931. this reason, a "no match" result should be interpreted as "partial
  6932. match of an empty string" when the pattern contains lookbehinds.
  6933. 4. Matching a subject string that is split into multiple segments may
  6934. not always produce exactly the same result as matching over one single
  6935. long string, especially when PCRE_PARTIAL_SOFT is used. The section
  6936. "Partial Matching and Word Boundaries" above describes an issue that
  6937. arises if the pattern ends with \b or \B. Another kind of difference
  6938. may occur when there are multiple matching possibilities, because (for
  6939. PCRE_PARTIAL_SOFT) a partial match result is given only when there are
  6940. no completed matches. This means that as soon as the shortest match has
  6941. been found, continuation to a new subject segment is no longer possi-
  6942. ble. Consider again this pcretest example:
  6943. re> /dog(sbody)?/
  6944. data> dogsb\P
  6945. 0: dog
  6946. data> do\P\D
  6947. Partial match: do
  6948. data> gsb\R\P\D
  6949. 0: g
  6950. data> dogsbody\D
  6951. 0: dogsbody
  6952. 1: dog
  6953. The first data line passes the string "dogsb" to a standard matching
  6954. function, setting the PCRE_PARTIAL_SOFT option. Although the string is
  6955. a partial match for "dogsbody", the result is not PCRE_ERROR_PARTIAL,
  6956. because the shorter string "dog" is a complete match. Similarly, when
  6957. the subject is presented to a DFA matching function in several parts
  6958. ("do" and "gsb" being the first two) the match stops when "dog" has
  6959. been found, and it is not possible to continue. On the other hand, if
  6960. "dogsbody" is presented as a single string, a DFA matching function
  6961. finds both matches.
  6962. Because of these problems, it is best to use PCRE_PARTIAL_HARD when
  6963. matching multi-segment data. The example above then behaves differ-
  6964. ently:
  6965. re> /dog(sbody)?/
  6966. data> dogsb\P\P
  6967. Partial match: dogsb
  6968. data> do\P\D
  6969. Partial match: do
  6970. data> gsb\R\P\P\D
  6971. Partial match: gsb
  6972. 5. Patterns that contain alternatives at the top level which do not all
  6973. start with the same pattern item may not work as expected when
  6974. PCRE_DFA_RESTART is used. For example, consider this pattern:
  6975. 1234|3789
  6976. If the first part of the subject is "ABC123", a partial match of the
  6977. first alternative is found at offset 3. There is no partial match for
  6978. the second alternative, because such a match does not start at the same
  6979. point in the subject string. Attempting to continue with the string
  6980. "7890" does not yield a match because only those alternatives that
  6981. match at one point in the subject are remembered. The problem arises
  6982. because the start of the second alternative matches within the first
  6983. alternative. There is no problem with anchored patterns or patterns
  6984. such as:
  6985. 1234|ABCD
  6986. where no string can be a partial match for both alternatives. This is
  6987. not a problem if a standard matching function is used, because the
  6988. entire match has to be rerun each time:
  6989. re> /1234|3789/
  6990. data> ABC123\P\P
  6991. Partial match: 123
  6992. data> 1237890
  6993. 0: 3789
  6994. Of course, instead of using PCRE_DFA_RESTART, the same technique of re-
  6995. running the entire match can also be used with the DFA matching func-
  6996. tions. Another possibility is to work with two buffers. If a partial
  6997. match at offset n in the first buffer is followed by "no match" when
  6998. PCRE_DFA_RESTART is used on the second buffer, you can then try a new
  6999. match starting at offset n+1 in the first buffer.
  7000. AUTHOR
  7001. Philip Hazel
  7002. University Computing Service
  7003. Cambridge CB2 3QH, England.
  7004. REVISION
  7005. Last updated: 02 July 2013
  7006. Copyright (c) 1997-2013 University of Cambridge.
  7007. ------------------------------------------------------------------------------
  7008. PCREPRECOMPILE(3) Library Functions Manual PCREPRECOMPILE(3)
  7009. NAME
  7010. PCRE - Perl-compatible regular expressions
  7011. SAVING AND RE-USING PRECOMPILED PCRE PATTERNS
  7012. If you are running an application that uses a large number of regular
  7013. expression patterns, it may be useful to store them in a precompiled
  7014. form instead of having to compile them every time the application is
  7015. run. If you are not using any private character tables (see the
  7016. pcre_maketables() documentation), this is relatively straightforward.
  7017. If you are using private tables, it is a little bit more complicated.
  7018. However, if you are using the just-in-time optimization feature, it is
  7019. not possible to save and reload the JIT data.
  7020. If you save compiled patterns to a file, you can copy them to a differ-
  7021. ent host and run them there. If the two hosts have different endianness
  7022. (byte order), you should run the pcre[16|32]_pat-
  7023. tern_to_host_byte_order() function on the new host before trying to
  7024. match the pattern. The matching functions return PCRE_ERROR_BADENDIAN-
  7025. NESS if they detect a pattern with the wrong endianness.
  7026. Compiling regular expressions with one version of PCRE for use with a
  7027. different version is not guaranteed to work and may cause crashes, and
  7028. saving and restoring a compiled pattern loses any JIT optimization
  7029. data.
  7030. SAVING A COMPILED PATTERN
  7031. The value returned by pcre[16|32]_compile() points to a single block of
  7032. memory that holds the compiled pattern and associated data. You can
  7033. find the length of this block in bytes by calling
  7034. pcre[16|32]_fullinfo() with an argument of PCRE_INFO_SIZE. You can then
  7035. save the data in any appropriate manner. Here is sample code for the
  7036. 8-bit library that compiles a pattern and writes it to a file. It
  7037. assumes that the variable fd refers to a file that is open for output:
  7038. int erroroffset, rc, size;
  7039. char *error;
  7040. pcre *re;
  7041. re = pcre_compile("my pattern", 0, &error, &erroroffset, NULL);
  7042. if (re == NULL) { ... handle errors ... }
  7043. rc = pcre_fullinfo(re, NULL, PCRE_INFO_SIZE, &size);
  7044. if (rc < 0) { ... handle errors ... }
  7045. rc = fwrite(re, 1, size, fd);
  7046. if (rc != size) { ... handle errors ... }
  7047. In this example, the bytes that comprise the compiled pattern are
  7048. copied exactly. Note that this is binary data that may contain any of
  7049. the 256 possible byte values. On systems that make a distinction
  7050. between binary and non-binary data, be sure that the file is opened for
  7051. binary output.
  7052. If you want to write more than one pattern to a file, you will have to
  7053. devise a way of separating them. For binary data, preceding each pat-
  7054. tern with its length is probably the most straightforward approach.
  7055. Another possibility is to write out the data in hexadecimal instead of
  7056. binary, one pattern to a line.
  7057. Saving compiled patterns in a file is only one possible way of storing
  7058. them for later use. They could equally well be saved in a database, or
  7059. in the memory of some daemon process that passes them via sockets to
  7060. the processes that want them.
  7061. If the pattern has been studied, it is also possible to save the normal
  7062. study data in a similar way to the compiled pattern itself. However, if
  7063. the PCRE_STUDY_JIT_COMPILE was used, the just-in-time data that is cre-
  7064. ated cannot be saved because it is too dependent on the current envi-
  7065. ronment. When studying generates additional information,
  7066. pcre[16|32]_study() returns a pointer to a pcre[16|32]_extra data
  7067. block. Its format is defined in the section on matching a pattern in
  7068. the pcreapi documentation. The study_data field points to the binary
  7069. study data, and this is what you must save (not the pcre[16|32]_extra
  7070. block itself). The length of the study data can be obtained by calling
  7071. pcre[16|32]_fullinfo() with an argument of PCRE_INFO_STUDYSIZE. Remem-
  7072. ber to check that pcre[16|32]_study() did return a non-NULL value
  7073. before trying to save the study data.
  7074. RE-USING A PRECOMPILED PATTERN
  7075. Re-using a precompiled pattern is straightforward. Having reloaded it
  7076. into main memory, called pcre[16|32]_pattern_to_host_byte_order() if
  7077. necessary, you pass its pointer to pcre[16|32]_exec() or
  7078. pcre[16|32]_dfa_exec() in the usual way.
  7079. However, if you passed a pointer to custom character tables when the
  7080. pattern was compiled (the tableptr argument of pcre[16|32]_compile()),
  7081. you must now pass a similar pointer to pcre[16|32]_exec() or
  7082. pcre[16|32]_dfa_exec(), because the value saved with the compiled pat-
  7083. tern will obviously be nonsense. A field in a pcre[16|32]_extra() block
  7084. is used to pass this data, as described in the section on matching a
  7085. pattern in the pcreapi documentation.
  7086. Warning: The tables that pcre_exec() and pcre_dfa_exec() use must be
  7087. the same as those that were used when the pattern was compiled. If this
  7088. is not the case, the behaviour is undefined.
  7089. If you did not provide custom character tables when the pattern was
  7090. compiled, the pointer in the compiled pattern is NULL, which causes the
  7091. matching functions to use PCRE's internal tables. Thus, you do not need
  7092. to take any special action at run time in this case.
  7093. If you saved study data with the compiled pattern, you need to create
  7094. your own pcre[16|32]_extra data block and set the study_data field to
  7095. point to the reloaded study data. You must also set the
  7096. PCRE_EXTRA_STUDY_DATA bit in the flags field to indicate that study
  7097. data is present. Then pass the pcre[16|32]_extra block to the matching
  7098. function in the usual way. If the pattern was studied for just-in-time
  7099. optimization, that data cannot be saved, and so is lost by a
  7100. save/restore cycle.
  7101. COMPATIBILITY WITH DIFFERENT PCRE RELEASES
  7102. In general, it is safest to recompile all saved patterns when you
  7103. update to a new PCRE release, though not all updates actually require
  7104. this.
  7105. AUTHOR
  7106. Philip Hazel
  7107. University Computing Service
  7108. Cambridge CB2 3QH, England.
  7109. REVISION
  7110. Last updated: 12 November 2013
  7111. Copyright (c) 1997-2013 University of Cambridge.
  7112. ------------------------------------------------------------------------------
  7113. PCREPERFORM(3) Library Functions Manual PCREPERFORM(3)
  7114. NAME
  7115. PCRE - Perl-compatible regular expressions
  7116. PCRE PERFORMANCE
  7117. Two aspects of performance are discussed below: memory usage and pro-
  7118. cessing time. The way you express your pattern as a regular expression
  7119. can affect both of them.
  7120. COMPILED PATTERN MEMORY USAGE
  7121. Patterns are compiled by PCRE into a reasonably efficient interpretive
  7122. code, so that most simple patterns do not use much memory. However,
  7123. there is one case where the memory usage of a compiled pattern can be
  7124. unexpectedly large. If a parenthesized subpattern has a quantifier with
  7125. a minimum greater than 1 and/or a limited maximum, the whole subpattern
  7126. is repeated in the compiled code. For example, the pattern
  7127. (abc|def){2,4}
  7128. is compiled as if it were
  7129. (abc|def)(abc|def)((abc|def)(abc|def)?)?
  7130. (Technical aside: It is done this way so that backtrack points within
  7131. each of the repetitions can be independently maintained.)
  7132. For regular expressions whose quantifiers use only small numbers, this
  7133. is not usually a problem. However, if the numbers are large, and par-
  7134. ticularly if such repetitions are nested, the memory usage can become
  7135. an embarrassment. For example, the very simple pattern
  7136. ((ab){1,1000}c){1,3}
  7137. uses 51K bytes when compiled using the 8-bit library. When PCRE is com-
  7138. piled with its default internal pointer size of two bytes, the size
  7139. limit on a compiled pattern is 64K data units, and this is reached with
  7140. the above pattern if the outer repetition is increased from 3 to 4.
  7141. PCRE can be compiled to use larger internal pointers and thus handle
  7142. larger compiled patterns, but it is better to try to rewrite your pat-
  7143. tern to use less memory if you can.
  7144. One way of reducing the memory usage for such patterns is to make use
  7145. of PCRE's "subroutine" facility. Re-writing the above pattern as
  7146. ((ab)(?2){0,999}c)(?1){0,2}
  7147. reduces the memory requirements to 18K, and indeed it remains under 20K
  7148. even with the outer repetition increased to 100. However, this pattern
  7149. is not exactly equivalent, because the "subroutine" calls are treated
  7150. as atomic groups into which there can be no backtracking if there is a
  7151. subsequent matching failure. Therefore, PCRE cannot do this kind of
  7152. rewriting automatically. Furthermore, there is a noticeable loss of
  7153. speed when executing the modified pattern. Nevertheless, if the atomic
  7154. grouping is not a problem and the loss of speed is acceptable, this
  7155. kind of rewriting will allow you to process patterns that PCRE cannot
  7156. otherwise handle.
  7157. STACK USAGE AT RUN TIME
  7158. When pcre_exec() or pcre[16|32]_exec() is used for matching, certain
  7159. kinds of pattern can cause it to use large amounts of the process
  7160. stack. In some environments the default process stack is quite small,
  7161. and if it runs out the result is often SIGSEGV. This issue is probably
  7162. the most frequently raised problem with PCRE. Rewriting your pattern
  7163. can often help. The pcrestack documentation discusses this issue in
  7164. detail.
  7165. PROCESSING TIME
  7166. Certain items in regular expression patterns are processed more effi-
  7167. ciently than others. It is more efficient to use a character class like
  7168. [aeiou] than a set of single-character alternatives such as
  7169. (a|e|i|o|u). In general, the simplest construction that provides the
  7170. required behaviour is usually the most efficient. Jeffrey Friedl's book
  7171. contains a lot of useful general discussion about optimizing regular
  7172. expressions for efficient performance. This document contains a few
  7173. observations about PCRE.
  7174. Using Unicode character properties (the \p, \P, and \X escapes) is
  7175. slow, because PCRE has to use a multi-stage table lookup whenever it
  7176. needs a character's property. If you can find an alternative pattern
  7177. that does not use character properties, it will probably be faster.
  7178. By default, the escape sequences \b, \d, \s, and \w, and the POSIX
  7179. character classes such as [:alpha:] do not use Unicode properties,
  7180. partly for backwards compatibility, and partly for performance reasons.
  7181. However, you can set PCRE_UCP if you want Unicode character properties
  7182. to be used. This can double the matching time for items such as \d,
  7183. when matched with a traditional matching function; the performance loss
  7184. is less with a DFA matching function, and in both cases there is not
  7185. much difference for \b.
  7186. When a pattern begins with .* not in parentheses, or in parentheses
  7187. that are not the subject of a backreference, and the PCRE_DOTALL option
  7188. is set, the pattern is implicitly anchored by PCRE, since it can match
  7189. only at the start of a subject string. However, if PCRE_DOTALL is not
  7190. set, PCRE cannot make this optimization, because the . metacharacter
  7191. does not then match a newline, and if the subject string contains new-
  7192. lines, the pattern may match from the character immediately following
  7193. one of them instead of from the very start. For example, the pattern
  7194. .*second
  7195. matches the subject "first\nand second" (where \n stands for a newline
  7196. character), with the match starting at the seventh character. In order
  7197. to do this, PCRE has to retry the match starting after every newline in
  7198. the subject.
  7199. If you are using such a pattern with subject strings that do not con-
  7200. tain newlines, the best performance is obtained by setting PCRE_DOTALL,
  7201. or starting the pattern with ^.* or ^.*? to indicate explicit anchor-
  7202. ing. That saves PCRE from having to scan along the subject looking for
  7203. a newline to restart at.
  7204. Beware of patterns that contain nested indefinite repeats. These can
  7205. take a long time to run when applied to a string that does not match.
  7206. Consider the pattern fragment
  7207. ^(a+)*
  7208. This can match "aaaa" in 16 different ways, and this number increases
  7209. very rapidly as the string gets longer. (The * repeat can match 0, 1,
  7210. 2, 3, or 4 times, and for each of those cases other than 0 or 4, the +
  7211. repeats can match different numbers of times.) When the remainder of
  7212. the pattern is such that the entire match is going to fail, PCRE has in
  7213. principle to try every possible variation, and this can take an
  7214. extremely long time, even for relatively short strings.
  7215. An optimization catches some of the more simple cases such as
  7216. (a+)*b
  7217. where a literal character follows. Before embarking on the standard
  7218. matching procedure, PCRE checks that there is a "b" later in the sub-
  7219. ject string, and if there is not, it fails the match immediately. How-
  7220. ever, when there is no following literal this optimization cannot be
  7221. used. You can see the difference by comparing the behaviour of
  7222. (a+)*\d
  7223. with the pattern above. The former gives a failure almost instantly
  7224. when applied to a whole line of "a" characters, whereas the latter
  7225. takes an appreciable time with strings longer than about 20 characters.
  7226. In many cases, the solution to this kind of performance issue is to use
  7227. an atomic group or a possessive quantifier.
  7228. AUTHOR
  7229. Philip Hazel
  7230. University Computing Service
  7231. Cambridge CB2 3QH, England.
  7232. REVISION
  7233. Last updated: 25 August 2012
  7234. Copyright (c) 1997-2012 University of Cambridge.
  7235. ------------------------------------------------------------------------------
  7236. PCREPOSIX(3) Library Functions Manual PCREPOSIX(3)
  7237. NAME
  7238. PCRE - Perl-compatible regular expressions.
  7239. SYNOPSIS
  7240. #include <pcreposix.h>
  7241. int regcomp(regex_t *preg, const char *pattern,
  7242. int cflags);
  7243. int regexec(regex_t *preg, const char *string,
  7244. size_t nmatch, regmatch_t pmatch[], int eflags);
  7245. size_t regerror(int errcode, const regex_t *preg,
  7246. char *errbuf, size_t errbuf_size);
  7247. void regfree(regex_t *preg);
  7248. DESCRIPTION
  7249. This set of functions provides a POSIX-style API for the PCRE regular
  7250. expression 8-bit library. See the pcreapi documentation for a descrip-
  7251. tion of PCRE's native API, which contains much additional functional-
  7252. ity. There is no POSIX-style wrapper for PCRE's 16-bit and 32-bit
  7253. library.
  7254. The functions described here are just wrapper functions that ultimately
  7255. call the PCRE native API. Their prototypes are defined in the
  7256. pcreposix.h header file, and on Unix systems the library itself is
  7257. called pcreposix.a, so can be accessed by adding -lpcreposix to the
  7258. command for linking an application that uses them. Because the POSIX
  7259. functions call the native ones, it is also necessary to add -lpcre.
  7260. I have implemented only those POSIX option bits that can be reasonably
  7261. mapped to PCRE native options. In addition, the option REG_EXTENDED is
  7262. defined with the value zero. This has no effect, but since programs
  7263. that are written to the POSIX interface often use it, this makes it
  7264. easier to slot in PCRE as a replacement library. Other POSIX options
  7265. are not even defined.
  7266. There are also some other options that are not defined by POSIX. These
  7267. have been added at the request of users who want to make use of certain
  7268. PCRE-specific features via the POSIX calling interface.
  7269. When PCRE is called via these functions, it is only the API that is
  7270. POSIX-like in style. The syntax and semantics of the regular expres-
  7271. sions themselves are still those of Perl, subject to the setting of
  7272. various PCRE options, as described below. "POSIX-like in style" means
  7273. that the API approximates to the POSIX definition; it is not fully
  7274. POSIX-compatible, and in multi-byte encoding domains it is probably
  7275. even less compatible.
  7276. The header for these functions is supplied as pcreposix.h to avoid any
  7277. potential clash with other POSIX libraries. It can, of course, be
  7278. renamed or aliased as regex.h, which is the "correct" name. It provides
  7279. two structure types, regex_t for compiled internal forms, and reg-
  7280. match_t for returning captured substrings. It also defines some con-
  7281. stants whose names start with "REG_"; these are used for setting
  7282. options and identifying error codes.
  7283. COMPILING A PATTERN
  7284. The function regcomp() is called to compile a pattern into an internal
  7285. form. The pattern is a C string terminated by a binary zero, and is
  7286. passed in the argument pattern. The preg argument is a pointer to a
  7287. regex_t structure that is used as a base for storing information about
  7288. the compiled regular expression.
  7289. The argument cflags is either zero, or contains one or more of the bits
  7290. defined by the following macros:
  7291. REG_DOTALL
  7292. The PCRE_DOTALL option is set when the regular expression is passed for
  7293. compilation to the native function. Note that REG_DOTALL is not part of
  7294. the POSIX standard.
  7295. REG_ICASE
  7296. The PCRE_CASELESS option is set when the regular expression is passed
  7297. for compilation to the native function.
  7298. REG_NEWLINE
  7299. The PCRE_MULTILINE option is set when the regular expression is passed
  7300. for compilation to the native function. Note that this does not mimic
  7301. the defined POSIX behaviour for REG_NEWLINE (see the following sec-
  7302. tion).
  7303. REG_NOSUB
  7304. The PCRE_NO_AUTO_CAPTURE option is set when the regular expression is
  7305. passed for compilation to the native function. In addition, when a pat-
  7306. tern that is compiled with this flag is passed to regexec() for match-
  7307. ing, the nmatch and pmatch arguments are ignored, and no captured
  7308. strings are returned.
  7309. REG_UCP
  7310. The PCRE_UCP option is set when the regular expression is passed for
  7311. compilation to the native function. This causes PCRE to use Unicode
  7312. properties when matchine \d, \w, etc., instead of just recognizing
  7313. ASCII values. Note that REG_UTF8 is not part of the POSIX standard.
  7314. REG_UNGREEDY
  7315. The PCRE_UNGREEDY option is set when the regular expression is passed
  7316. for compilation to the native function. Note that REG_UNGREEDY is not
  7317. part of the POSIX standard.
  7318. REG_UTF8
  7319. The PCRE_UTF8 option is set when the regular expression is passed for
  7320. compilation to the native function. This causes the pattern itself and
  7321. all data strings used for matching it to be treated as UTF-8 strings.
  7322. Note that REG_UTF8 is not part of the POSIX standard.
  7323. In the absence of these flags, no options are passed to the native
  7324. function. This means the the regex is compiled with PCRE default
  7325. semantics. In particular, the way it handles newline characters in the
  7326. subject string is the Perl way, not the POSIX way. Note that setting
  7327. PCRE_MULTILINE has only some of the effects specified for REG_NEWLINE.
  7328. It does not affect the way newlines are matched by . (they are not) or
  7329. by a negative class such as [^a] (they are).
  7330. The yield of regcomp() is zero on success, and non-zero otherwise. The
  7331. preg structure is filled in on success, and one member of the structure
  7332. is public: re_nsub contains the number of capturing subpatterns in the
  7333. regular expression. Various error codes are defined in the header file.
  7334. NOTE: If the yield of regcomp() is non-zero, you must not attempt to
  7335. use the contents of the preg structure. If, for example, you pass it to
  7336. regexec(), the result is undefined and your program is likely to crash.
  7337. MATCHING NEWLINE CHARACTERS
  7338. This area is not simple, because POSIX and Perl take different views of
  7339. things. It is not possible to get PCRE to obey POSIX semantics, but
  7340. then PCRE was never intended to be a POSIX engine. The following table
  7341. lists the different possibilities for matching newline characters in
  7342. PCRE:
  7343. Default Change with
  7344. . matches newline no PCRE_DOTALL
  7345. newline matches [^a] yes not changeable
  7346. $ matches \n at end yes PCRE_DOLLARENDONLY
  7347. $ matches \n in middle no PCRE_MULTILINE
  7348. ^ matches \n in middle no PCRE_MULTILINE
  7349. This is the equivalent table for POSIX:
  7350. Default Change with
  7351. . matches newline yes REG_NEWLINE
  7352. newline matches [^a] yes REG_NEWLINE
  7353. $ matches \n at end no REG_NEWLINE
  7354. $ matches \n in middle no REG_NEWLINE
  7355. ^ matches \n in middle no REG_NEWLINE
  7356. PCRE's behaviour is the same as Perl's, except that there is no equiva-
  7357. lent for PCRE_DOLLAR_ENDONLY in Perl. In both PCRE and Perl, there is
  7358. no way to stop newline from matching [^a].
  7359. The default POSIX newline handling can be obtained by setting
  7360. PCRE_DOTALL and PCRE_DOLLAR_ENDONLY, but there is no way to make PCRE
  7361. behave exactly as for the REG_NEWLINE action.
  7362. MATCHING A PATTERN
  7363. The function regexec() is called to match a compiled pattern preg
  7364. against a given string, which is by default terminated by a zero byte
  7365. (but see REG_STARTEND below), subject to the options in eflags. These
  7366. can be:
  7367. REG_NOTBOL
  7368. The PCRE_NOTBOL option is set when calling the underlying PCRE matching
  7369. function.
  7370. REG_NOTEMPTY
  7371. The PCRE_NOTEMPTY option is set when calling the underlying PCRE match-
  7372. ing function. Note that REG_NOTEMPTY is not part of the POSIX standard.
  7373. However, setting this option can give more POSIX-like behaviour in some
  7374. situations.
  7375. REG_NOTEOL
  7376. The PCRE_NOTEOL option is set when calling the underlying PCRE matching
  7377. function.
  7378. REG_STARTEND
  7379. The string is considered to start at string + pmatch[0].rm_so and to
  7380. have a terminating NUL located at string + pmatch[0].rm_eo (there need
  7381. not actually be a NUL at that location), regardless of the value of
  7382. nmatch. This is a BSD extension, compatible with but not specified by
  7383. IEEE Standard 1003.2 (POSIX.2), and should be used with caution in
  7384. software intended to be portable to other systems. Note that a non-zero
  7385. rm_so does not imply REG_NOTBOL; REG_STARTEND affects only the location
  7386. of the string, not how it is matched.
  7387. If the pattern was compiled with the REG_NOSUB flag, no data about any
  7388. matched strings is returned. The nmatch and pmatch arguments of
  7389. regexec() are ignored.
  7390. If the value of nmatch is zero, or if the value pmatch is NULL, no data
  7391. about any matched strings is returned.
  7392. Otherwise,the portion of the string that was matched, and also any cap-
  7393. tured substrings, are returned via the pmatch argument, which points to
  7394. an array of nmatch structures of type regmatch_t, containing the mem-
  7395. bers rm_so and rm_eo. These contain the offset to the first character
  7396. of each substring and the offset to the first character after the end
  7397. of each substring, respectively. The 0th element of the vector relates
  7398. to the entire portion of string that was matched; subsequent elements
  7399. relate to the capturing subpatterns of the regular expression. Unused
  7400. entries in the array have both structure members set to -1.
  7401. A successful match yields a zero return; various error codes are
  7402. defined in the header file, of which REG_NOMATCH is the "expected"
  7403. failure code.
  7404. ERROR MESSAGES
  7405. The regerror() function maps a non-zero errorcode from either regcomp()
  7406. or regexec() to a printable message. If preg is not NULL, the error
  7407. should have arisen from the use of that structure. A message terminated
  7408. by a binary zero is placed in errbuf. The length of the message,
  7409. including the zero, is limited to errbuf_size. The yield of the func-
  7410. tion is the size of buffer needed to hold the whole message.
  7411. MEMORY USAGE
  7412. Compiling a regular expression causes memory to be allocated and asso-
  7413. ciated with the preg structure. The function regfree() frees all such
  7414. memory, after which preg may no longer be used as a compiled expres-
  7415. sion.
  7416. AUTHOR
  7417. Philip Hazel
  7418. University Computing Service
  7419. Cambridge CB2 3QH, England.
  7420. REVISION
  7421. Last updated: 09 January 2012
  7422. Copyright (c) 1997-2012 University of Cambridge.
  7423. ------------------------------------------------------------------------------
  7424. PCRECPP(3) Library Functions Manual PCRECPP(3)
  7425. NAME
  7426. PCRE - Perl-compatible regular expressions.
  7427. SYNOPSIS OF C++ WRAPPER
  7428. #include <pcrecpp.h>
  7429. DESCRIPTION
  7430. The C++ wrapper for PCRE was provided by Google Inc. Some additional
  7431. functionality was added by Giuseppe Maxia. This brief man page was con-
  7432. structed from the notes in the pcrecpp.h file, which should be con-
  7433. sulted for further details. Note that the C++ wrapper supports only the
  7434. original 8-bit PCRE library. There is no 16-bit or 32-bit support at
  7435. present.
  7436. MATCHING INTERFACE
  7437. The "FullMatch" operation checks that supplied text matches a supplied
  7438. pattern exactly. If pointer arguments are supplied, it copies matched
  7439. sub-strings that match sub-patterns into them.
  7440. Example: successful match
  7441. pcrecpp::RE re("h.*o");
  7442. re.FullMatch("hello");
  7443. Example: unsuccessful match (requires full match):
  7444. pcrecpp::RE re("e");
  7445. !re.FullMatch("hello");
  7446. Example: creating a temporary RE object:
  7447. pcrecpp::RE("h.*o").FullMatch("hello");
  7448. You can pass in a "const char*" or a "string" for "text". The examples
  7449. below tend to use a const char*. You can, as in the different examples
  7450. above, store the RE object explicitly in a variable or use a temporary
  7451. RE object. The examples below use one mode or the other arbitrarily.
  7452. Either could correctly be used for any of these examples.
  7453. You must supply extra pointer arguments to extract matched subpieces.
  7454. Example: extracts "ruby" into "s" and 1234 into "i"
  7455. int i;
  7456. string s;
  7457. pcrecpp::RE re("(\\w+):(\\d+)");
  7458. re.FullMatch("ruby:1234", &s, &i);
  7459. Example: does not try to extract any extra sub-patterns
  7460. re.FullMatch("ruby:1234", &s);
  7461. Example: does not try to extract into NULL
  7462. re.FullMatch("ruby:1234", NULL, &i);
  7463. Example: integer overflow causes failure
  7464. !re.FullMatch("ruby:1234567891234", NULL, &i);
  7465. Example: fails because there aren't enough sub-patterns:
  7466. !pcrecpp::RE("\\w+:\\d+").FullMatch("ruby:1234", &s);
  7467. Example: fails because string cannot be stored in integer
  7468. !pcrecpp::RE("(.*)").FullMatch("ruby", &i);
  7469. The provided pointer arguments can be pointers to any scalar numeric
  7470. type, or one of:
  7471. string (matched piece is copied to string)
  7472. StringPiece (StringPiece is mutated to point to matched piece)
  7473. T (where "bool T::ParseFrom(const char*, int)" exists)
  7474. NULL (the corresponding matched sub-pattern is not copied)
  7475. The function returns true iff all of the following conditions are sat-
  7476. isfied:
  7477. a. "text" matches "pattern" exactly;
  7478. b. The number of matched sub-patterns is >= number of supplied
  7479. pointers;
  7480. c. The "i"th argument has a suitable type for holding the
  7481. string captured as the "i"th sub-pattern. If you pass in
  7482. void * NULL for the "i"th argument, or a non-void * NULL
  7483. of the correct type, or pass fewer arguments than the
  7484. number of sub-patterns, "i"th captured sub-pattern is
  7485. ignored.
  7486. CAVEAT: An optional sub-pattern that does not exist in the matched
  7487. string is assigned the empty string. Therefore, the following will
  7488. return false (because the empty string is not a valid number):
  7489. int number;
  7490. pcrecpp::RE::FullMatch("abc", "[a-z]+(\\d+)?", &number);
  7491. The matching interface supports at most 16 arguments per call. If you
  7492. need more, consider using the more general interface
  7493. pcrecpp::RE::DoMatch. See pcrecpp.h for the signature for DoMatch.
  7494. NOTE: Do not use no_arg, which is used internally to mark the end of a
  7495. list of optional arguments, as a placeholder for missing arguments, as
  7496. this can lead to segfaults.
  7497. QUOTING METACHARACTERS
  7498. You can use the "QuoteMeta" operation to insert backslashes before all
  7499. potentially meaningful characters in a string. The returned string,
  7500. used as a regular expression, will exactly match the original string.
  7501. Example:
  7502. string quoted = RE::QuoteMeta(unquoted);
  7503. Note that it's legal to escape a character even if it has no special
  7504. meaning in a regular expression -- so this function does that. (This
  7505. also makes it identical to the perl function of the same name; see
  7506. "perldoc -f quotemeta".) For example, "1.5-2.0?" becomes
  7507. "1\.5\-2\.0\?".
  7508. PARTIAL MATCHES
  7509. You can use the "PartialMatch" operation when you want the pattern to
  7510. match any substring of the text.
  7511. Example: simple search for a string:
  7512. pcrecpp::RE("ell").PartialMatch("hello");
  7513. Example: find first number in a string:
  7514. int number;
  7515. pcrecpp::RE re("(\\d+)");
  7516. re.PartialMatch("x*100 + 20", &number);
  7517. assert(number == 100);
  7518. UTF-8 AND THE MATCHING INTERFACE
  7519. By default, pattern and text are plain text, one byte per character.
  7520. The UTF8 flag, passed to the constructor, causes both pattern and
  7521. string to be treated as UTF-8 text, still a byte stream but potentially
  7522. multiple bytes per character. In practice, the text is likelier to be
  7523. UTF-8 than the pattern, but the match returned may depend on the UTF8
  7524. flag, so always use it when matching UTF8 text. For example, "." will
  7525. match one byte normally but with UTF8 set may match up to three bytes
  7526. of a multi-byte character.
  7527. Example:
  7528. pcrecpp::RE_Options options;
  7529. options.set_utf8();
  7530. pcrecpp::RE re(utf8_pattern, options);
  7531. re.FullMatch(utf8_string);
  7532. Example: using the convenience function UTF8():
  7533. pcrecpp::RE re(utf8_pattern, pcrecpp::UTF8());
  7534. re.FullMatch(utf8_string);
  7535. NOTE: The UTF8 flag is ignored if pcre was not configured with the
  7536. --enable-utf8 flag.
  7537. PASSING MODIFIERS TO THE REGULAR EXPRESSION ENGINE
  7538. PCRE defines some modifiers to change the behavior of the regular
  7539. expression engine. The C++ wrapper defines an auxiliary class,
  7540. RE_Options, as a vehicle to pass such modifiers to a RE class. Cur-
  7541. rently, the following modifiers are supported:
  7542. modifier description Perl corresponding
  7543. PCRE_CASELESS case insensitive match /i
  7544. PCRE_MULTILINE multiple lines match /m
  7545. PCRE_DOTALL dot matches newlines /s
  7546. PCRE_DOLLAR_ENDONLY $ matches only at end N/A
  7547. PCRE_EXTRA strict escape parsing N/A
  7548. PCRE_EXTENDED ignore white spaces /x
  7549. PCRE_UTF8 handles UTF8 chars built-in
  7550. PCRE_UNGREEDY reverses * and *? N/A
  7551. PCRE_NO_AUTO_CAPTURE disables capturing parens N/A (*)
  7552. (*) Both Perl and PCRE allow non capturing parentheses by means of the
  7553. "?:" modifier within the pattern itself. e.g. (?:ab|cd) does not cap-
  7554. ture, while (ab|cd) does.
  7555. For a full account on how each modifier works, please check the PCRE
  7556. API reference page.
  7557. For each modifier, there are two member functions whose name is made
  7558. out of the modifier in lowercase, without the "PCRE_" prefix. For
  7559. instance, PCRE_CASELESS is handled by
  7560. bool caseless()
  7561. which returns true if the modifier is set, and
  7562. RE_Options & set_caseless(bool)
  7563. which sets or unsets the modifier. Moreover, PCRE_EXTRA_MATCH_LIMIT can
  7564. be accessed through the set_match_limit() and match_limit() member
  7565. functions. Setting match_limit to a non-zero value will limit the exe-
  7566. cution of pcre to keep it from doing bad things like blowing the stack
  7567. or taking an eternity to return a result. A value of 5000 is good
  7568. enough to stop stack blowup in a 2MB thread stack. Setting match_limit
  7569. to zero disables match limiting. Alternatively, you can call
  7570. match_limit_recursion() which uses PCRE_EXTRA_MATCH_LIMIT_RECURSION to
  7571. limit how much PCRE recurses. match_limit() limits the number of
  7572. matches PCRE does; match_limit_recursion() limits the depth of internal
  7573. recursion, and therefore the amount of stack that is used.
  7574. Normally, to pass one or more modifiers to a RE class, you declare a
  7575. RE_Options object, set the appropriate options, and pass this object to
  7576. a RE constructor. Example:
  7577. RE_Options opt;
  7578. opt.set_caseless(true);
  7579. if (RE("HELLO", opt).PartialMatch("hello world")) ...
  7580. RE_options has two constructors. The default constructor takes no argu-
  7581. ments and creates a set of flags that are off by default. The optional
  7582. parameter option_flags is to facilitate transfer of legacy code from C
  7583. programs. This lets you do
  7584. RE(pattern,
  7585. RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str);
  7586. However, new code is better off doing
  7587. RE(pattern,
  7588. RE_Options().set_caseless(true).set_multiline(true))
  7589. .PartialMatch(str);
  7590. If you are going to pass one of the most used modifiers, there are some
  7591. convenience functions that return a RE_Options class with the appropri-
  7592. ate modifier already set: CASELESS(), UTF8(), MULTILINE(), DOTALL(),
  7593. and EXTENDED().
  7594. If you need to set several options at once, and you don't want to go
  7595. through the pains of declaring a RE_Options object and setting several
  7596. options, there is a parallel method that give you such ability on the
  7597. fly. You can concatenate several set_xxxxx() member functions, since
  7598. each of them returns a reference to its class object. For example, to
  7599. pass PCRE_CASELESS, PCRE_EXTENDED, and PCRE_MULTILINE to a RE with one
  7600. statement, you may write:
  7601. RE(" ^ xyz \\s+ .* blah$",
  7602. RE_Options()
  7603. .set_caseless(true)
  7604. .set_extended(true)
  7605. .set_multiline(true)).PartialMatch(sometext);
  7606. SCANNING TEXT INCREMENTALLY
  7607. The "Consume" operation may be useful if you want to repeatedly match
  7608. regular expressions at the front of a string and skip over them as they
  7609. match. This requires use of the "StringPiece" type, which represents a
  7610. sub-range of a real string. Like RE, StringPiece is defined in the
  7611. pcrecpp namespace.
  7612. Example: read lines of the form "var = value" from a string.
  7613. string contents = ...; // Fill string somehow
  7614. pcrecpp::StringPiece input(contents); // Wrap in a StringPiece
  7615. string var;
  7616. int value;
  7617. pcrecpp::RE re("(\\w+) = (\\d+)\n");
  7618. while (re.Consume(&input, &var, &value)) {
  7619. ...;
  7620. }
  7621. Each successful call to "Consume" will set "var/value", and also
  7622. advance "input" so it points past the matched text.
  7623. The "FindAndConsume" operation is similar to "Consume" but does not
  7624. anchor your match at the beginning of the string. For example, you
  7625. could extract all words from a string by repeatedly calling
  7626. pcrecpp::RE("(\\w+)").FindAndConsume(&input, &word)
  7627. PARSING HEX/OCTAL/C-RADIX NUMBERS
  7628. By default, if you pass a pointer to a numeric value, the corresponding
  7629. text is interpreted as a base-10 number. You can instead wrap the
  7630. pointer with a call to one of the operators Hex(), Octal(), or CRadix()
  7631. to interpret the text in another base. The CRadix operator interprets
  7632. C-style "0" (base-8) and "0x" (base-16) prefixes, but defaults to
  7633. base-10.
  7634. Example:
  7635. int a, b, c, d;
  7636. pcrecpp::RE re("(.*) (.*) (.*) (.*)");
  7637. re.FullMatch("100 40 0100 0x40",
  7638. pcrecpp::Octal(&a), pcrecpp::Hex(&b),
  7639. pcrecpp::CRadix(&c), pcrecpp::CRadix(&d));
  7640. will leave 64 in a, b, c, and d.
  7641. REPLACING PARTS OF STRINGS
  7642. You can replace the first match of "pattern" in "str" with "rewrite".
  7643. Within "rewrite", backslash-escaped digits (\1 to \9) can be used to
  7644. insert text matching corresponding parenthesized group from the pat-
  7645. tern. \0 in "rewrite" refers to the entire matching text. For example:
  7646. string s = "yabba dabba doo";
  7647. pcrecpp::RE("b+").Replace("d", &s);
  7648. will leave "s" containing "yada dabba doo". The result is true if the
  7649. pattern matches and a replacement occurs, false otherwise.
  7650. GlobalReplace is like Replace except that it replaces all occurrences
  7651. of the pattern in the string with the rewrite. Replacements are not
  7652. subject to re-matching. For example:
  7653. string s = "yabba dabba doo";
  7654. pcrecpp::RE("b+").GlobalReplace("d", &s);
  7655. will leave "s" containing "yada dada doo". It returns the number of
  7656. replacements made.
  7657. Extract is like Replace, except that if the pattern matches, "rewrite"
  7658. is copied into "out" (an additional argument) with substitutions. The
  7659. non-matching portions of "text" are ignored. Returns true iff a match
  7660. occurred and the extraction happened successfully; if no match occurs,
  7661. the string is left unaffected.
  7662. AUTHOR
  7663. The C++ wrapper was contributed by Google Inc.
  7664. Copyright (c) 2007 Google Inc.
  7665. REVISION
  7666. Last updated: 08 January 2012
  7667. ------------------------------------------------------------------------------
  7668. PCRESAMPLE(3) Library Functions Manual PCRESAMPLE(3)
  7669. NAME
  7670. PCRE - Perl-compatible regular expressions
  7671. PCRE SAMPLE PROGRAM
  7672. A simple, complete demonstration program, to get you started with using
  7673. PCRE, is supplied in the file pcredemo.c in the PCRE distribution. A
  7674. listing of this program is given in the pcredemo documentation. If you
  7675. do not have a copy of the PCRE distribution, you can save this listing
  7676. to re-create pcredemo.c.
  7677. The demonstration program, which uses the original PCRE 8-bit library,
  7678. compiles the regular expression that is its first argument, and matches
  7679. it against the subject string in its second argument. No PCRE options
  7680. are set, and default character tables are used. If matching succeeds,
  7681. the program outputs the portion of the subject that matched, together
  7682. with the contents of any captured substrings.
  7683. If the -g option is given on the command line, the program then goes on
  7684. to check for further matches of the same regular expression in the same
  7685. subject string. The logic is a little bit tricky because of the possi-
  7686. bility of matching an empty string. Comments in the code explain what
  7687. is going on.
  7688. If PCRE is installed in the standard include and library directories
  7689. for your operating system, you should be able to compile the demonstra-
  7690. tion program using this command:
  7691. gcc -o pcredemo pcredemo.c -lpcre
  7692. If PCRE is installed elsewhere, you may need to add additional options
  7693. to the command line. For example, on a Unix-like system that has PCRE
  7694. installed in /usr/local, you can compile the demonstration program
  7695. using a command like this:
  7696. gcc -o pcredemo -I/usr/local/include pcredemo.c \
  7697. -L/usr/local/lib -lpcre
  7698. In a Windows environment, if you want to statically link the program
  7699. against a non-dll pcre.a file, you must uncomment the line that defines
  7700. PCRE_STATIC before including pcre.h, because otherwise the pcre_mal-
  7701. loc() and pcre_free() exported functions will be declared
  7702. __declspec(dllimport), with unwanted results.
  7703. Once you have compiled and linked the demonstration program, you can
  7704. run simple tests like this:
  7705. ./pcredemo 'cat|dog' 'the cat sat on the mat'
  7706. ./pcredemo -g 'cat|dog' 'the dog sat on the cat'
  7707. Note that there is a much more comprehensive test program, called
  7708. pcretest, which supports many more facilities for testing regular
  7709. expressions and both PCRE libraries. The pcredemo program is provided
  7710. as a simple coding example.
  7711. If you try to run pcredemo when PCRE is not installed in the standard
  7712. library directory, you may get an error like this on some operating
  7713. systems (e.g. Solaris):
  7714. ld.so.1: a.out: fatal: libpcre.so.0: open failed: No such file or
  7715. directory
  7716. This is caused by the way shared library support works on those sys-
  7717. tems. You need to add
  7718. -R/usr/local/lib
  7719. (for example) to the compile command to get round this problem.
  7720. AUTHOR
  7721. Philip Hazel
  7722. University Computing Service
  7723. Cambridge CB2 3QH, England.
  7724. REVISION
  7725. Last updated: 10 January 2012
  7726. Copyright (c) 1997-2012 University of Cambridge.
  7727. ------------------------------------------------------------------------------
  7728. PCRELIMITS(3) Library Functions Manual PCRELIMITS(3)
  7729. NAME
  7730. PCRE - Perl-compatible regular expressions
  7731. SIZE AND OTHER LIMITATIONS
  7732. There are some size limitations in PCRE but it is hoped that they will
  7733. never in practice be relevant.
  7734. The maximum length of a compiled pattern is approximately 64K data
  7735. units (bytes for the 8-bit library, 16-bit units for the 16-bit
  7736. library, and 32-bit units for the 32-bit library) if PCRE is compiled
  7737. with the default internal linkage size, which is 2 bytes for the 8-bit
  7738. and 16-bit libraries, and 4 bytes for the 32-bit library. If you want
  7739. to process regular expressions that are truly enormous, you can compile
  7740. PCRE with an internal linkage size of 3 or 4 (when building the 16-bit
  7741. or 32-bit library, 3 is rounded up to 4). See the README file in the
  7742. source distribution and the pcrebuild documentation for details. In
  7743. these cases the limit is substantially larger. However, the speed of
  7744. execution is slower.
  7745. All values in repeating quantifiers must be less than 65536.
  7746. There is no limit to the number of parenthesized subpatterns, but there
  7747. can be no more than 65535 capturing subpatterns. There is, however, a
  7748. limit to the depth of nesting of parenthesized subpatterns of all
  7749. kinds. This is imposed in order to limit the amount of system stack
  7750. used at compile time. The limit can be specified when PCRE is built;
  7751. the default is 250.
  7752. There is a limit to the number of forward references to subsequent sub-
  7753. patterns of around 200,000. Repeated forward references with fixed
  7754. upper limits, for example, (?2){0,100} when subpattern number 2 is to
  7755. the right, are included in the count. There is no limit to the number
  7756. of backward references.
  7757. The maximum length of name for a named subpattern is 32 characters, and
  7758. the maximum number of named subpatterns is 10000.
  7759. The maximum length of a name in a (*MARK), (*PRUNE), (*SKIP), or
  7760. (*THEN) verb is 255 for the 8-bit library and 65535 for the 16-bit and
  7761. 32-bit libraries.
  7762. The maximum length of a subject string is the largest positive number
  7763. that an integer variable can hold. However, when using the traditional
  7764. matching function, PCRE uses recursion to handle subpatterns and indef-
  7765. inite repetition. This means that the available stack space may limit
  7766. the size of a subject string that can be processed by certain patterns.
  7767. For a discussion of stack issues, see the pcrestack documentation.
  7768. AUTHOR
  7769. Philip Hazel
  7770. University Computing Service
  7771. Cambridge CB2 3QH, England.
  7772. REVISION
  7773. Last updated: 05 November 2013
  7774. Copyright (c) 1997-2013 University of Cambridge.
  7775. ------------------------------------------------------------------------------
  7776. PCRESTACK(3) Library Functions Manual PCRESTACK(3)
  7777. NAME
  7778. PCRE - Perl-compatible regular expressions
  7779. PCRE DISCUSSION OF STACK USAGE
  7780. When you call pcre[16|32]_exec(), it makes use of an internal function
  7781. called match(). This calls itself recursively at branch points in the
  7782. pattern, in order to remember the state of the match so that it can
  7783. back up and try a different alternative if the first one fails. As
  7784. matching proceeds deeper and deeper into the tree of possibilities, the
  7785. recursion depth increases. The match() function is also called in other
  7786. circumstances, for example, whenever a parenthesized sub-pattern is
  7787. entered, and in certain cases of repetition.
  7788. Not all calls of match() increase the recursion depth; for an item such
  7789. as a* it may be called several times at the same level, after matching
  7790. different numbers of a's. Furthermore, in a number of cases where the
  7791. result of the recursive call would immediately be passed back as the
  7792. result of the current call (a "tail recursion"), the function is just
  7793. restarted instead.
  7794. The above comments apply when pcre[16|32]_exec() is run in its normal
  7795. interpretive manner. If the pattern was studied with the
  7796. PCRE_STUDY_JIT_COMPILE option, and just-in-time compiling was success-
  7797. ful, and the options passed to pcre[16|32]_exec() were not incompati-
  7798. ble, the matching process uses the JIT-compiled code instead of the
  7799. match() function. In this case, the memory requirements are handled
  7800. entirely differently. See the pcrejit documentation for details.
  7801. The pcre[16|32]_dfa_exec() function operates in an entirely different
  7802. way, and uses recursion only when there is a regular expression recur-
  7803. sion or subroutine call in the pattern. This includes the processing of
  7804. assertion and "once-only" subpatterns, which are handled like subrou-
  7805. tine calls. Normally, these are never very deep, and the limit on the
  7806. complexity of pcre[16|32]_dfa_exec() is controlled by the amount of
  7807. workspace it is given. However, it is possible to write patterns with
  7808. runaway infinite recursions; such patterns will cause
  7809. pcre[16|32]_dfa_exec() to run out of stack. At present, there is no
  7810. protection against this.
  7811. The comments that follow do NOT apply to pcre[16|32]_dfa_exec(); they
  7812. are relevant only for pcre[16|32]_exec() without the JIT optimization.
  7813. Reducing pcre[16|32]_exec()'s stack usage
  7814. Each time that match() is actually called recursively, it uses memory
  7815. from the process stack. For certain kinds of pattern and data, very
  7816. large amounts of stack may be needed, despite the recognition of "tail
  7817. recursion". You can often reduce the amount of recursion, and there-
  7818. fore the amount of stack used, by modifying the pattern that is being
  7819. matched. Consider, for example, this pattern:
  7820. ([^<]|<(?!inet))+
  7821. It matches from wherever it starts until it encounters "<inet" or the
  7822. end of the data, and is the kind of pattern that might be used when
  7823. processing an XML file. Each iteration of the outer parentheses matches
  7824. either one character that is not "<" or a "<" that is not followed by
  7825. "inet". However, each time a parenthesis is processed, a recursion
  7826. occurs, so this formulation uses a stack frame for each matched charac-
  7827. ter. For a long string, a lot of stack is required. Consider now this
  7828. rewritten pattern, which matches exactly the same strings:
  7829. ([^<]++|<(?!inet))+
  7830. This uses very much less stack, because runs of characters that do not
  7831. contain "<" are "swallowed" in one item inside the parentheses. Recur-
  7832. sion happens only when a "<" character that is not followed by "inet"
  7833. is encountered (and we assume this is relatively rare). A possessive
  7834. quantifier is used to stop any backtracking into the runs of non-"<"
  7835. characters, but that is not related to stack usage.
  7836. This example shows that one way of avoiding stack problems when match-
  7837. ing long subject strings is to write repeated parenthesized subpatterns
  7838. to match more than one character whenever possible.
  7839. Compiling PCRE to use heap instead of stack for pcre[16|32]_exec()
  7840. In environments where stack memory is constrained, you might want to
  7841. compile PCRE to use heap memory instead of stack for remembering back-
  7842. up points when pcre[16|32]_exec() is running. This makes it run a lot
  7843. more slowly, however. Details of how to do this are given in the pcre-
  7844. build documentation. When built in this way, instead of using the
  7845. stack, PCRE obtains and frees memory by calling the functions that are
  7846. pointed to by the pcre[16|32]_stack_malloc and pcre[16|32]_stack_free
  7847. variables. By default, these point to malloc() and free(), but you can
  7848. replace the pointers to cause PCRE to use your own functions. Since the
  7849. block sizes are always the same, and are always freed in reverse order,
  7850. it may be possible to implement customized memory handlers that are
  7851. more efficient than the standard functions.
  7852. Limiting pcre[16|32]_exec()'s stack usage
  7853. You can set limits on the number of times that match() is called, both
  7854. in total and recursively. If a limit is exceeded, pcre[16|32]_exec()
  7855. returns an error code. Setting suitable limits should prevent it from
  7856. running out of stack. The default values of the limits are very large,
  7857. and unlikely ever to operate. They can be changed when PCRE is built,
  7858. and they can also be set when pcre[16|32]_exec() is called. For details
  7859. of these interfaces, see the pcrebuild documentation and the section on
  7860. extra data for pcre[16|32]_exec() in the pcreapi documentation.
  7861. As a very rough rule of thumb, you should reckon on about 500 bytes per
  7862. recursion. Thus, if you want to limit your stack usage to 8Mb, you
  7863. should set the limit at 16000 recursions. A 64Mb stack, on the other
  7864. hand, can support around 128000 recursions.
  7865. In Unix-like environments, the pcretest test program has a command line
  7866. option (-S) that can be used to increase the size of its stack. As long
  7867. as the stack is large enough, another option (-M) can be used to find
  7868. the smallest limits that allow a particular pattern to match a given
  7869. subject string. This is done by calling pcre[16|32]_exec() repeatedly
  7870. with different limits.
  7871. Obtaining an estimate of stack usage
  7872. The actual amount of stack used per recursion can vary quite a lot,
  7873. depending on the compiler that was used to build PCRE and the optimiza-
  7874. tion or debugging options that were set for it. The rule of thumb value
  7875. of 500 bytes mentioned above may be larger or smaller than what is
  7876. actually needed. A better approximation can be obtained by running this
  7877. command:
  7878. pcretest -m -C
  7879. The -C option causes pcretest to output information about the options
  7880. with which PCRE was compiled. When -m is also given (before -C), infor-
  7881. mation about stack use is given in a line like this:
  7882. Match recursion uses stack: approximate frame size = 640 bytes
  7883. The value is approximate because some recursions need a bit more (up to
  7884. perhaps 16 more bytes).
  7885. If the above command is given when PCRE is compiled to use the heap
  7886. instead of the stack for recursion, the value that is output is the
  7887. size of each block that is obtained from the heap.
  7888. Changing stack size in Unix-like systems
  7889. In Unix-like environments, there is not often a problem with the stack
  7890. unless very long strings are involved, though the default limit on
  7891. stack size varies from system to system. Values from 8Mb to 64Mb are
  7892. common. You can find your default limit by running the command:
  7893. ulimit -s
  7894. Unfortunately, the effect of running out of stack is often SIGSEGV,
  7895. though sometimes a more explicit error message is given. You can nor-
  7896. mally increase the limit on stack size by code such as this:
  7897. struct rlimit rlim;
  7898. getrlimit(RLIMIT_STACK, &rlim);
  7899. rlim.rlim_cur = 100*1024*1024;
  7900. setrlimit(RLIMIT_STACK, &rlim);
  7901. This reads the current limits (soft and hard) using getrlimit(), then
  7902. attempts to increase the soft limit to 100Mb using setrlimit(). You
  7903. must do this before calling pcre[16|32]_exec().
  7904. Changing stack size in Mac OS X
  7905. Using setrlimit(), as described above, should also work on Mac OS X. It
  7906. is also possible to set a stack size when linking a program. There is a
  7907. discussion about stack sizes in Mac OS X at this web site:
  7908. http://developer.apple.com/qa/qa2005/qa1419.html.
  7909. AUTHOR
  7910. Philip Hazel
  7911. University Computing Service
  7912. Cambridge CB2 3QH, England.
  7913. REVISION
  7914. Last updated: 24 June 2012
  7915. Copyright (c) 1997-2012 University of Cambridge.
  7916. ------------------------------------------------------------------------------