简体   繁体   中英

Need regex expression to get only Tably name and primary key in python

May this question is silly, but i am not able to generate the regex expression to fetch table Name and PRIMARY KEY.

TABLE:

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);

I am using: begin = re.compile(r"CREATE TABLE[ \\"]*([^ \\"]+)[ \\"]*[(]([^/;]+)[/;]",re.IGNORECASE) to fetch all table name and data.

But i would need data only with table name and PRIMARY KEY.

Expected Output:

dhcpr_dhcprelayinterface
PRIMARY KEY(ifName,vrId)

This solution takes care of some issues you seem not worried about (but which are good to worry about), eg, SQLite allows you to write escaped ' as '' , and there may be any number of spaces, even newlines, between CREATE and TABLE , and between PRIMARY , KEY , and ( :

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())

Output:

('dhcpr_dhcprelayinterface', 'PRIMARY KEY(ifName,vrId)')

I'm sure you can solve it with regular expressions or sqlparse , but here is a "fun" way of approaching the problem just for educational purposes - using sqlite3 in memory database - actually create the table and get the table_name from the sqlite_master internal table and primary key columns from the PRAGMA table_info :

import sqlite3

query = """
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);
"""

db = sqlite3.connect(":memory:")
cursor = db.cursor()

cursor.execute(query)
db.commit()

# get table name
cursor.execute("select name from sqlite_master where type = 'table'")
table_name = cursor.fetchone()[0]
print(table_name)

# get primary key columns
cursor.execute("PRAGMA table_info(%s);" % table_name)
pk_columns = [row[1] for row in cursor.fetchall()[::-1]]
print(pk_columns)

Prints:

dhcpr_dhcprelayinterface
['ifName', 'vrId']

You can use this to just get the table name and primary key.

begin = re.compile(r"CREATE TABLE[ ']*([^ ']+)[ ']*[(][^/;]+(PRIMARY KEY.*),[^/;]+;$", re.IGNORECASE)
begin.findall(YOUR_STR)

Outputs:

In [1]: a = """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);"""
In [2]: begin = re.compile(r"CREATE TABLE[ ']*([^ ']+)[ ']*[(][^/;]+(PRIMARY KEY.*),[^/;]+;$", re.IGNORECASE)
In [3]: begin.findall(a)
Out[3]: [('dhcpr_dhcprelayinterface', 'PRIMARY KEY(ifName,vrId)')]

The following was tested using python2.7:

>>> table_string = """
... 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 CASCAD
E);"""
>>> p = r'CREATE TABLE\s+\'([^\']+)[\s\S]+PRIMARY KEY\(([^,]+),([^\)]+)\)'
>>> re.findall(p,table_string)
[('dhcpr_dhcprelayinterface', 'ifName', 'vrId')]

The explanation can be found here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM