命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | regex‎ | match results
 
 
 
正则表达式库
(C++11)
算法
迭代器
异常
特性
常量
(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) (since 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) (since 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) (since C++11)
string_type format( const char_type* fmt_s,

                    std::regex_constants::match_flag_type flags =

                        std::regex_constants::format_default ) const;
(4) (since C++11)

format 输出一个格式化字符串,将该字符串中的任何格式说明符或转义序列替换为来自 *this 的匹配数据。

1) 格式化字符序列由范围 [fmt_firstfmt_last) 定义。结果字符序列被复制到 out
2) 格式化字符序列由 fmt 中的字符定义。结果字符序列被复制到 out
3,4) 格式化字符序列分别由 fmtfmt_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) 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]

[编辑] 参见

替换正则表达式的出现,使用格式化的替换文本
(函数模板) [编辑]
匹配特定的选项
(类型定义) [编辑]