0

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 
  • what exception are you getting? – twSoulz Aug 12 '22 at 00:03
  • So you make a string that contains t e s t \ and convert that string to a regex. is it a valid regex syntax? Or does the \ need to be scaped? – user253751 Aug 12 '22 at 00:08
  • *Am I doing something wrong?* -- You should have used `try/catch` and looked at the exception being thrown, plus the details (the `what()` value) instead of leaving it unhandled. If you had done that, then [this](https://godbolt.org/z/MojY4jss4) would have been the result. – PaulMcKenzie Aug 12 '22 at 00:09
  • BTW, any function that throws an exception, you have all the information about why it was thrown by using `try/catch` and inspecting the `what()` value. The library author(s) should have documented that the function you're calling may throw an exception, which in that case you're responsible in catching it if it is thrown. Of course you can ignore it and let it go unhandled, but you then wind up in the situation you are in now, and that is not knowing what the reason is for the exception. – PaulMcKenzie Aug 12 '22 at 00:16
  • How can I catch the exception? After the exception is thrown I can't keep running my program –  Aug 12 '22 at 00:19
  • *How can I catch the exception?* --By using [`try` and then `catch`](https://godbolt.org/z/5xTjdTrqq). Are you not familiar with `try/catch`? – PaulMcKenzie Aug 12 '22 at 07:59
  • Nevermind, I read it up but couldn't catch the exceptions because my error-type in the catch was wrong. Thanks a lot for the tip, I wasn't familiar with it before. –  Aug 12 '22 at 15:49

1 Answers1

0

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\\)" };
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • that makes sense, thank you. I saw the raw string variant, but I get an error when I use it. "C++ parenthesis terminating raw string delimiter not found within 16 characters -- raw string indicator ignored" –  Aug 12 '22 at 00:19
  • @noergel1 while I can not see your code, the error would suggest that you forgot or misplaced the `)`, which is the _parenthesis terminating raw string delimiter_. – Drew Dormann Aug 12 '22 at 11:24