다음을 통해 공유


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);

매개 변수

  • BidIt
    부분 일치에 대한 반복기 형식입니다. 대부분의 경우 이 형식은 string::const_iterator, wstring::const_iterator, const char* 또는 const wchar_t* 중 하나입니다.

  • Alloc
    일치 결과 할당자 클래스입니다.

  • Elem
    매칭할 요소의 형식입니다. 대부분의 경우 이 형식은 string, wstring, char* 또는 wchar_t*입니다.

  • RXtraits
    요소에 대한 특성 클래스입니다.

  • Alloc2
    정규식 할당자 클래스입니다.

  • IOtraits
    문자열 특성 클래스입니다.

  • IOalloc
    문자열 할당자 클래스입니다.

  • flags
    일치에 대한 플래그입니다.

  • first
    일치하는 시퀀스의 시작입니다.

  • last
    일치하는 시퀀스의 끝입니다.

  • match
    일치 결과입니다. 다음과 같이 Elem 형식에 해당합니다. string의 경우 smatch, wstring의 경우 wsmatch, char*의 경우 cmatch 또는 wchar_t*의 경우 wcmatch입니다.

  • ptr
    일치하는 시퀀스의 시작에 대한 포인터입니다. ptr이 char*이면 cmatch 및 regex를 사용합니다. ptr이 wchar_t*이면 wcmatch 및 wregex를 사용합니다.

  • re
    일치하는 정규식입니다. string 및 char*의 경우 regex 형식이고 wstring 및 wchar_t*의 경우 wregex 형식입니다.

  • str
    일치하는 문자열입니다. Elem 형식에 해당합니다.

설명

각 템플릿 함수는 전체 피연산자 시퀀스 str이 정규식 인수 re와 정확하게 일치하는 경우에만 true를 반환합니다. 여러 일치 항목을 찾으려면 regex_search를 사용하여 대상 시퀀스 내의 하위 문자열과 regex_iterator를 일치시킵니다.

match_results 개체를 사용하는 함수는 일치에 성공했는지 여부를 반영하도록 멤버를 설정하고 그런 경우 정규식의 다양한 캡처 그룹이 캡처됩니다.

(1):

예제

 

// RegexTestBed.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <regex> 
#include <iostream> 

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
   
    // (1) with char*
    // Note how const char* requires cmatch and regex
    const char *first = "abc";
    const char *last = first + strlen(first);
    cmatch narrowMatch;
    regex rx("a(b?)c");

    bool found = regex_match(first, last, narrowMatch, rx);

    // (1) with std::wstring
    // Note how wstring requires wsmatch and wregex.
    // Note use of const iterators cbegin() and cend().
    wstring target(L"Hello");
    wsmatch wideMatch;
    wregex wrx(L"He(l+)o");

    if (regex_match(target.cbegin(), target.cend(), wideMatch, wrx))
        wcout << L"The matching text is:" << wideMatch.str() << endl; 

    // (2) with std::string
    string target2("Drizzle");
    regex rx2(R"(D\w+e)"); // no double backslashes with raw string literal
    found = regex_match(target2.cbegin(), target2.cend(), rx2);

    // (3) with wchar_t*
    const wchar_t* target3 = L"2014-04-02";
    wcmatch wideMatch2;

    // LR"(...)" is a  raw wide-string literal. Open and close parens
    // are delimiters, not string elements.
    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>

네임스페이스: std

참고 항목

참조

<regex>

regex_replace 함수

regex_search 함수