ATL 正则表达式, CAtlRegExp
/*最近简单看了下ATL中的正则表达式,感觉不好用,主要是她的正则表达式规则太简单,有些情况匹配不出来,当然也许是我自己对正则表达式都不是很熟练,所以写不出匹配的规则,不过还是要记录下他的使用方法*/CAtlRegExpregexp;//这里模板中不用写任何东西REParseErrorstatus=reg.Parse("{\\d\\d\\d\\d\\d\\d?}");//这里传入匹配的规则if(REPARSE_ERROR_OK!=status)//REPARSE_ERROR_OK表明解析正确{return;//解析不正确返回}CAtlREMatchContextmc;//模板中同样不用写任何东西,保持匹配的结果BOOLmatched=reg.Match(input.c_str(),&mc);//查找匹配,if(matched)//matched为true,表明有匹配的字符串,{constCAtlREMatchContext::RECHAR*szStart=0;//某个匹配的结果串的开始位置指针constCAtlREMatchContext::RECHAR*szEnd=0;//某个匹配的结果串的结束位置指针mc.GetMatch(0,&szStart,&szEnd);//根据标号来获取某个结果串的开始结束位置指针,0表示第一个,可以使用以下的代码来匹配多个并获取/*for(UINTnGroupIndex=0;nGroupIndex::RECHAR*szStart=0;constCAtlREMatchContext::RECHAR*szEnd=0;mcUrl.GetMatch(nGroupIndex,&szStart,&szEnd);ptrdiff_tnLength=szEnd-szStart;printf("%d:\"%.*s\"\n",nGroupIndex,nLength,szStart);}*/if(szStart==NULL)return;ptrdiff_tnLength=szEnd-szStart;stringstartstr=szStart;startstr=startstr.substr(0,nLength);//获取匹配的串}/*.Matchesanysinglecharacter.[]Indicatesacharacterclass.Matchesanycharacterinsidethebrackets(forexample,[abc]matches"a","b",and"c").^Ifthismetacharacteroccursatthestartofacharacterclass,itnegatesthecharacterclass.Anegatedcharacterclassmatchesanycharacterexceptthoseinsidethebrackets(forexample,[^abc]matchesallcharactersexcept"a","b",and"c").If^isatthebeginningoftheregularexpression,itmatchesthebeginningoftheinput(forexample,^[abc]willonlymatchinputthatbeginswith"a","b",or"c").-Inacharacterclass,indicatesarangeofcharacters(forexample,[0-9]matchesanyofthedigits"0"through"9").?Indicatesthattheprecedingexpressionisoptional:itmatchesonceornotatall(forexample,[0-9][0-9]?matches"2"and"12").+Indicatesthattheprecedingexpressionmatchesoneormoretimes(forexample,[0-9]+matches"1","13","666",andsoon).*Indicatesthattheprecedingexpressionmatcheszeroormoretimes.??,+?,*?Non-greedyversionsof?,+,and*.Thesematchaslittleaspossible,unlikethegreedyversionswhichmatchasmuchaspossible.Example:giventheinput"",matches""whilematches"".()Groupingoperator.Example:(\d+,)*\d+matchesalistofnumbersseparatedbycommas(suchas"1"or"1,23,456").{}Indicatesamatchgroup.TheactualtextintheinputthatmatchestheexpressioninsidethebracescanberetrievedthroughtheCAtlREMatchContextobject.\Escapecharacter:interpretthenextcharacterliterally(forexample,[0-9]+matchesoneormoredigits,but[0-9]\+matchesadigitfollowedbyapluscharacter).Alsousedforabbreviations(suchas\aforanyalphanumericcharacter;seetablebelow).If\isfollowedbyanumbern,itmatchesthenthmatchgroup(startingfrom0).Example:.*?matches"Contents".NotethatinC++stringliterals,twobackslashesmustbeused:"\\+","\\a",".*?".$Attheendofaregularexpression,thischaractermatchestheendoftheinput.Example:[0-9]$matchesadigitattheendoftheinput.|Alternationoperator:separatestwoexpressions,exactlyoneofwhichmatches(forexample,T|thematches"The"or"the").!Negationoperator:theexpressionfollowing!doesnotmatchtheinput.Example:a!bmatches"a"notfollowedby"b".AbbreviationsCAtlRegExpcanhandleabbreviations,suchas\dinsteadof[0-9].TheabbreviationsareprovidedbythecharactertraitsclasspassedintheCharTraitsparameter.Thepredefinedcharactertraitsclassesprovidethefollowingabbreviations.AbbreviationMatches\aAnyalphanumericcharacter:([a-zA-Z0-9])\bWhitespace(blank):([\\t])\cAnyalphabeticcharacter:([a-zA-Z])\dAnydecimaldigit:([0-9])\hAnyhexadecimaldigit:([0-9a-fA-F])\nNewline:(\r|(\r?\n))\qAquotedstring:(\"[^\"]*\")|(\'[^\']*\')\wAsimpleword:([a-zA-Z]+)\zAninteger:([0-9]+)*/