Namespaces
Variants

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

From cppreference.net
Regular expressions library
Classes
(C++11)
Algorithms
Iterators
Exceptions
Traits
Constants
(C++11)
Regex Grammar
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_first , fmt_last ) によって定義されます。結果の文字シーケンスは out にコピーされます。
2) フォーマット文字シーケンスは、 fmt 内の文字によって定義されます。結果の文字シーケンスは out にコピーされます。
3,4) フォーマット文字シーケンスは、それぞれ fmt および fmt_s 内の文字によって定義されます。結果の文字シーケンスは新しく構築された std::basic_string にコピーされ、それが返されます。

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

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

目次

パラメータ

fmt_begin, fmt_end - フォーマット文字シーケンスを定義する文字範囲へのポインタ
fmt - std::basic_string フォーマット文字シーケンスを定義する
fmt_s - フォーマット文字シーケンスを定義するヌル終端文字列へのポインタ
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)