C# 正则表达式:使用通配符根据用户输入检查IP地址

C# 正则表达式:使用通配符根据用户输入检查IP地址,c#,.net,regex,C#,.net,Regex,我有一个名为IpAddressList的列表,其中包含一些IP地址,如192.168.0.5等等 用户还可以使用通配符在列表中搜索给定的IP地址* 这是我的方法: 用户输入可以是,例如: 192.168.0.* 192 192.168.0.5 192.*.0.* 在所有情况下,该方法都应返回true,但我不知道如何将Regex与userinput结合使用,也不知道Regex的外观如何。我认为这应该也适用于192.*.0.*: 我认为这也适用于192.*.0.*: 下面是一个更健壮的版本,如果用户

我有一个名为IpAddressList的列表,其中包含一些IP地址,如192.168.0.5等等

用户还可以使用通配符在列表中搜索给定的IP地址*

这是我的方法:

用户输入可以是,例如:

192.168.0.* 192 192.168.0.5 192.*.0.*
在所有情况下,该方法都应返回true,但我不知道如何将Regex与userinput结合使用,也不知道Regex的外观如何。

我认为这应该也适用于192.*.0.*:


我认为这也适用于192.*.0.*:


下面是一个更健壮的版本,如果用户输入包含正则表达式元字符,如\,或不匹配的括号,则该版本不会中断:

public static bool IpAddressMatchUserInput(string userInput, string ipAddressFromList)
{
    // escape the user input. If user input contains e.g. an unescaped 
    // single backslash we might get an ArgumentException when not escaping
    var escapedInput = Regex.Escape(userInput);

    // replace the wildcard '*' with a regex pattern matching 1 to 3 digits
    var inputWithWildcardsReplaced = escapedInput.Replace("\\*", @"\d{1,3}");

    // require the user input to match at the beginning of the provided IP address
    var pattern = new Regex("^" + inputWithWildcardsReplaced);

    return pattern.IsMatch(ipAddressFromList);
}

下面是一个更健壮的版本,如果用户输入包含正则表达式元字符,如\,或不匹配的括号,则该版本不会中断:

public static bool IpAddressMatchUserInput(string userInput, string ipAddressFromList)
{
    // escape the user input. If user input contains e.g. an unescaped 
    // single backslash we might get an ArgumentException when not escaping
    var escapedInput = Regex.Escape(userInput);

    // replace the wildcard '*' with a regex pattern matching 1 to 3 digits
    var inputWithWildcardsReplaced = escapedInput.Replace("\\*", @"\d{1,3}");

    // require the user input to match at the beginning of the provided IP address
    var pattern = new Regex("^" + inputWithWildcardsReplaced);

    return pattern.IsMatch(ipAddressFromList);
}

这是有效输入192.*.0.*.user3185569是:检查下面的答案。IP地址列表字符串中的分隔符是什么?在foreach循环中为每个IP地址参数调用该方法:IPAddressFromList这是有效输入192.*.0.@user3185569是:检查下面的答案。IP地址列表字符串中的分隔符是什么方法在foreach循环中为每个IP地址参数调用:IPAddressFromList他可能也应该替换。用\。要逃避它并按字面意思对待它。@user3185569已测试,工作正常,做得很好,谢谢:这是一个错误的答案,因为[0-255]匹配0、1、2或5。小心,[0-255]不匹配从0到255的数字范围!它匹配单个字符,即“0”、“1”、“2”或“5”。匹配“1”是因为它包含在字符范围“0-2”中,该字符范围是他可能也应该替换的字符类的一部分。用\。要逃避它并按字面意思对待它。@user3185569已测试,工作正常,做得很好,谢谢:这是一个错误的答案,因为[0-255]匹配0、1、2或5。小心,[0-255]不匹配从0到255的数字范围!它匹配单个字符,即“0”、“1”、“2”或“5”。“1”已匹配,因为它包含在字符范围“0-2”中,该字符范围是字符类的一部分
public static bool IpAddressMatchUserInput(string userInput, string ipAddressFromList)
{
    // escape the user input. If user input contains e.g. an unescaped 
    // single backslash we might get an ArgumentException when not escaping
    var escapedInput = Regex.Escape(userInput);

    // replace the wildcard '*' with a regex pattern matching 1 to 3 digits
    var inputWithWildcardsReplaced = escapedInput.Replace("\\*", @"\d{1,3}");

    // require the user input to match at the beginning of the provided IP address
    var pattern = new Regex("^" + inputWithWildcardsReplaced);

    return pattern.IsMatch(ipAddressFromList);
}