`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 (将#修改为@)