首页 > 编程语言 > 详细

Python正则表达式

时间:2016-06-09 22:10:28      阅读:230      评论:0      收藏:0      [点我收藏+]

1.re.match函数

import re
print re.match("www","www.baidu.com").span()#(0,3)

2.group

import re
line = "Cats are smarter than dogs"
matchObj = re.match(r(.*) are (.*?) .*,line,re.M | re.I)

if matchObj:
    print matchObj.group()#Cats are smarter than dogs
    print matchObj.group(1)#Cats
    print matchObj.group(2)#smarter

3.re.search()

import re
print re.search("com","www.baidu.com").span()#(10, 13)

4.re.sub()

import re
phone = "2004-959-559 # This is Phone Number"
num = re.sub(r#.*$,"",phone)
print "Phone Num:",num#Phone Num: 2004-959-559
num = re.sub(r\D,"",phone)
print "Phone Num:",num#Phone Num: 2004959559

Python正则表达式

原文:http://www.cnblogs.com/2star/p/5572731.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!