2

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.

1
  • You need to compare it with "\\n" instead of "\n" Commented Sep 23, 2015 at 14:52

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.