public member function
<regex>

std::match_results::format

(1)
 string_type format (const char_type* fmt,     regex_constants::match_flag_type flags = regex_constants::format_default ) const;
(2)
template <class sT, class sA>  basic_string<char_type, sT, sA> format (const basic_string<char_type, sT, sA>& fmt,    regex_constants::match_flag_type flags = regex_constants::format_default ) const;
(3)
template <class OutputIterator, class sT, class sA>  OutputIterator format ( OutputIterator out,    const basic_string<char_type, sT, sA>& fmt,    regex_constants::match_flag_type flags = regex_constants::format_default ) const;
(4)
template <class OutputIterator>  OuputIterator format ( OutputIterator out,    const char_type* fmt_first, const char_type* fmt_last,    regex_constants::match_flag_type flags = regex_constants::format_default ) const;
格式化替換字串
版本(1)(2) 返回一個字串物件,其中包含 fmt 的副本,該副本的格式說明符和轉義序列已替換為它們所代表的字元。

版本 (3)(4) 執行相同的操作,但將結果字元序列儲存在從 out 開始的範圍內。

該函式使用 match_results 物件中的匹配項來替換引用它們的轉義序列。

match_results 物件應為 ready,這發生在它作為適當的引數傳遞給 regex_matchregex_search 的呼叫之後。

引數

fmt
格式字串。
這可能包括格式說明符和轉義序列,它們被它們所代表的字元替換。 對於format_default, 可能的說明符是
字元替換內容
$nn個反向引用(即,正則表示式模式中使用括號指定的第n個匹配組的副本)。
n 必須是指定有效反向引用的整數值,大於 1,並且最多兩位數。
$&整個匹配項的副本
$`字首(即,目標序列中位於匹配項之前的部分)。
字尾(即,目標序列中位於匹配項之後的部分)。
$$單個$字元。
標誌
用於解釋格式字串的標誌。
可以將這些常量中的一個或多個組合(使用按位 OR 運算子 |)以形成型別為有效的位掩碼值regex_constants::match_flag_type:
flag*對格式的影響說明
match_default預設format_default.
相同。 此常量的值為零**。
match_not_bol此函式忽略。
有關更多資訊,請參見 regex_constants
match_not_eol
match_not_bow
match_not_eow
match_any
match_not_null
match_continuous
match_prev_avail
format_default預設格式使用標準格式規則來替換匹配項(ECMAScript 的 replace 方法使用的規則)。
相同。 此常量的值為零**。
format_sedsed 格式使用與 POSIX 中 sed 實用程式相同的規則來替換匹配項。
format_no_copy不復制替換匹配項時,不會複製目標序列中與正則表示式不匹配的部分。
format_first_only僅首次僅替換正則表示式的第一次出現。
* 這些位掩碼標誌名稱在std::regex_constants名稱空間下可用(有關更多詳細資訊,請參見 regex_constants)。
** 如果設定了其他標誌,則值為零的常量將被忽略。
match_flag_typestd::regex_constants名稱空間內定義。
下可用的型別
輸出迭代器,指向儲存結果字串的字元序列。
fmt_first, fmt_last
包含格式字串的字元序列上的範圍。

返回值

對於 (1)(2),結果字串作為 字串 物件。
對於 (3)(4),該函式返回 out

string_type是一個成員型別,是與序列中使用的字元型別相對應的basic_string型別的別名(例如,對於cmatchsmatch).

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// match_results::format
// - using cmatch, a standard alias of match_results<const char*>
#include <iostream>
#include <regex>

int main ()
{
  std::cmatch m;

  std::regex_match ( "subject", m, std::regex("(sub)(.*)") );

  std::cout << m.format ("the expression matched [$0].\n");
  std::cout << m.format ("with sub-expressions [$1] and [$2].\n");

  return 0;
}

輸出
the expression matched [subject].
with sub-expressions [sub] and [ject].



另見