I thought this should work, but it doesn't:
import re
if re.match("\Qbla\E", "bla"):
print "works!"
Why it doesn't work? Can I use the '\Q' and '\E' symbols in python? How?
Python's regex engine doesn't support those; see §7.2.1 "Regular Expression Syntax" in the Python documentation for a list of what it does support. However, you can get the same effect by writing re.match(re.escape("bla"), "bla")
; re.escape
is a function that inserts backslashes before all special characters.
By the way, you should generally use "raw" strings, r"..."
instead of just "..."
, since otherwise backslashes will get processed twice (once when the string is parsed, and then again by the regex engine), which means you have to write things like \\b
instead of \b
. Using r"..."
prevents that first processing pass, so you can just write \b
.
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