std::regex_replace

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

# 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

DRApplied toBehavior as publishedCorrect behavior
LWG 2213C++11out was not updated by the replacementsout is updated

# See also