Regex for validating domain name

Posted by / 07-Dec-2017 13:02

Mine is more simple, I need to validate a domain name: So a domain in its rawest form - not even a subdomain like 4 revised: TLD should actually be labelled "subdomain" as it should include things like uk -- I would imagine the only validation possible (apart from checking against a list) would be 'after the first dot there should be one or more characters under rules #1 Thanks very much, believe me I did try! When it comes to uk, and some Japanese domains, I'm sure you will have to think twice before using regex for that.

My personal thought is that regex is not enough to validate a domain to a real-life domain.

In the near future this will be likely moved to

Firstly sorry for the 10,000th Reg Ex question, I realise there are other domain related questions but the regex is either not working properly, too complex, or for urls with subdomains, protocols, and filepaths.

) but maximum length restrictions are not enforced.

That’s a good question Michel, I’d have to look into how to define allowable Unicode characters with PCRE. I prefer to stick with functions that are native to PHP 4 for the sake of backwards compatibility.

\.) ((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/i It checks against all known tlds and ensures that the domain name begins and ends with an alphanumeric character, allowing for dashes and sub-domains.

Single character domains are allowed (did you know Pay Pal owns

It just makes more sense, especially from a future maintainability perspective, and will make things easier for you in the future.

The IP address part is actually very efficient on its own, so there are no further changes I would make to this part: The first group is matched by both, the second group is matched by only your regex, and the last group is matched by neither.

regex for validating domain name-61regex for validating domain name-89regex for validating domain name-13

There are ways of going even further and making sure that, when you talk to the recipient’s SMTP server, that it responds accordingly, but that’s one step too far for me right now.