我希望这3个断言能够通过,正则表达式使用了什么错误?字边界和小于符号
最后一个断言失败。在开始或一个字的结束和字
import re
def line_has_word(line, word):
expr = re.compile(r'\b' + re.escape(word))
return expr.search(line) is not None
assert line_has_word('foo', 'foo')
assert not line_has_word('zfoo', 'foo')
assert line_has_word('<foo', '<foo')
@anubhava,是的,但有一个'断言not' –
确定了它。尝试:'expr = re.compile(r'(^ | \ s)'+ re.escape(word))' – anubhava
@anubhava,作品,谢谢! (我仍然试图理解你的正则表达式) –