std:: regex_replace
|
헤더 파일에 정의됨
<regex>
|
||
|
template
<
class
OutputIt,
class
BidirIt,
class
Traits,
class
CharT,
class
STraits,
class
SAlloc
>
|
(1) | (C++11 이후) |
|
template
<
class
OutputIt,
class
BidirIt,
class
Traits,
class
CharT
>
OutputIt regex_replace
(
OutputIt out, BidirIt first, BidirIt last,
|
(2) | (C++11 이후) |
|
template
<
class
Traits,
class
CharT,
class
STraits,
class
SAlloc,
class
FTraits,
class
FAlloc
>
|
(3) | (C++11부터) |
|
template
<
class
Traits,
class
CharT,
class
STraits,
class
SAlloc
>
std::
basic_string
<
CharT, STraits, SAlloc
>
|
(4) | (C++11 이후) |
|
template
<
class
Traits,
class
CharT,
class
STraits,
class
SAlloc
>
std::
basic_string
<
CharT
>
|
(5) | (C++11 이후) |
|
template
<
class
Traits,
class
CharT
>
std::
basic_string
<
CharT
>
|
(6) | (C++11부터) |
regex_replace
는 정규 표현식
re
를 사용하여 대상 문자 시퀀스에 대한 치환을 수행합니다:
[
first
,
last
)
범위의 문자들을
out
으로 복사하되,
re
와 일치하는 모든 시퀀스를
fmt
로 포맷된 문자로 대체합니다. 다음 코드와 동일합니다:
using iter_type = std::regex_iterator<BidirIt, CharT, Traits>; iter_type seq_begin(first, last, re, flags), seq_end; using result_type = std::match_results<BidirIt>; result_type m; bool need_to_copy = (flags & std::regex_constants::format_no_copy) == 0; bool format_all = (flags & std::regex_constants::format_first_only) != 0; for (iter_type i = seq_begin; i != seq.end(); ++i) { m = *i; if (need_to_copy) out = std::copy(m.prefix().first, m.prefix().second, out); if (format_all || i == seq_begin) out = /* replace-expr */ } if (need_to_copy) out = m.ready() ? std::copy(m.suffix().first, m.suffix().second, out) : std::copy(first, last, out); return out;
regex_replace ( std:: back_inserter ( result ) ,
str. begin ( ) , str. end ( ) , re, fmt, flags ) ;
return result ; .
regex_replace ( std:: back_inserter ( result ) ,
s, s + std:: char_traits < CharT > :: length ( s ) , re, fmt, flags ) ;
return result ; .
목차 |
매개변수
| first, last | - | 대상 문자 범위 |
| str | - | 대상 std::string |
| s | - | 대상 null-terminated C-style 문자열 |
| re | - | 정규 표현식 |
| fmt | - | regex 치환 형식 문자열, 정확한 구문은 flags 값에 따라 결정됨 |
| flags | - | 매칭 수행 방식을 결정하는 데 사용되는 플래그 |
| out | - | 치환 결과를 저장할 출력 반복자 |
반환값
위에서 설명한 대로입니다.
예외
std::regex_error 를 던져 오류 상태 를 나타낼 수 있습니다.
예제
#include <iostream> #include <iterator> #include <regex> #include <string> int main() { std::string text = "Quick brown fox"; std::regex vowel_re("a|e|i|o|u"); // 결과를 출력 반복자에 기록 std::regex_replace(std::ostreambuf_iterator<char>(std::cout), text.begin(), text.end(), vowel_re, "*"); // 결과를 보관하는 문자열 생성 std::cout << '\n' << std::regex_replace(text, vowel_re, "[$&]") << '\n'; }
출력:
Q**ck br*wn f*x Q[u][i]ck br[o]wn f[o]x
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2213 | C++11 | out 이 대체 작업으로 업데이트되지 않음 | out 이 업데이트됨 |
참고 항목
|
(C++11)
|
정규 표현식을 문자 시퀀스의 임의의 부분과 매칭 시도
(함수 템플릿) |
|
(C++11)
|
매칭에 특화된 옵션들
(typedef) |
|
문자열의 지정된 부분을 교체
(
std::basic_string<CharT,Traits,Allocator>
의
public member function)
|