I am trying to write a regex in python where I wish to replace the escape characters like \n
, \t
, etc with \\n
, \\t
etc.
I have tried this to just escape the newline and tabs.
re.sub(r'\t',r'\\t',re.sub(r'\n',r'\\n',text))
eg:
>>> print re.sub(r'\t',r'\\t',re.sub(r'\n',r'\\n','ads;lfkjaldsf\ndsklajflad\tkjhklajf\n'))
ads;lfkjaldsf\ndsklajflad\tkjhklajf\n
Suppose I have text say "\a\b\c\d\n\g\h\t"
then it need not add double backslashes to non escape characters.
So here I don't need to escape every backslash with a double backslash but every special escape character with double backslash.
Any help is appreciated.