Chen Li Yong
Chen Li Yong

Reputation: 6097

A Regex that search for lines that contains a string, and not contains another string

I want to analyze an error log. So I decided to search for all the error header in the error log using Notepad++ so I can get all the first line of the errors on search result (which contains short description about the error) to determine if I need to look deeper into it. But the error log apparently is full of 'useless' error log from one kind of event, like 90% of it, so it kinds of hide the real error, like searching a needle in haystack.

So from this example made up error log:

ERROR on Server1: Network connection reset.
    DETAIL: The client is gone.
ERROR on Server2: Network connection reset.
    DETAIL: The client is gone.
ERROR on Server1: Network connection reset.
    DETAIL: The client is gone.
ERROR on Server1: Null Pointer Error.
    DETAIL: Object 'Cart' does not exists.
    STACKTRACE: 
        at UpdateCart function
        at AddProducttoCart function
ERROR on Server2: Network connection reset.
    DETAIL: The client is gone.
ERROR on Server2: IO Error
    DETAIL: The resource on URL (www.example.com/data.xls) does not exists.
ERROR on Server2: Network connection reset.
    DETAIL: The client is gone.

I want to create a regex on Notepad++ search that search for line that contains string "ERROR on" but does not contain "Network connection reset", so the search result will only contain:

ERROR on Server1: Null Pointer Error.
ERROR on Server2: IO Error

How can I do that? I've read somewhere that inverse matching on regex is somewhat hard and unusual, but it's possible. Thanks.

Btw, I've tried some other way to do this, like finding for "ERROR on" + (.*) + "Network connection reset", then replace it with empty string, so that next time I search for "ERROR on", they will not appear. But the error log becomes scrambled with weird symbols after the search and replace, and Notepad++ kinda crashing after that. I don't know. I never have any luck search and replace on Notepad++ using regex.

Upvotes: 0

Views: 831

Answers (2)

vks
vks

Reputation: 67968

^ERROR on (?:(?!Network connection reset).)*$

You can use a lookahead in your regex.See demo.

https://regex101.com/r/pV0fH2/1

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174726

I would use negative lookahead.

^(?!.*?\bNetwork connection reset\b).*\bERROR on\b.*

Upvotes: 2

Related Questions