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 への参照を返します。

if 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

関連項目

特定の部分マッチに対する文字シーケンスを返す
(公開メンバ関数)