Hailiang Zhang
Hailiang Zhang

Reputation: 18950

search a word not containing something in "vi"

I want to search words as following in "vi":

"AA" not followed by "BB" or "CC"

i.e.

AAXC... -- OK
AABB... -- NOT OK
AACC... -- NOT OK

Not sure how to do that.

Upvotes: 1

Views: 398

Answers (2)

0x90
0x90

Reputation: 41022

vim supports regex searching and substituting http://vimregex.com/:

That is the desired regex:

AA((?!B{2})|(?!C{2}))

The above regex can be validate here.


In vim:

/^\(AA\)\(BB\)\@!.*$

will find all the AA.. format strings and will skip AABB strings.

  1. in order to skip AACC and AABB you can use:

    /^\(AA\)\(\(BB\)\|\(CC\)\)\@!.*$
    

    or equivalently:

    /^\(AA\)\(\(B\{2\}\)\|\(C\{2\}\)\)\@!.*$
    
  2. eliminate the ^ from the strings if you want to find strings like AAXC inside string of the form BAAXC.

Upvotes: 3

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31203

You can search using regular expressions, just use A[^BC], if it's actually that simple (just characters, not words).

Upvotes: 0

Related Questions