正则表达式 re.findall 用法
正则 re.findall 的简单用法(返回string中所有与pattern相匹配的全部字串,返回形式为数组)
语法:
1 | findall(pattern, string, flags = 0 ) |
import re
Python 正则表达式 re findall 方法能够以列表的形式返回能匹配的子串
# print (help(re.findall))
# print (dir(re.findall))
findall查找全部r标识代表后面是正则的语句
1 2 3 | regular_v1 = re.findall(r "docs" , "https://docs.python.org/3/whatsnew/3.6.html" ) print (regular_v1) # ['docs'] |
符号^表示匹配以https开头的的字符串返回,
1 2 3 | regular_v2 = re.findall(r "^https" , "https://docs.python.org/3/whatsnew/3.6.html" ) print (regular_v2) # ['https'] |
用$符号表示以html结尾的字符串返回,判断是否字符串结束的字符串
1 2 3 | regular_v3 = re.findall(r "html$" , "https://docs.python.org/3/whatsnew/3.6.html" ) print (regular_v3) # ['html'] |
# [...]匹配括号中的其中一个字符
1 2 3 | regular_v4 = re.findall(r "[t,w]h" , "https://docs.python.org/3/whatsnew/3.6.html" ) print (regular_v4) # ['th', 'wh'] |
“d”是正则语法规则用来匹配0到9之间的数返回列表
1 2 3 4 5 6 | regular_v5 = re.findall(r "\d" , "https://docs.python.org/3/whatsnew/3.6.html" ) regular_v6 = re.findall(r "\d\d\d" , "https://docs.python.org/3/whatsnew/3.6.html/1234" ) print (regular_v5) # ['3', '3', '6'] print (regular_v6) # ['123'] |
小d表示取数字0-9,大D表示不要数字,也就是出了数字以外的内容返回
1 2 3 | regular_v7 = re.findall(r "\D" , "https://docs.python.org/3/whatsnew/3.6.html" ) print (regular_v7) # ['h', 't', 't', 'p', 's', ':', '/', '/', 'd', 'o', 'c', 's', '.', 'p', 'y', 't', 'h', 'o', 'n', '.', 'o', 'r', 'g', '/', '/', 'w', 'h', 'a', 't', 's', 'n', 'e', 'w', '/', '.', '.', 'h', 't', 'm', 'l'] |
“w”在正则里面代表匹配从小写a到z,大写A到Z,数字0到9
1 2 3 | regular_v8 = re.findall(r "\w" , "https://docs.python.org/3/whatsnew/3.6.html" ) print (regular_v8) #['h', 't', 't', 'p', 's', 'd', 'o', 'c', 's', 'p', 'y', 't', 'h', 'o', 'n', 'o', 'r', 'g', '3', 'w', 'h', 'a', 't', 's', 'n', 'e', 'w', '3', '6', 'h', 't', 'm', 'l'] |
“W”在正则里面代表匹配除了字母与数字以外的特殊符号
1 2 3 | regular_v9 = re.findall(r "\W" , "https://docs.python.org/3/whatsnew/3.6.html" ) print (regular_v9) # [':', '/', '/', '.', '.', '/', '/', '/', '.', '.'] |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!