Warm tip: This article is reproduced from serverfault.com, please click

Regex to not match expression when before equal sign?

发布于 2020-11-30 05:13:48
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 \.

Questioner
nswerhun
Viewed
0
Wiktor Stribiżew 2020-11-30 17:25:44

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.