std::regex_match - cppreference.com

文章推薦指數: 80 %
投票人數:10人

Example cppreference.com Createaccount Namespaces Page Discussion Variants Views View Edit History Actions std::regex_match Fromcppreference.com template boolregex_match(BidirItfirst,BidirItlast,          std::match_results&m,          conststd::basic_regex&e,          std::regex_constants::match_flag_typeflags=            std::regex_constants::match_default); (1) (sinceC++11) template boolregex_match(BidirItfirst,BidirItlast,          conststd::basic_regex&e,          std::regex_constants::match_flag_typeflags=            std::regex_constants::match_default); (2) (sinceC++11) template boolregex_match(constCharT*str,          std::match_results&m,          conststd::basic_regex&e,          std::regex_constants::match_flag_typeflags=            std::regex_constants::match_default); (3) (sinceC++11) template boolregex_match(conststd::basic_string&s,          std::match_results<            typenamestd::basic_string::const_iterator,            Alloc          >&m,          conststd::basic_regex&e,          std::regex_constants::match_flag_typeflags=            std::regex_constants::match_default); (4) (sinceC++11) template boolregex_match(constCharT*str,          conststd::basic_regex&e,          std::regex_constants::match_flag_typeflags=            std::regex_constants::match_default); (5) (sinceC++11) template boolregex_match(conststd::basic_string&s,          conststd::basic_regex&e,          std::regex_constants::match_flag_typeflags=            std::regex_constants::match_default); (6) (sinceC++11) template boolregex_match(conststd::basic_string&&,          std::match_results<            typenamestd::basic_string::const_iterator,            Alloc          >&,          conststd::basic_regex&,          std::regex_constants::match_flag_typeflags=            std::regex_constants::match_default)=delete; (7) (sinceC++11) Determinesiftheregularexpressionematchestheentiretargetcharactersequence,whichmaybespecifiedasstd::string,aC-string,oraniteratorpair. 1)Determinesifthereisamatchbetweentheregularexpressioneandtheentiretargetcharactersequence[first,last),takingintoaccounttheeffectofflags.Whendeterminingifthereisamatch,onlypotentialmatchesthatmatchtheentirecharactersequenceareconsidered.Matchresultsarereturnedinm. 2)Behavesas(1)above,omittingthematchresults. 3)Returnsstd::regex_match(str,str+std::char_traits::length(str),m,e,flags). 4)Returnsstd::regex_match(s.begin(),s.end(),m,e,flags). 5)Returnsstd::regex_match(str,str+std::char_traits::length(str),e,flags). 6)Returnsstd::regex_match(s.begin(),s.end(),e,flags). 7)Theoverload(4)isprohibitedfromacceptingtemporarystrings,otherwisethisfunctionpopulatesmatch_resultsmwithstringiteratorsthatbecomeinvalidimmediately. Notethatregex_matchwillonlysuccessfullymatcharegularexpressiontoanentirecharactersequence,whereasstd::regex_searchwillsuccessfullymatchsubsequences. Contents 1Parameters 2Returnvalue 3Notes 4Example 5Defectreports 6Seealso [edit]Parameters first,last - thetargetcharacterrangetoapplytheregexto,givenasiterators m - thematchresults str - thetargetstring,givenasanull-terminatedC-stylestring s - thetargetstring,givenasastd::basic_string e - theregularexpression flags - flagsusedtodeterminehowthematchwillbeperformed Typerequirements -BidirItmustmeettherequirementsofLegacyBidirectionalIterator. [edit]Returnvalue Returnstrueifamatchexists,falseotherwise.Ineithercase,theobjectmisupdated,asfollows: Ifthematchdoesnotexist: m.ready()==true m.empty()==true m.size()==0 Ifthematchexists: m.ready() true m.empty() false m.size() numberofmarkedsubexpressionsplus1,thatis,1+e.mark_count() m.prefix().first first m.prefix().second first m.prefix().matched false(thematchprefixisempty) m.suffix().first last m.suffix().second last m.suffix().matched false(thematchsuffixisempty) m[0].first first m[0].second last m[0].matched true(theentiresequenceismatched) m[n].first thestartofthesequencethatmatchedmarkedsub-expressionn,orlastifthesubexpressiondidnotparticipateinthematch m[n].second theendofthesequencethatmatchedmarkedsub-expressionn,orlastifthesubexpressiondidnotparticipateinthematch m[n].matched trueifsub-expressionnparticipatedinthematch,falseotherwise [edit]Notes Becauseregex_matchonlyconsidersfullmatches,thesameregexmaygivedifferentmatchesbetweenregex_matchandstd::regex_search: std::regexre("Get|GetValue"); std::cmatchm; std::regex_search("GetValue",m,re);//returnstrue,andm[0]contains"Get" std::regex_match("GetValue",m,re);//returnstrue,andm[0]contains"GetValue" std::regex_search("GetValues",m,re);//returnstrue,andm[0]contains"Get" std::regex_match("GetValues",m,re);//returnsfalse [edit]Example Runthiscode #include #include #include   intmain() { //Simpleregularexpressionmatching conststd::stringfnames[]={"foo.txt","bar.txt","baz.dat","zoidberg"}; conststd::regextxt_regex("[a-z]+\\.txt");   for(constauto&fname:fnames){ std::cout<



請為這篇文章評分?