Python Regex 获取索引名和列名

对于以下每个输入行,我需要获取索引名称和列名称。


PRIMARY INDEX FIRST_ONE ( PLATFORM_CD ,SYSTEM_NAME ,DB_NAME ,TABLE_NAME ,COLUMN_NAME );

) PRIMARY INDEX SECOND_ONE ( Hq_Id);

PRIMARY INDEX THIRD_ONE ( DB_NAME ,BU_NAME );

PRIMARY INDEX FOURTH_ONE ( DB_NAME ,TABLE_NAME ,COLUMN_NAME,  PII_CD);

UNIQUE PRIMARY INDEX FIFT_ONE( DB_NAME,PRIMARY_SYSTEM_NAME);

例如对于第一行我需要得到的输入:


index name : FIRST_ONE

column1 : PLATFORM_CD

column2 : SYSTEM_NAME

column3 : DB_NAME

column4 : TABLE_NAME

column5 : COLUMN_NAME

我编写的正则表达式能够选择列名。正则表达式是:


(?:(?<=,|\())\s*(\w+)\s*(?:\s*(\w+)\s*)?

我应该做哪些更改才能获取索引名称?任何人都可以建议一个正则表达式。我是正则表达式的初学者。正则表达式需要为我提供的每种输入格式提供输出。


慕勒3428872
浏览 310回答 3
3回答

湖上湖

除了正则表达式,您还可以使用sqlparse并拆分字符串:import sqlparsestmts = """PRIMARY INDEX FIRST_ONE ( PLATFORM_CD ,SYSTEM_NAME ,DB_NAME ,TABLE_NAME ,COLUMN_NAME );) PRIMARY INDEX SECOND_ONE ( Hq_Id);PRIMARY INDEX THIRD_ONE ( DB_NAME ,BU_NAME );PRIMARY INDEX FOURTH_ONE ( DB_NAME ,TABLE_NAME ,COLUMN_NAME,&nbsp; PII_CD);UNIQUE PRIMARY INDEX FIFT_ONE( DB_NAME,PRIMARY_SYSTEM_NAME);"""for stmt in sqlparse.split(stmts):&nbsp; &nbsp; res = sqlparse.parse(stmt)&nbsp; &nbsp;# parses each statement&nbsp; &nbsp; for tok in (res[0].tokens):&nbsp; # iterate all tokens&nbsp; &nbsp; &nbsp; &nbsp; if isinstance(tok, sqlparse.sql.Function):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idx, columns = str(tok).split('(')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cols = [c.strip() for c in columns[:columns.find(')')].split(',')]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print (idx.strip(),&nbsp; cols)返回:FIRST_ONE ['PLATFORM_CD', 'SYSTEM_NAME', 'DB_NAME', 'TABLE_NAME', 'COLUMN_NAME']SECOND_ONE ['Hq_Id']THIRD_ONE ['DB_NAME', 'BU_NAME']FOURTH_ONE ['DB_NAME', 'TABLE_NAME', 'COLUMN_NAME', 'PII_CD']FIFT_ONE ['DB_NAME', 'PRIMARY_SYSTEM_NAME']

智慧大石

splash58 提到的正则表达式有效..\w+(?=\s*()|(?:(?<=,|())\s*(\w+)\s*(?:\s*(\w+)\s*)?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python