正则模式根据特殊字符组成,可以在一长串字符串中获取匹配我们想要的数据
line = "bobby123" regex_str = "^b.*3$"#^以b字符开头,3结尾 if re.match(regex_str, line): print("yes") else: print("no")
贪婪模式想着尽可能多地去匹配满足自身条件的字符
# =========贪婪匹配==================== # 从右边开始匹配 line = "boddddddddddddddddddddddddbby123" regex_str = ".*(b.*b).*" # .*贪婪匹配尽可能多地占用匹配字符 match_obj = re.match(regex_str, line) print(match_obj.group(1)) #bb
非贪婪模式通过加 “?”匹配字符的限制,尽可能少地去匹配满足自身条件的字符,容易满足自身要求
# =========非贪婪匹配==================== # 从左边开始匹配 line = "boddddddddddddddddddddddddbby123" regex_str = ".*?(b.*?b).*" # .*非贪婪匹配尽可能少地占用匹配字符 match_obj = re.match(regex_str, line) print(match_obj.group(1)) # boddddddddddddddddddddddddb