pcre.txt 497 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502
  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 sytem-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 always allocate memory blocks of the same size.
  1269. There is a discussion about PCRE's stack usage in the pcrestack docu-
  1270. mentation.
  1271. The global variable pcre_callout initially contains NULL. It can be set
  1272. by the caller to a "callout" function, which PCRE will then call at
  1273. specified points during a matching operation. Details are given in the
  1274. pcrecallout documentation.
  1275. The global variable pcre_stack_guard initially contains NULL. It can be
  1276. set by the caller to a function that is called by PCRE whenever it
  1277. starts to compile a parenthesized part of a pattern. When parentheses
  1278. are nested, PCRE uses recursive function calls, which use up the system
  1279. stack. This function is provided so that applications with restricted
  1280. stacks can force a compilation error if the stack runs out. The func-
  1281. tion should return zero if all is well, or non-zero to force an error.
  1282. NEWLINES
  1283. PCRE supports five different conventions for indicating line breaks in
  1284. strings: a single CR (carriage return) character, a single LF (line-
  1285. feed) character, the two-character sequence CRLF, any of the three pre-
  1286. ceding, or any Unicode newline sequence. The Unicode newline sequences
  1287. are the three just mentioned, plus the single characters VT (vertical
  1288. tab, U+000B), FF (form feed, U+000C), NEL (next line, U+0085), LS (line
  1289. separator, U+2028), and PS (paragraph separator, U+2029).
  1290. Each of the first three conventions is used by at least one operating
  1291. system as its standard newline sequence. When PCRE is built, a default
  1292. can be specified. The default default is LF, which is the Unix stan-
  1293. dard. When PCRE is run, the default can be overridden, either when a
  1294. pattern is compiled, or when it is matched.
  1295. At compile time, the newline convention can be specified by the options
  1296. argument of pcre_compile(), or it can be specified by special text at
  1297. the start of the pattern itself; this overrides any other settings. See
  1298. the pcrepattern page for details of the special character sequences.
  1299. In the PCRE documentation the word "newline" is used to mean "the char-
  1300. acter or pair of characters that indicate a line break". The choice of
  1301. newline convention affects the handling of the dot, circumflex, and
  1302. dollar metacharacters, the handling of #-comments in /x mode, and, when
  1303. CRLF is a recognized line ending sequence, the match position advance-
  1304. ment for a non-anchored pattern. There is more detail about this in the
  1305. section on pcre_exec() options below.
  1306. The choice of newline convention does not affect the interpretation of
  1307. the \n or \r escape sequences, nor does it affect what \R matches,
  1308. which is controlled in a similar way, but by separate options.
  1309. MULTITHREADING
  1310. The PCRE functions can be used in multi-threading applications, with
  1311. the proviso that the memory management functions pointed to by
  1312. pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the
  1313. callout and stack-checking functions pointed to by pcre_callout and
  1314. pcre_stack_guard, are shared by all threads.
  1315. The compiled form of a regular expression is not altered during match-
  1316. ing, so the same compiled pattern can safely be used by several threads
  1317. at once.
  1318. If the just-in-time optimization feature is being used, it needs sepa-
  1319. rate memory stack areas for each thread. See the pcrejit documentation
  1320. for more details.
  1321. SAVING PRECOMPILED PATTERNS FOR LATER USE
  1322. The compiled form of a regular expression can be saved and re-used at a
  1323. later time, possibly by a different program, and even on a host other
  1324. than the one on which it was compiled. Details are given in the
  1325. pcreprecompile documentation, which includes a description of the
  1326. pcre_pattern_to_host_byte_order() function. However, compiling a regu-
  1327. lar expression with one version of PCRE for use with a different ver-
  1328. sion is not guaranteed to work and may cause crashes.
  1329. CHECKING BUILD-TIME OPTIONS
  1330. int pcre_config(int what, void *where);
  1331. The function pcre_config() makes it possible for a PCRE client to dis-
  1332. cover which optional features have been compiled into the PCRE library.
  1333. The pcrebuild documentation has more details about these optional fea-
  1334. tures.
  1335. The first argument for pcre_config() is an integer, specifying which
  1336. information is required; the second argument is a pointer to a variable
  1337. into which the information is placed. The returned value is zero on
  1338. success, or the negative error code PCRE_ERROR_BADOPTION if the value
  1339. in the first argument is not recognized. The following information is
  1340. available:
  1341. PCRE_CONFIG_UTF8
  1342. The output is an integer that is set to one if UTF-8 support is avail-
  1343. able; otherwise it is set to zero. This value should normally be given
  1344. to the 8-bit version of this function, pcre_config(). If it is given to
  1345. the 16-bit or 32-bit version of this function, the result is
  1346. PCRE_ERROR_BADOPTION.
  1347. PCRE_CONFIG_UTF16
  1348. The output is an integer that is set to one if UTF-16 support is avail-
  1349. able; otherwise it is set to zero. This value should normally be given
  1350. to the 16-bit version of this function, pcre16_config(). If it is given
  1351. to the 8-bit or 32-bit version of this function, the result is
  1352. PCRE_ERROR_BADOPTION.
  1353. PCRE_CONFIG_UTF32
  1354. The output is an integer that is set to one if UTF-32 support is avail-
  1355. able; otherwise it is set to zero. This value should normally be given
  1356. to the 32-bit version of this function, pcre32_config(). If it is given
  1357. to the 8-bit or 16-bit version of this function, the result is
  1358. PCRE_ERROR_BADOPTION.
  1359. PCRE_CONFIG_UNICODE_PROPERTIES
  1360. The output is an integer that is set to one if support for Unicode
  1361. character properties is available; otherwise it is set to zero.
  1362. PCRE_CONFIG_JIT
  1363. The output is an integer that is set to one if support for just-in-time
  1364. compiling is available; otherwise it is set to zero.
  1365. PCRE_CONFIG_JITTARGET
  1366. The output is a pointer to a zero-terminated "const char *" string. If
  1367. JIT support is available, the string contains the name of the architec-
  1368. ture for which the JIT compiler is configured, for example "x86 32bit
  1369. (little endian + unaligned)". If JIT support is not available, the
  1370. result is NULL.
  1371. PCRE_CONFIG_NEWLINE
  1372. The output is an integer whose value specifies the default character
  1373. sequence that is recognized as meaning "newline". The values that are
  1374. supported in ASCII/Unicode environments are: 10 for LF, 13 for CR, 3338
  1375. for CRLF, -2 for ANYCRLF, and -1 for ANY. In EBCDIC environments, CR,
  1376. ANYCRLF, and ANY yield the same values. However, the value for LF is
  1377. normally 21, though some EBCDIC environments use 37. The corresponding
  1378. values for CRLF are 3349 and 3365. The default should normally corre-
  1379. spond to the standard sequence for your operating system.
  1380. PCRE_CONFIG_BSR
  1381. The output is an integer whose value indicates what character sequences
  1382. the \R escape sequence matches by default. A value of 0 means that \R
  1383. matches any Unicode line ending sequence; a value of 1 means that \R
  1384. matches only CR, LF, or CRLF. The default can be overridden when a pat-
  1385. tern is compiled or matched.
  1386. PCRE_CONFIG_LINK_SIZE
  1387. The output is an integer that contains the number of bytes used for
  1388. internal linkage in compiled regular expressions. For the 8-bit
  1389. library, the value can be 2, 3, or 4. For the 16-bit library, the value
  1390. is either 2 or 4 and is still a number of bytes. For the 32-bit
  1391. library, the value is either 2 or 4 and is still a number of bytes. The
  1392. default value of 2 is sufficient for all but the most massive patterns,
  1393. since it allows the compiled pattern to be up to 64K in size. Larger
  1394. values allow larger regular expressions to be compiled, at the expense
  1395. of slower matching.
  1396. PCRE_CONFIG_POSIX_MALLOC_THRESHOLD
  1397. The output is an integer that contains the threshold above which the
  1398. POSIX interface uses malloc() for output vectors. Further details are
  1399. given in the pcreposix documentation.
  1400. PCRE_CONFIG_PARENS_LIMIT
  1401. The output is a long integer that gives the maximum depth of nesting of
  1402. parentheses (of any kind) in a pattern. This limit is imposed to cap
  1403. the amount of system stack used when a pattern is compiled. It is spec-
  1404. ified when PCRE is built; the default is 250. This limit does not take
  1405. into account the stack that may already be used by the calling applica-
  1406. tion. For finer control over compilation stack usage, you can set a
  1407. pointer to an external checking function in pcre_stack_guard.
  1408. PCRE_CONFIG_MATCH_LIMIT
  1409. The output is a long integer that gives the default limit for the num-
  1410. ber of internal matching function calls in a pcre_exec() execution.
  1411. Further details are given with pcre_exec() below.
  1412. PCRE_CONFIG_MATCH_LIMIT_RECURSION
  1413. The output is a long integer that gives the default limit for the depth
  1414. of recursion when calling the internal matching function in a
  1415. pcre_exec() execution. Further details are given with pcre_exec()
  1416. below.
  1417. PCRE_CONFIG_STACKRECURSE
  1418. The output is an integer that is set to one if internal recursion when
  1419. running pcre_exec() is implemented by recursive function calls that use
  1420. the stack to remember their state. This is the usual way that PCRE is
  1421. compiled. The output is zero if PCRE was compiled to use blocks of data
  1422. on the heap instead of recursive function calls. In this case,
  1423. pcre_stack_malloc and pcre_stack_free are called to manage memory
  1424. blocks on the heap, thus avoiding the use of the stack.
  1425. COMPILING A PATTERN
  1426. pcre *pcre_compile(const char *pattern, int options,
  1427. const char **errptr, int *erroffset,
  1428. const unsigned char *tableptr);
  1429. pcre *pcre_compile2(const char *pattern, int options,
  1430. int *errorcodeptr,
  1431. const char **errptr, int *erroffset,
  1432. const unsigned char *tableptr);
  1433. Either of the functions pcre_compile() or pcre_compile2() can be called
  1434. to compile a pattern into an internal form. The only difference between
  1435. the two interfaces is that pcre_compile2() has an additional argument,
  1436. errorcodeptr, via which a numerical error code can be returned. To
  1437. avoid too much repetition, we refer just to pcre_compile() below, but
  1438. the information applies equally to pcre_compile2().
  1439. The pattern is a C string terminated by a binary zero, and is passed in
  1440. the pattern argument. A pointer to a single block of memory that is
  1441. obtained via pcre_malloc is returned. This contains the compiled code
  1442. and related data. The pcre type is defined for the returned block; this
  1443. is a typedef for a structure whose contents are not externally defined.
  1444. It is up to the caller to free the memory (via pcre_free) when it is no
  1445. longer required.
  1446. Although the compiled code of a PCRE regex is relocatable, that is, it
  1447. does not depend on memory location, the complete pcre data block is not
  1448. fully relocatable, because it may contain a copy of the tableptr argu-
  1449. ment, which is an address (see below).
  1450. The options argument contains various bit settings that affect the com-
  1451. pilation. It should be zero if no options are required. The available
  1452. options are described below. Some of them (in particular, those that
  1453. are compatible with Perl, but some others as well) can also be set and
  1454. unset from within the pattern (see the detailed description in the
  1455. pcrepattern documentation). For those options that can be different in
  1456. different parts of the pattern, the contents of the options argument
  1457. specifies their settings at the start of compilation and execution. The
  1458. PCRE_ANCHORED, PCRE_BSR_xxx, PCRE_NEWLINE_xxx, PCRE_NO_UTF8_CHECK, and
  1459. PCRE_NO_START_OPTIMIZE options can be set at the time of matching as
  1460. well as at compile time.
  1461. If errptr is NULL, pcre_compile() returns NULL immediately. Otherwise,
  1462. if compilation of a pattern fails, pcre_compile() returns NULL, and
  1463. sets the variable pointed to by errptr to point to a textual error mes-
  1464. sage. This is a static string that is part of the library. You must not
  1465. try to free it. Normally, the offset from the start of the pattern to
  1466. the data unit that was being processed when the error was discovered is
  1467. placed in the variable pointed to by erroffset, which must not be NULL
  1468. (if it is, an immediate error is given). However, for an invalid UTF-8
  1469. or UTF-16 string, the offset is that of the first data unit of the
  1470. failing character.
  1471. Some errors are not detected until the whole pattern has been scanned;
  1472. in these cases, the offset passed back is the length of the pattern.
  1473. Note that the offset is in data units, not characters, even in a UTF
  1474. mode. It may sometimes point into the middle of a UTF-8 or UTF-16 char-
  1475. acter.
  1476. If pcre_compile2() is used instead of pcre_compile(), and the error-
  1477. codeptr argument is not NULL, a non-zero error code number is returned
  1478. via this argument in the event of an error. This is in addition to the
  1479. textual error message. Error codes and messages are listed below.
  1480. If the final argument, tableptr, is NULL, PCRE uses a default set of
  1481. character tables that are built when PCRE is compiled, using the
  1482. default C locale. Otherwise, tableptr must be an address that is the
  1483. result of a call to pcre_maketables(). This value is stored with the
  1484. compiled pattern, and used again by pcre_exec() and pcre_dfa_exec()
  1485. when the pattern is matched. For more discussion, see the section on
  1486. locale support below.
  1487. This code fragment shows a typical straightforward call to pcre_com-
  1488. pile():
  1489. pcre *re;
  1490. const char *error;
  1491. int erroffset;
  1492. re = pcre_compile(
  1493. "^A.*Z", /* the pattern */
  1494. 0, /* default options */
  1495. &error, /* for error message */
  1496. &erroffset, /* for error offset */
  1497. NULL); /* use default character tables */
  1498. The following names for option bits are defined in the pcre.h header
  1499. file:
  1500. PCRE_ANCHORED
  1501. If this bit is set, the pattern is forced to be "anchored", that is, it
  1502. is constrained to match only at the first matching point in the string
  1503. that is being searched (the "subject string"). This effect can also be
  1504. achieved by appropriate constructs in the pattern itself, which is the
  1505. only way to do it in Perl.
  1506. PCRE_AUTO_CALLOUT
  1507. If this bit is set, pcre_compile() automatically inserts callout items,
  1508. all with number 255, before each pattern item. For discussion of the
  1509. callout facility, see the pcrecallout documentation.
  1510. PCRE_BSR_ANYCRLF
  1511. PCRE_BSR_UNICODE
  1512. These options (which are mutually exclusive) control what the \R escape
  1513. sequence matches. The choice is either to match only CR, LF, or CRLF,
  1514. or to match any Unicode newline sequence. The default is specified when
  1515. PCRE is built. It can be overridden from within the pattern, or by set-
  1516. ting an option when a compiled pattern is matched.
  1517. PCRE_CASELESS
  1518. If this bit is set, letters in the pattern match both upper and lower
  1519. case letters. It is equivalent to Perl's /i option, and it can be
  1520. changed within a pattern by a (?i) option setting. In UTF-8 mode, PCRE
  1521. always understands the concept of case for characters whose values are
  1522. less than 128, so caseless matching is always possible. For characters
  1523. with higher values, the concept of case is supported if PCRE is com-
  1524. piled with Unicode property support, but not otherwise. If you want to
  1525. use caseless matching for characters 128 and above, you must ensure
  1526. that PCRE is compiled with Unicode property support as well as with
  1527. UTF-8 support.
  1528. PCRE_DOLLAR_ENDONLY
  1529. If this bit is set, a dollar metacharacter in the pattern matches only
  1530. at the end of the subject string. Without this option, a dollar also
  1531. matches immediately before a newline at the end of the string (but not
  1532. before any other newlines). The PCRE_DOLLAR_ENDONLY option is ignored
  1533. if PCRE_MULTILINE is set. There is no equivalent to this option in
  1534. Perl, and no way to set it within a pattern.
  1535. PCRE_DOTALL
  1536. If this bit is set, a dot metacharacter in the pattern matches a char-
  1537. acter of any value, including one that indicates a newline. However, it
  1538. only ever matches one character, even if newlines are coded as CRLF.
  1539. Without this option, a dot does not match when the current position is
  1540. at a newline. This option is equivalent to Perl's /s option, and it can
  1541. be changed within a pattern by a (?s) option setting. A negative class
  1542. such as [^a] always matches newline characters, independent of the set-
  1543. ting of this option.
  1544. PCRE_DUPNAMES
  1545. If this bit is set, names used to identify capturing subpatterns need
  1546. not be unique. This can be helpful for certain types of pattern when it
  1547. is known that only one instance of the named subpattern can ever be
  1548. matched. There are more details of named subpatterns below; see also
  1549. the pcrepattern documentation.
  1550. PCRE_EXTENDED
  1551. If this bit is set, most white space characters in the pattern are
  1552. totally ignored except when escaped or inside a character class. How-
  1553. ever, white space is not allowed within sequences such as (?> that
  1554. introduce various parenthesized subpatterns, nor within a numerical
  1555. quantifier such as {1,3}. However, ignorable white space is permitted
  1556. between an item and a following quantifier and between a quantifier and
  1557. a following + that indicates possessiveness.
  1558. White space did not used to include the VT character (code 11), because
  1559. Perl did not treat this character as white space. However, Perl changed
  1560. at release 5.18, so PCRE followed at release 8.34, and VT is now
  1561. treated as white space.
  1562. PCRE_EXTENDED also causes characters between an unescaped # outside a
  1563. character class and the next newline, inclusive, to be ignored.
  1564. PCRE_EXTENDED is equivalent to Perl's /x option, and it can be changed
  1565. within a pattern by a (?x) option setting.
  1566. Which characters are interpreted as newlines is controlled by the
  1567. options passed to pcre_compile() or by a special sequence at the start
  1568. of the pattern, as described in the section entitled "Newline conven-
  1569. tions" in the pcrepattern documentation. Note that the end of this type
  1570. of comment is a literal newline sequence in the pattern; escape
  1571. sequences that happen to represent a newline do not count.
  1572. This option makes it possible to include comments inside complicated
  1573. patterns. Note, however, that this applies only to data characters.
  1574. White space characters may never appear within special character
  1575. sequences in a pattern, for example within the sequence (?( that intro-
  1576. duces a conditional subpattern.
  1577. PCRE_EXTRA
  1578. This option was invented in order to turn on additional functionality
  1579. of PCRE that is incompatible with Perl, but it is currently of very
  1580. little use. When set, any backslash in a pattern that is followed by a
  1581. letter that has no special meaning causes an error, thus reserving
  1582. these combinations for future expansion. By default, as in Perl, a
  1583. backslash followed by a letter with no special meaning is treated as a
  1584. literal. (Perl can, however, be persuaded to give an error for this, by
  1585. running it with the -w option.) There are at present no other features
  1586. controlled by this option. It can also be set by a (?X) option setting
  1587. within a pattern.
  1588. PCRE_FIRSTLINE
  1589. If this option is set, an unanchored pattern is required to match
  1590. before or at the first newline in the subject string, though the
  1591. matched text may continue over the newline.
  1592. PCRE_JAVASCRIPT_COMPAT
  1593. If this option is set, PCRE's behaviour is changed in some ways so that
  1594. it is compatible with JavaScript rather than Perl. The changes are as
  1595. follows:
  1596. (1) A lone closing square bracket in a pattern causes a compile-time
  1597. error, because this is illegal in JavaScript (by default it is treated
  1598. as a data character). Thus, the pattern AB]CD becomes illegal when this
  1599. option is set.
  1600. (2) At run time, a back reference to an unset subpattern group matches
  1601. an empty string (by default this causes the current matching alterna-
  1602. tive to fail). A pattern such as (\1)(a) succeeds when this option is
  1603. set (assuming it can find an "a" in the subject), whereas it fails by
  1604. default, for Perl compatibility.
  1605. (3) \U matches an upper case "U" character; by default \U causes a com-
  1606. pile time error (Perl uses \U to upper case subsequent characters).
  1607. (4) \u matches a lower case "u" character unless it is followed by four
  1608. hexadecimal digits, in which case the hexadecimal number defines the
  1609. code point to match. By default, \u causes a compile time error (Perl
  1610. uses it to upper case the following character).
  1611. (5) \x matches a lower case "x" character unless it is followed by two
  1612. hexadecimal digits, in which case the hexadecimal number defines the
  1613. code point to match. By default, as in Perl, a hexadecimal number is
  1614. always expected after \x, but it may have zero, one, or two digits (so,
  1615. for example, \xz matches a binary zero character followed by z).
  1616. PCRE_MULTILINE
  1617. By default, for the purposes of matching "start of line" and "end of
  1618. line", PCRE treats the subject string as consisting of a single line of
  1619. characters, even if it actually contains newlines. The "start of line"
  1620. metacharacter (^) matches only at the start of the string, and the "end
  1621. of line" metacharacter ($) matches only at the end of the string, or
  1622. before a terminating newline (except when PCRE_DOLLAR_ENDONLY is set).
  1623. Note, however, that unless PCRE_DOTALL is set, the "any character"
  1624. metacharacter (.) does not match at a newline. This behaviour (for ^,
  1625. $, and dot) is the same as Perl.
  1626. When PCRE_MULTILINE it is set, the "start of line" and "end of line"
  1627. constructs match immediately following or immediately before internal
  1628. newlines in the subject string, respectively, as well as at the very
  1629. start and end. This is equivalent to Perl's /m option, and it can be
  1630. changed within a pattern by a (?m) option setting. If there are no new-
  1631. lines in a subject string, or no occurrences of ^ or $ in a pattern,
  1632. setting PCRE_MULTILINE has no effect.
  1633. PCRE_NEVER_UTF
  1634. This option locks out interpretation of the pattern as UTF-8 (or UTF-16
  1635. or UTF-32 in the 16-bit and 32-bit libraries). In particular, it pre-
  1636. vents the creator of the pattern from switching to UTF interpretation
  1637. by starting the pattern with (*UTF). This may be useful in applications
  1638. that process patterns from external sources. The combination of
  1639. PCRE_UTF8 and PCRE_NEVER_UTF also causes an error.
  1640. PCRE_NEWLINE_CR
  1641. PCRE_NEWLINE_LF
  1642. PCRE_NEWLINE_CRLF
  1643. PCRE_NEWLINE_ANYCRLF
  1644. PCRE_NEWLINE_ANY
  1645. These options override the default newline definition that was chosen
  1646. when PCRE was built. Setting the first or the second specifies that a
  1647. newline is indicated by a single character (CR or LF, respectively).
  1648. Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by the
  1649. two-character CRLF sequence. Setting PCRE_NEWLINE_ANYCRLF specifies
  1650. that any of the three preceding sequences should be recognized. Setting
  1651. PCRE_NEWLINE_ANY specifies that any Unicode newline sequence should be
  1652. recognized.
  1653. In an ASCII/Unicode environment, the Unicode newline sequences are the
  1654. three just mentioned, plus the single characters VT (vertical tab,
  1655. U+000B), FF (form feed, U+000C), NEL (next line, U+0085), LS (line sep-
  1656. arator, U+2028), and PS (paragraph separator, U+2029). For the 8-bit
  1657. library, the last two are recognized only in UTF-8 mode.
  1658. When PCRE is compiled to run in an EBCDIC (mainframe) environment, the
  1659. code for CR is 0x0d, the same as ASCII. However, the character code for
  1660. LF is normally 0x15, though in some EBCDIC environments 0x25 is used.
  1661. Whichever of these is not LF is made to correspond to Unicode's NEL
  1662. character. EBCDIC codes are all less than 256. For more details, see
  1663. the pcrebuild documentation.
  1664. The newline setting in the options word uses three bits that are
  1665. treated as a number, giving eight possibilities. Currently only six are
  1666. used (default plus the five values above). This means that if you set
  1667. more than one newline option, the combination may or may not be sensi-
  1668. ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to
  1669. PCRE_NEWLINE_CRLF, but other combinations may yield unused numbers and
  1670. cause an error.
  1671. The only time that a line break in a pattern is specially recognized
  1672. when compiling is when PCRE_EXTENDED is set. CR and LF are white space
  1673. characters, and so are ignored in this mode. Also, an unescaped # out-
  1674. side a character class indicates a comment that lasts until after the
  1675. next line break sequence. In other circumstances, line break sequences
  1676. in patterns are treated as literal data.
  1677. The newline option that is set at compile time becomes the default that
  1678. is used for pcre_exec() and pcre_dfa_exec(), but it can be overridden.
  1679. PCRE_NO_AUTO_CAPTURE
  1680. If this option is set, it disables the use of numbered capturing paren-
  1681. theses in the pattern. Any opening parenthesis that is not followed by
  1682. ? behaves as if it were followed by ?: but named parentheses can still
  1683. be used for capturing (and they acquire numbers in the usual way).
  1684. There is no equivalent of this option in Perl.
  1685. PCRE_NO_AUTO_POSSESS
  1686. If this option is set, it disables "auto-possessification". This is an
  1687. optimization that, for example, turns a+b into a++b in order to avoid
  1688. backtracks into a+ that can never be successful. However, if callouts
  1689. are in use, auto-possessification means that some of them are never
  1690. taken. You can set this option if you want the matching functions to do
  1691. a full unoptimized search and run all the callouts, but it is mainly
  1692. provided for testing purposes.
  1693. PCRE_NO_START_OPTIMIZE
  1694. This is an option that acts at matching time; that is, it is really an
  1695. option for pcre_exec() or pcre_dfa_exec(). If it is set at compile
  1696. time, it is remembered with the compiled pattern and assumed at match-
  1697. ing time. This is necessary if you want to use JIT execution, because
  1698. the JIT compiler needs to know whether or not this option is set. For
  1699. details see the discussion of PCRE_NO_START_OPTIMIZE below.
  1700. PCRE_UCP
  1701. This option changes the way PCRE processes \B, \b, \D, \d, \S, \s, \W,
  1702. \w, and some of the POSIX character classes. By default, only ASCII
  1703. characters are recognized, but if PCRE_UCP is set, Unicode properties
  1704. are used instead to classify characters. More details are given in the
  1705. section on generic character types in the pcrepattern page. If you set
  1706. PCRE_UCP, matching one of the items it affects takes much longer. The
  1707. option is available only if PCRE has been compiled with Unicode prop-
  1708. erty support.
  1709. PCRE_UNGREEDY
  1710. This option inverts the "greediness" of the quantifiers so that they
  1711. are not greedy by default, but become greedy if followed by "?". It is
  1712. not compatible with Perl. It can also be set by a (?U) option setting
  1713. within the pattern.
  1714. PCRE_UTF8
  1715. This option causes PCRE to regard both the pattern and the subject as
  1716. strings of UTF-8 characters instead of single-byte strings. However, it
  1717. is available only when PCRE is built to include UTF support. If not,
  1718. the use of this option provokes an error. Details of how this option
  1719. changes the behaviour of PCRE are given in the pcreunicode page.
  1720. PCRE_NO_UTF8_CHECK
  1721. When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is
  1722. automatically checked. There is a discussion about the validity of
  1723. UTF-8 strings in the pcreunicode page. If an invalid UTF-8 sequence is
  1724. found, pcre_compile() returns an error. If you already know that your
  1725. pattern is valid, and you want to skip this check for performance rea-
  1726. sons, you can set the PCRE_NO_UTF8_CHECK option. When it is set, the
  1727. effect of passing an invalid UTF-8 string as a pattern is undefined. It
  1728. may cause your program to crash or loop. Note that this option can also
  1729. be passed to pcre_exec() and pcre_dfa_exec(), to suppress the validity
  1730. checking of subject strings only. If the same string is being matched
  1731. many times, the option can be safely set for the second and subsequent
  1732. matchings to improve performance.
  1733. COMPILATION ERROR CODES
  1734. The following table lists the error codes than may be returned by
  1735. pcre_compile2(), along with the error messages that may be returned by
  1736. both compiling functions. Note that error messages are always 8-bit
  1737. ASCII strings, even in 16-bit or 32-bit mode. As PCRE has developed,
  1738. some error codes have fallen out of use. To avoid confusion, they have
  1739. not been re-used.
  1740. 0 no error
  1741. 1 \ at end of pattern
  1742. 2 \c at end of pattern
  1743. 3 unrecognized character follows \
  1744. 4 numbers out of order in {} quantifier
  1745. 5 number too big in {} quantifier
  1746. 6 missing terminating ] for character class
  1747. 7 invalid escape sequence in character class
  1748. 8 range out of order in character class
  1749. 9 nothing to repeat
  1750. 10 [this code is not in use]
  1751. 11 internal error: unexpected repeat
  1752. 12 unrecognized character after (? or (?-
  1753. 13 POSIX named classes are supported only within a class
  1754. 14 missing )
  1755. 15 reference to non-existent subpattern
  1756. 16 erroffset passed as NULL
  1757. 17 unknown option bit(s) set
  1758. 18 missing ) after comment
  1759. 19 [this code is not in use]
  1760. 20 regular expression is too large
  1761. 21 failed to get memory
  1762. 22 unmatched parentheses
  1763. 23 internal error: code overflow
  1764. 24 unrecognized character after (?<
  1765. 25 lookbehind assertion is not fixed length
  1766. 26 malformed number or name after (?(
  1767. 27 conditional group contains more than two branches
  1768. 28 assertion expected after (?(
  1769. 29 (?R or (?[+-]digits must be followed by )
  1770. 30 unknown POSIX class name
  1771. 31 POSIX collating elements are not supported
  1772. 32 this version of PCRE is compiled without UTF support
  1773. 33 [this code is not in use]
  1774. 34 character value in \x{} or \o{} is too large
  1775. 35 invalid condition (?(0)
  1776. 36 \C not allowed in lookbehind assertion
  1777. 37 PCRE does not support \L, \l, \N{name}, \U, or \u
  1778. 38 number after (?C is > 255
  1779. 39 closing ) for (?C expected
  1780. 40 recursive call could loop indefinitely
  1781. 41 unrecognized character after (?P
  1782. 42 syntax error in subpattern name (missing terminator)
  1783. 43 two named subpatterns have the same name
  1784. 44 invalid UTF-8 string (specifically UTF-8)
  1785. 45 support for \P, \p, and \X has not been compiled
  1786. 46 malformed \P or \p sequence
  1787. 47 unknown property name after \P or \p
  1788. 48 subpattern name is too long (maximum 32 characters)
  1789. 49 too many named subpatterns (maximum 10000)
  1790. 50 [this code is not in use]
  1791. 51 octal value is greater than \377 in 8-bit non-UTF-8 mode
  1792. 52 internal error: overran compiling workspace
  1793. 53 internal error: previously-checked referenced subpattern
  1794. not found
  1795. 54 DEFINE group contains more than one branch
  1796. 55 repeating a DEFINE group is not allowed
  1797. 56 inconsistent NEWLINE options
  1798. 57 \g is not followed by a braced, angle-bracketed, or quoted
  1799. name/number or by a plain number
  1800. 58 a numbered reference must not be zero
  1801. 59 an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)
  1802. 60 (*VERB) not recognized or malformed
  1803. 61 number is too big
  1804. 62 subpattern name expected
  1805. 63 digit expected after (?+
  1806. 64 ] is an invalid data character in JavaScript compatibility mode
  1807. 65 different names for subpatterns of the same number are
  1808. not allowed
  1809. 66 (*MARK) must have an argument
  1810. 67 this version of PCRE is not compiled with Unicode property
  1811. support
  1812. 68 \c must be followed by an ASCII character
  1813. 69 \k is not followed by a braced, angle-bracketed, or quoted name
  1814. 70 internal error: unknown opcode in find_fixedlength()
  1815. 71 \N is not supported in a class
  1816. 72 too many forward references
  1817. 73 disallowed Unicode code point (>= 0xd800 && <= 0xdfff)
  1818. 74 invalid UTF-16 string (specifically UTF-16)
  1819. 75 name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)
  1820. 76 character value in \u.... sequence is too large
  1821. 77 invalid UTF-32 string (specifically UTF-32)
  1822. 78 setting UTF is disabled by the application
  1823. 79 non-hex character in \x{} (closing brace missing?)
  1824. 80 non-octal character in \o{} (closing brace missing?)
  1825. 81 missing opening brace after \o
  1826. 82 parentheses are too deeply nested
  1827. 83 invalid range in character class
  1828. 84 group name must start with a non-digit
  1829. 85 parentheses are too deeply nested (stack check)
  1830. The numbers 32 and 10000 in errors 48 and 49 are defaults; different
  1831. values may be used if the limits were changed when PCRE was built.
  1832. STUDYING A PATTERN
  1833. pcre_extra *pcre_study(const pcre *code, int options,
  1834. const char **errptr);
  1835. If a compiled pattern is going to be used several times, it is worth
  1836. spending more time analyzing it in order to speed up the time taken for
  1837. matching. The function pcre_study() takes a pointer to a compiled pat-
  1838. tern as its first argument. If studying the pattern produces additional
  1839. information that will help speed up matching, pcre_study() returns a
  1840. pointer to a pcre_extra block, in which the study_data field points to
  1841. the results of the study.
  1842. The returned value from pcre_study() can be passed directly to
  1843. pcre_exec() or pcre_dfa_exec(). However, a pcre_extra block also con-
  1844. tains other fields that can be set by the caller before the block is
  1845. passed; these are described below in the section on matching a pattern.
  1846. If studying the pattern does not produce any useful information,
  1847. pcre_study() returns NULL by default. In that circumstance, if the
  1848. calling program wants to pass any of the other fields to pcre_exec() or
  1849. pcre_dfa_exec(), it must set up its own pcre_extra block. However, if
  1850. pcre_study() is called with the PCRE_STUDY_EXTRA_NEEDED option, it
  1851. returns a pcre_extra block even if studying did not find any additional
  1852. information. It may still return NULL, however, if an error occurs in
  1853. pcre_study().
  1854. The second argument of pcre_study() contains option bits. There are
  1855. three further options in addition to PCRE_STUDY_EXTRA_NEEDED:
  1856. PCRE_STUDY_JIT_COMPILE
  1857. PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
  1858. PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
  1859. If any of these are set, and the just-in-time compiler is available,
  1860. the pattern is further compiled into machine code that executes much
  1861. faster than the pcre_exec() interpretive matching function. If the
  1862. just-in-time compiler is not available, these options are ignored. All
  1863. undefined bits in the options argument must be zero.
  1864. JIT compilation is a heavyweight optimization. It can take some time
  1865. for patterns to be analyzed, and for one-off matches and simple pat-
  1866. terns the benefit of faster execution might be offset by a much slower
  1867. study time. Not all patterns can be optimized by the JIT compiler. For
  1868. those that cannot be handled, matching automatically falls back to the
  1869. pcre_exec() interpreter. For more details, see the pcrejit documenta-
  1870. tion.
  1871. The third argument for pcre_study() is a pointer for an error message.
  1872. If studying succeeds (even if no data is returned), the variable it
  1873. points to is set to NULL. Otherwise it is set to point to a textual
  1874. error message. This is a static string that is part of the library. You
  1875. must not try to free it. You should test the error pointer for NULL
  1876. after calling pcre_study(), to be sure that it has run successfully.
  1877. When you are finished with a pattern, you can free the memory used for
  1878. the study data by calling pcre_free_study(). This function was added to
  1879. the API for release 8.20. For earlier versions, the memory could be
  1880. freed with pcre_free(), just like the pattern itself. This will still
  1881. work in cases where JIT optimization is not used, but it is advisable
  1882. to change to the new function when convenient.
  1883. This is a typical way in which pcre_study() is used (except that in a
  1884. real application there should be tests for errors):
  1885. int rc;
  1886. pcre *re;
  1887. pcre_extra *sd;
  1888. re = pcre_compile("pattern", 0, &error, &erroroffset, NULL);
  1889. sd = pcre_study(
  1890. re, /* result of pcre_compile() */
  1891. 0, /* no options */
  1892. &error); /* set to NULL or points to a message */
  1893. rc = pcre_exec( /* see below for details of pcre_exec() options */
  1894. re, sd, "subject", 7, 0, 0, ovector, 30);
  1895. ...
  1896. pcre_free_study(sd);
  1897. pcre_free(re);
  1898. Studying a pattern does two things: first, a lower bound for the length
  1899. of subject string that is needed to match the pattern is computed. This
  1900. does not mean that there are any strings of that length that match, but
  1901. it does guarantee that no shorter strings match. The value is used to
  1902. avoid wasting time by trying to match strings that are shorter than the
  1903. lower bound. You can find out the value in a calling program via the
  1904. pcre_fullinfo() function.
  1905. Studying a pattern is also useful for non-anchored patterns that do not
  1906. have a single fixed starting character. A bitmap of possible starting
  1907. bytes is created. This speeds up finding a position in the subject at
  1908. which to start matching. (In 16-bit mode, the bitmap is used for 16-bit
  1909. values less than 256. In 32-bit mode, the bitmap is used for 32-bit
  1910. values less than 256.)
  1911. These two optimizations apply to both pcre_exec() and pcre_dfa_exec(),
  1912. and the information is also used by the JIT compiler. The optimiza-
  1913. tions can be disabled by setting the PCRE_NO_START_OPTIMIZE option.
  1914. You might want to do this if your pattern contains callouts or (*MARK)
  1915. and you want to make use of these facilities in cases where matching
  1916. fails.
  1917. PCRE_NO_START_OPTIMIZE can be specified at either compile time or exe-
  1918. cution time. However, if PCRE_NO_START_OPTIMIZE is passed to
  1919. pcre_exec(), (that is, after any JIT compilation has happened) JIT exe-
  1920. cution is disabled. For JIT execution to work with PCRE_NO_START_OPTI-
  1921. MIZE, the option must be set at compile time.
  1922. There is a longer discussion of PCRE_NO_START_OPTIMIZE below.
  1923. LOCALE SUPPORT
  1924. PCRE handles caseless matching, and determines whether characters are
  1925. letters, digits, or whatever, by reference to a set of tables, indexed
  1926. by character code point. When running in UTF-8 mode, or in the 16- or
  1927. 32-bit libraries, this applies only to characters with code points less
  1928. than 256. By default, higher-valued code points never match escapes
  1929. such as \w or \d. However, if PCRE is built with Unicode property sup-
  1930. port, all characters can be tested with \p and \P, or, alternatively,
  1931. the PCRE_UCP option can be set when a pattern is compiled; this causes
  1932. \w and friends to use Unicode property support instead of the built-in
  1933. tables.
  1934. The use of locales with Unicode is discouraged. If you are handling
  1935. characters with code points greater than 128, you should either use
  1936. Unicode support, or use locales, but not try to mix the two.
  1937. PCRE contains an internal set of tables that are used when the final
  1938. argument of pcre_compile() is NULL. These are sufficient for many
  1939. applications. Normally, the internal tables recognize only ASCII char-
  1940. acters. However, when PCRE is built, it is possible to cause the inter-
  1941. nal tables to be rebuilt in the default "C" locale of the local system,
  1942. which may cause them to be different.
  1943. The internal tables can always be overridden by tables supplied by the
  1944. application that calls PCRE. These may be created in a different locale
  1945. from the default. As more and more applications change to using Uni-
  1946. code, the need for this locale support is expected to die away.
  1947. External tables are built by calling the pcre_maketables() function,
  1948. which has no arguments, in the relevant locale. The result can then be
  1949. passed to pcre_compile() as often as necessary. For example, to build
  1950. and use tables that are appropriate for the French locale (where
  1951. accented characters with values greater than 128 are treated as let-
  1952. ters), the following code could be used:
  1953. setlocale(LC_CTYPE, "fr_FR");
  1954. tables = pcre_maketables();
  1955. re = pcre_compile(..., tables);
  1956. The locale name "fr_FR" is used on Linux and other Unix-like systems;
  1957. if you are using Windows, the name for the French locale is "french".
  1958. When pcre_maketables() runs, the tables are built in memory that is
  1959. obtained via pcre_malloc. It is the caller's responsibility to ensure
  1960. that the memory containing the tables remains available for as long as
  1961. it is needed.
  1962. The pointer that is passed to pcre_compile() is saved with the compiled
  1963. pattern, and the same tables are used via this pointer by pcre_study()
  1964. and also by pcre_exec() and pcre_dfa_exec(). Thus, for any single pat-
  1965. tern, compilation, studying and matching all happen in the same locale,
  1966. but different patterns can be processed in different locales.
  1967. It is possible to pass a table pointer or NULL (indicating the use of
  1968. the internal tables) to pcre_exec() or pcre_dfa_exec() (see the discus-
  1969. sion below in the section on matching a pattern). This facility is pro-
  1970. vided for use with pre-compiled patterns that have been saved and
  1971. reloaded. Character tables are not saved with patterns, so if a non-
  1972. standard table was used at compile time, it must be provided again when
  1973. the reloaded pattern is matched. Attempting to use this facility to
  1974. match a pattern in a different locale from the one in which it was com-
  1975. piled is likely to lead to anomalous (usually incorrect) results.
  1976. INFORMATION ABOUT A PATTERN
  1977. int pcre_fullinfo(const pcre *code, const pcre_extra *extra,
  1978. int what, void *where);
  1979. The pcre_fullinfo() function returns information about a compiled pat-
  1980. tern. It replaces the pcre_info() function, which was removed from the
  1981. library at version 8.30, after more than 10 years of obsolescence.
  1982. The first argument for pcre_fullinfo() is a pointer to the compiled
  1983. pattern. The second argument is the result of pcre_study(), or NULL if
  1984. the pattern was not studied. The third argument specifies which piece
  1985. of information is required, and the fourth argument is a pointer to a
  1986. variable to receive the data. The yield of the function is zero for
  1987. success, or one of the following negative numbers:
  1988. PCRE_ERROR_NULL the argument code was NULL
  1989. the argument where was NULL
  1990. PCRE_ERROR_BADMAGIC the "magic number" was not found
  1991. PCRE_ERROR_BADENDIANNESS the pattern was compiled with different
  1992. endianness
  1993. PCRE_ERROR_BADOPTION the value of what was invalid
  1994. PCRE_ERROR_UNSET the requested field is not set
  1995. The "magic number" is placed at the start of each compiled pattern as
  1996. an simple check against passing an arbitrary memory pointer. The endi-
  1997. anness error can occur if a compiled pattern is saved and reloaded on a
  1998. different host. Here is a typical call of pcre_fullinfo(), to obtain
  1999. the length of the compiled pattern:
  2000. int rc;
  2001. size_t length;
  2002. rc = pcre_fullinfo(
  2003. re, /* result of pcre_compile() */
  2004. sd, /* result of pcre_study(), or NULL */
  2005. PCRE_INFO_SIZE, /* what is required */
  2006. &length); /* where to put the data */
  2007. The possible values for the third argument are defined in pcre.h, and
  2008. are as follows:
  2009. PCRE_INFO_BACKREFMAX
  2010. Return the number of the highest back reference in the pattern. The
  2011. fourth argument should point to an int variable. Zero is returned if
  2012. there are no back references.
  2013. PCRE_INFO_CAPTURECOUNT
  2014. Return the number of capturing subpatterns in the pattern. The fourth
  2015. argument should point to an int variable.
  2016. PCRE_INFO_DEFAULT_TABLES
  2017. Return a pointer to the internal default character tables within PCRE.
  2018. The fourth argument should point to an unsigned char * variable. This
  2019. information call is provided for internal use by the pcre_study() func-
  2020. tion. External callers can cause PCRE to use its internal tables by
  2021. passing a NULL table pointer.
  2022. PCRE_INFO_FIRSTBYTE (deprecated)
  2023. Return information about the first data unit of any matched string, for
  2024. a non-anchored pattern. The name of this option refers to the 8-bit
  2025. library, where data units are bytes. The fourth argument should point
  2026. to an int variable. Negative values are used for special cases. How-
  2027. ever, this means that when the 32-bit library is in non-UTF-32 mode,
  2028. the full 32-bit range of characters cannot be returned. For this rea-
  2029. son, this value is deprecated; use PCRE_INFO_FIRSTCHARACTERFLAGS and
  2030. PCRE_INFO_FIRSTCHARACTER instead.
  2031. If there is a fixed first value, for example, the letter "c" from a
  2032. pattern such as (cat|cow|coyote), its value is returned. In the 8-bit
  2033. library, the value is always less than 256. In the 16-bit library the
  2034. value can be up to 0xffff. In the 32-bit library the value can be up to
  2035. 0x10ffff.
  2036. If there is no fixed first value, and if either
  2037. (a) the pattern was compiled with the PCRE_MULTILINE option, and every
  2038. branch starts with "^", or
  2039. (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not
  2040. set (if it were set, the pattern would be anchored),
  2041. -1 is returned, indicating that the pattern matches only at the start
  2042. of a subject string or after any newline within the string. Otherwise
  2043. -2 is returned. For anchored patterns, -2 is returned.
  2044. PCRE_INFO_FIRSTCHARACTER
  2045. Return the value of the first data unit (non-UTF character) of any
  2046. matched string in the situation where PCRE_INFO_FIRSTCHARACTERFLAGS
  2047. returns 1; otherwise return 0. The fourth argument should point to an
  2048. uint_t variable.
  2049. In the 8-bit library, the value is always less than 256. In the 16-bit
  2050. library the value can be up to 0xffff. In the 32-bit library in UTF-32
  2051. mode the value can be up to 0x10ffff, and up to 0xffffffff when not
  2052. using UTF-32 mode.
  2053. PCRE_INFO_FIRSTCHARACTERFLAGS
  2054. Return information about the first data unit of any matched string, for
  2055. a non-anchored pattern. The fourth argument should point to an int
  2056. variable.
  2057. If there is a fixed first value, for example, the letter "c" from a
  2058. pattern such as (cat|cow|coyote), 1 is returned, and the character
  2059. value can be retrieved using PCRE_INFO_FIRSTCHARACTER. If there is no
  2060. fixed first value, and if either
  2061. (a) the pattern was compiled with the PCRE_MULTILINE option, and every
  2062. branch starts with "^", or
  2063. (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not
  2064. set (if it were set, the pattern would be anchored),
  2065. 2 is returned, indicating that the pattern matches only at the start of
  2066. a subject string or after any newline within the string. Otherwise 0 is
  2067. returned. For anchored patterns, 0 is returned.
  2068. PCRE_INFO_FIRSTTABLE
  2069. If the pattern was studied, and this resulted in the construction of a
  2070. 256-bit table indicating a fixed set of values for the first data unit
  2071. in any matching string, a pointer to the table is returned. Otherwise
  2072. NULL is returned. The fourth argument should point to an unsigned char
  2073. * variable.
  2074. PCRE_INFO_HASCRORLF
  2075. Return 1 if the pattern contains any explicit matches for CR or LF
  2076. characters, otherwise 0. The fourth argument should point to an int
  2077. variable. An explicit match is either a literal CR or LF character, or
  2078. \r or \n.
  2079. PCRE_INFO_JCHANGED
  2080. Return 1 if the (?J) or (?-J) option setting is used in the pattern,
  2081. otherwise 0. The fourth argument should point to an int variable. (?J)
  2082. and (?-J) set and unset the local PCRE_DUPNAMES option, respectively.
  2083. PCRE_INFO_JIT
  2084. Return 1 if the pattern was studied with one of the JIT options, and
  2085. just-in-time compiling was successful. The fourth argument should point
  2086. to an int variable. A return value of 0 means that JIT support is not
  2087. available in this version of PCRE, or that the pattern was not studied
  2088. with a JIT option, or that the JIT compiler could not handle this par-
  2089. ticular pattern. See the pcrejit documentation for details of what can
  2090. and cannot be handled.
  2091. PCRE_INFO_JITSIZE
  2092. If the pattern was successfully studied with a JIT option, return the
  2093. size of the JIT compiled code, otherwise return zero. The fourth argu-
  2094. ment should point to a size_t variable.
  2095. PCRE_INFO_LASTLITERAL
  2096. Return the value of the rightmost literal data unit that must exist in
  2097. any matched string, other than at its start, if such a value has been
  2098. recorded. The fourth argument should point to an int variable. If there
  2099. is no such value, -1 is returned. For anchored patterns, a last literal
  2100. value is recorded only if it follows something of variable length. For
  2101. example, for the pattern /^a\d+z\d+/ the returned value is "z", but for
  2102. /^a\dz\d/ the returned value is -1.
  2103. Since for the 32-bit library using the non-UTF-32 mode, this function
  2104. is unable to return the full 32-bit range of characters, this value is
  2105. deprecated; instead the PCRE_INFO_REQUIREDCHARFLAGS and
  2106. PCRE_INFO_REQUIREDCHAR values should be used.
  2107. PCRE_INFO_MATCH_EMPTY
  2108. Return 1 if the pattern can match an empty string, otherwise 0. The
  2109. fourth argument should point to an int variable.
  2110. PCRE_INFO_MATCHLIMIT
  2111. If the pattern set a match limit by including an item of the form
  2112. (*LIMIT_MATCH=nnnn) at the start, the value is returned. The fourth
  2113. argument should point to an unsigned 32-bit integer. If no such value
  2114. has been set, the call to pcre_fullinfo() returns the error
  2115. PCRE_ERROR_UNSET.
  2116. PCRE_INFO_MAXLOOKBEHIND
  2117. Return the number of characters (NB not data units) in the longest
  2118. lookbehind assertion in the pattern. This information is useful when
  2119. doing multi-segment matching using the partial matching facilities.
  2120. Note that the simple assertions \b and \B require a one-character look-
  2121. behind. \A also registers a one-character lookbehind, though it does
  2122. not actually inspect the previous character. This is to ensure that at
  2123. least one character from the old segment is retained when a new segment
  2124. is processed. Otherwise, if there are no lookbehinds in the pattern, \A
  2125. might match incorrectly at the start of a new segment.
  2126. PCRE_INFO_MINLENGTH
  2127. If the pattern was studied and a minimum length for matching subject
  2128. strings was computed, its value is returned. Otherwise the returned
  2129. value is -1. The value is a number of characters, which in UTF mode may
  2130. be different from the number of data units. The fourth argument should
  2131. point to an int variable. A non-negative value is a lower bound to the
  2132. length of any matching string. There may not be any strings of that
  2133. length that do actually match, but every string that does match is at
  2134. least that long.
  2135. PCRE_INFO_NAMECOUNT
  2136. PCRE_INFO_NAMEENTRYSIZE
  2137. PCRE_INFO_NAMETABLE
  2138. PCRE supports the use of named as well as numbered capturing parenthe-
  2139. ses. The names are just an additional way of identifying the parenthe-
  2140. ses, which still acquire numbers. Several convenience functions such as
  2141. pcre_get_named_substring() are provided for extracting captured sub-
  2142. strings by name. It is also possible to extract the data directly, by
  2143. first converting the name to a number in order to access the correct
  2144. pointers in the output vector (described with pcre_exec() below). To do
  2145. the conversion, you need to use the name-to-number map, which is
  2146. described by these three values.
  2147. The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT
  2148. gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size
  2149. of each entry; both of these return an int value. The entry size
  2150. depends on the length of the longest name. PCRE_INFO_NAMETABLE returns
  2151. a pointer to the first entry of the table. This is a pointer to char in
  2152. the 8-bit library, where the first two bytes of each entry are the num-
  2153. ber of the capturing parenthesis, most significant byte first. In the
  2154. 16-bit library, the pointer points to 16-bit data units, the first of
  2155. which contains the parenthesis number. In the 32-bit library, the
  2156. pointer points to 32-bit data units, the first of which contains the
  2157. parenthesis number. The rest of the entry is the corresponding name,
  2158. zero terminated.
  2159. The names are in alphabetical order. If (?| is used to create multiple
  2160. groups with the same number, as described in the section on duplicate
  2161. subpattern numbers in the pcrepattern page, the groups may be given the
  2162. same name, but there is only one entry in the table. Different names
  2163. for groups of the same number are not permitted. Duplicate names for
  2164. subpatterns with different numbers are permitted, but only if PCRE_DUP-
  2165. NAMES is set. They appear in the table in the order in which they were
  2166. found in the pattern. In the absence of (?| this is the order of
  2167. increasing number; when (?| is used this is not necessarily the case
  2168. because later subpatterns may have lower numbers.
  2169. As a simple example of the name/number table, consider the following
  2170. pattern after compilation by the 8-bit library (assume PCRE_EXTENDED is
  2171. set, so white space - including newlines - is ignored):
  2172. (?<date> (?<year>(\d\d)?\d\d) -
  2173. (?<month>\d\d) - (?<day>\d\d) )
  2174. There are four named subpatterns, so the table has four entries, and
  2175. each entry in the table is eight bytes long. The table is as follows,
  2176. with non-printing bytes shows in hexadecimal, and undefined bytes shown
  2177. as ??:
  2178. 00 01 d a t e 00 ??
  2179. 00 05 d a y 00 ?? ??
  2180. 00 04 m o n t h 00
  2181. 00 02 y e a r 00 ??
  2182. When writing code to extract data from named subpatterns using the
  2183. name-to-number map, remember that the length of the entries is likely
  2184. to be different for each compiled pattern.
  2185. PCRE_INFO_OKPARTIAL
  2186. Return 1 if the pattern can be used for partial matching with
  2187. pcre_exec(), otherwise 0. The fourth argument should point to an int
  2188. variable. From release 8.00, this always returns 1, because the
  2189. restrictions that previously applied to partial matching have been
  2190. lifted. The pcrepartial documentation gives details of partial match-
  2191. ing.
  2192. PCRE_INFO_OPTIONS
  2193. Return a copy of the options with which the pattern was compiled. The
  2194. fourth argument should point to an unsigned long int variable. These
  2195. option bits are those specified in the call to pcre_compile(), modified
  2196. by any top-level option settings at the start of the pattern itself. In
  2197. other words, they are the options that will be in force when matching
  2198. starts. For example, if the pattern /(?im)abc(?-i)d/ is compiled with
  2199. the PCRE_EXTENDED option, the result is PCRE_CASELESS, PCRE_MULTILINE,
  2200. and PCRE_EXTENDED.
  2201. A pattern is automatically anchored by PCRE if all of its top-level
  2202. alternatives begin with one of the following:
  2203. ^ unless PCRE_MULTILINE is set
  2204. \A always
  2205. \G always
  2206. .* if PCRE_DOTALL is set and there are no back
  2207. references to the subpattern in which .* appears
  2208. For such patterns, the PCRE_ANCHORED bit is set in the options returned
  2209. by pcre_fullinfo().
  2210. PCRE_INFO_RECURSIONLIMIT
  2211. If the pattern set a recursion limit by including an item of the form
  2212. (*LIMIT_RECURSION=nnnn) at the start, the value is returned. The fourth
  2213. argument should point to an unsigned 32-bit integer. If no such value
  2214. has been set, the call to pcre_fullinfo() returns the error
  2215. PCRE_ERROR_UNSET.
  2216. PCRE_INFO_SIZE
  2217. Return the size of the compiled pattern in bytes (for all three
  2218. libraries). The fourth argument should point to a size_t variable. This
  2219. value does not include the size of the pcre structure that is returned
  2220. by pcre_compile(). The value that is passed as the argument to
  2221. pcre_malloc() when pcre_compile() is getting memory in which to place
  2222. the compiled data is the value returned by this option plus the size of
  2223. the pcre structure. Studying a compiled pattern, with or without JIT,
  2224. does not alter the value returned by this option.
  2225. PCRE_INFO_STUDYSIZE
  2226. Return the size in bytes (for all three libraries) of the data block
  2227. pointed to by the study_data field in a pcre_extra block. If pcre_extra
  2228. is NULL, or there is no study data, zero is returned. The fourth argu-
  2229. ment should point to a size_t variable. The study_data field is set by
  2230. pcre_study() to record information that will speed up matching (see the
  2231. section entitled "Studying a pattern" above). The format of the
  2232. study_data block is private, but its length is made available via this
  2233. option so that it can be saved and restored (see the pcreprecompile
  2234. documentation for details).
  2235. PCRE_INFO_REQUIREDCHARFLAGS
  2236. Returns 1 if there is a rightmost literal data unit that must exist in
  2237. any matched string, other than at its start. The fourth argument should
  2238. point to an int variable. If there is no such value, 0 is returned. If
  2239. returning 1, the character value itself can be retrieved using
  2240. PCRE_INFO_REQUIREDCHAR.
  2241. For anchored patterns, a last literal value is recorded only if it fol-
  2242. lows something of variable length. For example, for the pattern
  2243. /^a\d+z\d+/ the returned value 1 (with "z" returned from
  2244. PCRE_INFO_REQUIREDCHAR), but for /^a\dz\d/ the returned value is 0.
  2245. PCRE_INFO_REQUIREDCHAR
  2246. Return the value of the rightmost literal data unit that must exist in
  2247. any matched string, other than at its start, if such a value has been
  2248. recorded. The fourth argument should point to an uint32_t variable. If
  2249. there is no such value, 0 is returned.
  2250. REFERENCE COUNTS
  2251. int pcre_refcount(pcre *code, int adjust);
  2252. The pcre_refcount() function is used to maintain a reference count in
  2253. the data block that contains a compiled pattern. It is provided for the
  2254. benefit of applications that operate in an object-oriented manner,
  2255. where different parts of the application may be using the same compiled
  2256. pattern, but you want to free the block when they are all done.
  2257. When a pattern is compiled, the reference count field is initialized to
  2258. zero. It is changed only by calling this function, whose action is to
  2259. add the adjust value (which may be positive or negative) to it. The
  2260. yield of the function is the new value. However, the value of the count
  2261. is constrained to lie between 0 and 65535, inclusive. If the new value
  2262. is outside these limits, it is forced to the appropriate limit value.
  2263. Except when it is zero, the reference count is not correctly preserved
  2264. if a pattern is compiled on one host and then transferred to a host
  2265. whose byte-order is different. (This seems a highly unlikely scenario.)
  2266. MATCHING A PATTERN: THE TRADITIONAL FUNCTION
  2267. int pcre_exec(const pcre *code, const pcre_extra *extra,
  2268. const char *subject, int length, int startoffset,
  2269. int options, int *ovector, int ovecsize);
  2270. The function pcre_exec() is called to match a subject string against a
  2271. compiled pattern, which is passed in the code argument. If the pattern
  2272. was studied, the result of the study should be passed in the extra
  2273. argument. You can call pcre_exec() with the same code and extra argu-
  2274. ments as many times as you like, in order to match different subject
  2275. strings with the same pattern.
  2276. This function is the main matching facility of the library, and it
  2277. operates in a Perl-like manner. For specialist use there is also an
  2278. alternative matching function, which is described below in the section
  2279. about the pcre_dfa_exec() function.
  2280. In most applications, the pattern will have been compiled (and option-
  2281. ally studied) in the same process that calls pcre_exec(). However, it
  2282. is possible to save compiled patterns and study data, and then use them
  2283. later in different processes, possibly even on different hosts. For a
  2284. discussion about this, see the pcreprecompile documentation.
  2285. Here is an example of a simple call to pcre_exec():
  2286. int rc;
  2287. int ovector[30];
  2288. rc = pcre_exec(
  2289. re, /* result of pcre_compile() */
  2290. NULL, /* we didn't study the pattern */
  2291. "some string", /* the subject string */
  2292. 11, /* the length of the subject string */
  2293. 0, /* start at offset 0 in the subject */
  2294. 0, /* default options */
  2295. ovector, /* vector of integers for substring information */
  2296. 30); /* number of elements (NOT size in bytes) */
  2297. Extra data for pcre_exec()
  2298. If the extra argument is not NULL, it must point to a pcre_extra data
  2299. block. The pcre_study() function returns such a block (when it doesn't
  2300. return NULL), but you can also create one for yourself, and pass addi-
  2301. tional information in it. The pcre_extra block contains the following
  2302. fields (not necessarily in this order):
  2303. unsigned long int flags;
  2304. void *study_data;
  2305. void *executable_jit;
  2306. unsigned long int match_limit;
  2307. unsigned long int match_limit_recursion;
  2308. void *callout_data;
  2309. const unsigned char *tables;
  2310. unsigned char **mark;
  2311. In the 16-bit version of this structure, the mark field has type
  2312. "PCRE_UCHAR16 **".
  2313. In the 32-bit version of this structure, the mark field has type
  2314. "PCRE_UCHAR32 **".
  2315. The flags field is used to specify which of the other fields are set.
  2316. The flag bits are:
  2317. PCRE_EXTRA_CALLOUT_DATA
  2318. PCRE_EXTRA_EXECUTABLE_JIT
  2319. PCRE_EXTRA_MARK
  2320. PCRE_EXTRA_MATCH_LIMIT
  2321. PCRE_EXTRA_MATCH_LIMIT_RECURSION
  2322. PCRE_EXTRA_STUDY_DATA
  2323. PCRE_EXTRA_TABLES
  2324. Other flag bits should be set to zero. The study_data field and some-
  2325. times the executable_jit field are set in the pcre_extra block that is
  2326. returned by pcre_study(), together with the appropriate flag bits. You
  2327. should not set these yourself, but you may add to the block by setting
  2328. other fields and their corresponding flag bits.
  2329. The match_limit field provides a means of preventing PCRE from using up
  2330. a vast amount of resources when running patterns that are not going to
  2331. match, but which have a very large number of possibilities in their
  2332. search trees. The classic example is a pattern that uses nested unlim-
  2333. ited repeats.
  2334. Internally, pcre_exec() uses a function called match(), which it calls
  2335. repeatedly (sometimes recursively). The limit set by match_limit is
  2336. imposed on the number of times this function is called during a match,
  2337. which has the effect of limiting the amount of backtracking that can
  2338. take place. For patterns that are not anchored, the count restarts from
  2339. zero for each position in the subject string.
  2340. When pcre_exec() is called with a pattern that was successfully studied
  2341. with a JIT option, the way that the matching is executed is entirely
  2342. different. However, there is still the possibility of runaway matching
  2343. that goes on for a very long time, and so the match_limit value is also
  2344. used in this case (but in a different way) to limit how long the match-
  2345. ing can continue.
  2346. The default value for the limit can be set when PCRE is built; the
  2347. default default is 10 million, which handles all but the most extreme
  2348. cases. You can override the default by suppling pcre_exec() with a
  2349. pcre_extra block in which match_limit is set, and
  2350. PCRE_EXTRA_MATCH_LIMIT is set in the flags field. If the limit is
  2351. exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT.
  2352. A value for the match limit may also be supplied by an item at the
  2353. start of a pattern of the form
  2354. (*LIMIT_MATCH=d)
  2355. where d is a decimal number. However, such a setting is ignored unless
  2356. d is less than the limit set by the caller of pcre_exec() or, if no
  2357. such limit is set, less than the default.
  2358. The match_limit_recursion field is similar to match_limit, but instead
  2359. of limiting the total number of times that match() is called, it limits
  2360. the depth of recursion. The recursion depth is a smaller number than
  2361. the total number of calls, because not all calls to match() are recur-
  2362. sive. This limit is of use only if it is set smaller than match_limit.
  2363. Limiting the recursion depth limits the amount of machine stack that
  2364. can be used, or, when PCRE has been compiled to use memory on the heap
  2365. instead of the stack, the amount of heap memory that can be used. This
  2366. limit is not relevant, and is ignored, when matching is done using JIT
  2367. compiled code.
  2368. The default value for match_limit_recursion can be set when PCRE is
  2369. built; the default default is the same value as the default for
  2370. match_limit. You can override the default by suppling pcre_exec() with
  2371. a pcre_extra block in which match_limit_recursion is set, and
  2372. PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in the flags field. If the
  2373. limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT.
  2374. A value for the recursion limit may also be supplied by an item at the
  2375. start of a pattern of the form
  2376. (*LIMIT_RECURSION=d)
  2377. where d is a decimal number. However, such a setting is ignored unless
  2378. d is less than the limit set by the caller of pcre_exec() or, if no
  2379. such limit is set, less than the default.
  2380. The callout_data field is used in conjunction with the "callout" fea-
  2381. ture, and is described in the pcrecallout documentation.
  2382. The tables field is provided for use with patterns that have been pre-
  2383. compiled using custom character tables, saved to disc or elsewhere, and
  2384. then reloaded, because the tables that were used to compile a pattern
  2385. are not saved with it. See the pcreprecompile documentation for a dis-
  2386. cussion of saving compiled patterns for later use. If NULL is passed
  2387. using this mechanism, it forces PCRE's internal tables to be used.
  2388. Warning: The tables that pcre_exec() uses must be the same as those
  2389. that were used when the pattern was compiled. If this is not the case,
  2390. the behaviour of pcre_exec() is undefined. Therefore, when a pattern is
  2391. compiled and matched in the same process, this field should never be
  2392. set. In this (the most common) case, the correct table pointer is auto-
  2393. matically passed with the compiled pattern from pcre_compile() to
  2394. pcre_exec().
  2395. If PCRE_EXTRA_MARK is set in the flags field, the mark field must be
  2396. set to point to a suitable variable. If the pattern contains any back-
  2397. tracking control verbs such as (*MARK:NAME), and the execution ends up
  2398. with a name to pass back, a pointer to the name string (zero termi-
  2399. nated) is placed in the variable pointed to by the mark field. The
  2400. names are within the compiled pattern; if you wish to retain such a
  2401. name you must copy it before freeing the memory of a compiled pattern.
  2402. If there is no name to pass back, the variable pointed to by the mark
  2403. field is set to NULL. For details of the backtracking control verbs,
  2404. see the section entitled "Backtracking control" in the pcrepattern doc-
  2405. umentation.
  2406. Option bits for pcre_exec()
  2407. The unused bits of the options argument for pcre_exec() must be zero.
  2408. The only bits that may be set are PCRE_ANCHORED, PCRE_NEWLINE_xxx,
  2409. PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART,
  2410. PCRE_NO_START_OPTIMIZE, PCRE_NO_UTF8_CHECK, PCRE_PARTIAL_HARD, and
  2411. PCRE_PARTIAL_SOFT.
  2412. If the pattern was successfully studied with one of the just-in-time
  2413. (JIT) compile options, the only supported options for JIT execution are
  2414. PCRE_NO_UTF8_CHECK, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY,
  2415. PCRE_NOTEMPTY_ATSTART, PCRE_PARTIAL_HARD, and PCRE_PARTIAL_SOFT. If an
  2416. unsupported option is used, JIT execution is disabled and the normal
  2417. interpretive code in pcre_exec() is run.
  2418. PCRE_ANCHORED
  2419. The PCRE_ANCHORED option limits pcre_exec() to matching at the first
  2420. matching position. If a pattern was compiled with PCRE_ANCHORED, or
  2421. turned out to be anchored by virtue of its contents, it cannot be made
  2422. unachored at matching time.
  2423. PCRE_BSR_ANYCRLF
  2424. PCRE_BSR_UNICODE
  2425. These options (which are mutually exclusive) control what the \R escape
  2426. sequence matches. The choice is either to match only CR, LF, or CRLF,
  2427. or to match any Unicode newline sequence. These options override the
  2428. choice that was made or defaulted when the pattern was compiled.
  2429. PCRE_NEWLINE_CR
  2430. PCRE_NEWLINE_LF
  2431. PCRE_NEWLINE_CRLF
  2432. PCRE_NEWLINE_ANYCRLF
  2433. PCRE_NEWLINE_ANY
  2434. These options override the newline definition that was chosen or
  2435. defaulted when the pattern was compiled. For details, see the descrip-
  2436. tion of pcre_compile() above. During matching, the newline choice
  2437. affects the behaviour of the dot, circumflex, and dollar metacharac-
  2438. ters. It may also alter the way the match position is advanced after a
  2439. match failure for an unanchored pattern.
  2440. When PCRE_NEWLINE_CRLF, PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is
  2441. set, and a match attempt for an unanchored pattern fails when the cur-
  2442. rent position is at a CRLF sequence, and the pattern contains no
  2443. explicit matches for CR or LF characters, the match position is
  2444. advanced by two characters instead of one, in other words, to after the
  2445. CRLF.
  2446. The above rule is a compromise that makes the most common cases work as
  2447. expected. For example, if the pattern is .+A (and the PCRE_DOTALL
  2448. option is not set), it does not match the string "\r\nA" because, after
  2449. failing at the start, it skips both the CR and the LF before retrying.
  2450. However, the pattern [\r\n]A does match that string, because it con-
  2451. tains an explicit CR or LF reference, and so advances only by one char-
  2452. acter after the first failure.
  2453. An explicit match for CR of LF is either a literal appearance of one of
  2454. those characters, or one of the \r or \n escape sequences. Implicit
  2455. matches such as [^X] do not count, nor does \s (which includes CR and
  2456. LF in the characters that it matches).
  2457. Notwithstanding the above, anomalous effects may still occur when CRLF
  2458. is a valid newline sequence and explicit \r or \n escapes appear in the
  2459. pattern.
  2460. PCRE_NOTBOL
  2461. This option specifies that first character of the subject string is not
  2462. the beginning of a line, so the circumflex metacharacter should not
  2463. match before it. Setting this without PCRE_MULTILINE (at compile time)
  2464. causes circumflex never to match. This option affects only the behav-
  2465. iour of the circumflex metacharacter. It does not affect \A.
  2466. PCRE_NOTEOL
  2467. This option specifies that the end of the subject string is not the end
  2468. of a line, so the dollar metacharacter should not match it nor (except
  2469. in multiline mode) a newline immediately before it. Setting this with-
  2470. out PCRE_MULTILINE (at compile time) causes dollar never to match. This
  2471. option affects only the behaviour of the dollar metacharacter. It does
  2472. not affect \Z or \z.
  2473. PCRE_NOTEMPTY
  2474. An empty string is not considered to be a valid match if this option is
  2475. set. If there are alternatives in the pattern, they are tried. If all
  2476. the alternatives match the empty string, the entire match fails. For
  2477. example, if the pattern
  2478. a?b?
  2479. is applied to a string not beginning with "a" or "b", it matches an
  2480. empty string at the start of the subject. With PCRE_NOTEMPTY set, this
  2481. match is not valid, so PCRE searches further into the string for occur-
  2482. rences of "a" or "b".
  2483. PCRE_NOTEMPTY_ATSTART
  2484. This is like PCRE_NOTEMPTY, except that an empty string match that is
  2485. not at the start of the subject is permitted. If the pattern is
  2486. anchored, such a match can occur only if the pattern contains \K.
  2487. Perl has no direct equivalent of PCRE_NOTEMPTY or
  2488. PCRE_NOTEMPTY_ATSTART, but it does make a special case of a pattern
  2489. match of the empty string within its split() function, and when using
  2490. the /g modifier. It is possible to emulate Perl's behaviour after
  2491. matching a null string by first trying the match again at the same off-
  2492. set with PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED, and then if that
  2493. fails, by advancing the starting offset (see below) and trying an ordi-
  2494. nary match again. There is some code that demonstrates how to do this
  2495. in the pcredemo sample program. In the most general case, you have to
  2496. check to see if the newline convention recognizes CRLF as a newline,
  2497. and if so, and the current character is CR followed by LF, advance the
  2498. starting offset by two characters instead of one.
  2499. PCRE_NO_START_OPTIMIZE
  2500. There are a number of optimizations that pcre_exec() uses at the start
  2501. of a match, in order to speed up the process. For example, if it is
  2502. known that an unanchored match must start with a specific character, it
  2503. searches the subject for that character, and fails immediately if it
  2504. cannot find it, without actually running the main matching function.
  2505. This means that a special item such as (*COMMIT) at the start of a pat-
  2506. tern is not considered until after a suitable starting point for the
  2507. match has been found. Also, when callouts or (*MARK) items are in use,
  2508. these "start-up" optimizations can cause them to be skipped if the pat-
  2509. tern is never actually used. The start-up optimizations are in effect a
  2510. pre-scan of the subject that takes place before the pattern is run.
  2511. The PCRE_NO_START_OPTIMIZE option disables the start-up optimizations,
  2512. possibly causing performance to suffer, but ensuring that in cases
  2513. where the result is "no match", the callouts do occur, and that items
  2514. such as (*COMMIT) and (*MARK) are considered at every possible starting
  2515. position in the subject string. If PCRE_NO_START_OPTIMIZE is set at
  2516. compile time, it cannot be unset at matching time. The use of
  2517. PCRE_NO_START_OPTIMIZE at matching time (that is, passing it to
  2518. pcre_exec()) disables JIT execution; in this situation, matching is
  2519. always done using interpretively.
  2520. Setting PCRE_NO_START_OPTIMIZE can change the outcome of a matching
  2521. operation. Consider the pattern
  2522. (*COMMIT)ABC
  2523. When this is compiled, PCRE records the fact that a match must start
  2524. with the character "A". Suppose the subject string is "DEFABC". The
  2525. start-up optimization scans along the subject, finds "A" and runs the
  2526. first match attempt from there. The (*COMMIT) item means that the pat-
  2527. tern must match the current starting position, which in this case, it
  2528. does. However, if the same match is run with PCRE_NO_START_OPTIMIZE
  2529. set, the initial scan along the subject string does not happen. The
  2530. first match attempt is run starting from "D" and when this fails,
  2531. (*COMMIT) prevents any further matches being tried, so the overall
  2532. result is "no match". If the pattern is studied, more start-up opti-
  2533. mizations may be used. For example, a minimum length for the subject
  2534. may be recorded. Consider the pattern
  2535. (*MARK:A)(X|Y)
  2536. The minimum length for a match is one character. If the subject is
  2537. "ABC", there will be attempts to match "ABC", "BC", "C", and then
  2538. finally an empty string. If the pattern is studied, the final attempt
  2539. does not take place, because PCRE knows that the subject is too short,
  2540. and so the (*MARK) is never encountered. In this case, studying the
  2541. pattern does not affect the overall match result, which is still "no
  2542. match", but it does affect the auxiliary information that is returned.
  2543. PCRE_NO_UTF8_CHECK
  2544. When PCRE_UTF8 is set at compile time, the validity of the subject as a
  2545. UTF-8 string is automatically checked when pcre_exec() is subsequently
  2546. called. The entire string is checked before any other processing takes
  2547. place. The value of startoffset is also checked to ensure that it
  2548. points to the start of a UTF-8 character. There is a discussion about
  2549. the validity of UTF-8 strings in the pcreunicode page. If an invalid
  2550. sequence of bytes is found, pcre_exec() returns the error
  2551. PCRE_ERROR_BADUTF8 or, if PCRE_PARTIAL_HARD is set and the problem is a
  2552. truncated character at the end of the subject, PCRE_ERROR_SHORTUTF8. In
  2553. both cases, information about the precise nature of the error may also
  2554. be returned (see the descriptions of these errors in the section enti-
  2555. tled Error return values from pcre_exec() below). If startoffset con-
  2556. tains a value that does not point to the start of a UTF-8 character (or
  2557. to the end of the subject), PCRE_ERROR_BADUTF8_OFFSET is returned.
  2558. If you already know that your subject is valid, and you want to skip
  2559. these checks for performance reasons, you can set the
  2560. PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might want to
  2561. do this for the second and subsequent calls to pcre_exec() if you are
  2562. making repeated calls to find all the matches in a single subject
  2563. string. However, you should be sure that the value of startoffset
  2564. points to the start of a character (or the end of the subject). When
  2565. PCRE_NO_UTF8_CHECK is set, the effect of passing an invalid string as a
  2566. subject or an invalid value of startoffset is undefined. Your program
  2567. may crash or loop.
  2568. PCRE_PARTIAL_HARD
  2569. PCRE_PARTIAL_SOFT
  2570. These options turn on the partial matching feature. For backwards com-
  2571. patibility, PCRE_PARTIAL is a synonym for PCRE_PARTIAL_SOFT. A partial
  2572. match occurs if the end of the subject string is reached successfully,
  2573. but there are not enough subject characters to complete the match. If
  2574. this happens when PCRE_PARTIAL_SOFT (but not PCRE_PARTIAL_HARD) is set,
  2575. matching continues by testing any remaining alternatives. Only if no
  2576. complete match can be found is PCRE_ERROR_PARTIAL returned instead of
  2577. PCRE_ERROR_NOMATCH. In other words, PCRE_PARTIAL_SOFT says that the
  2578. caller is prepared to handle a partial match, but only if no complete
  2579. match can be found.
  2580. If PCRE_PARTIAL_HARD is set, it overrides PCRE_PARTIAL_SOFT. In this
  2581. case, if a partial match is found, pcre_exec() immediately returns
  2582. PCRE_ERROR_PARTIAL, without considering any other alternatives. In
  2583. other words, when PCRE_PARTIAL_HARD is set, a partial match is consid-
  2584. ered to be more important that an alternative complete match.
  2585. In both cases, the portion of the string that was inspected when the
  2586. partial match was found is set as the first matching string. There is a
  2587. more detailed discussion of partial and multi-segment matching, with
  2588. examples, in the pcrepartial documentation.
  2589. The string to be matched by pcre_exec()
  2590. The subject string is passed to pcre_exec() as a pointer in subject, a
  2591. length in length, and a starting offset in startoffset. The units for
  2592. length and startoffset are bytes for the 8-bit library, 16-bit data
  2593. items for the 16-bit library, and 32-bit data items for the 32-bit
  2594. library.
  2595. If startoffset is negative or greater than the length of the subject,
  2596. pcre_exec() returns PCRE_ERROR_BADOFFSET. When the starting offset is
  2597. zero, the search for a match starts at the beginning of the subject,
  2598. and this is by far the most common case. In UTF-8 or UTF-16 mode, the
  2599. offset must point to the start of a character, or the end of the sub-
  2600. ject (in UTF-32 mode, one data unit equals one character, so all off-
  2601. sets are valid). Unlike the pattern string, the subject may contain
  2602. binary zeroes.
  2603. A non-zero starting offset is useful when searching for another match
  2604. in the same subject by calling pcre_exec() again after a previous suc-
  2605. cess. Setting startoffset differs from just passing over a shortened
  2606. string and setting PCRE_NOTBOL in the case of a pattern that begins
  2607. with any kind of lookbehind. For example, consider the pattern
  2608. \Biss\B
  2609. which finds occurrences of "iss" in the middle of words. (\B matches
  2610. only if the current position in the subject is not a word boundary.)
  2611. When applied to the string "Mississipi" the first call to pcre_exec()
  2612. finds the first occurrence. If pcre_exec() is called again with just
  2613. the remainder of the subject, namely "issipi", it does not match,
  2614. because \B is always false at the start of the subject, which is deemed
  2615. to be a word boundary. However, if pcre_exec() is passed the entire
  2616. string again, but with startoffset set to 4, it finds the second occur-
  2617. rence of "iss" because it is able to look behind the starting point to
  2618. discover that it is preceded by a letter.
  2619. Finding all the matches in a subject is tricky when the pattern can
  2620. match an empty string. It is possible to emulate Perl's /g behaviour by
  2621. first trying the match again at the same offset, with the
  2622. PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED options, and then if that
  2623. fails, advancing the starting offset and trying an ordinary match
  2624. again. There is some code that demonstrates how to do this in the pcre-
  2625. demo sample program. In the most general case, you have to check to see
  2626. if the newline convention recognizes CRLF as a newline, and if so, and
  2627. the current character is CR followed by LF, advance the starting offset
  2628. by two characters instead of one.
  2629. If a non-zero starting offset is passed when the pattern is anchored,
  2630. one attempt to match at the given offset is made. This can only succeed
  2631. if the pattern does not require the match to be at the start of the
  2632. subject.
  2633. How pcre_exec() returns captured substrings
  2634. In general, a pattern matches a certain portion of the subject, and in
  2635. addition, further substrings from the subject may be picked out by
  2636. parts of the pattern. Following the usage in Jeffrey Friedl's book,
  2637. this is called "capturing" in what follows, and the phrase "capturing
  2638. subpattern" is used for a fragment of a pattern that picks out a sub-
  2639. string. PCRE supports several other kinds of parenthesized subpattern
  2640. that do not cause substrings to be captured.
  2641. Captured substrings are returned to the caller via a vector of integers
  2642. whose address is passed in ovector. The number of elements in the vec-
  2643. tor is passed in ovecsize, which must be a non-negative number. Note:
  2644. this argument is NOT the size of ovector in bytes.
  2645. The first two-thirds of the vector is used to pass back captured sub-
  2646. strings, each substring using a pair of integers. The remaining third
  2647. of the vector is used as workspace by pcre_exec() while matching cap-
  2648. turing subpatterns, and is not available for passing back information.
  2649. The number passed in ovecsize should always be a multiple of three. If
  2650. it is not, it is rounded down.
  2651. When a match is successful, information about captured substrings is
  2652. returned in pairs of integers, starting at the beginning of ovector,
  2653. and continuing up to two-thirds of its length at the most. The first
  2654. element of each pair is set to the offset of the first character in a
  2655. substring, and the second is set to the offset of the first character
  2656. after the end of a substring. These values are always data unit off-
  2657. sets, even in UTF mode. They are byte offsets in the 8-bit library,
  2658. 16-bit data item offsets in the 16-bit library, and 32-bit data item
  2659. offsets in the 32-bit library. Note: they are not character counts.
  2660. The first pair of integers, ovector[0] and ovector[1], identify the
  2661. portion of the subject string matched by the entire pattern. The next
  2662. pair is used for the first capturing subpattern, and so on. The value
  2663. returned by pcre_exec() is one more than the highest numbered pair that
  2664. has been set. For example, if two substrings have been captured, the
  2665. returned value is 3. If there are no capturing subpatterns, the return
  2666. value from a successful match is 1, indicating that just the first pair
  2667. of offsets has been set.
  2668. If a capturing subpattern is matched repeatedly, it is the last portion
  2669. of the string that it matched that is returned.
  2670. If the vector is too small to hold all the captured substring offsets,
  2671. it is used as far as possible (up to two-thirds of its length), and the
  2672. function returns a value of zero. If neither the actual string matched
  2673. nor any captured substrings are of interest, pcre_exec() may be called
  2674. with ovector passed as NULL and ovecsize as zero. However, if the pat-
  2675. tern contains back references and the ovector is not big enough to
  2676. remember the related substrings, PCRE has to get additional memory for
  2677. use during matching. Thus it is usually advisable to supply an ovector
  2678. of reasonable size.
  2679. There are some cases where zero is returned (indicating vector over-
  2680. flow) when in fact the vector is exactly the right size for the final
  2681. match. For example, consider the pattern
  2682. (a)(?:(b)c|bd)
  2683. If a vector of 6 elements (allowing for only 1 captured substring) is
  2684. given with subject string "abd", pcre_exec() will try to set the second
  2685. captured string, thereby recording a vector overflow, before failing to
  2686. match "c" and backing up to try the second alternative. The zero
  2687. return, however, does correctly indicate that the maximum number of
  2688. slots (namely 2) have been filled. In similar cases where there is tem-
  2689. porary overflow, but the final number of used slots is actually less
  2690. than the maximum, a non-zero value is returned.
  2691. The pcre_fullinfo() function can be used to find out how many capturing
  2692. subpatterns there are in a compiled pattern. The smallest size for
  2693. ovector that will allow for n captured substrings, in addition to the
  2694. offsets of the substring matched by the whole pattern, is (n+1)*3.
  2695. It is possible for capturing subpattern number n+1 to match some part
  2696. of the subject when subpattern n has not been used at all. For example,
  2697. if the string "abc" is matched against the pattern (a|(z))(bc) the
  2698. return from the function is 4, and subpatterns 1 and 3 are matched, but
  2699. 2 is not. When this happens, both values in the offset pairs corre-
  2700. sponding to unused subpatterns are set to -1.
  2701. Offset values that correspond to unused subpatterns at the end of the
  2702. expression are also set to -1. For example, if the string "abc" is
  2703. matched against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not
  2704. matched. The return from the function is 2, because the highest used
  2705. capturing subpattern number is 1, and the offsets for for the second
  2706. and third capturing subpatterns (assuming the vector is large enough,
  2707. of course) are set to -1.
  2708. Note: Elements in the first two-thirds of ovector that do not corre-
  2709. spond to capturing parentheses in the pattern are never changed. That
  2710. is, if a pattern contains n capturing parentheses, no more than ovec-
  2711. tor[0] to ovector[2n+1] are set by pcre_exec(). The other elements (in
  2712. the first two-thirds) retain whatever values they previously had.
  2713. Some convenience functions are provided for extracting the captured
  2714. substrings as separate strings. These are described below.
  2715. Error return values from pcre_exec()
  2716. If pcre_exec() fails, it returns a negative number. The following are
  2717. defined in the header file:
  2718. PCRE_ERROR_NOMATCH (-1)
  2719. The subject string did not match the pattern.
  2720. PCRE_ERROR_NULL (-2)
  2721. Either code or subject was passed as NULL, or ovector was NULL and
  2722. ovecsize was not zero.
  2723. PCRE_ERROR_BADOPTION (-3)
  2724. An unrecognized bit was set in the options argument.
  2725. PCRE_ERROR_BADMAGIC (-4)
  2726. PCRE stores a 4-byte "magic number" at the start of the compiled code,
  2727. to catch the case when it is passed a junk pointer and to detect when a
  2728. pattern that was compiled in an environment of one endianness is run in
  2729. an environment with the other endianness. This is the error that PCRE
  2730. gives when the magic number is not present.
  2731. PCRE_ERROR_UNKNOWN_OPCODE (-5)
  2732. While running the pattern match, an unknown item was encountered in the
  2733. compiled pattern. This error could be caused by a bug in PCRE or by
  2734. overwriting of the compiled pattern.
  2735. PCRE_ERROR_NOMEMORY (-6)
  2736. If a pattern contains back references, but the ovector that is passed
  2737. to pcre_exec() is not big enough to remember the referenced substrings,
  2738. PCRE gets a block of memory at the start of matching to use for this
  2739. purpose. If the call via pcre_malloc() fails, this error is given. The
  2740. memory is automatically freed at the end of matching.
  2741. This error is also given if pcre_stack_malloc() fails in pcre_exec().
  2742. This can happen only when PCRE has been compiled with --disable-stack-
  2743. for-recursion.
  2744. PCRE_ERROR_NOSUBSTRING (-7)
  2745. This error is used by the pcre_copy_substring(), pcre_get_substring(),
  2746. and pcre_get_substring_list() functions (see below). It is never
  2747. returned by pcre_exec().
  2748. PCRE_ERROR_MATCHLIMIT (-8)
  2749. The backtracking limit, as specified by the match_limit field in a
  2750. pcre_extra structure (or defaulted) was reached. See the description
  2751. above.
  2752. PCRE_ERROR_CALLOUT (-9)
  2753. This error is never generated by pcre_exec() itself. It is provided for
  2754. use by callout functions that want to yield a distinctive error code.
  2755. See the pcrecallout documentation for details.
  2756. PCRE_ERROR_BADUTF8 (-10)
  2757. A string that contains an invalid UTF-8 byte sequence was passed as a
  2758. subject, and the PCRE_NO_UTF8_CHECK option was not set. If the size of
  2759. the output vector (ovecsize) is at least 2, the byte offset to the
  2760. start of the the invalid UTF-8 character is placed in the first ele-
  2761. ment, and a reason code is placed in the second element. The reason
  2762. codes are listed in the following section. For backward compatibility,
  2763. if PCRE_PARTIAL_HARD is set and the problem is a truncated UTF-8 char-
  2764. acter at the end of the subject (reason codes 1 to 5),
  2765. PCRE_ERROR_SHORTUTF8 is returned instead of PCRE_ERROR_BADUTF8.
  2766. PCRE_ERROR_BADUTF8_OFFSET (-11)
  2767. The UTF-8 byte sequence that was passed as a subject was checked and
  2768. found to be valid (the PCRE_NO_UTF8_CHECK option was not set), but the
  2769. value of startoffset did not point to the beginning of a UTF-8 charac-
  2770. ter or the end of the subject.
  2771. PCRE_ERROR_PARTIAL (-12)
  2772. The subject string did not match, but it did match partially. See the
  2773. pcrepartial documentation for details of partial matching.
  2774. PCRE_ERROR_BADPARTIAL (-13)
  2775. This code is no longer in use. It was formerly returned when the
  2776. PCRE_PARTIAL option was used with a compiled pattern containing items
  2777. that were not supported for partial matching. From release 8.00
  2778. onwards, there are no restrictions on partial matching.
  2779. PCRE_ERROR_INTERNAL (-14)
  2780. An unexpected internal error has occurred. This error could be caused
  2781. by a bug in PCRE or by overwriting of the compiled pattern.
  2782. PCRE_ERROR_BADCOUNT (-15)
  2783. This error is given if the value of the ovecsize argument is negative.
  2784. PCRE_ERROR_RECURSIONLIMIT (-21)
  2785. The internal recursion limit, as specified by the match_limit_recursion
  2786. field in a pcre_extra structure (or defaulted) was reached. See the
  2787. description above.
  2788. PCRE_ERROR_BADNEWLINE (-23)
  2789. An invalid combination of PCRE_NEWLINE_xxx options was given.
  2790. PCRE_ERROR_BADOFFSET (-24)
  2791. The value of startoffset was negative or greater than the length of the
  2792. subject, that is, the value in length.
  2793. PCRE_ERROR_SHORTUTF8 (-25)
  2794. This error is returned instead of PCRE_ERROR_BADUTF8 when the subject
  2795. string ends with a truncated UTF-8 character and the PCRE_PARTIAL_HARD
  2796. option is set. Information about the failure is returned as for
  2797. PCRE_ERROR_BADUTF8. It is in fact sufficient to detect this case, but
  2798. this special error code for PCRE_PARTIAL_HARD precedes the implementa-
  2799. tion of returned information; it is retained for backwards compatibil-
  2800. ity.
  2801. PCRE_ERROR_RECURSELOOP (-26)
  2802. This error is returned when pcre_exec() detects a recursion loop within
  2803. the pattern. Specifically, it means that either the whole pattern or a
  2804. subpattern has been called recursively for the second time at the same
  2805. position in the subject string. Some simple patterns that might do this
  2806. are detected and faulted at compile time, but more complicated cases,
  2807. in particular mutual recursions between two different subpatterns, can-
  2808. not be detected until run time.
  2809. PCRE_ERROR_JIT_STACKLIMIT (-27)
  2810. This error is returned when a pattern that was successfully studied
  2811. using a JIT compile option is being matched, but the memory available
  2812. for the just-in-time processing stack is not large enough. See the
  2813. pcrejit documentation for more details.
  2814. PCRE_ERROR_BADMODE (-28)
  2815. This error is given if a pattern that was compiled by the 8-bit library
  2816. is passed to a 16-bit or 32-bit library function, or vice versa.
  2817. PCRE_ERROR_BADENDIANNESS (-29)
  2818. This error is given if a pattern that was compiled and saved is
  2819. reloaded on a host with different endianness. The utility function
  2820. pcre_pattern_to_host_byte_order() can be used to convert such a pattern
  2821. so that it runs on the new host.
  2822. PCRE_ERROR_JIT_BADOPTION
  2823. This error is returned when a pattern that was successfully studied
  2824. using a JIT compile option is being matched, but the matching mode
  2825. (partial or complete match) does not correspond to any JIT compilation
  2826. mode. When the JIT fast path function is used, this error may be also
  2827. given for invalid options. See the pcrejit documentation for more
  2828. details.
  2829. PCRE_ERROR_BADLENGTH (-32)
  2830. This error is given if pcre_exec() is called with a negative value for
  2831. the length argument.
  2832. Error numbers -16 to -20, -22, and 30 are not used by pcre_exec().
  2833. Reason codes for invalid UTF-8 strings
  2834. This section applies only to the 8-bit library. The corresponding
  2835. information for the 16-bit and 32-bit libraries is given in the pcre16
  2836. and pcre32 pages.
  2837. When pcre_exec() returns either PCRE_ERROR_BADUTF8 or PCRE_ERROR_SHORT-
  2838. UTF8, and the size of the output vector (ovecsize) is at least 2, the
  2839. offset of the start of the invalid UTF-8 character is placed in the
  2840. first output vector element (ovector[0]) and a reason code is placed in
  2841. the second element (ovector[1]). The reason codes are given names in
  2842. the pcre.h header file:
  2843. PCRE_UTF8_ERR1
  2844. PCRE_UTF8_ERR2
  2845. PCRE_UTF8_ERR3
  2846. PCRE_UTF8_ERR4
  2847. PCRE_UTF8_ERR5
  2848. The string ends with a truncated UTF-8 character; the code specifies
  2849. how many bytes are missing (1 to 5). Although RFC 3629 restricts UTF-8
  2850. characters to be no longer than 4 bytes, the encoding scheme (origi-
  2851. nally defined by RFC 2279) allows for up to 6 bytes, and this is
  2852. checked first; hence the possibility of 4 or 5 missing bytes.
  2853. PCRE_UTF8_ERR6
  2854. PCRE_UTF8_ERR7
  2855. PCRE_UTF8_ERR8
  2856. PCRE_UTF8_ERR9
  2857. PCRE_UTF8_ERR10
  2858. The two most significant bits of the 2nd, 3rd, 4th, 5th, or 6th byte of
  2859. the character do not have the binary value 0b10 (that is, either the
  2860. most significant bit is 0, or the next bit is 1).
  2861. PCRE_UTF8_ERR11
  2862. PCRE_UTF8_ERR12
  2863. A character that is valid by the RFC 2279 rules is either 5 or 6 bytes
  2864. long; these code points are excluded by RFC 3629.
  2865. PCRE_UTF8_ERR13
  2866. A 4-byte character has a value greater than 0x10fff; these code points
  2867. are excluded by RFC 3629.
  2868. PCRE_UTF8_ERR14
  2869. A 3-byte character has a value in the range 0xd800 to 0xdfff; this
  2870. range of code points are reserved by RFC 3629 for use with UTF-16, and
  2871. so are excluded from UTF-8.
  2872. PCRE_UTF8_ERR15
  2873. PCRE_UTF8_ERR16
  2874. PCRE_UTF8_ERR17
  2875. PCRE_UTF8_ERR18
  2876. PCRE_UTF8_ERR19
  2877. A 2-, 3-, 4-, 5-, or 6-byte character is "overlong", that is, it codes
  2878. for a value that can be represented by fewer bytes, which is invalid.
  2879. For example, the two bytes 0xc0, 0xae give the value 0x2e, whose cor-
  2880. rect coding uses just one byte.
  2881. PCRE_UTF8_ERR20
  2882. The two most significant bits of the first byte of a character have the
  2883. binary value 0b10 (that is, the most significant bit is 1 and the sec-
  2884. ond is 0). Such a byte can only validly occur as the second or subse-
  2885. quent byte of a multi-byte character.
  2886. PCRE_UTF8_ERR21
  2887. The first byte of a character has the value 0xfe or 0xff. These values
  2888. can never occur in a valid UTF-8 string.
  2889. PCRE_UTF8_ERR22
  2890. This error code was formerly used when the presence of a so-called
  2891. "non-character" caused an error. Unicode corrigendum #9 makes it clear
  2892. that such characters should not cause a string to be rejected, and so
  2893. this code is no longer in use and is never returned.
  2894. EXTRACTING CAPTURED SUBSTRINGS BY NUMBER
  2895. int pcre_copy_substring(const char *subject, int *ovector,
  2896. int stringcount, int stringnumber, char *buffer,
  2897. int buffersize);
  2898. int pcre_get_substring(const char *subject, int *ovector,
  2899. int stringcount, int stringnumber,
  2900. const char **stringptr);
  2901. int pcre_get_substring_list(const char *subject,
  2902. int *ovector, int stringcount, const char ***listptr);
  2903. Captured substrings can be accessed directly by using the offsets
  2904. returned by pcre_exec() in ovector. For convenience, the functions
  2905. pcre_copy_substring(), pcre_get_substring(), and pcre_get_sub-
  2906. string_list() are provided for extracting captured substrings as new,
  2907. separate, zero-terminated strings. These functions identify substrings
  2908. by number. The next section describes functions for extracting named
  2909. substrings.
  2910. A substring that contains a binary zero is correctly extracted and has
  2911. a further zero added on the end, but the result is not, of course, a C
  2912. string. However, you can process such a string by referring to the
  2913. length that is returned by pcre_copy_substring() and pcre_get_sub-
  2914. string(). Unfortunately, the interface to pcre_get_substring_list() is
  2915. not adequate for handling strings containing binary zeros, because the
  2916. end of the final string is not independently indicated.
  2917. The first three arguments are the same for all three of these func-
  2918. tions: subject is the subject string that has just been successfully
  2919. matched, ovector is a pointer to the vector of integer offsets that was
  2920. passed to pcre_exec(), and stringcount is the number of substrings that
  2921. were captured by the match, including the substring that matched the
  2922. entire regular expression. This is the value returned by pcre_exec() if
  2923. it is greater than zero. If pcre_exec() returned zero, indicating that
  2924. it ran out of space in ovector, the value passed as stringcount should
  2925. be the number of elements in the vector divided by three.
  2926. The functions pcre_copy_substring() and pcre_get_substring() extract a
  2927. single substring, whose number is given as stringnumber. A value of
  2928. zero extracts the substring that matched the entire pattern, whereas
  2929. higher values extract the captured substrings. For pcre_copy_sub-
  2930. string(), the string is placed in buffer, whose length is given by
  2931. buffersize, while for pcre_get_substring() a new block of memory is
  2932. obtained via pcre_malloc, and its address is returned via stringptr.
  2933. The yield of the function is the length of the string, not including
  2934. the terminating zero, or one of these error codes:
  2935. PCRE_ERROR_NOMEMORY (-6)
  2936. The buffer was too small for pcre_copy_substring(), or the attempt to
  2937. get memory failed for pcre_get_substring().
  2938. PCRE_ERROR_NOSUBSTRING (-7)
  2939. There is no substring whose number is stringnumber.
  2940. The pcre_get_substring_list() function extracts all available sub-
  2941. strings and builds a list of pointers to them. All this is done in a
  2942. single block of memory that is obtained via pcre_malloc. The address of
  2943. the memory block is returned via listptr, which is also the start of
  2944. the list of string pointers. The end of the list is marked by a NULL
  2945. pointer. The yield of the function is zero if all went well, or the
  2946. error code
  2947. PCRE_ERROR_NOMEMORY (-6)
  2948. if the attempt to get the memory block failed.
  2949. When any of these functions encounter a substring that is unset, which
  2950. can happen when capturing subpattern number n+1 matches some part of
  2951. the subject, but subpattern n has not been used at all, they return an
  2952. empty string. This can be distinguished from a genuine zero-length sub-
  2953. string by inspecting the appropriate offset in ovector, which is nega-
  2954. tive for unset substrings.
  2955. The two convenience functions pcre_free_substring() and pcre_free_sub-
  2956. string_list() can be used to free the memory returned by a previous
  2957. call of pcre_get_substring() or pcre_get_substring_list(), respec-
  2958. tively. They do nothing more than call the function pointed to by
  2959. pcre_free, which of course could be called directly from a C program.
  2960. However, PCRE is used in some situations where it is linked via a spe-
  2961. cial interface to another programming language that cannot use
  2962. pcre_free directly; it is for these cases that the functions are pro-
  2963. vided.
  2964. EXTRACTING CAPTURED SUBSTRINGS BY NAME
  2965. int pcre_get_stringnumber(const pcre *code,
  2966. const char *name);
  2967. int pcre_copy_named_substring(const pcre *code,
  2968. const char *subject, int *ovector,
  2969. int stringcount, const char *stringname,
  2970. char *buffer, int buffersize);
  2971. int pcre_get_named_substring(const pcre *code,
  2972. const char *subject, int *ovector,
  2973. int stringcount, const char *stringname,
  2974. const char **stringptr);
  2975. To extract a substring by name, you first have to find associated num-
  2976. ber. For example, for this pattern
  2977. (a+)b(?<xxx>\d+)...
  2978. the number of the subpattern called "xxx" is 2. If the name is known to
  2979. be unique (PCRE_DUPNAMES was not set), you can find the number from the
  2980. name by calling pcre_get_stringnumber(). The first argument is the com-
  2981. piled pattern, and the second is the name. The yield of the function is
  2982. the subpattern number, or PCRE_ERROR_NOSUBSTRING (-7) if there is no
  2983. subpattern of that name.
  2984. Given the number, you can extract the substring directly, or use one of
  2985. the functions described in the previous section. For convenience, there
  2986. are also two functions that do the whole job.
  2987. Most of the arguments of pcre_copy_named_substring() and
  2988. pcre_get_named_substring() are the same as those for the similarly
  2989. named functions that extract by number. As these are described in the
  2990. previous section, they are not re-described here. There are just two
  2991. differences:
  2992. First, instead of a substring number, a substring name is given. Sec-
  2993. ond, there is an extra argument, given at the start, which is a pointer
  2994. to the compiled pattern. This is needed in order to gain access to the
  2995. name-to-number translation table.
  2996. These functions call pcre_get_stringnumber(), and if it succeeds, they
  2997. then call pcre_copy_substring() or pcre_get_substring(), as appropri-
  2998. ate. NOTE: If PCRE_DUPNAMES is set and there are duplicate names, the
  2999. behaviour may not be what you want (see the next section).
  3000. Warning: If the pattern uses the (?| feature to set up multiple subpat-
  3001. terns with the same number, as described in the section on duplicate
  3002. subpattern numbers in the pcrepattern page, you cannot use names to
  3003. distinguish the different subpatterns, because names are not included
  3004. in the compiled code. The matching process uses only numbers. For this
  3005. reason, the use of different names for subpatterns of the same number
  3006. causes an error at compile time.
  3007. DUPLICATE SUBPATTERN NAMES
  3008. int pcre_get_stringtable_entries(const pcre *code,
  3009. const char *name, char **first, char **last);
  3010. When a pattern is compiled with the PCRE_DUPNAMES option, names for
  3011. subpatterns are not required to be unique. (Duplicate names are always
  3012. allowed for subpatterns with the same number, created by using the (?|
  3013. feature. Indeed, if such subpatterns are named, they are required to
  3014. use the same names.)
  3015. Normally, patterns with duplicate names are such that in any one match,
  3016. only one of the named subpatterns participates. An example is shown in
  3017. the pcrepattern documentation.
  3018. When duplicates are present, pcre_copy_named_substring() and
  3019. pcre_get_named_substring() return the first substring corresponding to
  3020. the given name that is set. If none are set, PCRE_ERROR_NOSUBSTRING
  3021. (-7) is returned; no data is returned. The pcre_get_stringnumber()
  3022. function returns one of the numbers that are associated with the name,
  3023. but it is not defined which it is.
  3024. If you want to get full details of all captured substrings for a given
  3025. name, you must use the pcre_get_stringtable_entries() function. The
  3026. first argument is the compiled pattern, and the second is the name. The
  3027. third and fourth are pointers to variables which are updated by the
  3028. function. After it has run, they point to the first and last entries in
  3029. the name-to-number table for the given name. The function itself
  3030. returns the length of each entry, or PCRE_ERROR_NOSUBSTRING (-7) if
  3031. there are none. The format of the table is described above in the sec-
  3032. tion entitled Information about a pattern above. Given all the rele-
  3033. vant entries for the name, you can extract each of their numbers, and
  3034. hence the captured data, if any.
  3035. FINDING ALL POSSIBLE MATCHES
  3036. The traditional matching function uses a similar algorithm to Perl,
  3037. which stops when it finds the first match, starting at a given point in
  3038. the subject. If you want to find all possible matches, or the longest
  3039. possible match, consider using the alternative matching function (see
  3040. below) instead. If you cannot use the alternative function, but still
  3041. need to find all possible matches, you can kludge it up by making use
  3042. of the callout facility, which is described in the pcrecallout documen-
  3043. tation.
  3044. What you have to do is to insert a callout right at the end of the pat-
  3045. tern. When your callout function is called, extract and save the cur-
  3046. rent matched substring. Then return 1, which forces pcre_exec() to
  3047. backtrack and try other alternatives. Ultimately, when it runs out of
  3048. matches, pcre_exec() will yield PCRE_ERROR_NOMATCH.
  3049. OBTAINING AN ESTIMATE OF STACK USAGE
  3050. Matching certain patterns using pcre_exec() can use a lot of process
  3051. stack, which in certain environments can be rather limited in size.
  3052. Some users find it helpful to have an estimate of the amount of stack
  3053. that is used by pcre_exec(), to help them set recursion limits, as
  3054. described in the pcrestack documentation. The estimate that is output
  3055. by pcretest when called with the -m and -C options is obtained by call-
  3056. ing pcre_exec with the values NULL, NULL, NULL, -999, and -999 for its
  3057. first five arguments.
  3058. Normally, if its first argument is NULL, pcre_exec() immediately
  3059. returns the negative error code PCRE_ERROR_NULL, but with this special
  3060. combination of arguments, it returns instead a negative number whose
  3061. absolute value is the approximate stack frame size in bytes. (A nega-
  3062. tive number is used so that it is clear that no match has happened.)
  3063. The value is approximate because in some cases, recursive calls to
  3064. pcre_exec() occur when there are one or two additional variables on the
  3065. stack.
  3066. If PCRE has been compiled to use the heap instead of the stack for
  3067. recursion, the value returned is the size of each block that is
  3068. obtained from the heap.
  3069. MATCHING A PATTERN: THE ALTERNATIVE FUNCTION
  3070. int pcre_dfa_exec(const pcre *code, const pcre_extra *extra,
  3071. const char *subject, int length, int startoffset,
  3072. int options, int *ovector, int ovecsize,
  3073. int *workspace, int wscount);
  3074. The function pcre_dfa_exec() is called to match a subject string
  3075. against a compiled pattern, using a matching algorithm that scans the
  3076. subject string just once, and does not backtrack. This has different
  3077. characteristics to the normal algorithm, and is not compatible with
  3078. Perl. Some of the features of PCRE patterns are not supported. Never-
  3079. theless, there are times when this kind of matching can be useful. For
  3080. a discussion of the two matching algorithms, and a list of features
  3081. that pcre_dfa_exec() does not support, see the pcrematching documenta-
  3082. tion.
  3083. The arguments for the pcre_dfa_exec() function are the same as for
  3084. pcre_exec(), plus two extras. The ovector argument is used in a differ-
  3085. ent way, and this is described below. The other common arguments are
  3086. used in the same way as for pcre_exec(), so their description is not
  3087. repeated here.
  3088. The two additional arguments provide workspace for the function. The
  3089. workspace vector should contain at least 20 elements. It is used for
  3090. keeping track of multiple paths through the pattern tree. More
  3091. workspace will be needed for patterns and subjects where there are a
  3092. lot of potential matches.
  3093. Here is an example of a simple call to pcre_dfa_exec():
  3094. int rc;
  3095. int ovector[10];
  3096. int wspace[20];
  3097. rc = pcre_dfa_exec(
  3098. re, /* result of pcre_compile() */
  3099. NULL, /* we didn't study the pattern */
  3100. "some string", /* the subject string */
  3101. 11, /* the length of the subject string */
  3102. 0, /* start at offset 0 in the subject */
  3103. 0, /* default options */
  3104. ovector, /* vector of integers for substring information */
  3105. 10, /* number of elements (NOT size in bytes) */
  3106. wspace, /* working space vector */
  3107. 20); /* number of elements (NOT size in bytes) */
  3108. Option bits for pcre_dfa_exec()
  3109. The unused bits of the options argument for pcre_dfa_exec() must be
  3110. zero. The only bits that may be set are PCRE_ANCHORED, PCRE_NEW-
  3111. LINE_xxx, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY,
  3112. PCRE_NOTEMPTY_ATSTART, PCRE_NO_UTF8_CHECK, PCRE_BSR_ANYCRLF,
  3113. PCRE_BSR_UNICODE, PCRE_NO_START_OPTIMIZE, PCRE_PARTIAL_HARD, PCRE_PAR-
  3114. TIAL_SOFT, PCRE_DFA_SHORTEST, and PCRE_DFA_RESTART. All but the last
  3115. four of these are exactly the same as for pcre_exec(), so their
  3116. description is not repeated here.
  3117. PCRE_PARTIAL_HARD
  3118. PCRE_PARTIAL_SOFT
  3119. These have the same general effect as they do for pcre_exec(), but the
  3120. details are slightly different. When PCRE_PARTIAL_HARD is set for
  3121. pcre_dfa_exec(), it returns PCRE_ERROR_PARTIAL if the end of the sub-
  3122. ject is reached and there is still at least one matching possibility
  3123. that requires additional characters. This happens even if some complete
  3124. matches have also been found. When PCRE_PARTIAL_SOFT is set, the return
  3125. code PCRE_ERROR_NOMATCH is converted into PCRE_ERROR_PARTIAL if the end
  3126. of the subject is reached, there have been no complete matches, but
  3127. there is still at least one matching possibility. The portion of the
  3128. string that was inspected when the longest partial match was found is
  3129. set as the first matching string in both cases. There is a more
  3130. detailed discussion of partial and multi-segment matching, with exam-
  3131. ples, in the pcrepartial documentation.
  3132. PCRE_DFA_SHORTEST
  3133. Setting the PCRE_DFA_SHORTEST option causes the matching algorithm to
  3134. stop as soon as it has found one match. Because of the way the alterna-
  3135. tive algorithm works, this is necessarily the shortest possible match
  3136. at the first possible matching point in the subject string.
  3137. PCRE_DFA_RESTART
  3138. When pcre_dfa_exec() returns a partial match, it is possible to call it
  3139. again, with additional subject characters, and have it continue with
  3140. the same match. The PCRE_DFA_RESTART option requests this action; when
  3141. it is set, the workspace and wscount options must reference the same
  3142. vector as before because data about the match so far is left in them
  3143. after a partial match. There is more discussion of this facility in the
  3144. pcrepartial documentation.
  3145. Successful returns from pcre_dfa_exec()
  3146. When pcre_dfa_exec() succeeds, it may have matched more than one sub-
  3147. string in the subject. Note, however, that all the matches from one run
  3148. of the function start at the same point in the subject. The shorter
  3149. matches are all initial substrings of the longer matches. For example,
  3150. if the pattern
  3151. <.*>
  3152. is matched against the string
  3153. This is <something> <something else> <something further> no more
  3154. the three matched strings are
  3155. <something>
  3156. <something> <something else>
  3157. <something> <something else> <something further>
  3158. On success, the yield of the function is a number greater than zero,
  3159. which is the number of matched substrings. The substrings themselves
  3160. are returned in ovector. Each string uses two elements; the first is
  3161. the offset to the start, and the second is the offset to the end. In
  3162. fact, all the strings have the same start offset. (Space could have
  3163. been saved by giving this only once, but it was decided to retain some
  3164. compatibility with the way pcre_exec() returns data, even though the
  3165. meaning of the strings is different.)
  3166. The strings are returned in reverse order of length; that is, the long-
  3167. est matching string is given first. If there were too many matches to
  3168. fit into ovector, the yield of the function is zero, and the vector is
  3169. filled with the longest matches. Unlike pcre_exec(), pcre_dfa_exec()
  3170. can use the entire ovector for returning matched strings.
  3171. NOTE: PCRE's "auto-possessification" optimization usually applies to
  3172. character repeats at the end of a pattern (as well as internally). For
  3173. example, the pattern "a\d+" is compiled as if it were "a\d++" because
  3174. there is no point even considering the possibility of backtracking into
  3175. the repeated digits. For DFA matching, this means that only one possi-
  3176. ble match is found. If you really do want multiple matches in such
  3177. cases, either use an ungreedy repeat ("a\d+?") or set the
  3178. PCRE_NO_AUTO_POSSESS option when compiling.
  3179. Error returns from pcre_dfa_exec()
  3180. The pcre_dfa_exec() function returns a negative number when it fails.
  3181. Many of the errors are the same as for pcre_exec(), and these are
  3182. described above. There are in addition the following errors that are
  3183. specific to pcre_dfa_exec():
  3184. PCRE_ERROR_DFA_UITEM (-16)
  3185. This return is given if pcre_dfa_exec() encounters an item in the pat-
  3186. tern that it does not support, for instance, the use of \C or a back
  3187. reference.
  3188. PCRE_ERROR_DFA_UCOND (-17)
  3189. This return is given if pcre_dfa_exec() encounters a condition item
  3190. that uses a back reference for the condition, or a test for recursion
  3191. in a specific group. These are not supported.
  3192. PCRE_ERROR_DFA_UMLIMIT (-18)
  3193. This return is given if pcre_dfa_exec() is called with an extra block
  3194. that contains a setting of the match_limit or match_limit_recursion
  3195. fields. This is not supported (these fields are meaningless for DFA
  3196. matching).
  3197. PCRE_ERROR_DFA_WSSIZE (-19)
  3198. This return is given if pcre_dfa_exec() runs out of space in the
  3199. workspace vector.
  3200. PCRE_ERROR_DFA_RECURSE (-20)
  3201. When a recursive subpattern is processed, the matching function calls
  3202. itself recursively, using private vectors for ovector and workspace.
  3203. This error is given if the output vector is not large enough. This
  3204. should be extremely rare, as a vector of size 1000 is used.
  3205. PCRE_ERROR_DFA_BADRESTART (-30)
  3206. When pcre_dfa_exec() is called with the PCRE_DFA_RESTART option, some
  3207. plausibility checks are made on the contents of the workspace, which
  3208. should contain data about the previous partial match. If any of these
  3209. checks fail, this error is given.
  3210. SEE ALSO
  3211. pcre16(3), pcre32(3), pcrebuild(3), pcrecallout(3), pcrecpp(3)(3),
  3212. pcrematching(3), pcrepartial(3), pcreposix(3), pcreprecompile(3), pcre-
  3213. sample(3), pcrestack(3).
  3214. AUTHOR
  3215. Philip Hazel
  3216. University Computing Service
  3217. Cambridge CB2 3QH, England.
  3218. REVISION
  3219. Last updated: 18 December 2015
  3220. Copyright (c) 1997-2015 University of Cambridge.
  3221. ------------------------------------------------------------------------------
  3222. PCRECALLOUT(3) Library Functions Manual PCRECALLOUT(3)
  3223. NAME
  3224. PCRE - Perl-compatible regular expressions
  3225. SYNOPSIS
  3226. #include <pcre.h>
  3227. int (*pcre_callout)(pcre_callout_block *);
  3228. int (*pcre16_callout)(pcre16_callout_block *);
  3229. int (*pcre32_callout)(pcre32_callout_block *);
  3230. DESCRIPTION
  3231. PCRE provides a feature called "callout", which is a means of temporar-
  3232. ily passing control to the caller of PCRE in the middle of pattern
  3233. matching. The caller of PCRE provides an external function by putting
  3234. its entry point in the global variable pcre_callout (pcre16_callout for
  3235. the 16-bit library, pcre32_callout for the 32-bit library). By default,
  3236. this variable contains NULL, which disables all calling out.
  3237. Within a regular expression, (?C) indicates the points at which the
  3238. external function is to be called. Different callout points can be
  3239. identified by putting a number less than 256 after the letter C. The
  3240. default value is zero. For example, this pattern has two callout
  3241. points:
  3242. (?C1)abc(?C2)def
  3243. If the PCRE_AUTO_CALLOUT option bit is set when a pattern is compiled,
  3244. PCRE automatically inserts callouts, all with number 255, before each
  3245. item in the pattern. For example, if PCRE_AUTO_CALLOUT is used with the
  3246. pattern
  3247. A(\d{2}|--)
  3248. it is processed as if it were
  3249. (?C255)A(?C255)((?C255)\d{2}(?C255)|(?C255)-(?C255)-(?C255))(?C255)
  3250. Notice that there is a callout before and after each parenthesis and
  3251. alternation bar. If the pattern contains a conditional group whose con-
  3252. dition is an assertion, an automatic callout is inserted immediately
  3253. before the condition. Such a callout may also be inserted explicitly,
  3254. for example:
  3255. (?(?C9)(?=a)ab|de)
  3256. This applies only to assertion conditions (because they are themselves
  3257. independent groups).
  3258. Automatic callouts can be used for tracking the progress of pattern
  3259. matching. The pcretest program has a pattern qualifier (/C) that sets
  3260. automatic callouts; when it is used, the output indicates how the pat-
  3261. tern is being matched. This is useful information when you are trying
  3262. to optimize the performance of a particular pattern.
  3263. MISSING CALLOUTS
  3264. You should be aware that, because of optimizations in the way PCRE com-
  3265. piles and matches patterns, callouts sometimes do not happen exactly as
  3266. you might expect.
  3267. At compile time, PCRE "auto-possessifies" repeated items when it knows
  3268. that what follows cannot be part of the repeat. For example, a+[bc] is
  3269. compiled as if it were a++[bc]. The pcretest output when this pattern
  3270. is anchored and then applied with automatic callouts to the string
  3271. "aaaa" is:
  3272. --->aaaa
  3273. +0 ^ ^
  3274. +1 ^ a+
  3275. +3 ^ ^ [bc]
  3276. No match
  3277. This indicates that when matching [bc] fails, there is no backtracking
  3278. into a+ and therefore the callouts that would be taken for the back-
  3279. tracks do not occur. You can disable the auto-possessify feature by
  3280. passing PCRE_NO_AUTO_POSSESS to pcre_compile(), or starting the pattern
  3281. with (*NO_AUTO_POSSESS). If this is done in pcretest (using the /O
  3282. qualifier), the output changes to this:
  3283. --->aaaa
  3284. +0 ^ ^
  3285. +1 ^ a+
  3286. +3 ^ ^ [bc]
  3287. +3 ^ ^ [bc]
  3288. +3 ^ ^ [bc]
  3289. +3 ^^ [bc]
  3290. No match
  3291. This time, when matching [bc] fails, the matcher backtracks into a+ and
  3292. tries again, repeatedly, until a+ itself fails.
  3293. Other optimizations that provide fast "no match" results also affect
  3294. callouts. For example, if the pattern is
  3295. ab(?C4)cd
  3296. PCRE knows that any matching string must contain the letter "d". If the
  3297. subject string is "abyz", the lack of "d" means that matching doesn't
  3298. ever start, and the callout is never reached. However, with "abyd",
  3299. though the result is still no match, the callout is obeyed.
  3300. If the pattern is studied, PCRE knows the minimum length of a matching
  3301. string, and will immediately give a "no match" return without actually
  3302. running a match if the subject is not long enough, or, for unanchored
  3303. patterns, if it has been scanned far enough.
  3304. You can disable these optimizations by passing the PCRE_NO_START_OPTI-
  3305. MIZE option to the matching function, or by starting the pattern with
  3306. (*NO_START_OPT). This slows down the matching process, but does ensure
  3307. that callouts such as the example above are obeyed.
  3308. THE CALLOUT INTERFACE
  3309. During matching, when PCRE reaches a callout point, the external func-
  3310. tion defined by pcre_callout or pcre[16|32]_callout is called (if it is
  3311. set). This applies to both normal and DFA matching. The only argument
  3312. to the callout function is a pointer to a pcre_callout or
  3313. pcre[16|32]_callout block. These structures contains the following
  3314. fields:
  3315. int version;
  3316. int callout_number;
  3317. int *offset_vector;
  3318. const char *subject; (8-bit version)
  3319. PCRE_SPTR16 subject; (16-bit version)
  3320. PCRE_SPTR32 subject; (32-bit version)
  3321. int subject_length;
  3322. int start_match;
  3323. int current_position;
  3324. int capture_top;
  3325. int capture_last;
  3326. void *callout_data;
  3327. int pattern_position;
  3328. int next_item_length;
  3329. const unsigned char *mark; (8-bit version)
  3330. const PCRE_UCHAR16 *mark; (16-bit version)
  3331. const PCRE_UCHAR32 *mark; (32-bit version)
  3332. The version field is an integer containing the version number of the
  3333. block format. The initial version was 0; the current version is 2. The
  3334. version number will change again in future if additional fields are
  3335. added, but the intention is never to remove any of the existing fields.
  3336. The callout_number field contains the number of the callout, as com-
  3337. piled into the pattern (that is, the number after ?C for manual call-
  3338. outs, and 255 for automatically generated callouts).
  3339. The offset_vector field is a pointer to the vector of offsets that was
  3340. passed by the caller to the matching function. When pcre_exec() or
  3341. pcre[16|32]_exec() is used, the contents can be inspected, in order to
  3342. extract substrings that have been matched so far, in the same way as
  3343. for extracting substrings after a match has completed. For the DFA
  3344. matching functions, this field is not useful.
  3345. The subject and subject_length fields contain copies of the values that
  3346. were passed to the matching function.
  3347. The start_match field normally contains the offset within the subject
  3348. at which the current match attempt started. However, if the escape
  3349. sequence \K has been encountered, this value is changed to reflect the
  3350. modified starting point. If the pattern is not anchored, the callout
  3351. function may be called several times from the same point in the pattern
  3352. for different starting points in the subject.
  3353. The current_position field contains the offset within the subject of
  3354. the current match pointer.
  3355. When the pcre_exec() or pcre[16|32]_exec() is used, the capture_top
  3356. field contains one more than the number of the highest numbered cap-
  3357. tured substring so far. If no substrings have been captured, the value
  3358. of capture_top is one. This is always the case when the DFA functions
  3359. are used, because they do not support captured substrings.
  3360. The capture_last field contains the number of the most recently cap-
  3361. tured substring. However, when a recursion exits, the value reverts to
  3362. what it was outside the recursion, as do the values of all captured
  3363. substrings. If no substrings have been captured, the value of cap-
  3364. ture_last is -1. This is always the case for the DFA matching func-
  3365. tions.
  3366. The callout_data field contains a value that is passed to a matching
  3367. function specifically so that it can be passed back in callouts. It is
  3368. passed in the callout_data field of a pcre_extra or pcre[16|32]_extra
  3369. data structure. If no such data was passed, the value of callout_data
  3370. in a callout block is NULL. There is a description of the pcre_extra
  3371. structure in the pcreapi documentation.
  3372. The pattern_position field is present from version 1 of the callout
  3373. structure. It contains the offset to the next item to be matched in the
  3374. pattern string.
  3375. The next_item_length field is present from version 1 of the callout
  3376. structure. It contains the length of the next item to be matched in the
  3377. pattern string. When the callout immediately precedes an alternation
  3378. bar, a closing parenthesis, or the end of the pattern, the length is
  3379. zero. When the callout precedes an opening parenthesis, the length is
  3380. that of the entire subpattern.
  3381. The pattern_position and next_item_length fields are intended to help
  3382. in distinguishing between different automatic callouts, which all have
  3383. the same callout number. However, they are set for all callouts.
  3384. The mark field is present from version 2 of the callout structure. In
  3385. callouts from pcre_exec() or pcre[16|32]_exec() it contains a pointer
  3386. to the zero-terminated name of the most recently passed (*MARK),
  3387. (*PRUNE), or (*THEN) item in the match, or NULL if no such items have
  3388. been passed. Instances of (*PRUNE) or (*THEN) without a name do not
  3389. obliterate a previous (*MARK). In callouts from the DFA matching func-
  3390. tions this field always contains NULL.
  3391. RETURN VALUES
  3392. The external callout function returns an integer to PCRE. If the value
  3393. is zero, matching proceeds as normal. If the value is greater than
  3394. zero, matching fails at the current point, but the testing of other
  3395. matching possibilities goes ahead, just as if a lookahead assertion had
  3396. failed. If the value is less than zero, the match is abandoned, the
  3397. matching function returns the negative value.
  3398. Negative values should normally be chosen from the set of
  3399. PCRE_ERROR_xxx values. In particular, PCRE_ERROR_NOMATCH forces a stan-
  3400. dard "no match" failure. The error number PCRE_ERROR_CALLOUT is
  3401. reserved for use by callout functions; it will never be used by PCRE
  3402. itself.
  3403. AUTHOR
  3404. Philip Hazel
  3405. University Computing Service
  3406. Cambridge CB2 3QH, England.
  3407. REVISION
  3408. Last updated: 12 November 2013
  3409. Copyright (c) 1997-2013 University of Cambridge.
  3410. ------------------------------------------------------------------------------
  3411. PCRECOMPAT(3) Library Functions Manual PCRECOMPAT(3)
  3412. NAME
  3413. PCRE - Perl-compatible regular expressions
  3414. DIFFERENCES BETWEEN PCRE AND PERL
  3415. This document describes the differences in the ways that PCRE and Perl
  3416. handle regular expressions. The differences described here are with
  3417. respect to Perl versions 5.10 and above.
  3418. 1. PCRE has only a subset of Perl's Unicode support. Details of what it
  3419. does have are given in the pcreunicode page.
  3420. 2. PCRE allows repeat quantifiers only on parenthesized assertions, but
  3421. they do not mean what you might think. For example, (?!a){3} does not
  3422. assert that the next three characters are not "a". It just asserts that
  3423. the next character is not "a" three times (in principle: PCRE optimizes
  3424. this to run the assertion just once). Perl allows repeat quantifiers on
  3425. other assertions such as \b, but these do not seem to have any use.
  3426. 3. Capturing subpatterns that occur inside negative lookahead asser-
  3427. tions are counted, but their entries in the offsets vector are never
  3428. set. Perl sometimes (but not always) sets its numerical variables from
  3429. inside negative assertions.
  3430. 4. Though binary zero characters are supported in the subject string,
  3431. they are not allowed in a pattern string because it is passed as a nor-
  3432. mal C string, terminated by zero. The escape sequence \0 can be used in
  3433. the pattern to represent a binary zero.
  3434. 5. The following Perl escape sequences are not supported: \l, \u, \L,
  3435. \U, and \N when followed by a character name or Unicode value. (\N on
  3436. its own, matching a non-newline character, is supported.) In fact these
  3437. are implemented by Perl's general string-handling and are not part of
  3438. its pattern matching engine. If any of these are encountered by PCRE,
  3439. an error is generated by default. However, if the PCRE_JAVASCRIPT_COM-
  3440. PAT option is set, \U and \u are interpreted as JavaScript interprets
  3441. them.
  3442. 6. The Perl escape sequences \p, \P, and \X are supported only if PCRE
  3443. is built with Unicode character property support. The properties that
  3444. can be tested with \p and \P are limited to the general category prop-
  3445. erties such as Lu and Nd, script names such as Greek or Han, and the
  3446. derived properties Any and L&. PCRE does support the Cs (surrogate)
  3447. property, which Perl does not; the Perl documentation says "Because
  3448. Perl hides the need for the user to understand the internal representa-
  3449. tion of Unicode characters, there is no need to implement the somewhat
  3450. messy concept of surrogates."
  3451. 7. PCRE does support the \Q...\E escape for quoting substrings. Charac-
  3452. ters in between are treated as literals. This is slightly different
  3453. from Perl in that $ and @ are also handled as literals inside the
  3454. quotes. In Perl, they cause variable interpolation (but of course PCRE
  3455. does not have variables). Note the following examples:
  3456. Pattern PCRE matches Perl matches
  3457. \Qabc$xyz\E abc$xyz abc followed by the
  3458. contents of $xyz
  3459. \Qabc\$xyz\E abc\$xyz abc\$xyz
  3460. \Qabc\E\$\Qxyz\E abc$xyz abc$xyz
  3461. The \Q...\E sequence is recognized both inside and outside character
  3462. classes.
  3463. 8. Fairly obviously, PCRE does not support the (?{code}) and (??{code})
  3464. constructions. However, there is support for recursive patterns. This
  3465. is not available in Perl 5.8, but it is in Perl 5.10. Also, the PCRE
  3466. "callout" feature allows an external function to be called during pat-
  3467. tern matching. See the pcrecallout documentation for details.
  3468. 9. Subpatterns that are called as subroutines (whether or not recur-
  3469. sively) are always treated as atomic groups in PCRE. This is like
  3470. Python, but unlike Perl. Captured values that are set outside a sub-
  3471. routine call can be reference from inside in PCRE, but not in Perl.
  3472. There is a discussion that explains these differences in more detail in
  3473. the section on recursion differences from Perl in the pcrepattern page.
  3474. 10. If any of the backtracking control verbs are used in a subpattern
  3475. that is called as a subroutine (whether or not recursively), their
  3476. effect is confined to that subpattern; it does not extend to the sur-
  3477. rounding pattern. This is not always the case in Perl. In particular,
  3478. if (*THEN) is present in a group that is called as a subroutine, its
  3479. action is limited to that group, even if the group does not contain any
  3480. | characters. Note that such subpatterns are processed as anchored at
  3481. the point where they are tested.
  3482. 11. If a pattern contains more than one backtracking control verb, the
  3483. first one that is backtracked onto acts. For example, in the pattern
  3484. A(*COMMIT)B(*PRUNE)C a failure in B triggers (*COMMIT), but a failure
  3485. in C triggers (*PRUNE). Perl's behaviour is more complex; in many cases
  3486. it is the same as PCRE, but there are examples where it differs.
  3487. 12. Most backtracking verbs in assertions have their normal actions.
  3488. They are not confined to the assertion.
  3489. 13. There are some differences that are concerned with the settings of
  3490. captured strings when part of a pattern is repeated. For example,
  3491. matching "aba" against the pattern /^(a(b)?)+$/ in Perl leaves $2
  3492. unset, but in PCRE it is set to "b".
  3493. 14. PCRE's handling of duplicate subpattern numbers and duplicate sub-
  3494. pattern names is not as general as Perl's. This is a consequence of the
  3495. fact the PCRE works internally just with numbers, using an external ta-
  3496. ble to translate between numbers and names. In particular, a pattern
  3497. such as (?|(?<a>A)|(?<b>B), where the two capturing parentheses have
  3498. the same number but different names, is not supported, and causes an
  3499. error at compile time. If it were allowed, it would not be possible to
  3500. distinguish which parentheses matched, because both names map to cap-
  3501. turing subpattern number 1. To avoid this confusing situation, an error
  3502. is given at compile time.
  3503. 15. Perl recognizes comments in some places that PCRE does not, for
  3504. example, between the ( and ? at the start of a subpattern. If the /x
  3505. modifier is set, Perl allows white space between ( and ? (though cur-
  3506. rent Perls warn that this is deprecated) but PCRE never does, even if
  3507. the PCRE_EXTENDED option is set.
  3508. 16. Perl, when in warning mode, gives warnings for character classes
  3509. such as [A-\d] or [a-[:digit:]]. It then treats the hyphens as liter-
  3510. als. PCRE has no warning features, so it gives an error in these cases
  3511. because they are almost certainly user mistakes.
  3512. 17. In PCRE, the upper/lower case character properties Lu and Ll are
  3513. not affected when case-independent matching is specified. For example,
  3514. \p{Lu} always matches an upper case letter. I think Perl has changed in
  3515. this respect; in the release at the time of writing (5.16), \p{Lu} and
  3516. \p{Ll} match all letters, regardless of case, when case independence is
  3517. specified.
  3518. 18. PCRE provides some extensions to the Perl regular expression facil-
  3519. ities. Perl 5.10 includes new features that are not in earlier ver-
  3520. sions of Perl, some of which (such as named parentheses) have been in
  3521. PCRE for some time. This list is with respect to Perl 5.10:
  3522. (a) Although lookbehind assertions in PCRE must match fixed length
  3523. strings, each alternative branch of a lookbehind assertion can match a
  3524. different length of string. Perl requires them all to have the same
  3525. length.
  3526. (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $
  3527. meta-character matches only at the very end of the string.
  3528. (c) If PCRE_EXTRA is set, a backslash followed by a letter with no spe-
  3529. cial meaning is faulted. Otherwise, like Perl, the backslash is quietly
  3530. ignored. (Perl can be made to issue a warning.)
  3531. (d) If PCRE_UNGREEDY is set, the greediness of the repetition quanti-
  3532. fiers is inverted, that is, by default they are not greedy, but if fol-
  3533. lowed by a question mark they are.
  3534. (e) PCRE_ANCHORED can be used at matching time to force a pattern to be
  3535. tried only at the first matching position in the subject string.
  3536. (f) The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART,
  3537. and PCRE_NO_AUTO_CAPTURE options for pcre_exec() have no Perl equiva-
  3538. lents.
  3539. (g) The \R escape sequence can be restricted to match only CR, LF, or
  3540. CRLF by the PCRE_BSR_ANYCRLF option.
  3541. (h) The callout facility is PCRE-specific.
  3542. (i) The partial matching facility is PCRE-specific.
  3543. (j) Patterns compiled by PCRE can be saved and re-used at a later time,
  3544. even on different hosts that have the other endianness. However, this
  3545. does not apply to optimized data created by the just-in-time compiler.
  3546. (k) The alternative matching functions (pcre_dfa_exec(),
  3547. pcre16_dfa_exec() and pcre32_dfa_exec(),) match in a different way and
  3548. are not Perl-compatible.
  3549. (l) PCRE recognizes some special sequences such as (*CR) at the start
  3550. of a pattern that set overall options that cannot be changed within the
  3551. pattern.
  3552. AUTHOR
  3553. Philip Hazel
  3554. University Computing Service
  3555. Cambridge CB2 3QH, England.
  3556. REVISION
  3557. Last updated: 10 November 2013
  3558. Copyright (c) 1997-2013 University of Cambridge.
  3559. ------------------------------------------------------------------------------
  3560. PCREPATTERN(3) Library Functions Manual PCREPATTERN(3)
  3561. NAME
  3562. PCRE - Perl-compatible regular expressions
  3563. PCRE REGULAR EXPRESSION DETAILS
  3564. The syntax and semantics of the regular expressions that are supported
  3565. by PCRE are described in detail below. There is a quick-reference syn-
  3566. tax summary in the pcresyntax page. PCRE tries to match Perl syntax and
  3567. semantics as closely as it can. PCRE also supports some alternative
  3568. regular expression syntax (which does not conflict with the Perl syn-
  3569. tax) in order to provide some compatibility with regular expressions in
  3570. Python, .NET, and Oniguruma.
  3571. Perl's regular expressions are described in its own documentation, and
  3572. regular expressions in general are covered in a number of books, some
  3573. of which have copious examples. Jeffrey Friedl's "Mastering Regular
  3574. Expressions", published by O'Reilly, covers regular expressions in
  3575. great detail. This description of PCRE's regular expressions is
  3576. intended as reference material.
  3577. This document discusses the patterns that are supported by PCRE when
  3578. one its main matching functions, pcre_exec() (8-bit) or
  3579. pcre[16|32]_exec() (16- or 32-bit), is used. PCRE also has alternative
  3580. matching functions, pcre_dfa_exec() and pcre[16|32_dfa_exec(), which
  3581. match using a different algorithm that is not Perl-compatible. Some of
  3582. the features discussed below are not available when DFA matching is
  3583. used. The advantages and disadvantages of the alternative functions,
  3584. and how they differ from the normal functions, are discussed in the
  3585. pcrematching page.
  3586. SPECIAL START-OF-PATTERN ITEMS
  3587. A number of options that can be passed to pcre_compile() can also be
  3588. set by special items at the start of a pattern. These are not Perl-com-
  3589. patible, but are provided to make these options accessible to pattern
  3590. writers who are not able to change the program that processes the pat-
  3591. tern. Any number of these items may appear, but they must all be
  3592. together right at the start of the pattern string, and the letters must
  3593. be in upper case.
  3594. UTF support
  3595. The original operation of PCRE was on strings of one-byte characters.
  3596. However, there is now also support for UTF-8 strings in the original
  3597. library, an extra library that supports 16-bit and UTF-16 character
  3598. strings, and a third library that supports 32-bit and UTF-32 character
  3599. strings. To use these features, PCRE must be built to include appropri-
  3600. ate support. When using UTF strings you must either call the compiling
  3601. function with the PCRE_UTF8, PCRE_UTF16, or PCRE_UTF32 option, or the
  3602. pattern must start with one of these special sequences:
  3603. (*UTF8)
  3604. (*UTF16)
  3605. (*UTF32)
  3606. (*UTF)
  3607. (*UTF) is a generic sequence that can be used with any of the
  3608. libraries. Starting a pattern with such a sequence is equivalent to
  3609. setting the relevant option. How setting a UTF mode affects pattern
  3610. matching is mentioned in several places below. There is also a summary
  3611. of features in the pcreunicode page.
  3612. Some applications that allow their users to supply patterns may wish to
  3613. restrict them to non-UTF data for security reasons. If the
  3614. PCRE_NEVER_UTF option is set at compile time, (*UTF) etc. are not
  3615. allowed, and their appearance causes an error.
  3616. Unicode property support
  3617. Another special sequence that may appear at the start of a pattern is
  3618. (*UCP). This has the same effect as setting the PCRE_UCP option: it
  3619. causes sequences such as \d and \w to use Unicode properties to deter-
  3620. mine character types, instead of recognizing only characters with codes
  3621. less than 128 via a lookup table.
  3622. Disabling auto-possessification
  3623. If a pattern starts with (*NO_AUTO_POSSESS), it has the same effect as
  3624. setting the PCRE_NO_AUTO_POSSESS option at compile time. This stops
  3625. PCRE from making quantifiers possessive when what follows cannot match
  3626. the repeated item. For example, by default a+b is treated as a++b. For
  3627. more details, see the pcreapi documentation.
  3628. Disabling start-up optimizations
  3629. If a pattern starts with (*NO_START_OPT), it has the same effect as
  3630. setting the PCRE_NO_START_OPTIMIZE option either at compile or matching
  3631. time. This disables several optimizations for quickly reaching "no
  3632. match" results. For more details, see the pcreapi documentation.
  3633. Newline conventions
  3634. PCRE supports five different conventions for indicating line breaks in
  3635. strings: a single CR (carriage return) character, a single LF (line-
  3636. feed) character, the two-character sequence CRLF, any of the three pre-
  3637. ceding, or any Unicode newline sequence. The pcreapi page has further
  3638. discussion about newlines, and shows how to set the newline convention
  3639. in the options arguments for the compiling and matching functions.
  3640. It is also possible to specify a newline convention by starting a pat-
  3641. tern string with one of the following five sequences:
  3642. (*CR) carriage return
  3643. (*LF) linefeed
  3644. (*CRLF) carriage return, followed by linefeed
  3645. (*ANYCRLF) any of the three above
  3646. (*ANY) all Unicode newline sequences
  3647. These override the default and the options given to the compiling func-
  3648. tion. For example, on a Unix system where LF is the default newline
  3649. sequence, the pattern
  3650. (*CR)a.b
  3651. changes the convention to CR. That pattern matches "a\nb" because LF is
  3652. no longer a newline. If more than one of these settings is present, the
  3653. last one is used.
  3654. The newline convention affects where the circumflex and dollar asser-
  3655. tions are true. It also affects the interpretation of the dot metachar-
  3656. acter when PCRE_DOTALL is not set, and the behaviour of \N. However, it
  3657. does not affect what the \R escape sequence matches. By default, this
  3658. is any Unicode newline sequence, for Perl compatibility. However, this
  3659. can be changed; see the description of \R in the section entitled "New-
  3660. line sequences" below. A change of \R setting can be combined with a
  3661. change of newline convention.
  3662. Setting match and recursion limits
  3663. The caller of pcre_exec() can set a limit on the number of times the
  3664. internal match() function is called and on the maximum depth of recur-
  3665. sive calls. These facilities are provided to catch runaway matches that
  3666. are provoked by patterns with huge matching trees (a typical example is
  3667. a pattern with nested unlimited repeats) and to avoid running out of
  3668. system stack by too much recursion. When one of these limits is
  3669. reached, pcre_exec() gives an error return. The limits can also be set
  3670. by items at the start of the pattern of the form
  3671. (*LIMIT_MATCH=d)
  3672. (*LIMIT_RECURSION=d)
  3673. where d is any number of decimal digits. However, the value of the set-
  3674. ting must be less than the value set (or defaulted) by the caller of
  3675. pcre_exec() for it to have any effect. In other words, the pattern
  3676. writer can lower the limits set by the programmer, but not raise them.
  3677. If there is more than one setting of one of these limits, the lower
  3678. value is used.
  3679. EBCDIC CHARACTER CODES
  3680. PCRE can be compiled to run in an environment that uses EBCDIC as its
  3681. character code rather than ASCII or Unicode (typically a mainframe sys-
  3682. tem). In the sections below, character code values are ASCII or Uni-
  3683. code; in an EBCDIC environment these characters may have different code
  3684. values, and there are no code points greater than 255.
  3685. CHARACTERS AND METACHARACTERS
  3686. A regular expression is a pattern that is matched against a subject
  3687. string from left to right. Most characters stand for themselves in a
  3688. pattern, and match the corresponding characters in the subject. As a
  3689. trivial example, the pattern
  3690. The quick brown fox
  3691. matches a portion of a subject string that is identical to itself. When
  3692. caseless matching is specified (the PCRE_CASELESS option), letters are
  3693. matched independently of case. In a UTF mode, PCRE always understands
  3694. the concept of case for characters whose values are less than 128, so
  3695. caseless matching is always possible. For characters with higher val-
  3696. ues, the concept of case is supported if PCRE is compiled with Unicode
  3697. property support, but not otherwise. If you want to use caseless
  3698. matching for characters 128 and above, you must ensure that PCRE is
  3699. compiled with Unicode property support as well as with UTF support.
  3700. The power of regular expressions comes from the ability to include
  3701. alternatives and repetitions in the pattern. These are encoded in the
  3702. pattern by the use of metacharacters, which do not stand for themselves
  3703. but instead are interpreted in some special way.
  3704. There are two different sets of metacharacters: those that are recog-
  3705. nized anywhere in the pattern except within square brackets, and those
  3706. that are recognized within square brackets. Outside square brackets,
  3707. the metacharacters are as follows:
  3708. \ general escape character with several uses
  3709. ^ assert start of string (or line, in multiline mode)
  3710. $ assert end of string (or line, in multiline mode)
  3711. . match any character except newline (by default)
  3712. [ start character class definition
  3713. | start of alternative branch
  3714. ( start subpattern
  3715. ) end subpattern
  3716. ? extends the meaning of (
  3717. also 0 or 1 quantifier
  3718. also quantifier minimizer
  3719. * 0 or more quantifier
  3720. + 1 or more quantifier
  3721. also "possessive quantifier"
  3722. { start min/max quantifier
  3723. Part of a pattern that is in square brackets is called a "character
  3724. class". In a character class the only metacharacters are:
  3725. \ general escape character
  3726. ^ negate the class, but only if the first character
  3727. - indicates character range
  3728. [ POSIX character class (only if followed by POSIX
  3729. syntax)
  3730. ] terminates the character class
  3731. The following sections describe the use of each of the metacharacters.
  3732. BACKSLASH
  3733. The backslash character has several uses. Firstly, if it is followed by
  3734. a character that is not a number or a letter, it takes away any special
  3735. meaning that character may have. This use of backslash as an escape
  3736. character applies both inside and outside character classes.
  3737. For example, if you want to match a * character, you write \* in the
  3738. pattern. This escaping action applies whether or not the following
  3739. character would otherwise be interpreted as a metacharacter, so it is
  3740. always safe to precede a non-alphanumeric with backslash to specify
  3741. that it stands for itself. In particular, if you want to match a back-
  3742. slash, you write \\.
  3743. In a UTF mode, only ASCII numbers and letters have any special meaning
  3744. after a backslash. All other characters (in particular, those whose
  3745. codepoints are greater than 127) are treated as literals.
  3746. If a pattern is compiled with the PCRE_EXTENDED option, most white
  3747. space in the pattern (other than in a character class), and characters
  3748. between a # outside a character class and the next newline, inclusive,
  3749. are ignored. An escaping backslash can be used to include a white space
  3750. or # character as part of the pattern.
  3751. If you want to remove the special meaning from a sequence of charac-
  3752. ters, you can do so by putting them between \Q and \E. This is differ-
  3753. ent from Perl in that $ and @ are handled as literals in \Q...\E
  3754. sequences in PCRE, whereas in Perl, $ and @ cause variable interpola-
  3755. tion. Note the following examples:
  3756. Pattern PCRE matches Perl matches
  3757. \Qabc$xyz\E abc$xyz abc followed by the
  3758. contents of $xyz
  3759. \Qabc\$xyz\E abc\$xyz abc\$xyz
  3760. \Qabc\E\$\Qxyz\E abc$xyz abc$xyz
  3761. The \Q...\E sequence is recognized both inside and outside character
  3762. classes. An isolated \E that is not preceded by \Q is ignored. If \Q
  3763. is not followed by \E later in the pattern, the literal interpretation
  3764. continues to the end of the pattern (that is, \E is assumed at the
  3765. end). If the isolated \Q is inside a character class, this causes an
  3766. error, because the character class is not terminated.
  3767. Non-printing characters
  3768. A second use of backslash provides a way of encoding non-printing char-
  3769. acters in patterns in a visible manner. There is no restriction on the
  3770. appearance of non-printing characters, apart from the binary zero that
  3771. terminates a pattern, but when a pattern is being prepared by text
  3772. editing, it is often easier to use one of the following escape
  3773. sequences than the binary character it represents. In an ASCII or Uni-
  3774. code environment, these escapes are as follows:
  3775. \a alarm, that is, the BEL character (hex 07)
  3776. \cx "control-x", where x is any ASCII character
  3777. \e escape (hex 1B)
  3778. \f form feed (hex 0C)
  3779. \n linefeed (hex 0A)
  3780. \r carriage return (hex 0D)
  3781. \t tab (hex 09)
  3782. \0dd character with octal code 0dd
  3783. \ddd character with octal code ddd, or back reference
  3784. \o{ddd..} character with octal code ddd..
  3785. \xhh character with hex code hh
  3786. \x{hhh..} character with hex code hhh.. (non-JavaScript mode)
  3787. \uhhhh character with hex code hhhh (JavaScript mode only)
  3788. The precise effect of \cx on ASCII characters is as follows: if x is a
  3789. lower case letter, it is converted to upper case. Then bit 6 of the
  3790. character (hex 40) is inverted. Thus \cA to \cZ become hex 01 to hex 1A
  3791. (A is 41, Z is 5A), but \c{ becomes hex 3B ({ is 7B), and \c; becomes
  3792. hex 7B (; is 3B). If the data item (byte or 16-bit value) following \c
  3793. has a value greater than 127, a compile-time error occurs. This locks
  3794. out non-ASCII characters in all modes.
  3795. When PCRE is compiled in EBCDIC mode, \a, \e, \f, \n, \r, and \t gener-
  3796. ate the appropriate EBCDIC code values. The \c escape is processed as
  3797. specified for Perl in the perlebcdic document. The only characters that
  3798. are allowed after \c are A-Z, a-z, or one of @, [, \, ], ^, _, or ?.
  3799. Any other character provokes a compile-time error. The sequence \c@
  3800. encodes character code 0; after \c the letters (in either case) encode
  3801. characters 1-26 (hex 01 to hex 1A); [, \, ], ^, and _ encode characters
  3802. 27-31 (hex 1B to hex 1F), and \c? becomes either 255 (hex FF) or 95
  3803. (hex 5F).
  3804. Thus, apart from \c?, 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, \cG always generates code value 7,
  3807. which is BEL in ASCII but DEL in EBCDIC.
  3808. The sequence \c? 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 \c? 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. An option change within a subpattern (see
  4589. below for a description of subpatterns) affects only that part of the
  4590. subpattern that follows it, so
  4591. (a(?i)b)c
  4592. matches abc and aBc and no other strings (assuming PCRE_CASELESS is not
  4593. used). By this means, options can be made to have different settings
  4594. in different parts of the pattern. Any changes made in one alternative
  4595. do carry on into subsequent branches within the same subpattern. For
  4596. example,
  4597. (a(?i)b|c)
  4598. matches "ab", "aB", "c", and "C", even though when matching "C" the
  4599. first branch is abandoned before the option setting. This is because
  4600. the effects of option settings happen at compile time. There would be
  4601. some very weird behaviour otherwise.
  4602. Note: There are other PCRE-specific options that can be set by the
  4603. application when the compiling or matching functions are called. In
  4604. some cases the pattern can contain special leading sequences such as
  4605. (*CRLF) to override what the application has set or what has been
  4606. defaulted. Details are given in the section entitled "Newline
  4607. sequences" above. There are also the (*UTF8), (*UTF16),(*UTF32), and
  4608. (*UCP) leading sequences that can be used to set UTF and Unicode prop-
  4609. erty modes; they are equivalent to setting the PCRE_UTF8, PCRE_UTF16,
  4610. PCRE_UTF32 and the PCRE_UCP options, respectively. The (*UTF) sequence
  4611. is a generic version that can be used with any of the libraries. How-
  4612. ever, the application can set the PCRE_NEVER_UTF option, which locks
  4613. out the use of the (*UTF) sequences.
  4614. SUBPATTERNS
  4615. Subpatterns are delimited by parentheses (round brackets), which can be
  4616. nested. Turning part of a pattern into a subpattern does two things:
  4617. 1. It localizes a set of alternatives. For example, the pattern
  4618. cat(aract|erpillar|)
  4619. matches "cataract", "caterpillar", or "cat". Without the parentheses,
  4620. it would match "cataract", "erpillar" or an empty string.
  4621. 2. It sets up the subpattern as a capturing subpattern. This means
  4622. that, when the whole pattern matches, that portion of the subject
  4623. string that matched the subpattern is passed back to the caller via the
  4624. ovector argument of the matching function. (This applies only to the
  4625. traditional matching functions; the DFA matching functions do not sup-
  4626. port capturing.)
  4627. Opening parentheses are counted from left to right (starting from 1) to
  4628. obtain numbers for the capturing subpatterns. For example, if the
  4629. string "the red king" is matched against the pattern
  4630. the ((red|white) (king|queen))
  4631. the captured substrings are "red king", "red", and "king", and are num-
  4632. bered 1, 2, and 3, respectively.
  4633. The fact that plain parentheses fulfil two functions is not always
  4634. helpful. There are often times when a grouping subpattern is required
  4635. without a capturing requirement. If an opening parenthesis is followed
  4636. by a question mark and a colon, the subpattern does not do any captur-
  4637. ing, and is not counted when computing the number of any subsequent
  4638. capturing subpatterns. For example, if the string "the white queen" is
  4639. matched against the pattern
  4640. the ((?:red|white) (king|queen))
  4641. the captured substrings are "white queen" and "queen", and are numbered
  4642. 1 and 2. The maximum number of capturing subpatterns is 65535.
  4643. As a convenient shorthand, if any option settings are required at the
  4644. start of a non-capturing subpattern, the option letters may appear
  4645. between the "?" and the ":". Thus the two patterns
  4646. (?i:saturday|sunday)
  4647. (?:(?i)saturday|sunday)
  4648. match exactly the same set of strings. Because alternative branches are
  4649. tried from left to right, and options are not reset until the end of
  4650. the subpattern is reached, an option setting in one branch does affect
  4651. subsequent branches, so the above patterns match "SUNDAY" as well as
  4652. "Saturday".
  4653. DUPLICATE SUBPATTERN NUMBERS
  4654. Perl 5.10 introduced a feature whereby each alternative in a subpattern
  4655. uses the same numbers for its capturing parentheses. Such a subpattern
  4656. starts with (?| and is itself a non-capturing subpattern. For example,
  4657. consider this pattern:
  4658. (?|(Sat)ur|(Sun))day
  4659. Because the two alternatives are inside a (?| group, both sets of cap-
  4660. turing parentheses are numbered one. Thus, when the pattern matches,
  4661. you can look at captured substring number one, whichever alternative
  4662. matched. This construct is useful when you want to capture part, but
  4663. not all, of one of a number of alternatives. Inside a (?| group, paren-
  4664. theses are numbered as usual, but the number is reset at the start of
  4665. each branch. The numbers of any capturing parentheses that follow the
  4666. subpattern start after the highest number used in any branch. The fol-
  4667. lowing example is taken from the Perl documentation. The numbers under-
  4668. neath show in which buffer the captured content will be stored.
  4669. # before ---------------branch-reset----------- after
  4670. / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
  4671. # 1 2 2 3 2 3 4
  4672. A back reference to a numbered subpattern uses the most recent value
  4673. that is set for that number by any subpattern. The following pattern
  4674. matches "abcabc" or "defdef":
  4675. /(?|(abc)|(def))\1/
  4676. In contrast, a subroutine call to a numbered subpattern always refers
  4677. to the first one in the pattern with the given number. The following
  4678. pattern matches "abcabc" or "defabc":
  4679. /(?|(abc)|(def))(?1)/
  4680. If a condition test for a subpattern's having matched refers to a non-
  4681. unique number, the test is true if any of the subpatterns of that num-
  4682. ber have matched.
  4683. An alternative approach to using this "branch reset" feature is to use
  4684. duplicate named subpatterns, as described in the next section.
  4685. NAMED SUBPATTERNS
  4686. Identifying capturing parentheses by number is simple, but it can be
  4687. very hard to keep track of the numbers in complicated regular expres-
  4688. sions. Furthermore, if an expression is modified, the numbers may
  4689. change. To help with this difficulty, PCRE supports the naming of sub-
  4690. patterns. This feature was not added to Perl until release 5.10. Python
  4691. had the feature earlier, and PCRE introduced it at release 4.0, using
  4692. the Python syntax. PCRE now supports both the Perl and the Python syn-
  4693. tax. Perl allows identically numbered subpatterns to have different
  4694. names, but PCRE does not.
  4695. In PCRE, a subpattern can be named in one of three ways: (?<name>...)
  4696. or (?'name'...) as in Perl, or (?P<name>...) as in Python. References
  4697. to capturing parentheses from other parts of the pattern, such as back
  4698. references, recursion, and conditions, can be made by name as well as
  4699. by number.
  4700. Names consist of up to 32 alphanumeric characters and underscores, but
  4701. must start with a non-digit. Named capturing parentheses are still
  4702. allocated numbers as well as names, exactly as if the names were not
  4703. present. The PCRE API provides function calls for extracting the name-
  4704. to-number translation table from a compiled pattern. There is also a
  4705. convenience function for extracting a captured substring by name.
  4706. By default, a name must be unique within a pattern, but it is possible
  4707. to relax this constraint by setting the PCRE_DUPNAMES option at compile
  4708. time. (Duplicate names are also always permitted for subpatterns with
  4709. the same number, set up as described in the previous section.) Dupli-
  4710. cate names can be useful for patterns where only one instance of the
  4711. named parentheses can match. Suppose you want to match the name of a
  4712. weekday, either as a 3-letter abbreviation or as the full name, and in
  4713. both cases you want to extract the abbreviation. This pattern (ignoring
  4714. the line breaks) does the job:
  4715. (?<DN>Mon|Fri|Sun)(?:day)?|
  4716. (?<DN>Tue)(?:sday)?|
  4717. (?<DN>Wed)(?:nesday)?|
  4718. (?<DN>Thu)(?:rsday)?|
  4719. (?<DN>Sat)(?:urday)?
  4720. There are five capturing substrings, but only one is ever set after a
  4721. match. (An alternative way of solving this problem is to use a "branch
  4722. reset" subpattern, as described in the previous section.)
  4723. The convenience function for extracting the data by name returns the
  4724. substring for the first (and in this example, the only) subpattern of
  4725. that name that matched. This saves searching to find which numbered
  4726. subpattern it was.
  4727. If you make a back reference to a non-unique named subpattern from
  4728. elsewhere in the pattern, the subpatterns to which the name refers are
  4729. checked in the order in which they appear in the overall pattern. The
  4730. first one that is set is used for the reference. For example, this pat-
  4731. tern matches both "foofoo" and "barbar" but not "foobar" or "barfoo":
  4732. (?:(?<n>foo)|(?<n>bar))\k<n>
  4733. If you make a subroutine call to a non-unique named subpattern, the one
  4734. that corresponds to the first occurrence of the name is used. In the
  4735. absence of duplicate numbers (see the previous section) this is the one
  4736. with the lowest number.
  4737. If you use a named reference in a condition test (see the section about
  4738. conditions below), either to check whether a subpattern has matched, or
  4739. to check for recursion, all subpatterns with the same name are tested.
  4740. If the condition is true for any one of them, the overall condition is
  4741. true. This is the same behaviour as testing by number. For further
  4742. details of the interfaces for handling named subpatterns, see the
  4743. pcreapi documentation.
  4744. Warning: You cannot use different names to distinguish between two sub-
  4745. patterns with the same number because PCRE uses only the numbers when
  4746. matching. For this reason, an error is given at compile time if differ-
  4747. ent names are given to subpatterns with the same number. However, you
  4748. can always give the same name to subpatterns with the same number, even
  4749. when PCRE_DUPNAMES is not set.
  4750. REPETITION
  4751. Repetition is specified by quantifiers, which can follow any of the
  4752. following items:
  4753. a literal data character
  4754. the dot metacharacter
  4755. the \C escape sequence
  4756. the \X escape sequence
  4757. the \R escape sequence
  4758. an escape such as \d or \pL that matches a single character
  4759. a character class
  4760. a back reference (see next section)
  4761. a parenthesized subpattern (including assertions)
  4762. a subroutine call to a subpattern (recursive or otherwise)
  4763. The general repetition quantifier specifies a minimum and maximum num-
  4764. ber of permitted matches, by giving the two numbers in curly brackets
  4765. (braces), separated by a comma. The numbers must be less than 65536,
  4766. and the first must be less than or equal to the second. For example:
  4767. z{2,4}
  4768. matches "zz", "zzz", or "zzzz". A closing brace on its own is not a
  4769. special character. If the second number is omitted, but the comma is
  4770. present, there is no upper limit; if the second number and the comma
  4771. are both omitted, the quantifier specifies an exact number of required
  4772. matches. Thus
  4773. [aeiou]{3,}
  4774. matches at least 3 successive vowels, but may match many more, while
  4775. \d{8}
  4776. matches exactly 8 digits. An opening curly bracket that appears in a
  4777. position where a quantifier is not allowed, or one that does not match
  4778. the syntax of a quantifier, is taken as a literal character. For exam-
  4779. ple, {,6} is not a quantifier, but a literal string of four characters.
  4780. In UTF modes, quantifiers apply to characters rather than to individual
  4781. data units. Thus, for example, \x{100}{2} matches two characters, each
  4782. of which is represented by a two-byte sequence in a UTF-8 string. Simi-
  4783. larly, \X{3} matches three Unicode extended grapheme clusters, each of
  4784. which may be several data units long (and they may be of different
  4785. lengths).
  4786. The quantifier {0} is permitted, causing the expression to behave as if
  4787. the previous item and the quantifier were not present. This may be use-
  4788. ful for subpatterns that are referenced as subroutines from elsewhere
  4789. in the pattern (but see also the section entitled "Defining subpatterns
  4790. for use by reference only" below). Items other than subpatterns that
  4791. have a {0} quantifier are omitted from the compiled pattern.
  4792. For convenience, the three most common quantifiers have single-charac-
  4793. ter abbreviations:
  4794. * is equivalent to {0,}
  4795. + is equivalent to {1,}
  4796. ? is equivalent to {0,1}
  4797. It is possible to construct infinite loops by following a subpattern
  4798. that can match no characters with a quantifier that has no upper limit,
  4799. for example:
  4800. (a?)*
  4801. Earlier versions of Perl and PCRE used to give an error at compile time
  4802. for such patterns. However, because there are cases where this can be
  4803. useful, such patterns are now accepted, but if any repetition of the
  4804. subpattern does in fact match no characters, the loop is forcibly bro-
  4805. ken.
  4806. By default, the quantifiers are "greedy", that is, they match as much
  4807. as possible (up to the maximum number of permitted times), without
  4808. causing the rest of the pattern to fail. The classic example of where
  4809. this gives problems is in trying to match comments in C programs. These
  4810. appear between /* and */ and within the comment, individual * and /
  4811. characters may appear. An attempt to match C comments by applying the
  4812. pattern
  4813. /\*.*\*/
  4814. to the string
  4815. /* first comment */ not comment /* second comment */
  4816. fails, because it matches the entire string owing to the greediness of
  4817. the .* item.
  4818. However, if a quantifier is followed by a question mark, it ceases to
  4819. be greedy, and instead matches the minimum number of times possible, so
  4820. the pattern
  4821. /\*.*?\*/
  4822. does the right thing with the C comments. The meaning of the various
  4823. quantifiers is not otherwise changed, just the preferred number of
  4824. matches. Do not confuse this use of question mark with its use as a
  4825. quantifier in its own right. Because it has two uses, it can sometimes
  4826. appear doubled, as in
  4827. \d??\d
  4828. which matches one digit by preference, but can match two if that is the
  4829. only way the rest of the pattern matches.
  4830. If the PCRE_UNGREEDY option is set (an option that is not available in
  4831. Perl), the quantifiers are not greedy by default, but individual ones
  4832. can be made greedy by following them with a question mark. In other
  4833. words, it inverts the default behaviour.
  4834. When a parenthesized subpattern is quantified with a minimum repeat
  4835. count that is greater than 1 or with a limited maximum, more memory is
  4836. required for the compiled pattern, in proportion to the size of the
  4837. minimum or maximum.
  4838. If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equiv-
  4839. alent to Perl's /s) is set, thus allowing the dot to match newlines,
  4840. the pattern is implicitly anchored, because whatever follows will be
  4841. tried against every character position in the subject string, so there
  4842. is no point in retrying the overall match at any position after the
  4843. first. PCRE normally treats such a pattern as though it were preceded
  4844. by \A.
  4845. In cases where it is known that the subject string contains no new-
  4846. lines, it is worth setting PCRE_DOTALL in order to obtain this opti-
  4847. mization, or alternatively using ^ to indicate anchoring explicitly.
  4848. However, there are some cases where the optimization cannot be used.
  4849. When .* is inside capturing parentheses that are the subject of a back
  4850. reference elsewhere in the pattern, a match at the start may fail where
  4851. a later one succeeds. Consider, for example:
  4852. (.*)abc\1
  4853. If the subject is "xyz123abc123" the match point is the fourth charac-
  4854. ter. For this reason, such a pattern is not implicitly anchored.
  4855. Another case where implicit anchoring is not applied is when the lead-
  4856. ing .* is inside an atomic group. Once again, a match at the start may
  4857. fail where a later one succeeds. Consider this pattern:
  4858. (?>.*?a)b
  4859. It matches "ab" in the subject "aab". The use of the backtracking con-
  4860. trol verbs (*PRUNE) and (*SKIP) also disable this optimization.
  4861. When a capturing subpattern is repeated, the value captured is the sub-
  4862. string that matched the final iteration. For example, after
  4863. (tweedle[dume]{3}\s*)+
  4864. has matched "tweedledum tweedledee" the value of the captured substring
  4865. is "tweedledee". However, if there are nested capturing subpatterns,
  4866. the corresponding captured values may have been set in previous itera-
  4867. tions. For example, after
  4868. /(a|(b))+/
  4869. matches "aba" the value of the second captured substring is "b".
  4870. ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS
  4871. With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
  4872. repetition, failure of what follows normally causes the repeated item
  4873. to be re-evaluated to see if a different number of repeats allows the
  4874. rest of the pattern to match. Sometimes it is useful to prevent this,
  4875. either to change the nature of the match, or to cause it fail earlier
  4876. than it otherwise might, when the author of the pattern knows there is
  4877. no point in carrying on.
  4878. Consider, for example, the pattern \d+foo when applied to the subject
  4879. line
  4880. 123456bar
  4881. After matching all 6 digits and then failing to match "foo", the normal
  4882. action of the matcher is to try again with only 5 digits matching the
  4883. \d+ item, and then with 4, and so on, before ultimately failing.
  4884. "Atomic grouping" (a term taken from Jeffrey Friedl's book) provides
  4885. the means for specifying that once a subpattern has matched, it is not
  4886. to be re-evaluated in this way.
  4887. If we use atomic grouping for the previous example, the matcher gives
  4888. up immediately on failing to match "foo" the first time. The notation
  4889. is a kind of special parenthesis, starting with (?> as in this example:
  4890. (?>\d+)foo
  4891. This kind of parenthesis "locks up" the part of the pattern it con-
  4892. tains once it has matched, and a failure further into the pattern is
  4893. prevented from backtracking into it. Backtracking past it to previous
  4894. items, however, works as normal.
  4895. An alternative description is that a subpattern of this type matches
  4896. the string of characters that an identical standalone pattern would
  4897. match, if anchored at the current point in the subject string.
  4898. Atomic grouping subpatterns are not capturing subpatterns. Simple cases
  4899. such as the above example can be thought of as a maximizing repeat that
  4900. must swallow everything it can. So, while both \d+ and \d+? are pre-
  4901. pared to adjust the number of digits they match in order to make the
  4902. rest of the pattern match, (?>\d+) can only match an entire sequence of
  4903. digits.
  4904. Atomic groups in general can of course contain arbitrarily complicated
  4905. subpatterns, and can be nested. However, when the subpattern for an
  4906. atomic group is just a single repeated item, as in the example above, a
  4907. simpler notation, called a "possessive quantifier" can be used. This
  4908. consists of an additional + character following a quantifier. Using
  4909. this notation, the previous example can be rewritten as
  4910. \d++foo
  4911. Note that a possessive quantifier can be used with an entire group, for
  4912. example:
  4913. (abc|xyz){2,3}+
  4914. Possessive quantifiers are always greedy; the setting of the
  4915. PCRE_UNGREEDY option is ignored. They are a convenient notation for the
  4916. simpler forms of atomic group. However, there is no difference in the
  4917. meaning of a possessive quantifier and the equivalent atomic group,
  4918. though there may be a performance difference; possessive quantifiers
  4919. should be slightly faster.
  4920. The possessive quantifier syntax is an extension to the Perl 5.8 syn-
  4921. tax. Jeffrey Friedl originated the idea (and the name) in the first
  4922. edition of his book. Mike McCloskey liked it, so implemented it when he
  4923. built Sun's Java package, and PCRE copied it from there. It ultimately
  4924. found its way into Perl at release 5.10.
  4925. PCRE has an optimization that automatically "possessifies" certain sim-
  4926. ple pattern constructs. For example, the sequence A+B is treated as
  4927. A++B because there is no point in backtracking into a sequence of A's
  4928. when B must follow.
  4929. When a pattern contains an unlimited repeat inside a subpattern that
  4930. can itself be repeated an unlimited number of times, the use of an
  4931. atomic group is the only way to avoid some failing matches taking a
  4932. very long time indeed. The pattern
  4933. (\D+|<\d+>)*[!?]
  4934. matches an unlimited number of substrings that either consist of non-
  4935. digits, or digits enclosed in <>, followed by either ! or ?. When it
  4936. matches, it runs quickly. However, if it is applied to
  4937. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  4938. it takes a long time before reporting failure. This is because the
  4939. string can be divided between the internal \D+ repeat and the external
  4940. * repeat in a large number of ways, and all have to be tried. (The
  4941. example uses [!?] rather than a single character at the end, because
  4942. both PCRE and Perl have an optimization that allows for fast failure
  4943. when a single character is used. They remember the last single charac-
  4944. ter that is required for a match, and fail early if it is not present
  4945. in the string.) If the pattern is changed so that it uses an atomic
  4946. group, like this:
  4947. ((?>\D+)|<\d+>)*[!?]
  4948. sequences of non-digits cannot be broken, and failure happens quickly.
  4949. BACK REFERENCES
  4950. Outside a character class, a backslash followed by a digit greater than
  4951. 0 (and possibly further digits) is a back reference to a capturing sub-
  4952. pattern earlier (that is, to its left) in the pattern, provided there
  4953. have been that many previous capturing left parentheses.
  4954. However, if the decimal number following the backslash is less than 10,
  4955. it is always taken as a back reference, and causes an error only if
  4956. there are not that many capturing left parentheses in the entire pat-
  4957. tern. In other words, the parentheses that are referenced need not be
  4958. to the left of the reference for numbers less than 10. A "forward back
  4959. reference" of this type can make sense when a repetition is involved
  4960. and the subpattern to the right has participated in an earlier itera-
  4961. tion.
  4962. It is not possible to have a numerical "forward back reference" to a
  4963. subpattern whose number is 10 or more using this syntax because a
  4964. sequence such as \50 is interpreted as a character defined in octal.
  4965. See the subsection entitled "Non-printing characters" above for further
  4966. details of the handling of digits following a backslash. There is no
  4967. such problem when named parentheses are used. A back reference to any
  4968. subpattern is possible using named parentheses (see below).
  4969. Another way of avoiding the ambiguity inherent in the use of digits
  4970. following a backslash is to use the \g escape sequence. This escape
  4971. must be followed by an unsigned number or a negative number, optionally
  4972. enclosed in braces. These examples are all identical:
  4973. (ring), \1
  4974. (ring), \g1
  4975. (ring), \g{1}
  4976. An unsigned number specifies an absolute reference without the ambigu-
  4977. ity that is present in the older syntax. It is also useful when literal
  4978. digits follow the reference. A negative number is a relative reference.
  4979. Consider this example:
  4980. (abc(def)ghi)\g{-1}
  4981. The sequence \g{-1} is a reference to the most recently started captur-
  4982. ing subpattern before \g, that is, is it equivalent to \2 in this exam-
  4983. ple. Similarly, \g{-2} would be equivalent to \1. The use of relative
  4984. references can be helpful in long patterns, and also in patterns that
  4985. are created by joining together fragments that contain references
  4986. within themselves.
  4987. A back reference matches whatever actually matched the capturing sub-
  4988. pattern in the current subject string, rather than anything matching
  4989. the subpattern itself (see "Subpatterns as subroutines" below for a way
  4990. of doing that). So the pattern
  4991. (sens|respons)e and \1ibility
  4992. matches "sense and sensibility" and "response and responsibility", but
  4993. not "sense and responsibility". If caseful matching is in force at the
  4994. time of the back reference, the case of letters is relevant. For exam-
  4995. ple,
  4996. ((?i)rah)\s+\1
  4997. matches "rah rah" and "RAH RAH", but not "RAH rah", even though the
  4998. original capturing subpattern is matched caselessly.
  4999. There are several different ways of writing back references to named
  5000. subpatterns. The .NET syntax \k{name} and the Perl syntax \k<name> or
  5001. \k'name' are supported, as is the Python syntax (?P=name). Perl 5.10's
  5002. unified back reference syntax, in which \g can be used for both numeric
  5003. and named references, is also supported. We could rewrite the above
  5004. example in any of the following ways:
  5005. (?<p1>(?i)rah)\s+\k<p1>
  5006. (?'p1'(?i)rah)\s+\k{p1}
  5007. (?P<p1>(?i)rah)\s+(?P=p1)
  5008. (?<p1>(?i)rah)\s+\g{p1}
  5009. A subpattern that is referenced by name may appear in the pattern
  5010. before or after the reference.
  5011. There may be more than one back reference to the same subpattern. If a
  5012. subpattern has not actually been used in a particular match, any back
  5013. references to it always fail by default. For example, the pattern
  5014. (a|(bc))\2
  5015. always fails if it starts to match "a" rather than "bc". However, if
  5016. the PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back refer-
  5017. ence to an unset value matches an empty string.
  5018. Because there may be many capturing parentheses in a pattern, all dig-
  5019. its following a backslash are taken as part of a potential back refer-
  5020. ence number. If the pattern continues with a digit character, some
  5021. delimiter must be used to terminate the back reference. If the
  5022. PCRE_EXTENDED option is set, this can be white space. Otherwise, the
  5023. \g{ syntax or an empty comment (see "Comments" below) can be used.
  5024. Recursive back references
  5025. A back reference that occurs inside the parentheses to which it refers
  5026. fails when the subpattern is first used, so, for example, (a\1) never
  5027. matches. However, such references can be useful inside repeated sub-
  5028. patterns. For example, the pattern
  5029. (a|b\1)+
  5030. matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-
  5031. ation of the subpattern, the back reference matches the character
  5032. string corresponding to the previous iteration. In order for this to
  5033. work, the pattern must be such that the first iteration does not need
  5034. to match the back reference. This can be done using alternation, as in
  5035. the example above, or by a quantifier with a minimum of zero.
  5036. Back references of this type cause the group that they reference to be
  5037. treated as an atomic group. Once the whole group has been matched, a
  5038. subsequent matching failure cannot cause backtracking into the middle
  5039. of the group.
  5040. ASSERTIONS
  5041. An assertion is a test on the characters following or preceding the
  5042. current matching point that does not actually consume any characters.
  5043. The simple assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are
  5044. described above.
  5045. More complicated assertions are coded as subpatterns. There are two
  5046. kinds: those that look ahead of the current position in the subject
  5047. string, and those that look behind it. An assertion subpattern is
  5048. matched in the normal way, except that it does not cause the current
  5049. matching position to be changed.
  5050. Assertion subpatterns are not capturing subpatterns. If such an asser-
  5051. tion contains capturing subpatterns within it, these are counted for
  5052. the purposes of numbering the capturing subpatterns in the whole pat-
  5053. tern. However, substring capturing is carried out only for positive
  5054. assertions. (Perl sometimes, but not always, does do capturing in nega-
  5055. tive assertions.)
  5056. WARNING: If a positive assertion containing one or more capturing sub-
  5057. patterns succeeds, but failure to match later in the pattern causes
  5058. backtracking over this assertion, the captures within the assertion are
  5059. reset only if no higher numbered captures are already set. This is,
  5060. unfortunately, a fundamental limitation of the current implementation,
  5061. and as PCRE1 is now in maintenance-only status, it is unlikely ever to
  5062. change.
  5063. For compatibility with Perl, assertion subpatterns may be repeated;
  5064. though it makes no sense to assert the same thing several times, the
  5065. side effect of capturing parentheses may occasionally be useful. In
  5066. practice, there only three cases:
  5067. (1) If the quantifier is {0}, the assertion is never obeyed during
  5068. matching. However, it may contain internal capturing parenthesized
  5069. groups that are called from elsewhere via the subroutine mechanism.
  5070. (2) If quantifier is {0,n} where n is greater than zero, it is treated
  5071. as if it were {0,1}. At run time, the rest of the pattern match is
  5072. tried with and without the assertion, the order depending on the greed-
  5073. iness of the quantifier.
  5074. (3) If the minimum repetition is greater than zero, the quantifier is
  5075. ignored. The assertion is obeyed just once when encountered during
  5076. matching.
  5077. Lookahead assertions
  5078. Lookahead assertions start with (?= for positive assertions and (?! for
  5079. negative assertions. For example,
  5080. \w+(?=;)
  5081. matches a word followed by a semicolon, but does not include the semi-
  5082. colon in the match, and
  5083. foo(?!bar)
  5084. matches any occurrence of "foo" that is not followed by "bar". Note
  5085. that the apparently similar pattern
  5086. (?!foo)bar
  5087. does not find an occurrence of "bar" that is preceded by something
  5088. other than "foo"; it finds any occurrence of "bar" whatsoever, because
  5089. the assertion (?!foo) is always true when the next three characters are
  5090. "bar". A lookbehind assertion is needed to achieve the other effect.
  5091. If you want to force a matching failure at some point in a pattern, the
  5092. most convenient way to do it is with (?!) because an empty string
  5093. always matches, so an assertion that requires there not to be an empty
  5094. string must always fail. The backtracking control verb (*FAIL) or (*F)
  5095. is a synonym for (?!).
  5096. Lookbehind assertions
  5097. Lookbehind assertions start with (?<= for positive assertions and (?<!
  5098. for negative assertions. For example,
  5099. (?<!foo)bar
  5100. does find an occurrence of "bar" that is not preceded by "foo". The
  5101. contents of a lookbehind assertion are restricted such that all the
  5102. strings it matches must have a fixed length. However, if there are sev-
  5103. eral top-level alternatives, they do not all have to have the same
  5104. fixed length. Thus
  5105. (?<=bullock|donkey)
  5106. is permitted, but
  5107. (?<!dogs?|cats?)
  5108. causes an error at compile time. Branches that match different length
  5109. strings are permitted only at the top level of a lookbehind assertion.
  5110. This is an extension compared with Perl, which requires all branches to
  5111. match the same length of string. An assertion such as
  5112. (?<=ab(c|de))
  5113. is not permitted, because its single top-level branch can match two
  5114. different lengths, but it is acceptable to PCRE if rewritten to use two
  5115. top-level branches:
  5116. (?<=abc|abde)
  5117. In some cases, the escape sequence \K (see above) can be used instead
  5118. of a lookbehind assertion to get round the fixed-length restriction.
  5119. The implementation of lookbehind assertions is, for each alternative,
  5120. to temporarily move the current position back by the fixed length and
  5121. then try to match. If there are insufficient characters before the cur-
  5122. rent position, the assertion fails.
  5123. In a UTF mode, PCRE does not allow the \C escape (which matches a sin-
  5124. gle data unit even in a UTF mode) to appear in lookbehind assertions,
  5125. because it makes it impossible to calculate the length of the lookbe-
  5126. hind. The \X and \R escapes, which can match different numbers of data
  5127. units, are also not permitted.
  5128. "Subroutine" calls (see below) such as (?2) or (?&X) are permitted in
  5129. lookbehinds, as long as the subpattern matches a fixed-length string.
  5130. Recursion, however, is not supported.
  5131. Possessive quantifiers can be used in conjunction with lookbehind
  5132. assertions to specify efficient matching of fixed-length strings at the
  5133. end of subject strings. Consider a simple pattern such as
  5134. abcd$
  5135. when applied to a long string that does not match. Because matching
  5136. proceeds from left to right, PCRE will look for each "a" in the subject
  5137. and then see if what follows matches the rest of the pattern. If the
  5138. pattern is specified as
  5139. ^.*abcd$
  5140. the initial .* matches the entire string at first, but when this fails
  5141. (because there is no following "a"), it backtracks to match all but the
  5142. last character, then all but the last two characters, and so on. Once
  5143. again the search for "a" covers the entire string, from right to left,
  5144. so we are no better off. However, if the pattern is written as
  5145. ^.*+(?<=abcd)
  5146. there can be no backtracking for the .*+ item; it can match only the
  5147. entire string. The subsequent lookbehind assertion does a single test
  5148. on the last four characters. If it fails, the match fails immediately.
  5149. For long strings, this approach makes a significant difference to the
  5150. processing time.
  5151. Using multiple assertions
  5152. Several assertions (of any sort) may occur in succession. For example,
  5153. (?<=\d{3})(?<!999)foo
  5154. matches "foo" preceded by three digits that are not "999". Notice that
  5155. each of the assertions is applied independently at the same point in
  5156. the subject string. First there is a check that the previous three
  5157. characters are all digits, and then there is a check that the same
  5158. three characters are not "999". This pattern does not match "foo" pre-
  5159. ceded by six characters, the first of which are digits and the last
  5160. three of which are not "999". For example, it doesn't match "123abc-
  5161. foo". A pattern to do that is
  5162. (?<=\d{3}...)(?<!999)foo
  5163. This time the first assertion looks at the preceding six characters,
  5164. checking that the first three are digits, and then the second assertion
  5165. checks that the preceding three characters are not "999".
  5166. Assertions can be nested in any combination. For example,
  5167. (?<=(?<!foo)bar)baz
  5168. matches an occurrence of "baz" that is preceded by "bar" which in turn
  5169. is not preceded by "foo", while
  5170. (?<=\d{3}(?!999)...)foo
  5171. is another pattern that matches "foo" preceded by three digits and any
  5172. three characters that are not "999".
  5173. CONDITIONAL SUBPATTERNS
  5174. It is possible to cause the matching process to obey a subpattern con-
  5175. ditionally or to choose between two alternative subpatterns, depending
  5176. on the result of an assertion, or whether a specific capturing subpat-
  5177. tern has already been matched. The two possible forms of conditional
  5178. subpattern are:
  5179. (?(condition)yes-pattern)
  5180. (?(condition)yes-pattern|no-pattern)
  5181. If the condition is satisfied, the yes-pattern is used; otherwise the
  5182. no-pattern (if present) is used. If there are more than two alterna-
  5183. tives in the subpattern, a compile-time error occurs. Each of the two
  5184. alternatives may itself contain nested subpatterns of any form, includ-
  5185. ing conditional subpatterns; the restriction to two alternatives
  5186. applies only at the level of the condition. This pattern fragment is an
  5187. example where the alternatives are complex:
  5188. (?(1) (A|B|C) | (D | (?(2)E|F) | E) )
  5189. There are four kinds of condition: references to subpatterns, refer-
  5190. ences to recursion, a pseudo-condition called DEFINE, and assertions.
  5191. Checking for a used subpattern by number
  5192. If the text between the parentheses consists of a sequence of digits,
  5193. the condition is true if a capturing subpattern of that number has pre-
  5194. viously matched. If there is more than one capturing subpattern with
  5195. the same number (see the earlier section about duplicate subpattern
  5196. numbers), the condition is true if any of them have matched. An alter-
  5197. native notation is to precede the digits with a plus or minus sign. In
  5198. this case, the subpattern number is relative rather than absolute. The
  5199. most recently opened parentheses can be referenced by (?(-1), the next
  5200. most recent by (?(-2), and so on. Inside loops it can also make sense
  5201. to refer to subsequent groups. The next parentheses to be opened can be
  5202. referenced as (?(+1), and so on. (The value zero in any of these forms
  5203. is not used; it provokes a compile-time error.)
  5204. Consider the following pattern, which contains non-significant white
  5205. space to make it more readable (assume the PCRE_EXTENDED option) and to
  5206. divide it into three parts for ease of discussion:
  5207. ( \( )? [^()]+ (?(1) \) )
  5208. The first part matches an optional opening parenthesis, and if that
  5209. character is present, sets it as the first captured substring. The sec-
  5210. ond part matches one or more characters that are not parentheses. The
  5211. third part is a conditional subpattern that tests whether or not the
  5212. first set of parentheses matched. If they did, that is, if subject
  5213. started with an opening parenthesis, the condition is true, and so the
  5214. yes-pattern is executed and a closing parenthesis is required. Other-
  5215. wise, since no-pattern is not present, the subpattern matches nothing.
  5216. In other words, this pattern matches a sequence of non-parentheses,
  5217. optionally enclosed in parentheses.
  5218. If you were embedding this pattern in a larger one, you could use a
  5219. relative reference:
  5220. ...other stuff... ( \( )? [^()]+ (?(-1) \) ) ...
  5221. This makes the fragment independent of the parentheses in the larger
  5222. pattern.
  5223. Checking for a used subpattern by name
  5224. Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a
  5225. used subpattern by name. For compatibility with earlier versions of
  5226. PCRE, which had this facility before Perl, the syntax (?(name)...) is
  5227. also recognized.
  5228. Rewriting the above example to use a named subpattern gives this:
  5229. (?<OPEN> \( )? [^()]+ (?(<OPEN>) \) )
  5230. If the name used in a condition of this kind is a duplicate, the test
  5231. is applied to all subpatterns of the same name, and is true if any one
  5232. of them has matched.
  5233. Checking for pattern recursion
  5234. If the condition is the string (R), and there is no subpattern with the
  5235. name R, the condition is true if a recursive call to the whole pattern
  5236. or any subpattern has been made. If digits or a name preceded by amper-
  5237. sand follow the letter R, for example:
  5238. (?(R3)...) or (?(R&name)...)
  5239. the condition is true if the most recent recursion is into a subpattern
  5240. whose number or name is given. This condition does not check the entire
  5241. recursion stack. If the name used in a condition of this kind is a
  5242. duplicate, the test is applied to all subpatterns of the same name, and
  5243. is true if any one of them is the most recent recursion.
  5244. At "top level", all these recursion test conditions are false. The
  5245. syntax for recursive patterns is described below.
  5246. Defining subpatterns for use by reference only
  5247. If the condition is the string (DEFINE), and there is no subpattern
  5248. with the name DEFINE, the condition is always false. In this case,
  5249. there may be only one alternative in the subpattern. It is always
  5250. skipped if control reaches this point in the pattern; the idea of
  5251. DEFINE is that it can be used to define subroutines that can be refer-
  5252. enced from elsewhere. (The use of subroutines is described below.) For
  5253. example, a pattern to match an IPv4 address such as "192.168.23.245"
  5254. could be written like this (ignore white space and line breaks):
  5255. (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
  5256. \b (?&byte) (\.(?&byte)){3} \b
  5257. The first part of the pattern is a DEFINE group inside which a another
  5258. group named "byte" is defined. This matches an individual component of
  5259. an IPv4 address (a number less than 256). When matching takes place,
  5260. this part of the pattern is skipped because DEFINE acts like a false
  5261. condition. The rest of the pattern uses references to the named group
  5262. to match the four dot-separated components of an IPv4 address, insist-
  5263. ing on a word boundary at each end.
  5264. Assertion conditions
  5265. If the condition is not in any of the above formats, it must be an
  5266. assertion. This may be a positive or negative lookahead or lookbehind
  5267. assertion. Consider this pattern, again containing non-significant
  5268. white space, and with the two alternatives on the second line:
  5269. (?(?=[^a-z]*[a-z])
  5270. \d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} )
  5271. The condition is a positive lookahead assertion that matches an
  5272. optional sequence of non-letters followed by a letter. In other words,
  5273. it tests for the presence of at least one letter in the subject. If a
  5274. letter is found, the subject is matched against the first alternative;
  5275. otherwise it is matched against the second. This pattern matches
  5276. strings in one of the two forms dd-aaa-dd or dd-dd-dd, where aaa are
  5277. letters and dd are digits.
  5278. COMMENTS
  5279. There are two ways of including comments in patterns that are processed
  5280. by PCRE. In both cases, the start of the comment must not be in a char-
  5281. acter class, nor in the middle of any other sequence of related charac-
  5282. ters such as (?: or a subpattern name or number. The characters that
  5283. make up a comment play no part in the pattern matching.
  5284. The sequence (?# marks the start of a comment that continues up to the
  5285. next closing parenthesis. Nested parentheses are not permitted. If the
  5286. PCRE_EXTENDED option is set, an unescaped # character also introduces a
  5287. comment, which in this case continues to immediately after the next
  5288. newline character or character sequence in the pattern. Which charac-
  5289. ters are interpreted as newlines is controlled by the options passed to
  5290. a compiling function or by a special sequence at the start of the pat-
  5291. tern, as described in the section entitled "Newline conventions" above.
  5292. Note that the end of this type of comment is a literal newline sequence
  5293. in the pattern; escape sequences that happen to represent a newline do
  5294. not count. For example, consider this pattern when PCRE_EXTENDED is
  5295. set, and the default newline convention is in force:
  5296. abc #comment \n still comment
  5297. On encountering the # character, pcre_compile() skips along, looking
  5298. for a newline in the pattern. The sequence \n is still literal at this
  5299. stage, so it does not terminate the comment. Only an actual character
  5300. with the code value 0x0a (the default newline) does so.
  5301. RECURSIVE PATTERNS
  5302. Consider the problem of matching a string in parentheses, allowing for
  5303. unlimited nested parentheses. Without the use of recursion, the best
  5304. that can be done is to use a pattern that matches up to some fixed
  5305. depth of nesting. It is not possible to handle an arbitrary nesting
  5306. depth.
  5307. For some time, Perl has provided a facility that allows regular expres-
  5308. sions to recurse (amongst other things). It does this by interpolating
  5309. Perl code in the expression at run time, and the code can refer to the
  5310. expression itself. A Perl pattern using code interpolation to solve the
  5311. parentheses problem can be created like this:
  5312. $re = qr{\( (?: (?>[^()]+) | (?p{$re}) )* \)}x;
  5313. The (?p{...}) item interpolates Perl code at run time, and in this case
  5314. refers recursively to the pattern in which it appears.
  5315. Obviously, PCRE cannot support the interpolation of Perl code. Instead,
  5316. it supports special syntax for recursion of the entire pattern, and
  5317. also for individual subpattern recursion. After its introduction in
  5318. PCRE and Python, this kind of recursion was subsequently introduced
  5319. into Perl at release 5.10.
  5320. A special item that consists of (? followed by a number greater than
  5321. zero and a closing parenthesis is a recursive subroutine call of the
  5322. subpattern of the given number, provided that it occurs inside that
  5323. subpattern. (If not, it is a non-recursive subroutine call, which is
  5324. described in the next section.) The special item (?R) or (?0) is a
  5325. recursive call of the entire regular expression.
  5326. This PCRE pattern solves the nested parentheses problem (assume the
  5327. PCRE_EXTENDED option is set so that white space is ignored):
  5328. \( ( [^()]++ | (?R) )* \)
  5329. First it matches an opening parenthesis. Then it matches any number of
  5330. substrings which can either be a sequence of non-parentheses, or a
  5331. recursive match of the pattern itself (that is, a correctly parenthe-
  5332. sized substring). Finally there is a closing parenthesis. Note the use
  5333. of a possessive quantifier to avoid backtracking into sequences of non-
  5334. parentheses.
  5335. If this were part of a larger pattern, you would not want to recurse
  5336. the entire pattern, so instead you could use this:
  5337. ( \( ( [^()]++ | (?1) )* \) )
  5338. We have put the pattern into parentheses, and caused the recursion to
  5339. refer to them instead of the whole pattern.
  5340. In a larger pattern, keeping track of parenthesis numbers can be
  5341. tricky. This is made easier by the use of relative references. Instead
  5342. of (?1) in the pattern above you can write (?-2) to refer to the second
  5343. most recently opened parentheses preceding the recursion. In other
  5344. words, a negative number counts capturing parentheses leftwards from
  5345. the point at which it is encountered.
  5346. It is also possible to refer to subsequently opened parentheses, by
  5347. writing references such as (?+2). However, these cannot be recursive
  5348. because the reference is not inside the parentheses that are refer-
  5349. enced. They are always non-recursive subroutine calls, as described in
  5350. the next section.
  5351. An alternative approach is to use named parentheses instead. The Perl
  5352. syntax for this is (?&name); PCRE's earlier syntax (?P>name) is also
  5353. supported. We could rewrite the above example as follows:
  5354. (?<pn> \( ( [^()]++ | (?&pn) )* \) )
  5355. If there is more than one subpattern with the same name, the earliest
  5356. one is used.
  5357. This particular example pattern that we have been looking at contains
  5358. nested unlimited repeats, and so the use of a possessive quantifier for
  5359. matching strings of non-parentheses is important when applying the pat-
  5360. tern to strings that do not match. For example, when this pattern is
  5361. applied to
  5362. (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
  5363. it yields "no match" quickly. However, if a possessive quantifier is
  5364. not used, the match runs for a very long time indeed because there are
  5365. so many different ways the + and * repeats can carve up the subject,
  5366. and all have to be tested before failure can be reported.
  5367. At the end of a match, the values of capturing parentheses are those
  5368. from the outermost level. If you want to obtain intermediate values, a
  5369. callout function can be used (see below and the pcrecallout documenta-
  5370. tion). If the pattern above is matched against
  5371. (ab(cd)ef)
  5372. the value for the inner capturing parentheses (numbered 2) is "ef",
  5373. which is the last value taken on at the top level. If a capturing sub-
  5374. pattern is not matched at the top level, its final captured value is
  5375. unset, even if it was (temporarily) set at a deeper level during the
  5376. matching process.
  5377. If there are more than 15 capturing parentheses in a pattern, PCRE has
  5378. to obtain extra memory to store data during a recursion, which it does
  5379. by using pcre_malloc, freeing it via pcre_free afterwards. If no memory
  5380. can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.
  5381. Do not confuse the (?R) item with the condition (R), which tests for
  5382. recursion. Consider this pattern, which matches text in angle brack-
  5383. ets, allowing for arbitrary nesting. Only digits are allowed in nested
  5384. brackets (that is, when recursing), whereas any characters are permit-
  5385. ted at the outer level.
  5386. < (?: (?(R) \d++ | [^<>]*+) | (?R)) * >
  5387. In this pattern, (?(R) is the start of a conditional subpattern, with
  5388. two different alternatives for the recursive and non-recursive cases.
  5389. The (?R) item is the actual recursive call.
  5390. Differences in recursion processing between PCRE and Perl
  5391. Recursion processing in PCRE differs from Perl in two important ways.
  5392. In PCRE (like Python, but unlike Perl), a recursive subpattern call is
  5393. always treated as an atomic group. That is, once it has matched some of
  5394. the subject string, it is never re-entered, even if it contains untried
  5395. alternatives and there is a subsequent matching failure. This can be
  5396. illustrated by the following pattern, which purports to match a palin-
  5397. dromic string that contains an odd number of characters (for example,
  5398. "a", "aba", "abcba", "abcdcba"):
  5399. ^(.|(.)(?1)\2)$
  5400. The idea is that it either matches a single character, or two identical
  5401. characters surrounding a sub-palindrome. In Perl, this pattern works;
  5402. in PCRE it does not if the pattern is longer than three characters.
  5403. Consider the subject string "abcba":
  5404. At the top level, the first character is matched, but as it is not at
  5405. the end of the string, the first alternative fails; the second alterna-
  5406. tive is taken and the recursion kicks in. The recursive call to subpat-
  5407. tern 1 successfully matches the next character ("b"). (Note that the
  5408. beginning and end of line tests are not part of the recursion).
  5409. Back at the top level, the next character ("c") is compared with what
  5410. subpattern 2 matched, which was "a". This fails. Because the recursion
  5411. is treated as an atomic group, there are now no backtracking points,
  5412. and so the entire match fails. (Perl is able, at this point, to re-
  5413. enter the recursion and try the second alternative.) However, if the
  5414. pattern is written with the alternatives in the other order, things are
  5415. different:
  5416. ^((.)(?1)\2|.)$
  5417. This time, the recursing alternative is tried first, and continues to
  5418. recurse until it runs out of characters, at which point the recursion
  5419. fails. But this time we do have another alternative to try at the
  5420. higher level. That is the big difference: in the previous case the
  5421. remaining alternative is at a deeper recursion level, which PCRE cannot
  5422. use.
  5423. To change the pattern so that it matches all palindromic strings, not
  5424. just those with an odd number of characters, it is tempting to change
  5425. the pattern to this:
  5426. ^((.)(?1)\2|.?)$
  5427. Again, this works in Perl, but not in PCRE, and for the same reason.
  5428. When a deeper recursion has matched a single character, it cannot be
  5429. entered again in order to match an empty string. The solution is to
  5430. separate the two cases, and write out the odd and even cases as alter-
  5431. natives at the higher level:
  5432. ^(?:((.)(?1)\2|)|((.)(?3)\4|.))
  5433. If you want to match typical palindromic phrases, the pattern has to
  5434. ignore all non-word characters, which can be done like this:
  5435. ^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$
  5436. If run with the PCRE_CASELESS option, this pattern matches phrases such
  5437. as "A man, a plan, a canal: Panama!" and it works well in both PCRE and
  5438. Perl. Note the use of the possessive quantifier *+ to avoid backtrack-
  5439. ing into sequences of non-word characters. Without this, PCRE takes a
  5440. great deal longer (ten times or more) to match typical phrases, and
  5441. Perl takes so long that you think it has gone into a loop.
  5442. WARNING: The palindrome-matching patterns above work only if the sub-
  5443. ject string does not start with a palindrome that is shorter than the
  5444. entire string. For example, although "abcba" is correctly matched, if
  5445. the subject is "ababa", PCRE finds the palindrome "aba" at the start,
  5446. then fails at top level because the end of the string does not follow.
  5447. Once again, it cannot jump back into the recursion to try other alter-
  5448. natives, so the entire match fails.
  5449. The second way in which PCRE and Perl differ in their recursion pro-
  5450. cessing is in the handling of captured values. In Perl, when a subpat-
  5451. tern is called recursively or as a subpattern (see the next section),
  5452. it has no access to any values that were captured outside the recur-
  5453. sion, whereas in PCRE these values can be referenced. Consider this
  5454. pattern:
  5455. ^(.)(\1|a(?2))
  5456. In PCRE, this pattern matches "bab". The first capturing parentheses
  5457. match "b", then in the second group, when the back reference \1 fails
  5458. to match "b", the second alternative matches "a" and then recurses. In
  5459. the recursion, \1 does now match "b" and so the whole match succeeds.
  5460. In Perl, the pattern fails to match because inside the recursive call
  5461. \1 cannot access the externally set value.
  5462. SUBPATTERNS AS SUBROUTINES
  5463. If the syntax for a recursive subpattern call (either by number or by
  5464. name) is used outside the parentheses to which it refers, it operates
  5465. like a subroutine in a programming language. The called subpattern may
  5466. be defined before or after the reference. A numbered reference can be
  5467. absolute or relative, as in these examples:
  5468. (...(absolute)...)...(?2)...
  5469. (...(relative)...)...(?-1)...
  5470. (...(?+1)...(relative)...
  5471. An earlier example pointed out that the pattern
  5472. (sens|respons)e and \1ibility
  5473. matches "sense and sensibility" and "response and responsibility", but
  5474. not "sense and responsibility". If instead the pattern
  5475. (sens|respons)e and (?1)ibility
  5476. is used, it does match "sense and responsibility" as well as the other
  5477. two strings. Another example is given in the discussion of DEFINE
  5478. above.
  5479. All subroutine calls, whether recursive or not, are always treated as
  5480. atomic groups. That is, once a subroutine has matched some of the sub-
  5481. ject string, it is never re-entered, even if it contains untried alter-
  5482. natives and there is a subsequent matching failure. Any capturing
  5483. parentheses that are set during the subroutine call revert to their
  5484. previous values afterwards.
  5485. Processing options such as case-independence are fixed when a subpat-
  5486. tern is defined, so if it is used as a subroutine, such options cannot
  5487. be changed for different calls. For example, consider this pattern:
  5488. (abc)(?i:(?-1))
  5489. It matches "abcabc". It does not match "abcABC" because the change of
  5490. processing option does not affect the called subpattern.
  5491. ONIGURUMA SUBROUTINE SYNTAX
  5492. For compatibility with Oniguruma, the non-Perl syntax \g followed by a
  5493. name or a number enclosed either in angle brackets or single quotes, is
  5494. an alternative syntax for referencing a subpattern as a subroutine,
  5495. possibly recursively. Here are two of the examples used above, rewrit-
  5496. ten using this syntax:
  5497. (?<pn> \( ( (?>[^()]+) | \g<pn> )* \) )
  5498. (sens|respons)e and \g'1'ibility
  5499. PCRE supports an extension to Oniguruma: if a number is preceded by a
  5500. plus or a minus sign it is taken as a relative reference. For example:
  5501. (abc)(?i:\g<-1>)
  5502. Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are not
  5503. synonymous. The former is a back reference; the latter is a subroutine
  5504. call.
  5505. CALLOUTS
  5506. Perl has a feature whereby using the sequence (?{...}) causes arbitrary
  5507. Perl code to be obeyed in the middle of matching a regular expression.
  5508. This makes it possible, amongst other things, to extract different sub-
  5509. strings that match the same pair of parentheses when there is a repeti-
  5510. tion.
  5511. PCRE provides a similar feature, but of course it cannot obey arbitrary
  5512. Perl code. The feature is called "callout". The caller of PCRE provides
  5513. an external function by putting its entry point in the global variable
  5514. pcre_callout (8-bit library) or pcre[16|32]_callout (16-bit or 32-bit
  5515. library). By default, this variable contains NULL, which disables all
  5516. calling out.
  5517. Within a regular expression, (?C) indicates the points at which the
  5518. external function is to be called. If you want to identify different
  5519. callout points, you can put a number less than 256 after the letter C.
  5520. The default value is zero. For example, this pattern has two callout
  5521. points:
  5522. (?C1)abc(?C2)def
  5523. If the PCRE_AUTO_CALLOUT flag is passed to a compiling function, call-
  5524. outs are automatically installed before each item in the pattern. They
  5525. are all numbered 255. If there is a conditional group in the pattern
  5526. whose condition is an assertion, an additional callout is inserted just
  5527. before the condition. An explicit callout may also be set at this posi-
  5528. tion, as in this example:
  5529. (?(?C9)(?=a)abc|def)
  5530. Note that this applies only to assertion conditions, not to other types
  5531. of condition.
  5532. During matching, when PCRE reaches a callout point, the external func-
  5533. tion is called. It is provided with the number of the callout, the
  5534. position in the pattern, and, optionally, one item of data originally
  5535. supplied by the caller of the matching function. The callout function
  5536. may cause matching to proceed, to backtrack, or to fail altogether.
  5537. By default, PCRE implements a number of optimizations at compile time
  5538. and matching time, and one side-effect is that sometimes callouts are
  5539. skipped. If you need all possible callouts to happen, you need to set
  5540. options that disable the relevant optimizations. More details, and a
  5541. complete description of the interface to the callout function, are
  5542. given in the pcrecallout documentation.
  5543. BACKTRACKING CONTROL
  5544. Perl 5.10 introduced a number of "Special Backtracking Control Verbs",
  5545. which are still described in the Perl documentation as "experimental
  5546. and subject to change or removal in a future version of Perl". It goes
  5547. on to say: "Their usage in production code should be noted to avoid
  5548. problems during upgrades." The same remarks apply to the PCRE features
  5549. described in this section.
  5550. The new verbs make use of what was previously invalid syntax: an open-
  5551. ing parenthesis followed by an asterisk. They are generally of the form
  5552. (*VERB) or (*VERB:NAME). Some may take either form, possibly behaving
  5553. differently depending on whether or not a name is present. A name is
  5554. any sequence of characters that does not include a closing parenthesis.
  5555. The maximum length of name is 255 in the 8-bit library and 65535 in the
  5556. 16-bit and 32-bit libraries. If the name is empty, that is, if the
  5557. closing parenthesis immediately follows the colon, the effect is as if
  5558. the colon were not there. Any number of these verbs may occur in a
  5559. pattern.
  5560. Since these verbs are specifically related to backtracking, most of
  5561. them can be used only when the pattern is to be matched using one of
  5562. the traditional matching functions, because these use a backtracking
  5563. algorithm. With the exception of (*FAIL), which behaves like a failing
  5564. negative assertion, the backtracking control verbs cause an error if
  5565. encountered by a DFA matching function.
  5566. The behaviour of these verbs in repeated groups, assertions, and in
  5567. subpatterns called as subroutines (whether or not recursively) is docu-
  5568. mented below.
  5569. Optimizations that affect backtracking verbs
  5570. PCRE contains some optimizations that are used to speed up matching by
  5571. running some checks at the start of each match attempt. For example, it
  5572. may know the minimum length of matching subject, or that a particular
  5573. character must be present. When one of these optimizations bypasses the
  5574. running of a match, any included backtracking verbs will not, of
  5575. course, be processed. You can suppress the start-of-match optimizations
  5576. by setting the PCRE_NO_START_OPTIMIZE option when calling pcre_com-
  5577. pile() or pcre_exec(), or by starting the pattern with (*NO_START_OPT).
  5578. There is more discussion of this option in the section entitled "Option
  5579. bits for pcre_exec()" in the pcreapi documentation.
  5580. Experiments with Perl suggest that it too has similar optimizations,
  5581. sometimes leading to anomalous results.
  5582. Verbs that act immediately
  5583. The following verbs act as soon as they are encountered. They may not
  5584. be followed by a name.
  5585. (*ACCEPT)
  5586. This verb causes the match to end successfully, skipping the remainder
  5587. of the pattern. However, when it is inside a subpattern that is called
  5588. as a subroutine, only that subpattern is ended successfully. Matching
  5589. then continues at the outer level. If (*ACCEPT) in triggered in a posi-
  5590. tive assertion, the assertion succeeds; in a negative assertion, the
  5591. assertion fails.
  5592. If (*ACCEPT) is inside capturing parentheses, the data so far is cap-
  5593. tured. For example:
  5594. A((?:A|B(*ACCEPT)|C)D)
  5595. This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is cap-
  5596. tured by the outer parentheses.
  5597. (*FAIL) or (*F)
  5598. This verb causes a matching failure, forcing backtracking to occur. It
  5599. is equivalent to (?!) but easier to read. The Perl documentation notes
  5600. that it is probably useful only when combined with (?{}) or (??{}).
  5601. Those are, of course, Perl features that are not present in PCRE. The
  5602. nearest equivalent is the callout feature, as for example in this pat-
  5603. tern:
  5604. a+(?C)(*FAIL)
  5605. A match with the string "aaaa" always fails, but the callout is taken
  5606. before each backtrack happens (in this example, 10 times).
  5607. Recording which path was taken
  5608. There is one verb whose main purpose is to track how a match was
  5609. arrived at, though it also has a secondary use in conjunction with
  5610. advancing the match starting point (see (*SKIP) below).
  5611. (*MARK:NAME) or (*:NAME)
  5612. A name is always required with this verb. There may be as many
  5613. instances of (*MARK) as you like in a pattern, and their names do not
  5614. have to be unique.
  5615. When a match succeeds, the name of the last-encountered (*MARK:NAME),
  5616. (*PRUNE:NAME), or (*THEN:NAME) on the matching path is passed back to
  5617. the caller as described in the section entitled "Extra data for
  5618. pcre_exec()" in the pcreapi documentation. Here is an example of
  5619. pcretest output, where the /K modifier requests the retrieval and out-
  5620. putting of (*MARK) data:
  5621. re> /X(*MARK:A)Y|X(*MARK:B)Z/K
  5622. data> XY
  5623. 0: XY
  5624. MK: A
  5625. XZ
  5626. 0: XZ
  5627. MK: B
  5628. The (*MARK) name is tagged with "MK:" in this output, and in this exam-
  5629. ple it indicates which of the two alternatives matched. This is a more
  5630. efficient way of obtaining this information than putting each alterna-
  5631. tive in its own capturing parentheses.
  5632. If a verb with a name is encountered in a positive assertion that is
  5633. true, the name is recorded and passed back if it is the last-encoun-
  5634. tered. This does not happen for negative assertions or failing positive
  5635. assertions.
  5636. After a partial match or a failed match, the last encountered name in
  5637. the entire match process is returned. For example:
  5638. re> /X(*MARK:A)Y|X(*MARK:B)Z/K
  5639. data> XP
  5640. No match, mark = B
  5641. Note that in this unanchored example the mark is retained from the
  5642. match attempt that started at the letter "X" in the subject. Subsequent
  5643. match attempts starting at "P" and then with an empty string do not get
  5644. as far as the (*MARK) item, but nevertheless do not reset it.
  5645. If you are interested in (*MARK) values after failed matches, you
  5646. should probably set the PCRE_NO_START_OPTIMIZE option (see above) to
  5647. ensure that the match is always attempted.
  5648. Verbs that act after backtracking
  5649. The following verbs do nothing when they are encountered. Matching con-
  5650. tinues with what follows, but if there is no subsequent match, causing
  5651. a backtrack to the verb, a failure is forced. That is, backtracking
  5652. cannot pass to the left of the verb. However, when one of these verbs
  5653. appears inside an atomic group or an assertion that is true, its effect
  5654. is confined to that group, because once the group has been matched,
  5655. there is never any backtracking into it. In this situation, backtrack-
  5656. ing can "jump back" to the left of the entire atomic group or asser-
  5657. tion. (Remember also, as stated above, that this localization also
  5658. applies in subroutine calls.)
  5659. These verbs differ in exactly what kind of failure occurs when back-
  5660. tracking reaches them. The behaviour described below is what happens
  5661. when the verb is not in a subroutine or an assertion. Subsequent sec-
  5662. tions cover these special cases.
  5663. (*COMMIT)
  5664. This verb, which may not be followed by a name, causes the whole match
  5665. to fail outright if there is a later matching failure that causes back-
  5666. tracking to reach it. Even if the pattern is unanchored, no further
  5667. attempts to find a match by advancing the starting point take place. If
  5668. (*COMMIT) is the only backtracking verb that is encountered, once it
  5669. has been passed pcre_exec() is committed to finding a match at the cur-
  5670. rent starting point, or not at all. For example:
  5671. a+(*COMMIT)b
  5672. This matches "xxaab" but not "aacaab". It can be thought of as a kind
  5673. of dynamic anchor, or "I've started, so I must finish." The name of the
  5674. most recently passed (*MARK) in the path is passed back when (*COMMIT)
  5675. forces a match failure.
  5676. If there is more than one backtracking verb in a pattern, a different
  5677. one that follows (*COMMIT) may be triggered first, so merely passing
  5678. (*COMMIT) during a match does not always guarantee that a match must be
  5679. at this starting point.
  5680. Note that (*COMMIT) at the start of a pattern is not the same as an
  5681. anchor, unless PCRE's start-of-match optimizations are turned off, as
  5682. shown in this output from pcretest:
  5683. re> /(*COMMIT)abc/
  5684. data> xyzabc
  5685. 0: abc
  5686. data> xyzabc\Y
  5687. No match
  5688. For this pattern, PCRE knows that any match must start with "a", so the
  5689. optimization skips along the subject to "a" before applying the pattern
  5690. to the first set of data. The match attempt then succeeds. In the sec-
  5691. ond set of data, the escape sequence \Y is interpreted by the pcretest
  5692. program. It causes the PCRE_NO_START_OPTIMIZE option to be set when
  5693. pcre_exec() is called. This disables the optimization that skips along
  5694. to the first character. The pattern is now applied starting at "x", and
  5695. so the (*COMMIT) causes the match to fail without trying any other
  5696. starting points.
  5697. (*PRUNE) or (*PRUNE:NAME)
  5698. This verb causes the match to fail at the current starting position in
  5699. the subject if there is a later matching failure that causes backtrack-
  5700. ing to reach it. If the pattern is unanchored, the normal "bumpalong"
  5701. advance to the next starting character then happens. Backtracking can
  5702. occur as usual to the left of (*PRUNE), before it is reached, or when
  5703. matching to the right of (*PRUNE), but if there is no match to the
  5704. right, backtracking cannot cross (*PRUNE). In simple cases, the use of
  5705. (*PRUNE) is just an alternative to an atomic group or possessive quan-
  5706. tifier, but there are some uses of (*PRUNE) that cannot be expressed in
  5707. any other way. In an anchored pattern (*PRUNE) has the same effect as
  5708. (*COMMIT).
  5709. The behaviour of (*PRUNE:NAME) is the not the same as
  5710. (*MARK:NAME)(*PRUNE). It is like (*MARK:NAME) in that the name is
  5711. remembered for passing back to the caller. However, (*SKIP:NAME)
  5712. searches only for names set with (*MARK).
  5713. (*SKIP)
  5714. This verb, when given without a name, is like (*PRUNE), except that if
  5715. the pattern is unanchored, the "bumpalong" advance is not to the next
  5716. character, but to the position in the subject where (*SKIP) was encoun-
  5717. tered. (*SKIP) signifies that whatever text was matched leading up to
  5718. it cannot be part of a successful match. Consider:
  5719. a+(*SKIP)b
  5720. If the subject is "aaaac...", after the first match attempt fails
  5721. (starting at the first character in the string), the starting point
  5722. skips on to start the next attempt at "c". Note that a possessive quan-
  5723. tifer does not have the same effect as this example; although it would
  5724. suppress backtracking during the first match attempt, the second
  5725. attempt would start at the second character instead of skipping on to
  5726. "c".
  5727. (*SKIP:NAME)
  5728. When (*SKIP) has an associated name, its behaviour is modified. When it
  5729. is triggered, the previous path through the pattern is searched for the
  5730. most recent (*MARK) that has the same name. If one is found, the
  5731. "bumpalong" advance is to the subject position that corresponds to that
  5732. (*MARK) instead of to where (*SKIP) was encountered. If no (*MARK) with
  5733. a matching name is found, the (*SKIP) is ignored.
  5734. Note that (*SKIP:NAME) searches only for names set by (*MARK:NAME). It
  5735. ignores names that are set by (*PRUNE:NAME) or (*THEN:NAME).
  5736. (*THEN) or (*THEN:NAME)
  5737. This verb causes a skip to the next innermost alternative when back-
  5738. tracking reaches it. That is, it cancels any further backtracking
  5739. within the current alternative. Its name comes from the observation
  5740. that it can be used for a pattern-based if-then-else block:
  5741. ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
  5742. If the COND1 pattern matches, FOO is tried (and possibly further items
  5743. after the end of the group if FOO succeeds); on failure, the matcher
  5744. skips to the second alternative and tries COND2, without backtracking
  5745. into COND1. If that succeeds and BAR fails, COND3 is tried. If subse-
  5746. quently BAZ fails, there are no more alternatives, so there is a back-
  5747. track to whatever came before the entire group. If (*THEN) is not
  5748. inside an alternation, it acts like (*PRUNE).
  5749. The behaviour of (*THEN:NAME) is the not the same as
  5750. (*MARK:NAME)(*THEN). It is like (*MARK:NAME) in that the name is
  5751. remembered for passing back to the caller. However, (*SKIP:NAME)
  5752. searches only for names set with (*MARK).
  5753. A subpattern that does not contain a | character is just a part of the
  5754. enclosing alternative; it is not a nested alternation with only one
  5755. alternative. The effect of (*THEN) extends beyond such a subpattern to
  5756. the enclosing alternative. Consider this pattern, where A, B, etc. are
  5757. complex pattern fragments that do not contain any | characters at this
  5758. level:
  5759. A (B(*THEN)C) | D
  5760. If A and B are matched, but there is a failure in C, matching does not
  5761. backtrack into A; instead it moves to the next alternative, that is, D.
  5762. However, if the subpattern containing (*THEN) is given an alternative,
  5763. it behaves differently:
  5764. A (B(*THEN)C | (*FAIL)) | D
  5765. The effect of (*THEN) is now confined to the inner subpattern. After a
  5766. failure in C, matching moves to (*FAIL), which causes the whole subpat-
  5767. tern to fail because there are no more alternatives to try. In this
  5768. case, matching does now backtrack into A.
  5769. Note that a conditional subpattern is not considered as having two
  5770. alternatives, because only one is ever used. In other words, the |
  5771. character in a conditional subpattern has a different meaning. Ignoring
  5772. white space, consider:
  5773. ^.*? (?(?=a) a | b(*THEN)c )
  5774. If the subject is "ba", this pattern does not match. Because .*? is
  5775. ungreedy, it initially matches zero characters. The condition (?=a)
  5776. then fails, the character "b" is matched, but "c" is not. At this
  5777. point, matching does not backtrack to .*? as might perhaps be expected
  5778. from the presence of the | character. The conditional subpattern is
  5779. part of the single alternative that comprises the whole pattern, and so
  5780. the match fails. (If there was a backtrack into .*?, allowing it to
  5781. match "b", the match would succeed.)
  5782. The verbs just described provide four different "strengths" of control
  5783. when subsequent matching fails. (*THEN) is the weakest, carrying on the
  5784. match at the next alternative. (*PRUNE) comes next, failing the match
  5785. at the current starting position, but allowing an advance to the next
  5786. character (for an unanchored pattern). (*SKIP) is similar, except that
  5787. the advance may be more than one character. (*COMMIT) is the strongest,
  5788. causing the entire match to fail.
  5789. More than one backtracking verb
  5790. If more than one backtracking verb is present in a pattern, the one
  5791. that is backtracked onto first acts. For example, consider this pat-
  5792. tern, where A, B, etc. are complex pattern fragments:
  5793. (A(*COMMIT)B(*THEN)C|ABD)
  5794. If A matches but B fails, the backtrack to (*COMMIT) causes the entire
  5795. match to fail. However, if A and B match, but C fails, the backtrack to
  5796. (*THEN) causes the next alternative (ABD) to be tried. This behaviour
  5797. is consistent, but is not always the same as Perl's. It means that if
  5798. two or more backtracking verbs appear in succession, all the the last
  5799. of them has no effect. Consider this example:
  5800. ...(*COMMIT)(*PRUNE)...
  5801. If there is a matching failure to the right, backtracking onto (*PRUNE)
  5802. causes it to be triggered, and its action is taken. There can never be
  5803. a backtrack onto (*COMMIT).
  5804. Backtracking verbs in repeated groups
  5805. PCRE differs from Perl in its handling of backtracking verbs in
  5806. repeated groups. For example, consider:
  5807. /(a(*COMMIT)b)+ac/
  5808. If the subject is "abac", Perl matches, but PCRE fails because the
  5809. (*COMMIT) in the second repeat of the group acts.
  5810. Backtracking verbs in assertions
  5811. (*FAIL) in an assertion has its normal effect: it forces an immediate
  5812. backtrack.
  5813. (*ACCEPT) in a positive assertion causes the assertion to succeed with-
  5814. out any further processing. In a negative assertion, (*ACCEPT) causes
  5815. the assertion to fail without any further processing.
  5816. The other backtracking verbs are not treated specially if they appear
  5817. in a positive assertion. In particular, (*THEN) skips to the next
  5818. alternative in the innermost enclosing group that has alternations,
  5819. whether or not this is within the assertion.
  5820. Negative assertions are, however, different, in order to ensure that
  5821. changing a positive assertion into a negative assertion changes its
  5822. result. Backtracking into (*COMMIT), (*SKIP), or (*PRUNE) causes a neg-
  5823. ative assertion to be true, without considering any further alternative
  5824. branches in the assertion. Backtracking into (*THEN) causes it to skip
  5825. to the next enclosing alternative within the assertion (the normal be-
  5826. haviour), but if the assertion does not have such an alternative,
  5827. (*THEN) behaves like (*PRUNE).
  5828. Backtracking verbs in subroutines
  5829. These behaviours occur whether or not the subpattern is called recur-
  5830. sively. Perl's treatment of subroutines is different in some cases.
  5831. (*FAIL) in a subpattern called as a subroutine has its normal effect:
  5832. it forces an immediate backtrack.
  5833. (*ACCEPT) in a subpattern called as a subroutine causes the subroutine
  5834. match to succeed without any further processing. Matching then contin-
  5835. ues after the subroutine call.
  5836. (*COMMIT), (*SKIP), and (*PRUNE) in a subpattern called as a subroutine
  5837. cause the subroutine match to fail.
  5838. (*THEN) skips to the next alternative in the innermost enclosing group
  5839. within the subpattern that has alternatives. If there is no such group
  5840. within the subpattern, (*THEN) causes the subroutine match to fail.
  5841. SEE ALSO
  5842. pcreapi(3), pcrecallout(3), pcrematching(3), pcresyntax(3), pcre(3),
  5843. pcre16(3), pcre32(3).
  5844. AUTHOR
  5845. Philip Hazel
  5846. University Computing Service
  5847. Cambridge CB2 3QH, England.
  5848. REVISION
  5849. Last updated: 23 October 2016
  5850. Copyright (c) 1997-2016 University of Cambridge.
  5851. ------------------------------------------------------------------------------
  5852. PCRESYNTAX(3) Library Functions Manual PCRESYNTAX(3)
  5853. NAME
  5854. PCRE - Perl-compatible regular expressions
  5855. PCRE REGULAR EXPRESSION SYNTAX SUMMARY
  5856. The full syntax and semantics of the regular expressions that are sup-
  5857. ported by PCRE are described in the pcrepattern documentation. This
  5858. document contains a quick-reference summary of the syntax.
  5859. QUOTING
  5860. \x where x is non-alphanumeric is a literal x
  5861. \Q...\E treat enclosed characters as literal
  5862. CHARACTERS
  5863. \a alarm, that is, the BEL character (hex 07)
  5864. \cx "control-x", where x is any ASCII character
  5865. \e escape (hex 1B)
  5866. \f form feed (hex 0C)
  5867. \n newline (hex 0A)
  5868. \r carriage return (hex 0D)
  5869. \t tab (hex 09)
  5870. \0dd character with octal code 0dd
  5871. \ddd character with octal code ddd, or backreference
  5872. \o{ddd..} character with octal code ddd..
  5873. \xhh character with hex code hh
  5874. \x{hhh..} character with hex code hhh..
  5875. Note that \0dd is always an octal code, and that \8 and \9 are the lit-
  5876. eral characters "8" and "9".
  5877. CHARACTER TYPES
  5878. . any character except newline;
  5879. in dotall mode, any character whatsoever
  5880. \C one data unit, even in UTF mode (best avoided)
  5881. \d a decimal digit
  5882. \D a character that is not a decimal digit
  5883. \h a horizontal white space character
  5884. \H a character that is not a horizontal white space character
  5885. \N a character that is not a newline
  5886. \p{xx} a character with the xx property
  5887. \P{xx} a character without the xx property
  5888. \R a newline sequence
  5889. \s a white space character
  5890. \S a character that is not a white space character
  5891. \v a vertical white space character
  5892. \V a character that is not a vertical white space character
  5893. \w a "word" character
  5894. \W a "non-word" character
  5895. \X a Unicode extended grapheme cluster
  5896. By default, \d, \s, and \w match only ASCII characters, even in UTF-8
  5897. mode or in the 16- bit and 32-bit libraries. However, if locale-spe-
  5898. cific matching is happening, \s and \w may also match characters with
  5899. code points in the range 128-255. If the PCRE_UCP option is set, the
  5900. behaviour of these escape sequences is changed to use Unicode proper-
  5901. ties and they match many more characters.
  5902. GENERAL CATEGORY PROPERTIES FOR \p and \P
  5903. C Other
  5904. Cc Control
  5905. Cf Format
  5906. Cn Unassigned
  5907. Co Private use
  5908. Cs Surrogate
  5909. L Letter
  5910. Ll Lower case letter
  5911. Lm Modifier letter
  5912. Lo Other letter
  5913. Lt Title case letter
  5914. Lu Upper case letter
  5915. L& Ll, Lu, or Lt
  5916. M Mark
  5917. Mc Spacing mark
  5918. Me Enclosing mark
  5919. Mn Non-spacing mark
  5920. N Number
  5921. Nd Decimal number
  5922. Nl Letter number
  5923. No Other number
  5924. P Punctuation
  5925. Pc Connector punctuation
  5926. Pd Dash punctuation
  5927. Pe Close punctuation
  5928. Pf Final punctuation
  5929. Pi Initial punctuation
  5930. Po Other punctuation
  5931. Ps Open punctuation
  5932. S Symbol
  5933. Sc Currency symbol
  5934. Sk Modifier symbol
  5935. Sm Mathematical symbol
  5936. So Other symbol
  5937. Z Separator
  5938. Zl Line separator
  5939. Zp Paragraph separator
  5940. Zs Space separator
  5941. PCRE SPECIAL CATEGORY PROPERTIES FOR \p and \P
  5942. Xan Alphanumeric: union of properties L and N
  5943. Xps POSIX space: property Z or tab, NL, VT, FF, CR
  5944. Xsp Perl space: property Z or tab, NL, VT, FF, CR
  5945. Xuc Univerally-named character: one that can be
  5946. represented by a Universal Character Name
  5947. Xwd Perl word: property Xan or underscore
  5948. Perl and POSIX space are now the same. Perl added VT to its space char-
  5949. acter set at release 5.18 and PCRE changed at release 8.34.
  5950. SCRIPT NAMES FOR \p AND \P
  5951. Arabic, Armenian, Avestan, Balinese, Bamum, Bassa_Vah, Batak, Bengali,
  5952. Bopomofo, Brahmi, Braille, Buginese, Buhid, Canadian_Aboriginal, Car-
  5953. ian, Caucasian_Albanian, Chakma, Cham, Cherokee, Common, Coptic, Cunei-
  5954. form, Cypriot, Cyrillic, Deseret, Devanagari, Duployan, Egyptian_Hiero-
  5955. glyphs, Elbasan, Ethiopic, Georgian, Glagolitic, Gothic, Grantha,
  5956. Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hiragana,
  5957. Imperial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscrip-
  5958. tional_Parthian, Javanese, Kaithi, Kannada, Katakana, Kayah_Li,
  5959. Kharoshthi, Khmer, Khojki, Khudawadi, Lao, Latin, Lepcha, Limbu, Lin-
  5960. ear_A, Linear_B, Lisu, Lycian, Lydian, Mahajani, Malayalam, Mandaic,
  5961. Manichaean, Meetei_Mayek, Mende_Kikakui, Meroitic_Cursive,
  5962. Meroitic_Hieroglyphs, Miao, Modi, Mongolian, Mro, Myanmar, Nabataean,
  5963. New_Tai_Lue, Nko, Ogham, Ol_Chiki, Old_Italic, Old_North_Arabian,
  5964. Old_Permic, Old_Persian, Old_South_Arabian, Old_Turkic, Oriya, Osmanya,
  5965. Pahawh_Hmong, Palmyrene, Pau_Cin_Hau, Phags_Pa, Phoenician,
  5966. Psalter_Pahlavi, Rejang, Runic, Samaritan, Saurashtra, Sharada, Sha-
  5967. vian, Siddham, Sinhala, Sora_Sompeng, Sundanese, Syloti_Nagri, Syriac,
  5968. Tagalog, Tagbanwa, Tai_Le, Tai_Tham, Tai_Viet, Takri, Tamil, Telugu,
  5969. Thaana, Thai, Tibetan, Tifinagh, Tirhuta, Ugaritic, Vai, Warang_Citi,
  5970. Yi.
  5971. CHARACTER CLASSES
  5972. [...] positive character class
  5973. [^...] negative character class
  5974. [x-y] range (can be used for hex characters)
  5975. [[:xxx:]] positive POSIX named set
  5976. [[:^xxx:]] negative POSIX named set
  5977. alnum alphanumeric
  5978. alpha alphabetic
  5979. ascii 0-127
  5980. blank space or tab
  5981. cntrl control character
  5982. digit decimal digit
  5983. graph printing, excluding space
  5984. lower lower case letter
  5985. print printing, including space
  5986. punct printing, excluding alphanumeric
  5987. space white space
  5988. upper upper case letter
  5989. word same as \w
  5990. xdigit hexadecimal digit
  5991. In PCRE, POSIX character set names recognize only ASCII characters by
  5992. default, but some of them use Unicode properties if PCRE_UCP is set.
  5993. You can use \Q...\E inside a character class.
  5994. QUANTIFIERS
  5995. ? 0 or 1, greedy
  5996. ?+ 0 or 1, possessive
  5997. ?? 0 or 1, lazy
  5998. * 0 or more, greedy
  5999. *+ 0 or more, possessive
  6000. *? 0 or more, lazy
  6001. + 1 or more, greedy
  6002. ++ 1 or more, possessive
  6003. +? 1 or more, lazy
  6004. {n} exactly n
  6005. {n,m} at least n, no more than m, greedy
  6006. {n,m}+ at least n, no more than m, possessive
  6007. {n,m}? at least n, no more than m, lazy
  6008. {n,} n or more, greedy
  6009. {n,}+ n or more, possessive
  6010. {n,}? n or more, lazy
  6011. ANCHORS AND SIMPLE ASSERTIONS
  6012. \b word boundary
  6013. \B not a word boundary
  6014. ^ start of subject
  6015. also after internal newline in multiline mode
  6016. \A start of subject
  6017. $ end of subject
  6018. also before newline at end of subject
  6019. also before internal newline in multiline mode
  6020. \Z end of subject
  6021. also before newline at end of subject
  6022. \z end of subject
  6023. \G first matching position in subject
  6024. MATCH POINT RESET
  6025. \K reset start of match
  6026. \K is honoured in positive assertions, but ignored in negative ones.
  6027. ALTERNATION
  6028. expr|expr|expr...
  6029. CAPTURING
  6030. (...) capturing group
  6031. (?<name>...) named capturing group (Perl)
  6032. (?'name'...) named capturing group (Perl)
  6033. (?P<name>...) named capturing group (Python)
  6034. (?:...) non-capturing group
  6035. (?|...) non-capturing group; reset group numbers for
  6036. capturing groups in each alternative
  6037. ATOMIC GROUPS
  6038. (?>...) atomic, non-capturing group
  6039. COMMENT
  6040. (?#....) comment (not nestable)
  6041. OPTION SETTING
  6042. (?i) caseless
  6043. (?J) allow duplicate names
  6044. (?m) multiline
  6045. (?s) single line (dotall)
  6046. (?U) default ungreedy (lazy)
  6047. (?x) extended (ignore white space)
  6048. (?-...) unset option(s)
  6049. The following are recognized only at the very start of a pattern or
  6050. after one of the newline or \R options with similar syntax. More than
  6051. one of them may appear.
  6052. (*LIMIT_MATCH=d) set the match limit to d (decimal number)
  6053. (*LIMIT_RECURSION=d) set the recursion limit to d (decimal number)
  6054. (*NO_AUTO_POSSESS) no auto-possessification (PCRE_NO_AUTO_POSSESS)
  6055. (*NO_START_OPT) no start-match optimization (PCRE_NO_START_OPTIMIZE)
  6056. (*UTF8) set UTF-8 mode: 8-bit library (PCRE_UTF8)
  6057. (*UTF16) set UTF-16 mode: 16-bit library (PCRE_UTF16)
  6058. (*UTF32) set UTF-32 mode: 32-bit library (PCRE_UTF32)
  6059. (*UTF) set appropriate UTF mode for the library in use
  6060. (*UCP) set PCRE_UCP (use Unicode properties for \d etc)
  6061. Note that LIMIT_MATCH and LIMIT_RECURSION can only reduce the value of
  6062. the limits set by the caller of pcre_exec(), not increase them.
  6063. NEWLINE CONVENTION
  6064. These are recognized only at the very start of the pattern or after
  6065. option settings with a similar syntax.
  6066. (*CR) carriage return only
  6067. (*LF) linefeed only
  6068. (*CRLF) carriage return followed by linefeed
  6069. (*ANYCRLF) all three of the above
  6070. (*ANY) any Unicode newline sequence
  6071. WHAT \R MATCHES
  6072. These are recognized only at the very start of the pattern or after
  6073. option setting with a similar syntax.
  6074. (*BSR_ANYCRLF) CR, LF, or CRLF
  6075. (*BSR_UNICODE) any Unicode newline sequence
  6076. LOOKAHEAD AND LOOKBEHIND ASSERTIONS
  6077. (?=...) positive look ahead
  6078. (?!...) negative look ahead
  6079. (?<=...) positive look behind
  6080. (?<!...) negative look behind
  6081. Each top-level branch of a look behind must be of a fixed length.
  6082. BACKREFERENCES
  6083. \n reference by number (can be ambiguous)
  6084. \gn reference by number
  6085. \g{n} reference by number
  6086. \g{-n} relative reference by number
  6087. \k<name> reference by name (Perl)
  6088. \k'name' reference by name (Perl)
  6089. \g{name} reference by name (Perl)
  6090. \k{name} reference by name (.NET)
  6091. (?P=name) reference by name (Python)
  6092. SUBROUTINE REFERENCES (POSSIBLY RECURSIVE)
  6093. (?R) recurse whole pattern
  6094. (?n) call subpattern by absolute number
  6095. (?+n) call subpattern by relative number
  6096. (?-n) call subpattern by relative number
  6097. (?&name) call subpattern by name (Perl)
  6098. (?P>name) call subpattern by name (Python)
  6099. \g<name> call subpattern by name (Oniguruma)
  6100. \g'name' call subpattern by name (Oniguruma)
  6101. \g<n> call subpattern by absolute number (Oniguruma)
  6102. \g'n' call subpattern by absolute number (Oniguruma)
  6103. \g<+n> call subpattern by relative number (PCRE extension)
  6104. \g'+n' call subpattern by relative number (PCRE extension)
  6105. \g<-n> call subpattern by relative number (PCRE extension)
  6106. \g'-n' call subpattern by relative number (PCRE extension)
  6107. CONDITIONAL PATTERNS
  6108. (?(condition)yes-pattern)
  6109. (?(condition)yes-pattern|no-pattern)
  6110. (?(n)... absolute reference condition
  6111. (?(+n)... relative reference condition
  6112. (?(-n)... relative reference condition
  6113. (?(<name>)... named reference condition (Perl)
  6114. (?('name')... named reference condition (Perl)
  6115. (?(name)... named reference condition (PCRE)
  6116. (?(R)... overall recursion condition
  6117. (?(Rn)... specific group recursion condition
  6118. (?(R&name)... specific recursion condition
  6119. (?(DEFINE)... define subpattern for reference
  6120. (?(assert)... assertion condition
  6121. BACKTRACKING CONTROL
  6122. The following act immediately they are reached:
  6123. (*ACCEPT) force successful match
  6124. (*FAIL) force backtrack; synonym (*F)
  6125. (*MARK:NAME) set name to be passed back; synonym (*:NAME)
  6126. The following act only when a subsequent match failure causes a back-
  6127. track to reach them. They all force a match failure, but they differ in
  6128. what happens afterwards. Those that advance the start-of-match point do
  6129. so only if the pattern is not anchored.
  6130. (*COMMIT) overall failure, no advance of starting point
  6131. (*PRUNE) advance to next starting character
  6132. (*PRUNE:NAME) equivalent to (*MARK:NAME)(*PRUNE)
  6133. (*SKIP) advance to current matching position
  6134. (*SKIP:NAME) advance to position corresponding to an earlier
  6135. (*MARK:NAME); if not found, the (*SKIP) is ignored
  6136. (*THEN) local failure, backtrack to next alternation
  6137. (*THEN:NAME) equivalent to (*MARK:NAME)(*THEN)
  6138. CALLOUTS
  6139. (?C) callout
  6140. (?Cn) callout with data n
  6141. SEE ALSO
  6142. pcrepattern(3), pcreapi(3), pcrecallout(3), pcrematching(3), pcre(3).
  6143. AUTHOR
  6144. Philip Hazel
  6145. University Computing Service
  6146. Cambridge CB2 3QH, England.
  6147. REVISION
  6148. Last updated: 08 January 2014
  6149. Copyright (c) 1997-2014 University of Cambridge.
  6150. ------------------------------------------------------------------------------
  6151. PCREUNICODE(3) Library Functions Manual PCREUNICODE(3)
  6152. NAME
  6153. PCRE - Perl-compatible regular expressions
  6154. UTF-8, UTF-16, UTF-32, AND UNICODE PROPERTY SUPPORT
  6155. As well as UTF-8 support, PCRE also supports UTF-16 (from release 8.30)
  6156. and UTF-32 (from release 8.32), by means of two additional libraries.
  6157. They can be built as well as, or instead of, the 8-bit library.
  6158. UTF-8 SUPPORT
  6159. In order process UTF-8 strings, you must build PCRE's 8-bit library
  6160. with UTF support, and, in addition, you must call pcre_compile() with
  6161. the PCRE_UTF8 option flag, or the pattern must start with the sequence
  6162. (*UTF8) or (*UTF). When either of these is the case, both the pattern
  6163. and any subject strings that are matched against it are treated as
  6164. UTF-8 strings instead of strings of individual 1-byte characters.
  6165. UTF-16 AND UTF-32 SUPPORT
  6166. In order process UTF-16 or UTF-32 strings, you must build PCRE's 16-bit
  6167. or 32-bit library with UTF support, and, in addition, you must call
  6168. pcre16_compile() or pcre32_compile() with the PCRE_UTF16 or PCRE_UTF32
  6169. option flag, as appropriate. Alternatively, the pattern must start with
  6170. the sequence (*UTF16), (*UTF32), as appropriate, or (*UTF), which can
  6171. be used with either library. When UTF mode is set, both the pattern and
  6172. any subject strings that are matched against it are treated as UTF-16
  6173. or UTF-32 strings instead of strings of individual 16-bit or 32-bit
  6174. characters.
  6175. UTF SUPPORT OVERHEAD
  6176. If you compile PCRE with UTF support, but do not use it at run time,
  6177. the library will be a bit bigger, but the additional run time overhead
  6178. is limited to testing the PCRE_UTF[8|16|32] flag occasionally, so
  6179. should not be very big.
  6180. UNICODE PROPERTY SUPPORT
  6181. If PCRE is built with Unicode character property support (which implies
  6182. UTF support), the escape sequences \p{..}, \P{..}, and \X can be used.
  6183. The available properties that can be tested are limited to the general
  6184. category properties such as Lu for an upper case letter or Nd for a
  6185. decimal number, the Unicode script names such as Arabic or Han, and the
  6186. derived properties Any and L&. Full lists is given in the pcrepattern
  6187. and pcresyntax documentation. Only the short names for properties are
  6188. supported. For example, \p{L} matches a letter. Its Perl synonym,
  6189. \p{Letter}, is not supported. Furthermore, in Perl, many properties
  6190. may optionally be prefixed by "Is", for compatibility with Perl 5.6.
  6191. PCRE does not support this.
  6192. Validity of UTF-8 strings
  6193. When you set the PCRE_UTF8 flag, the byte strings passed as patterns
  6194. and subjects are (by default) checked for validity on entry to the rel-
  6195. evant functions. The entire string is checked before any other process-
  6196. ing takes place. From release 7.3 of PCRE, the check is according the
  6197. rules of RFC 3629, which are themselves derived from the Unicode speci-
  6198. fication. Earlier releases of PCRE followed the rules of RFC 2279,
  6199. which allows the full range of 31-bit values (0 to 0x7FFFFFFF). The
  6200. current check allows only values in the range U+0 to U+10FFFF, exclud-
  6201. ing the surrogate area. (From release 8.33 the so-called "non-charac-
  6202. ter" code points are no longer excluded because Unicode corrigendum #9
  6203. makes it clear that they should not be.)
  6204. Characters in the "Surrogate Area" of Unicode are reserved for use by
  6205. UTF-16, where they are used in pairs to encode codepoints with values
  6206. greater than 0xFFFF. The code points that are encoded by UTF-16 pairs
  6207. are available independently in the UTF-8 and UTF-32 encodings. (In
  6208. other words, the whole surrogate thing is a fudge for UTF-16 which
  6209. unfortunately messes up UTF-8 and UTF-32.)
  6210. If an invalid UTF-8 string is passed to PCRE, an error return is given.
  6211. At compile time, the only additional information is the offset to the
  6212. first byte of the failing character. The run-time functions pcre_exec()
  6213. and pcre_dfa_exec() also pass back this information, as well as a more
  6214. detailed reason code if the caller has provided memory in which to do
  6215. this.
  6216. In some situations, you may already know that your strings are valid,
  6217. and therefore want to skip these checks in order to improve perfor-
  6218. mance, for example in the case of a long subject string that is being
  6219. scanned repeatedly. If you set the PCRE_NO_UTF8_CHECK flag at compile
  6220. time or at run time, PCRE assumes that the pattern or subject it is
  6221. given (respectively) contains only valid UTF-8 codes. In this case, it
  6222. does not diagnose an invalid UTF-8 string.
  6223. Note that passing PCRE_NO_UTF8_CHECK to pcre_compile() just disables
  6224. the check for the pattern; it does not also apply to subject strings.
  6225. If you want to disable the check for a subject string you must pass
  6226. this option to pcre_exec() or pcre_dfa_exec().
  6227. If you pass an invalid UTF-8 string when PCRE_NO_UTF8_CHECK is set, the
  6228. result is undefined and your program may crash.
  6229. Validity of UTF-16 strings
  6230. When you set the PCRE_UTF16 flag, the strings of 16-bit data units that
  6231. are passed as patterns and subjects are (by default) checked for valid-
  6232. ity on entry to the relevant functions. Values other than those in the
  6233. surrogate range U+D800 to U+DFFF are independent code points. Values in
  6234. the surrogate range must be used in pairs in the correct manner.
  6235. If an invalid UTF-16 string is passed to PCRE, an error return is
  6236. given. At compile time, the only additional information is the offset
  6237. to the first data unit of the failing character. The run-time functions
  6238. pcre16_exec() and pcre16_dfa_exec() also pass back this information, as
  6239. well as a more detailed reason code if the caller has provided memory
  6240. in which to do this.
  6241. In some situations, you may already know that your strings are valid,
  6242. and therefore want to skip these checks in order to improve perfor-
  6243. mance. If you set the PCRE_NO_UTF16_CHECK flag at compile time or at
  6244. run time, PCRE assumes that the pattern or subject it is given (respec-
  6245. tively) contains only valid UTF-16 sequences. In this case, it does not
  6246. diagnose an invalid UTF-16 string. However, if an invalid string is
  6247. passed, the result is undefined.
  6248. Validity of UTF-32 strings
  6249. When you set the PCRE_UTF32 flag, the strings of 32-bit data units that
  6250. are passed as patterns and subjects are (by default) checked for valid-
  6251. ity on entry to the relevant functions. This check allows only values
  6252. in the range U+0 to U+10FFFF, excluding the surrogate area U+D800 to
  6253. U+DFFF.
  6254. If an invalid UTF-32 string is passed to PCRE, an error return is
  6255. given. At compile time, the only additional information is the offset
  6256. to the first data unit of the failing character. The run-time functions
  6257. pcre32_exec() and pcre32_dfa_exec() also pass back this information, as
  6258. well as a more detailed reason code if the caller has provided memory
  6259. in which to do this.
  6260. In some situations, you may already know that your strings are valid,
  6261. and therefore want to skip these checks in order to improve perfor-
  6262. mance. If you set the PCRE_NO_UTF32_CHECK flag at compile time or at
  6263. run time, PCRE assumes that the pattern or subject it is given (respec-
  6264. tively) contains only valid UTF-32 sequences. In this case, it does not
  6265. diagnose an invalid UTF-32 string. However, if an invalid string is
  6266. passed, the result is undefined.
  6267. General comments about UTF modes
  6268. 1. Codepoints less than 256 can be specified in patterns by either
  6269. braced or unbraced hexadecimal escape sequences (for example, \x{b3} or
  6270. \xb3). Larger values have to use braced sequences.
  6271. 2. Octal numbers up to \777 are recognized, and in UTF-8 mode they
  6272. match two-byte characters for values greater than \177.
  6273. 3. Repeat quantifiers apply to complete UTF characters, not to individ-
  6274. ual data units, for example: \x{100}{3}.
  6275. 4. The dot metacharacter matches one UTF character instead of a single
  6276. data unit.
  6277. 5. The escape sequence \C can be used to match a single byte in UTF-8
  6278. mode, or a single 16-bit data unit in UTF-16 mode, or a single 32-bit
  6279. data unit in UTF-32 mode, but its use can lead to some strange effects
  6280. because it breaks up multi-unit characters (see the description of \C
  6281. in the pcrepattern documentation). The use of \C is not supported in
  6282. the alternative matching function pcre[16|32]_dfa_exec(), nor is it
  6283. supported in UTF mode by the JIT optimization of pcre[16|32]_exec(). If
  6284. JIT optimization is requested for a UTF pattern that contains \C, it
  6285. will not succeed, and so the matching will be carried out by the normal
  6286. interpretive function.
  6287. 6. The character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly
  6288. test characters of any code value, but, by default, the characters that
  6289. PCRE recognizes as digits, spaces, or word characters remain the same
  6290. set as in non-UTF mode, all with values less than 256. This remains
  6291. true even when PCRE is built to include Unicode property support,
  6292. because to do otherwise would slow down PCRE in many common cases. Note
  6293. in particular that this applies to \b and \B, because they are defined
  6294. in terms of \w and \W. If you really want to test for a wider sense of,
  6295. say, "digit", you can use explicit Unicode property tests such as
  6296. \p{Nd}. Alternatively, if you set the PCRE_UCP option, the way that the
  6297. character escapes work is changed so that Unicode properties are used
  6298. to determine which characters match. There are more details in the sec-
  6299. tion on generic character types in the pcrepattern documentation.
  6300. 7. Similarly, characters that match the POSIX named character classes
  6301. are all low-valued characters, unless the PCRE_UCP option is set.
  6302. 8. However, the horizontal and vertical white space matching escapes
  6303. (\h, \H, \v, and \V) do match all the appropriate Unicode characters,
  6304. whether or not PCRE_UCP is set.
  6305. 9. Case-insensitive matching applies only to characters whose values
  6306. are less than 128, unless PCRE is built with Unicode property support.
  6307. A few Unicode characters such as Greek sigma have more than two code-
  6308. points that are case-equivalent. Up to and including PCRE release 8.31,
  6309. only one-to-one case mappings were supported, but later releases (with
  6310. Unicode property support) do treat as case-equivalent all versions of
  6311. characters such as Greek sigma.
  6312. AUTHOR
  6313. Philip Hazel
  6314. University Computing Service
  6315. Cambridge CB2 3QH, England.
  6316. REVISION
  6317. Last updated: 27 February 2013
  6318. Copyright (c) 1997-2013 University of Cambridge.
  6319. ------------------------------------------------------------------------------
  6320. PCREJIT(3) Library Functions Manual PCREJIT(3)
  6321. NAME
  6322. PCRE - Perl-compatible regular expressions
  6323. PCRE JUST-IN-TIME COMPILER SUPPORT
  6324. Just-in-time compiling is a heavyweight optimization that can greatly
  6325. speed up pattern matching. However, it comes at the cost of extra pro-
  6326. cessing before the match is performed. Therefore, it is of most benefit
  6327. when the same pattern is going to be matched many times. This does not
  6328. necessarily mean many calls of a matching function; if the pattern is
  6329. not anchored, matching attempts may take place many times at various
  6330. positions in the subject, even for a single call. Therefore, if the
  6331. subject string is very long, it may still pay to use JIT for one-off
  6332. matches.
  6333. JIT support applies only to the traditional Perl-compatible matching
  6334. function. It does not apply when the DFA matching function is being
  6335. used. The code for this support was written by Zoltan Herczeg.
  6336. 8-BIT, 16-BIT AND 32-BIT SUPPORT
  6337. JIT support is available for all of the 8-bit, 16-bit and 32-bit PCRE
  6338. libraries. To keep this documentation simple, only the 8-bit interface
  6339. is described in what follows. If you are using the 16-bit library, sub-
  6340. stitute the 16-bit functions and 16-bit structures (for example,
  6341. pcre16_jit_stack instead of pcre_jit_stack). If you are using the
  6342. 32-bit library, substitute the 32-bit functions and 32-bit structures
  6343. (for example, pcre32_jit_stack instead of pcre_jit_stack).
  6344. AVAILABILITY OF JIT SUPPORT
  6345. JIT support is an optional feature of PCRE. The "configure" option
  6346. --enable-jit (or equivalent CMake option) must be set when PCRE is
  6347. built if you want to use JIT. The support is limited to the following
  6348. hardware platforms:
  6349. ARM v5, v7, and Thumb2
  6350. Intel x86 32-bit and 64-bit
  6351. MIPS 32-bit
  6352. Power PC 32-bit and 64-bit
  6353. SPARC 32-bit (experimental)
  6354. If --enable-jit is set on an unsupported platform, compilation fails.
  6355. A program that is linked with PCRE 8.20 or later can tell if JIT sup-
  6356. port is available by calling pcre_config() with the PCRE_CONFIG_JIT
  6357. option. The result is 1 when JIT is available, and 0 otherwise. How-
  6358. ever, a simple program does not need to check this in order to use JIT.
  6359. The normal API is implemented in a way that falls back to the interpre-
  6360. tive code if JIT is not available. For programs that need the best pos-
  6361. sible performance, there is also a "fast path" API that is JIT-spe-
  6362. cific.
  6363. If your program may sometimes be linked with versions of PCRE that are
  6364. older than 8.20, but you want to use JIT when it is available, you can
  6365. test the values of PCRE_MAJOR and PCRE_MINOR, or the existence of a JIT
  6366. macro such as PCRE_CONFIG_JIT, for compile-time control of your code.
  6367. Also beware that the pcre_jit_exec() function was not available at all
  6368. before 8.32, and may not be available at all if PCRE isn't compiled
  6369. with --enable-jit. See the "JIT FAST PATH API" section below for
  6370. details.
  6371. SIMPLE USE OF JIT
  6372. You have to do two things to make use of the JIT support in the sim-
  6373. plest way:
  6374. (1) Call pcre_study() with the PCRE_STUDY_JIT_COMPILE option for
  6375. each compiled pattern, and pass the resulting pcre_extra block to
  6376. pcre_exec().
  6377. (2) Use pcre_free_study() to free the pcre_extra block when it is
  6378. no longer needed, instead of just freeing it yourself. This
  6379. ensures that
  6380. any JIT data is also freed.
  6381. For a program that may be linked with pre-8.20 versions of PCRE, you
  6382. can insert
  6383. #ifndef PCRE_STUDY_JIT_COMPILE
  6384. #define PCRE_STUDY_JIT_COMPILE 0
  6385. #endif
  6386. so that no option is passed to pcre_study(), and then use something
  6387. like this to free the study data:
  6388. #ifdef PCRE_CONFIG_JIT
  6389. pcre_free_study(study_ptr);
  6390. #else
  6391. pcre_free(study_ptr);
  6392. #endif
  6393. PCRE_STUDY_JIT_COMPILE requests the JIT compiler to generate code for
  6394. complete matches. If you want to run partial matches using the
  6395. PCRE_PARTIAL_HARD or PCRE_PARTIAL_SOFT options of pcre_exec(), you
  6396. should set one or both of the following options in addition to, or
  6397. instead of, PCRE_STUDY_JIT_COMPILE when you call pcre_study():
  6398. PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
  6399. PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
  6400. If using pcre_jit_exec() and supporting a pre-8.32 version of PCRE, you
  6401. can insert:
  6402. #if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
  6403. pcre_jit_exec(...);
  6404. #else
  6405. pcre_exec(...)
  6406. #endif
  6407. but as described in the "JIT FAST PATH API" section below this assumes
  6408. version 8.32 and later are compiled with --enable-jit, which may break.
  6409. The JIT compiler generates different optimized code for each of the
  6410. three modes (normal, soft partial, hard partial). When pcre_exec() is
  6411. called, the appropriate code is run if it is available. Otherwise, the
  6412. pattern is matched using interpretive code.
  6413. In some circumstances you may need to call additional functions. These
  6414. are described in the section entitled "Controlling the JIT stack"
  6415. below.
  6416. If JIT support is not available, PCRE_STUDY_JIT_COMPILE etc. are
  6417. ignored, and no JIT data is created. Otherwise, the compiled pattern is
  6418. passed to the JIT compiler, which turns it into machine code that exe-
  6419. cutes much faster than the normal interpretive code. When pcre_exec()
  6420. is passed a pcre_extra block containing a pointer to JIT code of the
  6421. appropriate mode (normal or hard/soft partial), it obeys that code
  6422. instead of running the interpreter. The result is identical, but the
  6423. compiled JIT code runs much faster.
  6424. There are some pcre_exec() options that are not supported for JIT exe-
  6425. cution. There are also some pattern items that JIT cannot handle.
  6426. Details are given below. In both cases, execution automatically falls
  6427. back to the interpretive code. If you want to know whether JIT was
  6428. actually used for a particular match, you should arrange for a JIT
  6429. callback function to be set up as described in the section entitled
  6430. "Controlling the JIT stack" below, even if you do not need to supply a
  6431. non-default JIT stack. Such a callback function is called whenever JIT
  6432. code is about to be obeyed. If the execution options are not right for
  6433. JIT execution, the callback function is not obeyed.
  6434. If the JIT compiler finds an unsupported item, no JIT data is gener-
  6435. ated. You can find out if JIT execution is available after studying a
  6436. pattern by calling pcre_fullinfo() with the PCRE_INFO_JIT option. A
  6437. result of 1 means that JIT compilation was successful. A result of 0
  6438. means that JIT support is not available, or the pattern was not studied
  6439. with PCRE_STUDY_JIT_COMPILE etc., or the JIT compiler was not able to
  6440. handle the pattern.
  6441. Once a pattern has been studied, with or without JIT, it can be used as
  6442. many times as you like for matching different subject strings.
  6443. UNSUPPORTED OPTIONS AND PATTERN ITEMS
  6444. The only pcre_exec() options that are supported for JIT execution are
  6445. PCRE_NO_UTF8_CHECK, PCRE_NO_UTF16_CHECK, PCRE_NO_UTF32_CHECK, PCRE_NOT-
  6446. BOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART, PCRE_PAR-
  6447. TIAL_HARD, and PCRE_PARTIAL_SOFT.
  6448. The only unsupported pattern items are \C (match a single data unit)
  6449. when running in a UTF mode, and a callout immediately before an asser-
  6450. tion condition in a conditional group.
  6451. RETURN VALUES FROM JIT EXECUTION
  6452. When a pattern is matched using JIT execution, the return values are
  6453. the same as those given by the interpretive pcre_exec() code, with the
  6454. addition of one new error code: PCRE_ERROR_JIT_STACKLIMIT. This means
  6455. that the memory used for the JIT stack was insufficient. See "Control-
  6456. ling the JIT stack" below for a discussion of JIT stack usage. For com-
  6457. patibility with the interpretive pcre_exec() code, no more than two-
  6458. thirds of the ovector argument is used for passing back captured sub-
  6459. strings.
  6460. The error code PCRE_ERROR_MATCHLIMIT is returned by the JIT code if
  6461. searching a very large pattern tree goes on for too long, as it is in
  6462. the same circumstance when JIT is not used, but the details of exactly
  6463. what is counted are not the same. The PCRE_ERROR_RECURSIONLIMIT error
  6464. code is never returned by JIT execution.
  6465. SAVING AND RESTORING COMPILED PATTERNS
  6466. The code that is generated by the JIT compiler is architecture-spe-
  6467. cific, and is also position dependent. For those reasons it cannot be
  6468. saved (in a file or database) and restored later like the bytecode and
  6469. other data of a compiled pattern. Saving and restoring compiled pat-
  6470. terns is not something many people do. More detail about this facility
  6471. is given in the pcreprecompile documentation. It should be possible to
  6472. run pcre_study() on a saved and restored pattern, and thereby recreate
  6473. the JIT data, but because JIT compilation uses significant resources,
  6474. it is probably not worth doing this; you might as well recompile the
  6475. original pattern.
  6476. CONTROLLING THE JIT STACK
  6477. When the compiled JIT code runs, it needs a block of memory to use as a
  6478. stack. By default, it uses 32K on the machine stack. However, some
  6479. large or complicated patterns need more than this. The error
  6480. PCRE_ERROR_JIT_STACKLIMIT is given when there is not enough stack.
  6481. Three functions are provided for managing blocks of memory for use as
  6482. JIT stacks. There is further discussion about the use of JIT stacks in
  6483. the section entitled "JIT stack FAQ" below.
  6484. The pcre_jit_stack_alloc() function creates a JIT stack. Its arguments
  6485. are a starting size and a maximum size, and it returns a pointer to an
  6486. opaque structure of type pcre_jit_stack, or NULL if there is an error.
  6487. The pcre_jit_stack_free() function can be used to free a stack that is
  6488. no longer needed. (For the technically minded: the address space is
  6489. allocated by mmap or VirtualAlloc.)
  6490. JIT uses far less memory for recursion than the interpretive code, and
  6491. a maximum stack size of 512K to 1M should be more than enough for any
  6492. pattern.
  6493. The pcre_assign_jit_stack() function specifies which stack JIT code
  6494. should use. Its arguments are as follows:
  6495. pcre_extra *extra
  6496. pcre_jit_callback callback
  6497. void *data
  6498. The extra argument must be the result of studying a pattern with
  6499. PCRE_STUDY_JIT_COMPILE etc. There are three cases for the values of the
  6500. other two options:
  6501. (1) If callback is NULL and data is NULL, an internal 32K block
  6502. on the machine stack is used.
  6503. (2) If callback is NULL and data is not NULL, data must be
  6504. a valid JIT stack, the result of calling pcre_jit_stack_alloc().
  6505. (3) If callback is not NULL, it must point to a function that is
  6506. called with data as an argument at the start of matching, in
  6507. order to set up a JIT stack. If the return from the callback
  6508. function is NULL, the internal 32K stack is used; otherwise the
  6509. return value must be a valid JIT stack, the result of calling
  6510. pcre_jit_stack_alloc().
  6511. A callback function is obeyed whenever JIT code is about to be run; it
  6512. is not obeyed when pcre_exec() is called with options that are incom-
  6513. patible for JIT execution. A callback function can therefore be used to
  6514. determine whether a match operation was executed by JIT or by the
  6515. interpreter.
  6516. You may safely use the same JIT stack for more than one pattern (either
  6517. by assigning directly or by callback), as long as the patterns are all
  6518. matched sequentially in the same thread. In a multithread application,
  6519. if you do not specify a JIT stack, or if you assign or pass back NULL
  6520. from a callback, that is thread-safe, because each thread has its own
  6521. machine stack. However, if you assign or pass back a non-NULL JIT
  6522. stack, this must be a different stack for each thread so that the
  6523. application is thread-safe.
  6524. Strictly speaking, even more is allowed. You can assign the same non-
  6525. NULL stack to any number of patterns as long as they are not used for
  6526. matching by multiple threads at the same time. For example, you can
  6527. assign the same stack to all compiled patterns, and use a global mutex
  6528. in the callback to wait until the stack is available for use. However,
  6529. this is an inefficient solution, and not recommended.
  6530. This is a suggestion for how a multithreaded program that needs to set
  6531. up non-default JIT stacks might operate:
  6532. During thread initalization
  6533. thread_local_var = pcre_jit_stack_alloc(...)
  6534. During thread exit
  6535. pcre_jit_stack_free(thread_local_var)
  6536. Use a one-line callback function
  6537. return thread_local_var
  6538. All the functions described in this section do nothing if JIT is not
  6539. available, and pcre_assign_jit_stack() does nothing unless the extra
  6540. argument is non-NULL and points to a pcre_extra block that is the
  6541. result of a successful study with PCRE_STUDY_JIT_COMPILE etc.
  6542. JIT STACK FAQ
  6543. (1) Why do we need JIT stacks?
  6544. PCRE (and JIT) is a recursive, depth-first engine, so it needs a stack
  6545. where the local data of the current node is pushed before checking its
  6546. child nodes. Allocating real machine stack on some platforms is diffi-
  6547. cult. For example, the stack chain needs to be updated every time if we
  6548. extend the stack on PowerPC. Although it is possible, its updating
  6549. time overhead decreases performance. So we do the recursion in memory.
  6550. (2) Why don't we simply allocate blocks of memory with malloc()?
  6551. Modern operating systems have a nice feature: they can reserve an
  6552. address space instead of allocating memory. We can safely allocate mem-
  6553. ory pages inside this address space, so the stack could grow without
  6554. moving memory data (this is important because of pointers). Thus we can
  6555. allocate 1M address space, and use only a single memory page (usually
  6556. 4K) if that is enough. However, we can still grow up to 1M anytime if
  6557. needed.
  6558. (3) Who "owns" a JIT stack?
  6559. The owner of the stack is the user program, not the JIT studied pattern
  6560. or anything else. The user program must ensure that if a stack is used
  6561. by pcre_exec(), (that is, it is assigned to the pattern currently run-
  6562. ning), that stack must not be used by any other threads (to avoid over-
  6563. writing the same memory area). The best practice for multithreaded pro-
  6564. grams is to allocate a stack for each thread, and return this stack
  6565. through the JIT callback function.
  6566. (4) When should a JIT stack be freed?
  6567. You can free a JIT stack at any time, as long as it will not be used by
  6568. pcre_exec() again. When you assign the stack to a pattern, only a
  6569. pointer is set. There is no reference counting or any other magic. You
  6570. can free the patterns and stacks in any order, anytime. Just do not
  6571. call pcre_exec() with a pattern pointing to an already freed stack, as
  6572. that will cause SEGFAULT. (Also, do not free a stack currently used by
  6573. pcre_exec() in another thread). You can also replace the stack for a
  6574. pattern at any time. You can even free the previous stack before
  6575. assigning a replacement.
  6576. (5) Should I allocate/free a stack every time before/after calling
  6577. pcre_exec()?
  6578. No, because this is too costly in terms of resources. However, you
  6579. could implement some clever idea which release the stack if it is not
  6580. used in let's say two minutes. The JIT callback can help to achieve
  6581. this without keeping a list of the currently JIT studied patterns.
  6582. (6) OK, the stack is for long term memory allocation. But what happens
  6583. if a pattern causes stack overflow with a stack of 1M? Is that 1M kept
  6584. until the stack is freed?
  6585. Especially on embedded sytems, it might be a good idea to release mem-
  6586. ory sometimes without freeing the stack. There is no API for this at
  6587. the moment. Probably a function call which returns with the currently
  6588. allocated memory for any stack and another which allows releasing mem-
  6589. ory (shrinking the stack) would be a good idea if someone needs this.
  6590. (7) This is too much of a headache. Isn't there any better solution for
  6591. JIT stack handling?
  6592. No, thanks to Windows. If POSIX threads were used everywhere, we could
  6593. throw out this complicated API.
  6594. EXAMPLE CODE
  6595. This is a single-threaded example that specifies a JIT stack without
  6596. using a callback.
  6597. int rc;
  6598. int ovector[30];
  6599. pcre *re;
  6600. pcre_extra *extra;
  6601. pcre_jit_stack *jit_stack;
  6602. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  6603. /* Check for errors */
  6604. extra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &error);
  6605. jit_stack = pcre_jit_stack_alloc(32*1024, 512*1024);
  6606. /* Check for error (NULL) */
  6607. pcre_assign_jit_stack(extra, NULL, jit_stack);
  6608. rc = pcre_exec(re, extra, subject, length, 0, 0, ovector, 30);
  6609. /* Check results */
  6610. pcre_free(re);
  6611. pcre_free_study(extra);
  6612. pcre_jit_stack_free(jit_stack);
  6613. JIT FAST PATH API
  6614. Because the API described above falls back to interpreted execution
  6615. when JIT is not available, it is convenient for programs that are writ-
  6616. ten for general use in many environments. However, calling JIT via
  6617. pcre_exec() does have a performance impact. Programs that are written
  6618. for use where JIT is known to be available, and which need the best
  6619. possible performance, can instead use a "fast path" API to call JIT
  6620. execution directly instead of calling pcre_exec() (obviously only for
  6621. patterns that have been successfully studied by JIT).
  6622. The fast path function is called pcre_jit_exec(), and it takes exactly
  6623. the same arguments as pcre_exec(), plus one additional argument that
  6624. must point to a JIT stack. The JIT stack arrangements described above
  6625. do not apply. The return values are the same as for pcre_exec().
  6626. When you call pcre_exec(), as well as testing for invalid options, a
  6627. number of other sanity checks are performed on the arguments. For exam-
  6628. ple, if the subject pointer is NULL, or its length is negative, an
  6629. immediate error is given. Also, unless PCRE_NO_UTF[8|16|32] is set, a
  6630. UTF subject string is tested for validity. In the interests of speed,
  6631. these checks do not happen on the JIT fast path, and if invalid data is
  6632. passed, the result is undefined.
  6633. Bypassing the sanity checks and the pcre_exec() wrapping can give
  6634. speedups of more than 10%.
  6635. Note that the pcre_jit_exec() function is not available in versions of
  6636. PCRE before 8.32 (released in November 2012). If you need to support
  6637. versions that old you must either use the slower pcre_exec(), or switch
  6638. between the two codepaths by checking the values of PCRE_MAJOR and
  6639. PCRE_MINOR.
  6640. Due to an unfortunate implementation oversight, even in versions 8.32
  6641. and later there will be no pcre_jit_exec() stub function defined when
  6642. PCRE is compiled with --disable-jit, which is the default, and there's
  6643. no way to detect whether PCRE was compiled with --enable-jit via a
  6644. macro.
  6645. If you need to support versions older than 8.32, or versions that may
  6646. not build with --enable-jit, you must either use the slower
  6647. pcre_exec(), or switch between the two codepaths by checking the values
  6648. of PCRE_MAJOR and PCRE_MINOR.
  6649. Switching between the two by checking the version assumes that all the
  6650. versions being targeted are built with --enable-jit. To also support
  6651. builds that may use --disable-jit either pcre_exec() must be used, or a
  6652. compile-time check for JIT via pcre_config() (which assumes the runtime
  6653. environment will be the same), or as the Git project decided to do,
  6654. simply assume that pcre_jit_exec() is present in 8.32 or later unless a
  6655. compile-time flag is provided, see the "grep: un-break building with
  6656. PCRE >= 8.32 without --enable-jit" commit in git.git for an example of
  6657. that.
  6658. SEE ALSO
  6659. pcreapi(3)
  6660. AUTHOR
  6661. Philip Hazel (FAQ by Zoltan Herczeg)
  6662. University Computing Service
  6663. Cambridge CB2 3QH, England.
  6664. REVISION
  6665. Last updated: 05 July 2017
  6666. Copyright (c) 1997-2017 University of Cambridge.
  6667. ------------------------------------------------------------------------------
  6668. PCREPARTIAL(3) Library Functions Manual PCREPARTIAL(3)
  6669. NAME
  6670. PCRE - Perl-compatible regular expressions
  6671. PARTIAL MATCHING IN PCRE
  6672. In normal use of PCRE, if the subject string that is passed to a match-
  6673. ing function matches as far as it goes, but is too short to match the
  6674. entire pattern, PCRE_ERROR_NOMATCH is returned. There are circumstances
  6675. where it might be helpful to distinguish this case from other cases in
  6676. which there is no match.
  6677. Consider, for example, an application where a human is required to type
  6678. in data for a field with specific formatting requirements. An example
  6679. might be a date in the form ddmmmyy, defined by this pattern:
  6680. ^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$
  6681. If the application sees the user's keystrokes one by one, and can check
  6682. that what has been typed so far is potentially valid, it is able to
  6683. raise an error as soon as a mistake is made, by beeping and not
  6684. reflecting the character that has been typed, for example. This immedi-
  6685. ate feedback is likely to be a better user interface than a check that
  6686. is delayed until the entire string has been entered. Partial matching
  6687. can also be useful when the subject string is very long and is not all
  6688. available at once.
  6689. PCRE supports partial matching by means of the PCRE_PARTIAL_SOFT and
  6690. PCRE_PARTIAL_HARD options, which can be set when calling any of the
  6691. matching functions. For backwards compatibility, PCRE_PARTIAL is a syn-
  6692. onym for PCRE_PARTIAL_SOFT. The essential difference between the two
  6693. options is whether or not a partial match is preferred to an alterna-
  6694. tive complete match, though the details differ between the two types of
  6695. matching function. If both options are set, PCRE_PARTIAL_HARD takes
  6696. precedence.
  6697. If you want to use partial matching with just-in-time optimized code,
  6698. you must call pcre_study(), pcre16_study() or pcre32_study() with one
  6699. or both of these options:
  6700. PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
  6701. PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
  6702. PCRE_STUDY_JIT_COMPILE should also be set if you are going to run non-
  6703. partial matches on the same pattern. If the appropriate JIT study mode
  6704. has not been set for a match, the interpretive matching code is used.
  6705. Setting a partial matching option disables two of PCRE's standard opti-
  6706. mizations. PCRE remembers the last literal data unit in a pattern, and
  6707. abandons matching immediately if it is not present in the subject
  6708. string. This optimization cannot be used for a subject string that
  6709. might match only partially. If the pattern was studied, PCRE knows the
  6710. minimum length of a matching string, and does not bother to run the
  6711. matching function on shorter strings. This optimization is also dis-
  6712. abled for partial matching.
  6713. PARTIAL MATCHING USING pcre_exec() OR pcre[16|32]_exec()
  6714. A partial match occurs during a call to pcre_exec() or
  6715. pcre[16|32]_exec() when the end of the subject string is reached suc-
  6716. cessfully, but matching cannot continue because more characters are
  6717. needed. However, at least one character in the subject must have been
  6718. inspected. This character need not form part of the final matched
  6719. string; lookbehind assertions and the \K escape sequence provide ways
  6720. of inspecting characters before the start of a matched substring. The
  6721. requirement for inspecting at least one character exists because an
  6722. empty string can always be matched; without such a restriction there
  6723. would always be a partial match of an empty string at the end of the
  6724. subject.
  6725. If there are at least two slots in the offsets vector when a partial
  6726. match is returned, the first slot is set to the offset of the earliest
  6727. character that was inspected. For convenience, the second offset points
  6728. to the end of the subject so that a substring can easily be identified.
  6729. If there are at least three slots in the offsets vector, the third slot
  6730. is set to the offset of the character where matching started.
  6731. For the majority of patterns, the contents of the first and third slots
  6732. will be the same. However, for patterns that contain lookbehind asser-
  6733. tions, or begin with \b or \B, characters before the one where matching
  6734. started may have been inspected while carrying out the match. For exam-
  6735. ple, consider this pattern:
  6736. /(?<=abc)123/
  6737. This pattern matches "123", but only if it is preceded by "abc". If the
  6738. subject string is "xyzabc12", the first two offsets after a partial
  6739. match are for the substring "abc12", because all these characters were
  6740. inspected. However, the third offset is set to 6, because that is the
  6741. offset where matching began.
  6742. What happens when a partial match is identified depends on which of the
  6743. two partial matching options are set.
  6744. PCRE_PARTIAL_SOFT WITH pcre_exec() OR pcre[16|32]_exec()
  6745. If PCRE_PARTIAL_SOFT is set when pcre_exec() or pcre[16|32]_exec()
  6746. identifies a partial match, the partial match is remembered, but match-
  6747. ing continues as normal, and other alternatives in the pattern are
  6748. tried. If no complete match can be found, PCRE_ERROR_PARTIAL is
  6749. returned instead of PCRE_ERROR_NOMATCH.
  6750. This option is "soft" because it prefers a complete match over a par-
  6751. tial match. All the various matching items in a pattern behave as if
  6752. the subject string is potentially complete. For example, \z, \Z, and $
  6753. match at the end of the subject, as normal, and for \b and \B the end
  6754. of the subject is treated as a non-alphanumeric.
  6755. If there is more than one partial match, the first one that was found
  6756. provides the data that is returned. Consider this pattern:
  6757. /123\w+X|dogY/
  6758. If this is matched against the subject string "abc123dog", both alter-
  6759. natives fail to match, but the end of the subject is reached during
  6760. matching, so PCRE_ERROR_PARTIAL is returned. The offsets are set to 3
  6761. and 9, identifying "123dog" as the first partial match that was found.
  6762. (In this example, there are two partial matches, because "dog" on its
  6763. own partially matches the second alternative.)
  6764. PCRE_PARTIAL_HARD WITH pcre_exec() OR pcre[16|32]_exec()
  6765. If PCRE_PARTIAL_HARD is set for pcre_exec() or pcre[16|32]_exec(),
  6766. PCRE_ERROR_PARTIAL is returned as soon as a partial match is found,
  6767. without continuing to search for possible complete matches. This option
  6768. is "hard" because it prefers an earlier partial match over a later com-
  6769. plete match. For this reason, the assumption is made that the end of
  6770. the supplied subject string may not be the true end of the available
  6771. data, and so, if \z, \Z, \b, \B, or $ are encountered at the end of the
  6772. subject, the result is PCRE_ERROR_PARTIAL, provided that at least one
  6773. character in the subject has been inspected.
  6774. Setting PCRE_PARTIAL_HARD also affects the way UTF-8 and UTF-16 subject
  6775. strings are checked for validity. Normally, an invalid sequence causes
  6776. the error PCRE_ERROR_BADUTF8 or PCRE_ERROR_BADUTF16. However, in the
  6777. special case of a truncated character at the end of the subject,
  6778. PCRE_ERROR_SHORTUTF8 or PCRE_ERROR_SHORTUTF16 is returned when
  6779. PCRE_PARTIAL_HARD is set.
  6780. Comparing hard and soft partial matching
  6781. The difference between the two partial matching options can be illus-
  6782. trated by a pattern such as:
  6783. /dog(sbody)?/
  6784. This matches either "dog" or "dogsbody", greedily (that is, it prefers
  6785. the longer string if possible). If it is matched against the string
  6786. "dog" with PCRE_PARTIAL_SOFT, it yields a complete match for "dog".
  6787. However, if PCRE_PARTIAL_HARD is set, the result is PCRE_ERROR_PARTIAL.
  6788. On the other hand, if the pattern is made ungreedy the result is dif-
  6789. ferent:
  6790. /dog(sbody)??/
  6791. In this case the result is always a complete match because that is
  6792. found first, and matching never continues after finding a complete
  6793. match. It might be easier to follow this explanation by thinking of the
  6794. two patterns like this:
  6795. /dog(sbody)?/ is the same as /dogsbody|dog/
  6796. /dog(sbody)??/ is the same as /dog|dogsbody/
  6797. The second pattern will never match "dogsbody", because it will always
  6798. find the shorter match first.
  6799. PARTIAL MATCHING USING pcre_dfa_exec() OR pcre[16|32]_dfa_exec()
  6800. The DFA functions move along the subject string character by character,
  6801. without backtracking, searching for all possible matches simultane-
  6802. ously. If the end of the subject is reached before the end of the pat-
  6803. tern, there is the possibility of a partial match, again provided that
  6804. at least one character has been inspected.
  6805. When PCRE_PARTIAL_SOFT is set, PCRE_ERROR_PARTIAL is returned only if
  6806. there have been no complete matches. Otherwise, the complete matches
  6807. are returned. However, if PCRE_PARTIAL_HARD is set, a partial match
  6808. takes precedence over any complete matches. The portion of the string
  6809. that was inspected when the longest partial match was found is set as
  6810. the first matching string, provided there are at least two slots in the
  6811. offsets vector.
  6812. Because the DFA functions always search for all possible matches, and
  6813. there is no difference between greedy and ungreedy repetition, their
  6814. behaviour is different from the standard functions when PCRE_PAR-
  6815. TIAL_HARD is set. Consider the string "dog" matched against the
  6816. ungreedy pattern shown above:
  6817. /dog(sbody)??/
  6818. Whereas the standard functions stop as soon as they find the complete
  6819. match for "dog", the DFA functions also find the partial match for
  6820. "dogsbody", and so return that when PCRE_PARTIAL_HARD is set.
  6821. PARTIAL MATCHING AND WORD BOUNDARIES
  6822. If a pattern ends with one of sequences \b or \B, which test for word
  6823. boundaries, partial matching with PCRE_PARTIAL_SOFT can give counter-
  6824. intuitive results. Consider this pattern:
  6825. /\bcat\b/
  6826. This matches "cat", provided there is a word boundary at either end. If
  6827. the subject string is "the cat", the comparison of the final "t" with a
  6828. following character cannot take place, so a partial match is found.
  6829. However, normal matching carries on, and \b matches at the end of the
  6830. subject when the last character is a letter, so a complete match is
  6831. found. The result, therefore, is not PCRE_ERROR_PARTIAL. Using
  6832. PCRE_PARTIAL_HARD in this case does yield PCRE_ERROR_PARTIAL, because
  6833. then the partial match takes precedence.
  6834. FORMERLY RESTRICTED PATTERNS
  6835. For releases of PCRE prior to 8.00, because of the way certain internal
  6836. optimizations were implemented in the pcre_exec() function, the
  6837. PCRE_PARTIAL option (predecessor of PCRE_PARTIAL_SOFT) could not be
  6838. used with all patterns. From release 8.00 onwards, the restrictions no
  6839. longer apply, and partial matching with can be requested for any pat-
  6840. tern.
  6841. Items that were formerly restricted were repeated single characters and
  6842. repeated metasequences. If PCRE_PARTIAL was set for a pattern that did
  6843. not conform to the restrictions, pcre_exec() returned the error code
  6844. PCRE_ERROR_BADPARTIAL (-13). This error code is no longer in use. The
  6845. PCRE_INFO_OKPARTIAL call to pcre_fullinfo() to find out if a compiled
  6846. pattern can be used for partial matching now always returns 1.
  6847. EXAMPLE OF PARTIAL MATCHING USING PCRETEST
  6848. If the escape sequence \P is present in a pcretest data line, the
  6849. PCRE_PARTIAL_SOFT option is used for the match. Here is a run of
  6850. pcretest that uses the date example quoted above:
  6851. re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/
  6852. data> 25jun04\P
  6853. 0: 25jun04
  6854. 1: jun
  6855. data> 25dec3\P
  6856. Partial match: 23dec3
  6857. data> 3ju\P
  6858. Partial match: 3ju
  6859. data> 3juj\P
  6860. No match
  6861. data> j\P
  6862. No match
  6863. The first data string is matched completely, so pcretest shows the
  6864. matched substrings. The remaining four strings do not match the com-
  6865. plete pattern, but the first two are partial matches. Similar output is
  6866. obtained if DFA matching is used.
  6867. If the escape sequence \P is present more than once in a pcretest data
  6868. line, the PCRE_PARTIAL_HARD option is set for the match.
  6869. MULTI-SEGMENT MATCHING WITH pcre_dfa_exec() OR pcre[16|32]_dfa_exec()
  6870. When a partial match has been found using a DFA matching function, it
  6871. is possible to continue the match by providing additional subject data
  6872. and calling the function again with the same compiled regular expres-
  6873. sion, this time setting the PCRE_DFA_RESTART option. You must pass the
  6874. same working space as before, because this is where details of the pre-
  6875. vious partial match are stored. Here is an example using pcretest,
  6876. using the \R escape sequence to set the PCRE_DFA_RESTART option (\D
  6877. specifies the use of the DFA matching function):
  6878. re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/
  6879. data> 23ja\P\D
  6880. Partial match: 23ja
  6881. data> n05\R\D
  6882. 0: n05
  6883. The first call has "23ja" as the subject, and requests partial match-
  6884. ing; the second call has "n05" as the subject for the continued
  6885. (restarted) match. Notice that when the match is complete, only the
  6886. last part is shown; PCRE does not retain the previously partially-
  6887. matched string. It is up to the calling program to do that if it needs
  6888. to.
  6889. That means that, for an unanchored pattern, if a continued match fails,
  6890. it is not possible to try again at a new starting point. All this
  6891. facility is capable of doing is continuing with the previous match
  6892. attempt. In the previous example, if the second set of data is "ug23"
  6893. the result is no match, even though there would be a match for "aug23"
  6894. if the entire string were given at once. Depending on the application,
  6895. this may or may not be what you want. The only way to allow for start-
  6896. ing again at the next character is to retain the matched part of the
  6897. subject and try a new complete match.
  6898. You can set the PCRE_PARTIAL_SOFT or PCRE_PARTIAL_HARD options with
  6899. PCRE_DFA_RESTART to continue partial matching over multiple segments.
  6900. This facility can be used to pass very long subject strings to the DFA
  6901. matching functions.
  6902. MULTI-SEGMENT MATCHING WITH pcre_exec() OR pcre[16|32]_exec()
  6903. From release 8.00, the standard matching functions can also be used to
  6904. do multi-segment matching. Unlike the DFA functions, it is not possible
  6905. to restart the previous match with a new segment of data. Instead, new
  6906. data must be added to the previous subject string, and the entire match
  6907. re-run, starting from the point where the partial match occurred. Ear-
  6908. lier data can be discarded.
  6909. It is best to use PCRE_PARTIAL_HARD in this situation, because it does
  6910. not treat the end of a segment as the end of the subject when matching
  6911. \z, \Z, \b, \B, and $. Consider an unanchored pattern that matches
  6912. dates:
  6913. re> /\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d/
  6914. data> The date is 23ja\P\P
  6915. Partial match: 23ja
  6916. At this stage, an application could discard the text preceding "23ja",
  6917. add on text from the next segment, and call the matching function
  6918. again. Unlike the DFA matching functions, the entire matching string
  6919. must always be available, and the complete matching process occurs for
  6920. each call, so more memory and more processing time is needed.
  6921. Note: If the pattern contains lookbehind assertions, or \K, or starts
  6922. with \b or \B, the string that is returned for a partial match includes
  6923. characters that precede the start of what would be returned for a com-
  6924. plete match, because it contains all the characters that were inspected
  6925. during the partial match.
  6926. ISSUES WITH MULTI-SEGMENT MATCHING
  6927. Certain types of pattern may give problems with multi-segment matching,
  6928. whichever matching function is used.
  6929. 1. If the pattern contains a test for the beginning of a line, you need
  6930. to pass the PCRE_NOTBOL option when the subject string for any call
  6931. does start at the beginning of a line. There is also a PCRE_NOTEOL
  6932. option, but in practice when doing multi-segment matching you should be
  6933. using PCRE_PARTIAL_HARD, which includes the effect of PCRE_NOTEOL.
  6934. 2. Lookbehind assertions that have already been obeyed are catered for
  6935. in the offsets that are returned for a partial match. However a lookbe-
  6936. hind assertion later in the pattern could require even earlier charac-
  6937. ters to be inspected. You can handle this case by using the
  6938. PCRE_INFO_MAXLOOKBEHIND option of the pcre_fullinfo() or
  6939. pcre[16|32]_fullinfo() functions to obtain the length of the longest
  6940. lookbehind in the pattern. This length is given in characters, not
  6941. bytes. If you always retain at least that many characters before the
  6942. partially matched string, all should be well. (Of course, near the
  6943. start of the subject, fewer characters may be present; in that case all
  6944. characters should be retained.)
  6945. From release 8.33, there is a more accurate way of deciding which char-
  6946. acters to retain. Instead of subtracting the length of the longest
  6947. lookbehind from the earliest inspected character (offsets[0]), the
  6948. match start position (offsets[2]) should be used, and the next match
  6949. attempt started at the offsets[2] character by setting the startoffset
  6950. argument of pcre_exec() or pcre_dfa_exec().
  6951. For example, if the pattern "(?<=123)abc" is partially matched against
  6952. the string "xx123a", the three offset values returned are 2, 6, and 5.
  6953. This indicates that the matching process that gave a partial match
  6954. started at offset 5, but the characters "123a" were all inspected. The
  6955. maximum lookbehind for that pattern is 3, so taking that away from 5
  6956. shows that we need only keep "123a", and the next match attempt can be
  6957. started at offset 3 (that is, at "a") when further characters have been
  6958. added. When the match start is not the earliest inspected character,
  6959. pcretest shows it explicitly:
  6960. re> "(?<=123)abc"
  6961. data> xx123a\P\P
  6962. Partial match at offset 5: 123a
  6963. 3. Because a partial match must always contain at least one character,
  6964. what might be considered a partial match of an empty string actually
  6965. gives a "no match" result. For example:
  6966. re> /c(?<=abc)x/
  6967. data> ab\P
  6968. No match
  6969. If the next segment begins "cx", a match should be found, but this will
  6970. only happen if characters from the previous segment are retained. For
  6971. this reason, a "no match" result should be interpreted as "partial
  6972. match of an empty string" when the pattern contains lookbehinds.
  6973. 4. Matching a subject string that is split into multiple segments may
  6974. not always produce exactly the same result as matching over one single
  6975. long string, especially when PCRE_PARTIAL_SOFT is used. The section
  6976. "Partial Matching and Word Boundaries" above describes an issue that
  6977. arises if the pattern ends with \b or \B. Another kind of difference
  6978. may occur when there are multiple matching possibilities, because (for
  6979. PCRE_PARTIAL_SOFT) a partial match result is given only when there are
  6980. no completed matches. This means that as soon as the shortest match has
  6981. been found, continuation to a new subject segment is no longer possi-
  6982. ble. Consider again this pcretest example:
  6983. re> /dog(sbody)?/
  6984. data> dogsb\P
  6985. 0: dog
  6986. data> do\P\D
  6987. Partial match: do
  6988. data> gsb\R\P\D
  6989. 0: g
  6990. data> dogsbody\D
  6991. 0: dogsbody
  6992. 1: dog
  6993. The first data line passes the string "dogsb" to a standard matching
  6994. function, setting the PCRE_PARTIAL_SOFT option. Although the string is
  6995. a partial match for "dogsbody", the result is not PCRE_ERROR_PARTIAL,
  6996. because the shorter string "dog" is a complete match. Similarly, when
  6997. the subject is presented to a DFA matching function in several parts
  6998. ("do" and "gsb" being the first two) the match stops when "dog" has
  6999. been found, and it is not possible to continue. On the other hand, if
  7000. "dogsbody" is presented as a single string, a DFA matching function
  7001. finds both matches.
  7002. Because of these problems, it is best to use PCRE_PARTIAL_HARD when
  7003. matching multi-segment data. The example above then behaves differ-
  7004. ently:
  7005. re> /dog(sbody)?/
  7006. data> dogsb\P\P
  7007. Partial match: dogsb
  7008. data> do\P\D
  7009. Partial match: do
  7010. data> gsb\R\P\P\D
  7011. Partial match: gsb
  7012. 5. Patterns that contain alternatives at the top level which do not all
  7013. start with the same pattern item may not work as expected when
  7014. PCRE_DFA_RESTART is used. For example, consider this pattern:
  7015. 1234|3789
  7016. If the first part of the subject is "ABC123", a partial match of the
  7017. first alternative is found at offset 3. There is no partial match for
  7018. the second alternative, because such a match does not start at the same
  7019. point in the subject string. Attempting to continue with the string
  7020. "7890" does not yield a match because only those alternatives that
  7021. match at one point in the subject are remembered. The problem arises
  7022. because the start of the second alternative matches within the first
  7023. alternative. There is no problem with anchored patterns or patterns
  7024. such as:
  7025. 1234|ABCD
  7026. where no string can be a partial match for both alternatives. This is
  7027. not a problem if a standard matching function is used, because the
  7028. entire match has to be rerun each time:
  7029. re> /1234|3789/
  7030. data> ABC123\P\P
  7031. Partial match: 123
  7032. data> 1237890
  7033. 0: 3789
  7034. Of course, instead of using PCRE_DFA_RESTART, the same technique of re-
  7035. running the entire match can also be used with the DFA matching func-
  7036. tions. Another possibility is to work with two buffers. If a partial
  7037. match at offset n in the first buffer is followed by "no match" when
  7038. PCRE_DFA_RESTART is used on the second buffer, you can then try a new
  7039. match starting at offset n+1 in the first buffer.
  7040. AUTHOR
  7041. Philip Hazel
  7042. University Computing Service
  7043. Cambridge CB2 3QH, England.
  7044. REVISION
  7045. Last updated: 02 July 2013
  7046. Copyright (c) 1997-2013 University of Cambridge.
  7047. ------------------------------------------------------------------------------
  7048. PCREPRECOMPILE(3) Library Functions Manual PCREPRECOMPILE(3)
  7049. NAME
  7050. PCRE - Perl-compatible regular expressions
  7051. SAVING AND RE-USING PRECOMPILED PCRE PATTERNS
  7052. If you are running an application that uses a large number of regular
  7053. expression patterns, it may be useful to store them in a precompiled
  7054. form instead of having to compile them every time the application is
  7055. run. If you are not using any private character tables (see the
  7056. pcre_maketables() documentation), this is relatively straightforward.
  7057. If you are using private tables, it is a little bit more complicated.
  7058. However, if you are using the just-in-time optimization feature, it is
  7059. not possible to save and reload the JIT data.
  7060. If you save compiled patterns to a file, you can copy them to a differ-
  7061. ent host and run them there. If the two hosts have different endianness
  7062. (byte order), you should run the pcre[16|32]_pat-
  7063. tern_to_host_byte_order() function on the new host before trying to
  7064. match the pattern. The matching functions return PCRE_ERROR_BADENDIAN-
  7065. NESS if they detect a pattern with the wrong endianness.
  7066. Compiling regular expressions with one version of PCRE for use with a
  7067. different version is not guaranteed to work and may cause crashes, and
  7068. saving and restoring a compiled pattern loses any JIT optimization
  7069. data.
  7070. SAVING A COMPILED PATTERN
  7071. The value returned by pcre[16|32]_compile() points to a single block of
  7072. memory that holds the compiled pattern and associated data. You can
  7073. find the length of this block in bytes by calling
  7074. pcre[16|32]_fullinfo() with an argument of PCRE_INFO_SIZE. You can then
  7075. save the data in any appropriate manner. Here is sample code for the
  7076. 8-bit library that compiles a pattern and writes it to a file. It
  7077. assumes that the variable fd refers to a file that is open for output:
  7078. int erroroffset, rc, size;
  7079. char *error;
  7080. pcre *re;
  7081. re = pcre_compile("my pattern", 0, &error, &erroroffset, NULL);
  7082. if (re == NULL) { ... handle errors ... }
  7083. rc = pcre_fullinfo(re, NULL, PCRE_INFO_SIZE, &size);
  7084. if (rc < 0) { ... handle errors ... }
  7085. rc = fwrite(re, 1, size, fd);
  7086. if (rc != size) { ... handle errors ... }
  7087. In this example, the bytes that comprise the compiled pattern are
  7088. copied exactly. Note that this is binary data that may contain any of
  7089. the 256 possible byte values. On systems that make a distinction
  7090. between binary and non-binary data, be sure that the file is opened for
  7091. binary output.
  7092. If you want to write more than one pattern to a file, you will have to
  7093. devise a way of separating them. For binary data, preceding each pat-
  7094. tern with its length is probably the most straightforward approach.
  7095. Another possibility is to write out the data in hexadecimal instead of
  7096. binary, one pattern to a line.
  7097. Saving compiled patterns in a file is only one possible way of storing
  7098. them for later use. They could equally well be saved in a database, or
  7099. in the memory of some daemon process that passes them via sockets to
  7100. the processes that want them.
  7101. If the pattern has been studied, it is also possible to save the normal
  7102. study data in a similar way to the compiled pattern itself. However, if
  7103. the PCRE_STUDY_JIT_COMPILE was used, the just-in-time data that is cre-
  7104. ated cannot be saved because it is too dependent on the current envi-
  7105. ronment. When studying generates additional information,
  7106. pcre[16|32]_study() returns a pointer to a pcre[16|32]_extra data
  7107. block. Its format is defined in the section on matching a pattern in
  7108. the pcreapi documentation. The study_data field points to the binary
  7109. study data, and this is what you must save (not the pcre[16|32]_extra
  7110. block itself). The length of the study data can be obtained by calling
  7111. pcre[16|32]_fullinfo() with an argument of PCRE_INFO_STUDYSIZE. Remem-
  7112. ber to check that pcre[16|32]_study() did return a non-NULL value
  7113. before trying to save the study data.
  7114. RE-USING A PRECOMPILED PATTERN
  7115. Re-using a precompiled pattern is straightforward. Having reloaded it
  7116. into main memory, called pcre[16|32]_pattern_to_host_byte_order() if
  7117. necessary, you pass its pointer to pcre[16|32]_exec() or
  7118. pcre[16|32]_dfa_exec() in the usual way.
  7119. However, if you passed a pointer to custom character tables when the
  7120. pattern was compiled (the tableptr argument of pcre[16|32]_compile()),
  7121. you must now pass a similar pointer to pcre[16|32]_exec() or
  7122. pcre[16|32]_dfa_exec(), because the value saved with the compiled pat-
  7123. tern will obviously be nonsense. A field in a pcre[16|32]_extra() block
  7124. is used to pass this data, as described in the section on matching a
  7125. pattern in the pcreapi documentation.
  7126. Warning: The tables that pcre_exec() and pcre_dfa_exec() use must be
  7127. the same as those that were used when the pattern was compiled. If this
  7128. is not the case, the behaviour is undefined.
  7129. If you did not provide custom character tables when the pattern was
  7130. compiled, the pointer in the compiled pattern is NULL, which causes the
  7131. matching functions to use PCRE's internal tables. Thus, you do not need
  7132. to take any special action at run time in this case.
  7133. If you saved study data with the compiled pattern, you need to create
  7134. your own pcre[16|32]_extra data block and set the study_data field to
  7135. point to the reloaded study data. You must also set the
  7136. PCRE_EXTRA_STUDY_DATA bit in the flags field to indicate that study
  7137. data is present. Then pass the pcre[16|32]_extra block to the matching
  7138. function in the usual way. If the pattern was studied for just-in-time
  7139. optimization, that data cannot be saved, and so is lost by a
  7140. save/restore cycle.
  7141. COMPATIBILITY WITH DIFFERENT PCRE RELEASES
  7142. In general, it is safest to recompile all saved patterns when you
  7143. update to a new PCRE release, though not all updates actually require
  7144. this.
  7145. AUTHOR
  7146. Philip Hazel
  7147. University Computing Service
  7148. Cambridge CB2 3QH, England.
  7149. REVISION
  7150. Last updated: 12 November 2013
  7151. Copyright (c) 1997-2013 University of Cambridge.
  7152. ------------------------------------------------------------------------------
  7153. PCREPERFORM(3) Library Functions Manual PCREPERFORM(3)
  7154. NAME
  7155. PCRE - Perl-compatible regular expressions
  7156. PCRE PERFORMANCE
  7157. Two aspects of performance are discussed below: memory usage and pro-
  7158. cessing time. The way you express your pattern as a regular expression
  7159. can affect both of them.
  7160. COMPILED PATTERN MEMORY USAGE
  7161. Patterns are compiled by PCRE into a reasonably efficient interpretive
  7162. code, so that most simple patterns do not use much memory. However,
  7163. there is one case where the memory usage of a compiled pattern can be
  7164. unexpectedly large. If a parenthesized subpattern has a quantifier with
  7165. a minimum greater than 1 and/or a limited maximum, the whole subpattern
  7166. is repeated in the compiled code. For example, the pattern
  7167. (abc|def){2,4}
  7168. is compiled as if it were
  7169. (abc|def)(abc|def)((abc|def)(abc|def)?)?
  7170. (Technical aside: It is done this way so that backtrack points within
  7171. each of the repetitions can be independently maintained.)
  7172. For regular expressions whose quantifiers use only small numbers, this
  7173. is not usually a problem. However, if the numbers are large, and par-
  7174. ticularly if such repetitions are nested, the memory usage can become
  7175. an embarrassment. For example, the very simple pattern
  7176. ((ab){1,1000}c){1,3}
  7177. uses 51K bytes when compiled using the 8-bit library. When PCRE is com-
  7178. piled with its default internal pointer size of two bytes, the size
  7179. limit on a compiled pattern is 64K data units, and this is reached with
  7180. the above pattern if the outer repetition is increased from 3 to 4.
  7181. PCRE can be compiled to use larger internal pointers and thus handle
  7182. larger compiled patterns, but it is better to try to rewrite your pat-
  7183. tern to use less memory if you can.
  7184. One way of reducing the memory usage for such patterns is to make use
  7185. of PCRE's "subroutine" facility. Re-writing the above pattern as
  7186. ((ab)(?2){0,999}c)(?1){0,2}
  7187. reduces the memory requirements to 18K, and indeed it remains under 20K
  7188. even with the outer repetition increased to 100. However, this pattern
  7189. is not exactly equivalent, because the "subroutine" calls are treated
  7190. as atomic groups into which there can be no backtracking if there is a
  7191. subsequent matching failure. Therefore, PCRE cannot do this kind of
  7192. rewriting automatically. Furthermore, there is a noticeable loss of
  7193. speed when executing the modified pattern. Nevertheless, if the atomic
  7194. grouping is not a problem and the loss of speed is acceptable, this
  7195. kind of rewriting will allow you to process patterns that PCRE cannot
  7196. otherwise handle.
  7197. STACK USAGE AT RUN TIME
  7198. When pcre_exec() or pcre[16|32]_exec() is used for matching, certain
  7199. kinds of pattern can cause it to use large amounts of the process
  7200. stack. In some environments the default process stack is quite small,
  7201. and if it runs out the result is often SIGSEGV. This issue is probably
  7202. the most frequently raised problem with PCRE. Rewriting your pattern
  7203. can often help. The pcrestack documentation discusses this issue in
  7204. detail.
  7205. PROCESSING TIME
  7206. Certain items in regular expression patterns are processed more effi-
  7207. ciently than others. It is more efficient to use a character class like
  7208. [aeiou] than a set of single-character alternatives such as
  7209. (a|e|i|o|u). In general, the simplest construction that provides the
  7210. required behaviour is usually the most efficient. Jeffrey Friedl's book
  7211. contains a lot of useful general discussion about optimizing regular
  7212. expressions for efficient performance. This document contains a few
  7213. observations about PCRE.
  7214. Using Unicode character properties (the \p, \P, and \X escapes) is
  7215. slow, because PCRE has to use a multi-stage table lookup whenever it
  7216. needs a character's property. If you can find an alternative pattern
  7217. that does not use character properties, it will probably be faster.
  7218. By default, the escape sequences \b, \d, \s, and \w, and the POSIX
  7219. character classes such as [:alpha:] do not use Unicode properties,
  7220. partly for backwards compatibility, and partly for performance reasons.
  7221. However, you can set PCRE_UCP if you want Unicode character properties
  7222. to be used. This can double the matching time for items such as \d,
  7223. when matched with a traditional matching function; the performance loss
  7224. is less with a DFA matching function, and in both cases there is not
  7225. much difference for \b.
  7226. When a pattern begins with .* not in parentheses, or in parentheses
  7227. that are not the subject of a backreference, and the PCRE_DOTALL option
  7228. is set, the pattern is implicitly anchored by PCRE, since it can match
  7229. only at the start of a subject string. However, if PCRE_DOTALL is not
  7230. set, PCRE cannot make this optimization, because the . metacharacter
  7231. does not then match a newline, and if the subject string contains new-
  7232. lines, the pattern may match from the character immediately following
  7233. one of them instead of from the very start. For example, the pattern
  7234. .*second
  7235. matches the subject "first\nand second" (where \n stands for a newline
  7236. character), with the match starting at the seventh character. In order
  7237. to do this, PCRE has to retry the match starting after every newline in
  7238. the subject.
  7239. If you are using such a pattern with subject strings that do not con-
  7240. tain newlines, the best performance is obtained by setting PCRE_DOTALL,
  7241. or starting the pattern with ^.* or ^.*? to indicate explicit anchor-
  7242. ing. That saves PCRE from having to scan along the subject looking for
  7243. a newline to restart at.
  7244. Beware of patterns that contain nested indefinite repeats. These can
  7245. take a long time to run when applied to a string that does not match.
  7246. Consider the pattern fragment
  7247. ^(a+)*
  7248. This can match "aaaa" in 16 different ways, and this number increases
  7249. very rapidly as the string gets longer. (The * repeat can match 0, 1,
  7250. 2, 3, or 4 times, and for each of those cases other than 0 or 4, the +
  7251. repeats can match different numbers of times.) When the remainder of
  7252. the pattern is such that the entire match is going to fail, PCRE has in
  7253. principle to try every possible variation, and this can take an
  7254. extremely long time, even for relatively short strings.
  7255. An optimization catches some of the more simple cases such as
  7256. (a+)*b
  7257. where a literal character follows. Before embarking on the standard
  7258. matching procedure, PCRE checks that there is a "b" later in the sub-
  7259. ject string, and if there is not, it fails the match immediately. How-
  7260. ever, when there is no following literal this optimization cannot be
  7261. used. You can see the difference by comparing the behaviour of
  7262. (a+)*\d
  7263. with the pattern above. The former gives a failure almost instantly
  7264. when applied to a whole line of "a" characters, whereas the latter
  7265. takes an appreciable time with strings longer than about 20 characters.
  7266. In many cases, the solution to this kind of performance issue is to use
  7267. an atomic group or a possessive quantifier.
  7268. AUTHOR
  7269. Philip Hazel
  7270. University Computing Service
  7271. Cambridge CB2 3QH, England.
  7272. REVISION
  7273. Last updated: 25 August 2012
  7274. Copyright (c) 1997-2012 University of Cambridge.
  7275. ------------------------------------------------------------------------------
  7276. PCREPOSIX(3) Library Functions Manual PCREPOSIX(3)
  7277. NAME
  7278. PCRE - Perl-compatible regular expressions.
  7279. SYNOPSIS
  7280. #include <pcreposix.h>
  7281. int regcomp(regex_t *preg, const char *pattern,
  7282. int cflags);
  7283. int regexec(regex_t *preg, const char *string,
  7284. size_t nmatch, regmatch_t pmatch[], int eflags);
  7285. size_t regerror(int errcode, const regex_t *preg,
  7286. char *errbuf, size_t errbuf_size);
  7287. void regfree(regex_t *preg);
  7288. DESCRIPTION
  7289. This set of functions provides a POSIX-style API for the PCRE regular
  7290. expression 8-bit library. See the pcreapi documentation for a descrip-
  7291. tion of PCRE's native API, which contains much additional functional-
  7292. ity. There is no POSIX-style wrapper for PCRE's 16-bit and 32-bit
  7293. library.
  7294. The functions described here are just wrapper functions that ultimately
  7295. call the PCRE native API. Their prototypes are defined in the
  7296. pcreposix.h header file, and on Unix systems the library itself is
  7297. called pcreposix.a, so can be accessed by adding -lpcreposix to the
  7298. command for linking an application that uses them. Because the POSIX
  7299. functions call the native ones, it is also necessary to add -lpcre.
  7300. I have implemented only those POSIX option bits that can be reasonably
  7301. mapped to PCRE native options. In addition, the option REG_EXTENDED is
  7302. defined with the value zero. This has no effect, but since programs
  7303. that are written to the POSIX interface often use it, this makes it
  7304. easier to slot in PCRE as a replacement library. Other POSIX options
  7305. are not even defined.
  7306. There are also some other options that are not defined by POSIX. These
  7307. have been added at the request of users who want to make use of certain
  7308. PCRE-specific features via the POSIX calling interface.
  7309. When PCRE is called via these functions, it is only the API that is
  7310. POSIX-like in style. The syntax and semantics of the regular expres-
  7311. sions themselves are still those of Perl, subject to the setting of
  7312. various PCRE options, as described below. "POSIX-like in style" means
  7313. that the API approximates to the POSIX definition; it is not fully
  7314. POSIX-compatible, and in multi-byte encoding domains it is probably
  7315. even less compatible.
  7316. The header for these functions is supplied as pcreposix.h to avoid any
  7317. potential clash with other POSIX libraries. It can, of course, be
  7318. renamed or aliased as regex.h, which is the "correct" name. It provides
  7319. two structure types, regex_t for compiled internal forms, and reg-
  7320. match_t for returning captured substrings. It also defines some con-
  7321. stants whose names start with "REG_"; these are used for setting
  7322. options and identifying error codes.
  7323. COMPILING A PATTERN
  7324. The function regcomp() is called to compile a pattern into an internal
  7325. form. The pattern is a C string terminated by a binary zero, and is
  7326. passed in the argument pattern. The preg argument is a pointer to a
  7327. regex_t structure that is used as a base for storing information about
  7328. the compiled regular expression.
  7329. The argument cflags is either zero, or contains one or more of the bits
  7330. defined by the following macros:
  7331. REG_DOTALL
  7332. The PCRE_DOTALL option is set when the regular expression is passed for
  7333. compilation to the native function. Note that REG_DOTALL is not part of
  7334. the POSIX standard.
  7335. REG_ICASE
  7336. The PCRE_CASELESS option is set when the regular expression is passed
  7337. for compilation to the native function.
  7338. REG_NEWLINE
  7339. The PCRE_MULTILINE option is set when the regular expression is passed
  7340. for compilation to the native function. Note that this does not mimic
  7341. the defined POSIX behaviour for REG_NEWLINE (see the following sec-
  7342. tion).
  7343. REG_NOSUB
  7344. The PCRE_NO_AUTO_CAPTURE option is set when the regular expression is
  7345. passed for compilation to the native function. In addition, when a pat-
  7346. tern that is compiled with this flag is passed to regexec() for match-
  7347. ing, the nmatch and pmatch arguments are ignored, and no captured
  7348. strings are returned.
  7349. REG_UCP
  7350. The PCRE_UCP option is set when the regular expression is passed for
  7351. compilation to the native function. This causes PCRE to use Unicode
  7352. properties when matchine \d, \w, etc., instead of just recognizing
  7353. ASCII values. Note that REG_UTF8 is not part of the POSIX standard.
  7354. REG_UNGREEDY
  7355. The PCRE_UNGREEDY option is set when the regular expression is passed
  7356. for compilation to the native function. Note that REG_UNGREEDY is not
  7357. part of the POSIX standard.
  7358. REG_UTF8
  7359. The PCRE_UTF8 option is set when the regular expression is passed for
  7360. compilation to the native function. This causes the pattern itself and
  7361. all data strings used for matching it to be treated as UTF-8 strings.
  7362. Note that REG_UTF8 is not part of the POSIX standard.
  7363. In the absence of these flags, no options are passed to the native
  7364. function. This means the the regex is compiled with PCRE default
  7365. semantics. In particular, the way it handles newline characters in the
  7366. subject string is the Perl way, not the POSIX way. Note that setting
  7367. PCRE_MULTILINE has only some of the effects specified for REG_NEWLINE.
  7368. It does not affect the way newlines are matched by . (they are not) or
  7369. by a negative class such as [^a] (they are).
  7370. The yield of regcomp() is zero on success, and non-zero otherwise. The
  7371. preg structure is filled in on success, and one member of the structure
  7372. is public: re_nsub contains the number of capturing subpatterns in the
  7373. regular expression. Various error codes are defined in the header file.
  7374. NOTE: If the yield of regcomp() is non-zero, you must not attempt to
  7375. use the contents of the preg structure. If, for example, you pass it to
  7376. regexec(), the result is undefined and your program is likely to crash.
  7377. MATCHING NEWLINE CHARACTERS
  7378. This area is not simple, because POSIX and Perl take different views of
  7379. things. It is not possible to get PCRE to obey POSIX semantics, but
  7380. then PCRE was never intended to be a POSIX engine. The following table
  7381. lists the different possibilities for matching newline characters in
  7382. PCRE:
  7383. Default Change with
  7384. . matches newline no PCRE_DOTALL
  7385. newline matches [^a] yes not changeable
  7386. $ matches \n at end yes PCRE_DOLLARENDONLY
  7387. $ matches \n in middle no PCRE_MULTILINE
  7388. ^ matches \n in middle no PCRE_MULTILINE
  7389. This is the equivalent table for POSIX:
  7390. Default Change with
  7391. . matches newline yes REG_NEWLINE
  7392. newline matches [^a] yes REG_NEWLINE
  7393. $ matches \n at end no REG_NEWLINE
  7394. $ matches \n in middle no REG_NEWLINE
  7395. ^ matches \n in middle no REG_NEWLINE
  7396. PCRE's behaviour is the same as Perl's, except that there is no equiva-
  7397. lent for PCRE_DOLLAR_ENDONLY in Perl. In both PCRE and Perl, there is
  7398. no way to stop newline from matching [^a].
  7399. The default POSIX newline handling can be obtained by setting
  7400. PCRE_DOTALL and PCRE_DOLLAR_ENDONLY, but there is no way to make PCRE
  7401. behave exactly as for the REG_NEWLINE action.
  7402. MATCHING A PATTERN
  7403. The function regexec() is called to match a compiled pattern preg
  7404. against a given string, which is by default terminated by a zero byte
  7405. (but see REG_STARTEND below), subject to the options in eflags. These
  7406. can be:
  7407. REG_NOTBOL
  7408. The PCRE_NOTBOL option is set when calling the underlying PCRE matching
  7409. function.
  7410. REG_NOTEMPTY
  7411. The PCRE_NOTEMPTY option is set when calling the underlying PCRE match-
  7412. ing function. Note that REG_NOTEMPTY is not part of the POSIX standard.
  7413. However, setting this option can give more POSIX-like behaviour in some
  7414. situations.
  7415. REG_NOTEOL
  7416. The PCRE_NOTEOL option is set when calling the underlying PCRE matching
  7417. function.
  7418. REG_STARTEND
  7419. The string is considered to start at string + pmatch[0].rm_so and to
  7420. have a terminating NUL located at string + pmatch[0].rm_eo (there need
  7421. not actually be a NUL at that location), regardless of the value of
  7422. nmatch. This is a BSD extension, compatible with but not specified by
  7423. IEEE Standard 1003.2 (POSIX.2), and should be used with caution in
  7424. software intended to be portable to other systems. Note that a non-zero
  7425. rm_so does not imply REG_NOTBOL; REG_STARTEND affects only the location
  7426. of the string, not how it is matched.
  7427. If the pattern was compiled with the REG_NOSUB flag, no data about any
  7428. matched strings is returned. The nmatch and pmatch arguments of
  7429. regexec() are ignored.
  7430. If the value of nmatch is zero, or if the value pmatch is NULL, no data
  7431. about any matched strings is returned.
  7432. Otherwise,the portion of the string that was matched, and also any cap-
  7433. tured substrings, are returned via the pmatch argument, which points to
  7434. an array of nmatch structures of type regmatch_t, containing the mem-
  7435. bers rm_so and rm_eo. These contain the offset to the first character
  7436. of each substring and the offset to the first character after the end
  7437. of each substring, respectively. The 0th element of the vector relates
  7438. to the entire portion of string that was matched; subsequent elements
  7439. relate to the capturing subpatterns of the regular expression. Unused
  7440. entries in the array have both structure members set to -1.
  7441. A successful match yields a zero return; various error codes are
  7442. defined in the header file, of which REG_NOMATCH is the "expected"
  7443. failure code.
  7444. ERROR MESSAGES
  7445. The regerror() function maps a non-zero errorcode from either regcomp()
  7446. or regexec() to a printable message. If preg is not NULL, the error
  7447. should have arisen from the use of that structure. A message terminated
  7448. by a binary zero is placed in errbuf. The length of the message,
  7449. including the zero, is limited to errbuf_size. The yield of the func-
  7450. tion is the size of buffer needed to hold the whole message.
  7451. MEMORY USAGE
  7452. Compiling a regular expression causes memory to be allocated and asso-
  7453. ciated with the preg structure. The function regfree() frees all such
  7454. memory, after which preg may no longer be used as a compiled expres-
  7455. sion.
  7456. AUTHOR
  7457. Philip Hazel
  7458. University Computing Service
  7459. Cambridge CB2 3QH, England.
  7460. REVISION
  7461. Last updated: 09 January 2012
  7462. Copyright (c) 1997-2012 University of Cambridge.
  7463. ------------------------------------------------------------------------------
  7464. PCRECPP(3) Library Functions Manual PCRECPP(3)
  7465. NAME
  7466. PCRE - Perl-compatible regular expressions.
  7467. SYNOPSIS OF C++ WRAPPER
  7468. #include <pcrecpp.h>
  7469. DESCRIPTION
  7470. The C++ wrapper for PCRE was provided by Google Inc. Some additional
  7471. functionality was added by Giuseppe Maxia. This brief man page was con-
  7472. structed from the notes in the pcrecpp.h file, which should be con-
  7473. sulted for further details. Note that the C++ wrapper supports only the
  7474. original 8-bit PCRE library. There is no 16-bit or 32-bit support at
  7475. present.
  7476. MATCHING INTERFACE
  7477. The "FullMatch" operation checks that supplied text matches a supplied
  7478. pattern exactly. If pointer arguments are supplied, it copies matched
  7479. sub-strings that match sub-patterns into them.
  7480. Example: successful match
  7481. pcrecpp::RE re("h.*o");
  7482. re.FullMatch("hello");
  7483. Example: unsuccessful match (requires full match):
  7484. pcrecpp::RE re("e");
  7485. !re.FullMatch("hello");
  7486. Example: creating a temporary RE object:
  7487. pcrecpp::RE("h.*o").FullMatch("hello");
  7488. You can pass in a "const char*" or a "string" for "text". The examples
  7489. below tend to use a const char*. You can, as in the different examples
  7490. above, store the RE object explicitly in a variable or use a temporary
  7491. RE object. The examples below use one mode or the other arbitrarily.
  7492. Either could correctly be used for any of these examples.
  7493. You must supply extra pointer arguments to extract matched subpieces.
  7494. Example: extracts "ruby" into "s" and 1234 into "i"
  7495. int i;
  7496. string s;
  7497. pcrecpp::RE re("(\\w+):(\\d+)");
  7498. re.FullMatch("ruby:1234", &s, &i);
  7499. Example: does not try to extract any extra sub-patterns
  7500. re.FullMatch("ruby:1234", &s);
  7501. Example: does not try to extract into NULL
  7502. re.FullMatch("ruby:1234", NULL, &i);
  7503. Example: integer overflow causes failure
  7504. !re.FullMatch("ruby:1234567891234", NULL, &i);
  7505. Example: fails because there aren't enough sub-patterns:
  7506. !pcrecpp::RE("\\w+:\\d+").FullMatch("ruby:1234", &s);
  7507. Example: fails because string cannot be stored in integer
  7508. !pcrecpp::RE("(.*)").FullMatch("ruby", &i);
  7509. The provided pointer arguments can be pointers to any scalar numeric
  7510. type, or one of:
  7511. string (matched piece is copied to string)
  7512. StringPiece (StringPiece is mutated to point to matched piece)
  7513. T (where "bool T::ParseFrom(const char*, int)" exists)
  7514. NULL (the corresponding matched sub-pattern is not copied)
  7515. The function returns true iff all of the following conditions are sat-
  7516. isfied:
  7517. a. "text" matches "pattern" exactly;
  7518. b. The number of matched sub-patterns is >= number of supplied
  7519. pointers;
  7520. c. The "i"th argument has a suitable type for holding the
  7521. string captured as the "i"th sub-pattern. If you pass in
  7522. void * NULL for the "i"th argument, or a non-void * NULL
  7523. of the correct type, or pass fewer arguments than the
  7524. number of sub-patterns, "i"th captured sub-pattern is
  7525. ignored.
  7526. CAVEAT: An optional sub-pattern that does not exist in the matched
  7527. string is assigned the empty string. Therefore, the following will
  7528. return false (because the empty string is not a valid number):
  7529. int number;
  7530. pcrecpp::RE::FullMatch("abc", "[a-z]+(\\d+)?", &number);
  7531. The matching interface supports at most 16 arguments per call. If you
  7532. need more, consider using the more general interface
  7533. pcrecpp::RE::DoMatch. See pcrecpp.h for the signature for DoMatch.
  7534. NOTE: Do not use no_arg, which is used internally to mark the end of a
  7535. list of optional arguments, as a placeholder for missing arguments, as
  7536. this can lead to segfaults.
  7537. QUOTING METACHARACTERS
  7538. You can use the "QuoteMeta" operation to insert backslashes before all
  7539. potentially meaningful characters in a string. The returned string,
  7540. used as a regular expression, will exactly match the original string.
  7541. Example:
  7542. string quoted = RE::QuoteMeta(unquoted);
  7543. Note that it's legal to escape a character even if it has no special
  7544. meaning in a regular expression -- so this function does that. (This
  7545. also makes it identical to the perl function of the same name; see
  7546. "perldoc -f quotemeta".) For example, "1.5-2.0?" becomes
  7547. "1\.5\-2\.0\?".
  7548. PARTIAL MATCHES
  7549. You can use the "PartialMatch" operation when you want the pattern to
  7550. match any substring of the text.
  7551. Example: simple search for a string:
  7552. pcrecpp::RE("ell").PartialMatch("hello");
  7553. Example: find first number in a string:
  7554. int number;
  7555. pcrecpp::RE re("(\\d+)");
  7556. re.PartialMatch("x*100 + 20", &number);
  7557. assert(number == 100);
  7558. UTF-8 AND THE MATCHING INTERFACE
  7559. By default, pattern and text are plain text, one byte per character.
  7560. The UTF8 flag, passed to the constructor, causes both pattern and
  7561. string to be treated as UTF-8 text, still a byte stream but potentially
  7562. multiple bytes per character. In practice, the text is likelier to be
  7563. UTF-8 than the pattern, but the match returned may depend on the UTF8
  7564. flag, so always use it when matching UTF8 text. For example, "." will
  7565. match one byte normally but with UTF8 set may match up to three bytes
  7566. of a multi-byte character.
  7567. Example:
  7568. pcrecpp::RE_Options options;
  7569. options.set_utf8();
  7570. pcrecpp::RE re(utf8_pattern, options);
  7571. re.FullMatch(utf8_string);
  7572. Example: using the convenience function UTF8():
  7573. pcrecpp::RE re(utf8_pattern, pcrecpp::UTF8());
  7574. re.FullMatch(utf8_string);
  7575. NOTE: The UTF8 flag is ignored if pcre was not configured with the
  7576. --enable-utf8 flag.
  7577. PASSING MODIFIERS TO THE REGULAR EXPRESSION ENGINE
  7578. PCRE defines some modifiers to change the behavior of the regular
  7579. expression engine. The C++ wrapper defines an auxiliary class,
  7580. RE_Options, as a vehicle to pass such modifiers to a RE class. Cur-
  7581. rently, the following modifiers are supported:
  7582. modifier description Perl corresponding
  7583. PCRE_CASELESS case insensitive match /i
  7584. PCRE_MULTILINE multiple lines match /m
  7585. PCRE_DOTALL dot matches newlines /s
  7586. PCRE_DOLLAR_ENDONLY $ matches only at end N/A
  7587. PCRE_EXTRA strict escape parsing N/A
  7588. PCRE_EXTENDED ignore white spaces /x
  7589. PCRE_UTF8 handles UTF8 chars built-in
  7590. PCRE_UNGREEDY reverses * and *? N/A
  7591. PCRE_NO_AUTO_CAPTURE disables capturing parens N/A (*)
  7592. (*) Both Perl and PCRE allow non capturing parentheses by means of the
  7593. "?:" modifier within the pattern itself. e.g. (?:ab|cd) does not cap-
  7594. ture, while (ab|cd) does.
  7595. For a full account on how each modifier works, please check the PCRE
  7596. API reference page.
  7597. For each modifier, there are two member functions whose name is made
  7598. out of the modifier in lowercase, without the "PCRE_" prefix. For
  7599. instance, PCRE_CASELESS is handled by
  7600. bool caseless()
  7601. which returns true if the modifier is set, and
  7602. RE_Options & set_caseless(bool)
  7603. which sets or unsets the modifier. Moreover, PCRE_EXTRA_MATCH_LIMIT can
  7604. be accessed through the set_match_limit() and match_limit() member
  7605. functions. Setting match_limit to a non-zero value will limit the exe-
  7606. cution of pcre to keep it from doing bad things like blowing the stack
  7607. or taking an eternity to return a result. A value of 5000 is good
  7608. enough to stop stack blowup in a 2MB thread stack. Setting match_limit
  7609. to zero disables match limiting. Alternatively, you can call
  7610. match_limit_recursion() which uses PCRE_EXTRA_MATCH_LIMIT_RECURSION to
  7611. limit how much PCRE recurses. match_limit() limits the number of
  7612. matches PCRE does; match_limit_recursion() limits the depth of internal
  7613. recursion, and therefore the amount of stack that is used.
  7614. Normally, to pass one or more modifiers to a RE class, you declare a
  7615. RE_Options object, set the appropriate options, and pass this object to
  7616. a RE constructor. Example:
  7617. RE_Options opt;
  7618. opt.set_caseless(true);
  7619. if (RE("HELLO", opt).PartialMatch("hello world")) ...
  7620. RE_options has two constructors. The default constructor takes no argu-
  7621. ments and creates a set of flags that are off by default. The optional
  7622. parameter option_flags is to facilitate transfer of legacy code from C
  7623. programs. This lets you do
  7624. RE(pattern,
  7625. RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str);
  7626. However, new code is better off doing
  7627. RE(pattern,
  7628. RE_Options().set_caseless(true).set_multiline(true))
  7629. .PartialMatch(str);
  7630. If you are going to pass one of the most used modifiers, there are some
  7631. convenience functions that return a RE_Options class with the appropri-
  7632. ate modifier already set: CASELESS(), UTF8(), MULTILINE(), DOTALL(),
  7633. and EXTENDED().
  7634. If you need to set several options at once, and you don't want to go
  7635. through the pains of declaring a RE_Options object and setting several
  7636. options, there is a parallel method that give you such ability on the
  7637. fly. You can concatenate several set_xxxxx() member functions, since
  7638. each of them returns a reference to its class object. For example, to
  7639. pass PCRE_CASELESS, PCRE_EXTENDED, and PCRE_MULTILINE to a RE with one
  7640. statement, you may write:
  7641. RE(" ^ xyz \\s+ .* blah$",
  7642. RE_Options()
  7643. .set_caseless(true)
  7644. .set_extended(true)
  7645. .set_multiline(true)).PartialMatch(sometext);
  7646. SCANNING TEXT INCREMENTALLY
  7647. The "Consume" operation may be useful if you want to repeatedly match
  7648. regular expressions at the front of a string and skip over them as they
  7649. match. This requires use of the "StringPiece" type, which represents a
  7650. sub-range of a real string. Like RE, StringPiece is defined in the
  7651. pcrecpp namespace.
  7652. Example: read lines of the form "var = value" from a string.
  7653. string contents = ...; // Fill string somehow
  7654. pcrecpp::StringPiece input(contents); // Wrap in a StringPiece
  7655. string var;
  7656. int value;
  7657. pcrecpp::RE re("(\\w+) = (\\d+)\n");
  7658. while (re.Consume(&input, &var, &value)) {
  7659. ...;
  7660. }
  7661. Each successful call to "Consume" will set "var/value", and also
  7662. advance "input" so it points past the matched text.
  7663. The "FindAndConsume" operation is similar to "Consume" but does not
  7664. anchor your match at the beginning of the string. For example, you
  7665. could extract all words from a string by repeatedly calling
  7666. pcrecpp::RE("(\\w+)").FindAndConsume(&input, &word)
  7667. PARSING HEX/OCTAL/C-RADIX NUMBERS
  7668. By default, if you pass a pointer to a numeric value, the corresponding
  7669. text is interpreted as a base-10 number. You can instead wrap the
  7670. pointer with a call to one of the operators Hex(), Octal(), or CRadix()
  7671. to interpret the text in another base. The CRadix operator interprets
  7672. C-style "0" (base-8) and "0x" (base-16) prefixes, but defaults to
  7673. base-10.
  7674. Example:
  7675. int a, b, c, d;
  7676. pcrecpp::RE re("(.*) (.*) (.*) (.*)");
  7677. re.FullMatch("100 40 0100 0x40",
  7678. pcrecpp::Octal(&a), pcrecpp::Hex(&b),
  7679. pcrecpp::CRadix(&c), pcrecpp::CRadix(&d));
  7680. will leave 64 in a, b, c, and d.
  7681. REPLACING PARTS OF STRINGS
  7682. You can replace the first match of "pattern" in "str" with "rewrite".
  7683. Within "rewrite", backslash-escaped digits (\1 to \9) can be used to
  7684. insert text matching corresponding parenthesized group from the pat-
  7685. tern. \0 in "rewrite" refers to the entire matching text. For example:
  7686. string s = "yabba dabba doo";
  7687. pcrecpp::RE("b+").Replace("d", &s);
  7688. will leave "s" containing "yada dabba doo". The result is true if the
  7689. pattern matches and a replacement occurs, false otherwise.
  7690. GlobalReplace is like Replace except that it replaces all occurrences
  7691. of the pattern in the string with the rewrite. Replacements are not
  7692. subject to re-matching. For example:
  7693. string s = "yabba dabba doo";
  7694. pcrecpp::RE("b+").GlobalReplace("d", &s);
  7695. will leave "s" containing "yada dada doo". It returns the number of
  7696. replacements made.
  7697. Extract is like Replace, except that if the pattern matches, "rewrite"
  7698. is copied into "out" (an additional argument) with substitutions. The
  7699. non-matching portions of "text" are ignored. Returns true iff a match
  7700. occurred and the extraction happened successfully; if no match occurs,
  7701. the string is left unaffected.
  7702. AUTHOR
  7703. The C++ wrapper was contributed by Google Inc.
  7704. Copyright (c) 2007 Google Inc.
  7705. REVISION
  7706. Last updated: 08 January 2012
  7707. ------------------------------------------------------------------------------
  7708. PCRESAMPLE(3) Library Functions Manual PCRESAMPLE(3)
  7709. NAME
  7710. PCRE - Perl-compatible regular expressions
  7711. PCRE SAMPLE PROGRAM
  7712. A simple, complete demonstration program, to get you started with using
  7713. PCRE, is supplied in the file pcredemo.c in the PCRE distribution. A
  7714. listing of this program is given in the pcredemo documentation. If you
  7715. do not have a copy of the PCRE distribution, you can save this listing
  7716. to re-create pcredemo.c.
  7717. The demonstration program, which uses the original PCRE 8-bit library,
  7718. compiles the regular expression that is its first argument, and matches
  7719. it against the subject string in its second argument. No PCRE options
  7720. are set, and default character tables are used. If matching succeeds,
  7721. the program outputs the portion of the subject that matched, together
  7722. with the contents of any captured substrings.
  7723. If the -g option is given on the command line, the program then goes on
  7724. to check for further matches of the same regular expression in the same
  7725. subject string. The logic is a little bit tricky because of the possi-
  7726. bility of matching an empty string. Comments in the code explain what
  7727. is going on.
  7728. If PCRE is installed in the standard include and library directories
  7729. for your operating system, you should be able to compile the demonstra-
  7730. tion program using this command:
  7731. gcc -o pcredemo pcredemo.c -lpcre
  7732. If PCRE is installed elsewhere, you may need to add additional options
  7733. to the command line. For example, on a Unix-like system that has PCRE
  7734. installed in /usr/local, you can compile the demonstration program
  7735. using a command like this:
  7736. gcc -o pcredemo -I/usr/local/include pcredemo.c \
  7737. -L/usr/local/lib -lpcre
  7738. In a Windows environment, if you want to statically link the program
  7739. against a non-dll pcre.a file, you must uncomment the line that defines
  7740. PCRE_STATIC before including pcre.h, because otherwise the pcre_mal-
  7741. loc() and pcre_free() exported functions will be declared
  7742. __declspec(dllimport), with unwanted results.
  7743. Once you have compiled and linked the demonstration program, you can
  7744. run simple tests like this:
  7745. ./pcredemo 'cat|dog' 'the cat sat on the mat'
  7746. ./pcredemo -g 'cat|dog' 'the dog sat on the cat'
  7747. Note that there is a much more comprehensive test program, called
  7748. pcretest, which supports many more facilities for testing regular
  7749. expressions and both PCRE libraries. The pcredemo program is provided
  7750. as a simple coding example.
  7751. If you try to run pcredemo when PCRE is not installed in the standard
  7752. library directory, you may get an error like this on some operating
  7753. systems (e.g. Solaris):
  7754. ld.so.1: a.out: fatal: libpcre.so.0: open failed: No such file or
  7755. directory
  7756. This is caused by the way shared library support works on those sys-
  7757. tems. You need to add
  7758. -R/usr/local/lib
  7759. (for example) to the compile command to get round this problem.
  7760. AUTHOR
  7761. Philip Hazel
  7762. University Computing Service
  7763. Cambridge CB2 3QH, England.
  7764. REVISION
  7765. Last updated: 10 January 2012
  7766. Copyright (c) 1997-2012 University of Cambridge.
  7767. ------------------------------------------------------------------------------
  7768. PCRELIMITS(3) Library Functions Manual PCRELIMITS(3)
  7769. NAME
  7770. PCRE - Perl-compatible regular expressions
  7771. SIZE AND OTHER LIMITATIONS
  7772. There are some size limitations in PCRE but it is hoped that they will
  7773. never in practice be relevant.
  7774. The maximum length of a compiled pattern is approximately 64K data
  7775. units (bytes for the 8-bit library, 16-bit units for the 16-bit
  7776. library, and 32-bit units for the 32-bit library) if PCRE is compiled
  7777. with the default internal linkage size, which is 2 bytes for the 8-bit
  7778. and 16-bit libraries, and 4 bytes for the 32-bit library. If you want
  7779. to process regular expressions that are truly enormous, you can compile
  7780. PCRE with an internal linkage size of 3 or 4 (when building the 16-bit
  7781. or 32-bit library, 3 is rounded up to 4). See the README file in the
  7782. source distribution and the pcrebuild documentation for details. In
  7783. these cases the limit is substantially larger. However, the speed of
  7784. execution is slower.
  7785. All values in repeating quantifiers must be less than 65536.
  7786. There is no limit to the number of parenthesized subpatterns, but there
  7787. can be no more than 65535 capturing subpatterns. There is, however, a
  7788. limit to the depth of nesting of parenthesized subpatterns of all
  7789. kinds. This is imposed in order to limit the amount of system stack
  7790. used at compile time. The limit can be specified when PCRE is built;
  7791. the default is 250.
  7792. There is a limit to the number of forward references to subsequent sub-
  7793. patterns of around 200,000. Repeated forward references with fixed
  7794. upper limits, for example, (?2){0,100} when subpattern number 2 is to
  7795. the right, are included in the count. There is no limit to the number
  7796. of backward references.
  7797. The maximum length of name for a named subpattern is 32 characters, and
  7798. the maximum number of named subpatterns is 10000.
  7799. The maximum length of a name in a (*MARK), (*PRUNE), (*SKIP), or
  7800. (*THEN) verb is 255 for the 8-bit library and 65535 for the 16-bit and
  7801. 32-bit libraries.
  7802. The maximum length of a subject string is the largest positive number
  7803. that an integer variable can hold. However, when using the traditional
  7804. matching function, PCRE uses recursion to handle subpatterns and indef-
  7805. inite repetition. This means that the available stack space may limit
  7806. the size of a subject string that can be processed by certain patterns.
  7807. For a discussion of stack issues, see the pcrestack documentation.
  7808. AUTHOR
  7809. Philip Hazel
  7810. University Computing Service
  7811. Cambridge CB2 3QH, England.
  7812. REVISION
  7813. Last updated: 05 November 2013
  7814. Copyright (c) 1997-2013 University of Cambridge.
  7815. ------------------------------------------------------------------------------
  7816. PCRESTACK(3) Library Functions Manual PCRESTACK(3)
  7817. NAME
  7818. PCRE - Perl-compatible regular expressions
  7819. PCRE DISCUSSION OF STACK USAGE
  7820. When you call pcre[16|32]_exec(), it makes use of an internal function
  7821. called match(). This calls itself recursively at branch points in the
  7822. pattern, in order to remember the state of the match so that it can
  7823. back up and try a different alternative if the first one fails. As
  7824. matching proceeds deeper and deeper into the tree of possibilities, the
  7825. recursion depth increases. The match() function is also called in other
  7826. circumstances, for example, whenever a parenthesized sub-pattern is
  7827. entered, and in certain cases of repetition.
  7828. Not all calls of match() increase the recursion depth; for an item such
  7829. as a* it may be called several times at the same level, after matching
  7830. different numbers of a's. Furthermore, in a number of cases where the
  7831. result of the recursive call would immediately be passed back as the
  7832. result of the current call (a "tail recursion"), the function is just
  7833. restarted instead.
  7834. The above comments apply when pcre[16|32]_exec() is run in its normal
  7835. interpretive manner. If the pattern was studied with the
  7836. PCRE_STUDY_JIT_COMPILE option, and just-in-time compiling was success-
  7837. ful, and the options passed to pcre[16|32]_exec() were not incompati-
  7838. ble, the matching process uses the JIT-compiled code instead of the
  7839. match() function. In this case, the memory requirements are handled
  7840. entirely differently. See the pcrejit documentation for details.
  7841. The pcre[16|32]_dfa_exec() function operates in an entirely different
  7842. way, and uses recursion only when there is a regular expression recur-
  7843. sion or subroutine call in the pattern. This includes the processing of
  7844. assertion and "once-only" subpatterns, which are handled like subrou-
  7845. tine calls. Normally, these are never very deep, and the limit on the
  7846. complexity of pcre[16|32]_dfa_exec() is controlled by the amount of
  7847. workspace it is given. However, it is possible to write patterns with
  7848. runaway infinite recursions; such patterns will cause
  7849. pcre[16|32]_dfa_exec() to run out of stack. At present, there is no
  7850. protection against this.
  7851. The comments that follow do NOT apply to pcre[16|32]_dfa_exec(); they
  7852. are relevant only for pcre[16|32]_exec() without the JIT optimization.
  7853. Reducing pcre[16|32]_exec()'s stack usage
  7854. Each time that match() is actually called recursively, it uses memory
  7855. from the process stack. For certain kinds of pattern and data, very
  7856. large amounts of stack may be needed, despite the recognition of "tail
  7857. recursion". You can often reduce the amount of recursion, and there-
  7858. fore the amount of stack used, by modifying the pattern that is being
  7859. matched. Consider, for example, this pattern:
  7860. ([^<]|<(?!inet))+
  7861. It matches from wherever it starts until it encounters "<inet" or the
  7862. end of the data, and is the kind of pattern that might be used when
  7863. processing an XML file. Each iteration of the outer parentheses matches
  7864. either one character that is not "<" or a "<" that is not followed by
  7865. "inet". However, each time a parenthesis is processed, a recursion
  7866. occurs, so this formulation uses a stack frame for each matched charac-
  7867. ter. For a long string, a lot of stack is required. Consider now this
  7868. rewritten pattern, which matches exactly the same strings:
  7869. ([^<]++|<(?!inet))+
  7870. This uses very much less stack, because runs of characters that do not
  7871. contain "<" are "swallowed" in one item inside the parentheses. Recur-
  7872. sion happens only when a "<" character that is not followed by "inet"
  7873. is encountered (and we assume this is relatively rare). A possessive
  7874. quantifier is used to stop any backtracking into the runs of non-"<"
  7875. characters, but that is not related to stack usage.
  7876. This example shows that one way of avoiding stack problems when match-
  7877. ing long subject strings is to write repeated parenthesized subpatterns
  7878. to match more than one character whenever possible.
  7879. Compiling PCRE to use heap instead of stack for pcre[16|32]_exec()
  7880. In environments where stack memory is constrained, you might want to
  7881. compile PCRE to use heap memory instead of stack for remembering back-
  7882. up points when pcre[16|32]_exec() is running. This makes it run a lot
  7883. more slowly, however. Details of how to do this are given in the pcre-
  7884. build documentation. When built in this way, instead of using the
  7885. stack, PCRE obtains and frees memory by calling the functions that are
  7886. pointed to by the pcre[16|32]_stack_malloc and pcre[16|32]_stack_free
  7887. variables. By default, these point to malloc() and free(), but you can
  7888. replace the pointers to cause PCRE to use your own functions. Since the
  7889. block sizes are always the same, and are always freed in reverse order,
  7890. it may be possible to implement customized memory handlers that are
  7891. more efficient than the standard functions.
  7892. Limiting pcre[16|32]_exec()'s stack usage
  7893. You can set limits on the number of times that match() is called, both
  7894. in total and recursively. If a limit is exceeded, pcre[16|32]_exec()
  7895. returns an error code. Setting suitable limits should prevent it from
  7896. running out of stack. The default values of the limits are very large,
  7897. and unlikely ever to operate. They can be changed when PCRE is built,
  7898. and they can also be set when pcre[16|32]_exec() is called. For details
  7899. of these interfaces, see the pcrebuild documentation and the section on
  7900. extra data for pcre[16|32]_exec() in the pcreapi documentation.
  7901. As a very rough rule of thumb, you should reckon on about 500 bytes per
  7902. recursion. Thus, if you want to limit your stack usage to 8Mb, you
  7903. should set the limit at 16000 recursions. A 64Mb stack, on the other
  7904. hand, can support around 128000 recursions.
  7905. In Unix-like environments, the pcretest test program has a command line
  7906. option (-S) that can be used to increase the size of its stack. As long
  7907. as the stack is large enough, another option (-M) can be used to find
  7908. the smallest limits that allow a particular pattern to match a given
  7909. subject string. This is done by calling pcre[16|32]_exec() repeatedly
  7910. with different limits.
  7911. Obtaining an estimate of stack usage
  7912. The actual amount of stack used per recursion can vary quite a lot,
  7913. depending on the compiler that was used to build PCRE and the optimiza-
  7914. tion or debugging options that were set for it. The rule of thumb value
  7915. of 500 bytes mentioned above may be larger or smaller than what is
  7916. actually needed. A better approximation can be obtained by running this
  7917. command:
  7918. pcretest -m -C
  7919. The -C option causes pcretest to output information about the options
  7920. with which PCRE was compiled. When -m is also given (before -C), infor-
  7921. mation about stack use is given in a line like this:
  7922. Match recursion uses stack: approximate frame size = 640 bytes
  7923. The value is approximate because some recursions need a bit more (up to
  7924. perhaps 16 more bytes).
  7925. If the above command is given when PCRE is compiled to use the heap
  7926. instead of the stack for recursion, the value that is output is the
  7927. size of each block that is obtained from the heap.
  7928. Changing stack size in Unix-like systems
  7929. In Unix-like environments, there is not often a problem with the stack
  7930. unless very long strings are involved, though the default limit on
  7931. stack size varies from system to system. Values from 8Mb to 64Mb are
  7932. common. You can find your default limit by running the command:
  7933. ulimit -s
  7934. Unfortunately, the effect of running out of stack is often SIGSEGV,
  7935. though sometimes a more explicit error message is given. You can nor-
  7936. mally increase the limit on stack size by code such as this:
  7937. struct rlimit rlim;
  7938. getrlimit(RLIMIT_STACK, &rlim);
  7939. rlim.rlim_cur = 100*1024*1024;
  7940. setrlimit(RLIMIT_STACK, &rlim);
  7941. This reads the current limits (soft and hard) using getrlimit(), then
  7942. attempts to increase the soft limit to 100Mb using setrlimit(). You
  7943. must do this before calling pcre[16|32]_exec().
  7944. Changing stack size in Mac OS X
  7945. Using setrlimit(), as described above, should also work on Mac OS X. It
  7946. is also possible to set a stack size when linking a program. There is a
  7947. discussion about stack sizes in Mac OS X at this web site:
  7948. http://developer.apple.com/qa/qa2005/qa1419.html.
  7949. AUTHOR
  7950. Philip Hazel
  7951. University Computing Service
  7952. Cambridge CB2 3QH, England.
  7953. REVISION
  7954. Last updated: 24 June 2012
  7955. Copyright (c) 1997-2012 University of Cambridge.
  7956. ------------------------------------------------------------------------------