Namespaces
Variants

std:: regex_match

From cppreference.net
헤더 파일에 정의됨 <regex>
template < class BidirIt, class Alloc, class CharT, class Traits >

bool regex_match ( BidirIt first, BidirIt last,
std:: match_results < BidirIt, Alloc > & m,
const std:: basic_regex < CharT, Traits > & e,
std:: regex_constants :: match_flag_type flags =

std:: regex_constants :: match_default ) ;
(1) (C++11 이후)
template < class BidirIt, class CharT, class Traits >

bool regex_match ( BidirIt first, BidirIt last,
const std:: basic_regex < CharT, Traits > & e,
std:: regex_constants :: match_flag_type flags =

std:: regex_constants :: match_default ) ;
(2) (C++11 이후)
template < class CharT, class Alloc, class Traits >

bool regex_match ( const CharT * str,
std:: match_results < const CharT * , Alloc > & m,
const std:: basic_regex < CharT, Traits > & e,
std:: regex_constants :: match_flag_type flags =

std:: regex_constants :: match_default ) ;
(3) (C++11 이후)
template < class CharT, class Traits >

bool regex_match ( const CharT * str, const std:: basic_regex < CharT, Traits > & e,
std:: regex_constants :: match_flag_type flags =

std:: regex_constants :: match_default ) ;
(4) (C++11 이후)
template < class STraits, class SAlloc, class Alloc,

class CharT, class Traits >
bool regex_match
( const std:: basic_string < CharT, STraits, SAlloc > & s,
std:: match_results
< typename std:: basic_string < CharT, STraits, SAlloc > :: const_iterator ,
Alloc > & m,
const std:: basic_regex < CharT, Traits > & e,
std:: regex_constants :: match_flag_type flags =

std:: regex_constants :: match_default ) ;
(5) (C++11 이후)
template < class STraits, class SAlloc, class CharT, class Traits >

bool regex_match ( const std:: basic_string < CharT, STraits, SAlloc > & s,
const std:: basic_regex < CharT, Traits > & e,
std:: regex_constants :: match_flag_type flags =

std:: regex_constants :: match_default ) ;
(6) (C++11 이후)
template < class STraits, class SAlloc, class Alloc,

class CharT, class Traits >
bool regex_match
( const std:: basic_string < CharT, STraits, SAlloc > && ,
std:: match_results
< typename std:: basic_string < CharT, STraits, SAlloc > :: const_iterator ,
Alloc > & ,
const std:: basic_regex < CharT, Traits > & ,
std:: regex_constants :: match_flag_type flags =

std:: regex_constants :: match_default ) = delete ;
(7) (C++11 이후)

정규 표현식 e 가 대상 문자 시퀀스 전체와 일치하는지 판단합니다. 상세한 일치 결과는 m 에 저장됩니다(존재하는 경우).

1,2) 대상 문자 시퀀스는 범위 [ first , last ) 로 표현됩니다.

만약 BidirIt LegacyBidirectionalIterator 의 요구 사항을 만족하지 않으면, 동작은 정의되지 않습니다.

(C++23 이전)

만약 BidirIt bidirectional_iterator 를 모델로 하지 않으면, 동작은 정의되지 않습니다.

(C++23 이후)
3,4) 대상 문자 시퀀스는 범위 [ str , str + std:: char_traits < CharT > :: length ( str ) ) 로 표현됩니다.
5,6) 대상 문자 시퀀스는 문자열 s 로 표현됩니다.
7) 대상 문자 시퀀스가 std::string rvalue로 표현될 수 없습니다.

매치가 존재하지 않는 경우, 다음 표현식들에서 m (존재할 경우)는 지정된 값들을 반환해야 합니다:

표현식
m. ready ( ) true
m. size ( ) 0
m. empty ( ) true

매치가 존재하는 경우, ( 0 , m. size ( ) ) 범위 내의 임의의 정수를 n 으로 지정할 때, 아래 나열된 각 오버로드에 대해 m 을 포함하는 다음 표현식들은 지정된 값을 반환해야 합니다:

**번역 결과:** **번역 설명:** - `last` → `마지막`으로 번역 - C++ 코드 내부(` ` 태그 내부)의 텍스트는 번역하지 않음 - HTML 태그와 속성은 원본 그대로 유지 - ` `, `
`, `` 태그가 없으므로 해당 사항 없음
- C++ 관련 용어(`std::char_traits`, `length`, `end` 등)는 번역하지 않음
Expression Value
오버로드 (1) 오버로드 (3) 오버로드 (5)
m. ready ( ) true
m. size ( ) 1 + e. mark_count ( )
m. empty ( ) false
m. prefix ( ) . first first str s. begin ( )
m. prefix ( ) . second
m. prefix ( ) . matched false [1]
m. suffix ( ) . first last std:: char_traits < CharT > ::
length ( str ) + str
s. end ( )
m. suffix ( ) . second
m. suffix ( ) . matched false [2]
m [0] . first first str s. begin ( )
m [0] . second last std:: char_traits < CharT > ::
length ( str ) + str
s. end ( )
m [0] . second 마지막 std:: char_traits < CharT > ::
length ( str ) + str
s. end ( )
m [0] . matched true [3]
m [n] . first
  • last 만약 표시된 부분 표현식 n 이 매치에 참여하지 않은 경우
  • 그렇지 않으면 부분 표현식 n 과 매치되는 시퀀스의 시작
m [n] . second
  • last 만약 표시된 부분 표현식 n 이 매치에 참여하지 않은 경우
  • 그렇지 않으면 부분 표현식 n 과 매치되는 시퀀스의 끝
m [n] . matched
  1. 매치 접두사가 비어 있습니다.
  2. 매치 접미사가 비어 있습니다.
  3. 전체 시퀀스가 매치되었습니다.

목차

매개변수

first, last - 대상 문자 범위
str - 대상 null-terminated C-style 문자열
s - 대상 std::basic_string
m - 매치 결과
e - 정규 표현식
flags - 매치 수행 방식을 결정하는 데 사용되는 플래그

반환값

전체 대상 시퀀스가 e 와 일치하면 true 를 반환하고, 그렇지 않으면 false 를 반환합니다.

참고 사항

regex_match 는 완전한 일치만 고려하기 때문에, 동일한 정규 표현식이 regex_match std::regex_search 사이에서 서로 다른 일치 결과를 반환할 수 있습니다:

std::regex re("Get|GetValue");
std::cmatch m;
std::regex_search("GetValue", m, re);  // true를 반환하고, m[0]은 "Get"을 포함
std::regex_match ("GetValue", m, re);  // true를 반환하고, m[0]은 "GetValue"를 포함
std::regex_search("GetValues", m, re); // true를 반환하고, m[0]은 "Get"을 포함
std::regex_match ("GetValues", m, re); // false를 반환

예제

#include <cstddef>
#include <iostream>
#include <regex>
#include <string>
int main()
{
    // 단순 정규식 매칭
    const std::string fnames[] = {"foo.txt", "bar.txt", "baz.dat", "zoidberg"};
    const std::regex txt_regex("[a-z]+\\.txt");
    for (const auto& fname : fnames)
        std::cout << fname << ": " << std::regex_match(fname, txt_regex) << '\n';
    // 부분 매치 추출
    const std::regex base_regex("([a-z]+)\\.txt");
    std::smatch base_match;
    for (const auto& fname : fnames)
        if (std::regex_match(fname, base_match, base_regex))
            // 첫 번째 sub_match는 전체 문자열입니다; 다음
            // sub_match는 첫 번째 괄호로 묶인 표현식입니다.
            if (base_match.size() == 2)
            {
                std::ssub_match base_sub_match = base_match[1];
                std::string base = base_sub_match.str();
                std::cout << fname << " has a base of " << base << '\n';
            }
    // 여러 부분 매치 추출
    const std::regex pieces_regex("([a-z]+)\\.([a-z]+)");
    std::smatch pieces_match;
    for (const auto& fname : fnames)
        if (std::regex_match(fname, pieces_match, pieces_regex))
        {
            std::cout << fname << '\n';
            for (std::size_t i = 0; i < pieces_match.size(); ++i)
            {
                std::ssub_match sub_match = pieces_match[i];
                std::string piece = sub_match.str();
                std::cout << "  submatch " << i << ": " << piece << '\n';
            }
        }
}

출력:

foo.txt: 1
bar.txt: 1
baz.dat: 0
zoidberg: 0
foo.txt has a base of foo
bar.txt has a base of bar
foo.txt
  submatch 0: foo.txt
  submatch 1: foo
  submatch 2: txt
bar.txt
  submatch 0: bar.txt
  submatch 1: bar
  submatch 2: txt
baz.dat
  submatch 0: baz.dat
  submatch 1: baz
  submatch 2: dat

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2205 C++11 n 사후 조건에서 0일 수 있었음 양수만 가능함
LWG 2273 C++11 부분 일치가 고려되는지 불명확했음 완전 일치만 고려함
LWG 2329 C++11 오버로드 (5) basic_string rvalue를 수용했으며,
이로 인해 댕글링 반복자가 발생할 수 있었음
삭제된 오버로드 (7) 를 통해 거부됨

참고 항목

정규 표현식 객체
(클래스 템플릿)
단일 정규 표현식 매치와 모든 하위 표현식 매치들을 식별함
(클래스 템플릿)
문자 시퀀스의 임의 부분에 정규 표현식 매칭을 시도함
(함수 템플릿)