K
K
k0r0g2021-10-02 15:15:40
Python
k0r0g, 2021-10-02 15:15:40

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)


Returns empty quotes.
Please help me formulate the pattern.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
O
o5a, 2021-10-02
@k0r0g

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.

V
Vindicar, 2021-10-02
@Vindicar

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?

A
Alexander Karabanov, 2021-10-02
@karabanov

Train here https://regex101.com/

D
DENIS Kokorev, 2021-10-02
@shmaroder

Hold on . So?
61588470c2c12518405645.jpeg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question