std::regex_match
| ヘッダ <regex> で定義 |
||
| template< class BidirIt, class Alloc, class CharT, class Traits > bool regex_match( BidirIt first, BidirIt last, |
(1) | (C++11以降) |
| template< class BidirIt, class CharT, class Traits > bool regex_match( BidirIt first, BidirIt last, |
(2) | (C++11以降) |
| template< class CharT, class Alloc, class Traits > bool regex_match( const CharT* str, |
(3) | (C++11以降) |
| template< class CharT, class Traits > bool regex_match( const CharT* str, const std::basic_regex<CharT, Traits>& e, |
(4) | (C++11以降) |
| template< class STraits, class SAlloc, class Alloc, class CharT, class Traits > |
(5) | (C++11以降) |
| template< class STraits, class SAlloc, class CharT, class Traits > bool regex_match( const std::basic_string<CharT, STraits, SAlloc>& s, |
(6) | (C++11以降) |
| template< class STraits, class SAlloc, class Alloc, class CharT, class Traits > |
(7) | (C++11以降) |
正規表現 e がターゲット文字列シーケンス全体にマッチするかどうかを判定します。詳細なマッチ結果は(存在する場合)m に格納されます。
[first, last) で表されます。|
|
(C++23まで) |
|
|
(C++23から) |
[str, str + std::char_traits<CharT>::length(str)) で表されます。マッチが存在しない場合、m (存在する場合)に関する以下の式は、指定された値を返すべきです。
| 式 | 値 |
|---|---|
| m.ready() | true |
| m.size() | 0 |
| m.empty() | true |
マッチが存在する場合、(0, m.size()) の範囲の任意の整数を n として、以下の式は、以下にリストされた各オーバーロードについて、指定された値を返すべきです。
| 式 | 値 | ||
|---|---|---|---|
| オーバーロード (1) | オーバーロード (3) | オーバーロード (5) | |
| m.ready() | true | ||
| m.size() | 1 + e.mark_count() | ||
| m.empty() | false | ||
| m.prefix().first | first | str | s.begin() |
| m.prefix().second | |||
| m.prefix().matched | false[1] | ||
| m.suffix().first | 最後 | std::char_traits<CharT>:: length(str) + str |
s.end() |
| m.suffix().second | |||
| m.suffix().matched | false[2] | ||
| m[0].first | first | str | s.begin() |
| m[0].second | 最後 | std::char_traits<CharT>:: length(str) + str |
s.end() |
| m[0].matched | true[3] | ||
| m[n].first |
| ||
| m[n].second |
| ||
| m[n].matched |
| ||
目次 |
[編集] パラメータ
| first, last | - | 対象の文字範囲 |
| str | - | 対象のヌル終端Cスタイルの文字列 |
| s | - | ターゲット std::basic_string |
| m | - | マッチ結果 |
| e | - | 正規表現 |
| flags | - | 一致の実行方法を決定するために使用されるフラグ |
[編集] 戻り値
ターゲットシーケンス全体が e にマッチした場合、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 <cstddef> #include <iostream> #include <regex> #include <string> int main() { // Simple regular expression matching 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'; // Extraction of a sub-match 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)) // The first sub_match is the whole string; the next // sub_match is the first parenthesized expression. 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'; } // Extraction of several sub-matches 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 (std::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++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2205 | C++11 | n は後条件でゼロになる可能性がある | 正の値しか取れない |
| LWG 2273 | C++11 | 部分的なマッチが考慮されるか不明確だった | 完全なマッチのみを考慮する |
| LWG 2329 | C++11 | オーバーロード (5) は basic_string の右辺値を受け入れたこれにより、ダングリングイテレータが発生する可能性があった |
削除されたオーバーロード (7) によって拒否された |
[編集] 関連項目
| (C++11) |
正規表現オブジェクト (クラステンプレート) |
| (C++11) |
すべての部分式マッチを含む、1つの正規表現マッチを識別する (クラステンプレート) |
| (C++11) |
正規表現を文字シーケンスの任意の部分にマッチさせようと試みる (関数テンプレート) |