Skip to content

Latest commit

 

History

History
79 lines (63 loc) · 2.51 KB

op_at.md

File metadata and controls

79 lines (63 loc) · 2.51 KB

operator[]

  • regex[meta header]
  • std[meta namespace]
  • match_results[meta class]
  • function[meta id-type]
  • cpp11[meta cpp]
const_reference operator[](size_type n) const;

概要

指定されたサブマッチを返す。

要件

ready() == true

戻り値

n 番目のキャプチャグループ(正規表現内のカッコで囲まれた部分)に対応する sub_match オブジェクトへの参照。
n == 0 の場合、マッチした文字列全体に対応する sub_match オブジェクトへの参照を返す。
n >= size() の場合、マッチしていないことを表す sub_match オブジェクト(備考参照)への参照を返す。

備考

マッチしていないことを表す sub_match オブジェクトとは、firstsecond が検索対象文字列の末尾を指し、match == false であるようなオブジェクトである。

#include <iostream>
#include <regex>

int main()
{
  const char s[] = " abc 0123 defgh ";
  const std::regex re("(\\w+) (\\d+) (?:(\\d+)|(\\w+))");

  std::cmatch m;
  std::cout << std::boolalpha;
  if (std::regex_search(s, m, re)) {
    for (std::size_t i = 0, n = m.size(); i < n; ++i) {
      const std::csub_match& sub = m[i];
      std::cout << i << ":matched = " << sub.matched << ", str() = '" << sub.str() << "', length() = " << sub.length() << std::endl;
    }
  } else {
    std::cout << "not match" << std::endl;
  }
}
  • m[i][color ff0000]
  • std::regex[link ../basic_regex.md]
  • std::cmatch[link ../match_results.md]
  • std::regex_search[link ../regex_search.md]
  • m.size()[link size.md]
  • std::csub_match[link ../sub_match.md]
  • sub.str()[link ../sub_match/str.md]
  • sub.length()[link ../sub_match/length.md]

出力

0:matched = true, str() = 'abc 0123 defgh', length() = 14
1:matched = true, str() = 'abc', length() = 3
2:matched = true, str() = '0123', length() = 4
3:matched = false, str() = '', length() = 0
4:matched = true, str() = 'defgh', length() = 5

バージョン

言語

  • C++11

処理系

  • Clang: 3.0 [mark verified], 3.1 [mark verified], 3.2 [mark verified], 3.3 [mark verified], 3.4 [mark verified], 3.5 [mark verified], 3.6 [mark verified]
  • GCC: 4.9.0 [mark verified], 4.9.1 [mark verified], 5.0.0 [mark verified]
  • ICC: ??
  • Visual C++: ??