user1256045
user1256045

Reputation: 23

regex - find specific string but not when present in certain sentence

I am not sure if it is possible with one regex line. I want to find sentence with particular string but not when it present in specific sentence.

This is like grep 'word' file | grep -iv "particular sentence"

Eg: Input:

Hello world foo here

foobar here there

foo with bar

bar with foo now

result should be to find word 'foo' but not when the sentence is 'bar with foo now': Output:

Hello world foo here

foobar here there

foo with bar

Is this possible with one line regex pattern?

Upvotes: 0

Views: 859

Answers (1)

mathematical.coffee
mathematical.coffee

Reputation: 56935

You could do:

^(?!bar with foo now).*?foo.*$

Which says "match a line that isn't "bar with foo now", but has "foo" in it".

However, you'll only get one match per line. So if you had:

Hello my foo is foo.

That will match, but only once for the entire line (as opposed to twice, once for each foo).

Upvotes: 1

Related Questions