python中利用re模块使用正则表达式

来源:互联网 发布:informix sql 语法 编辑:程序博客网 时间:2024/05/17 23:59

Python通过re模块提供对正则表达式的支持。使用re的一般步骤是先使用re.compile()函数,将正则表达式的字符串形式编译为Pattern实例,然后使用Pattern实例处理文本并获得匹配结果(一个Match实例),最后使用Match实例获得信息,进行其他的操作。

举一个简单的例子,在寻找一个字符串中所有的英文字符:

1
2
3
4
5
import re
pattern = re.compile('[a-zA-Z]')
result = pattern.findall('as3SiOPdj#@23awe')
print result
# ['a', 's', 'S', 'i', 'O', 'P', 'd', 'j', 'a', 'w', 'e']
原创粉丝点击