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)
CodePudding user response:
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)')