标准库,re

re.match(pattern, string),从字符串头,开始匹配

import re

result = re.match(r"hello", "hello world")
print(result.group())  # "hello"(匹配成功)

result = re.match(r"world", "hello world")
print(result)  # None(匹配失败)

re.search(pattern, string),整个字符串扫描,匹配

result = re.search(r"world", "hello world")
print(result.group())  # "world"(匹配成功)
posted @ 2025-06-05 16:20  呆呆酱  阅读(10)  评论(0)    收藏  举报