Date: 2018nov16
Date: 2026aug8
Language: C++
Q. C/C++: How should I quote a regular expression in C++
A. Use raw strings!
They have been in the language since C++11.
In a raw string, escape characters are not processed.
For example:
main() {
const char *regex_raw = R"([\r\n]+)";
const char *regex_old = "[\\r\\n]";
}
As you can see the backslashes don't need to be escaped in the raw string.
This is great to avoid typing but even better it improves clarity.
----
You can also use raw strings to embed multi-line scripts from another language in your program:
const char *code = R"
line one
line two
";
You can use another delimiter, eg:
const char *saying = R|He said "hello"|;
Since C++23 you can use #embed to pull in binary files - like an image.