fawzib fawzib - 6 months ago 9
SQL Question

match line that doesnt contain certain words

I have the following string:

ignoreword1,word1, ignoreword2


i would like to match any word that is not
ignoreword1
or
ignoreword2


this is what i have so far

(?s)^((?!ignoreword1).)*$


the main goal is to use the regex as part of postgresql database to select rows where the column match a substring after removing "ignoreword1", "ignoreword2" and the comma ","

Answer

To match any word that is not ignoreword1 or Ignoreword2 use 

\b(?!(?:ignoreword1|ignoreword2)\b)\w+

In PostgreSQL, word boundaries are [[:<:]] and [[:>:]], so use something like:

[[:<:]](?!(?:ignoreword1|ignoreword2)[[:>:]])[a-zA-Z]+

Pattern details:

  • [[:<:]] - leading word boundary
  • (?!(?:ignoreword1|ignoreword2)[[:>:]]) - fail the match if the whole string is either ignoreword1 or ignoreword2
  • [a-zA-Z]+ - one or more any ASCII letters.