我觉得这种行为有点奇怪:
pattern = re.compile(r"test")
print pattern.match("testsda") is not None
True
print pattern.match("astest") is not None
False
所以,当字符串与末端的模式不匹配时,一切正常。但是当字符串在开始时与模式不匹配时,字符串就不再与模式匹配。你知道吗
相比之下,grep在这两种情况下都取得了成功:
echo "testsda" | grep test
testsda
echo "adtest" | grep test
adtest
你知道为什么会这样吗?你知道吗
re.match
设计为只在字符串开头匹配。作为docs状态:如果您使用
re.search
,您应该可以:印刷品
相关问题 更多 >
编程相关推荐