62

I have many lines containing the names of US Presidents Carter, Bush, Clinton, Obama. Some contain 1 of those names, some 2, some 3, some all 4 of them (in any order).

I know how to search for Carter AND Clinton AND Obama ->

:g/.*Carter\&.*Clinton\&.*Obama/p

I know how to search for Carter AND (Clinton OR Bush) ->

:g/.*Carter\&\(.*Clinton\|.*Bush\)/p

(There are most certainly better ways to do that)

But I can't figure how to search (and I looked at the related questions), e.g., for Bush AND Clinton NOT Carter and even less how to search, e.g., for Bush AND Clinton NOT (Carter OR Obama).

2
  • 1
    I don't have a full answer for you, but you can use negative look-ahead to achieve negative string matches. It's not pretty though. See :help \@<!. If it were me, I'd use a grep | grep -v approach from the command line instead. Oct 7, 2010 at 17:34
  • @ire_and_curses \@<! is negative look-behind, not look-ahead.
    – ZyX
    Oct 7, 2010 at 17:48

2 Answers 2

69

To represent a NOT, use the negative assertion \@!.

For example, "NOT Bush" would be:

^\(.*Bush\)\@!

or using \v:

\v^(.*Bush)@!

Important: note the leading ^. While it's optional if you only use positive assertions (one match is as good as any other), it is required to anchor negative assertions (otherwise they can still match at the end of a line).

Translating "Bush AND Clinton AND NOT (Carter OR Obama)":

\v^(.*Bush)&(.*Clinton)&(.*Carter|.*Obama)@!

Addendum

To explain the relationship between \& and \@=:

One&Two&Three

is interchangeable with:

(One)@=(Two)@=Three

The only difference is that \& directly mirrors \| (which should be more obvious and natural), while \@= mirrors Perl's (?=pattern).

5
  • Piet Delport : I tried your solution and it worked perfectly. Thanks a lot. But may I say, as a newbie, that I am amazed by the number of different ways Vim has to achieve things successfully (here your solution and ZyX's)?
    – ThG
    Oct 7, 2010 at 19:27
  • 1
    Piet -- Great answer, along with Zyx's. I take it that your solution is still using the \& operator that Zyx advises to avoid. Still, I like it because it's easier to see the actual boolean structure of the expression, especially the magic version. Oct 7, 2010 at 19:48
  • There's absolutely no reason to avoid \&: this is Vim, not Perl. :) (Even if you are planning to use your regexes in both Vim and Perl/PCRE, it takes the same amount of effort to translate both \& and \@= to Perl's syntax: avoiding \& wouldn't buy you anything, anyway.)
    – Pi Delport
    Oct 7, 2010 at 20:31
  • I advised to avoid because 1) You could get used to it and have problems with understanding or writing Perl-style regular expressions. 2) There is no negative branches, but there is negative look-ahead. Why do we need two different things to express one idea?
    – ZyX
    Oct 7, 2010 at 20:39
  • Wow, that \v is awesome.
    – Aaron Gray
    Nov 8, 2013 at 19:41
15

If you want to use Perl-style regular expressions after vim, forget about \&: it is a vim-specific feature which is useless since vim also has lookaheads, so any r1\&r2 can be rewritten as as \%(r1\)\@=r2. But lookaheads are better as there is a negative version of it and they are also available in most of Perl-style regular expression engines. Your (Bush AND Clinton AND NOT (Carter OR Obama)) can be expressed in the following way:

g/^\%(.*\%(Carter\|Obama\)\)\@!\%(.*Bush\)\@=.*Clinton/

Or, with very magic:

g/^\v%(.*%(Carter|Obama))@!%(.*Bush)@=.*Clinton/

See :h /\@=

About inner logic: look-ahead is like branches: for regex (reg1)@=reg2 assuming that reg2 matches at position N (match starts at position N), regex engine checks whether reg1 also matches at this position. If it does not, then the position is discarded and regex engine tries next possible match for reg2. Same for the negative look-ahead, but with the difference that regex engine discards the position if reg1 does match.


Example:

Regex: (.b)@!a.

String: aba.

  1. Found match: a matches at position 0 (aba). Trying to match look-ahead: . matches a (aba) and b matches b (aba), look-ahead matches, discarding position.
  2. Position 1 (aba) does not match a.
  3. Found match: a matches at position 2 (aba). Trying to match look-ahead: . matches a (aba), but b does not match: no symbols left, look-ahead fails. Result: regex matches at position 2.
3
  • ZyX : First of all, thanks : I tried it and it worked (of course). The problem left is that I do not understand the inner logic of your regex. This being a forum and not a classroom, I shall follow your advice and look around Vim help (if it is not too hard for a regex newbie
    – ThG
    Oct 7, 2010 at 18:01
  • I did not know that. How must I proceed ?
    – ThG
    Nov 11, 2010 at 8:00
  • @ThG In the left column near the question you can see an up triangle, a number, a down triangle and a tick. Latter is used to accept answer, triangles are used to vote for questions.You must have read FAQ, accepting answers is described there.
    – ZyX
    Nov 11, 2010 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.