Python中re.findall()怎么使用
导读:本文共1276字符,通常情况下阅读需要4分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要:希望大家仔细阅读,能够学有所成!在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配。本文重点给大家介绍python中正则表达式 re.findall 用法re.findall():函数返回包含所有匹配项的列表。返回string中所有与pattern相匹配的全部字串,返回形式为数组。示例代码1:【打印所有的匹配项】importres="Longlivethepeo... ...
目录
(为您整理了一些要点),点击可以直达。希望大家仔细阅读,能够学有所成!
在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配。本文重点给大家介绍python中正则表达式 re.findall 用法
re.findall():函数返回包含所有匹配项的列表。返回string中所有与pattern相匹配的全部字串,返回形式为数组。
示例代码1:【打印所有的匹配项】
importres="Longlivethepeople'sRepublicofChina"
ret=re.findall('h',s)print(ret)
运行结果:
示例代码2:【如果未找到匹配项,返回空列表】
importres="Longlivethepeople'sRepublicofChina"
ret=re.findall('USA',s)print(ret)
运行结果:
示例代码:
importres="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)
运行结果:
获取网站中的title:
importrequests
importreurl='https://pz.wendu.com/'
response=requests.get(url)
data=response.textprint(data)
res=re.findall(r'<title>(.*?)</title>',data)[0]
print(res)
运行效果:
Python中re.findall()怎么使用的详细内容,希望对您有所帮助,信息来源于网络。