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

履歴 編集

class template
<regex>

std::sub_match(C++11)

namespace std {
  template <class BidirectionalIterator>
  class sub_match : public pair<BidirectionalIterator, BidirectionalIterator>;

  using csub_match  = sub_match<const char*>;
  using wcsub_match = sub_match<const wchar_t*>;
  using ssub_match  = sub_match<string::const_iterator>;
  using wssub_match = sub_match<wstring::const_iterator>;
}

概要

クラステンプレート sub_match は、文字列が正規表現にマッチした際に、その正規表現がマッチした範囲、あるいは、その正規表現内のあるグループがマッチした範囲を表す。 sub_matchpair から public 継承している。

メンバ関数

名前 説明 対応バージョン
(constructor) コンストラクタ C++11
length マッチした文字列の長さを返す。 C++11
operator string_type マッチした文字列を string_type 型にキャストする。 C++11
str マッチした文字列を string_type 型で返す。キャストと同じ。 C++11
compare マッチした文字列を比較する。 C++11

注:上記の他、オブジェクトの内容を交換するメンバ関数 swap もあるが、pair から継承したものであるため、メンバ変数 matched の内容は交換しないため注意が必要。

メンバ変数

名前 説明 対応バージョン
first BidirectionalIterator このサブマッチがマッチに参加している場合、当該サブマッチの開始位置を表す C++11
second BidirectionalIterator このサブマッチがマッチに参加している場合、当該サブマッチの終了位置の一つ先を表す C++11
matched bool このサブマッチがマッチに参加しているか否かを表す C++11

メンバ型

名前 説明 対応バージョン
value_type 検索対象の文字の型。iterator_traits<BidirectionalIterator>::value_type の別名 C++11
difference_type 2 つのイテレータの差を表すための型。iterator_traits<BidirectionalIterator>::difference_type の別名 C++11
iterator 検索対象の文字を指すイテレータの型。BidirectionalIterator の別名 C++11
string_type 検索対象の文字の型に対応する文字列型。basic_string<value_type> の別名 C++11
first_type メンバ変数 first の型。pair から継承。BidirectionalIterator の別名 C++11
second_type メンバ変数 second の型。pair から継承。BidirectionalIterator の別名 C++11

非メンバ関数

名前 説明 対応バージョン
operator== 等値比較を行う C++11
operator!= 非等値比較を行う C++11
operator<=> 三方比較を行う C++20
operator< 左辺が右辺よりも小さいか判定を行う C++11
operator<= 左辺が右辺以下か判定を行う C++11
operator> 左辺が右辺より大きいか判定を行う C++11
operator>= 左辺が右辺以上か判定を行う C++11
operator<< ストリームへの出力を行う C++11

注:非メンバ関数版の swapsub_match 用にオーバーロードされていないため、pair から継承したメンバ関数 swap は呼び出さずに汎用版の動作となる(一時変数を利用したムーブでオブジェクトの内容を交換する)。従って、こちらはメンバ変数 matched も交換される。

非メンバ型

名前 説明 対応バージョン
csub_match sub_match<const char*> の別名 C++11
wcsub_match sub_match<const wchar_t*> の別名 C++11
ssub_match sub_match<string::const_iterator> の別名 C++11
wssub_match sub_match<wstring::const_iterator> の別名 C++11

#include <iostream>
#include <iterator>
#include <regex>
#include <string>

int main()
{
  const std::string s(" abc 123 def ");
  const std::regex re(R"((?:(\d+)|(\w+))\s+)");
  std::smatch m;
  if (std::regex_search(s, m, re)) {
    for (int i = 0, n = m.size(); i < n; i++) {
      std::ssub_match sub = m[i];
      if (sub.matched) {
        std::cout << i << ":range = [" << (sub.first - std::begin(s)) << ", " << (sub.second - std::begin(s)) << "), "
                     "length() = " << sub.length() << ", str() = '" << sub.str() << '\'' << std::endl;
      } else {
        std::cout << i << ":not participate" << std::endl;
      }
    }
  } else {
    std::cout << "not match" << std::endl;
  }
}

出力

0:range = [1, 5), length() = 4, str() = 'abc '
1:not participate
2:range = [1, 4), length() = 3, str() = 'abc'

バージョン

言語

  • C++11

処理系