[re.alg.replace]

# 28 Text processing library [[text]](./#text)

## 28.6 Regular expressions library [[re]](re#alg.replace)

### 28.6.10 Regular expression algorithms [[re.alg]](re.alg#replace)

#### 28.6.10.4 regex_replace [re.alg.replace]

[ð](#lib:regex_replace)

`template<class OutputIterator, class BidirectionalIterator,
         class traits, class charT, class ST, class SA>
  OutputIterator
    regex_replace(OutputIterator out,
                  BidirectionalIterator first, BidirectionalIterator last,
                  const basic_regex<charT, traits>& e,
                  const basic_string<charT, ST, SA>& fmt,
                  regex_constants::match_flag_type flags = regex_constants::match_default);
template<class OutputIterator, class BidirectionalIterator, class traits, class charT>
  OutputIterator
    regex_replace(OutputIterator out,
                  BidirectionalIterator first, BidirectionalIterator last,
                  const basic_regex<charT, traits>& e,
                  const charT* fmt,
                  regex_constants::match_flag_type flags = regex_constants::match_default);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L12109)

*Effects*: Constructs a regex_iterator object i as if byregex_iterator<BidirectionalIterator, charT, traits> i(first, last, e, flags) and uses i to enumerate through all
of the matches m of type match_results<BidirectionalIterator> that occur within the sequence [first, last)[.](#1.sentence-1)

If no such
matches are found and!(flags & regex_constants​::​format_no_copy), then callsout = copy(first, last, out)

If any matches are found then, for each such match:

- [(1.1)](#1.1)

If !(flags & regex_constants​::​format_no_copy), callsout = copy(m.prefix().first, m.prefix().second, out)

- [(1.2)](#1.2)

Then callsout = m.format(out, fmt, flags) for the first form of the function andout = m.format(out, fmt, fmt + char_traits<charT>::length(fmt), flags) for the second.

Finally, if such a match
is found and !(flags & regex_constants​::​format_no_copy),
callsout = copy(last_m.suffix().first, last_m.suffix().second, out) where last_m is a copy of the last match
found[.](#1.sentence-3)

If flags & regex_constants​::​format_first_only is nonzero, then only the first match found is replaced[.](#1.sentence-4)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L12155)

*Returns*: out[.](#2.sentence-1)

[ð](#lib:regex_replace_)

`template<class traits, class charT, class ST, class SA, class FST, class FSA>
  basic_string<charT, ST, SA>
    regex_replace(const basic_string<charT, ST, SA>& s,
                  const basic_regex<charT, traits>& e,
                  const basic_string<charT, FST, FSA>& fmt,
                  regex_constants::match_flag_type flags = regex_constants::match_default);
template<class traits, class charT, class ST, class SA>
  basic_string<charT, ST, SA>
    regex_replace(const basic_string<charT, ST, SA>& s,
                  const basic_regex<charT, traits>& e,
                  const charT* fmt,
                  regex_constants::match_flag_type flags = regex_constants::match_default);
`

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L12177)

*Effects*: Constructs an empty string result of
type basic_string<charT, ST, SA> and calls:regex_replace(back_inserter(result), s.begin(), s.end(), e, fmt, flags);

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L12185)

*Returns*: result[.](#4.sentence-1)

[ð](#lib:regex_replace__)

`template<class traits, class charT, class ST, class SA>
  basic_string<charT>
    regex_replace(const charT* s,
                  const basic_regex<charT, traits>& e,
                  const basic_string<charT, ST, SA>& fmt,
                  regex_constants::match_flag_type flags = regex_constants::match_default);
template<class traits, class charT>
  basic_string<charT>
    regex_replace(const charT* s,
                  const basic_regex<charT, traits>& e,
                  const charT* fmt,
                  regex_constants::match_flag_type flags = regex_constants::match_default);
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L12207)

*Effects*: Constructs an empty string result of
type basic_string<charT> and calls:regex_replace(back_inserter(result), s, s + char_traits<charT>::length(s), e, fmt, flags);

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L12215)

*Returns*: result[.](#6.sentence-1)
