Regex expression to extract tupples from a file?

Question:

This is an odd task, but I have to extract tupples from the text of a SQL query as in the below example. In this example, I want to extract a list of tupples [(116065239,61655186),(116065239,63112123),(116065239,68748795),(116065239,97834462)]

import re 

def extract_tupples(s):
    # For sample, should return a list of
    # [(116065239,61655186),(116065239,63112123),(116065239,68748795),(116065239,97834462)]
    return None

s = "insert  into `a_b_c`(`u1`,`u2`) values (116065239,61655186),(116065239,63112123),(116065239,68748795),(116065239,97834462)"
extract_tupples(s)

Asked By: statsman

||

Answers:

re.findall with a lazy matching regex does the trick:

import re 

def extract_tupples(s):
    # For sample, should return a list of
    # [(116065239,61655186),(116065239,63112123),(116065239,68748795),(116065239,97834462)]
    return re.findall("([0-9,]*?)", s)

s = "insert  into `a_b_c`(`u1`,`u2`) values (116065239,61655186),(116065239,63112123),(116065239,68748795),(116065239,97834462)"
extract_tupples(s)

['(116065239,61655186)',
 '(116065239,63112123)',
 '(116065239,68748795)',
 '(116065239,97834462)']

tuple('(116065239,97834462)')
Answered By: dcsuka
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.