VB6.0中如何引用《正则表达式》
首先在 工程->引用 中引用 Microsoft VBScript Regular Expression 5.5。
例程一在程序里写入以下代码:以下代码是验证IP!
Dim IPReg As New RegExp
IPReg.Pattern = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
Private Function CheckIP(IPValue As String) As Boolean
Dim posIP1 As Integer: Dim intIP1 As Integer
Dim posIP2 As Integer: Dim intIP2 As Integer
Dim posIP3 As Integer: Dim intIP3 As Integer
Dim intIP4 As Integer
If IPReg.Test(IPValue) = False Then
CheckIP = False
MsgBox "您输入的 IP 地址格式不对,请重新输入!"
Exit Function
End If
posIP1 = InStr(1, IPValue, ".")
intIP1 = CInt(Mid(IPValue, 1, posIP1 - 1))
If intIP1 < 1 Or intIP1 > 224 Then
CheckIP = False
MsgBox "第一段IP地址必须在 1-223 之间!"
Exit Function
End If
posIP2 = InStr(posIP1 + 1, IPValue, ".")
intIP2 = CInt(Mid(IPValue, posIP1 + 1, posIP2 - posIP1 - 1))
If intIP2 > 255 Then
CheckIP = False
MsgBox "第二段IP地址必须在 0-255 之间!"
Exit Function
End If
posIP3 = InStr(posIP2 + 1, IPValue, ".")
intIP3 = CInt(Mid(IPValue, posIP2 + 1, posIP3 - posIP2 - 1))
If intIP3 > 255 Then
CheckIP = False
MsgBox "第三段IP地址必须在 0-255 之间!"
Exit Function
End If
intIP4 = CInt(Mid(IPValue, posIP3 + 1, Len(IPValue) - posIP3))
If intIP4 > 255 Then
CheckIP = False
MsgBox "第四段IP地址必须在 0-255 之间!"
Exit Function
End If
CheckIP = True
End Function
然后在程序中调用 CheckIP 函数即可!
例程二,验证文本为带有两位小数点!
Dim NumberReg As New RegExp
NumberReg.Pattern = "^\d*\.?\d{0,2}$"
Private Function DotIsTwo(ByRef tempTxt As TextBox) As Boolean
If NumberReg.Test(tempTxt) = False Then
DotIsTwo = False
MsgBox "您输入的数据格式不对或小数点只能保留两位!"
tempTxt.SetFocus
Call SelectAll(tempTxt)
Else
DotIsTwo = True
End If
End Function
然后调用 DotIsTwo 函数即可 |