Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You don't need to escape slashes if you use a regexp string like this: r'no need to escape slash\n'.


It's called a "raw string" rather than "regexp string", though that is the common use for it.


The 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.)


Very 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


Yeah, 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).




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: