std::regex_search
在標頭檔案 <regex> 中定義 |
||
template< class BidirIt, class Alloc, class CharT, class Traits > bool regex_search( BidirIt first, BidirIt last, |
(1) | (C++11 起) |
template< class BidirIt, class CharT, class Traits > bool regex_search( BidirIt first, BidirIt last, |
(2) | (C++11 起) |
template< class CharT, class Alloc, class Traits > bool regex_search( const CharT* str, |
(3) | (C++11 起) |
template< class CharT, class Traits > bool regex_search( 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_search( 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[0].first | ||
m.prefix().matched | m.prefix().first != m.prefix().second | ||
m.suffix().first | m[0].second | ||
m.suffix().second | last | std::char_traits<CharT>:: length(str) + str |
s.end() |
m.suffix().matched | m.suffix().first != m.suffix().second | ||
m[0].first | 匹配 e 的序列的開始 | ||
m[0].second | 匹配 e 的序列的結束 | ||
m[0].matched | true | ||
m[n].first |
| ||
m[n].second |
| ||
m[n].matched |
|
目錄 |
[編輯] 引數
first, last | - | 目標字元範圍 |
str | - | 目標空終止 C 風格字串 |
s | - | 目標 std::basic_string |
m | - | 匹配結果 |
e | - | 正則表示式 |
flags | - | 用於確定匹配方式的標誌 |
[編輯] 返回值
如果存在匹配,則返回 true,否則返回 false。
[編輯] 注意
為了檢查目標序列中的所有匹配,可以迴圈呼叫 std::regex_search
,每次從上次呼叫的 m[0].second 開始。 std::regex_iterator 提供了這種迭代的簡單介面。
[編輯] 示例
#include <cstddef> #include <iostream> #include <regex> #include <string> int main() { std::string lines[] = {"Roses are #ff0000", "violets are #0000ff", "all of my base are belong to you"}; std::regex color_regex("#([a-f0-9]{2})" "([a-f0-9]{2})" "([a-f0-9]{2})"); // simple match for (const auto& line : lines) std::cout << line << ": " << std::boolalpha << std::regex_search(line, color_regex) << '\n'; std::cout << '\n'; // show contents of marked subexpressions within each match std::smatch color_match; for (const auto& line : lines) if (std::regex_search(line, color_match, color_regex)) { std::cout << "matches for '" << line << "'\n"; std::cout << "Prefix: '" << color_match.prefix() << "'\n"; for (std::size_t i = 0; i < color_match.size(); ++i) std::cout << i << ": " << color_match[i] << '\n'; std::cout << "Suffix: '" << color_match.suffix() << "\'\n\n"; } // repeated search (see also std::regex_iterator) std::string log(R"( Speed: 366 Mass: 35 Speed: 378 Mass: 32 Speed: 400 Mass: 30)"); std::regex r(R"(Speed:\t\d*)"); for (std::smatch sm; regex_search(log, sm, r);) { std::cout << sm.str() << '\n'; log = sm.suffix(); } // C-style string demo std::cmatch cm; if (std::regex_search("this is a test", cm, std::regex("test"))) std::cout << "\nFound " << cm[0] << " at position " << cm.prefix().length() << '\n'; }
輸出
Roses are #ff0000: true violets are #0000ff: true all of my base are belong to you: false matches for 'Roses are #ff0000' Prefix: 'Roses are ' 0: #ff0000 1: ff 2: 00 3: 00 Suffix: '' matches for 'violets are #0000ff' Prefix: 'violets are ' 0: #0000ff 1: 00 2: 00 3: ff Suffix: '' Speed: 366 Speed: 378 Speed: 400 Found test at position 10
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2205 | C++11 | 後置條件中 n 可以為零 | 只能為正 |
LWG 2329 | C++11 | 過載 (5) 接受了 basic_string 右值,這可能導致懸空迭代器 |
透過刪除過載 (7) 拒絕 |
[編輯] 另請參閱
(C++11) |
正則表示式物件 (類模板) |
(C++11) |
標識一個正則表示式匹配,包括所有子表示式匹配 (類模板) |
(C++11) |
嘗試將正則表示式與整個字元序列匹配 (函式模板) |