我查看了re的python文档发现对于|的匹配是从左到右的,可是其中下方源码未按期望的那样得出结果
原代码片段如下:
bsObj.findAll("a", href=re.compile("^(/|.*"+includeUrl+")"))
用以下代码测试
import re
def matchstr(str,str1):
regex = re.compile("^(/|.*"+str1+")")
print(regex.match(str))
print(matchstr("/url","url"))
运行结果是
<_sre.SRE_Match object; span=(0, 1), match='/'>
None
期望能够匹配整个“/url”,但结果并没有,想搞清原因
是因为A|B中.*url整个作为B吗?