Long regex expression causes error

2020-05-01 03:52发布

问题:

This

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

line causes this

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.

but this

std::regex line("abc");

does not.

The long expression works here: https://www.myregextester.com/index.php

I'm just trying to get 3 consequent floating point values between other data.


Visual studio 2015 community edition debug 64 bit. Windows 10.

回答1:

You either need to escape the back slashes with \\ (two for one), or use a raw string literal like this:

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

Raw string literals surround the string with (at least) R"( and )".

Read more about raw string literals HERE - Syntax (6).



标签: c++ regex std