Namespaces
Variants

std:: sub_match

From cppreference.net
Regular expressions library
Classes
sub_match
(C++11)
Algorithms
Iterators
Exceptions
Traits
Constants
(C++11)
Regex Grammar
헤더 파일에 정의됨 <regex>
template < class BidirIt >
class sub_match ;
(C++11부터)

클래스 템플릿 std::sub_match 는 정규 표현식 엔진에 의해 표시된 부분 표현식으로 매칭된 문자 시퀀스를 나타내기 위해 사용됩니다. 매치는 정규 표현식으로 매칭된 대상 범위 내의 [ begin , end ) 쌍이지만, 코드 명확성을 향상시키기 위한 추가 관찰자 함수들을 가지고 있습니다.

기본 생성자만 공개적으로 접근 가능합니다. std::sub_match 인스턴스는 일반적으로 정규식 알고리즘 중 하나를 처리하는 동안 std::match_results 컨테이너의 일부로 생성되고 채워집니다.

멤버 함수들은 matched 멤버가 true 가 아닌 경우 정의된 기본값들을 반환합니다.

std::sub_match std:: pair < BidirIt, BidirIt > 로부터 상속받지만, 대입과 같은 멤버 함수들이 예상대로 동작하지 않기 때문에 std::pair 객체처럼 취급할 수 없습니다.

목차

타입 요구사항

-
BidirIt LegacyBidirectionalIterator 요구사항을 충족해야 합니다.

특수화

일반적인 문자 시퀀스 타입에 대한 여러 특수화가 제공됩니다:

헤더 파일에 정의됨 <regex>
유형 정의
std::csub_match std :: sub_match < const char * >
std::wcsub_match std :: sub_match < const wchar_t * >
std::ssub_match std :: sub_match < std :: string :: const_iterator >
std::wssub_match std :: sub_match < std :: wstring :: const_iterator >

중첩 타입

유형 정의
iterator BidirIt
value_type std:: iterator_traits < BidirIt > :: value_type
difference_type std:: iterator_traits < BidirIt > :: difference_type
string_type std:: basic_string < value_type >

데이터 멤버

멤버 설명
bool matched
이 매치가 성공했는지 여부
(공개 멤버 객체)

std:: pair 로부터 상속됨

BidirIt first
매치 시퀀스의 시작
(public member object)
BidirIt second
매치 시퀀스의 끝 바로 다음 위치
(public member object)

멤버 함수

매치 객체를 생성합니다
(public member function)
Observers
매치의 길이를 반환합니다 (존재하는 경우)
(public member function)
기본 문자열 타입으로 변환합니다
(public member function)
매치된 부분 시퀀스를 비교합니다 (존재하는 경우)
(public member function)
Modifiers
내용을 교환합니다
(public member function)

비멤버 함수

(C++20에서 제거됨) (C++20에서 제거됨) (C++20에서 제거됨) (C++20에서 제거됨) (C++20에서 제거됨) (C++20)
sub_match 를 다른 sub_match , 문자열 또는 문자와 비교
(함수 템플릿)
일치하는 문자 시퀀스를 출력
(함수 템플릿)

예제

#include <cassert>
#include <iostream>
#include <regex>
#include <string>
int main()
{
    std::string sentence{"Friday the thirteenth."};
    const std::regex re{"([A-z]+) ([a-z]+) ([a-z]+)"};
    std::smatch words;
    std::regex_search(sentence, words, re);
    std::cout << std::boolalpha;
    for (const auto& m : words)
    {
        assert(m.matched);
        std::cout << "m: [" << m << "], m.length(): " << m.length() << ", "
                     "*m.first: '" << *m.first << "', "
                     "*m.second: '" << *m.second << "'\n";
    }
}

출력:

m: [Friday the thirteenth], m.length(): 21, *m.first: 'F', *m.second: '.'
m: [Friday], m.length(): 6, *m.first: 'F', *m.second: ' '
m: [the], m.length(): 3, *m.first: 't', *m.second: ' '
m: [thirteenth], m.length(): 10, *m.first: 't', *m.second: '.'

참고 항목

주어진 문자열 내의 모든 정규식 매치에서 지정된 부분 표현식을 순회하거나 매치되지 않은 부분 문자열을 순회합니다
(클래스 템플릿)