std::match_results<BidirIt,Alloc>::format
来自cppreference.com
| |
(1) | (C++11 起) |
| |
(2) | (C++11 起) |
| |
(3) | (C++11 起) |
| |
(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 | - | 指向定义格式字符序列的空终止字符串的指针 |
| out | - | 要复制结果字符串到的迭代器 |
| flags | - | 指定辨识哪些格式指定符和转义序列的 std::regex_constants::match_flag_type 位掩码 |
| 类型要求 | ||
-OutputIt 必须满足老式输出迭代器 (LegacyOutputIterator) 。
| ||
返回值
1,2)
out3,4) 含结果字符序列的新构造字符串。
异常
可能会抛出由实现定义的异常。
示例
运行此代码
#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) |