簡體   English   中英

如何從sql查詢中提取目標表?

[英]How to extract destination table from sql query?

我想從任何 sql 查詢中提取表名,例如

insert into...
create table .... as (select *..)

任何優化的正則表達式或其他任何有助於實現這一目標的東西?

regex = r"(?ims)\b(?:Table|INTO)\s+(\w+(?:\.\w+)*)"

d = "insert into ex"
x = re.search(regex, d)

print(x.group())

這應該是你想要的:

regex = re.compile(r"(?:create table|insert into) (\w+)", re.IGNORECASE)

# Try it
d = "insert into ex"
x = regex.search(d)
print(x.group(1))

# Try it again
d = "create table my_table as (select *...)"
x = regex.search(d)
print(x.group(1))

請注意,如果您要多次使用它,最好事先使用re.compile編譯正則表達式並將其存儲在一個單獨的變量中,就像我在這里所做的那樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM