Vb.net 将字符串与特定模式匹配
我想让我的程序检查特定模式的用户输入。我在旁边使用了一个标签,表明这个特定的TextBox1必须是这种格式的“XX/XX”(其中as XX必须是数字,并且可以有无限个字符) 但是,没有编码端检查来检查用户输入。目前我在internet上搜索的解决方案正在使用Vb.net 将字符串与特定模式匹配,vb.net,Vb.net,我想让我的程序检查特定模式的用户输入。我在旁边使用了一个标签,表明这个特定的TextBox1必须是这种格式的“XX/XX”(其中as XX必须是数字,并且可以有无限个字符) 但是,没有编码端检查来检查用户输入。目前我在internet上搜索的解决方案正在使用 Dim sMatch As Boolean sMatch = TextBox1.Text Like "[0-9]/[0-9]" 唯一的问题是它只接受单个数字。例如5/4,我想要的是动态输入,例如123/1或9/12或999999/3000
Dim sMatch As Boolean
sMatch = TextBox1.Text Like "[0-9]/[0-9]"
唯一的问题是它只接受单个数字。例如5/4,我想要的是动态输入,例如123/1或9/12或999999/30000。提前谢谢。你就快到了
sMatch = TextBox1.Text Like "\d{1,}/\d{1,}"
或
好的参考资料来自:
\d匹配任何十进制数字。{n,}与前面的元素至少匹配n次我认为在这个场景中使用更好 或者你也可以试试这个
Sub Main()
Dim regex As Regex = New Regex("[0-9][0-9]/[0-9][0-9]")
Dim match As Match = regex.Match("x1/1x2")
If match.Success Then
Console.WriteLine(match.Value)
Else
Console.WriteLine("Does not match")
End If
Console.ReadKey()
End Sub
Sub Main()
Dim regex As Regex = New Regex("[0-9][0-9]/[0-9][0-9]")
Dim match As Match = regex.Match("x1/1x2")
If match.Success Then
Console.WriteLine(match.Value)
Else
Console.WriteLine("Does not match")
End If
Console.ReadKey()
End Sub