我试图将一个巨大的SQL文件分解成小的sql文件,我正在使用python来实现这一点,但我使用的代码不匹配,从我在google上看到的情况来看,应该是匹配的。
代码如下:
import sys, re
p = [0]
f = open('/root/testsql/data.sql', 'r')
tables =["tabel1", "table2"]
contor = 0;
con = 0;
for line in f:
for table in tables:
stri = "root/testsql/" + str(con)
con = con + 1
stri2 = ".*" + table + ".*"
if re.match(stri2,line):
print table
f2 = open(stri,"w")
f2.write(line)
f2.close()
如果有人知道为什么re.match不能工作,我们将不胜感激。
该sql文件非常长(73595行),并且包含如下代码行:
insert into table ...
insert into table
发布于 2012-06-22 16:02:16
您只需要查找逐字字符串。在这种情况下,正则表达式是过度杀伤力。相反,请使用in
for line in f:
for table in tables:
# snip...
if table in line:
# ...
发布于 2012-06-22 15:54:41
我认为
stri2 = ".*" + table + ".*"
应该是:
stri2 = ".*?" + table + ".*"
.*是贪婪的,并且将匹配整个行。
发布于 2012-06-22 15:58:36
您应该使用re.search
而不是re.match
,而不是将正则表达式包装在.*
中。
您看不到匹配的原因是输入以换行符结尾,并且点元字符与换行符不匹配。
https://stackoverflow.com/questions/11159477
复制相似问题