命名空间
变体
操作

std::sub_match<BidirIt>::operator string_type, std::sub_match<BidirIt>::str

来自 cppreference.cn
< cpp‎ | regex‎ | sub match
 
 
 
正则表达式库
(C++11)
算法
迭代器
异常
特性
常量
(C++11)
正则表达式语法
 
std::sub_match
成员函数
sub_match::strsub_match::operator string_type
非成员函数
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
 
operator string_type() const;
(1)
string_type str() const;
(2)

转换为底层 std::basic_string 类型的对象。

1) 隐式转换。
2) 显式转换。

[编辑] 返回值

匹配的字符序列,作为底层 std::basic_string 类型的对象。如果 matched 成员为 false,则返回空字符串。

[编辑] 复杂度

与底层字符序列的长度呈线性关系。

[编辑] 示例

#include <cassert>
#include <iostream>
#include <regex>
#include <string>
 
int main()
{
    const std::string html{R"("<a href="https://cppreference.cn/">)"};
    const std::regex re{"(http|https|ftp)://([a-z]+)\\.([a-z]{3})"};
    std::smatch parts;
    std::regex_search(html, parts, re);
    for (std::ssub_match const& sub : parts)
    {
        const std::string s = sub; // (1) implicit conversion
        assert(s == sub.str());    // (2)
        std::cout << s << '\n';
    }
}

输出

https://cppreference.cn
https
cppreference
com