nswerhun
nswerhun

Reputation: 154

Regex to not match expression when before equal sign?

foo = bar
foo        = bar
bar = foo
bar = foo asdfghjk

Is there a way to match every foo that is NOT on the left of an equal sign, so in this case the last two? I've tried:

(foo)([^=])

But that doesn't work. I'm not that good with regex, I don't know what to do from here.

Also, I am trying to pattern match in vim. I don't know if that makes a difference. I am aware the parentheses have to be escaped with \.

Upvotes: 2

Views: 490

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

You can use

:g/\vfoo(.*\=)@!/

Vim test:

enter image description here

The global pattern

  • \v - sets the very magic mode to avoid overescaping
  • foo - a foo string
  • (.*\=)@! - Vim-style classic negative lookahead that means that there can't be a = after any 0 or more chars (but line break chars) as many as possible.

Upvotes: 3

Related Questions