【发布时间】:2011-09-02 11:06:13
【问题描述】:
在 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 效果。
那么,有什么好的方法可以做到吗,还是我必须一遍又一遍地运行正则表达式?
【问题讨论】: