Bug在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
与此处的模式不匹配。我做的对吗?有什么问题?
I'm a newbie learning regular expression.I expected nothing should be output,since "cei"
doesn't match the pattern here. Am I doing it right? What's the problem?
更新:
此报告已被报告并确认为错误,详情请访问:
https://gcc.gnu.org /bugzilla/show_bug.cgi?id=63497
This one has been reported and confirmed as a bug, for detail please visit here : https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63497
推荐答案
这是实施中的错误。不仅仅是一对夫妇其他工具我尝试同意你的模式不匹配你的输入,但我尝试这:
It's a bug in the implementation. Not only do a couple other tools I tried agree that your pattern does not match your input, but I tried this:
#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';
}
}
}
为了简单起见,我用 [az]
替换了 [:alpha:]
code> [^ c] 与 [az]
,因为这似乎使它正常工作。下面是它打印的(Linux x86-64上的GCC 4.9.0):
This is basically similar to what you had, but I replaced [:alpha:]
with [a-z]
for simplicity, and I also temporarily replaced [^c]
with [a-z]
because that seems to make it work correctly. Here's what it prints (GCC 4.9.0 on Linux x86-64):
cei
0: cei
1:
2: c
3: e
4: i
5:
如果我将 [az]
替换为 [^ c]
只是将 f
放在那里,它正确地说,模式不匹配。但是如果我像你这样使用 [^ c]
:
If I replace [a-z]
where you had [^c]
and just put f
there instead, it correctly says the pattern doesn't match. But if I use [^c]
like you did:
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。然后,结果[1]也是cei,我想可能是好的。但结果[2]崩溃,因为它尝试构造一个 std :: string
的长度 18446744073709551614
with begin = nullptr。这个巨型数字是 2 ^ 64 - 2
,aka std :: string :: npos - 1
我的系统)。
So it claims to match successfully, and results[0] is "cei" which is expected. Then, results[1] is "cei" also, which I guess might be OK. But then results[2] crashes, because it tries to construct a std::string
of length 18446744073709551614
with begin=nullptr. And that giant number is exactly 2^64 - 2
, aka std::string::npos - 1
(on my system).
所以我认为在某个地方有一个错误的错误,影响可以远远超过一个杂乱的正则表达式匹配 - 它可以在运行时崩溃。
So I think there is an off-by-one error somewhere, and the impact can be much more than just a spurious regex match--it can crash at runtime.
这篇关于Bug在std :: regex?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!