为什么我的正则表达式会导致无限循环
Why is my regex causing an infinite loop?
我是 c++ regex
库的新手。我在关注有关 cplusplus.com 的文档时注意到,在他们的示例中,如果我使用与整个目标序列匹配的正则表达式,他们用于终止迭代循环的条件将始终返回true
。理想情况下,循环应匹配一次,然后终止。这是我的代码:
#include <iostream>
#include <regex>
int main()
{
std::string str("Foo bar");
std::regex reg("(.|[rn])*"); // Match the whole string
std::regex_iterator<std::string::iterator> rit(str.begin(), str.end(), reg);
std::regex_iterator<std::string::iterator> rend;
while (rit != rend) // For some reason this is always true
{
std::cout << "Infinite loop!" << std::endl;
rit++;
}
return 0;
}
我做错了什么?
不是肯定的,必须验证,但我相信你的*
意味着 0 或更多。因此,它可以永远匹配字符串末尾的 0。
相关文章:
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- 试图找出为什么我的代码保持无限循环
- 当我的 if 语句在字符串中找到空格时,在无限循环中切换
- 在控制台上输出我从文件中读取的整数,将程序插入无限循环
- 如何找出为什么我的程序一直有一个无限循环?
- 为什么我的代码在 c++ 中无限循环.我的代码需要反复提示用户
- 插入无限循环运行的单向链表
- 无法通过字符串循环我的程序以将其从英语翻译成猪拉丁语
- 为什么我有一个无限循环,我如何以不同的方式做到这一点
- 从二进制文件加载列表后,我的代码似乎进入了无限循环
- 谁能告诉我为什么我的程序在无限的循环中进行
- 我的c ++数组值不想改变,它输出无限循环
- 为什么我的埃拉托色尼代码筛子无限循环.我已经用几个数字进行了测试
- 无法发现无限循环背后的原因
- 找不到无限循环发生的位置
- 如何循环我的程序
- 有人能帮我循环我的程序吗(GAUSS SEIDEL METHOD)
- 无限循环中的Windows消息泵
- 精确地减少无限循环中的CPU使用
- 为什么当我按下一个字母时,会出现一个无限循环?我如何更改错误检查