Is gcc 4.8 or earlier buggy about regular expressions?(gcc 4.8 或更早版本对正则表达式有问题吗?)
问题描述
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;
}
outputs:
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
or
g++ *.cc -o test -std=gnu++0x
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?
Update one possible solution is to use groups to implement multiple alternatives, e.g. (st|mt)|tr
.
<regex>
was implemented and released in GCC 4.9.0.
In your (older) version of GCC, it is not implemented.
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.
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.
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.
Seriously though, who though that shipping an implementation of regex_search that only does "return false" was a good idea?
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 或更早版本对正则表达式有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:gcc 4.8 或更早版本对正则表达式有问题吗?
基础教程推荐
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++,'if' 表达式中的变量声明 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 设计字符串本地化的最佳方法 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01