例如:
import re pattern = r"g+" if re.match(pattern, "g"): print("Match 1") if re.match(pattern, "gggggggggggggg"): print("Match 2") if re.match(pattern, "abc"): print("Match 3")
结果:
>>> Match 1 Match 2 >>>
1