Comment¶
- Pass
re.DOTALL
to the optionflags
to make a dot match newlines as well.
In [2]:
import re
In [5]:
pattern = ".*select\s+.*\s+from\s+(\w+\.?\w+)\s+"
sql = """
select
c1,
c2,
from
abc.table
where
1 = 1
""".strip().lower()
re.search(pattern, sql)
In [7]:
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))
In [8]:
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))
In [9]:
"abc".split(", ")
Out[9]:
In [10]:
(x,) = ["nima"]
In [12]:
x, *_ = ["nima"]
In [13]:
x
Out[13]: