Negative Lookahead Regular Expression

7 posts / 0 new
Last post
rssuser

I am using the information on QRegExp from http://doc.qt.io/qt-4.8/qregexp.html and am having a bit of a problem with negative lookaheads. It seems that it is ignoring them when I use regular expressions on my news filter.

Here is the code I am using:

(?!words|I|do|not|want|to|match)(the|ones|that|should|be|included)

But it is including the words from the negative expression in my news filter.  Am I missing something?

Sheldon
Did you try the non-part (?

Did you try the non-part

(?!words|I|do|not|want|to|match)

only to see if it works?

I use this

^(?:(?!text).)*$

for exclusions.

rssuser
You're right. My negative

You're right. My negative lookahead wasn't working and the one you mention works great. But when I add the words I want included it doesn't return the desired results.

I've tried both

^(?:(?!words|I|do|not|want).)*$(the|ones|that|should|be|included)

and

^(?:(?!words|I|do|not|want)(the|ones|that|should|be|included).)*$

and I'm not getting the results I'm looking for. Is there a particular order or placement for the expressions?

Sheldon
I shortly tested this

I shortly tested this positive:

^(?:(?!words?!I?!do?!not?!want?the?ones?that?should?be?included).)*$

rssuser
That didn't do it for me.

That didn't do it for me. Here is what I got to work in case anybody needs to use this in the future:

^(?=.*(?:the|ones|that|should|be|included))(?!.*(?:words|I|do|not|want)).*$

rssuser
Hmm, I seem to be having

Hmm, I seem to be having problem with the word boundary \b. Is anyone else having problems with that escape character?

For instance, (?!.*(?:words|\bI\b|do|not|want)).*$ is different from (?!.*(?:words|I|do|not|want)).*$ in that the second one will match any word with the letter I in it but the first will only match the whole word I.

Sheldon
The behaviour is correct. See

The behaviour is correct. See f.e. this description.

http://www.rexegg.com/regex-boundaries.html

What are you trying to achieve?
I guess it's \B you need.