命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | regex‎ | match results
 
 
 
正则表达式库
(C++11)
算法
迭代器
异常
特性
常量
(C++11)
正则表达式语法
 
 
const_reference operator[]( size_type n ) const;
(自 C++11 起)

如果 n > 0n < size(),则返回对 std::sub_match 的引用,它表示目标序列中被第 n捕获的标记子表达式匹配的部分。

如果 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

[编辑] 参见

返回特定子匹配的字符序列
(公共成员函数) [编辑]