Boost C++ regex - how to get multiple matches(Boost C++ regex - 如何获得多个匹配项)
问题描述
如果我有一个简单的正则表达式模式,比如ab".我有一个字符串,它有多个匹配项,例如abc abd".如果我执行以下操作...
If I have a simple regex pattern like "ab." and I have a string that has multiple matches like "abc abd". If I do the following...
boost::match_flag_type flags = boost::match_default;
boost::cmatch mcMatch;
boost::regex_search("abc abd", mcMatch, "ab.", flags)
然后 mcMatch 只包含第一个abc"结果.如何获得所有可能的匹配项?
Then mcMatch contains just the first "abc" result. How can I get all possible matches?
推荐答案
你可以使用 boost::sregex_token_iterator
就像这个简短的例子:
You can use the boost::sregex_token_iterator
like in this short example:
#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<<'
';
}
return 0;
}
这个程序的输出是:
abc
abd
这篇关于Boost C++ regex - 如何获得多个匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Boost C++ regex - 如何获得多个匹配项


基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01