Namespaces
Variants

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

From cppreference.net
Regular expressions library
Classes
(C++11)
Algorithms
Iterators
Exceptions
Traits
Constants
(C++11)
Regex Grammar
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 비트마스크는 어떤 형식 지정자와 이스케이프 시퀀스가 인식되는지 결정합니다.

format 의 동작은 ready ( ) ! = true 일 때 정의되지 않습니다.

목차

매개변수

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]

참고 항목

정규 표현식의 발생을 형식화된 대체 텍스트로 교체
(함수 템플릿)
매칭에 특화된 옵션들
(typedef)