提高C ++正则表达式 - 如何让多个匹配提高C ++正则表达式 - 如何让多个匹配(Boost C

2019-05-12 07:15发布

如果我有像一个简单的正则表达式“AB”。 我也有像“ABC,ABD”多个匹配的字符串。 如果我做了以下...

boost::match_flag_type flags = boost::match_default;
boost::cmatch mcMatch;
boost::regex_search("abc abd", mcMatch, "ab.", flags)

然后mcMatch只包含第一个“ABC”的结果。 我怎样才能得到所有可能的匹配?

Answer 1:

您可以使用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


文章来源: Boost C++ regex - how to get multiple matches
标签: c++ regex boost