首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::match_results::format

template< class OutputIt > OutputIter 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 > OutputIter 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%29格式字符序列由范围定义。[fmt_first, fmt_last)生成的字符序列被复制到out...

2%29格式字符序列由fmt生成的字符序列被复制到out...

格式字符序列由fmtfmt_s分别。生成的字符序列被复制到新构造的std::basic_string,这是返回的。

flags位掩码确定识别哪种格式说明符和转义序列。

...的行为format是未定义的,如果ready() != true...

参数

fmt_begin, fmt_end

-

pointers to a range of characters defining the format character sequence

fmt

-

std::basic_string defining the format character sequence

fmt_s

-

pointer to a null-terminated character string defining the format character sequence

out

-

iterator that the resulting character sequence is copied to

flags

-

std::regex_constants::match_flag_type bitmask specifying which format specifiers and escape sequences are recognized

类型要求

-输出必须符合输出器的要求。

返回值

1-2%29out

3-4%29新构造的字符串,包含产生的字符序列。

例外

%280%29

二次

代码语言:javascript
复制
#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';
    }   
}

二次

产出:

二次

代码语言:javascript
复制
for a good time, call [867-5309]

二次

另见

regex_replace (C++11)

replaces occurrences of a regular expression with formatted replacement text (function template)

match_flag_type (C++11)

options specific to matching (typedef)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券