【问题标题】:std::regex_search reverse searchstd::regex_search 反向搜索
【发布时间】:2021-07-31 15:37:33
【问题描述】:

https://stackoverflow.com/a/33307828/9250490 你可以通过regex_search(s.cbegin(), s.cend()...找到第一个5序号12345

string s = "abcde, eee12345 11111ddd 55555 hello";
std::smatch m;
bool b = std::regex_search(s.cbegin(), s.cend(), m, std::regex("[0-9]{5}"));
cout << m[0] << endl;

但是如果我想找到最后一个55555,我试过了,

std::regex_search(s.crbegin(), s.crend(), m, std::regex("[0-9]{5}"));

但是编译不会出错,

Error C2672 'std::regex_search': no matching overloaded function found ForTest

基于 C++17 标准的 Visual Studion 2019。 为什么?

【问题讨论】:

    标签: c++ regex string iterator c++17


    【解决方案1】:

    这是因为迭代器类型不匹配,如果你在 Visual Studio 中查看std::smatch 的源代码就会发现。

    using smatch  = match_results<string::const_iterator>;
    

    而您正试图在您的 std::regex_search 呼叫中传递 std::string::const_reverse_iterator

    直接使用std::match_results&lt;std::string::const_reverse_iterator&gt;

    const std::string s = "abcde, eee12345 11111ddd 55555 hello";
    std::match_results<std::string::const_reverse_iterator> m;
    bool b = std::regex_search(s.crbegin(), s.crend(), m, std::regex("[0-9]{5}"));
    std::cout << m[0] << std::endl;
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    相关资源
    最近更新 更多