- regex[meta header]
- std[meta namespace]
- class template[meta id-type]
- cpp11[meta cpp]
namespace std {
template <class BidirectionalIterator,
class Allocator = allocator<sub_match<BidirectionalIterator>>>
class match_results;
using cmatch = match_results<const char*>;
using wcmatch = match_results<const wchar_t*>;
using smatch = match_results<string::const_iterator>;
using wsmatch = match_results<wstring::const_iterator>;
namespace pmr { // C++17から
template <class BidirectionalIterator>
using match_results =
std::match_results<BidirectionalIterator,
polymorphic_allocator<sub_match<BidirectionalIterator>>>;
using cmatch = match_results<const char*>;
using wcmatch = match_results<const wchar_t*>;
using smatch = match_results<string::const_iterator>;
using wsmatch = match_results<wstring::const_iterator>;
}
}
- allocator[link ../memory/allocator.md]
- sub_match[link sub_match.md]
- string[link ../string/basic_string.md]
- wstring[link ../string/basic_string.md]
- polymorphic_allocator[link /reference/memory_resource/polymorphic_allocator.md]
match_results
は正規表現によるマッチ結果を格納するコンテナである。コンテナの要素はマッチ結果を表すサブマッチ(sub_match
)である。
コンテナとは言っても regex_match
や regex_search
のマッチ結果を格納することを目的としているため、
一般的なコンテナのように通常の操作でコンテナに要素を格納したり変更したりすることはできない。
構築直後の match_results
オブジェクトは結果を格納していない(ready
() == false
)ため、ほとんどのメンバ関数は使用することができない。
regex_match
や regex_search
に引数として渡されると、マッチが成功したか否かにかかわらずその結果を格納する(ready
() == true
)。
なお、regex_iterator
を間接参照した際の match_results
オブジェクトは常にマッチ結果を格納している(ready
() == true
)。
マッチ結果を格納した match_results
オブジェクトは、マッチが成功した場合には 1 つ以上の要素を格納しているため、empty
() == false
となる。
マッチに成功し empty
() == false
となった match_results
オブジェクトに格納されている各要素(サブマッチ:sub_match
)には、標準シーケンスコンテナの vector
等と同様に operator[]
で直接アクセスすることができるだけでなく、
str
、position
、length
といったメンバ関数で各要素の内容にアクセスすることもできる。
最初の要素には、マッチした文字列全体を表すサブマッチが格納され、以降に各キャプチャグループ(正規表現内のカッコで囲まれた部分に対応する)が続く。
また、マッチした文字列だけでなく、マッチした文字列の前(prefix
)、および、後(suffix
)を指すサブマッチも保持している。
さらに、マッチした結果を用いた書式出力機能も有する(format
)。
match_results
はアロケータ対応コンテナの要件のすべて、および、シーケンスコンテナの要件のうち読み取り専用の操作をサポートしている。
match_results
オブジェクトからメンバ関数で取得できるイテレータについて規格では特に言及されていないが、operator[]
が使用できることから通常ランダムアクセスイテレータであるもの考えても差し支えないものと思われる。
名前 |
説明 |
対応バージョン |
ready |
結果が利用可能か否かを返す |
C++11 |
名前 |
説明 |
対応バージョン |
size |
サブマッチの数を返す |
C++11 |
max_size |
格納できるサブマッチの最大数を返す |
C++11 |
empty |
マッチしたか否かを返す |
C++11 |
名前 |
説明 |
対応バージョン |
length |
指定されたサブマッチの長さを返す |
C++11 |
position |
指定されたサブマッチの位置を返す |
C++11 |
str |
指定されたサブマッチを文字列の形で返す |
C++11 |
operator[] |
指定されたサブマッチを返す |
C++11 |
prefix |
マッチした文字列の前の文字列を示すサブマッチを返す |
C++11 |
suffix |
マッチした文字列の後の文字列を示すサブマッチを返す |
C++11 |
begin |
先頭のサブマッチを指すイテレータを取得する |
C++11 |
end |
末尾のサブマッチの次を指すイテレータを取得する |
C++11 |
cbegin |
先頭のサブマッチを指すイテレータを取得する |
C++11 |
cend |
末尾のサブマッチの次を指すイテレータを取得する |
C++11 |
名前 |
説明 |
対応バージョン |
format |
match_results オブジェクトを書式文字列に従って出力する |
C++11 |
名前 |
説明 |
対応バージョン |
swap |
オブジェクトの内容を交換する |
C++11 |
名前 |
説明 |
対応バージョン |
value_type |
要素の型。sub_match <BidirectionalIterator> の別名 |
C++11 |
const_reference |
const 参照の型。const value_type& の別名 |
C++11 |
reference |
参照の型。value_type& の別名(C++11 では const value_type& となっていたが、規格のバグとして C++14 で修正された) |
C++11 |
const_iterator |
読み取り専用イテレータの型。実装依存の型の別名 |
C++11 |
iterator |
イテレータの型。const_iterator |
C++11 |
difference_type |
2 つのイテレータの差の型。typename iterator_traits <BidirectionalIterator>::difference_type の別名 |
C++11 |
size_type |
typename allocator_traits <Allocator>::size_type の別名 |
C++11 |
allocator_type |
アロケータオブジェクトの型。Allocator の別名 |
C++11 |
char_type |
文字の型。typename iterator_traits <BidirectionalIterator>::value_type の別名 |
C++11 |
string_type |
文字列の型。basic_string <char_type> の別名 |
C++11 |
#include <iostream>
#include <regex>
int main()
{
const char s[] = "The C++11 is very cool!!";
const std::regex re("(\\w+) is (\\w+)");
std::cmatch m;
if (std::regex_search(s, m, re)) {
std::cout << "ready = " << std::boolalpha << m.ready() << ", empty = " << m.empty() << std::endl << std::endl;
std::cout << "prefix:'" << m.prefix() << '\'' << std::endl;
for (std::size_t i = 0, n = m.size(); i < n; ++i) {
std::cout << i << ":'" << m.str(i) << "\', position = " << m.position(i) << ", length = " << m.length(i) << std::endl;
}
std::cout << "suffix:'" << m.suffix() << '\'' << std::endl << std::endl;
std::cout << m.format("$`14 is $2$'") << std::endl;
} else {
std::cout << "not match" << std::endl;
}
}
- std::cmatch[color ff0000]
- std::regex[link basic_regex.md]
- std::regex_search[link regex_search.md]
- m.ready()[link match_results/ready.md]
- m.empty()[link match_results/empty.md]
- m.size()[link match_results/size.md]
- m.prefix()[link match_results/prefix.md]
- m.suffix()[link match_results/suffix.md]
- m.str[link match_results/str.md]
- m.position[link match_results/position.md]
- m.length[link match_results/length.md]
- m.format[link match_results/format.md]
ready = true, empty = false
prefix:'The C++'
0:'11 is very', position = 7, length = 10
1:'11', position = 7, length = 2
2:'very', position = 13, length = 4
suffix:' cool!!'
The C++14 is very cool!!
- 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], 4.9.2 [mark verified], 5.0.0 [mark verified]
- ICC: ??
- Visual C++: ??