From cppreference.com
Defined in header
template<classBidirIt,classAlloc,classCharT,classTraits>boolregex_match(BidirItfirst,BidirItlast,std::match_results<BidirIt,Alloc>&m,conststd::basic_regex<CharT,Traits>&e,std::regex_constants::match_flag_typeflags=std::regex_constants::match_default); (1) (since C++11)template<classBidirIt,classCharT,classTraits>boolregex_match(BidirItfirst,BidirItlast,conststd::basic_regex<CharT,Traits>&e,std::regex_constants::match_flag_typeflags=std::regex_constants::match_default); (2) (since C++11)template<classCharT,classAlloc,classTraits>boolregex_match(constCharT*str,std::match_results<constCharT*,Alloc>&m,conststd::basic_regex<CharT,Traits>&e,std::regex_constants::match_flag_typeflags=std::regex_constants::match_default); (3) (since C++11)template<classCharT,classTraits>boolregex_match(constCharT*str,conststd::basic_regex<CharT,Traits>&e,std::regex_constants::match_flag_typeflags=std::regex_constants::match_default); (4) (since C++11)template<classSTraits,classSAlloc,classAlloc,classCharT,classTraits>boolregex_match(conststd::basic_string<CharT,STraits,SAlloc>&s,std::match_results<typenamestd::basic_string<CharT,STraits,SAlloc>::const_iterator,Alloc>&m,conststd::basic_regex<CharT,Traits>&e,std::regex_constants::match_flag_typeflags=std::regex_constants::match_default); (5) (since C++11)template<classSTraits,classSAlloc,classCharT,classTraits>boolregex_match(conststd::basic_string<CharT,STraits,SAlloc>&s,conststd::basic_regex<CharT,Traits>&e,std::regex_constants::match_flag_typeflags=std::regex_constants::match_default); (6) (since C++11)template<classSTraits,classSAlloc,classAlloc,classCharT,classTraits>boolregex_match(conststd::basic_string<CharT,STraits,SAlloc>&&,std::match_results<typenamestd::basic_string<CharT,STraits,SAlloc>::const_iterator,Alloc>&,conststd::basic_regex<CharT,Traits>&,std::regex_constants::match_flag_typeflags=std::regex_constants::match_default)=delete; (7) (since C++11)Determines if the regular expression e matches the entire target character sequence. The detailed match result is stored in m (if present).
1,2) The target character sequence is represented by the range [first, last).
3,4) The target character sequence is represented by the range [str, str+std::char_traits<CharT>::length(str)).
5,6) The target character sequence is represented by the string s.
7) The target character sequence cannot be represented by a
rvalue.
If the match does not exist, the following expressions involving m (if exists) should yield the specified values:
Expression Value m.
()truem.
()0m.
()trueIf the match exists, given any integer in (0, m.size()) as n, the following expressions involving m should yield the specified values for each overload listed below:
Expression Value Overload (1) Overload (3) Overload (5)m.
()truem.
()1+e.
()m.
()falsem.
().firstfirststrs.begin()m.
().secondm.
().matchedfalse
m.
().firstlaststd::char_traits<CharT>::
length(str)+strs.end()m.
().secondm.
().matchedfalse
m
.firstfirststrs.begin()m
.secondlaststd::char_traits<CharT>::
length(str)+strs.end()m
.matchedtrue
m
.firstlast if
n did not participate in the match
the start of the sequence otherwise matching sub-expression n otherwise
m
.secondlast if
n did not participate in the match
the end of the sequence otherwise matching sub-expression n otherwise
m
.matchedfalse if
n did not participate in the match
true otherwise
The match prefix is empty.
The match suffix is empty.
The entire sequence is matched.
Parameters
first, last - the target character range str - the target null-terminated C-style string s - the target
m - the match results e - the regular expression flags - flags used to determine how the match will be performed Return value
Returns true if the entire target sequence matches e, false otherwise.
Notes
Because regex_match only considers full matches, the same regex may give different matches between regex_match and
:
std::regexre("Get|GetValue");std::cmatchm;std::regex_search("GetValue",m,re);// returns true, and m[0] contains "Get"std::regex_match("GetValue",m,re);// returns true, and m[0] contains "GetValue"std::regex_search("GetValues",m,re);// returns true, and m[0] contains "Get"std::regex_match("GetValues",m,re);// returns falseExample
Run this code
#include<cstddef>#include<iostream>#include<regex>#include<string>intmain(){// Simple regular expression matchingconststd::stringfnames[]={"foo.txt","bar.txt","baz.dat","zoidberg"};conststd::regextxt_regex("[a-z]+\\.txt");for(constauto&fname:fnames)std::cout<<fname<<": "<<std::regex_match(fname,txt_regex)<<'\n';// Extraction of a sub-matchconststd::regexbase_regex("([a-z]+)\\.txt");std::smatchbase_match;for(constauto&fname:fnames)if(std::regex_match(fname,base_match,base_regex))// The first sub_match is the whole string; the next// sub_match is the first parenthesized expression.if(base_match.size()==2){std::ssub_matchbase_sub_match=base_match[1];std::stringbase=base_sub_match.str();std::cout<<fname<<" has a base of "<<base<<'\n';}// Extraction of several sub-matchesconststd::regexpieces_regex("([a-z]+)\\.([a-z]+)");std::smatchpieces_match;for(constauto&fname:fnames)if(std::regex_match(fname,pieces_match,pieces_regex)){std::cout<<fname<<'\n';for(std::size_ti=0;i<pieces_match.size();++i){std::ssub_matchsub_match=pieces_match[i];std::stringpiece=sub_match.str();std::cout<<" submatch "<<i<<": "<<piece<<'\n';}}}Output:
foo.txt: 1 bar.txt: 1 baz.dat: 0 zoidberg: 0 foo.txt has a base of foo bar.txt has a base of bar foo.txt submatch 0: foo.txt submatch 1: foo submatch 2: txt bar.txt submatch 0: bar.txt submatch 1: bar submatch 2: txt baz.dat submatch 0: baz.dat submatch 1: baz submatch 2: dat Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++11 n could be zero in the postcondition can only be positive
C++11 it was unclear whether partial matches are considered only considers full matches
C++11 overload (5) accepted basic_string rvalues,
which could result in dangling iterators rejected via deleted overload (7)See also
(C++11)
regular expression object
(class template)
(C++11)
identifies one regular expression match, including all sub-expression matches
(class template)
(C++11)
attempts to match a regular expression to any part of a character sequence
(function template)