名稱空間
變體
操作

std::regex_replace

來自 cppreference.com
< cpp‎ | regex
在標頭檔案 <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) 複製範圍 [firstlast) 中的字元到 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 = /* 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;
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;

目錄

[edit] 引數

first, last - 目標字元範圍
str - 目標 std::string
s - 目標空終止 C 風格字串
re - 正則表示式
fmt - 正則表示式替換格式字串,確切語法取決於 flags 的值
flags - 用於確定匹配方式的標誌
out - 輸出迭代器,用於儲存替換結果

[edit] 返回值

如上所述。

[edit] 異常

可能丟擲 std::regex_error 以指示錯誤條件

[edit] 示例

#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';
}

輸出

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

[edit] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2213 C++11 out 未被替換更新 out 已更新

[edit] 參閱

嘗試將正則表示式與字元序列的任何部分匹配
(函式模板) [編輯]
匹配特有的選項
(typedef) [編輯]
替換字串的指定部分
(std::basic_string<CharT,Traits,Allocator> 的公有成員函式) [編輯]