同様の 質問 が なされていますが、この特定の質問に答えるものはありません。
この代表的なテキストを考えると:
foo
bar
foo bar
bar foo
foo bar foo
bar foo bar
正規表現を使用して、単語を含むが単語を含まない行のみに一致させることはできfoo
ますbar
か?
この目的の正規表現を上記のテキストで実行すると、次の結果になります。
foo
これを行うためのかなり簡単な方法を次に示します。
^(?!.*\bbar\b).*\bfoo\b.*
説明:
^ # starting at beginning of the string
(?! # fail if (negative lookahead)
.*\bbar\b # the word 'bar' exists anywhere in the string
) # end negative lookahead
.*\bfoo\b.* # match the line with the word 'foo' anywhere in the string
ルビュラー: http://www.rubular.com/r/pLeqGQUXbj
\b
正規表現では単語境界です。
ヴィムのバージョン:
^\(.*\<bar\>\)\@!.*\<foo\>.*
はい。ルックアラウンドを使用します。
/^.*(?<!\bbar\b.*)\bfoo\b(?!.*\bar\b).*$/