模板代码:
import re #python 自1.5版本增加了re模块,它提供了Perl风格的正则表达式模式
print(re.match('www', 'www.baidu.com').span()) #在起初位置匹配
line = "Cats are smarter than dogs"
matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)
if matchObj:
ms = matchObj.groups()
for m in ms:
print(m)
输出:
(0, 3)
Cats
smarter