简体   繁体   中英

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.

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

                                       ^^

This should do it for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM