自己遇到的问题,小记一下!
# -*- coding: utf-8 -*-
__author__ = 'Johnny'
import re
line = "baaaaacfbsbdada"
reg_str = ".*(b.*b).*"
match_str = re.match(reg_str, line)
if match_str:
print match_str.group(1)
----------------------------------------
line = "baaaaacfbsdada"
reg_str = ".*(b.*b).*"
match_str = re.match(reg_str, line)
if match_str:
print match_str.group(1)
----------------------------------------
line = ""
reg_str = ".*"
match_str = re.match(reg_str, line)
if match_str:
print match_str.group() is ''
print match_str.group() is True
print match_str.group() is False
以上代码输出为:
>>> bsb
>>> baaaaacfb
>>> True
>>> False
>>> False
第一种情况是,在满足分组条件下,贪婪,所以‘bsb’左侧全部被贪婪。
第二种情况是,在满足分组条件下,无法再贪婪,所以左侧开始就直接匹配分组了。
第三种情况是,’.*’可以匹配空,所以找到了。但是空既不是True也不是False。
非贪婪就比较好理解了。
本文通过几个具体的Python代码示例,详细解释了正则表达式的使用方法,包括贪婪匹配与非贪婪匹配的区别,以及如何正确使用正则表达式进行字符串匹配。
1014

被折叠的 条评论
为什么被折叠?



