I am trying to search for lines that contain any permutation of a group of words (case-insensitively). For example, if I am interested in the words foo
and bar
, I would want to match the first four lines but not the last four lines in the following file
:
Foo and bar.
Bar and foo.
The foo and the bar.
The bar and the foo.
Foobar.
Barfoo.
The foobar.
The barfoo.
Having looked at this post, I realize I can construct something like this in perl
:
perl -n -e 'print if (/\bfoo\b.*?\bbar\b/i || /\bbar\b.*?\bfoo\b/i)' file
which correctly matches only the first four lines. Alternatively, using a look-ahead construct as suggested by this post, the match can be made with slightly more concise code:
perl -n -e 'print if (/(?=.*\bfoo\b)(?=.*\bbar\b)/i)' file
I cannot, however, figure out how to write these in vim
regex syntax, which I find to be far more byzantine than perl
regex syntax. I have tried many different expressions in vim
using the search function (/
or ?
), but none of them produce successful matches. I realize that instead of the (?=string)
syntax used by perl
, vim
uses \(string\)\@=
and string\&
.
However, a variety of attempts, e.g.:
\c\(foo\)\@=\(bar\)@=
\c\(foo\)\@=\.*\(bar\)@=
\cfoo\&bar\&
(where \c
is used for a case-insensitive match) have all been unsuccessful.
Could someone please demonstrate the correct vim
syntax?