regex_match, Tests whether a regular expression matches the entire ... Example. C++. Copy. // std__regex__regex_match.cpp // compile with: ...
Skiptomaincontent
Thisbrowserisnolongersupported.
UpgradetoMicrosoftEdgetotakeadvantageofthelatestfeatures,securityupdates,andtechnicalsupport.
DownloadMicrosoftEdge
Moreinfo
Tableofcontents
Exitfocusmode
Language
ReadinEnglish
Save
Tableofcontents
ReadinEnglish
Save
Feedback
Edit
Twitter
LinkedIn
Facebook
Email
Tableofcontents
functions
Article
02/08/2022
8minutestoread
6contributors
Inthisarticle
Name
Description
regex_match
Testswhetheraregularexpressionmatchestheentiretargetstring.
regex_replace
Replacesmatchedregularexpressions.
regex_search
Searchesforaregularexpressionmatch.
swap
Swapstwobasic_regexormatch_resultsobjects.
regex_match
Testswhetheraregularexpressionmatchestheentiretargetstring.
//(1)
template
boolregex_match(
BidItfirst,
Biditlast,
match_results&match,
constbasic_regex&re,
match_flag_typeflags=match_default);
//(2)
template
boolregex_match(
BidItfirst,
Biditlast,
constbasic_regex&re,
match_flag_typeflags=match_default);
//(3)
template
boolregex_match(
constElem*ptr,
match_results&match,
constbasic_regex&re,
match_flag_typeflags=match_default);
//(4)
template
boolregex_match(
constElem*ptr,
constbasic_regex&re,
match_flag_typeflags=match_default);
//(5)
template
boolregex_match(
constbasic_string&str,
match_results::const_iterator,Alloc>&match,
constbasic_regex&re,
match_flag_typeflags=match_default);
//(6)
template
boolregex_match(
constbasic_string&str,
constbasic_regex&re,
match_flag_typeflags=match_default);
Parameters
BidIt
Theiteratortypeforsubmatches.Forcommoncasesthisoneofstring::const_iterator,wstring::const_iterator,constchar*orconstwchar_t*.
Alloc
Thematchresultsallocatorclass.
Elem
Thetypeofelementstomatch.Forcommoncasesthisisstring,wstring,char*orwchar_t*.
RXtraits
Traitsclassforelements.
Alloc2
Theregularexpressionallocatorclass.
IOtraits
Thestringtraitsclass.
IOalloc
Thestringallocatorclass.
flags
Flagsformatches.
first
Beginningofsequencetomatch.
last
Endofsequencetomatch.
match
Thematchresults.CorrespondstoElemtype:smatchforstring,wsmatchforwstring,cmatchforchar*orwcmatchforwchar_t*.
ptr
Pointertobeginningofsequencetomatch.Ifptrischar*,thenusecmatchandregex.Ifptriswchar_t*thenusewcmatchandwregex.
re
Theregularexpressiontomatch.Typeregexforstringandchar*,orwregexforwstringandwchar_t*.
str
Stringtomatch.CorrespondstothetypeofElem.
Remarks
Eachtemplatefunctionreturnstrueonlyiftheentireoperandsequencestrexactlymatchestheregularexpressionargumentre.Useregex_searchtomatchasubstringwithinatargetsequenceandregex_iteratortofindmultiplematches.Thefunctionsthattakeamatch_resultsobjectsetitsmemberstoreflectwhetherthematchsucceededandifsowhatthevariouscapturegroupsintheregularexpressioncaptured.
Thefunctionsthattakeamatch_resultsobjectsetitsmemberstoreflectwhetherthematchsucceededandifsowhatthevariouscapturegroupsintheregularexpressioncaptured.
Example
//std__regex__regex_match.cpp
//compilewith:/EHsc
#include
#include
usingnamespacestd;
intmain()
{
//(1)withchar*
//Notehowconstchar*requirescmatchandregex
constchar*first="abc";
constchar*last=first+strlen(first);
cmatchnarrowMatch;
regexrx("a(b)c");
boolfound=regex_match(first,last,narrowMatch,rx);
if(found)
wcout<
OutItregex_replace(
OutItout,
BidItfirst,
BidItlast,
constbasic_regex&re,
constbasic_string&fmt,
match_flag_typeflags=match_default);
template
basic_stringregex_replace(
constbasic_string&str,
constbasic_regex&re,
constbasic_string&fmt,
match_flag_typeflags=match_default);
Parameters
OutIt
Theiteratortypeforreplacements.
BidIt
Theiteratortypeforsubmatches.
RXtraits
Traitsclassforelements.
Alloc
Theregularexpressionallocatorclass.
Elem
Thetypeofelementstomatch.
flags
Flagsformatches.
first
Beginningofsequencetomatch.
fmt
Theformatforreplacements.
last
Endofsequencetomatch.
out
Theoutputiterator.
re
Theregularexpressiontomatch.
str
Stringtomatch.
Remarks
Thefirstfunctionconstructsaregex_iteratorClassobjectiter(first,last,re,flags)andusesittosplititsinputrange[first,last)intoaseriesofsubsequencesT0M0T1M1...TN-1MN-1TN,whereMnisthenthmatchdetectedbytheiterator.Ifnomatchesarefound,T0istheentireinputrangeandNiszero.If(flags&format_first_only)!=0onlythefirstmatchisused,T1isalloftheinputtextthatfollowsthematch,andNis1.Foreachiintherange[0,N),if(flags&format_no_copy)==0itcopiesthetextintherangeTitotheiteratorout.Itthencallsm.format(out,fmt,flags),wheremisthematch_resultsobjectreturnedbytheiteratorobjectiterforthesubsequenceMi.Finally,if(flags&format_no_copy)==0itcopiesthetextintherangeTNtotheiteratorout.Thefunctionreturnsout.
Thesecondfunctionconstructsalocalvariableresultoftypebasic_stringandcallsregex_replace(back_inserter(result),str.begin(),str.end(),re,fmt,flags).Itreturnsresult.
Example
//std__regex__regex_replace.cpp
//compilewith:/EHsc
#include
#include
intmain()
{
charbuf[20];
constchar*first="axayaz";
constchar*last=first+strlen(first);
std::regexrx("a");
std::stringfmt("A");
std::regex_constants::match_flag_typefonly=
std::regex_constants::format_first_only;
*std::regex_replace(&buf[0],first,last,rx,fmt)='\0';
std::cout<
boolregex_search(
BidItfirst,
Biditlast,
match_results&match,
constbasic_regex&re,
match_flag_typeflags=match_default);
template
boolregex_search(
BidItfirst,
Biditlast,
constbasic_regex&re,
match_flag_typeflags=match_default);
template
boolregex_search(
constElem*ptr,
match_results&match,
constbasic_regex&re,
match_flag_typeflags=match_default);
template
boolregex_search(
constElem*ptr,
constbasic_regex&re,
match_flag_typeflags=match_default);
template
boolregex_search(
constbasic_string&str,
match_results::const_iterator,Alloc>&match,
constbasic_regex&re,
match_flag_typeflags=match_default);
template
boolregex_search(
constbasic_string&str,
constbasic_regex&re,
match_flag_typeflags=match_default);
Parameters
BidIt
Theiteratortypeforsubmatches.
Alloc
Thematchresultsallocatorclass.
Elem
Thetypeofelementstomatch.
RXtraits
Traitsclassforelements.
Alloc2
Theregularexpressionallocatorclass.
IOtraits
Thestringtraitsclass.
IOalloc
Thestringallocatorclass.
flags
Flagsformatches.
first
Beginningofsequencetomatch.
last
Endofsequencetomatch.
match
Thematchresults.
ptr
Pointertobeginningofsequencetomatch.
re
Theregularexpressiontomatch.
str
Stringtomatch.
Remarks
Eachtemplatefunctionreturnstrueonlyifasearchforitsregularexpressionargumentreinitsoperandsequencesucceeds.Thefunctionsthattakeamatch_resultsobjectsetitsmemberstoreflectwhetherthesearchsucceededandifsowhatthevariouscapturegroupsintheregularexpressioncaptured.
Example
//std__regex__regex_search.cpp
//compilewith:/EHsc
#include
#include
intmain()
{
constchar*first="abcd";
constchar*last=first+strlen(first);
std::cmatchmr;
std::regexrx("abc");
std::regex_constants::match_flag_typefl=
std::regex_constants::match_default;
std::cout<mr2;
std::cout<
voidswap(
basic_regex&left,
basic_regex&right)noexcept;
template
voidswap(
match_results&left,
match_results&right)noexcept;
Parameters
Elem
Thetypeofelementstomatch.
RXtraits
Traitsclassforelements.
Remarks
Thetemplatefunctionsswapthecontentsoftheirrespectiveargumentsinconstanttimeanddon'tthrowexceptions.
Example
//std__regex__swap.cpp
//compilewith:/EHsc
#include
#include
intmain()
{
std::regexrx0("c(a*)|(b)");
std::regexrx1;
std::cmatchmr0;
std::cmatchmr1;
swap(rx0,rx1);
std::regex_search("xcaaay",mr1,rx1);
swap(mr0,mr1);
std::csub_matchsub=mr0[1];
std::cout<
regex_constantsClass
regex_errorClass
regex_iteratorClass
operators
regex_token_iteratorClass
regex_traitsClass
typedefs
Feedback
Submitandviewfeedbackfor
Thisproduct
Thispage
Viewallpagefeedback
Inthisarticle