例如:
import re pattern = r"^gr.y$" if re.match(pattern, "grey"): print("Match 1") if re.match(pattern, "gray"): print("Match 2") if re.match(pattern, "stingray"): print("Match 3")
结果:
>>> Match 1 Match 2 >>>
1