最終更新日時(UTC):
が更新

履歴 編集

function
<regex>

std::match_results::operator[](C++11)

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;
  }
}

出力

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

処理系