Namespaces
Variants

std::match_results<BidirIt,Alloc>:: operator[]

From cppreference.net
Regular expressions library
Classes
(C++11)
Algorithms
Iterators
Exceptions
Traits
Constants
(C++11)
Regex Grammar
const_reference operator [ ] ( size_type n ) const ;
(C++11 이후)

만약 n > 0 이고 n < size ( ) 인 경우, 대상 시퀀스에서 n 번째 표시된 부분식 에 의해 매칭된 부분을 나타내는 std::sub_match 에 대한 참조를 반환합니다.

만약 n == 0 이면, 전체 정규 표현식에 매치된 대상 시퀀스의 부분을 나타내는 std::sub_match 에 대한 참조를 반환합니다.

만약 n >= size ( ) 인 경우, 대상 시퀀스의 일치하지 않는 부분 표현(대상 시퀀스의 빈 부분 범위)을 나타내는 std::sub_match 에 대한 참조를 반환합니다.

ready() true 여야 합니다. 그렇지 않으면 동작은 정의되지 않습니다.

목차

매개변수

n - 반환할 매치를 지정하는 정수형 숫자

반환값

대상 시퀀스 내에서 지정된 일치하는 하위 범위를 나타내는 std::sub_match 에 대한 참조입니다.

예제

#include <iostream>
#include <regex>
#include <string>
int main()
{
    std::string target("baaaby");
    std::smatch sm;
    std::regex re1("a(a)*b");
    std::regex_search(target, sm, re1);
    std::cout << "entire match: " << sm[0] << '\n'
              << "submatch #1: " << sm[1] << '\n';
    std::regex re2("a(a*)b");
    std::regex_search(target, sm, re2);
    std::cout << "entire match: " << sm[0] << '\n'
              << "submatch #1: " << sm[1] << '\n';
}

출력:

entire match: aaab
submatch #1: a
entire match: aaab
submatch #1: aa

참고 항목

특정 부분 일치에 대한 문자 시퀀스를 반환합니다
(public member function)