Answer the question
In order to leave comments, you need to log in
Why doesn't OR work in re python?
I have a set of SQL queries. I want to extract the WHERE clause from them.
Wrote a small script. I will give an example with one of the requests.
s = '''FROM clinics INNER JOIN clinic_drug on clinics.clinicId = clinic_drug.clinicId
WHERE clinic_drug.amount = 0
GROUP BY clinics.name;'''
import re
pattern = re.compile(r'WHERE[.\s\t]*(GROUP BY|;)')
text = pattern.findall(s)
print(text)
Answer the question
In order to leave comments, you need to log in
re.compile(r'WHERE(.+?)(GROUP BY|;)', re.S)
But for more precision, it's better to add '\b' tokens to limit words, otherwise WHERE might catch as part of another word as well.
You're looking for the following:
WHERE followed by 0 or more dots, whitespace (including tabs) or tabs followed by either GROUP BY or ;
What exactly are you trying to extract from the string?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question