Greta 正则表达式Word格式.docx
《Greta 正则表达式Word格式.docx》由会员分享,可在线阅读,更多相关《Greta 正则表达式Word格式.docx(34页珍藏版)》请在冰豆网上搜索。
![Greta 正则表达式Word格式.docx](https://file1.bdocx.com/fileroot1/2022-11/25/3dd92cc3-2e46-4756-89e4-2dab43e84d4a/3dd92cc3-2e46-4756-89e4-2dab43e84d4a1.gif)
cgroups
match_results,subst_resultsandsplit_results
match_results:
cbackrefs
backref
rstart
rlength
all_backrefs
subst_results:
backref_str
split_results:
strings
TheSyntaxModule
register_intrinsic_charset
CustomizingYourSearch
NOCASE
GLOBAL
MULTILINE
SINGLELINE
EXTENDED
RIGHTMOST
NOBACKREFS
ALLBACKREFS
FIRSTBACKREFS
NORMALIZE
MatchingModes
MODE_FAST
MODE_SAFE
MODE_MIXED
KnownIssuesandPerlIncompatibilities
EmbeddedCodeinaRegularExpression
PatternModifierScope
CommentBlocksBeforeQuantifiers
VariableWidthLook-BehindAssertions
RecursivePatterns
Compile-TimeSwitches
REGEX_WIDE_AND_NARROW
REGEX_POSIX
REGEX_NO_PERL
REGEX_DEBUG
REGEX_DEBUG_HEAP
REGEX_STACK_ALIGNMENT
REGEX_FOLD_INSTANTIATIONS
REGEX_TO_INSTANTIATE
Miscellaneous
StaticConstPatterns
Thread-safety
StackUsage
DBCS
STL
VC7andManagedCode
TemplateInstantiation
ContactInformation
Appendix1:
History
Appendix2:
ImplementationDetails
TheregularexpressiontemplatelibrarycontainsobjectsandfunctionsthatmakeitpossibletoperformpatternmatchingandsubstitutiononstringsinC++.
Theyare:
∙rpattern:
thepatterntouseduringthesearch.
∙match_results/subst_results:
containerfortheresultsofamatch/substitution.
Toperformasearchorreplaceoperation,youwilltypicallyfirstinitializean
rpattern
objectbygivingitastringrepresentingthepatternagainstwhichtomatch.
Youwillthencallamethodontherpatternobject(match()
or
substitute(),forinstance),passingitastringtomatchagainstanda
match_results
objectstoreceivetheresultsofthematch.
Ifthe
match()/substitute()
fails,themethodreturnsfalse.
Ifitsucceeds,itreturnstrue,andthe
objectstorestheresultingarrayof
backreferences
internally.
(Here,theterm
backreference
hasthesamemeaningasitdoesinPerl.
Backreferencesprovideextrainformationaboutwhatpartsofthepatternmatchedwhichpartsofthestring.)
Therearemethodsonthe
objecttomakethebackreferenceinformationavailable.
Forexample:
#include<
iostream>
string>
#include“regexpr2.h”
usingnamespacestd;
usingnamespaceregex;
intmain(){
match_resultsresults;
stringstr(“Thebookcost$12.34”);
rpatternpat(“\\$(\\d+)(\\.(\\d\\d))?
”);
//Matchadollarsignfollowedbyoneormoredigits,
//optionallyfollowedbyaperiodandtwomoredigits.
//Thedouble-escapesarenecessarytosatisfythecompiler.
match_results:
backref_typebr=pat.match(str,results);
if(br.matched){
cout<
<
“matchsuccess!
”<
endl;
“price:
”<
br<
}else{
“matchfailed!
}
return0;
}
Theaboveprogramwouldprintoutthefollowing:
matchsuccess!
price:
$12.34
Thefollowingsectionsdiscussthe
objectindetailandhowtocustomizeyoursearchestobefasterandmoreefficient.
Note:
alldeclarationsintheheaderfile(regexpr2.h)arecontainedinthe
regex
namespace.
Touseanyoftheobjects,methodsorenumerationsdescribedinthisdocument,youmustprependalldeclarationswith“regex:
”oryoumusthavethe“usingnamespaceregex;
”directivesomewherewithintheenclosingscopeofyourdeclarations.
Forsimplicity,I’veleftoffthe“regex:
”prefixesintherestofmycodesnippets.
Differentregexenginesaregoodondifferenttypesofpatterns.
Thatsaid,Ihavefoundmyregexenginetobeprettyquick.
Forabenchmark,Imatchedthepattern“^([0-9]+)(\-||$)(.*)$”againstthestring“100-thisisalineofftpresponsewhichcontainsamessagestring”.
GRETAisabout7timesfasterthantheregexlibraryinboost(http:
//www.boost.org),andabout10timesfasterthantheregularexpressionclassesinATL7.
Forthisinput,GRETAisevenfasterthanPerl,althoughPerlisfasterforsomeotherpatterns.
MostregexenginesIhaveseenbuildupanNFA
(non-deterministicfinitestateautomaton)
andexecuteititeratively,oftenwithabig,slowswitchstatement.
Ihaveadifferentapproach:
patternsarecompiledintoadirected,possiblycyclicgraph,andmatchinghappensbytraversingthisgraphrecursively.
Inaddition,thecodemakesheavyuseoftemplatestofreezethestateoftheflagsintothecompiledpatternsothattheydon’tneedtobecheckedatmatchtime.
Theresultisaprettyleanblobofcodethatcanmatchyourpatternquickly.
Eventhebestalgorithmshavetheirweaknesses,though.
MatchingregularexpressionswithbackreferencesisanNP-completeproblem.
Therearepatternsthatwillmakeanybacktrackingregexenginetakeexponentialtimetofinish.
(Theseusuallyinvolvenestedquantifiers.)
Ifyouhaveaperformancecriticalapp,youwouldbesmarttotestyourpatternsforspeed,orprofileyourapptomakesureyouarenotspendingtoomuchtimethrashingaroundintheregexcode.
You’vebeenwarned!
Also,seethesection
VC7andManagedCode
forsomeadviceforcompilingGRETAunderVC7.
Manythingshavechangedsinceversion1.xoftheRegularExpressionTemplateArchive.
Ifyouhavecodewhichusesversion1.x,youwillnotbeabletouseversion2withoutmakingchangestoyourcode.
Sorry!
Therewereanumberofunsafe,unintuitiveinterfacefeaturesofversion1thatIfeltwereworthfixingforversion2.
Ifyouneedversion1,IhaveacopyandI’dbehappytogiveittoyou.
Mostnotably,the
regexpr
objecthasgoneaway.
Itwasasubclassof
std:
string,with
match()
and
substitute()
methods,anditstoredtheresultsofthematch/substituteinternally.
Subclassing
string
isdangerousbecause
doesn’thaveavirtualdestructor.
Also,matchingisconceptuallyaconstoperation,anditseemedwrongthatitshouldchangeinternalstate.
The
match/count/substitute
methodshavemovedtothe
object.
Thestatethatusedtobestoredinthe
objectisnowputinamatch_results/subst_results
container,whichispassedasanoutparametertothe
match/substitute
methods.
Also,the
CSTRINGS
flaghasgoneaway.
ItisnolongernecessarytooptimizeapatternforusewithC-styleNULL-terminatedstrings.
WhenyoupassaC-stylestringtothe
matchmethod,thesameoptimizationisusedautomatically.
(Inearly2.Xversionsofthelibrary,therewasa
basic_rpattern_c
objectforperformingthisoptimization,butitisnolongernecessaryandhasbeendeprecated.)
Anotherminorchangeinvolvesthe
register_intrinsic_charset()
method.
Itusedtobeapartof
rpattern’sinterface,butithasmovedtothesyntaxmodule.
Despitethesweepinginterfacechanges,themajorityoftheback-endcodeisunchanged.
Youshouldexpectpatternsthatworkedinversion1.xtocontinuetoworkinversion2.
objectcontainstheregularexpressionpatternagainstwhichtomatch.
Italsoexposesthe
match(),
substitute(),and
count()
methodsyouwillusetoperformregularexpressionmatches.
Whenyouinstantiatean
object,thepatternis“compiled”intoastructurethatspeedsuppatternmatching.
Oncecompiled,youmayreusethesamepatternformultiplematchoperations.
Hereishow
isdeclared:
template<
typenameCI,
typenameSY=perl_syntax<
iterator_traits<
CI>
value_type>
>
classbasic_rpattern{
…
};
typedefbasic_rpattern<
basic_string<
TCHAR>
const_iterator>
rpattern;
TCHARconst*>
rpattern_c;
classisatemplateoniteratortype.
Itisalsoatemplateonthesyntaxmodule.
Bydefault,thePerlsyntaxmoduleisused,butyouarefreetowriteyourownsyntaxandspecifyitasatemplateparameter.
Seethesectiononthe
SyntaxModule.
Thefollowingsectionsdescribethemethodsavailableonthe
object.
string_type
isatypedefthatisusedinmanyofthefollowingfunctionprototypes.
Itisdefinedasfollows:
typedefCIconst_iterator;
typedefstd:
value_typechar_type;
char_type>
string_type;
Thetypedefisalittlecomplicated,butitseffectiswhatyouwouldexpect.
Iftheresultofdereferencinga
const_iterator
isa
char,then
isthesameas
string.
Ifdereferencinga
resultsina
wchar_t,then
wstring.
Therearetwoconstructorsforinstantiatingan
Herearetheirprototypes:
rpattern(
const
&
pat,
REGEX_FLAGSflags=NOFLAGS,
REGEX_MODEmode=MODE_DEFAULT);
//throw(bad_alloc,bad