yael
yael

Reputation: 2913

VBScript Regular Expressions to check IP address validity with some adtional characters

How to create VB script Irregular expression syntax to check the VPparam (IP address validity) When the last octatat of the IP address is a range between ip's (x-y) and between each IP we can put the "," separator in order to add another IP

example of VBparam

VBparam=172.17.202.1-20

VBparam=172.17.202.1-10,192.9.200.1-100

VBparam=172.17.202.1-10,192.9.200.1-100,180.1.1.1-20

THX yael

Upvotes: 3

Views: 6959

Answers (1)

volody
volody

Reputation: 7199

cscript test.vbs

Updated: to verify the range of the IP: 1-255

Updated: fixed matching end of line

Dim strByteMatch, strIpMatch, strPattern 
strByteMatch = "(25[0-5]|2[0-4]\d|[01]?\d\d?)"
strIpMatch = strByteMatch & "\." & strByteMatch & "\." & strByteMatch & _
"\.(" & strByteMatch & "|(" & strByteMatch & "-" & strByteMatch & "))"
strPattern = "^" & strIpMatch & "(," & strIpMatch & ")*$"


Test "172.17.202.1-20", strPattern
Test "172.17.202.1-10,192.9.200.1-100", strPattern
Test "172.17.202.1-10,192.9.200.1-100,180.1.1.1-20", strPattern
Test "172.17.202.1bug-20", strPattern            ' This should fail
Test "172.17.202.333,172.17.202.1", strPattern   ' This should fail

Sub Test(strString, strPattern)
    if RegExIsMatch(strString, strPattern) Then
        WScript.Echo "Test Pass"
    else
        WScript.Echo "Test Fail"
    end if
End Sub

Function RegExIsMatch(strString,strPattern)
    Dim RegEx
    RegExMatch=False

    Set RegEx = New RegExp                
    RegEx.IgnoreCase = True                
    RegEx.Global=True                   
    RegEx.Pattern=strPattern

    If RegEx.Test(strString) Then RegExIsMatch=True
End Function 

Upvotes: 4

Related Questions