I am trying to replace characters in a string using re.sub. However, i am unable to replace \ in the string.
import re
find = '[/:*\?"<>|]'
s = "he*l\l/*o<>|"
rp = re.sub(find,"a",s)
print(rp)
output
heal\laaoaaa
Is this a normal behavioir for regex re.sub function?
CodePudding user response:
The problem is with escape character \
.
Please it with [/:*\\\?"<>|]
CodePudding user response:
you need to escape the \
character you do this by doing \\
this will allow the regex to replace the \
this is because the \
character is used for escaping character such as the "
in a string so to use the \
character you have to first escape itself.
making your fixed search string
find = '[/:*\\?"<>|]'