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/