網站首頁 編程語言 正文
在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
相關推薦
- 2023-03-16 Android虛擬機Dalvik和ART科普_Android
- 2023-08-28 axios的使用和接口請求統一封裝處理
- 2022-12-13 Python實現根據Excel生成Model和數據導入腳本_python
- 2023-07-03 Docker之容器導出為鏡像問題_docker
- 2022-10-01 Python類與實例的使用詳解_python
- 2022-02-04 Hive在內存不足以存儲所有數據時,會使用外存,而Impala也是如此:錯
- 2022-12-03 Android開發數據結構算法ArrayList源碼詳解_Android
- 2023-10-14 C/C++--跨平臺--預定義宏 WIN32、_WIN32、_WIN64
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支