如果我有一个IP地址:192.100.100.2并且需要确保它落在使用通配符模式指定的范围内。如何查找IP地址是否落入使用模式指定的范围内? - ASP.NET
的模式可以是:
1. 192. *. *. * 2. *. *. *. * 3. 192.1**. *.2
所以基本上,星号或三个星号指定的有效范围。有没有内置的ASP.NET我可以用来验证IP地址,或者这是更多的自定义验证?
如果我有一个IP地址:192.100.100.2并且需要确保它落在使用通配符模式指定的范围内。如何查找IP地址是否落入使用模式指定的范围内? - ASP.NET
的模式可以是:
1. 192. *. *. * 2. *. *. *. * 3. 192.1**. *.2
所以基本上,星号或三个星号指定的有效范围。有没有内置的ASP.NET我可以用来验证IP地址,或者这是更多的自定义验证?
正如@AtoMerZ说,只是转换你的模式正则表达式:
''//Patterns to search for
Dim Patterns() As String = {"192.*.*.*", "*.*.*.*", "192.1**.*.2"}
''//Test IP
Dim TestIP = "192.100.100.2"
''//Loop through each pattern
For Each P In Patterns
''//Swap two asterisk for two regex digits (\d\d) and one asterisk for one or more digits. Also escape the period
Trace.WriteLine(System.Text.RegularExpressions.Regex.IsMatch(TestIP, P.Replace("**", "\d\d").Replace("*", "\d+").Replace(".", "\.")))
Next
将其转换为字符串,并使用Regex.Match
使用此验证表达式
ValidationExpression =“\ B(25 [0-5] | 2 [0-4] [0-9] | [01] [0-9] [0-9])(25 [0-5] |?2 [0-4] [0-9] | [01] [0-9] [0-9? ])(25 [0-5] | 2 [0-4] [0-9] | [01] [0-9] [0-9])(25 [0-5] |?2 [0-4] [0-9] |?[01] [0-9] [0-9])\ b”的
示例代码
<asp:TextBox ID="TextBox1" runat="server" Width="321px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Validate" />
<br />
<asp:RegularExpressionValidator ValidationExpression="\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
ID="RegularExpressionValidator1" runat="server" ErrorMessage="Invalid IP !" ControlToValidate="TextBox1"></asp:RegularExpressionValidator></div>
请问是何下投票是对? – atoMerz 2011-05-11 17:11:11
不是我的downvote,但可能是因为星号(*)意味着通配符中的不同事物(匹配任何符号的任何次数)和正则表达式(match charical zero or more times) – Alex 2012-05-19 11:21:49