"""使用模块re实现字符串的匹配"""
"""
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.
参数pattern是一个正则表达式,或对正则表达式预编译之后得到的对象
参数flag是一个可选标志,用于控制正则表达式匹配方式。如有:是否区分大小写、多行匹配等
"""
import re
print(re.match(r'...', 'a\nc')) # None
print(re.match(r'...', 'ab')) # None
# <re.Match object; span=(0, 3), match='abc'>
print(re.match(r'...', 'abcd'))
# <re.Match object; span=(0, 7), match='abCDefa'>
print(re.match(r'[a-z]{7}', 'abCDefab', re.I))
# <re.Match object; span=(0, 10), match='a\nbcdfEfhi'>
print(re.match(r'...[a-z]{7}', 'a\nbcdfEfhijK', re.I|re.S))
"""
匹配成功re.match方法返回一个匹配的对象,否则返回None。
我们可以使用group(num) 或 groups() 匹配对象函数来获取匹配表达式。
"""
# <re.Match object; span=(0, 3), match='abc'>
match_objs = re.match(r'...', 'abc')
# abc
print(match_objs.group())