I'm a little confused by raw-string search with Python regex. My code snippet goes like this:
import re
text = r"aaa\r\nbbb\r\nccc\r\n" # I am going to replace '\r\n' into '\n'
text1 = re.sub(r"\r\n", "\n", text) # but, text1 remains identical with text
text2 = re.sub(r"\\r\\n", "\n", text) # while text2 get what I expected
Per my understanding, text is stored in memory like "aaa\\r\\nbbb\\r\\nccc"
, in which "\\"
is a single char of '\'
. So text1 should have worked as I wanted, because r"\r\n"
will be like "\\r\\n"
in memory and match with text. However, it is not true.
What is wrong with my understanding about raw-string in this situation? Thanks.