3

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

madth3
  • 7,275
  • 12
  • 50
  • 74
yael
  • 2,765
  • 10
  • 40
  • 48

1 Answers1

4

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
  • 6,946
  • 3
  • 42
  • 54
  • THX but I check the RE seems that its not verify the range of the IP: 1-254 , can we add this rule? – yael Jun 08 '10 at 20:55
  • 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 Jun 08 '10 at 21:35
  • another thing its not fail on: 172.17.202.1bug-20 (for example) whay? – yael Jun 08 '10 at 21:43
  • excellent RE you wrote , I tested the regular E and its work on all scenarios , THX for your help yael – yael Jun 09 '10 at 04:00