std::sub_match
來自 cppreference.com
在標頭檔案 <regex> 中定義 |
||
template< class BidirIt > class sub_match; |
(C++11 起) | |
類模板 std::sub_match
被正則表示式引擎用於表示被標記子表示式匹配的字元序列。匹配是正則表示式在目標範圍內匹配到的 [
begin,
end)
對,但附加了觀察器函式以提高程式碼清晰度。
只有預設建構函式是公共可訪問的。std::sub_match
的例項通常作為 std::match_results 容器的一部分在處理某個正則表示式演算法時構造和填充。
除非 matched
成員為 true,否則成員函式返回定義的預設值。
std::sub_match
繼承自 std::pair<BidirIt, BidirIt>,儘管它不能被視為 std::pair 物件,因為賦值等成員函式不會按預期工作。
目錄 |
[編輯] 型別要求
-BidirIt 必須滿足 LegacyBidirectionalIterator 的要求。 |
[編輯] 特化
為常見的字元序列型別提供了幾種特化
在標頭檔案
<regex> 中定義 | |
型別 | 定義 |
std::csub_match
|
std::sub_match<const char*> |
std::wcsub_match
|
std::sub_match<const wchar_t*> |
std::ssub_match
|
std::sub_match<std::string::const_iterator> |
std::wssub_match
|
std::sub_match<std::wstring::const_iterator> |
[編輯] 巢狀型別
型別 | 定義 |
iterator
|
BidirIt
|
value_type
|
std::iterator_traits<BidirIt>::value_type |
difference_type
|
std::iterator_traits<BidirIt>::difference_type |
string_type
|
std::basic_string<value_type> |
[編輯] 資料成員
成員 | 描述 |
bool matched |
此匹配是否成功 (公有成員物件) |
繼承自 std::pair
BidirIt first |
匹配序列的開始 (公有成員物件) |
BidirIt second |
匹配序列的結束(過一) (公有成員物件) |
[編輯] 成員函式
構造匹配物件 (公共成員函式) | |
觀察器 | |
返回匹配的長度(如果有) (公共成員函式) | |
轉換為底層字串型別 (公共成員函式) | |
比較匹配的子序列(如果有) (公共成員函式) | |
修改器 | |
交換內容 (公共成員函式) |
[編輯] 非成員函式
(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(C++20) |
將 sub_match 與另一個 sub_match 、字串或字元進行比較(函式模板) |
輸出匹配的字元子序列 (函式模板) |
[編輯] 示例
執行此程式碼
#include <cassert> #include <iostream> #include <regex> #include <string> int main() { std::string sentence{"Friday the thirteenth."}; const std::regex re{"([A-z]+) ([a-z]+) ([a-z]+)"}; std::smatch words; std::regex_search(sentence, words, re); std::cout << std::boolalpha; for (const auto& m : words) { assert(m.matched); std::cout << "m: [" << m << "], m.length(): " << m.length() << ", " "*m.first: '" << *m.first << "', " "*m.second: '" << *m.second << "'\n"; } }
輸出
m: [Friday the thirteenth], m.length(): 21, *m.first: 'F', *m.second: '.' m: [Friday], m.length(): 6, *m.first: 'F', *m.second: ' ' m: [the], m.length(): 3, *m.first: 't', *m.second: ' ' m: [thirteenth], m.length(): 10, *m.first: 't', *m.second: '.'
[編輯] 參見
(C++11) |
迭代給定字串中所有正則表示式匹配中指定的子表示式或未匹配的子字串 (類模板) |