Andreas Otto
Andreas Otto

Reputation: 329

Regular expression matching LINE but NOT a single special word

I have a regex matching a line having the word "Mq\w+GetContext"…

^.*Mq\w+GetContext.*$

example: MqBufferGetContext, MqDumpGetContext, MqErrorGetContext etc

and NOW my problem… I dont't want to have the line matching the word…

MqErrorGetContext

try to use

^.*Mq(?!Error)GetContext.*$

does not work.

Upvotes: 0

Views: 22

Answers (2)

The fourth bird
The fourth bird

Reputation: 163362

You might use a negative lookahead (?! if it is supported to assert that MqErrorGetContext is not present.

You could use a word boundary \b for it not be part of a longer match.

^(?!.*\bMqErrorGetContext\b).*Mq\w+GetContext.*$

Upvotes: 2

wp78de
wp78de

Reputation: 18960

Almost there, try it like this:

^.*Mq(?!Error)\w+GetContext.*$

When you use the negative lookahead, the sequence to look at must be ahead of it.

Demo

Upvotes: 1

Related Questions