问题
创建对象std::regex会导致语法异常,但我和在线服务均未在初始化表达式中看到语法错误。
例子
#include <regex>
#include <iostream>
using namespace std;
int main() {
try {
regex R("^([^;]+);(?:\\s*([^=]+)=((?<q>\"?)([^\"]*)\\k<q>);?)*$", regex_constants::ECMAScript);
}
catch (const regex_error& e) {
cout << "regex_error caught: " << e.what() << '\n';
if (e.code() == regex_constants::error_brack) {
cout << "The code was error_brack\n";
}
}
return 0;
}
结论
regex_error caught: regex_error(error_syntax)
笔记
- 没有屏幕截图:
^([^;]+);(?:\s*([^=]+)=((?<q>"?)([^"]*)\k<q>);?)*$ - 在regex101.com 上
问题
- 这个问题的原因是什么?
- 如果是语法错误,那么也许您知道有助于找到此类技巧的服务?
std::regex未实现对命名捕获组的支持。有必要用编号的捕获组替换它们。使用原始字符串文字定义正则表达式也更方便,因此只使用一个反斜杠字符。
您的表达式声明可以写成
组“q”被组#4 替换。
R"(...)"- 字符串文字的功能部分,模板本身在括号内。请参阅C++ 代码示例: