Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
Comment¶
Pass
re.DOTALLto the optionflagsto make a dot match newlines as well.
import repattern = ".*select\s+.*\s+from\s+(\w+\.?\w+)\s+"
sql = """
select
c1,
c2,
from
abc.table
where
1 = 1
""".strip().lower()
re.search(pattern, sql)pattern = ".*select\s+.*\s+from\s+(\w+\.?\w+)\s+"
sql = """
select
c1,
c2
from
db.table
where
1 = 1
""".strip().lower()
match = re.search(pattern, sql, re.DOTALL)
print(match.group(0))
print(match.group(1))select
c1,
c2
from
db.table
db.table
pattern = ".*select\s+.*\s+from\s+(\w+\.?\w+)\s+"
sql = """
select
count(*) as total,
count(case
when item_site_id = 15 then 1
else 0
end) as total_au
from
db.table
"""
match = re.search(pattern, sql, re.DOTALL)
print(match.group(0))
print(match.group(1))
select
count(*) as total,
count(case
when item_site_id = 15 then 1
else 0
end) as total_au
from
db.table
db.table
"abc".split(", ")['abc'](x,) = ["nima"]x, *_ = ["nima"]x'nima'