首页 > 其他 > 详细

Re模块的 三个方法

时间:2020-05-06 16:26:15      阅读:50      评论:0      收藏:0      [点我收藏+]
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

Re模块的 三个方法

原文:https://www.cnblogs.com/cherry2020/p/12836446.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!