Python3通过re模块提供对正则表达式的支持。使用re的一般步骤是先将正则表达式的字符串形式编译为Pattern实例,然后使用Pattern实例处理文本并获得匹配结果(一个Match实例),最后使用Match实例获得信息,进行其他的操作。
举个例子:
import re
pattern = re.compile(r'This')
match = pattern.match('This is a test')
if match:
print (match
Python3通过re模块提供对正则表达式的支持。使用re的一般步骤是先将正则表达式的字符串形式编译为Pattern实例,然后使用Pattern实例处理文本并获得匹配结果(一个Match实例),最后使用Match实例获得信息,进行其他的操作。
举个例子:
import re
pattern = re.compile(r'This')
match = pattern.match('This is a test')
if match:
print (match