std::regex_replace
Min standard notice:
Header: <regex>
regex_replace uses the regular expression re to perform substitution on the target character sequence:
# Declarations
template< class OutputIt, class BidirIt, class Traits, class CharT,
class STraits, class SAlloc >
OutputIt regex_replace( OutputIt out, BidirIt first, BidirIt last,
const std::basic_regex<CharT, Traits>& re,
const std::basic_string<CharT, STraits, SAlloc>& fmt,
std::regex_constants::match_flag_type flags =
std::regex_constants::match_default );
(since C++11)
template< class OutputIt, class BidirIt, class Traits, class CharT >
OutputIt regex_replace( OutputIt out, BidirIt first, BidirIt last,
const std::basic_regex<CharT, Traits>& re,
const CharT* fmt,
std::regex_constants::match_flag_type flags =
std::regex_constants::match_default );
(since C++11)
template< class Traits, class CharT,
class STraits, class SAlloc, class FTraits, class FAlloc >
std::basic_string<CharT, STraits, SAlloc>
regex_replace( const std::basic_string<CharT, STraits, SAlloc>& str,
const std::basic_regex<CharT, Traits>& re,
const std::basic_string<CharT, FTraits, FAlloc>& fmt,
std::regex_constants::match_flag_type flags =
std::regex_constants::match_default );
(since C++11)
template< class Traits, class CharT, class STraits, class SAlloc >
std::basic_string<CharT, STraits, SAlloc>
regex_replace( const std::basic_string<CharT, STraits, SAlloc>& str,
const std::basic_regex<CharT, Traits>& re,
const CharT* fmt,
std::regex_constants::match_flag_type flags =
std::regex_constants::match_default );
(since C++11)
template< class Traits, class CharT, class STraits, class SAlloc >
std::basic_string<CharT>
regex_replace( const CharT* s, const std::basic_regex<CharT, Traits>& re,
const std::basic_string<CharT, STraits, SAlloc>& fmt,
std::regex_constants::match_flag_type flags =
std::regex_constants::match_default );
(since C++11)
template< class Traits, class CharT >
std::basic_string<CharT>
regex_replace( const CharT* s, const std::basic_regex<CharT, Traits>& re,
const CharT* fmt,
std::regex_constants::match_flag_type flags =
std::regex_constants::match_default );
(since C++11)
# Parameters
first, last: the target character rangestr: the target std::strings: the target null-terminated C-style stringre: the regular expressionfmt: the regex replacement format string, exact syntax depends on the value of flagsflags: flags used to determine how the match will be performedout: output iterator to store the result of the replacement
# Return value
As described above.
# 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';
}
# Defect reports
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2213 | C++11 | out was not updated by the replacements | out is updated |