How to make a regex to match all internet domains regardless of the extention

Go To


I need to a model validation to block personal email accounts.

I have the following:


The regex validation:

:format => {
  :without => /#{{|a| Regexp.quote(a)}.join('|')}/

The reason yahoo has a dot at the end is to allow but block all the personal domains like,, etc...

This regex is failing and causing to get rejected... Any suggestions on how this can be updated to make the use case above pass? Thanks

2012-04-04 01:21
by Rachela Meadows
Can't reproduce, the regex doesn't (and shouldn't) match "" - Niklas B. 2012-04-04 01:27


Try doing your match without Rails to help narrow down what's happening:

PERSONAL_DOMAINS_REGEXP = /#{{|a| Regexp.quote(a)}.join('|')}/
=> /yahoo\.|ymail|verizon/

=> nil

=> #<MatchData "yahoo.">
2012-04-04 01:37
by joelparkerhenderson


Try the following regex:

/(^|\.)(#{{|a| Regexp.quote(a)}.join('|')})\.[^.]+$/i

This will match all domains whose second component is exactly one of the list.

so it will match:

etc, but not

(change "yahoo." back to "yahoo")

Note also the "/i" at the end. domains are case-insensitive, so you will need this.

2012-04-04 10:43
by Michael Slade