How can I regex-escape a dynamically inputted string. I would like to surround it with actual regex code and then do matching, but I need all regex-special characters from the input escaped.
Asked
Active
Viewed 1,078 times
6
-
1@The Paramagnetic Croissant Why not? Other languages have it and it could simplify my code. – Petr Skocik Jul 23 '15 at 18:07
-
if you escape all the regex metacharacters, then what you are really doing is plain string matching, and you don't need to use regexes at all. – The Paramagnetic Croissant Jul 23 '15 at 18:08
-
1As I said, I want to surround it with actual unescaped regex code. – Petr Skocik Jul 23 '15 at 18:11
-
1Here is [almost what you need](http://stackoverflow.com/a/1253004/3832970). – Wiktor Stribiżew Jul 23 '15 at 18:26
-
Boost.Regex supports PCRE escaping via `\Q`...`\E`. – wOxxOm Jul 23 '15 at 18:56
-
You will do the same as what is done in JavaScript: http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript – nhahtdh Jul 24 '15 at 03:14
-
does C++ support \Q\E ? http://www.regular-expressions.info/characters.html I use a lot of Perl and that is my go to for literals. – sniperd May 31 '17 at 13:58
1 Answers
1
You can use raw string literal
string LitString = R"(^(?:[1-9]\d*?|0)?(?:\.(?:\d*?[1-9]|0))?(?<=[\d])$)";

doom87er
- 458
- 2
- 8