bug in std :: regex?

Yue Wang

这是代码:

#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

约翰·兹温克

这是实现中的错误。我尝试过的其他几个工具不仅同意您的模式与您的输入不匹配,而且还尝试了以下方法:

#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';
    }
  }
}

这基本上与您所拥有的相似,但是为了简单起见,我用替换[:alpha:]了它[a-z],并且我暂时也替换[^c][a-z]因为这似乎使其正常工作。打印内容如下(Linux x86-64上的GCC 4.9.0):

cei
0: cei
1:
2: c
3: e
4: i
5:

如果我替换了[a-z]原来的位置,[^c]而只是放在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)

因此,它声称匹配成功,并且result [0]是预期的“ cei”。然后,结果[1]也是“ cei”,我想可能还可以。但是结果[2]崩溃了,因为它试图用begin = nullptr构造一个std::string长度为a的长度18446744073709551614这个巨大的数字2^64 - 2就是std::string::npos - 1(在我的系统上)。

因此,我认为某个地方存在一个错误的错误,其影响可能不仅仅是伪造的正则表达式匹配-它可能在运行时崩溃。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用std :: regex?

来自分类Dev

返回std.regex.regex的值?

来自分类Dev

std :: regex忽略regex命令中的空格

来自分类Dev

使用std :: regex过滤输入

来自分类Dev

C ++ 11 std :: regex替代

来自分类Dev

std :: regex_match与字符éèà

来自分类Dev

std :: regex构造抛出异常

来自分类Dev

constexpr bug,std :: is_same具有多重继承

来自分类Dev

解决python regex bug ^ H ^ H ^ H ... ahem ...错误的功能

来自分类Dev

如何使用std :: regex匹配多个结果

来自分类Dev

std :: regex_replace期间堆栈溢出

来自分类Dev

如何知道libstdc ++是否支持std :: regex

来自分类Dev

C ++ std :: regex与预期的不匹配

来自分类Dev

带有std :: regex的大型程序C ++

来自分类Dev

内存位置的std :: regex_error

来自分类Dev

如何使用std :: regex匹配多个结果

来自分类Dev

std :: regex_replace期间堆栈溢出

来自分类Dev

使用 std::regex 按点解析数字

来自分类Dev

<regex> std::regex 等价于 Qt 的 QRegularExpression::isValid() 而不会触发异常

来自分类Dev

std :: regex_match和std :: regex_search之间的区别?

来自分类Dev

std :: regex是否支持不区分大小写的“(?i)”?

来自分类Dev

C ++的std :: regex是否等效于Python的re.MULTILINE?

来自分类Dev

文件路径中的std :: regex转义反斜杠

来自分类Dev

抛出'std :: regex_error'实例后调用终止

来自分类Dev

运行时抛出std :: regex_error异常

来自分类Dev

C ++ std :: regex垃圾管道error_escape异常

来自分类Dev

VS2010的std :: regex_match问题

来自分类Dev

C ++。如何使用std :: regex替换括号内的任何内容?

来自分类Dev

C ++使用std :: regex逐行拆分字符串