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