On this page
std::regex_replace
Defined in header <regex> |
||
|---|---|---|
|
(1) | (since C++11) |
|
(2) | (since C++11) |
|
(3) | (since C++11) |
|
(4) | (since C++11) |
|
(5) | (since C++11) |
|
(6) | (since C++11) |
regex_replace uses a regular expression to perform substitution on a sequence of characters:
1) Copies characters in the range
[first, last) to out, replacing any sequences that match re with characters formatted by fmt. In other words:
- Constructs a
std::regex_iteratorobjectias if bystd::regex_iterator<BidirIt, CharT, traits> i(first, last, re, flags), and uses it to step through every match ofrewithin the sequence[first,last). - For each such match
m, copies the non-matched subsequence (m.prefix()) intooutas if byout = std::copy(m.prefix().first, m.prefix().second, out)and then replaces the matched subsequence with the formatted replacement string as if by callingout = m.format(out, fmt, flags). - When no more matches are found, copies the remaining non-matched characters to
outas if byout = std::copy(last_m.suffix().first, last_m.suffix().second, out)wherelast_mis a copy of the last match found. - If there are no matches, copies the entire sequence into
outas-is, byout = std::copy(first, last, out). - If
flagscontainsstd::regex_constants::format_no_copy, the non-matched subsequences are not copied intoout. - If
flagscontainsstd::regex_constants::format_first_only, only the first match is replaced.
2) Same as (1), but the formatted replacement is performed as if by calling
out = m.format(out, fmt, fmt + char_traits<CharT>::length(fmt), flags).
3,4) Constructs an empty string
result of type std::basic_string<CharT, ST, SA> and calls std::regex_replace(std::back_inserter(result), s.begin(), s.end(), re, fmt, flags).
5,6) Constructs an empty string
result of type std::basic_string<CharT> and calls std::regex_replace(std::back_inserter(result), s, s + std::char_traits<CharT>::length(s), re, fmt, flags).
Parameters
| first, last | - | the input character sequence, represented as a pair of iterators |
| s | - | the input character sequence, represented as std::basic_string or character array |
| re | - | the std::basic_regex that will be matched against the input sequence |
| fmt | - | the regex replacement format string, exact syntax depends on the value of flags |
| flags | - | the match flags of type std::regex_constants::match_flag_type |
| out | - | output iterator to store the result of the replacement |
| Type requirements | ||
-OutputIt must meet the requirements of LegacyOutputIterator. |
||
-BidirIt must meet the requirements of LegacyBidirectionalIterator. |
||
Return value
1,2) Returns a copy of the output iterator
out after all the insertions.
3-6) Returns the string
result which contains the output.
Exceptions
May throw std::regex_error to indicate an error condition.
Example
#include <iostream>
#include <iterator>
#include <regex>
#include <string>
int main()
{
std::string text = "Quick brown fox";
std::regex vowel_re("a|e|i|o|u");
// write the results to an output iterator
std::regex_replace(std::ostreambuf_iterator<char>(std::cout),
text.begin(), text.end(), vowel_re, "*");
// construct a string holding the results
std::cout << '\n' << std::regex_replace(text, vowel_re, "[$&]") << '\n';
}
Output:
Q**ck br*wn f*x
Q[u][i]ck br[o]wn f[o]x
See also
|
(C++11)
|
attempts to match a regular expression to any part of a character sequence (function template) |
|
(C++11)
|
options specific to matching (typedef) |
| replaces specified portion of a string (public member function of std::basic_string<CharT,Traits,Allocator>) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/regex/regex_replace