名前空間
変種
操作

std::regex_token_iterator

提供: cppreference.com
< cpp‎ | regex
ヘッダ <regex> で定義
template<

    class BidirIt,
    class CharT =typenamestd::iterator_traits<BidirIt>::value_type,
    class Traits =std::regex_traits<CharT>

>class regex_token_iterator
(C++11以上)

std::regex_token_iterator は、ベースとなる文字シーケンス内の、正規表現のすべてのマッチの個々の部分マッチにアクセスする、読み込み専用の LegacyForwardIterator です。 また (トークナイザのように) シーケンスの指定された正規表現がマッチしなかった部分にアクセスするために使うこともできます。

構築時に std::regex_iterator を構築し、毎回のインクリメント時に要求される部分マッチを現在の match_results から辿り、最後の部分マッチをインクリメントし終わるとベースとなる regex_iterator をインクリメントします。

デフォルト構築された std::regex_token_iterator は終端イテレータです。 有効な std::regex_token_iterator が最後のマッチの最後のサブマッチに達した後にインクリメントされると、終端イテレータと等しくなります。 終端イテレータを逆参照したりさらにインクリメントすると未定義動作を発生させます。

要求される部分マッチのインデックスのリストにインデックス -1 (マッチしない部分) がある場合、 std::regex_token_iterator は終端イテレータになる直前に接尾辞イテレータになることがあります。 そのようなイテレータは、逆参照されると、最後のマッチとシーケンスの終端の間の文字シーケンスに対応する match_results を返します。

std::regex_token_iterator の一般的な実装はベースとなる std::regex_iterator、要求される部分マッチのコンテナ (std::vector<int> など)、部分マッチのインデックスと等しい内部カウンタ、 現在のマッチの現在のサブマッチを指す std::sub_match へのポインタ、および (トークナイザモードで使用される) 最後のマッチしなかった文字シーケンスを格納する std::match_results オブジェクトを保持します。

目次

[編集]型要件

-
BidirItLegacyBidirectionalIterator の要件を満たさなければなりません。

[編集]特殊化

一般的な文字シーケンス型のためにいくつかの特殊化が定義されます。

ヘッダ <regex> で定義
定義
cregex_token_iteratorregex_token_iterator<constchar*>
wcregex_token_iteratorregex_token_iterator<constwchar_t*>
sregex_token_iteratorregex_token_iterator<std::string::const_iterator>
wsregex_token_iteratorregex_token_iterator<std::wstring::const_iterator>

[編集]メンバ型

メンバ型 定義
value_typestd::sub_match<BidirIt>
difference_typestd::ptrdiff_t
pointerconst value_type*
referenceconst value_type&
iterator_categorystd::forward_iterator_tag
regex_typebasic_regex<CharT, Traits>

[編集]メンバ関数

新しい regex_token_iterator を構築します
(パブリックメンバ関数)[edit]
デストラクタ
(暗黙に宣言)
キャッシュされている値を含め、 regex_token_iterator を破棄します
(パブリックメンバ関数)[edit]
内容を代入します
(パブリックメンバ関数)[edit]
(C++20で削除)
2つの regex_token_iterator を比較します
(パブリックメンバ関数)[edit]
現在の部分マッチにアクセスします
(パブリックメンバ関数)[edit]
イテレータを次の部分マッチに進めます
(パブリックメンバ関数)[edit]

[編集]ノート

イテレータのコンストラクタに渡された std::basic_regex オブジェクトがイテレータより長生きすることを保証するのはプログラマの責任です。 イテレータは regex を指すポインタを格納する std::regex_iterator を格納するため、 regex が破棄された後でイテレータをインクリメントすると未定義動作になります。

[編集]

#include <fstream>#include <iostream>#include <algorithm>#include <iterator>#include <regex>   int main(){std::string text ="Quick brown fox.";// tokenization (non-matched fragments)// Note that regex is matched only two times: when the third value is obtained// the iterator is a suffix iterator.std::regex ws_re("\\s+");// whitespacestd::copy( std::sregex_token_iterator(text.begin(), text.end(), ws_re, -1), std::sregex_token_iterator(), std::ostream_iterator<std::string>(std::cout, "\n"));   // iterating the first submatchesstd::string html ="<p><a href=\"http://google.com\">google</a> ""< a HREF =\"http://cppreference.com\">cppreference</a>\n</p>";std::regex url_re("<\\s*A\\s+[^>]*href\\s*=\\s*\"([^\"]*)\"", std::regex::icase);std::copy( std::sregex_token_iterator(html.begin(), html.end(), url_re, 1), std::sregex_token_iterator(), std::ostream_iterator<std::string>(std::cout, "\n"));}

出力:

Quick brown fox. http://google.com http://cppreference.com
close