例如:
import re pattern = r"spam" if re.match(pattern, "eggspamsausagespam"): print("Match") else: print("No match") if re.search(pattern, "eggspamsausagespam"): print("Match") else: print("No match") print(re.findall(pattern, "eggspamsausagespam"))
结果:
>>> No match Match ['spam', 'spam'] >>>