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

履歴 編集

function
<regex>

std::match_results::begin(C++11)

const_iterator begin() const;

概要

*this 内の全てのサブマッチを列挙するため、先頭のサブマッチを指すイテレータを返す。

戻り値

先頭のサブマッチを指すイテレータ

備考

  • 「先頭のサブマッチ」は、正規表現にマッチした文字列全体を指す。
  • 本メンバ関数で返されるイテレータも、読み取り専用イテレータ(const_iterator)である。

#include <iostream>
#include <regex>

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

  std::cmatch m;
  if (std::regex_search(s, m, re)) {
    for (auto it = m.begin(), end = m.end(); it != end; ++it) {
      std::cout << "str() = '" << it->str() << "', "
        "range = [" << it->first - s << ", " << it->second - s << "), "
        "matched = " << std::boolalpha << it->matched << std::endl;
    }
  } else {
    std::cout << "not match" << std::endl;
  }
}

出力

str() = 'abc 0123 defgh', range = [1, 15), matched = true
str() = 'abc', range = [1, 4), matched = true
str() = '0123', range = [5, 9), matched = true
str() = 'defgh', range = [10, 15), matched = true

バージョン

言語

  • C++11

処理系