Stalpotaten
Stalpotaten

Reputation: 501

vim regex: Match with negation in middle of pattern

I am trying to learn regular expressions in vim and have gotten stuck on the following task:

I want to make a substitute which matches all of the following lines - and similar - except for the one containing Look aXXhead. With similar I mean just anything in between the a and the head except XX.

Look ahead
Look aYhead
Look aXhead 
Look aXXhead    
Look aXXXhead   

In case it is relevant I had my highest hopes when trying

:%s/Look a\(XX\)\@!head/*\0
:%s/Look a\(.*\&\(XX\)\@!\)head/*\0

which only matches Look ahead due to the zero width of the \(XX\)\@! I pressume.

Tries like

:%s/Look a\(XX\)\@!.*head/*\0

misses the "triple X" line.

So, how is it done?

P.S. This is my first post on stack exchange so please help and correct me in case there is better ways or places for this question.

Upvotes: 3

Views: 560

Answers (2)

Kent
Kent

Reputation: 195269

how about:

/\vLook a(XXhead)@!.*head

or without very magic:

/Look a\(XXhead\)\@!.*head

Upvotes: 1

yolenoyer
yolenoyer

Reputation: 9465

Try with this pattern:

/\(Look aXXhead\)\@!\&Look a.*head/

It's made of tho parts:

  • \(Look aXXhead\)\@!

    match at every position in the file BUT not at /Look aXXhead/

  • Look a.*head

    match also /Look aXXhead/

The \& operator tells that each of these parts have to match at the same pos.

Upvotes: 2

Related Questions