我需要为VB.NET编写一个用于模式匹配的正则表达式。我需要让Regex寻找像12345-1234-12345-123这样的模式,包括短划线。数字可以是任何变化。该值存储为varchar。不确定我的例子在下面有多近或远。非常感谢任何帮助/指导。
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim testString As String = "12345-1234-12345-123"
Dim testNumberWithDashesRegEx As Regex = New Regex("^\d{5}-d{4}-d{5}-\d{3}$")
Dim regExMatch As Match = testNumberWithDashesRegEx.Match(testString)
If regExMatch.Success Then
Label1.Text = "There is a match."
Else
Label1.Text = "There is no match."
End If
End Sub
答案 0 :(得分:1)
让我们打破这个正则表达式:
^\d{5}-d{4}-d{5}-\d{3}$
^
:在目标字符串的开头匹配\d
:匹配数字0-9 -
:匹配短划线( - )字符d
:匹配字母“d”{5}
:匹配前一课5次$
:匹配目标字符串的末尾。除了你应该将你的简单“d”改为“\ d”之外,一切看起来都不错:
^\d{5}-\d{4}-\d{5}-\d{3}$