日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python中re.findall()用法詳解_python

作者:IT之一小佬 ? 更新時間: 2022-09-17 編程語言

在python中,通過內嵌集成re模塊,程序媛們可以直接調用來實現正則匹配。本文重點給大家介紹python中正則表達式 re.findall 用法

re.findall():函數返回包含所有匹配項的列表。返回string中所有與pattern相匹配的全部字串,返回形式為數組。

示例代碼1:【打印所有的匹配項】

import re
 
s = "Long live the people's Republic of China"
ret = re.findall('h', s)
 
print(ret)

運行結果:

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

import re
 
s = "Long live the people's Republic of China"
ret = re.findall('USA', s)
 
print(ret)

運行結果:

示例代碼:

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"\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:

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)

運行效果:?

原文鏈接:https://blog.csdn.net/weixin_44799217/article/details/122069533

欄目分類
最近更新