No One in Particular
No One in Particular

Reputation: 2894

How to match and print list of comma separated numbers in python?

There are several questions regarding this topic, but none of them seem to answer this question specifically.

If I have the pattern p='([0-9]+)(,([0-9]+))*' and s='1,2,3,4,5' and I run m = re.match(p, s, 0) I get a match (as expected). However, I would like to be able to print the list ('1', '2', '3', '4', '5'). I can't seem to do this with the re.match output. It gives me ('1', ',5', '5').

Also, how do I get the number of matches (in this case 5)?

Upvotes: 1

Views: 546

Answers (1)

Max Noel
Max Noel

Reputation: 8910

Don't use regexes for that. s.split(',') will do exactly what you want.

Upvotes: 5

Related Questions