python 正则表达式操作指南 分类: 正则表达式 20130528 16:18 384人阅读 评论(0) 收藏

python 正则表达式操作指南

以下内容不错


http://luy.li/2010/05/12/python-re/


http://wiki.ubuntu.org.cn/Python%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%93%8D%E4%BD%9C%E6%8C%87%E5%8D

%97#.E6.90.9C.E7.B4.A2.E5.92.8C.E6.9B.BF.E6.8D.A2


松散表达式:


re.VERBOSE 参数可选,这个参数通过一个或一组标志(flag)控制预编译正则表达式的选项。指定了re.VERBOSE选项,告诉python正则表达式里有内联注释,注释和它们周围的空白不会被认作正则表达式的一部分,在编译正则表达式时re.compile函数会忽略它们。

import re

s='my name is sam,and i am 25 _years 26 old.'

#松散表达式
p=re.compile('''
\d+ #math data
\s #match space
\w+ # match _years
''',re.VERBOSE)

def main():
    print p.findall(s)

if __name__ == '__main__':
    main()


原文地址:https://www.cnblogs.com/think1988/p/4628178.html