美文网首页
000.re正则表达式-match、search与findall

000.re正则表达式-match、search与findall

作者: 一月山 | 来源:发表于2021-01-12 21:23 被阅读0次

1.match函数

re.match(pattern,string,flags=0)

判断是否是起始位置

import re

str = 'stata club'

print(re.match(r'a',str))

运行结果:

match函数

2.search函数

扫描整个字符串

re.search(pattern,string,flags)

print(re.search(r'a',str))

运行结果:

search函数

3. findall 函数

在字符串中找所有可匹配的信息

re.findall(pattern,string,flags=0)

print(re.findall(r"a",str,flags=0))

运行结果:

findall函数

4.练习部分

match函数

str1='wen0128yue0625feng1004'

h =re.match(r'[a-z]{3}',str1,re.I)

print(h.group())

print(h.span())

结果显示:

search函数

str2='08116 zhongnancai 430070'

m=re.search(r'\d+',str2)

print(m.group())

print(m.span())

print(m.end())

结果显示:

findall函数

str2='08116 zhongnancai 430070'

m=re.findall(r'\d+',str2)

print(m)

显示结果:

相关文章

网友评论

      本文标题:000.re正则表达式-match、search与findall

      本文链接:https://www.haomeiwen.com/subject/hqneaktx.html