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?


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 ).

链接地址: http://www.djcxy.com/p/77022.html

上一篇: 匹配字词不以&开头,

下一篇: 找到特定的字符串,但不出现在某个句子中时