Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression - Block Spam

Tags:

regex

php

Could someone lead me on finding a regular expression that blocks a comma separated list of Spam words I already have?

The regular expression needs to match a string with the spam word list I already have.

Not that it matters, but I am using PHP.

like image 785
Immanuel Avatar asked Jan 19 '26 08:01

Immanuel


2 Answers

Try this:

 \b(word1|word2|...)\b

The \b will match between a word character and a non-word character (so that the expression won't match if the words appear as part of a longer word).

like image 142
Aaron Digulla Avatar answered Jan 20 '26 22:01

Aaron Digulla


You could generate a regular expression that matches anything containing a spamword from you list by replacing you commas with | and adding round brackets and word boundaries.

If your spamlist is "spam1,spam2,spam3", your regular expression would be "\b(spam1|spam2|spam3)\b".

like image 21
Jens Avatar answered Jan 20 '26 22:01

Jens