实例
#!/usr/bin/python# -*- coding: UTF-8 -*- importreprint(re.match('www', 'www.runoob.com').span()) # 在起始位置匹配print(re.match('com', 'www.runoob.com')) # 不在起始位置匹配
以上实例运行输出结果为:
(0,3)
None
实例
#!/usr/bin/pythonimportreline = "Cats are smarter than dogs"matchObj = re.match(r'(.*) are (.*?) .*', line, re.M|re.I)ifmatchObj: print"matchObj.group() : ", matchObj.group() print"matchObj.group(1) : ", matchObj.group(1) print"matchObj.group(2) : ", matchObj.group(2)else: print"No match!!"
以上实例执行结果如下:
matchObj.group(): Cats are smarter than dogs
matchObj.group(1): Cats
matchObj.group(2): smarter