简体   繁体   English

Python正则表达式-一次匹配匹配的重复模式

[英]Python Regular expression -matching repeating pattern in one shot

CREATE TABLE IF NOT EXISTS test (col1 decimal(10,2),col2 char(20))

From this query String i want to get each of the column details (ie, col1 decimal(10,2) and col2 char(20) etc ) as a list in a single group(#anynumber) call. 我想从这个查询字符串中获取每个列的详细信息(即col1小数(10,2)和col2 char(20)等)作为单个group(#anynumber)调用中的列表。 currently what i'am doing is given below : 目前我在做什么如下:

columnResult = re.match(r"^CREATE(\s\w+\s*){1,}\((\w+)\s(\S+),\s*(\w+)\s(\S+)",line)
    if columnResult == None:
        pass
    else:
        print("column = ",columnResult.group(2),",",columnResult.group(4))
        print("type = ",columnResult.group(3),", ",columnResult.group(5))

Instead of repeating (\\w+)\\s(\\S+) for each column ,how can i match all repeating column details in one shot? 而不是为每列重复(\\ w +)\\ s(\\ S +),我如何一次匹配所有重复的列详细信息? Please help 请帮忙

Try Regex: (?:(?<=, )|(?<=\\(|,))(\\w+) (\\w+)(?:\\((\\d+)(?:,(\\d+))?\\))? 尝试Regex: (?:(?<=, )|(?<=\\(|,))(\\w+) (\\w+)(?:\\((\\d+)(?:,(\\d+))?\\))?

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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