【发布时间】:2021-12-27 20:12:40
【问题描述】:
我有如下字符串:
sql = """DROP TABLE IF EXISTS table1;
ALTER TABLE table1 DROP PRIMARY KEY;
INSERT INTO table1 (id, created, name, telefonnummer, erPatient_id) VALUES
(1, '2015-08-06 12;09:08', ' ', ' ', 16528),
(2, '2015-08-06 12:43:11', ' ', ' ', 16529)
;
INSERT INTO table2 (comment, id) VALUES
('hello this is a semicolon ;', 2);"""
我想得到语句 INSERT INTO table1:
INSERT INTO table1 (id, created, name, telefonnummer, erPatient_id) VALUES
(1, '2015-08-06 12;09:08', ' ', ' ', 16528),
(2, '2015-08-06 12:43:11', ' ', ' ', 16529)
;
我无法用sql.split(';) 拆分字符串,因为要插入的VALUES 中有分号。
我尝试了正则表达式但没有成功:
import re
pattern_string = r"INSERT INTO table1[(]*[^)]+\)[^)]"
q = re.findall(pattern_string, data, re.MULTILINE | re.DOTALL)
在真正的字符串中,将插入数千个值和数十个表。
【问题讨论】:
-
如果您的数据不规则,那么正则表达式是错误的工具。你需要一个解析引擎。这个问题并不新鲜。 CSV 和无数其他东西也存在同样的问题。