Raw string in Python regex using windows folder path

Trying to use backslashes on raw lines with this regex:

import re
print re.sub(r'^[a-zA-Z]:\\.+(\\Data.+)', r'D:\folder\1', r'C:\Some\Path\Data\File.txt')

      

Expected Result:

D:\folder\Data\File.txt

      

However, it is \f

interpreted. Is there a way to make this work without converting to forward slashes?

+3


source to share


2 answers


re.sub interprets the escape sequences in the replacement string ( docs ). Adding an extra backslash before the \ f to avoid the backslash seems to do the trick:

import re
print re.sub(r'^[a-zA-Z]:\\.+(\\Data.+)', r'D:\\folder\1', r'C:\Some\Path\Data\File.txt')

      



If your replacement string is dynamic, you can always use a different regex to remove backslashes, or use str.encode ('unicode-escape').

+2


source


To avoid translating special characters, you can use a lambda function:



print re.sub(r'^[a-zA-Z]:\\.+(\\Data.+)', lambda x: r'D:\\folder\1', r'C:\Some\Path\Data\File.txt')    

      

+2


source







All Articles