例如:
import re pattern = r"(D+d)" match = re.match(pattern, "Hi 999!") if match: print("Match 1") match = re.match(pattern, "1, 23, 456!") if match: print("Match 2") match = re.match(pattern, " ! $?") if match: print("Match 3")
结果:
>>> Match 1 >>>
0