python正则表达式使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dreamzuora/article/details/88539374

模板代码:

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

猜你喜欢

转载自blog.csdn.net/dreamzuora/article/details/88539374