c++ - Boost C++ 正则表达式 - 如何获得多个匹配项

标签 c++ regex boost

如果我有一个简单的正则表达式模式,比如“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

关于c++ - Boost C++ 正则表达式 - 如何获得多个匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3122344/

相关文章:

regex - 从 pyspark 中的 RDD 选择子字符串

c++ - Qt 对 boost::system::generic_category() 的 undefined reference

c++ - 未能使用 boost 1.48 构建共享库

c++ - C++ 中用于删除重复项的通用函数

c++ - 如何解决 MSCV 中 Boost 文件系统库的 LNK1104 错误?

c++ - 命名空间中的变量如何优于全局变量

regex - .htaccess 替换一个词后重定向?

javascript - 错误: javascript key in invalid format integer

c++ - 从另一个 vector 类型中包含的组件构建 vector

C++ - (*) 之间的区别。和->?