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

来自cppreference.com
< cpp‎ | regex‎ | sub match
operator string_type() const;
(1)
string_type str() const;
(2)

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

第一版本为隐式转换,第二版本为显式。

参数

(无)

返回值

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

复杂度

与底层字符序列的长度成线性。

示例

#include <cassert>
#include <iostream>
#include <regex>
#include <string>
 
int main()
{
    const std::string html{R"("<a href="https://cppreference.com/">)"};
    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) 隐式转换
        assert(s == sub.str());    // (2)
        std::cout << s << '\n';
    }
}

输出:

https://cppreference.com
https
cppreference
com