在加速正则表达式中转义""时异常

Exception when escaping "" in Boost Regex

本文关键字:异常 转义 加速 正则表达式      更新时间:2024-09-23

当我试图转义这样的反斜杠时,总是会遇到异常:

boost::regex shaderRegex{ "test\" };

我做错什么了吗?

Unhandled exception at 0x00007FFD13034FD9 in project.exe: Microsoft C++ 
exception: boost::wrapexcept<boost::regex_error> at memory location 

文字反斜杠应该是

boost::regex shaderRegex{ "test\\" };

在C++字符串中,字符\表示单个反斜杠字符。

要表示regex文本(转义(反斜杠,您需要其中两个。

如果这看起来令人困惑,您也可以使用C++11原始字符串文字。

boost::regex shaderRegex{ R"(test\)" };