Replace escape characters like n t etc with \t , \n

Question:

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;lfkjaldsfndsklajfladtkjhklajfn'))
ads;lfkjaldsfndsklajfladtkjhklajfn

Suppose I have text say "abcdnght" 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.

Asked By: Akshay Hazari

||

Answers:

I found re.escape as pointed to by Karoly Horvath. This is how it works.

>>> re.escape('ads;lfkjaldsfndsklajfladtkjhklajfn')
'ads\;lfkjaldsf\ndsklajflad\tkjhklajf\n'

Update:

While I see re.escape escapes a lot too much. Spaces , semicolons and lot many characters which don’t need to be escaped in my case.

>>> re.sub(r'(n|t|"|')',lambda m:{'n':'\n','t':'\t',''':'\'','"':'\"'}[m.group()], "hello hi  n 'GM' t TC  n "Bye" t")
'hello hi  \n \'GM\' \t TC  \n \"Bye\" \t'

This is what I figured out which really helped.

Answered By: Akshay Hazari
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.