std::match_results::operator[]

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

参阅

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

版本历史

  • (当前 | 先前 2017年9月29日 (五) 01:17Fruderica讨论 | 贡献. . (1,634字节) (-52). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 18:06P12bot讨论 | 贡献 . . (1,686字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:12P12bot讨论 | 贡献 . . (1,686字节) (-51). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 12:14P12bot讨论 | 贡献 . . (1,737字节) (+345). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 09:07P12讨论 | 贡献 . . (1,392字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,392字节) (-46). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 15:33P12讨论 | 贡献 . . (1,438字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,438字节) (+1,438). . (Translated from the English version using Google Translate)