- regex[meta header]
- std[meta namespace]
- class template[meta id-type]
- cpp11[meta cpp]
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>;
}
- pair[link ../utility/pair.md]
クラステンプレート sub_match
は、文字列が正規表現にマッチした際に、その正規表現がマッチした範囲、あるいは、その正規表現内のあるグループがマッチした範囲を表す。
sub_match
は pair
から public 継承している。
注:上記の他、オブジェクトの内容を交換するメンバ関数 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 |
注:非メンバ関数版の swap
は sub_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;
}
}
- std::ssub_match[color ff0000]
- std::regex[link basic_regex.md]
- sub.length()[link sub_match/length.md]
- sub.str()[link sub_match/str.md]
- std::regex_search[link regex_search.md]
- std::smatch[link match_results.md]
0:range = [1, 5), length() = 4, str() = 'abc '
1:not participate
2:range = [1, 4), length() = 3, str() = 'abc'
- Clang: 3.0 [mark verified], 3.1 [mark verified], 3.2 [mark verified], 3.3 [mark verified], 3.4 [mark verified], 3.5 [mark verified], 3.6 [mark verified]
- GCC: 4.9.0 [mark verified], 4.9.1 [mark verified], 5.0.0 [mark verified]
- ICC: ??
- Visual C++: ??