Namespaces
Variants

std:: sub_match

From cppreference.net
Regular expressions library
Classes
sub_match
(C++11)
Algorithms
Iterators
Exceptions
Traits
Constants
(C++11)
Regex Grammar
ヘッダーで定義 <regex>
template < class BidirIt >
class sub_match ;
(C++11以降)

クラステンプレート std::sub_match は、正規表現エンジンによってマークされた部分式に一致する文字シーケンスを表すために使用されます。 マッチは、正規表現に一致するターゲット範囲内の [ begin , end ) のペアですが、コードの明確性を高めるための追加のオブザーバ関数を備えています。

デフォルトコンストラクタのみが公開でアクセス可能です。 std::sub_match のインスタンスは通常、 std::match_results コンテナの一部として構築され、正規表現アルゴリズムの処理中に値が設定されます。

matched メンバーが true でない限り、メンバー関数は定義済みのデフォルト値を返します。

std::sub_match std:: pair < BidirIt, BidirIt > から継承していますが、代入などのメンバ関数が期待通りに動作しないため、 std::pair オブジェクトとして扱うことはできません。

目次

型要件

-
BidirIt LegacyBidirectionalIterator の要件を満たさなければならない。

特殊化

一般的な文字シーケンス型に対するいくつかの特殊化が提供されています:

定義済みヘッダ <regex>
定義
std::csub_match std :: sub_match < const char * >
std::wcsub_match std :: sub_match < const wchar_t * >
std::ssub_match std :: sub_match < std :: string :: const_iterator >
std::wssub_match std :: sub_match < std :: wstring :: const_iterator >

ネスト型

定義
iterator BidirIt
value_type std:: iterator_traits < BidirIt > :: value_type
difference_type std:: iterator_traits < BidirIt > :: difference_type
string_type std:: basic_string < value_type >

データメンバ

メンバー 説明
bool matched
このマッチが成功したかどうか
(公開メンバーオブジェクト)

std::pairから継承 std:: pair

BidirIt first
マッチシーケンスの開始位置
(公開メンバオブジェクト)
BidirIt second
マッチシーケンスの終了位置の次
(公開メンバオブジェクト)

メンバー関数

マッチオブジェクトを構築する
(公開メンバ関数)
オブザーバ
マッチの長さを返す(存在する場合)
(公開メンバ関数)
基となる文字列型に変換する
(公開メンバ関数)
マッチした部分シーケンスを比較する(存在する場合)
(公開メンバ関数)
モディファイア
内容を交換する
(公開メンバ関数)

非メンバー関数

(C++20で削除) (C++20で削除) (C++20で削除) (C++20で削除) (C++20で削除) (C++20)
sub_match を他の sub_match 、文字列、または文字と比較する
(関数テンプレート)
マッチした文字シーケンスを出力する
(関数テンプレート)

#include <cassert>
#include <iostream>
#include <regex>
#include <string>
int main()
{
    std::string sentence{"Friday the thirteenth."};
    const std::regex re{"([A-z]+) ([a-z]+) ([a-z]+)"};
    std::smatch words;
    std::regex_search(sentence, words, re);
    std::cout << std::boolalpha;
    for (const auto& m : words)
    {
        assert(m.matched);
        std::cout << "m: [" << m << "], m.length(): " << m.length() << ", "
                     "*m.first: '" << *m.first << "', "
                     "*m.second: '" << *m.second << "'\n";
    }
}

出力:

m: [Friday the thirteenth], m.length(): 21, *m.first: 'F', *m.second: '.'
m: [Friday], m.length(): 6, *m.first: 'F', *m.second: ' '
m: [the], m.length(): 3, *m.first: 't', *m.second: ' '
m: [thirteenth], m.length(): 10, *m.first: 't', *m.second: '.'

関連項目

指定された文字列内のすべての正規表現マッチにおける特定の部分式、またはマッチしなかった部分文字列を反復処理する
(クラステンプレート)