Issue
I’m using ANTLR to make a simple language, and right now I’m trying to implement escape characters into my strings, what I have now is:
String
: ["] ( ~["\r\n\\] .*? | .*? '\\' ~[\r\n] )* ["]
;
However, whenever I feed it an input like
"Test 1234\n"
It discards the \
, leaving me with Test 1234n
, which won’t work. Is there a RegEx to match any String and keep the back slashes?
Solution
Here’s a regex that matches a quoted string of letters, numbers, spaces and backslashes but only when the backslash is followed by r or n:
"(\\[rn]|[a-zA-Z0-9 ])*"
See live demo.
Answered By – Bohemian
Answer Checked By – Cary Denson (BugsFixing Admin)