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.
std::smatch m; if (std::regex_search(text, m, std::regex(R"((\w+)@(\w+))"))) { std::cout << m[1] << " at " << m[2] << "\n"; }
Try it
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.
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
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_searchbehave likeregex_match, anchor the pattern with^...$. To makeregex_matchfind a substring, wrap the pattern in.*on both sides, butregex_searchis clearer. - Group count.
m.size()is the number of groups in the pattern plus one, even for groups that did not participate. Checkm[i].matchedbefore using an optional group. - Dangling results. A
smatchstores 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 astd::stringtemporary, becausemwould dangle. Store the text first. - Zero-length matches (for example from
a*) are allowed;sregex_iteratoradvances by one character to avoid looping forever. regex_searchwith aregex_iteratorrepeats the search from the end of the previous match, so matches never overlap.- Performance.
std::regexis slow for large inputs. Preferstd::string::find,std::string_viewoperations or hand-written parsing for simple jobs.