C++ 这是谁的bug-叮当声还是gcc? #包括 #包括 #包括 使用名称空间std; bool IsMatched() { 字符串str=R“(Liy_0-3863)”; 字符串re=R“([:\-\u a-zA-Z\d]+)”; 自动标志=std::regex_常量::ECMAScript; 返回std::regex_match(str.data(), std::regex(re.data(),re.size(),flags)); } int main() { cout

C++ 这是谁的bug-叮当声还是gcc? #包括 #包括 #包括 使用名称空间std; bool IsMatched() { 字符串str=R“(Liy_0-3863)”; 字符串re=R“([:\-\u a-zA-Z\d]+)”; 自动标志=std::regex_常量::ECMAScript; 返回std::regex_match(str.data(), std::regex(re.data(),re.size(),flags)); } int main() { cout,c++,c++11,gcc,clang,standards,C++,C++11,Gcc,Clang,Standards,g++(或者更确切地说是stdlibc++)存在错误 根据ECMAScript规范,转义的减号字符应该在字符类中按字面意思处理。libstdc++无法做到这一点。可以在一个更简单的示例中查看 #include <regex> #include <string> #include <iostream> using namespace std; bool IsMatched() { string str = R"(Liy_0-3863)"; s

g++(或者更确切地说是stdlibc++)存在错误

根据ECMAScript规范,转义的减号字符应该在字符类中按字面意思处理。libstdc++无法做到这一点。可以在一个更简单的示例中查看

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

using namespace std;

bool IsMatched()
{
    string str = R"(Liy_0-3863)";
    string re = R"([:\-_a-zA-Z\d]+)";

    auto flags = std::regex_constants::ECMAScript;
    return std::regex_match(str.data(),
        std::regex(re.data(), re.size(), flags));
}

int main()
{
    cout << boolalpha << IsMatched();
}

gcc说没有匹配项,不同的正则表达式测试人员说有。

gcc trunk打印出
true
,所以我认为答案是显而易见的。问题标题非常容易误导。这个问题永远无法通过搜索找到。请使标题更能描述问题,并将正则表达式添加到关键字中。@Ryan就是这个问题。@Ryan\-
   string: a-b
   regex: [a\-b]+