【问题标题】:C++11 regex::icase inconsistent behaviorC++11 regex::icase 不一致的行为
【发布时间】:2016-10-11 23:45:09
【问题描述】:

来自类似 perl 的正则表达式,我希望下面的代码在所有 8 种情况下都能匹配正则表达式。但事实并非如此。我错过了什么?

#include <iostream>
#include <regex>
#include <string>

using namespace std;

void check(const string& s, regex re) {
    cout << s << " : " << (regex_match(s, re) ? "Match" : "Nope") << endl;
}

int main() {
    regex re1 = regex("[A-F]+", regex::icase);
    check("aaa", re1);
    check("AAA", re1);
    check("fff", re1);
    check("FFF", re1);
    regex re2 = regex("[a-f]+", regex::icase);
    check("aaa", re2);
    check("AAA", re2);
    check("fff", re2);
    check("FFF", re2);
}

使用 gcc 5.2 运行:

$ g++ -std=c++11 test.cc -o test && ./test
aaa : Match
AAA : Match
fff : Nope
FFF : Match
aaa : Match
AAA : Match
fff : Match
FFF : Nope

【问题讨论】:

  • 这看起来像一个 libstdc++ 错误。我认为他们在检查 other 情况时只检查指定范围内的第一个字符。请参阅此example,我将范围更改为"C-Z",然后"ccc" 匹配,但其他一切都失败了。带有 libc++ 的 clang 的行为符合您的预期。
  • 作为一种解决方法,我们在为 gcc 编译时使用 boost::regex。
  • 在 std::regex 中观察到了类似的问题。如果您需要字符类不区分大小写,请使用 Boost。
  • #71500 正在讨论这个问题。
  • c++ 是否支持模式修饰符?你可以试试这个解决方法(?i)[A-F]+

标签: c++ regex c++11 gcc gcc5.2


【解决方案1】:

对于未来的用户,这已被确认为一个错误,并且它(和类似的问题)在 8.1 中已根据this 线程得到解决。

我怀疑原作者参与了引起人们注意的事情,但我想也许我们可以解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-04
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    相关资源
    最近更新 更多