1. compile(pattern, flag)
import re
c = re.compile('abc')
type(c) # -> <class '_sre.SRE_Pattern'>
c # -> re.compile('abc')
备注:
1.1 re.compile() 返回一个 预编译过的正则表达式对象。
1.2 方法文档
compile(pattern, flags=0)
Compile a regular expression pattern, returning a pattern object.
2. match(pattern, string, flags=0)
import re
m = re.match('foo', 'foo')
type(m) # -> <class '_sre.SRE_Match'>
m # -> <_sre.SRE_Match object; span=(0, 3), match='foo'>
m is None # -> False
备注:
2.1 match() 试图从字符串起始部分对模式进行匹配,如果匹配成功,则返会匹配对象,如果匹配失败,则返回None.
2.2 在实际操作中,最好不要省略if判断,都在可能包AttributeError异常
2.2 官方文档
match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.