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

参见

返回特定子匹配的字符序列
(公开成员函数)