regex_sub_match

regex_sub_match (부분 일치를 나타내는 클래스 템플릿)

std::sub_match 클래스 템플릿은 정규 표현식 엔진이 표시된 하위 표현식(marked sub-expressions)과 일치하는 문자 시퀀스를 나타내는 데 사용돼요. 이 타입은 일치된 부분의 시작과 끝을 가리키는 반복자 쌍을 보관하면서, 길이나 문자열 변환 같은 편의 기능도 제공해요. 기본 생성자 외에는 공개적으로 접근할 수 없고, 주로 std::match_results 컨테이너 안에서 생성되고 채워져요.

출처: cppreference

본문

헤더에 정의됨
template < class BidirIt > class sub_match ; (since C++11)

std::sub_match 클래스 템플릿은 정규 표현식 엔진이 표시된 하위 표현식(marked sub-expressions)과 일치하는 문자 시퀀스를 나타내는 데 사용해요. 일치(match)는 정규 표현식과 일치하는 대상 범위 내의 [begin, end) 쌍이지만, 코드 명확성을 높이기 위한 추가 관찰자 함수(observer functions)가 있어요.

기본 생성자만 공개적으로 접근할 수 있어요. std::sub_match 인스턴스는 일반적으로 정규 표현식 알고리즘 중 하나를 처리하는 동안 std::match_results 컨테이너의 일부로 생성되고 채워져요.

멤버 함수는 matched 멤버가 true가 아니면 정의된 기본값을 반환해요.

std::sub_match는 std::pair<BidirIt, BidirIt>에서 상속하지만, 할당(assignment) 같은 멤버 함수가 예상대로 작동하지 않기 때문에 std::pair 객체로 취급할 수 없어요.

Type requirements

- BidirIt은 LegacyBidirectionalIterator의 요구 사항을 충족해야 해요.

Specializations

일반적인 문자 시퀀스 타입을 위한 몇 가지 특수화가 제공돼요:

헤더에 정의됨
Type
std::csub_match
std::wcsub_match
std::ssub_match
std::wssub_match

Nested types

Type Definition
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 >

Data members

Member Description
matched 이 일치가 성공했는지 여부 (공개 멤버 객체)

Inherited from std:: pair

BidirIt first 일치 시퀀스의 시작 (공개 멤버 객체)
BidirIt second 일치 시퀀스의 끝-다음(one-past-the-end) (공개 멤버 객체)

Member functions

(constructor) 일치 객체를 생성해요 (공개 멤버 함수) [edit]
Observers
length 일치의 길이를 반환해요 (있는 경우) (공개 멤버 함수) [edit]
str operator string_type 기본 문자열 타입으로 변환해요 (공개 멤버 함수) [edit]
compare 일치된 부분 시퀀스를 비교해요 (있는 경우) (공개 멤버 함수) [edit]
Modifiers
swap 내용을 교환해요 (공개 멤버 함수) [edit]

Non-member functions

operator== operator!= operator< operator<= operator> operator>= operator<=> (removed in C++20) (removed in C++20) (removed in C++20) (removed in C++20) (removed in C++20) (C++20) sub_match를 다른 sub_match, 문자열 또는 문자와 비교해요 (함수 템플릿) [edit]
operator<< 일치하는 문자 부분 시퀀스를 출력해요 (함수 템플릿) [edit]

Example

#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";
    }
}

Output:

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: '.'

See also

regex_token_iterator (C++11) 주어진 문자열의 모든 정규 표현식 일치 내에서 지정된 하위 표현식을 반복하거나 일치하지 않는 부분 문자열을 반복해요 (클래스 템플릿) [edit]

더 알아보기 (Learn more)

cppreference