Regexp for subdomain

Subdomain

According to the pertinent internet recommendations (RFC3986 section 2.2, which in turn refers to: RFC1034 section 3.5 and RFC1123 section 2.1), a subdomain (which is a part of a DNS domain host name), must meet several requirements:

  • Each subdomain part must have a length no greater than 63.
  • Each subdomain part must begin and end with an alpha-numeric (i.e. letters [A-Za-z] or digits [0-9]).
  • Each subdomain part may contain hyphens (dashes), but may not begin or end with a hyphen.

Here is an expression fragment for a subdomain part which meets these requirements:

[A-Za-z0-9](?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9])?

Note that this expression fragment should not be used alone – it requires the incorporation of boundary conditions in a larger context, as demonstrated in the following expression for a DNS host name…

DNS host name

A named host, (not an IP address), must meet additional requirements:

  • The host name may consist of multiple subdomain parts, each separated by a single dot.
  • The length of the overall host name should not exceed 255 characters.
  • The top level domain, (the rightmost part of the DNS host name), must be one of the internationally recognized values. The list of valid top level domains is maintained by IANA.ORG. (See the bare-bones current list here: http://data.iana.org/TLD/tlds-alpha-by-domain.txt).

With this is mind, here a commented regex (in PHP syntax), which will pseudo-validate a DNS host name: (Note that this incorporates a modified version of the above expression for a subdomain and adds comments to this as well).

Update 2016-08-20: Since this answer was originally posted back in 2011, the number of top-level domains has exploded. As of August 2016 there are now more than 1400. The original regex to this answer incorporated all of these but this is no loger practical. The new regex below incorporates a different expression for the top-level domain. The algorithm comes from: Top Level Domain Name Specification draft-liman-tld-names-06.

$DNS_named_host="%(?#!php/i DNS_named_host Rev:20160820_0800)
    # Match DNS named host domain having one or more subdomains.
    # See: http://stackoverflow.com/a/7933253/433790
    ^                     # Anchor to start of string.
    (?!.{256})            # Whole domain must be 255 or less.
    (?:                   # One or more sub-domains.
      [a-z0-9]            # Subdomain begins with alpha-num.
      (?:                 # Optionally more than one char.
        [a-z0-9-]{0,61}   # Middle part may have dashes.
        [a-z0-9]          # Starts and ends with alpha-num.
      )?                  # Subdomain length from 1 to 63.
      \.                  # Required dot separates subdomains.
    )+                    # End one or more sub-domains.
    (?:                   # Top level domain (length from 1 to 63).
      [a-z]{1,63}         # Either traditional-tld-label = 1*63(ALPHA).
    | xn--[a-z0-9]{1,59}  # Or an idn-label = Restricted-A-Label.
    )                     # End top level domain.
    $                     # Anchor to end of string.
    %xi";  // End $DNS_named_host.

Note that this expression is not perfect. It requires one or more subdomains, but technically, a host can consist of a TLD having no subdomain (but this is rare).

Update 2014-08-12: Added simplified expression for subdomain which does not require alternation.

Update 2016-08-20: Modified DNS host name regex to (more generally) match the new vast number of valid top level domains. Also, trimmed out unnecessary material from answer.

Leave a Comment