名稱空間
變體
操作

std::match_results<BidirIt,Alloc>::format

來自 cppreference.com
< cpp‎ | regex‎ | match results
 
 
 
正則表示式庫
(C++11)
演算法
迭代器
異常
特性
常量
(C++11)
正則表示式語法
 
 
template< class OutputIt >

OutputIt format( OutputIt out,
                 const char_type* fmt_first, const char_type* fmt_last,
                 std::regex_constants::match_flag_type flags =

                     std::regex_constants::format_default ) const;
(1) (C++11 起)
template< class OutputIt, class ST, class SA >

OutputIt format( OutputIt out,
                 const basic_string<char_type,ST,SA>& fmt,
                 std::regex_constants::match_flag_type flags =

                     std::regex_constants::format_default ) const;
(2) (C++11 起)
template< class ST, class SA >

std::basic_string<char_type,ST,SA>
    format( const std::basic_string<char_type,ST,SA>& fmt,
            std::regex_constants::match_flag_type flags =

                std::regex_constants::format_default ) const;
(3) (C++11 起)
string_type format( const char_type* fmt_s,

                    std::regex_constants::match_flag_type flags =

                        std::regex_constants::format_default ) const;
(4) (C++11 起)

format 輸出一個格式字串,將該字串中的所有格式說明符或轉義序列替換為來自 *this 的匹配資料。

1) 格式字元序列由範圍 [fmt_firstfmt_last) 定義。生成的字元序列被複制到 out
2) 格式字元序列由 fmt 中的字元定義。生成的字元序列被複制到 out
3,4) 格式字元序列分別由 fmtfmt_s 中的字元定義。生成的字元序列被複制到一個新構造的 std::basic_string 中並返回。

flags 位掩碼決定哪些格式說明符和轉義序列被識別。

如果 ready() != true,則 format 的行為未定義。

目錄

[編輯] 引數

fmt_begin, fmt_end - 指向定義格式字元序列的字元範圍的指標
fmt - 定義格式字元序列的 std::basic_string
fmt_s - 指向定義格式字元序列的以 null 結尾的字串的指標
out - 用於複製生成字元序列的迭代器
flags - std::regex_constants::match_flag_type 位掩碼,指定識別哪些格式說明符和轉義序列
型別要求
-
OutputIt 必須滿足 LegacyOutputIterator 的要求。

[編輯] 返回值

1,2) out
3,4) 包含結果字元序列的新構造字串。

[編輯] 異常

可能丟擲實現定義的異常。

[編輯] 示例

#include <iostream>
#include <regex>
#include <string>
 
int main()
{
    std::string s = "for a good time, call 867-5309";
    std::regex phone_regex("\\d{3}-\\d{4}");
    std::smatch phone_match;
 
    if (std::regex_search(s, phone_match, phone_regex))
    {
        std::string fmt_s = phone_match.format(
            "$`"   // $` means characters before the match
            "[$&]" // $& means the matched characters
            "$'"); // $' means characters following the match
        std::cout << fmt_s << '\n';
    }   
}

輸出

for a good time, call [867-5309]

[編輯] 另請參閱

用格式化的替換文字替換正則表示式的出現
(函式模板) [編輯]
匹配特有的選項
(typedef) [編輯]