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

I found m[1].first is "abcdlogin", m[1].second is "dlogin".

But I think is m[1].first should be "abc"?

有帮助吗?

解决方案

As noted in the 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.

Note how they are iterators into the matching sub-expression. In your example, if you want a string with "abc", you can construct a string like this: std::string s(m[1].first, m[1].second);.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top