c++ - boost regex_search 找不到第一个匹配项

标签 c++ regex boost boost-regex

boost::regex re("(abc)(.*?)");
boost::smatch m;
std::string str = "abcdlogin";
boost::regex_search(str, m, re);

我发现 m[1].first 是“abcdlogin”,m[1].second 是“dlogin”。

但是我觉得是m[1].first应该是"abc"吧?

最佳答案

documentation 中所述:

m[n].first: For all integers n < m.size(), the start of the sequence that matched sub-expression n. Alternatively, if sub-expression n did not participate in the match, then last.

m[n].second: For all integers n < m.size(), the end of the sequence that matched sub-expression n. Alternatively, if sub-expression n did not participate in the match, then last.

注意它们是如何迭代器到匹配的子表达式中的。 在您的示例中,如果您想要一个带有 "abc" 的字符串,您可以像这样构造一个字符串:std::string s(m[1].first, m[1]。第二);

关于c++ - boost regex_search 找不到第一个匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13794365/

相关文章:

c++ - 压扁容器容器的通用函数

java - 需要根据分隔符分割字符串,但它们被分组

c++ - 读取和解析 JSON 文件 C++ BOOST

c++ - 运行良好,但在完成之前崩溃

c++ - 在 C++ 中仅连接字符串中的选定元素

c++ - 如何修复 std::stoi 中的 "std::invalid_argument"

regex - 搜索子字符串的节省空间的方法

java - 如何为这种情况编写正则表达式?

c++ - BOOST_PHOENIX_ADAPT_FUNCTION 导致无效模板错误

C++错误:使用已删除的函数boost::asio::io_context::io_context