如何访问重复捕获组的所有匹配项,而不仅仅是最后一个?

yeg*_*256 5 c++ regex boost

我的代码是:

#include <boost/regex.hpp>
boost::cmatch matches;
boost::regex_match("alpha beta", matches, boost::regex("([a-z])+"));
cout << "found: " << matches.size() << endl;
Run Code Online (Sandbox Code Playgroud)

并且它显示found: 2哪个意味着只找到一个事件......如何指示它找到三个事件?谢谢!

yeg*_*256 2

这是我到目前为止发现的:

text = "alpha beta";
string::const_iterator begin = text.begin();
string::const_iterator end = text.end();
boost::match_results<string::const_iterator> what;
while (regex_search(begin, end, what, boost::regex("([a-z]+)"))) {
    cout << string(what[1].first, what[2].second-1);
    begin = what[0].second;
}
Run Code Online (Sandbox Code Playgroud)

它按预期工作。也许有人知道更好的解决方案?