标签:
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
标签:
原文地址:http://www.cnblogs.com/2star/p/5572731.html