-1

I am looking for a domain validator regex which validates domain. Preferably I want to use some built-in regex using URI::Regexp.

Domain scheme and protocol are optional. It should validate all domains like:

abc.com
abc.cm
ab.com/aka/jaka
abc.aio.com

It should invalidate domains of like:

abc
abc. jska
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
utcghghvgh
  • 119
  • 1
  • 6
  • 2
    There are similar questions, take a look: http://stackoverflow.com/questions/1128168/validation-for-url-domain-using-regex-rails – mrzasa Sep 06 '16 at 11:00
  • 1
    Your third example is not a valid domain name. Domain names aren't allowed to contain slashes. – Jörg W Mittag Sep 06 '16 at 16:09
  • With the internet opening up new TLDs, it's going to be difficult to build a validating regex unless you regularly pull in a current list from a registrar or DNS. – the Tin Man Sep 07 '16 at 00:45
  • @theTinMan: The syntax for valid domain names is defined clearly in the RfC. I'd still not do it with a `Regexp`, but there is no reason this should be difficult: maximum 127 labels, maximum length of each label 63 octets, total maximum length 255 octets, labels must start with a letter, end with a letter or digit, and can have letters, digits and hyphens in between. Labels are separated by periods. The empty domain name is valid. The domain name `'.'` is valid. I think that's all the rules. That can be certainly validated, maybe even with a `Regexp`. – Jörg W Mittag Sep 07 '16 at 08:52
  • The form of the TLD can be validated, but whether it is an actual domain won't be possible without actually trying to hit that URL, or getting information from an authoritative source. This is the same as dealing with email addresses; An address can pass a validator and still be invalid because it's not alive. – the Tin Man Sep 07 '16 at 18:11
  • @theTinMan: you are confusing domain names and host names. Domain names are just identifiers, nothing more. There does not need to be any machine that responds under that name. Think about the reverse domain name convention for Java packages. Nobody is going to create a domain name entry for every Java package they create. Also, even *if* we were talking about host names, there is nothing stopping me from creating an entry for `tinman.google.com` in my name server, and then that hostname *will* resolve perfectly fine from within my network. – Jörg W Mittag Sep 16 '16 at 10:42

1 Answers1

1

Something like this:

[a-z0-9-]+\.[a-z0-9-\/\.]+

You can test and improve it here: http://rubular.com/

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
gmrash
  • 650
  • 7
  • 15
  • This fails for the following valid domain names: `''`, `'.'`, `'a'`. It also validates the following invalid domain names: `'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl.a'`, `'a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a'`, … – Jörg W Mittag Sep 06 '16 at 16:37
  • … `'abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdef'` – Jörg W Mittag Sep 06 '16 at 16:38