I would like to as a basic Python regular expression problem.
I have a dataset
line = "(1,2) (2,3)"
That can repeat many times so line can also be
line = "(1,2) (3,4) (6,5)"
I have a regular expression
rx = "(\(\s*\d+\s*,\s*\d+\s*\)\s*){2,}$"
I want
a = re.match(rx,line).groups();
to match
('(1,2)','(3,4)'...)
But I can only match the last (6,5). I need the last $ because I don’t know how many bracketed inputs I can have, otherwise an incorrect input such as
(1,2),(3,4),(5,6
will pass the regexp.
any tips?
Edit:
Added the fact that the data was not exactly formatted as detailed. Instead
line= 'blah(1,2) (2,3)blah'
So indeed regular expressions are needed
Thanks
If you really want to use regular expressions (I’m not a regex specialist, but it worked with the given data):
or else follow nightcracker’s excellent suggestion. Most often the simplest answer is the better answer.
EDIT: Thanks to Joran Beasley for the suggestion.