码迷,mamicode.com
首页 > 编程语言 > 详细

Python正则表达式

时间:2016-06-09 22:11:45      阅读:193      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!