(grp)+ が group1 にコレクション オブジェクトを作成するように、Dot Net には単一のキャプチャ グループ コレクションを作成する機能があると思います。ブースト エンジンの 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