2011-05-11 43 views

回答

2

正如@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 
2

将其转换为字符串,并使用Regex.Match

+0

请问是何下投票是对? – atoMerz 2011-05-11 17:11:11

+0

不是我的downvote,但可能是因为星号(*)意味着通配符中的不同事物(匹配任何符号的任何次数)和正则表达式(match charical zero or more times) – Alex 2012-05-19 11:21:49

-3

使用此验证表达式

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> 
+2

所有这些都是检查你是否有看起来像一个IP地址,它不检查它是否在一个范围内。 – Kev 2011-05-11 17:14:54

+0

你有没有读过这个问题? – Alex 2011-05-11 17:20:39

相关问题