Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Strings and I/O

C++ regex_search and regex_match

Reference for std::regex_match and std::regex_search: whole-string versus partial matching, smatch capture groups, prefix and suffix, and sregex_iterator.

std::regex_match and std::regex_search both test a string against a std::regex, and both can report what matched in a std::match_results object (std::smatch for a std::string, std::cmatch for a C string). They differ in one important way. regex_match succeeds only if the entire string matches the pattern. regex_search succeeds if the pattern matches anywhere inside the string and reports the first match.

To find every match instead of just the first, use std::sregex_iterator. It repeatedly calls regex_search and moves past the previous match.

C++
std::smatch m;
if (std::regex_search(text, m, std::regex(R"((\w+)@(\w+))"))) {
    std::cout << m[1] << " at " << m[2] << "\n";
}

Try it

C++ Whole string versus anywhere, with capture groups

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Finding every match

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
bool std::regex_match (const std::string& s, std::smatch& m, const std::regex& re,
                       std::regex_constants::match_flag_type flags = std::regex_constants::match_default);
bool std::regex_match (const std::string& s, const std::regex& re, /* flags */);
bool std::regex_search(const std::string& s, std::smatch& m, const std::regex& re, /* flags */);
bool std::regex_search(const std::string& s, const std::regex& re, /* flags */);
// also overloads for const char*, iterator pairs (std::cmatch, std::match_results<It>)
Name Description
s The text. Must be an lvalue when you pass a match object (a temporary std::string is a compile error).
m Receives the result. m[0] is the whole match, m[i] is group i.
re The compiled pattern.
flags match_not_bol, match_not_eol, match_not_null, match_continuous, match_prev_avail, ...

Returns: true if there is a match, otherwise false. On false the match object is "not ready" for use of its contents (m.empty() is true).

Match object members: m[i] (a sub_match with .matched, .str(), implicit conversion to string), m.size(), m.prefix(), m.suffix(), m.position(i), m.length(i).

Usage notes

  • Match versus search. To make regex_search behave like regex_match, anchor the pattern with ^...$. To make regex_match find a substring, wrap the pattern in .* on both sides, but regex_search is clearer.
  • Group count. m.size() is the number of groups in the pattern plus one, even for groups that did not participate. Check m[i].matched before using an optional group.
  • Dangling results. A smatch stores iterators into the original string. If the string is changed or destroyed, the match becomes invalid. Copy parts with .str() if you need them later.
  • Temporary strings are rejected. std::regex_search(get_text(), m, re) does not compile for a std::string temporary, because m would dangle. Store the text first.
  • Zero-length matches (for example from a*) are allowed; sregex_iterator advances by one character to avoid looping forever.
  • regex_search with a regex_iterator repeats the search from the end of the previous match, so matches never overlap.
  • Performance. std::regex is slow for large inputs. Prefer std::string::find, std::string_view operations or hand-written parsing for simple jobs.