tags:

views:

177

answers:

1

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

A: 

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 
volody
THX but I check the RE seems that its not verify the range of the IP:1-254 , can we add this rule?
yael
OK its check fine the first IP but if we have: 172.17.202.1-10,192.9.200.1-100,180.1.1.1-20 IP How we can improve the syntax to verify the second or third IP?? because its not check the second or th
yael
another thing its not fail on: 172.17.202.1bug-20 (for example) whay?
yael
excellent RE you wrote , I tested the regular E and its work on allscenarios , THX for your helpyael
yael