[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 OutputIterator regex_replace(OutputIterator out, BidirectionalIterator first, BidirectionalIterator last, const basic_regex& e, const basic_string& fmt, regex_constants::match_flag_type flags = regex_constants::match_default); template OutputIterator regex_replace(OutputIterator out, BidirectionalIterator first, BidirectionalIterator last, const basic_regex& 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 i(first, last, e, flags) and uses i to enumerate through all of the matches m of type match_results 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::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 basic_string regex_replace(const basic_string& s, const basic_regex& e, const basic_string& fmt, regex_constants::match_flag_type flags = regex_constants::match_default); template basic_string regex_replace(const basic_string& s, const basic_regex& 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 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 basic_string regex_replace(const charT* s, const basic_regex& e, const basic_string& fmt, regex_constants::match_flag_type flags = regex_constants::match_default); template basic_string regex_replace(const charT* s, const basic_regex& 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 and calls:regex_replace(back_inserter(result), s, s + char_traits::length(s), e, fmt, flags); [6](#6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L12215) *Returns*: result[.](#6.sentence-1)