Trying to use backslashes in raw strings with this regular expression:
import re
print re.sub(r'^[a-zA-Z]:\\.+(\\Data.+)', r'D:\folder\1', r'C:\Some\Path\Data\File.txt')
Expected output:
D:\folder\Data\File.txt
However \f is being interpreted. Is there any way to make this work without converting to forward slashes?
re.sub interprets escape sequences in the replacement string (docs). Adding an extra backslash before the \f to escape the backslash seems to do the trick:
If your replacement string is dynamic, you can always use another regexp to escape backslashes, or use str.encode(‘unicode-escape’).