是关于正则表达式的gcc 4.8或更早的bug吗? [英] Is gcc 4.8 or earlier buggy about regular expressions?

查看:391
本文介绍了是关于正则表达式的gcc 4.8或更早的bug吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C ++ 11代码中使用std :: regex,但它似乎支持有点儿车。示例:

I am trying to use std::regex in a C++11 piece of code, but it appears that the support is a bit buggy. An example:

#include <regex>
#include <iostream>

int main (int argc, const char * argv[]) {
    std::regex r("st|mt|tr");
    std::cerr << "st|mt|tr" << " matches st? " << std::regex_match("st", r) << std::endl;
    std::cerr << "st|mt|tr" << " matches mt? " << std::regex_match("mt", r) << std::endl;
    std::cerr << "st|mt|tr" << " matches tr? " << std::regex_match("tr", r) << std::endl;
}

输出:

st|mt|tr matches st? 1
st|mt|tr matches mt? 1
st|mt|tr matches tr? 0
when compiled with gcc (MacPorts gcc47 4.7.1_2) 4.7.1, either with 
g++ *.cc -o test -std=c++11
g++ *.cc -o test -std=c++0x

g++ *.cc -o test -std=gnu++0x

此外,如果我只有两个替代模式,正则表达式工作很好,例如 st | mt ,因此看起来最后一个因为某些原因不匹配。该代码适用于Apple LLVM编译器。

Besides, the regex works well if I only have two alternative patterns, e.g. st|mt, so it looks like the last one is not matched for some reasons. The code works well with the Apple LLVM compiler.

有关如何解决这个问题的任何想法?

Any ideas about how to solve the issue?

strong> Update 一个可能的解决方案是使用组来实现多个选项,例如(st | mt)| tr

Update one possible solution is to use groups to implement multiple alternatives, e.g. (st|mt)|tr.

推荐答案

在您的(较旧版本)中,您已经在GCC 4.9.0中实施和发布了code>< regex> 未实施。

In your (older) version of GCC, it is not implemented.

当所有GCC的C ++ 0x支持高度实验性时,添加原型< regex> 代码,跟踪早期C + + 0x草稿,并提供给人们进行实验。这使得人们在标准定稿之前找到问题并向标准委员会提供反馈。当时许多人感激在C ++ 11完成之前,许多其他编译器提供了任何支持之前已经访问出血边缘功能,并且反馈真的帮助提高C ++ 11 。这是一个Good Thing TM

That prototype <regex> code was added when all of GCC's C++0x support was highly experimental, tracking early C++0x drafts and being made available for people to experiment with. That allowed people to find problems and give feedback to the standard committee before the standard was finalised. At the time lots of people were grateful to have had access to bleeding edge features long before C++11 was finished and before many other compilers provided any support, and that feedback really helped improve C++11. This was a Good ThingTM.

< regex> 在一个有用的状态,但被添加为一个工作在进行中像许多其他位的代码在当时。

The <regex> code was never in a useful state, but was added as a work-in-progress like many other bits of code at the time. It was checked in and made available for others to collaborate on if they wanted to, with the intention that it would be finished eventually.

这是开源工作的常见方式:

That's often how open source works: Release early, release often -- unfortunately in the case of <regex> we only got the early part right and not the often part that would have finished the implementation.

库的大部分更完整,现在几乎完全实现,但< regex> hadn'

Most parts of the library were more complete and are now almost fully implemented, but <regex> hadn't been, so it stayed in the same unfinished state since it was added.


严重的是,虽然运送一个regex_search的实现,只有return false才是一个好主意?

Seriously though, who though that shipping an implementation of regex_search that only does "return false" was a good idea?

几年前这不是一个坏主意, + 0x仍然是一个进展中的工作,我们发运了很多部分实现。没有人认为它将永远不能使用这么久,事后来看,也许它应该已被禁用,需要一个宏或内部时间选项来启用它。但那艘船很久以前就航行了。有来自依赖于正则表达式代码的 libstdc ++。so 库的导出符号,所以简单地删除它(例如GCC 4.8)不会是微不足道的。

It wasn't such a bad idea a few years ago, when C++0x was still a work in progress and we shipped lots of partial implementations. No-one thought it would remain unusable for so long so, with hindsight, maybe it should have been disabled and required a macro or built-time option to enable it. But that ship sailed long ago. There are exported symbols from the libstdc++.so library that depend on the regex code, so simply removing it (in, say, GCC 4.8) would not have been trivial.

这篇关于是关于正则表达式的gcc 4.8或更早的bug吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆