更新时间:2021年03月05日17时55分 来源:传智教育 浏览次数:
import re
example = "<li>goods</li><li>name</li>"
# 贪婪模式
greed_pattern = re.compile("<li>.*</li>")
# 非贪婪模式
not_greed_pattern = re.compile("<li>.*?</li>")
greed_result = greed_pattern.search(example)
not_greed_result = not_greed_pattern.search(example)
print(f"贪婪模式:{greed_result.group()}")
print(f"非贪婪模式:{not_greed_result.group()}")
(4)结合项目中使用