template< class OutputIt >
OutputIter format( OutputIt out, |
(1) | (C++11 起) |
template< class OutputIt, class ST, class SA >
OutputIter format( OutputIt out, |
(2) | (C++11 起) |
template< class ST, class SA >
std::basic_string<char_type,ST,SA> |
(3) | (C++11 起) |
string_type format( const char_type* fmt_s,
std::regex_constants::match_flag_type flags = |
(4) | (C++11 起) |
format
输出格式字符串,它将字符串中的任何格式指定符或转义序列替换为来自 *this 的数据。
[fmt_first, fmt_last)
定义。复制结果字符序列到 out
的副本。fmt
中的字符定义。复制结果字符序列到 out
的副本。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 位掩码 |
类型要求 | ||
-
OutputIt 必须满足 OutputIterator 的要求。
|
out
(无)
#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( "$`" // $` 意味着匹配之前的字符 "[$&]" // $& 意味着匹配的字符 "$'"); // $' 意味着后随匹配的字符 std::cout << fmt_s << '\n'; } }
输出:
for a good time, call [867-5309]
(C++11)
|
以格式化的替换文本来替换正则表达式匹配的出现位置 (函数模板) |
(C++11)
|
特定到匹配的选项 (typedef) |