0

I have this string: feed_\\d{6}.txt.gz and I need to replace the double backslash with a single backslash. I have looked at other answers (including How to replace a double backslash with a single backslash in python?) but they haven't helped.

Here are things that don't work

mystr = 'feed_\\d{6}.txt.gz'
mystr.replace('\\', '\')
# invalid syntax. string is unterminated.

mystr.replace('\\\\', '\\')
#returns the original string

import codecs
codecs.escape_decode(mystr)
# returns a tuple ('feed_\\d{6}.txt.gz', 17)
# Got this from the aforementioned other thread but it doesn't 
# actually explain how to use it

I'm stumped. Any ideas?

jamiet
  • 10,501
  • 14
  • 80
  • 159
  • Can you provide an example string that fails with your attempts? – chevybow Oct 12 '18 at 14:20
  • 7
    `'feed_\\d{6}.txt.gz'` does not contain two backslashes. It contains one backslash. It _looks_ like two backslashes in string literal form or when it's displayed in a REPL, because that's how a backslash is represented via `repr`. But any logic that inspects individual characters of the string will only see one backslash. Similarly, `print('feed_\\d{6}.txt.gz')` will display `feed_\d{6}.txt.gz`. – Kevin Oct 12 '18 at 14:21

0 Answers0