Guy I
Guy I

Reputation: 1

Regular expression - look for a substring with excluding another substring

Similar subjects have been discussed many time, but up to now, I've not found the right syntax, if it possible, corresponding to my need.

I want to raise alerts depending on a particular substring in a logfile but avoiding to have alerts when a another substring (to ignore) is present on the same line.

Substrings are always in the same order on the line when both are present with other strings before, between and after like this:

bla bla bla StringToIgnore bla bla bla StringToLookFor bla bla bla

To summarize, with a logical syntax, I would like to do: StringToLookFor AND (NOT StringToIgnore)

I must use for this a unique regular expression. The regular expression is entered though a field on the tool generating the logs. No other combination, logic, code or script is possible.

Upvotes: 0

Views: 29

Answers (1)

Aran-Fey
Aran-Fey

Reputation: 43166

I think this does what you want:

^(?=.*StringToLookFor)(?!.*StringToIgnore).*

with multi-line modifier m.

regex101 demo.

The .* bit at the end is only to make the match easier to see, it's not actually required.

Upvotes: 1

Related Questions