function template
<regex>

std::regex_search

C-strings (1)
template <class charT, class traits>  bool regex_search (const charT* s, const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
strings (2)
template <class ST, class SA, char charT, class traits>  bool regex_search (const basic_string<charT,ST,SA>& s,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
ranges (3)
template <class BidirectionalIterator, class charT, class traits>  bool regex_search (BidirectionalIterator first, BidirectionalIterator last,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
with match_results (4,5,6)
template <class charT, class Alloc, class traits>  bool regex_search (const charT* s, match_results<const charT*, Alloc>& m,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);template <class ST, class SA, class Alloc, class charT, class traits>  bool regex_search (const basic_string<charT,ST,SA>& s,          match_results<typename basic_string<charT,ST,SA>::const_iterator,Alloc>& m,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);template <class BidirectionalIterator, class Alloc, class charT, class traits>  bool regex_search (BidirectionalIterator first, BidirectionalIterator last,          match_results<BidirectionalIterator, Alloc>& m,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
C-strings (1)
template <class charT, class traits>  bool regex_search (const charT* s, const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
strings (2)
template <class ST, class SA, char charT, class traits>  bool regex_search (const basic_string<charT,ST,SA>& s,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
ranges (3)
template <class BidirectionalIterator, class charT, class traits>  bool regex_search (BidirectionalIterator first, BidirectionalIterator last,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
with match_results (4,5,6)
template <class charT, class Alloc, class traits>  bool regex_search (const charT* s, match_results<const charT*, Alloc>& m,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);template <class ST, class SA, class Alloc, class charT, class traits>  bool regex_search (const basic_string<charT,ST,SA>& s,          match_results<typename basic_string<charT,ST,SA>::const_iterator,Alloc>& m,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);template <class BidirectionalIterator, class Alloc, class charT, class traits>  bool regex_search (BidirectionalIterator first, BidirectionalIterator last,          match_results<BidirectionalIterator, Alloc>& m,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
moving string (deleted) (7)
template <class ST, class SA, class Alloc, class charT, class traits>  bool regex_search (const basic_string<charT,ST,SA>&&,          match_results<typename basic_string<charT,ST,SA>::const_iterator,Alloc>&,          const basic_regex<charT,traits>&,          regex_constants::match_flag_type=regex_constants::match_default) = delete;
Search sequence
Returns whether some sub-sequence in the target sequence (the subject) matches the regular expression rgx (the pattern). The target sequence is either s or the character sequence between first and last, depending on the version used.

The versions 4, 5 and 6, are identical to 1, 2 and 3 respectively, except that they take an object of a match_results type as argument, which is filled with information about the match results.

An optional parameter, flags, allows to specify options on how to search for the expression.

This function returns true if the expression is found anywhere in the target sequence, even if the sequence contains more characters that are not part of the match before or after the match. For a function that returns true only when the entire sequence matches, see regex_match.

Parameters

s
A string with the target sequence (the subject) to be searched for a match of the regex expression.
rgx
A basic_regex object (the pattern) to match.
flags
Flags used to control how rgx is matched.
One or more of these constants can be combined (using the bitwise OR operator, |) to form a valid bitmask value of type regex_constants::match_flag_type:
flag*effects on matchnotes
match_defaultDefaultDefault matching behavior.
This constant has a value of zero**.
match_not_bolNot Beginning-Of-LineThe first character is not considered a beginning of line ("^" does not match).
match_not_eolNot End-Of-LineThe last character is not considered an end of line ("$" does not match).
match_not_bowNot Beginning-Of-WordThe escape sequence "\b" does not match as a beginning-of-word.
match_not_eowNot End-Of-WordThe escape sequence "\b" does not match as an end-of-word.
match_anyAny matchAny match is acceptable if more than one match is possible.
match_not_nullNot nullEmpty sequences do not match.
match_continuousContinuousThe expression must match a sub-sequence that begins at the first character.
Sub-sequences must begin at the first character to match.
match_prev_availPrevious AvailableOne or more characters exist before the first one. (match_not_bol and match_not_bow are ignored)
format_defaultDefaultSame as match_default.
This constant has a value of zero**.
format_sedNoneIgnored by this function.
See regex_constants for more info.
format_no_copy
format_first_only
* These bitmask flag names are available under the std::regex_constants namespace (see regex_constants for more details).
** Constants with a value of zero are ignored if some other flag is set.
match_flag_type is a type available under the std::regex_constants namespace.
first, last
Bidirectional iterators to the initial and final positions in the range of characters used as target sequence for the search. The range used is [first,last), which includes all the characters between first and last, including the character pointed by first but not the character pointed by last.
The function template type can be any type of bidirectional iterator to characters.
m
Object of a match_results type (such as cmatch or smatch) that is filled by this function with information about the match results and any submatches found.
The type of the match_results object shall be instantiated with the appropriate iterator type to iterate through the characters in the character sequence s (or between begin and end).

Return value

true if rgx matches against a sub-sequence in the target sequence. false otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// regex_search example
#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::string s ("this subject has a submarine as a subsequence");
  std::smatch m;
  std::regex e ("\\b(sub)([^ ]*)");   // matches words beginning by "sub"

  std::cout << "Target sequence: " << s << std::endl;
  std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
  std::cout << "The following matches and submatches were found:" << std::endl;

  while (std::regex_search (s,m,e)) {
    for (auto x:m) std::cout << x << " ";
    std::cout << std::endl;
    s = m.suffix().str();
  }

  return 0;
}

Output:
Target sequence: this subject has a submarine as subsequence
Regular expression: /\b(sub)([^ ]*)/
The following matches and submatches were found:
subject sub ject
submarine sub marine
subsequence sub sequence


See also