{"id":27061,"date":"2022-12-22T03:52:35","date_gmt":"2022-12-21T22:22:35","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/"},"modified":"2022-12-22T03:52:35","modified_gmt":"2022-12-21T22:22:35","slug":"solved-what-could-be-the-best-regex-for-domain-validation-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/","title":{"rendered":"[Solved] what could be the best regex for domain validation? [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-15644482\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"15644482\" data-parentid=\"15641699\" data-score=\"11\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p><strong>Disclaimer:<\/strong> Note that the rules which define a &#8220;valid domain&#8221; constitute a moving target. The answer below deals only with the &#8220;old school&#8221; DNS rules (using exclusively ASCII characters) and does not attempt to deal with international domains (as laid out in <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc3490\">RFC3490<\/a>). Note also that there will soon be lots of new top level domains (TLD) popping up so the solution below will need to be updated on a regular basis (see: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/data.iana.org\/TLD\/tlds-alpha-by-domain.txt\">IANA.ORG<\/a> for the current list of valid TLDs).<\/p>\n<h2>DNS Named Host Validation<\/h2>\n<p>According to the pertinent internet recommendations (<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc3986#section-3.2.2\">RFC3986 section 2.2<\/a>, which in turn refers to: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc1034#section-3.5\">RFC1034 section 3.5<\/a> and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc1123#section-2.1\">RFC1123 section 2.1<\/a>), a subdomain (which is a part of a DNS domain host name), must meet several requirements:<\/p>\n<h2>Subdomain<\/h2>\n<ul>\n<li>Each subdomain part must have a length no greater than 63.<\/li>\n<li>Each subdomain part must begin and end with an alpha-numeric (i.e. letters <code>[A-Za-z]<\/code> or digits <code>[0-9]<\/code>).<\/li>\n<li>Each subdomain part may contain hyphens (dashes), but may not begin or end with a hyphen.<\/li>\n<\/ul>\n<p>Here is an expression fragment for a subdomain part which meets these requirements:<\/p>\n<p><code>(?:[A-Za-z0-9][A-Za-z0-9\\-]{0,61}[A-Za-z0-9]|[A-Za-z0-9])<\/code><\/p>\n<p>Note that this expression requires a group with two alternatives to handle the special case of a subdomain having only one character. Also, this expression fragment should not be used alone &#8211; it requires the incorporation of boundary conditions in a larger context, as demonstrated in the following expression for a DNS host name&#8230;<\/p>\n<h2>DNS host name<\/h2>\n<p>A named host, (not an IP address), must meet additional requirements:<\/p>\n<ul>\n<li>The host name may consist of multiple subdomain parts, each separated by a single dot.<\/li>\n<li>The length of the  overall host name should not exceed 255 characters.<\/li>\n<li>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: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/data.iana.org\/TLD\/tlds-alpha-by-domain.txt\">http:\/\/data.iana.org\/TLD\/tlds-alpha-by-domain.txt<\/a>).<\/li>\n<\/ul>\n<p>With this is mind, here a commented regex (in C# 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).<\/p>\n<pre class=\"lang-cs prettyprint-override\"><code>if (Regex.IsMatch(text, @\" # Rev:2013-03-26\n    # Match DNS host domain having one or more subdomains.\n    # Top level domain subset taken from IANA.ORG. See:\n    # http:\/\/data.iana.org\/TLD\/tlds-alpha-by-domain.txt\n    ^                  # Anchor to start of string.\n    (?!.{256})         # Whole domain must be 255 or less.\n    (?:                # Group for one or more sub-domains.\n      [a-z0-9]         # Either subdomain length from 2-63.\n      [a-z0-9-]{0,61}  # Middle part may have dashes.\n      [a-z0-9]         # Starts and ends with alphanum.\n      \\.               # Dot separates subdomains.\n    | [a-z0-9]         # or subdomain length == 1 char.\n      \\.               # Dot separates subdomains.\n    )+                 # One or more sub-domains.\n    (?:                # Top level domain alternatives.\n      [a-z]{2}         # Either any 2 char country code,\n    | AERO|ARPA|ASIA|BIZ|CAT|COM|COOP|EDU|  # or TLD \n      GOV|INFO|INT|JOBS|MIL|MOBI|MUSEUM|    # from list.\n      NAME|NET|ORG|POST|PRO|TEL|TRAVEL|XXX  # IANA.ORG\n    )                  # End group of TLD alternatives.\n    $                  # Anchor to end of string.\",\n    RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace))\n{\n    \/\/ Valid named DNS host (domain).\n} else {\n    \/\/ NOT a valid named DNS host.\n} \n<\/code><\/pre>\n<p>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). It also does not explicitly spell out each two character country code TLD &#8211; it simply allows any two letters. It also does not list the various TLDs of the: <code>XN--XXXXX<\/code> variety. This solution also does not consider the <em>not-yet-fully-implemented-and-universally-acceptable<\/em> international domain names.<\/p>\n<p>For more on validating other URI components, you may want to take a look at an article I wrote a while back: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/jmrware.com\/articles\/2009\/uri_regexp\/URI_regex.html\">Regular Expression URI Validation<\/a>. It provides code snippets in a variety of languages for all of the various URI components as defined by <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc3986\">RFC3986<\/a>.<\/p>\n<p>Happy regexing!<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved what could be the best regex for domain validation? [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Disclaimer: Note that the rules which define a &#8220;valid domain&#8221; constitute a moving target. The answer below deals only with the &#8220;old school&#8221; DNS rules (using exclusively ASCII characters) and does not attempt to deal with international domains (as laid out in RFC3490). Note also that there will soon be lots of new top &#8230; <a title=\"[Solved] what could be the best regex for domain validation? [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/\" aria-label=\"More on [Solved] what could be the best regex for domain validation? [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,347,475],"class_list":["post-27061","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-regex","tag-validation"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] what could be the best regex for domain validation? [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] what could be the best regex for domain validation? [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Disclaimer: Note that the rules which define a &#8220;valid domain&#8221; constitute a moving target. The answer below deals only with the &#8220;old school&#8221; DNS rules (using exclusively ASCII characters) and does not attempt to deal with international domains (as laid out in RFC3490). Note also that there will soon be lots of new top ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-21T22:22:35+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] what could be the best regex for domain validation? [closed]\",\"datePublished\":\"2022-12-21T22:22:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/\"},\"wordCount\":496,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"regex\",\"validation\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/\",\"name\":\"[Solved] what could be the best regex for domain validation? [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-21T22:22:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] what could be the best regex for domain validation? [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] what could be the best regex for domain validation? [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] what could be the best regex for domain validation? [closed] - JassWeb","og_description":"[ad_1] Disclaimer: Note that the rules which define a &#8220;valid domain&#8221; constitute a moving target. The answer below deals only with the &#8220;old school&#8221; DNS rules (using exclusively ASCII characters) and does not attempt to deal with international domains (as laid out in RFC3490). Note also that there will soon be lots of new top ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/","og_site_name":"JassWeb","article_published_time":"2022-12-21T22:22:35+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] what could be the best regex for domain validation? [closed]","datePublished":"2022-12-21T22:22:35+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/"},"wordCount":496,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","regex","validation"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/","name":"[Solved] what could be the best regex for domain validation? [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-21T22:22:35+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-what-could-be-the-best-regex-for-domain-validation-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] what could be the best regex for domain validation? [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/27061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=27061"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/27061\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=27061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=27061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=27061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}