Home > Enterprise >  Exception when escaping "\" in Boost Regex
Exception when escaping "\" in Boost Regex

Time:08-12

I'm always getting an exception when trying to escape a backslash like this:

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

Am I doing something wrong?

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

CodePudding user response:

A literal backslash would be

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

In a C string, the characters \\ represent a single backslash character.

To represent a regex literal (escaped) backslash, you would need two of those.

If this looks confusing, you can also use a C 11 raw string literal.

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