I have a CharField storing user input, including things like "\n" (literally, not by hitting "enter"), which is later used for parsing text with regular expressions. When I fetch the model from the database, however, comparing the value to '\n' fails - it's equal to '\n' instead. How do I unescape it? I do escape it later before insertion into the regular expression, since the field may contain e.g. "*", which I want to be interpreted literally. I tried mark_safe
but no luck.
Asked
Active
Viewed 901 times
2

code_assassin
- 123
- 7
-
You need to compare it with "\\n" instead of "\n" – Kartik Anand Sep 23 '15 at 14:52
1 Answers
1
OK I figured it out, thanks to the answers in this question: Python Replace \\ with \
Basically, I needed to do something like that:
separator = processor.separator.decode('string_escape') # Decode
expr = r"<something>" + re.escape(separator) # Escape for the regular expression

Community
- 1
- 1

code_assassin
- 123
- 7