Search code examples
regexqregexp

How to exclude one set of words but include another in qregexp?


I am trying to exclude a group of words but include another group of words in a qregexp expression but I am currently having issues figuring this out.

Here are some of the things I tried (this example included all of the words):

(words|I|want|to|include)(?!the|ones|that|should|not|match)

So I tried this (which returned nothing):

^(words|I|want|to|include)(?:(?!the|ones|that|should|not|match).)*$

Am I missing something?

Edit: The reason why I need such an unusual regex (include/exclude) is because I want to search through a series of articles and filter the ones that have the included words in them but not if they also have the excluded words in them.

So for example if article A is:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

and article B is:

Vivamus fermentum semper porta.

Then a regex that includes lorem would filter article A but not B. But if ipsum is a word that I'm excluding, I do not want article A to be filtered.

I considered doing a regex to filter out the articles with the words that I want and then run a second regex excluding articles from the first set that I do not want, but unfortunately the software I am using does not allow me to do this. I can only run one regular expression.


Solution

  • ^(?:(?!\b(?:the|ones|that|should|not|match)\b).)*\b(?:words|I|want|to|include)\b(?:(?!\b(?:the|ones|that|should|not|match)\b).)*$
    

    You need to add lookahead to both parts after you find words whcih should match.See demo.

    https://regex101.com/r/bK9wF1/3

    or

    ^(?!.*\b(?:the|ones|that|should|not|match)\b)(?=.*\b(?:words|I|want|to|include)\b).*$
    

    Add both conditions under lookaheads.See demo.

    https://regex101.com/r/uF4oY4/60