简体   繁体   中英

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. 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? Please help

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

Demo

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