Anvi
Anvi

Reputation: 2741

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

Upvotes: 0

Views: 312

Answers (1)

Matt.G
Matt.G

Reputation: 3609

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

Demo

Upvotes: 2

Related Questions