C++ Reference ยท Strings and I/O
C++ std::regex_replace function
Reference for std::regex_replace: replace matches with format strings like $1, use format_first_only and format_no_copy, and build custom replacements by hand.
std::regex_replace returns a copy of a string in which every match of a regular expression has been replaced by a format string. The format string can refer to the match and to its capture groups: $& is the whole match, $1 to $9 are the groups, $` and $' are the text before and after the match, and $$ is a literal dollar sign.
The input is never modified. If a replacement needs real logic (convert to upper case, look something up, compute a number), regex_replace is not enough because it takes only a fixed format string, not a function. In that case iterate with std::sregex_iterator and build the result yourself, as the second example shows.
std::string out = std::regex_replace("2025-03-14", std::regex(R"((\d+)-(\d+)-(\d+))"), "$3.$2.$1"); // "14.03.2025"
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
std::string std::regex_replace(const std::string& s, const std::regex& re, const std::string& fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default); OutIt std::regex_replace(OutIt out, BidirIt first, BidirIt last, const std::regex& re, const std::string& fmt, std::regex_constants::match_flag_type flags = ...); // also overloads taking const char* for the text and/or the format
| Name | Description |
|---|---|
s or first, last |
The input text. |
re |
The pattern. Every non-overlapping match is replaced. |
fmt |
The replacement. In ECMAScript format: $& match, $1...$99 group, $` prefix, $' suffix, $$ dollar sign. |
flags |
format_first_only (replace one match), format_no_copy (leave out unmatched text), format_sed (POSIX sed-style \1), plus the usual match_* flags. |
out |
Output iterator for the iterator overload. |
Returns: the new string, or the output iterator one past the last character written. If nothing matches, the string overload returns an unchanged copy.
Usage notes
- No callbacks. The replacement is a template, not a function. For computed replacements, loop over
sregex_iteratorand append pieces, as shown above. - Group references.
$1refers to group 1.$10is group 10 if the pattern has 10 groups; otherwise it is group 1 followed by0. Use$&or restructure the pattern when it matters. A reference to a nonexistent group becomes empty text. - A literal
$in the output needs$$. Replacement text coming from users should be escaped this way, or you get surprising results. - Empty matches. A pattern that can match nothing (
x*) inserts the replacement between every character, including at both ends. - Copy cost. The result is a new string every time. To clean a buffer repeatedly, use the iterator overload to write into a reserved string.
- Simpler tools. Replacing a fixed substring needs no regex;
std::string::findwithreplace, or a small loop, is faster and easier to read. - Not Unicode-aware.
\w,\dand case-insensitivity work on singlecharvalues, so non-ASCII letters are not matched as letters.