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

Python练习正则

时间:2016-04-23 13:26:59      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

# _*_ coding:utf-8 _*_
import re
line = "Cats are smarter than dogs"

matchObj = re.match(r‘(.*) are (.*?) .*‘, line, re.M|re.I)

if matchObj:
    print("matchObj.group():", matchObj.group())
    print("matchObj.group(1):", matchObj.group(1))
    print("matchObj.group(2):", matchObj.group(2))
else:
    print("NO MATCH")

print()

matchObj = re.search(r‘dogs‘, line, re.M|re.I)
if matchObj:
    print("search --> matchObj.group():", matchObj.group())
else:
    print("NO MATCH:")

print()

phone = "2004-959-559 # This is Phone Number"

num = re.sub(r‘#.*$‘, "", phone)
print("phone num:", num)
num = re.sub(r‘\D‘, "", phone)
print("phone num:", num)
print()


print(re.match(‘www‘, ‘www.runoob.com‘).span())
print(re.match(‘com‘, ‘www.runoob.com‘))

  

Python练习正则

标签:

原文地址:http://www.cnblogs.com/dennysong/p/5424325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!