[英]VB.NET conditionals with a regex match
我構造了一個代碼,用於檢查文本框中的內容是字符串還是整數。 我的工作如下:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
Dim value As String = txtBox.Text
Dim i As Integer
If (Integer.TryParse(value, i)) = True Then
If IsValid(value) = True Then
MsgBox(value & " is a correct entry")
Else
MsgBox("Not a correct TVA number !")
End If
Else
MsgBox(value & " is a string")
End If
End Sub
Function IsValid(ByRef value As String) As Boolean
Return Regex.IsMatch(value, "^0[1-9]{9}$")
End Function
End Class
現在一切正常,直到我在文本框中輸入11個或更多數字 ,然后代碼突然告訴我,例如012345678912是字符串(!)。
所以要清楚,當我鍵入以下數字時:
也許我在這里遺漏了一些明顯的東西,但是我一遍又一遍地看了一下代碼,並在其他應用程序中對正則表達式進行了幾次測試,在這些應用程序中它們正常工作。
請問我在這里俯瞰什么?
關於Wim
對於Integer
,該值太大,因此它將溢出並且Int32.TryParse
返回false
。
您可以使用此:
If value.All(AddressOf Char.IsDigit) Then
If IsValid(value) = True Then
MsgBox(value & " is a correct entry")
Else
MsgBox("Not a correct TVA number !")
End If
Else
MsgBox(value & " is a string")
End If
或改用Int64.TryParse
允許更大的數字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.