How can I check either string is full domain or subdomain using regex.
like
abc.com | Full domain
xyz.abc.com | Subdomain
test.co.uk | Full domain
text.test.co.uk/ | Subdomain
How can I check either string is full domain or subdomain using regex.
like
abc.com | Full domain
xyz.abc.com | Subdomain
test.co.uk | Full domain
text.test.co.uk/ | Subdomain
Recursively do a DNS lookup, while removing one section from the left at a time
subdomain
xyz.abc.com //DNS check PASS
abc.com //DNS check PASS
domain
xxx.com.hk //DNS check PASS
com.hk //DNS check FAIL
If there no nameservers/DNS resolution fails for a different reason, the other way to confirm if it is a subdomain or not is to use IANA's TLD (Top-Level Domain) Registry Here.
(USE CAUTIOUSLY)
There are several cases like com.com, com.google.com, and several other URLs using TLDs in their name.
us.com, de.com, etc (see centralnic.com/names/domains), and IANA does not recognize them. Very well know such tlds are co.uk, co.jp, or com.br.With Regex if not possible to check is a domain or subdomain. The most browser use for this problem the project of Mozilla - PublicSuffix. It is a maintained list of TLD.
You can use the following nuget package. (Install-Package Nager.PublicSuffix) https://www.nuget.org/packages/Nager.PublicSuffix/
Example:
var domainParser = new DomainParser();
var data = await domainParser.LoadDataAsync();
var tldRules = domainParser.ParseRules(data);
domainParser.AddRules(tldRules);
var domainName = domainParser.Get("sub.test.co.uk");
//domainName.Domain = "test";
//domainName.Hostname = "sub.test.co.uk";
//domainName.RegistrableDomain = "test.co.uk";
//domainName.SubDomain = "sub";
//domainName.TLD = "co.uk";