Search code examples
c++boostboost-regex

Exception when escaping "\" in Boost Regex


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 

Solution

  • 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\\)" };