编程语言
首页 > 编程语言> > python笔记37:正则的贪婪模式和非贪婪模式

python笔记37:正则的贪婪模式和非贪婪模式

作者:互联网

import re
s = "this is a number 234-235-22-423"
r = re.match(r".+(\d+-\d+-\d+-\d+)",s)
result = r.group(1)
print(result)  #4-235-22-423

r = re.match(r"(.+)(\d+-\d+-\d+-\d+)",s)
result = r.groups()
print(result)  #('this is a number 23', '4-235-22-423')

# 去掉贪婪模式方法
r = re.match(r"(.+?)(\d+-\d+-\d+-\d+)",s)
result = r.groups()
print(result)  #('this is a number ', '234-235-22-423')

 

标签:22,python,number,模式,re,result,贪婪,235,423
来源: https://www.cnblogs.com/liulilitoday/p/13263557.html