I'm trying to validate if the IP address argument passed to a batch file is valid or not. Unfortunately, Windows findstr is not reliably working though the regex fed into is matched.
Following is the summary of all the results.
findstr sets errorlevel to 0 if there is a match, 1 if there is no match.
C:\iSTEP\VMTicketing\RealVNC>echo 192.10.10.10 | findstr /R "[0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]"
C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1
C:\iSTEP\VMTicketing\RealVNC>echo 192.101.101.101 | findstr /R "[0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]"
192.101.101.101
C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
0
C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /R "[0-2][0-9][0-9]?\.[0-2]?[0-9][0-9]?\.[0-2]?[0-9][0-9]?\.[0-2]?[0-9][0-9]?"
C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /R "[0-2]?[0-9][0-9]?\.[0-2]?[0-9][0-9]?\.[0-2]?[0-9][0-9]?\.[0-2]?[0-9][0-9]?"
C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /R "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1
C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1
C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"
C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1
C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1
C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}*"
FINDSTR: No search strings
C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
FINDSTR: No search strings
C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}*"
C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1
C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\>"
C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1
CodePudding user response:
A simple way of performing matches using powershell regex:
@Echo off
Call :Regex "131.21.1.101" "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
Echo(%Errorlevel%
Call :Regex "1321.101" "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
Echo(%Errorlevel%
Goto :Eof
:Regex
powershell.exe -noprofile -c "'%~1' -match '%~2'" | findstr /l "True" > nul || Exit /b 1
Exit /b 0
For details on powershell regex syntax see here
CodePudding user response:
I just tried the following:
echo 1.1.1.1 | findstr "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"
It looks like it's working fine, but there are other issues, like a weird value like isolationlayer_31bf3856ad364e35_10.0.14393.479_none_3b8c
also being shown.