Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Regular expression matching tuple pairs

Looking for a regular expression to match tuple pairs within a list. Have been using the below regular expression

s = '[(aleakedteaserand, NN), (abehind, IN), (the, DT)]'    
re.findall(r'\((.*,.*)\)',s)

but it still missing the end braces.

['aleakedteaserand, NN), (abehind, IN), (the, DT']

Expected output:

[(aleakedteaserand, NN), (abehind, IN), (the, DT)]

like image 635
karthikbharadwaj Avatar asked Dec 19 '22 02:12

karthikbharadwaj


2 Answers

You didn't make the RegEx ungreedy. The solution is re.findall(r'\((.*?,.*?)\)',s).

like image 64
csabinho Avatar answered Dec 26 '22 14:12

csabinho


Alternatives. First one uses a complement match often used as an alternative to non-greedy search where it is not available.

>>> re.findall(r'\(([^)]*)\)',s)
['aleakedteaserand, NN', 'abehind, IN', 'the, DT']

>>> re.split('\), \(', s.strip('[()]'))
['aleakedteaserand, NN', 'abehind, IN', 'the, DT']

No regex

>>> s.strip('[()]').split('), (')
['aleakedteaserand, NN', 'abehind, IN', 'the, DT']
like image 35
SigmaPiEpsilon Avatar answered Dec 26 '22 14:12

SigmaPiEpsilon