在 Perl 中,我可以这样做:
$text = '1747239';
@matches = ($text =~ m/(\d)/g);
# @matches now contains ('1', '7', '4', '7', '2', '3', '9')
使用 C++ 正则表达式匹配,复制这种行为的最佳方法是什么,以便我得到一个包含所有匹配项的匹配集?
我现在有这个:-
compiledRegex = std::regex(regex, std::tr1::regex_constants::extended);
regex_search(text, results, compiledRegex);
int count = results.size();
// Alloc pointer array based on count * sizeof(mystruct).
for ( std::cmatch::iterator match = results.begin();
match != results.end();
++match )
{
// Do something with match;
}
但是,这只会给我第一个匹配项,就像没有 /g 的 Perl 一样,这很好,但我想要 /g 效果。
那么,有没有一种好方法可以做到这一点,还是我必须一遍又一遍地运行正则表达式?