c++ - 长正则表达式会导致错误

标签 c++ regex std

这个

std::regex line("[\s]+\+?[0-9]+.[0-9]+[\s]+\+?[0-9]+.[0-9]+[\s]+\+?[0-9]+.[0-9]+[\s]");

线路导致此问题

Exception thrown at 0x00007FFE39E69E08 in DosyaOkuHizli.exe: Microsoft C++ exception: std::regex_error at memory location 0x000000F751EFEAB0.
Exception thrown at 0x00007FFE39E69E08 in DosyaOkuHizli.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
Exception thrown at 0x00007FFE39E69E08 in DosyaOkuHizli.exe: Microsoft C++ exception: std::regex_error at memory location 0x000000F751EFEAB0.
Unhandled exception at 0x00007FFE39E69E08 in DosyaOkuHizli.exe: Microsoft C++ exception: std::regex_error at memory location 0x000000F751EFEAB0.

但是这个

std::regex line("abc");

没有。

长表达式在这里起作用:https://www.myregextester.com/index.php

我只是想获取其他数据之间的 3 个后续浮点值。


Visual Studio 2015 社区版调试 64 位。 Windows 10。

最佳答案

您需要使用 \\ 转义反斜杠(两个为一),或者使用像这样的原始字符串文字:

regex line{R"([\s]+\+?[0-9]+.[0-9]+[\s]+\+?[0-9]+.[0-9]+[\s]+\+?[0-9]+.[0-9]+[\s])"};

原始字符串文字(至少)用 R"()" 包围字符串。

了解有关原始字符串文字的更多信息 HERE - 语法 (6)。

关于c++ - 长正则表达式会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44705302/

相关文章:

c++ - "no match for ' 运算符<< ' (operand types are ' __FILE* ' {aka ' __sFILE64* '} and ' MyObject ' ) error C++"

c++ - 使用复制昂贵的类作为 std::map 中的映射值是否有效?

c++ - 类内的 stable_sort

c++ - Qt禁用触摸屏检测

python - 使用交替运算符匹配多个正则表达式模式?

java - Scanner.useDelimiter() 正则表达式 java

c++ - ISO C++ 禁止声明没有类型的 ‘tuple’

c++ - 如何根据 C++11 中的特定条件从 vector 中删除元素

c++ - valgrind 和 std::unique_ptr --- 误报与否?

ruby - 用于替换反斜杠的正则表达式?