Python中re.findall()用法詳解
在python中,通過(guò)內(nèi)嵌集成re模塊,程序媛們可以直接調(diào)用來(lái)實(shí)現(xiàn)正則匹配。本文重點(diǎn)給大家介紹python中正則表達(dá)式 re.findall 用法
re.findall():函數(shù)返回包含所有匹配項(xiàng)的列表。返回string中所有與pattern相匹配的全部字串,返回形式為數(shù)組。
示例代碼1:【打印所有的匹配項(xiàng)】
import re s = "Long live the people's Republic of China" ret = re.findall('h', s) print(ret)
運(yùn)行結(jié)果:
示例代碼2:【如果未找到匹配項(xiàng),返回空列表】
import re s = "Long live the people's Republic of China" ret = re.findall('USA', s) print(ret)
運(yùn)行結(jié)果:
示例代碼:
import re s = "https://blog.csdn.net/weixin_44799217" ret = re.findall(r"^http", s) print(ret) ret2 = re.findall(r"[t,b,s]", s) # 匹配括號(hào)中的其中一個(gè)字符 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)
運(yùn)行結(jié)果:
獲取網(wǎng)站中的title:
import requests import re url = 'https://pz.wendu.com/' response = requests.get(url) data = response.text # print(data) res = re.findall(r'<title>(.*?)</title>', data)[0] print(res)
運(yùn)行效果:
到此這篇關(guān)于Python中re.findall()用法詳解的文章就介紹到這了,更多相關(guān)Python re.findall()用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 經(jīng)典數(shù)字濾波實(shí)例
今天小編就為大家分享一篇python 經(jīng)典數(shù)字濾波實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12解決項(xiàng)目pycharm能運(yùn)行,在終端卻無(wú)法運(yùn)行的問(wèn)題
今天小編就為大家分享一篇解決項(xiàng)目pycharm能運(yùn)行,在終端卻無(wú)法運(yùn)行的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01python 生成器協(xié)程運(yùn)算實(shí)例
下面小編就為大家?guī)?lái)一篇python 生成器協(xié)程運(yùn)算實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09Python開(kāi)發(fā)的單詞頻率統(tǒng)計(jì)工具wordsworth使用方法
wordsworth是字母,單詞和n元組頻率分析,用來(lái)分析文件中的單詞出現(xiàn)頻率的工具。2014-06-06python中關(guān)于CIFAR10數(shù)據(jù)集的使用
這篇文章主要介紹了python中關(guān)于CIFAR10數(shù)據(jù)集的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02R語(yǔ)言屬性知識(shí)點(diǎn)總結(jié)及實(shí)例
在本篇文章里小編給大家整理了一篇關(guān)于R語(yǔ)言屬性知識(shí)點(diǎn)總結(jié)及實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03python基于itchat模塊實(shí)現(xiàn)微信防撤回
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)微信防撤回,基于itchat模塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04