例如:
import re pattern = r"egg(spam)*" if re.match(pattern, "egg"): print("Match 1") if re.match(pattern, "eggspamspamspamegg"): print("Match 2") if re.match(pattern, "spam"): print("Match 3")
结果:
>>> Match 1 Match 2 >>>
0