首先需要明确一点,正则默认就是贪婪模式,何为贪婪模式? 就是在满足条件的前提下,尽可能多的进行匹配。

下面先来个例子

s ='my tel number is 132-4567-1258'
r = re.match(r'(.+)(\d+-\d+-\d+)',s)
print(r.groups())  # ('my tel number is 13', '2-4567-1258')
'''
结查分析:
    .+ : 表示任意字符匹配一次或者多次

    \d+-\d+-\d+  : 1个数字或者多个数字,然后横线 , 然后1个数字或者多个数字,然后横线 ,然后1个数字或者多个数字

    因为正则是贪婪的,所以在满意第2个分组的前提下(给你1个数字吧),  .+ 会尽可能多的匹配字符串,
    所以就出现了('my tel number is 13', '2-4567-1258')这个结果。

'''


"""
    关闭贪婪
"""

s ='my tel number is 132-4567-1258'
r = re.match(r'(.+?)(\d+-\d+-\d+)',s)
print(r.groups())  # ('my tel number is ', '132-4567-1258')

'''
注意:? 只是关闭了.+的贪婪模式, 尽可能少的匹配,
      \d+-\d+-\d+  还是贪婪的
'''

 

In [20]: re.match(r'aa(\d+)bb','aa1234bb').group(1)
Out[20]: '1234'

In [21]: re.match(r'aa(\d+?)bb','aa1234bb').group(1)
Out[21]: '1234'

In [22]: re.match(r'aa(\d+?)','aa1234bb').group(1)
Out[22]: '1'

 

 

 

posted on 2019-12-06 23:33  显示账号  阅读(880)  评论(0编辑  收藏  举报