Python中re.findall()怎么使用(python,re.findall(),开发技术)

时间:2024-04-30 02:13:16 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

希望大家仔细阅读,能够学有所成!

在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配。本文重点给大家介绍python中正则表达式 re.findall 用法

re.findall():函数返回包含所有匹配项的列表。返回string中所有与pattern相匹配的全部字串,返回形式为数组。

Python中re.findall()怎么使用

示例代码1:【打印所有的匹配项】

importre

s="Longlivethepeople'sRepublicofChina"
ret=re.findall('h',s)

print(ret)

运行结果:

Python中re.findall()怎么使用

示例代码2:【如果未找到匹配项,返回空列表】

importre

s="Longlivethepeople'sRepublicofChina"
ret=re.findall('USA',s)

print(ret)

运行结果:

Python中re.findall()怎么使用

示例代码:

importre

s="https://blog.csdn.net/weixin_44799217"
ret=re.findall(r"^http",s)
print(ret)

ret2=re.findall(r"[t,b,s]",s)#匹配括号中的其中一个字符
print(ret2)

ret3=re.findall(r"\d\d\d",s)
print(ret3)

ret4=re.findall(r"\d",s)
print(ret4)

ret5=re.findall(r"[^\d]",s)#取非
print(ret5)

ret6=re.findall(r"[^https://]",s)#取非
print(ret6)

运行结果:

Python中re.findall()怎么使用

获取网站中的title:

importrequests
importre

url='https://pz.wendu.com/'

response=requests.get(url)
data=response.text

print(data)

res=re.findall(r'<title>(.*?)</title>',data)[0]
print(res)

运行效果:

Python中re.findall()怎么使用

本文:Python中re.findall()怎么使用的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:SpringMVC拦截器应用实例分析下一篇:

9 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18