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()方法功能相同,都是分隔字符串

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)#
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

 

你可能感兴趣的:(Python,python)