std::match_results<BidirIt,Alloc>::format
提供: cppreference.com
<tbody>
</tbody>
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 ビットマスクは、どの書式指定子およびエスケープシーケンスが認識されるかを決定します。
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) 結果の文字シーケンスを格納する新たに構築された文字列。
例外
(なし)
例
Run this code
#include <iostream>
#include <string>
#include <regex>
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]
関連項目
(C++11) |
正規表現の現れた部分を書式化された置換テキストに置き換えます (関数テンプレート) |
(C++11) |
マッチングに固有のオプション (typedef) |