Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The selected answer is incomplete/wrong.</p> <p>The regex pattern; </p> <ul> <li><p><strong>should NOT</strong> validate domains such as:<br> <code>-domain.com</code>, <code>domain--.com</code>, <code>-domain-.-.com</code>, <code>domain.000</code>, etc... </p></li> <li><p><strong>should</strong> validate domains such as:<br> <code>schools.k12</code>, <code>newTLD.clothing</code>, <code>good.photography</code>, etc...</p></li> </ul> <p>After some further research; below is the most correct, cross-language and compact pattern I could come up with: </p> <pre class="lang-js prettyprint-override"><code>^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$ </code></pre> <p>This pattern conforms with most* of the rules defined in the specs: </p> <ul> <li>Each label/level (splitted by a dot) may contain up to <strong>63 characters</strong>.</li> <li>The full domain name may have up to <strong>127 levels</strong>.</li> <li>The full domain name may not exceed the length of <strong>253 characters</strong> in its textual representation.</li> <li>Each label can consist of <strong>letters, digits and hyphens</strong>.</li> <li>Labels cannot <strong>start</strong> or <strong>end</strong> with a hyphen.</li> <li>The top-level domain (extension) cannot be <strong>all-numeric</strong>.</li> </ul> <p><strong><em>Note 1</strong>: The full domain length check is not included in the regex. It should be simply checked by native methods e.g. <code>strlen(domain) &lt;= 253</code>.</em><br> <strong><em>Note 2</strong>: This pattern works with most languages including PHP, Javascript, Python, etc...</em> </p> <p>See <a href="https://regex101.com/r/ZIMkXh/1" rel="noreferrer"><strong>DEMO here</strong></a> (for JS, PHP, Python)</p> <h3>More Info:</h3> <ul> <li><p>The regex above does not support <a href="http://en.wikipedia.org/wiki/Internationalized_domain_name" rel="noreferrer">IDN</a>s.</p></li> <li><p>There is no spec that says the extension (TLD) should be between 2 and 6 characters. It actually supports 63 characters. See the current <a href="http://data.iana.org/TLD/tlds-alpha-by-domain.txt" rel="noreferrer"><strong>TLD list</strong> here</a>. Also, some networks do internally use custom/pseudo TLDs. </p></li> <li><p>Registration authorities might impose some extra, <a href="https://www.register.com/policy/domain-extension-rules.rcmx" rel="noreferrer">specific rules</a> which are not explicitly supported in this regex. For example, <code>.CO.UK</code> and <code>.ORG.UK</code> must have at least 3 characters, but less than 23, not including the extension. These kinds of rules are non-standard and subject to change. Do not implement them if you cannot maintain.</p></li> <li><p>Regular Expressions are great but not the best effective, performant solution to every problem. So a native URL parser should be used instead, whenever possible. e.g. Python's <a href="https://docs.python.org/2/library/urlparse.html" rel="noreferrer"><code>urlparse()</code></a> method or PHP's <a href="http://php.net/manual/en/function.parse-url.php" rel="noreferrer"><code>parse_url()</code></a> method...</p></li> <li><p>After all, this is just a format validation. A regex test does not confirm that a domain name is actually configured/exists! You should test the existence by making a request.</p></li> </ul> <h3>Specs &amp; References:</h3> <ul> <li><a href="http://tools.ietf.org/html/rfc1035" rel="noreferrer">IETF: RFC1035</a></li> <li><a href="http://tools.ietf.org/html/rfc1123#section-2.1" rel="noreferrer">IETF: RFC1123</a></li> <li><a href="http://tools.ietf.org/html/rfc2181#page-13" rel="noreferrer">IETF: RFC2181</a></li> <li><a href="http://tools.ietf.org/html/rfc952" rel="noreferrer">IETF: RFC952</a></li> <li><a href="http://en.wikipedia.org/wiki/Domain_Name_System#cite_ref-rfc1034_1-2" rel="noreferrer">Wikipedia: Domain Name System</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload