std::regex_match
ヘッダ <regex> で定義
|
||
template< class BidirIt, class Alloc, class CharT, class Traits > |
(1) | (C++11以上) |
template< class BidirIt, class CharT, class Traits > |
(2) | (C++11以上) |
template< class CharT, class Alloc, class Traits > bool regex_match( const CharT* str, |
(3) | (C++11以上) |
template< class STraits, class SAlloc, class Alloc, class CharT, class Traits > |
(4) | (C++11以上) |
template< class CharT, class Traits > bool regex_match( const CharT* str, |
(5) | (C++11以上) |
template< class STraits, class SAlloc, class CharT, class Traits > |
(6) | (C++11以上) |
template< class STraits, class SAlloc, class Alloc, class CharT, class Traits > |
(7) | (C++14以上) |
正規表現 e
が std::string、 C の文字列、またはイテレータの組として指定されるターゲット文字シーケンス全体にマッチするかどうかを判定します。
flags
の効果を考慮して、正規表現 e
とターゲット文字シーケンス [first,last)
全体の間のマッチが存在するかどうか判定します。 マッチの存在を判定するとき、文字シーケンス全体にマッチするマッチの可能性のみが考慮されます。 マッチの結果は m
に返されます。std::regex_search が部分シーケンスのマッチに成功するのに対して、 regex_match
は正規表現を文字シーケンス全体のマッチにのみ成功することに注意してください。
目次 |
[編集] 引数
first, last | - | イテレータとして指定された、正規表現を適用するターゲット文字範囲 |
m | - | マッチ結果 |
str | - | C スタイルのヌル終端文字列として指定された、ターゲット文字列 |
s | - | std::basic_string として指定された、ターゲット文字列 |
e | - | 正規表現 |
flags | - | マッチがどのように行われるかを決定するために使用するフラグ |
型の要件 | ||
-BidirIt は LegacyBidirectionalIterator の要件を満たさなければなりません。
|
[編集] 戻り値
マッチが存在する場合は true、そうでなければ false を返します。 いずれの場合でも、オブジェクト m
は以下のように更新されます。
マッチが存在しない場合、
m.ready() == true | |
m.empty() == true | |
m.size() == 0 |
マッチが存在する場合、
m.ready() | true |
m.empty() | false |
m.size() | マーク付き部分表現の数プラス1、つまり 1+e.mark_count() |
m.prefix().first | first
|
m.prefix().second | first
|
m.prefix().matched | false (マッチの接頭辞は空です) |
m.suffix().first | last
|
m.suffix().second | last
|
m.suffix().matched | false (マッチの接尾辞は空です) |
m[0].first | first
|
m[0].second | last
|
m[0].matched | true (シーケンス全体にマッチします) |
m[n].first | n 番目のマーク付き部分表現にマッチしたシーケンスの開始位置、または部分表現がマッチしなかった場合は last
|
m[n].second | n 番目のマーク付き部分表現にマッチしたシーケンスの終了位置、または部分表現がマッチしなかった場合は last
|
m[n].matched | n 番目の部分表現がマッチした場合は true、そうでなければ false |
[編集] ノート
regex_match
はマッチ全体のみを考慮するため、同じ正規表現が regex_match
と std::regex_search で異なる結果を出す場合があります。
std::regex re("Get|GetValue"); std::cmatch m; std::regex_search("GetValue", m, re); // returns true, and m[0] contains "Get" std::regex_match ("GetValue", m, re); // returns true, and m[0] contains "GetValue" std::regex_search("GetValues", m, re); // returns true, and m[0] contains "Get" std::regex_match ("GetValues", m, re); // returns false
[編集] 例
#include <iostream> #include <string> #include <regex> int main() { // 単純な正規表現のマッチ。 const std::string fnames[] = {"foo.txt", "bar.txt", "baz.dat", "zoidberg"}; const std::regex txt_regex("[a-z]+\\.txt"); for (const auto &fname : fnames) { std::cout << fname << ": " << std::regex_match(fname, txt_regex) << '\n'; } // 部分マッチの抽出。 const std::regex base_regex("([a-z]+)\\.txt"); std::smatch base_match; for (const auto &fname : fnames) { if (std::regex_match(fname, base_match, base_regex)) { // 1つめの sub_match は文字列全体です。 // 2つめの sub_match は1つめの括弧で囲まれた表現です。 if (base_match.size() == 2) { std::ssub_match base_sub_match = base_match[1]; std::string base = base_sub_match.str(); std::cout << fname << " has a base of " << base << '\n'; } } } // 複数の部分マッチの抽出。 const std::regex pieces_regex("([a-z]+)\\.([a-z]+)"); std::smatch pieces_match; for (const auto &fname : fnames) { if (std::regex_match(fname, pieces_match, pieces_regex)) { std::cout << fname << '\n'; for (size_t i = 0; i < pieces_match.size(); ++i) { std::ssub_match sub_match = pieces_match[i]; std::string piece = sub_match.str(); std::cout << " submatch " << i << ": " << piece << '\n'; } } } }
出力:
foo.txt: 1 bar.txt: 1 baz.dat: 0 zoidberg: 0 foo.txt has a base of foo bar.txt has a base of bar foo.txt submatch 0: foo.txt submatch 1: foo submatch 2: txt bar.txt submatch 0: bar.txt submatch 1: bar submatch 2: txt baz.dat submatch 0: baz.dat submatch 1: baz submatch 2: dat
[編集] 関連項目
(C++11) |
正規表現オブジェクト (クラステンプレート) |
(C++11) |
すべての部分表現のマッチを含む1つの正規表現マッチを識別します (クラステンプレート) |
(C++11) |
文字シーケンスの任意の部分への正規表現のマッチを試みます (関数テンプレート) |