Python how to replace backslash with re.sub()
python, python-2.7
Solution
You need a quadruple backslash:
newstr = re.sub(mystr1 + "\\\\", "", myfile)
Reason:
- Regex to match a single backslash: `\\`
- String to describe this regex: `"\\\\"`.
Or you can use a raw string, so you only need a double backslash: `r"\\"`
Problem
I have the following string ``` mystr1 = 'mydirname' myfile = 'mydirname\myfilename' ``` I'm trying to do this ``` newstr = re.sub(mystr1 + "\","",myfile) ``` How do I escape the backslash I'm trying to concatenate to mystr1?