使用re.findall()查找字符串,使用了$标识查找到字符串结尾,但是返回的list中最后一个元素为空字符串
s = 'ABCD'
m = re.findall(r'.*$', s)
if m:
print(m)
# output ['ABCD', '']
为什么在第一次已经匹配到字符串结尾$的情况下,findall没有停止还做了一次匹配?而相比^就没有在开始处^多做一次这样的查找:
s = 'ABCD'
m = re.findall(r'^.*', s)
if m:
print(m)
# output ['ABCD']
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
别的语言也是这样
js
python3
这几个是
零宽断言,只匹配位置,不消耗字符。位置之后的零个字符
''正好符合.*