Normally you would put an r in front of the string to make it raw, but how to do this with a variable (string)?
This is what I tried so far:
import re
var = "++"
re.search(r"++", "++") # also does not work
re.search(var, "++") # fails
re.search(r(var), "++") # fails
re.search(r + var, "++") # fails
re.search("r" + var, "++") # fails
Use the re.escape() function for this.
>>> import re
>>> var = "++"
>>> re.search(re.escape(var), '++')
<_sre.SRE_Match object at 0x02B36B80>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With