regex_replace Function
대체 정규식 일치 합니다.
template<class OutIt, class BidIt, class RXtraits, class Alloc, class Elem>
OutIt regex_replace(OutIt out,
BidIt first, BidIt last,
const basic_regex<Elem, RXtraits, Alloc>& re,
const basic_string<Elem>& fmt,
match_flag_type flags = match_default);
template<class RXtraits, class Alloc, class Elem>
basic_string<Elem> regex_replace(const basic_string<Elem>& str,
const basic_regex<Elem, RXtraits, Alloc>& re,
const basic_string<Elem>& fmt,
match_flag_type flags = match_default);
매개 변수
OutIt
교체에 대 한 반복기의 형식입니다.BidIt
부분에 대 한 반복기의 형식입니다.RXtraits
특성 클래스 요소입니다.Alloc
정규식 할당자 클래스입니다.Elem
일치 하는 요소의 형식입니다.flags
일치 하는 항목에 대 한 플래그입니다.first
일치 하는 시퀀스의 시작 부분입니다.fmt
바꾸기 작업에 대 한 형식입니다.last
일치 하는 시퀀스의 끝입니다.out
출력 반복기입니다.re
일치 하는 정규식입니다.str
일치 하는 문자열입니다.
설명
첫 번째 함수가 구성는 regex_iterator Class 개체 iter(first, last, re, flags) 입력된 범위 분할을 사용 하 여 [first, last) 에 일련의가 T0M0T1M1...TN-1MN-1TN여기서 Mn 되는 nth 반복기가 발견 되는 일치.일치 하는 경우 T0 전체 범위를 입력 하 고 N 0입니다.경우 (flags & format_first_only) != 0 첫 번째 일치 항목이 사용 하는 T1 모든 일치에 오는 텍스트를 입력된 하 고 N 1입니다.각 i 범위에서 [0, N)경우 (flags & format_no_copy) == 0 텍스트 범위에서 복사 하 여 Ti 를 out.그런 다음 호출 m.format(out, fmt, flags)여기서 m 되는 match_results 반복기 개체에서 반환 되는 개체 iter subsequence에 대 한 Mi.마지막으로, 경우 (flags & format_no_copy) == 0 텍스트 범위에서 복사 하 여 TN 를 out.함수 반환 out.
두 번째 함수는 로컬 변수 생성 result 유형 basic_string<charT> 호출 하 고 regex_replace(back_inserter(result), str.begin(), str.end(), re, fmt, flags).반환 result.
예제
// std_tr1__regex__regex_replace.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
int main()
{
char buf[20];
const char *first = "axayaz";
const char *last = first + strlen(first);
std::regex rx("a");
std::string fmt("A");
std::regex_constants::match_flag_type fonly =
std::regex_constants::format_first_only;
*std::regex_replace(&buf[0], first, last, rx, fmt) = '\0';
std::cout << "replacement == " << &buf[0] << std::endl;
*std::regex_replace(&buf[0], first, last, rx, fmt, fonly) = '\0';
std::cout << "replacement == " << &buf[0] << std::endl;
std::string str("adaeaf");
std::cout << "replacement == "
<< std::regex_replace(str, rx, fmt) << std::endl;
std::cout << "replacement == "
<< std::regex_replace(str, rx, fmt, fonly) << std::endl;
return (0);
}
요구 사항
헤더: <regex>
네임 스페이스: std