import re s1 = ‘绿茶白茶黄茶青茶红茶黑茶‘ s2 = ‘中国绿茶白茶黄茶青茶红茶黑茶‘ ret = re.findall(".茶", s1) print(ret) r1 = re.search(".茶", s1) print("search方法直接返回", r1) if r1: print(r1.group()) r21 = re.match(".茶", s1) print("match方法直接返回", r21) if r21: print(r21.group()) r22 = re.match(".茶", s2) print("match方法直接返回", r22) if r22: print(r22.group())
[‘绿茶‘, ‘白茶‘, ‘黄茶‘, ‘青茶‘, ‘红茶‘, ‘黑茶‘]
search方法直接返回 <re.Match object; span=(0, 2), match=‘绿茶‘>
绿茶
match方法直接返回 <re.Match object; span=(0, 2), match=‘绿茶‘>
绿茶
match方法直接返回 None
原文:https://www.cnblogs.com/cherry2020/p/12836446.html