std::match_results<BidirIt,Alloc>::operator[]
來自 cppreference.com
< cpp | regex | match_results
const_reference operator[]( size_type n ) const; |
(C++11 起) | |
如果 n > 0 且 n < size(),則返回對 std::sub_match 的引用,該物件表示由第 n 個捕獲的標記子表示式匹配的目標序列部分。
如果 n == 0,則返回對 std::sub_match 的引用,該物件表示由整個匹配的正則表示式匹配的目標序列部分。
如果 n >= size(),則返回對 std::sub_match 的引用,該物件表示未匹配的子表示式(目標序列的空子範圍)。
ready()
必須為 true。否則,行為未定義。
目錄 |
[編輯] 引數
n | - | 指定要返回哪個匹配項的整數 |
[編輯] 返回值
對 std::sub_match 的引用,該物件表示目標序列中指定的匹配子範圍。
[編輯] 示例
執行此程式碼
#include <iostream> #include <regex> #include <string> int main() { std::string target("baaaby"); std::smatch sm; std::regex re1("a(a)*b"); std::regex_search(target, sm, re1); std::cout << "entire match: " << sm[0] << '\n' << "submatch #1: " << sm[1] << '\n'; std::regex re2("a(a*)b"); std::regex_search(target, sm, re2); std::cout << "entire match: " << sm[0] << '\n' << "submatch #1: " << sm[1] << '\n'; }
輸出
entire match: aaab submatch #1: a entire match: aaab submatch #1: aa
[編輯] 參閱
返回特定子匹配項的字元序列 (公共成員函式) |