1

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.

Akshay Hazari
  • 2,995
  • 3
  • 40
  • 76

1 Answers1

2

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

>>> re.escape('ads;lfkjaldsf\ndsklajflad\tkjhklajf\n')
'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.

Akshay Hazari
  • 2,995
  • 3
  • 40
  • 76