擅长:python、mysql、java
<p>此解决方案解决了一些您似乎不担心的问题(但值得担心),例如,SQLite允许您将转义的<code>'</code>写入<code>''</code>,并且<code>CREATE</code>和<code>TABLE</code>之间以及<code>PRIMARY</code>、<code>KEY</code>和<code>(</code>之间可能有任意数量的空格,甚至换行:</p>
<pre><code>s = """\
CREATE TABLE 'dhcpr_dhcprelayinterface' (
'vrId' integer default 0,
'ifName' string ,
PRIMARY KEY(ifName,vrId),
FOREIGN KEY (vrId) REFERENCES 'vr_vr'(vrId)
ON DELETE CASCADE ON UPDATE CASCADE);
"""
pattern = """
CREATE \s+ TABLE \s+
'((?:[^']|'')*)' # allows escaped single quote
.+ # stuff between table name and primary key
(PRIMARY \s+ KEY\s? \([^)]*\))
"""
mo = re.search(pattern, s, re.IGNORECASE | re.VERBOSE | re.DOTALL)
print(mo.groups())
</code></pre>
<p>输出:</p>
<pre><code>('dhcpr_dhcprelayinterface', 'PRIMARY KEY(ifName,vrId)')
</code></pre>