4 ms·
You don't need to escape slashes if you use a regexp string like this: r'no need to escape slash\n'.
by edward 13y ago
You don't need to escape slashes if you use a regexp string like this: r'no need to escape slash\n'.
- sheetjs 13y agoVery cool! In this case, since the injected text will end in a newline, that approach will work (and it appears to work with r"""triple-quoted\'s""" But I do see syntax errors if the bare slash is at the end: >>> r'slash at end\' File "<stdin>", line 1 r'slash at end\' ^ SyntaxError: EOL while scanning string literal
- cstejerean 13y agoYeah, that's about the only restriction. From the docs: Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character).
- swinglock 13y agoIt's called a "raw string" rather than "regexp string", though that is the common use for it.
- bobbyi_settv 13y agoThe other common use is Windows paths since traditionally backslashes are used as the delimiter. (Obviously hardcoded paths in code aren't generally a great idea, but that's another story.)