您的位置:首页 > 编程语言 > Python开发

python正则表达式一:match、search和findall

2017-07-15 09:32 976 查看
match是匹配起始位置,如果匹配成功,就返回一个匹配对象;如果匹配失败,就返回None
search()会用它的字符串参数,在任意位置对给定正则表达式模式搜索第一次出现的匹配情况。如果搜索到成功的匹配,就会返回一个匹配对象;否则,返回None
findall函数返回的是正则表达式在字符串中所有匹配结果的列表

代码:

import re

m=re.match('zc','zcdd')
if m is not None:
print(m.group())
else:
print(m)

m=re.match('zc','ddzc')
if m is not None:
print(m.group())
else:
print(m)

m=re.search('zc','zcdd')
if m is not None:
print(m.group())
else:
print(m)

m=re.search('zc','ddzc')
if m is not None:
print(m.group())
else:
print(m)

m=re.findall('a','abacd')
print(m)


运行:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: