Programming/c++
[c++] regex_match 함수
owls
2023. 1. 20. 12:55
728x90
1.함수 헤더 파일
#include <regex>
2.함수 원형
regex_match : 정규식이 전체 대상 문자열과 일치하는지 여부를 테스트합니다.
// (1)
template <class BidIt, class Alloc, class Elem, class RXtraits, class Alloc2>
bool regex_match(
BidIt first,
Bidit last,
match_results<BidIt, Alloc>& match,
const basic_regex<Elem, RXtraits, Alloc2>& re,
match_flag_type flags = match_default);
// (2)
template <class BidIt, class Elem, class RXtraits, class Alloc2>
bool regex_match(
BidIt first,
Bidit last,
const basic_regex<Elem, RXtraits, Alloc2>& re,
match_flag_type flags = match_default);
// (3)
template <class Elem, class Alloc, class RXtraits, class Alloc2>
bool regex_match(
const Elem *ptr,
match_results<const Elem*, Alloc>& match,
const basic_regex<Elem, RXtraits, Alloc2>& re,
match_flag_type flags = match_default);
// (4)
template <class Elem, class RXtraits, class Alloc2>
bool regex_match(
const Elem *ptr,
const basic_regex<Elem, RXtraits, Alloc2>& re,
match_flag_type flags = match_default);
// (5)
template <class IOtraits, class IOalloc, class Alloc, class Elem, class RXtraits, class Alloc2>
bool regex_match(
const basic_string<Elem, IOtraits, IOalloc>& str,
match_results<typename basic_string<Elem, IOtraits, IOalloc>::const_iterator, Alloc>& match,
const basic_regex<Elem, RXtraits, Alloc2>& re,
match_flag_type flags = match_default);
// (6)
template <class IOtraits, class IOalloc, class Elem, class RXtraits, class Alloc2>
bool regex_match(
const basic_string<Elem, IOtraits, IOalloc>& str,
const basic_regex<Elem, RXtraits, Alloc2>& re,
match_flag_type flags = match_default);
3. 매개 변수
BidIt | 부분 일치에 대한 반복기 형식입니다. string::const_iterator wstring::const_iterator const char* const wchar_t* |
Alloc | 일치 결과 할당자 클래스입니다. |
Elem | 일치 항목을 찾을 요소를 형식입니다. 일반적인 경우는 다음과 같습니다. string char* wstring wchar_t* |
RXtraits | 요소에 대한 특성 클래스입니다. |
Alloc2 | 정규식 할당자 클래스입니다. |
IOtraits | 문자열 특성 클래스입니다. |
IOalloc | 문자열 할당자 클래스입니다. |
flags | 일치에 대한 플래그입니다. |
first | 일치하는 시퀀스의 시작입니다. |
last | 일치하는 시퀀스의 끝입니다. |
match | 일치 결과입니다. Elem형식인 smatch입니다. string 타입은 wsmatch for wstring, cmatch for char*, wcmatch for wchar_t* 입니다. |
ptr | 일치하는 시퀀스의 시작에 대한 포인터입니다. if ptr is char*, then use cmatch and regex. if ptr is then wchar_t* use wcmatch and wregex. |
re | 일치 항목을 찾는 정규식입니다. wregex, char*, string, wstring, regex, wchar_t* |
str | 일치하는 문자열입니다. Elem 형식에 해당합니다. |
4.예제
각 템플릿 함수는 전체 피연산자 시퀀스 str이 정규식 인수 re와 정확하게 일치하는 경우에만 true를 반환합니다.
대상 시퀀스 내의 부분 문자열을 일치시키고 regex_iterator 여러 일치 항목을 찾는 데 사용합니다.
re 는 대상 시퀀스 regex_iterator 내에서 하위 문자열을 일치시키고 일치하는 regex_search를 여러개 찾습니다.
match_result 개체를 가져오는 함수가 match에 성공했는지 여부를 반영하기 위해 멤버를 설정하고, 성공한 경우 정규식의 다양한 캡처 그룹을 캡처합니다.
#include <regex>
#include <iostream>
int main()
{
string target("Drizzle");
regex rx(R"(D\w+e)");
bool found = regex_match(target.begin(), target.end(), rx);
if(found){
cout << "Regex found in Drizzle" << endl;
}
const wchar_t* target3 = L"2014-04-02";
wcmatch wideMatch2;
wregex wrx2(LR"(\d{4}(-|/)\d{2}(-|/)\d{2})");
if (regex_match(target3, wideMatch2, wrx2))
{
wcout << L"Matching text: " << wideMatch2.str() << endl;
}
return 0;
}
Regex found in Drizzle
Matching text: 2014-04-02
728x90