1、贪婪匹配
总是尝试匹配尽可能多的字符
2、非贪婪匹配
是尝试匹配尽可能少的字符
import re secret_code = ‘hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse‘ b = re.findall(‘xx.*xx‘,secret_code) # 贪婪匹配 print (b) # [‘xxIxxfasdjifja134xxlovexx23345sdfxxyouxx‘] c = re.findall(‘xx.*?xx‘,secret_code) # 非贪婪匹配 print(c) # [‘xxIxx‘, ‘xxlovexx‘, ‘xxyouxx‘]
贪婪格式:xx.*xx
非贪婪格式:xx.*?xx
区别重点在:.* 和 .*?
原文地址:https://www.cnblogs.com/zhuifeng-mayi/p/9200910.html
时间: 2024-11-01 22:44:40