From cppreference.com
Defined in header
template<classBidirIt,classAlloc,classCharT,classTraits>boolregex_search(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_search(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_search(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_search(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_search(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_search(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_search(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 there is a match between the regular expression e and some subsequence in the 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 a match does not exist, the following expressions involving m (if exists) should yield the specified values:
Expression Value m.
()truem.
()0m.
()trueIf a 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[0].firstm.
().matchedm.prefix().first!=m.prefix().secondm.
().firstm[0].secondm.
().secondlaststd::char_traits<CharT>::
length(str)+strs.end()m.
().matchedm.suffix().first!=m.suffix().secondm
.firstthe start of the sequence that matched em
.secondthe end of the sequence that matched em
.matchedtruem
.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
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 a match exists, false otherwise.
Notes
In order to examine all matches within the target sequence, std::regex_search may be called in a loop, restarting each time from m[0].second of the previous call.
offers an easy interface to this iteration.
Example
Run this code
#include<cstddef>#include<iostream>#include<regex>#include<string>intmain(){std::stringlines[]={"Roses are #ff0000","violets are #0000ff","all of my base are belong to you"};std::regexcolor_regex("#([a-f0-9]{2})""([a-f0-9]{2})""([a-f0-9]{2})");// simple matchfor(constauto&line:lines)std::cout<<line<<": "<<std::boolalpha<<std::regex_search(line,color_regex)<<'\n';std::cout<<'\n';// show contents of marked subexpressions within each matchstd::smatchcolor_match;for(constauto&line:lines)if(std::regex_search(line,color_match,color_regex)){std::cout<<"matches for '"<<line<<"'\n";std::cout<<"Prefix: '"<<color_match.prefix()<<"'\n";for(std::size_ti=0;i<color_match.size();++i)std::cout<<i<<": "<<color_match[i]<<'\n';std::cout<<"Suffix: '"<<color_match.suffix()<<"\'\n\n";}// repeated search (see also std::regex_iterator)std::stringlog(R"( Speed: 366 Mass: 35 Speed: 378 Mass: 32 Speed: 400 Mass: 30)");std::regexr(R"(Speed:\t\d*)");for(std::smatchsm;regex_search(log,sm,r);){std::cout<<sm.str()<<'\n';log=sm.suffix();}// C-style string demostd::cmatchcm;if(std::regex_search("this is a test",cm,std::regex("test")))std::cout<<"\nFound "<<cm[0]<<" at position "<<cm.prefix().length()<<'\n';}Output:
Roses are #ff0000: true violets are #0000ff: true all of my base are belong to you: false matches for 'Roses are #ff0000' Prefix: 'Roses are ' 0: #ff0000 1: ff 2: 00 3: 00 Suffix: '' matches for 'violets are #0000ff' Prefix: 'violets are ' 0: #0000ff 1: 00 2: 00 3: ff Suffix: '' Speed: 366 Speed: 378 Speed: 400 Found test at position 10 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 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 an entire character sequence
(function template)