Oliver Spryn
Oliver Spryn

Reputation: 17348

Regular Expression: Match with Containing and not Containing

I am trying to parse through a log file which contains many lines with the word ERROR followed by the class and method from which an exception was thrown, like this:

2016-08-18 19:38:17 ERROR  [KUHOMNYXARCH] [ 47] [c43215c4-acfd-491b-b418-653b53195be3] [MultiFormatRoiStreamer.GetImageRoi]
2016-08-18 19:39:46 ERROR  [KUHOMNYXARCH] [ 46] [10ba0a0e-0ede-4d0e-a582-d7e811bb7ade] [ExceptionHandlingBehavior.ProvideFault] 

I am trying to write a regular expression which will match all lines containing ERROR, but not containing MultiFormatRoiStreamer.GetImageRoi and ExceptionHandlingBehavior.ProvideFault. My attempts to use negative lookahead assertions have not worked:

ERROR\s+\[KUHOMNYXARCH\] \[ [0-9]+\] \[[0-9a-f\-]+\] (?!(\[MultiFormatRoiStreamer\.GetImageRoi\]|\[ExceptionHandlingBehavior\.ProvideFault\]))
ERROR.+(?!(MultiFormatRoiStreamer|ExceptionHandlingBehavior))

Any ideas on this?

Upvotes: 0

Views: 43

Answers (1)

user557597
user557597

Reputation:

You can use this to find the line(s)

(?m)^.*?ERROR[^\S\r\n]+(?:(?!MultiFormatRoiStreamer\.GetImageRoi|ExceptionHandlingBehavior\.ProvideFault).)+$

It uses multi-line mode (?m)

Expanded:

 (?m)
 ^ 
 .*? 
 ERROR [^\S\r\n]+ 
 (?:
      (?!
           MultiFormatRoiStreamer\.GetImageRoi
        |  ExceptionHandlingBehavior\.ProvideFault
      )
      . 
 )+
 $

Upvotes: 2

Related Questions