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