Python正则表达式中group和groups 的区别

# groups的误区
import re
r = re.match('\d', a)
print(r.groups()) # 打印 ()

r = re.match('(\d)', a)
print(r.groups()) # 打印 ('1',)
# groups只会打印括号括起来的元组
import re
s = 'life is short, i use python, i love python'
r1 = re.match('life(.*)python(.*)python', s)
r1 = re.search('life(.*)python(.*)python', s)

print(r1.group()) # 打印结果: life is short, i use python, i love python
print(r1.group(0)) # 打印结果: life is short, i use python, i love python
print(r1.group(1)) # 打印结果: is short, i use
print(r1.group(2)) # 打印结果: , i love 

print(r1.groups()) # 打印结果: (' is short, i use ', ', i love ')

你可能感兴趣的:(python,正则表达式,开发语言)