匹配子域和其他包含通配符的 URL



我试图根据预定义的网址库和我从另一个表获得的实际网址进行一些通用类型的网址匹配。

网址库包含我需要使用的通用网址,以检查传入的网址是否匹配。

因此,网址库可能如下所示:

1. https://*.sharepoint.com
2. https://trello.com/b/*
3. https://*.google.*

然后传入的 url 可能如下所示(如果它们应该匹配,请给出是/否(

https://bob.sharepoint.com 是

的https://ilovesharepoint.com 否

https://trello.com 否

https://trello.com/b/3782932 是

的https://www.google.com 是

的你明白了。除了针对每种情况进行编码之外,我正在努力找到一种通用方法来解析这些传入的 URL,以查看它们是否与其中任何一个匹配。

目前我为每一个编码,但这是一场噩梦。

这可能会给你一个很好的起点:

string[] patterns = new[] 
{ 
"https://*.sharepoint.com", 
"https://trello.com/b/*", 
"https://*.google.*" 
};
public bool IsMatch(string input)
{
foreach (var p in patterns)
{
if (Regex.Match(input, Regex.Escape(p).Replace("\*", "[a-zA-Z0-9]+")).Success)
return true;
}
return false;
}

请注意,掩码[a-zA-Z0-9]+非常简单,您可能需要使用更好的掩码,具体取决于您需要实现的目标。

最新更新