Bug 20217 – Regex literals don't escape double quotes

Status
RESOLVED
Resolution
INVALID
Severity
minor
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2019-09-16T20:30:59Z
Last change time
2019-09-16T20:42:11Z
Assigned to
No Owner
Creator
MarkSill

Comments

Comment #0 by marksillweb — 2019-09-16T20:30:59Z
You can't create a regex literal that contains an escaped double quote in it. Example: import std.regex; import std.stdio; void main() { auto re = regex(r"\""); // r`"` doesn't work either writeln(re); } Errors: source/app.d(4,16): Error: unterminated string constant starting at source/app.d(4,16) source/app.d(4,16): Error: Implicit string concatenation is deprecated, use "\\" ~ "" instead source/app.d(7,1): Error: semicolon expected following auto declaration, not End of File source/app.d(7,1): Error: found End of File when expecting } following compound statement
Comment #1 by destructionator — 2019-09-16T20:34:00Z
Note that there's no such thing as a regex literal in D, they are just different types of strings. The r"xxx" string specifically does not have escaping. If you want that, you can try plain "" strings. Or if you want to go raw, use the `string`. But the key thing to remember is they are all strings, there's nothing special about using it with the regex function.
Comment #2 by marksillweb — 2019-09-16T20:42:11Z
Ah, that makes much more sense. It wasn't clear to me that they were the same when I was reading https://dlang.org/articles/regular-expression.html, though reading it again, there are two examples of using regex without the r"" prefix.