Regex (PCRE) matching row with excluded words

Published on Author gryzli

Recently I had a task to match a given row, only if it contains words, different than a predefined non-matching word list. 

 

Here is the example:

  1. Let say we have the excluded words: “bad_word1|bad_word2”
  2. We must match a row only if there words, different than the bad words from above

 

That should match:

MATCH:good_word1 good_word2 bad_word1 bad_word2

, because we have good words (different than the excluded ones)

 

If our string consists of only bad words it should not match:

DON'T MATCH:bad_word1 bad_word2 bad_word2

 

In my specific requirement I had a string of Cookie (HTTP Cookie header), where I wanted to match only if there are cookie names, different than a predefined list of excluded cookies.

The final regex looked like tihs

WORKING REGEX: "(^|; *)(excluded_cookie)\w*="

 

Now let say we have the following Cookie: headers:

 

This will match: Cookie: good_cookie2=some_value; excluded_cookie=other_value;

 

This wont match: Cookie: excluded_cookie=some_value;

 

Finally here is a link at regex101, where you could test this regex

https://regex101.com/r/SwI3CD/8