几个小时以来,我一直试图以多行文本的形式提取SQL查询,但一直以来我都得到了错误的输出,因此决定发布答案,以便可能获得一些帮助。我很抱歉,因为在SO中已经有很多python regex问题,但是这个问题确实占用了我很多时间,所以我决定将其发布在这里,而不是将我的头撞在墙上。

问题:如何使一两个引号之间的文本?
''""""""""''''''
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)
我的尝试
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.
所需输出
select 
name, breed, sum(weight) over (partition by breed order by name) as running_total_weight
from cats 
order by breed, name