分组匹配
import re
p1 = re.compile('\d-\d-\d') #不分组
m1 = p1.match('1-2-3')
print(m1.groups())
print(m1.group())
p2 = re.compile('(\d)-(\d)-(\d)') #分组
m2 = p2.match('1-2-3')
print(m2.groups())
print(m2.group())
m3 = re.findall('(\d)-(\d)-(\d)', '1-2-3 4-5-6')
print(m3)
输出结果
()
1-2-3
('1', '2', '3')
1-2-3
[('1', '2', '3'), ('4', '5', '6')]
分组之后,要想获得某个分组的内容,直接使用group()或者groups()函数提取即可