re模块中match函数的使用

|-------------------------------------------|------------------------------------------------|
| 函数 | 功能描述 |
| re.match(pattern,string,flags=0) | 用于字符串的开始位置进行匹配,如果起始位置匹配成功,结果为Match对象,否则结果位None |
| re.search(pattern,string,flags=0) | 用于整个字符串中搜索第一个匹配的值,如果匹配成功,结果为Match对象,否则结果位None |
| re.findall(pattern,string,flags=0) | 用于在整个字符串搜索所有符合正则表达式的值,结果是一个列表类型 |
| re.sub(patern,repl,string,count,flags=0) | 用于实现对字符串中指定字符串的替换 |
| re.split(pattern,string,maxsplit,flags=0) | 字符串中的split()方法功能相同,都是分隔字符串 |

python 复制代码
import re#导入
pattern='\d\.\d+'#+限定符.\d 0-9数字出现一次或者多次
s='I study Python 3.11 every day'#待匹配字符串
match=re.match(pattern,s,re.I )
print(match)#None
s2='3.11 Python I study every day'
match2=re.match(pattern,s2)
print(match2)#<re.Match object;span=(0,4),match='3.11>
print('匹配值的起始位置:',match2.start())#0
print('匹配值的结束位置:',match2.end())#4
print('匹配区间的位置元素:',match2.span())#(0, 4)
print('待匹配的字符串:',match2.string)#3.11 Python I study every day
print('匹配的数据:',match2.group())#3.11
相关推荐
databook3 小时前
时间序列数据,先拆规律还是先查异常
python·数据挖掘·数据分析
师夷长技以自愈3 小时前
手搓 AI Agent 第 8 篇:从手写循环到画流程图——LangGraph 封装
人工智能·python
aifannihao5 小时前
Rust 1.100 的 DropGuard:用 Python 实测失败回滚与吞异常
python·rust·异常处理·标准库·contextlib
用户019027581616 小时前
天勤 tqsdk 能做 A 股行情吗?期货强项与股票数据选型
python
2601_967979128 小时前
pip在Python中的9个必须知道的用法
python·pip·版本控制·包管理工具·requirements.txt
曲径海盗8 小时前
【Python 学习第 58 天】列表方法
python·学习笔记·编程入门
DeepVisionary8 小时前
Runway开源机器人大模型Praxis-1,推演延迟压到62毫秒
python·自动化
Cooper259 小时前
DDC 命令成功但屏幕不动:用 gamma 表重做亮度映射
python·mac
师夷长技以自愈10 小时前
手搓 AI Agent 第 7 篇:菜做砸了别硬炒——错误分类 + 双账本熔断
人工智能·python
用户9562562542910 小时前
15 分钟社媒简报 Agent:我只用了搜索和模型两步
python·api