std::regex_replace(3)
π std::regex_replace β the C++ Standard Library glow-up you've been waiting for
okay bestie, so this one lives in the C++ Standard Library and it's literally the find-and-replace of your regex dreams. std::regex_replace is the name, replacing matching stuff is the game, and she's been here since C++11. periodt. β¨
πΈ SYNOPSIS πΈ
First things first, she's Defined in header <regex> π
Overload (1) β (since C++11)
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 );
Overload (2) β (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 );
Overload (3) β (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>& s,
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 );
Overload (4) β (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>& s,
const std::basic_regex<CharT,Traits>& re,
const CharT* fmt,
std::regex_constants::match_flag_type flags =
std::regex_constants::match_default );
Overload (5) β (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 );
Overload (6) β (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 );
π DESCRIPTION π
so regex_replace uses a regular expression to perform substitution on a sequence of characters, and honestly? it's giving main character energy. here's the whole breakdown:
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. π«Ά (she's selective, we respect it) - If
flagscontainsstd::regex_constants::format_first_only, only the first match is replaced. one and done, iconic.
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 iteratorssβ the input character sequence, represented asstd::basic_stringor character arrayreβ thestd::basic_regexthat will be matched against the input sequencefmtβ the regex replacement format string, exact syntax depends on the value offlags(iykyk, check the flags bestie)flagsβ the match flags of typestd::regex_constants::match_flag_typeoutβ output iterator to store the result of the replacement
β¨ TYPE REQUIREMENTS β¨
OutputItmust meet the requirements ofLegacyOutputIterator.BidirItmust meet the requirements ofLegacyBidirectionalIterator.
no cap, those are non-negotiable π
π RETURN VALUE π
- 1,2) Returns a copy of the output iterator
outafter all the insertions. - 3-6) Returns the string
resultwhich contains the output.
π₯ EXCEPTIONS π₯
May throw std::regex_error to indicate an error condition. π (she's dramatic but fair)
π EXAMPLE π
// Run this code
#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
look at her go. vowels got absolutely ate. β
π SEE ALSO π
regex_searchβ attempts to match a regular expression to any part of a character sequence (function template)(C++11)match_flag_typeβ options specific to matching (typedef)(C++11)replaceβ replaces specified portion of a string (public member function ofstd::basic_string<CharT,Traits,Allocator>)
and that's the whole page, bestie π huge thanks to cppreference.com, version 2024.06.10 β this little love letter to std::regex_replace was dated then too, so go forth and replace responsibly π§ββοΈβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms