Python のルックアラウンドに関して問題があります。
>>> spacereplace = re.compile(b'(?<!\band)(?<!\bor)\s(?!or\b)(?!and\b)', re.I)
>>> q = "a b (c or d)"
>>> q = spacereplace.sub(" and ", q)
>>> q
# What is meant to happen:
'a and b and (c or d)'
# What instead happens
'a and b and (c and or and d)'
正規表現は、「and」または「or」という単語の隣にないスペースに一致するはずですが、これは機能していないようです。
誰でもこれで私を助けることができますか?
編集:コメント者に応えて、正規表現を複数の行に分割しました。
(?<!\band) # Looks behind the \s, matching if there isn't a word break, followed by "and", there.
(?<!\bor) # Looks behind the \s, matching if there isn't a word break, followed by "or", there.
\s # Matches a single whitespace character.
(?!or\b) # Looks after the \s, matching if there isn't the word "or", followed by a word break there.
(?!and\b) # Looks after the \s, matching if there isn't the word "and", followed by a word break there.