I have been trying to extract the SQL query in a multi-line text but all the time I am getting wrong output.
How to get the text between one or three quotes?
Note: there can be anything before and after first complete quotes ''
, ""
, """"""
, ''''''
and I am only interested finding the first text between the quotes.
import re
cell_text = """\
#%%sql
q = \"\"\"
select
name, breed, sum(weight) over (partition by breed order by name) as running_total_weight
from cats
order by breed, name
\"\"\"
f(q)
"""
print(cell_text)
My attempt:
pat = """.*select(.*)['"].*"""
out = re.findall(pat,cell_text,flags=re.M)[0]
sql = 'select ' + out
print(sql)
# I am getting empty outputs for re.findall instead of text there.
Required output:
input
----
#%%sql
q = """
select
name, breed, sum(weight) over (partition by breed order by name) as running_total_weight
from cats
order by breed, name
"""
f(q)
output
------
select
name, breed, sum(weight) over (partition by breed order by name) as running_total_weight
from cats
order by breed, name
input
-----
#%%sql
q = "select * from cats;"
f(q)
output
-------
select * from cats;
input
-----
q = 'select * from cats limit 2'
output
------
select * from cats limit 2