简体   繁体   English

用于在 C# 中获取域和子域的正则表达式

[英]Regex for getting domain and subdomain in C#

I am having a requirement to correctly get the domain/subdomain based on the current url.我需要根据当前的 url 正确获取域/子域。 this is required in order to correctly fetch the data from database and further call web api with correct parameters.这是为了正确地从数据库中获取数据并使用正确的参数进一步调用 web api 所必需的。

In perticular, I am facing issues with local and production urls.特别是,我面临本地和生产网址的问题。 for ex.例如。

In local, i have在当地,我有

http://sample.local.example.com 
http://test.dev.example.com

In production, i have在生产中,我有

http://client.example.com 
http://program.live.example.com

i need我需要

Subdomain as: sample / test / client / program
Domain as: exmpale 

So far i tried to use c# with following code to identify the same.到目前为止,我尝试使用 c# 和以下代码来识别相同的。 It works fine on my local but i am sure this will create an issue on production at some point of time.它在我本地运行良好,但我相信这会在某个时间点对生产造成问题。 Basically, for Subdomain, get the first part and for Domain, get the last part before ''.com''基本上,对于子域,获取第一部分,对于域,获取 ''.com'' 之前的最后一部分

 var host = Request.Url.Host;
 var domains = host.Split('.');
 var subDomain = domains[0];
 string mainDomain = string.Empty;
  #if DEBUG
    mainDomain = domains[2]; 
  #else
    mainDomain = domains[1]; 
  #endif

 return Tuple.Create(mainDomain, subDomain);

Instead of a regex, I think Linq should help your here. 而不是正则表达式,我认为Linq应该帮助你。 Try: 尝试:

public static Tuple<string, string> GetDomains(Uri url)
{
    var domains = url.Host.Substring(0, url.Host.LastIndexOf(".")).Split('.');
    var subDomain = string.Join("/", domains.Take(domains.Length - 1));
    var mainDomain = domains.Last();
    return Tuple.Create(mainDomain, subDomain);
}

output for "http://program.live.example.com" 输出"http://program.live.example.com"

example
program/live

Try it Online! 在线试试吧!

This regex should work for you: 这个正则表达式应该适合你:

Match match = Regex.Match(temp, @"http://(\w+)\.?.*\.(\w+).com$");

string subdomain = match.Groups[1].Value;
string domain = match.Groups[2].Value;

http://(\\w+)\\. matches 1 or more word characters as group 1 before a dot and after http:// 匹配1个或多个单词字符作为点之前的组1和http://

.* matches zero or more occurences of any character .*匹配任何字符的零个或多个出现

\\.(\\w+).com matches 1 or more word characters as group 2 before .com and after a dot \\.(\\w+).com.com之前和之后匹配1个或多个单词字符作为组2

$ specifies the end of the string $指定字符串的结尾

\\.? makes the dot optional to catch the case if there is nothing between group 1 and 2 like in http://client.example.com 如果在第1组和第2组之间没有任何内容,就像在http://client.example.com那样,使点可选以捕获大小写

You are doing the right and you can get the domain name as the second last value in the array. 您正在做正确的事情,您可以将域名作为数组中的倒数第二个值。

var host = Request.Url.Host;
var domains = host.Split('.');
string subDomain = domains[0].Split('/')[2];
string mainDomain = domains[domains.Length-2];  
return Tuple.Create(mainDomain, subDomain);

If you want all the subdomains you can put a loop here. 如果你想要所有子域,你可以在这里放一个循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM