function template
<regex>

std::regex_replace

c-string/c-string (1)
template <class traits, class charT>  basic_string<charT> regex_replace (const charT* s,          const basic_regex<charT,traits>& rgx,          const charT* fmt,          regex_constants::match_flag_type flags = regex_constants::match_default);
c-string/string (2)
template <class traits, class charT, class ST, class SA>  basic_string<charT> regex_replace (const charT*s,          const basic_regex<charT,traits>& rgx,          const basic_string<charT,ST,SA>& fmt,          regex_constants::match_flag_type flags = regex_constants::match_default);
string/c-string (3)
template <class traits, class charT, class ST, class SA>  basic_string<charT,ST,SA> regex_replace (const basic_string<charT,ST,SA>& s,          const basic_regex<charT,traits>& rgx,          const charT* fmt,          regex_constants::match_flag_type flags = regex_constants::match_default);
string/string (4)
template <class traits, class charT, class ST, class SA, class FST, class FSA>  basic_string<charT,ST,SA> regex_replace (const basic_string<charT,ST,SA>& s,          const basic_regex<charT,traits>& rgx,          const basic_string<charT,FST,FSA>& fmt,          regex_constants::match_flag_type flags = regex_constants::match_default);
range/c-string (5)
template <class OutputIterator, class BidirectionalIterator,          class traits, class charT>  OutputIterator regex_replace (OutputIterator out,          BidirectionalIterator first, BidirectionalIterator last,          const basic_regex<charT,traits>& rgx,          const charT* fmt,          regex_constants::match_flag_type flags = regex_constants::match_default);
range/string (6)
template <class OutputIterator, class BidirectionalIterator,          class traits, class charT, class ST, class SA>  OutputIterator regex_replace (OutputIterator out,          BidirectionalIterator first, BidirectionalIterator last,          const basic_regex<charT,traits>& rgx,          const basic_string<charT,ST,SA>& fmt,          regex_constants::match_flag_type flags = regex_constants::match_default);
替換匹配序列
使用正則表示式rgx(模式)的所有匹配項都被fmt(替換)替換,從而建立目標序列(主題)的副本。根據所使用的版本,目標序列sfirstlast之間的字元序列。

版本1234將結果序列作為string物件返回。版本56輸出迭代器作為第一個引數,用於儲存結果序列。

一個可選引數flags允許指定如何匹配表示式和格式化表示式的選項。

引數

s
包含目標序列(主題)的字串。
rgx
一個用於匹配的basic_regex物件(模式)。
fmt
包含每次匹配替換的字串。
這可能包含格式說明符和轉義序列,它們將被表示的字元替換。對於format_default,可能的說明符是
字元替換內容
$nn次反向引用(即,與正則表示式模式中用括號指定的第n個匹配組的副本)。
n必須是一個整數值,表示一個有效的反向引用,大於0,最多兩位數。
$&整個匹配的副本
$`字首(即,目標序列中位於匹配之前的部分)。
字尾(即,目標序列中位於匹配之後的部分)。
$$單個$字元。
標誌
用於控制rgx如何匹配以及fmt如何格式化的標誌。
這些常量可以一個或多個(使用按位或運算子 |)組合,形成一個有效的位掩碼值,型別為regex_constants::match_flag_type:
flag*effects說明
match_default預設預設匹配行為。
此常量的值為零**。
match_not_bol非行首第一個字元不被視為行首"^"不匹配)。
match_not_eol非行尾最後一個字元不被視為行尾"$"不匹配)。
match_not_bow非詞首轉義序列"\b"不匹配作為詞首
match_not_eow非詞尾轉義序列"\b"不匹配作為詞尾
match_any任何匹配如果可能存在多個匹配,則接受任何匹配。
match_not_null非空空序列不匹配。
match_continuous連續表示式必須匹配以第一個字元開始的子序列。
子序列必須以第一個字元開始才能匹配。
match_prev_avail先前可用第一個字元之前存在一個或多個字元。(match_not_bolmatch_not_bow被忽略)
format_default預設格式使用標準的格式化規則替換匹配項(ECMAScript的replace方法使用的規則)。
此常量的值為零**。
format_sedsed 格式使用與 POSIX 中 sed 實用程式相同的規則來替換匹配項。
format_no_copy不復制替換匹配項時,不會複製目標序列中與正則表示式不匹配的部分。
format_first_only僅首次僅替換正則表示式的第一次出現。
* 這些位掩碼標誌名稱可在std::regex_constants名稱空間下找到(更多詳情請參見regex_constants)。
** 值為零的常量如果設定了其他標誌,則會被忽略。
match_flag_type是一個可在std::regex_constants名稱空間內定義。
下可用的型別
輸出迭代器型別的型別,指向序列的第一個字元,結果序列儲存在那裡。
函式模板型別可以是任何型別的輸出迭代器到字元。
first, last
雙向迭代器,指向用作匹配目標序列的字元範圍的起始和結束位置。使用的範圍是[first,last),其中包括firstlast之間的所有字元,包括first指向的字元,但不包括last指向的字元。
函式模板型別可以是任何指向字元的雙向迭代器

返回值

版本1234返回一個包含結果序列的string物件。
版本56返回out
版本56返回一個指向out指向的序列中最後一個寫入字元之後的元素的迭代器。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// regex_replace example
#include <iostream>
#include <string>
#include <regex>
#include <iterator>

int main ()
{
  std::string s ("there is a subsequence in the string\n");
  std::regex e ("\\b(sub)([^ ]*)");   // matches words beginning by "sub"

  // using string/c-string (3) version:
  std::cout << std::regex_replace (s,e,"sub-$2");

  // using range/c-string (6) version:
  std::string result;
  std::regex_replace (std::back_inserter(result), s.begin(), s.end(), e, "$2");
  std::cout << result;

  // with flags:
  std::cout << std::regex_replace (s,e,"$1 and $2",std::regex_constants::format_no_copy);
  std::cout << std::endl;

  return 0;
}

輸出
there is a sub-sequence in the string
there is a sequence in the string
sub and sequence


另見