Namespaces
Variants

std::match_results<BidirIt,Alloc>:: str

From cppreference.net
Regular expressions library
Classes
(C++11)
Algorithms
Iterators
Exceptions
Traits
Constants
(C++11)
Regex Grammar
string_type str ( size_type n = 0 ) const ;
(自 C++11 起)

返回表示指定子匹配的字符串。

如果 n == 0 ,将返回表示整个匹配表达式的字符串。

如果 0 < n && n < size ( ) ,则返回表示第 n 个子匹配的字符串。

如果 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

参见

返回指定的子匹配
(公开成员函数)