您的位置:首页 > 其它

高级特性-正则表达式

2014-11-07 14:16 148 查看
re.search(pattern,string,flag)

与re.match()不同,匹配整个字符串,match如果开始不匹配则返回none

2. re.sub替换

re.sub(pattern,replace,string)

#!/bin/python

import re

#re.sub function testing

phone="Cats are smarter than dogs"

number=re.sub('\D',"",phone)

print(number)

#re.searach function testing
#caution: space in the regex
matchObj=re.search("(.*) are (.*?) .*",phone,re.M|re.I)
if matchObj:
print(matchObj.groups())
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: