可以用?P<name>的方法给正则匹配的部分命名。

例:要将<字母,数字>的部分命名为test

x = "abc <haha,123> test @@"
pattern = "(?P<test>\<\w+,\d+\>)"
m = re.search(pattern, x)
r = m.group("test")
print r

输出:
<haha,123>

相关文章: