![]() |
regular expression - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: regular expression (/thread-10726.html) |
regular expression - bluefrog - Jun-03-2018 Hi How can one escape the "\" character? Escaping the "^" character works fine >>> import re >>> re.findall(re.escape("^"), "^hello^") ['^', '^']But when escaping the "\" character I get an error: >>> re.findall(re.escape("\"), "\hello\") File "<stdin>", line 1 re.findall(re.escape("\"), "\hello\")Thanks Hi discovered the answer here RE: regular expression - perfringo - Jun-09-2018 From Python documentation: re - Regular expression operations:: Quote:The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So r"\n" is a two-character string containing '\' and 'n', while "\n" is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation. |