網站首頁 編程語言 正文
在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
相關推薦
- 2022-11-05 安裝ingress-nginx遇到的一些坑實戰記錄_云其它
- 2023-07-04 Linux之top命令詳解
- 2022-07-29 Linux進程管理方法介紹_linux shell
- 2022-08-04 Android開發EditText禁止輸入監聽及InputFilter字符過濾_Android
- 2023-07-15 react實現路由懶加載
- 2023-02-27 python實現定時任務的八種方式總結_python
- 2022-12-30 python算法與數據結構朋友圈與水杯實驗題分析實例_python
- 2022-11-02 Python中turtle庫常用代碼匯總_python
- 最近更新
-
- 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同步修改后的遠程分支