re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match() 就返回 none。
re.match(pattern, string, flags=0)
import re
print(re.match('www', 'www.runoob.com').span()) # 在起始位置匹配
print(re.match('com', 'www.runoob.com')) # 不在起始位置匹配
以上实例运行输出结果为:
(0, 3)
None
https://www.runoob.com/python/python-reg-expressions.html
六种在正则表达式中的常见函数,分别是re.match、re.search、re.findall、re.compile、re.split和re.sub。
match函数匹配成功的话,re.match方法返回一个匹配的对象,而不是匹配的正则表达式;通过span()可以获取匹配的位置。
>>> import re
>>> astr='11you are 3344 my apple\n 11开心果,you\n66a77'
>>> re.match('11',astr)
>>> re.match('11',astr).span()
<_sre.SRE_Match object; span=(0, 2), match='11'>
(0, 2)
https://blog.csdn.net/weixin_39565390/article/details/110756526