felix001
felix001

Reputation: 16691

Python Optional Match Groups

I have the following that I need to place into match groups.

a = '1,2,3(1)'
b = '1,2,3'

parsing a is fine,

>>> m = re.match('^([0-9,-,\,]*)(\([0-9]*\))',a)
>>> m.groups()
('1,2,3', '(1)')

I just need to confirm how to make the second match group optional so I can parse the variable b.

Upvotes: 4

Views: 1770

Answers (1)

vks
vks

Reputation: 67968

m = re.match('^([0-9,-,\,]*)(\([0-9]*\))?',a)

                                       ^^

This should do it for you

Upvotes: 7

Related Questions