Drupal 7. Drupal 6 had a way to configure a rule to check for disallowed words inside the body or subject line of a private message.

I am trying to adapt that rule to Drupal 7. The purpose of this check is to send a warning message to the sender (email or private message) if banned words are found in his message. This can be expanded to various actions, including a temporary ban then a permanent ban if the user persists.

Here is an export of the first rule (but I can't get it to work:)

{ "rules_warning_to_privatemsg_sender_about_curse_words_" : {
    "LABEL" : "Warning to privatemsg sender about curse words.",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_i18n", "privatemsg_rules" ],
    "ON" : { "privatemsg_insert" : [] },
    "IF" : [
      { "text_matches" : {
          "text" : [ "privatemsg-message:body" ],
          "match" : "word1|word2|word3",
          "operation" : "regex"
    "DO" : [ { "drupal_message" : { "message" : "Bad word!", "type" : "warning" } } ]

Word1, word2, and word3 are the words to check for. They are separated by the "|" (pipe or vertical bar) character.

This format was agreeable with Drupal 6 but I am not sure if that still works with Drual 7.

UPDATE: After studying the Regex website, I was able to get the rule to work changing the expression. See my answer below.

  • Have you tried importing it in d7? – Chris Rockwell Feb 23 '16 at 0:30
  • I do not have access to it any more. It was on an old site I made for someone I lost touch with. However, the "private message" link at the top of this page takes you to the doc page on drupal.org where this is demonstrated. – user14666 Feb 23 '16 at 4:21
  • 1
    Permanent ban is probably via a rules action like removing access to some role or blocking the user entirely, but how would you implement a temporary ban? Using rules scheduler to grant access to some role or unblocking the user again somewhere in the future? – Pierre.Vriens Feb 23 '16 at 7:39
  • @user14666, sorry...my question was just to see if you tried to import and something in particular wasn't working. I imported it into a base d7 install with only the required modules and the import worked just fine. Glad you figured it out! – Chris Rockwell Feb 23 '16 at 18:07
  • @Pierre.Vriens: The User Ban module allows temporary or permanent bans. You can set the length of the ban. Then adding a flag to the message rule. – user14666 Feb 23 '16 at 21:48

Finally found a format that works.

Contrary to what the Drupal documentation says, the format:


does not work.

The proper syntax is:


i.e.: caret then dot then star then open parenthesis then the words to search separated with a vertical bar then closed parenthesis then dot then star then dollar sign.

Many words may be added between the parenthesis. No need to add the plurals as regex will match the singular inside the plural.

Reminder: Regex stands for REGular EXpression so this value must be selected at the bottom of the condition page.

  • Interesting usecase for using rules! Can you think of a variation of your regex in which the 'word' string only occurs once? Not sure if the syntax is correct but something like ^.*word(1|2|3).*$ ...? – Pierre.Vriens Feb 23 '16 at 7:34
  • 1
    I am not an expert on regex but your syntax appears to be correct. You would use this if you had words with the same root and different endings such as "commencing" and "commencement" (commenc(ing|ement)) Another case use of the vertical bar would be ((W|w)ord) unless you tell regex not to be case sensitive. This would be arduous however, especially with a long list of terms, besides being prone to errors. In that case I would precede with (?i) so that regex would not check the character case. For the expression above it would be ^.*(?i)(word1|word2|word3).*$ And it works on Drupal. – user14666 Feb 23 '16 at 19:35
  • 1
    Oops, almost correct! Since you are writing the whole expression you need the parentheses. It would become: ^.*(word(1|2|3)).*$ or ^.*(?i)(word(1|2|3)).*$ – user14666 Feb 23 '16 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.