名前空間
変種
操作

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

From cppreference.com
< cpp‎ | regex‎ | match results
 
 
 
正規表現ライブラリ
クラス
(C++11)
アルゴリズム
イテレータ
例外
Traits
定数
(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) フォーマット文字シーケンスは、それぞれ fmt および fmt_s の文字によって定義されます。結果の文字シーケンスは、新しく構築された std::basic_string にコピーされ、それが返されます。

flags ビットマスクは、どのフォーマット指定子およびエスケープシーケンスが認識されるかを決定します。

ready() != true の場合、format の動作は未定義です。

目次

[編集] パラメータ

fmt_begin, fmt_end - フォーマット文字シーケンスを定義する文字の範囲へのポインタ
fmt - std::basic_string フォーマット文字シーケンスを定義
fmt_s - フォーマット文字シーケンスを定義するヌル終端文字列へのポインタ
out - 結果の文字シーケンスがコピーされるイテレータ
flags - std::regex_constants::match_flag_type 認識されるフォーマット指定子およびエスケープシーケンスを指定するビットマスク
型要件
-
OutputItLegacyOutputIterator の要件を満たさなければなりません。

[編集] 戻り値

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) [編集]
English 日本語 中文(简体) 中文(繁體)