微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

`regex_match` 返回 'not found' 和 `match_results`

如何解决`regex_match` 返回 'not found' 和 `match_results`

在以下代码 (gcc 10.2.1) 中,对 regex_match调用返回“不匹配”,我认为这是正确的。

sm.size() 返回 0,但是当从 sm.begin() 迭代到 end() 时,它发现出现了 3 次(都是空字符串)。

如果这是正确的,这三个发现是什么意思?

但是既然size()==0,不应该begin() == end()吗?

编辑:根据评论,我在输出添加ready 标志

#include <iostream>
#include <string>
#include <regex>
#include <assert.h>

int main()
{
    std::string input("4321");
    std::regex rg("^([0-9])");
    std::smatch sm;

    bool found = std::regex_match(input,sm,rg);

    assert(!sm.size() == sm.empty());

     std::cout << "ready: " << sm.ready() << ",found: " <<
          found << ",size: " << sm.size() << std::endl;


    for (auto it = sm.begin(); it != sm.end(); ++it)
    {
        std::cout << "iterate '" << *it << "'\n";
    }
}

输出

ready: 1,found: 0,size: 0
iterate ''
iterate ''
iterate ''

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。