Blocking email address with regular expression not working

AI-generated summary

Blocking email address with regular expression not working

devnull reported that the regular expression (^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$) used to block email addresses was not working. simon pointed out that the issue was with the regular expression itself, which would only match email addresses if they were posted on their own line.

simon provided an alternative regular expression \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b that should work for blocking email addresses. This solution was confirmed to be working by devnull.

However, devnull raised another issue, where the regular expression was also blocking email addresses in private messages. Unfortunately, as noted by simon, it is not possible to apply watched words only to public posts. A potential workaround suggested by simon is to have users enter email addresses in a way that doesn’t get matched by the regex, such as adding a space before the @ character.

Hey all,

the blocking of posts with regular expressions is not working anymore. The RegEx Test Tool is finding that mail-address but users are able to post mail-addresses:

RegEx: (^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)

Is that a bug?

Thanks a lot for your help!

I think there is a problem with that regular expression. It looks like it will fail unless the email address is posted on its own line in the composer. You can confirm this by adding some text other than an email address when you test the regex on Discourse. Even just adding a space before the email address will cause it to fail.

Here’s a regular expression that should work for email addresses:

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b

For reference, I found that regex here: How to Find or Validate an Email Address.

Thanks! That’s working. Now another problem: Is it possible to use that RegEx for public posts only? Now it’s not possible to send mail-addresses through private messages as well. This is an unfortunate behavior.

No, it is not possible to only have watched words applied to public posts. With that regex in place, you’ll need to get your users to enter email addresses in a way that doesn’t get matched by the regex. Maybe something like adding a space before the @ character would work.