3

I am struggling combining a negative lookahead assertion @! with the very magic specifier \v

I have a buffer with this content

<div class='vvv'>abc</div>
<div class='ttt'>abc</div>

and I want to search for all classes except vvv. I can do this like so

/class=.\(vvv\)\@!

This finds the second line (class='ttt'), as expected. Yet, when I try to search with the very magic modifier:

/\vclass=.(vvv)@!

the search finds both classes. I had wrongly assumed these searches to be equivalent. Why are they different?

3
  • 4
    be carefull with\=
    – JJoao
    Oct 3, 2015 at 14:07
  • My earlier comment was just ignorant, sorry. JJoao is quite right: the second, very magic pattern needs to be \vclass\=.(vvv)@!. Oct 3, 2015 at 15:13
  • 1
    @JJoao you might want to turn your comment along with Peter's comment into an answer, so that I can accept it. Oct 3, 2015 at 18:02

1 Answer 1

4

The code you wrote is perfect but in \v mode = needs protection.

\vclass\='(ttt)@!

In normal mode \= means optional (the same as \?). In \v mode, = means optional: class= ends up meaning class|clas (I would prefer = were just a normal char).

So, to mark the classes different form ttt we can:

:%s/\vclass\='(ttt)@!.{-}'/MARKED&/

When we have more complex "different-from" patterns it may be easier the long way:

:%s/\vclass\='(tt1|...)'/classAUX=\1/          rename exceptions
:%s/\vclass\='.{-}'/MARKED&/                   treat normal cases
:%s/\vclassAUX/class/                          unrename

\thanks{Peter Lewerin}

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.