[英]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. 我只需要确认如何使第二个匹配组为可选,就可以解析变量b。
m = re.match('^([0-9,-,\,]*)(\([0-9]*\))?',a)
^^
This should do it for you 这应该为你做
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.