Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 匹配子域和其他包含通配符的URL_C#_Regex - Fatal编程技术网

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

C# 匹配子域和其他包含通配符的URL,c#,regex,C#,Regex,我试图根据预定义的url库和从另一个表中获得的实际url进行一些通用类型url匹配 url库包含我需要使用的通用url,以便检查传入url是否匹配 因此,URL库可能如下所示: 1. https://*.sharepoint.com 2. https://trello.com/b/* 3. https://*.google.* 然后传入的url可能如下所示(如果匹配,我会给出是/否) 对 没有 没有 对 对 你明白了。除了为每种情况编码之外,我还在努力寻找一种通用的方法来解析这些传入的URL,

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

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

因此,URL库可能如下所示:

1. https://*.sharepoint.com
2. https://trello.com/b/*
3. https://*.google.*
然后传入的url可能如下所示(如果匹配,我会给出是/否)

没有

没有

你明白了。除了为每种情况编码之外,我还在努力寻找一种通用的方法来解析这些传入的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]+
非常简单,您可能希望使用更好的掩码,具体取决于您需要实现的目标。

是“https://.google.“图案是否正确或缺少一些星号?似乎编辑器正在使用*字符作为斜体!”!瓦塞克,+1给你。或者,用
+
代替
[a-zA-Z0-9]+
怎么样?另外,如果用
(.+)
代替
[a-zA-Z0-9]+
替换
\*
,它还可以为您提供用户代码可能需要的匹配组名。图例。我只是不喜欢正则表达式。这是一个完美的选择。我会玩
[a-zA-Z0-9]+