我试图用Python正则表达式库解析维基百科SQL转储。最终目标是将此转储导入PostgreSQL,但我知道字符串中的撇号需要事先加倍。
尽管在这个转储中的字符串中的每个撇号都有一个反斜杠,我宁愿不删除反斜杠。
(42,'Thirty_Years\'_War',33,5,0,0)
使用该命令
re.match(".*?([\w]+?'[\w\s]+?).*?", line)当'line'从文本文件中解析出来时,我无法在'Thirty_Years \'War'中找出撇号。
为了比较,这些行在解析时工作正常(没有最后一行)。
The person's car
The person's car's gasoline
Hodges' Harbrace Handbook
'Hodges' Harbrace Handbook'
portspeople',1475,29,0,0),(42,'Thirty_Years\'_War',33,5,0,0)
正确和预期的输出(无最后一行):
The person''s car
The person''s car''s gasoline
Hodges'' Harbrace Handbook
('Hodges'' Harbrace Handbook')
portspeople',1475,29,0,0),(42,'Thirty_Years\'_War',33,5,0,0)
使用该命令
re.match(".*?([\w\\]+?'[\w\s]+?).*?", line)打破它。
The person''s car
The person''''s car''''s gasoline
Hodges'' Harbrace Handbook
(''''''''Hodges'''''''' Harbrace Handbook'''''''')
portspeople'''''''''''''''',1475,29,0,0),(42,''''''''''''''''Thirty_Years\''''''''''''''''_War'''''''''''''''',33,5,0,0)
它是否陷入某种循环?什么是正确的正则表达式代码使用?
我没有考虑SQL注入攻击,因为这个脚本只用于解析维基百科文章的转储(不包含SQL注入攻击的例子)。