在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效

problem with use regex in c++ error Invalid range in bracket expression

本文关键字:表达式 问题 无效 范围 C++ 正则表达式 错误      更新时间:2023-10-16

我的代码是使用正则表达式匹配组进行 5 件事 这是我的代码:

#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main(){

try{
string input("(66+89i)+(12+5i)");
regex re("([\d]+)([+\-]?[\di]+)\)([+\-*=\/!]+)\(([\d]+)([+\-]?[\di]+)");
smatch match;
if (regex_search(input, match, re)) {
cout << "x1 : " << match.str(1)<<endl;
cout << "y1 : " << match.str(2)<<endl;
cout << "operator : " << match.str(3)<<endl;
cout << "x2 : " << match.str(4)<<endl;
cout << "y2 : " << match.str(5)<<endl;
}
else {
cout << "No match is found" << endl;
}

} catch (std::regex_error r) {
cout<<r.what();
}

return 0;
}

当我运行此代码时,我收到此错误" 括号表达式中的范围无效"。 我该如何解决这个问题?

更新:这段代码在 c++17 中有效,但在 c++14 中我收到错误! 在此编译器链接中不起作用

请注意,这是g++ 7.x中针对所有标准修复的错误,C++1z/C++17 中从未出现过。onlinegdb平台使用旧的g++ 5.4.1版本,其中错误仍然存在(请参阅 @brc-dd 注释(。

请参阅 Bug 77356 - ECMAScript 语法字符串的正则表达式错误和 bug 77469 - std::regex x("[b\-a]"( 抛出消息"括号表达式中的范围无效"。这个想法是 ECMAScript 语法允许在字符类内的任何位置使用转义连字符,而 POSIX 标准不允许这样做(它不允许在括号表达式内进行正则表达式转义(。

这里的问题是,即使在字符类中转义连字符,正则表达式也不会匹配。在此处使用连字符的唯一方法是将其放在字符类的开头或结尾。

此外,您最好使用原始字符串文字来定义正则表达式模式。

您可以使用

regex re(R"~~~((d+)([+-]?[di]+))([+*=/!-]+)((d+)([+-]?[di]+))~~~");

更新演示的输出:

x1 : 66
y1 : +89i
operator : +
x2 : 12
y2 : +5i

这里

  • R"~~~(...)~~~"- 一个原始字符串文字声明,这里是文字反斜杠,不形成字符串转义序列(所以,t不是制表符,它是一个双字符组合,t,尽管它仍然会匹配制表符(
  • (d+)- 第 1 组:一个或多个数字
  • ([+-]?[di]+)- 第 2 组:可选的+-,然后是 1+ 位数字或i字符
  • )-)
  • ([+*=/!-]+)- 第3组:一个或多个+*=/!-
  • (-(
  • (d+)- 第 4 组:1+ 位数字
  • ([+-]?[di]+)- 第 5 组:可选的+-,然后是 1+ 位数字或i字符

注意:您不需要转义/字符,它不是正则表达式中的任何特殊正则表达式元字符。由于C++模式是用字符串文字定义的,而不是经常使用/作为正则表达式分隔符字符的正则表达式文字,因此您不需要在此处将其放在前面

似乎 C++14 在正则表达式解析器中有一个错误.
任何转义的破折号"[\-]"无论它被解析到哪里
一个未转义的破折号[-],即范围运算符.
这是一个错误

但是,如果它位于类的
开头或结尾,它仍然尊重它是一个文字破折号。

C++l7(作品(https://onlinegdb.com/S1rH3rCRI

C++L4(错误(https://onlinegdb.com/Bya3jSR0I

C++

#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main(){

try{ string rs1 = "[+\-*=/!]"; cout<<rs1<<" --> "; regex re( rs1 );  } catch (std::regex_error r) {
cout<<"The error is: "<<r.what();
}
try{ string rs2 = "[+\-]"; cout<<"n"<<rs2<<" --> "; regex re( rs2 ); } catch (std::regex_error r) {
cout<<"The error is: "<<r.what();
}
try{
string rs3 = "[\-+]"; cout<<"n"<<rs3<<" --> "; regex re( rs3 ); } catch (std::regex_error r) {
cout<<"The error is: "<<r.what();
}
try{
string rs4 = "[+-]"; cout<<"n"<<rs4<<" --> "; regex re( rs4 ); } catch (std::regex_error r) {
cout<<"The error is: "<<r.what();
}
try{
string rs5 = "[-+]"; cout<<"n"<<rs5<<" --> "; regex re( rs5 ); } catch (std::regex_error r) {
cout<<"The error is: "<<r.what();
}
try{ string rs6 = "[-+*=/!]"; cout<<"n"<<rs6<<" --> "; regex re( rs6 );  } catch (std::regex_error r) {
cout<<"The error is: "<<r.what();
}
return 0;
}

结果 (C++14(

[+-*=/!] --> The error is: Invalid range in bracket expression.                                                                 
[+-] -->                                                                                                                        
[-+] -->                                                                                                                        
[+-] -->                                                                                                                         
[-+] -->                                                                                                                         
[-+*=/!] -->