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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.escapeis 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\\binstead of\b. Usingr"..."prevents that first processing pass, so you can just write\b.