std::regex中的错误
Bug in std::regex?
以下是代码:
#include <string>
#include <regex>
#include <iostream>
int main()
{
std::string pattern("[^c]ei");
pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
std::regex r(pattern);
std::smatch results;
std::string test_str = "cei";
if (std::regex_search(test_str, results, r))
std::cout << results.str() << std::endl;
return 0;
}
输出:
cei
使用的编译器是gcc 4.9.1
。
我是一个学习正则表达式的新手。我希望不输出任何内容,因为"cei"
与这里的模式不匹配。我做得对吗?怎么了?
更新:
此错误已被报告并确认为错误,有关详细信息,请访问此处:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63497
这是实现中的一个bug。我尝试过的其他几个工具不仅同意你的模式与你的输入不匹配,而且我尝试过这个:
#include <string>
#include <regex>
#include <iostream>
int main()
{
std::string pattern("([a-z]*)([a-z])(e)(i)([a-z]*)");
std::regex r(pattern);
std::smatch results;
std::string test_str = "cei";
if (std::regex_search(test_str, results, r))
{
std::cout << results.str() << std::endl;
for (size_t i = 0; i < results.size(); ++i) {
std::ssub_match sub_match = results[i];
std::string sub_match_str = sub_match.str();
std::cout << i << ": " << sub_match_str << 'n';
}
}
}
这基本上与您的类似,但为了简单起见,我用[a-z]
替换了[:alpha:]
,还临时用[a-z]
替换了[^c]
,因为这似乎可以使其正常工作。以下是它打印的内容(Linux x86-64上的GCC 4.9.0):
cei
0: cei
1:
2: c
3: e
4: i
5:
如果我将[^c]
替换为[a-z]
,而只是将f
放在那里,它正确地表示模式不匹配。但如果我像你一样使用[^c]
:
std::string pattern("([a-z]*)([^c])(e)(i)([a-z]*)");
然后我得到这个输出:
cei
0: cei
1: cei
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create
Aborted (core dumped)
所以它声称匹配成功,结果[0]是预期的"cei"。那么,results[1]也是"cei",我想这可能还可以。但随后results[2]崩溃了,因为它试图构造一个长度为18446744073709551614
、begin=nullptr的std::string
。这个巨大的数字正是2^64 - 2
,也就是std::string::npos - 1
(在我的系统中)。
因此,我认为某个地方存在一个错误,其影响可能不仅仅是一个虚假的正则表达式匹配——它可能在运行时崩溃。
正则表达式是正确的,并且应该与字符串"cei"不匹配。
正则表达式可以在Perl:中得到最好的测试和解释
my $regex = qr{ # start regular expression
[[:alpha:]]* # 0 or any number of alpha chars
[^c] # followed by NOT-c character
ei # followed by e and i characters
[[:alpha:]]* # followed by 0 or any number of alpha chars
}x; # end + declare 'x' mode (ignore whitespace)
print "xei" =~ /$regex/ ? "matchn" : "no matchn";
print "cei" =~ /$regex/ ? "matchn" : "no matchn";
正则表达式将首先消耗字符串末尾的所有字符([[:alpha:]]*
),然后回溯以找到NON-c字符[^c]
并继续进行e和i匹配(通过另一次回溯)。
结果:
"xei" --> match
"cei" --> no match
原因显而易见。在各种C++库和测试工具中,任何与此不一致的地方都是实现的问题,imho。
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 使用Regex过滤错误的输入
- 使用clang编译时regex-segfault,可能存在编译器错误
- 在OS X上使用clang编译boost regex时发生链接错误
- Boost Regex中的命名子表达式错误
- Boost.Regex联动装置错误LUbuntu 14.04 g++4.8.2
- std::regex中的错误
- std::regex上的分段错误,以及使用C++驱动程序进行多线程时的分段错误
- regex.h中的编译错误cygwin没有这样的文件或目录
- c++ boost regex日期错误