我是C ++regex
库的新手。我注意到在遵循cplusplus.com上的文档时,在他们的示例中,true
如果我使用与整个目标序列匹配的正则表达式,则终止终止循环所使用的条件将始终返回。理想情况下,循环应匹配一次,然后终止。这是我的代码:
#include <iostream>
#include <regex>
int main()
{
std::string str("Foo bar");
std::regex reg("(.|[\r\n])*"); // 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。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句