Can someone explain this weird behavior of python re? Obviously the string ‘test’ does not contain either ‘Bid’ or ‘Ask’. Why is this matching?
import re
pat=r'[Bid|Ask]'
reg=re.compile(pat)
if reg.search('test'): print "matched!"
…
matched!
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.
[...]defines a character class, matching any character listed inside. What you wanted ispar = r'(Bid|Ask)'.However, you should not use regex for this at all, do the following instead:
If you need to perform a substring check (thanks @agf):