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,則涉及 m 的以下表達式應為下面列出的每個過載產生指定值
表示式 | 值 | ||
---|---|---|---|
過載 (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 | last | 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 | last | 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++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2205 | C++11 | 後置條件中 n 可能為零 | 只能為正 |
LWG 2273 | C++11 | 不清楚是否考慮部分匹配 | 只考慮完全匹配 |
LWG 2329 | C++11 | 過載 (5) 接受 basic_string 右值,可能導致懸空迭代器 |
透過刪除的過載 (7) 拒絕 |
[編輯] 另見
(C++11) |
正則表示式物件 (類模板) |
(C++11) |
標識一個正則表示式匹配,包括所有子表示式匹配 (類模板) |
(C++11) |
嘗試將正則表示式與字元序列的任何部分匹配 (函式模板) |