我正在尝试使用python正则表达式捕获字符串上可变数量的组,如下所示:
matchre = re.search(r"^Hello\sThere\s(?P<first>[ 0-9\.]+?)\s(?:\[(?P<sqbracks>.+?)\]\s)+here\s(?P<second>\S+)\sare.*$", "Hello There 1.0.0 [AAA] [BBB] [CCC] here we are")
version = matchre.group('first')
print(version)
second = matchre.group('second')
print(sec)
sqbracks = matchre.group('sqbracks')
print(sqbracks)
上面的代码在Python 2.7.13上具有以下结果:
1.0.0
we
CCC
我对此有以下问题:
CCC
”,而不是三个中的每个括号,即AAA
BBB
CCC
。 Hello There 1.0.0 [AAA] here we are Hello There 2.0.0 [AAA] [BBB] here we are Hello There 3.0.0 [AAA] [BBB] [CCC] here we are
对此有什么想法吗?