我认为 Dot Net 有能力制作单个捕获组集合,以便 (grp)+ 将在 group1 上创建一个集合对象。boost 引擎的 regex_search() 就像任何普通的匹配函数一样。你坐在一个 while() 循环中,匹配最后一个匹配停止的模式。您使用的表单不使用出价迭代器,因此该函数不会在最后一个匹配停止的地方开始下一个匹配。
您可以使用迭代器形式:(
编辑-您也可以使用令牌迭代器,定义要迭代的组。在下面的代码中添加)。
#include <boost/regex.hpp>
#include <string>
#include <iostream>
using namespace std;
using namespace boost;
int main()
{
string input = "test1 ,, test2,, test3,, test0,,";
boost::regex r("(test[0-9])(?:$|[ ,]+)");
boost::smatch what;
std::string::const_iterator start = input.begin();
std::string::const_iterator end = input.end();
while (boost::regex_search(start, end, what, r))
{
string stest(what[1].first, what[1].second);
cout << stest << endl;
// Update the beginning of the range to the character
// following the whole match
start = what[0].second;
}
// Alternate method using token iterator
const int subs[] = {1}; // we just want to see group 1
boost::sregex_token_iterator i(input.begin(), input.end(), r, subs);
boost::sregex_token_iterator j;
while(i != j)
{
cout << *i++ << endl;
}
return 0;
}
输出:
test1
test2
test3
test0