如果我有像一个简单的正则表达式“AB”。 我也有像“ABC,ABD”多个匹配的字符串。 如果我做了以下...
boost::match_flag_type flags = boost::match_default;
boost::cmatch mcMatch;
boost::regex_search("abc abd", mcMatch, "ab.", flags)
然后mcMatch只包含第一个“ABC”的结果。 我怎样才能得到所有可能的匹配?
如果我有像一个简单的正则表达式“AB”。 我也有像“ABC,ABD”多个匹配的字符串。 如果我做了以下...
boost::match_flag_type flags = boost::match_default;
boost::cmatch mcMatch;
boost::regex_search("abc abd", mcMatch, "ab.", flags)
然后mcMatch只包含第一个“ABC”的结果。 我怎样才能得到所有可能的匹配?
您可以使用boost::sregex_token_iterator
喜欢在这短短的例子:
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main() {
std::string text("abc abd");
boost::regex regex("ab.");
boost::sregex_token_iterator iter(text.begin(), text.end(), regex, 0);
boost::sregex_token_iterator end;
for( ; iter != end; ++iter ) {
std::cout<<*iter<<'\n';
}
return 0;
}
该程序的输出是:
abc
abd