std::match_results<BidirIt,Alloc>::str
来自 cppreference.cn
< cpp | regex | match results
string_type str( size_type n = 0 ) const; |
(C++11 起) | |
返回表示指示的子匹配的字符串。
如果 n == 0,则返回表示整个匹配表达式的字符串。
如果 0 < n && n < size(),则返回表示第 nth 个子匹配的字符串。
如果 n >= size(),则返回表示未匹配的匹配项的字符串。
此调用等效于 string_type((*this)[n]);
ready()
必须为 true。否则,行为是未定义的。
目录 |
[编辑] 参数
n | - | 指定要返回哪个匹配项的整数 |
[编辑] 返回值
返回表示指定匹配项或子匹配项的字符串。
[编辑] 示例
运行此代码
#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.str(0) << '\n' << "submatch #1: " << sm.str(1) << '\n'; std::regex re2("a(a*)b"); std::regex_search(target, sm, re2); std::cout << "entire match: " << sm.str(0) << '\n' << "submatch #1: " << sm.str(1) << '\n'; }
输出
entire match: aaab submatch #1: a entire match: aaab submatch #1: aa
[编辑] 参见
返回指定的子匹配项 (公共成员函数) |