在python中,通過內(nèi)嵌集成re模塊,程序媛們可以直接調(diào)用來實(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) # 匹配括號中得其中一個字符print(ret2) ret3 = re.findall(r"ddd", 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 requestsimport 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()用法詳解得內(nèi)容就介紹到這了,更多相關(guān)Python re.findall()用法內(nèi)容請搜索之家以前得內(nèi)容或繼續(xù)瀏覽下面得相關(guān)內(nèi)容希望大家以后多多支持之家!
聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。