Namespaces
Variants

std:: regex_replace

From cppreference.net
定义于头文件 <regex>
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 ) ;
(1) (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 ) ;
(2) (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 ) ;
(3) (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 ) ;
(4) (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 ) ;
(5) (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 ) ;
(6) (C++11 起)

regex_replace 使用正则表达式 re 对目标字符序列执行替换操作:

1,2) 将范围 [ first , last ) 中的字符复制到 out ,并将所有匹配 re 的序列替换为通过 fmt 格式化的字符。等价于:
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 = /* 替换表达式 */
}
if (need_to_copy)
    out = m.ready()
              ? std::copy(m.suffix().first, m.suffix().second, out)
              : std::copy(first, last, out);
return out;
1) 表达式 /* replace-expr */ m. format ( out, fmt, flags )
2) 表达式 /* replace-expr */ m. format ( out, fmt, fmt + std:: char_traits < CharT > :: length ( fmt ) , flags )
3,4) 等价于 std:: basic_string < CharT, STraits, SAlloc > result ;
regex_replace ( std:: back_inserter ( result ) ,
str. begin ( ) , str. end ( ) , re, fmt, flags ) ;
return result ;
5,6) 等价于 std:: basic_string < CharT, STraits, SAlloc > result ;
regex_replace ( std:: back_inserter ( result ) ,
s, s + std:: char_traits < CharT > :: length ( s ) , re, fmt, flags ) ;
return result ;

目录

参数

first, last - 目标字符范围
str - 目标 std::string
s - 目标以空字符结尾的C风格字符串
re - 正则表达式
fmt - 正则表达式替换格式字符串,具体语法取决于 flags 的值
flags - 用于确定匹配执行方式的标志位
out - 用于存储替换结果的输出迭代器

返回值

如上所述。

异常

可能抛出 std::regex_error 以指示 错误条件

示例

#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");
    // 将结果写入输出迭代器
    std::regex_replace(std::ostreambuf_iterator<char>(std::cout),
                       text.begin(), text.end(), vowel_re, "*");
    // 构造包含结果的字符串
    std::cout << '\n' << std::regex_replace(text, vowel_re, "[$&]") << '\n';
}

输出:

Q**ck br*wn f*x
Q[u][i]ck br[o]wn f[o]x

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 2213 C++11 out 未通过替换操作更新 out 会被更新

参见

尝试将正则表达式与字符序列的任何部分进行匹配
(函数模板)
匹配专用的选项
(类型定义)
替换字符串的指定部分
( std::basic_string<CharT,Traits,Allocator> 的公开成员函数)