これらの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、はい、しかし –
[OK]はそれを得たnot' '主張があります。 'expr = re.compile(r '(^ | \ s)' + re.escape(word))' – anubhava
@anubhava、ありがとう! (私はまだあなたの正規表現を理解しようとしています) –