名前空間
変種
操作

std::sub_match

From cppreference.com
< cpp‎ | regex
 
 
 
正規表現ライブラリ
クラス
sub_match
(C++11)
アルゴリズム
イテレータ
例外
Traits
定数
(C++11)
正規表現文法
 
 
ヘッダ <regex> で定義
template< class BidirIt >
class sub_match;
(C++11以降)

クラステンプレートstd::sub_matchは、正規表現エンジンによって、マークされたサブ式のパターンに一致した文字シーケンスを表すために使用されます。一致は、正規表現によってターゲット範囲内で一致した[beginend)のペアですが、コードの可読性を高めるための追加のオブザーバー関数があります。

デフォルトコンストラクタのみが公開されています。std::sub_matchのインスタンスは、通常、正規表現アルゴリズムのいずれかの処理中にstd::match_resultsコンテナの一部として構築され、ポピュレートされます。

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

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

目次

[編集] 型要件

-
BidirItLegacyBidirectionalIterator の要件を満たしている必要があります。

[編集] 特殊化

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

ヘッダ <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
この一致が成功したかどうか。
(public メンバーオブジェクト)

std::pairから継承

BidirIt first
一致シーケンスの開始。
(public メンバーオブジェクト)
BidirIt second
一致シーケンスの末尾の1つ前。
(public メンバーオブジェクト)

[編集] メンバ関数

一致オブジェクトを構築します。
(public member function) [編集]
監視
一致の長さ(存在する場合)を返します。
(public member function) [編集]
基になる文字列型に変換します。
(public member function) [編集]
一致した部分シーケンス(存在する場合)を比較します。
(public member function) [編集]
変更
内容を交換する
(public member function) [編集]

[編集] 非メンバ関数

(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20)
sub_match を別の sub_match、文字列、または文字と比較する
(関数テンプレート) [edit]
一致した文字部分シーケンスを出力します。
(function template) [編集]

[編集]

#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: '.'

[編集] 関連項目

指定された文字列内のすべての正規表現マッチの中の指定された部分式、またはマッチしなかった部分文字列をイテレートする
(クラステンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)