Localization library | |||||||||||||||||||||||||
Regular expressions library(C++11) | |||||||||||||||||||||||||
Formatting library(C++20) | |||||||||||||||||||||||||
Null-terminated sequence utilities | |||||||||||||||||||||||||
Byte strings | |||||||||||||||||||||||||
Multibyte strings | |||||||||||||||||||||||||
Wide strings | |||||||||||||||||||||||||
Primitive numeric conversions | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
Text encoding identifications | |||||||||||||||||||||||||
|
Classes | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Algorithms | ||||
(C++11) | ||||
(C++11) | ||||
regex_replace (C++11) | ||||
Iterators | ||||
(C++11) | ||||
(C++11) | ||||
Exceptions | ||||
(C++11) | ||||
Traits | ||||
(C++11) | ||||
Constants | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Regex Grammar | ||||
(C++11) |
Defined in header <regex> | ||
template<class OutputIt,class BidirIt,class Traits,class CharT, class STraits,class SAlloc> | (1) | (since C++11) |
template<class OutputIt,class BidirIt,class Traits,class CharT> OutputIt regex_replace( OutputIt out, BidirIt first, BidirIt last, | (2) | (since C++11) |
template<class Traits,class CharT, class STraits,class SAlloc,class FTraits,class FAlloc> | (3) | (since C++11) |
template<class Traits,class CharT,class STraits,class SAlloc> std::basic_string<CharT, STraits, SAlloc> | (4) | (since C++11) |
template<class Traits,class CharT,class STraits,class SAlloc> std::basic_string<CharT> | (5) | (since C++11) |
template<class Traits,class CharT> std::basic_string<CharT> | (6) | (since C++11) |
regex_replace
uses the regular expressionre to perform substitution on the target character sequence:
[
first,
last)
toout, replacing any sequences that matchre with characters formatted byfmt. Equivalent to:using iter_type=std::regex_iterator<BidirIt, CharT, Traits>;iter_type seq_begin(first, last, re, flags), seq_end; using result_type=std::match_results<BidirIt>;result_type m; bool need_to_copy=(flags&std::regex_constants::format_no_copy)==0;bool format_all=(flags&std::regex_constants::format_first_only)!=0; for(iter_type i= seq_begin; i!= seq.end();++i){ m=*i;if(need_to_copy) out=std::copy(m.prefix().first, m.prefix().second, out);if(format_all|| i== seq_begin) out=/* replace-expr */} if(need_to_copy) out= m.ready()?std::copy(m.suffix().first, m.suffix().second, out):std::copy(first, last, out); return out;
Contents |
first, last | - | the target character range |
str | - | the targetstd::string |
s | - | the target null-terminated C-style string |
re | - | the regular expression |
fmt | - | the regex replacement format string, exact syntax depends on the value offlags |
flags | - | flags used to determine how the match will be performed |
out | - | output iterator to store the result of the replacement |
As described above.
May throwstd::regex_error to indicate anerror condition.
#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 resultsstd::cout<<'\n'<< std::regex_replace(text, vowel_re,"[$&]")<<'\n';}
Output:
Q**ck br*wn f*xQ[u][i]ck br[o]wn f[o]x
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2213 | C++11 | out was not updated by the replacements | out is updated |
(C++11) | attempts to match a regular expression to any part of a character sequence (function template)[edit] |
(C++11) | options specific to matching (typedef)[edit] |
replaces specified portion of a string (public member function of std::basic_string<CharT,Traits,Allocator> )[edit] |