regex_regex_replace

regex_regex_replace (정규식 대체)

std::regex_replace은 정규식 일치를 포맷 문자열로 대체해 대상 시퀀스에서 일치하지 않는 부분은 복사본으로 유지해 출력하는 함수예요. C++11부터 사용할 수 있어요.

출처: cppreference

본문

<regex> 헤더에 정의되어 있고, 시그니처는 다음과 같아요.

template< class OutputIt, class BidirIt, class Traits, class CharT,
          class STraits, class SAlloc >
OutputIt regex_replace( OutputIt out, BidirIt first, BidirIt last,
                        const std::basic_regex<CharT, Traits>& re,
                        const std::basic_string<CharT, STraits, SAlloc>& fmt,
                        std::regex_constants::match_flag_type flags =
                            std::regex_constants::match_default );

(1) (since C++11)

template< class OutputIt, class BidirIt, class Traits, class CharT >
OutputIt regex_replace( OutputIt out, BidirIt first, BidirIt last,
                        const std::basic_regex<CharT, Traits>& re,
                        const CharT* fmt,
                        std::regex_constants::match_flag_type flags =
                            std::regex_constants::match_default );

(2) (since C++11)

template< class Traits, class CharT, class STraits, class SAlloc, class FAlloc >
std::basic_string<CharT, STraits, SAlloc> regex_replace(
    const std::basic_string<CharT, STraits, SAlloc>& str,
    const std::basic_regex<CharT, Traits>& re,
    const std::basic_string<CharT, STraits, SAlloc>& fmt,
    std::regex_constants::match_flag_type flags =
        std::regex_constants::match_default );

(3)

정규식 일치를 포맷 문자열로 대체해 대상 시퀀스에서 일치하지 않는 부분은 복사본으로 유지해 출력해요.

    1. [first, last) 시퀀스를 순회하며 re와 일치하는 부분을 fmt로 바꾼 문자열을 out에 써요.
    1. fmt가 CharT* 포인터라는 점을 제외하면 (1)과 같아요.
    1. 문자열 str의 모든 일치를 fmt로 바꾼 결과 문자열을 반환해요.

반환값 (Return value)

  • 1,2) out에 쓴 후의 출력 반복자
    1. 대체가 적용된 결과 문자열

참고 (Notes)

일치하지 않는 부분은 기본적으로 출력에 복사되지만, match_not_null 또는 format_no_copy 플래그로 동작을 바꿀 수 있어요.

더 알아보기 (Learn more)

cppreference