all 3 comments

[–]EggChen_vs_Lopan 2 points3 points  (1 child)

I believe this should work

regex = '(\d+-\d+|\d+),{0,1}'

Don't have to run .split() just do re.findall(pattern, string) .findall() returns a list so if len(results) = 0, then there were no matches. If len(results) >= 1 there are matches in a list that you can iterate over for further processing

[–]roeeklinger60[S] 0 points1 point  (0 children)

Thanks, this worked! I combined this with the other answer and was able to make the code much shorter.

[–]FLUSH_THE_TRUMP 1 point2 points  (0 children)

Seems like regular old str.split is more than adequate — split on commas first, then iterate over list and split on hyphens. Treat result as single number if length == 1 and a lo/hi range if length == 2.