Windows Cmd findstr – Regex with digits not working

  batch-file, findstr, windows

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.

C:iSTEPVMTicketingRealVNC>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:iSTEPVMTicketingRealVNC>echo %errorlevel%
1

C:iSTEPVMTicketingRealVNC>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:iSTEPVMTicketingRealVNC>echo %errorlevel%
0

C:iSTEPVMTicketingRealVNC>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:iSTEPVMTicketingRealVNC>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:iSTEPVMTicketingRealVNC>echo 1.1.1.1 | findstr /R "d{1,3}.d{1,3}.d{1,3}.d{1,3}"

C:iSTEPVMTicketingRealVNC>echo %errorlevel%
1

C:iSTEPVMTicketingRealVNC>echo 1.1.1.1 | findstr /b /e /R "d{1,3}.d{1,3}.d{1,3}.d{1,3}"

C:iSTEPVMTicketingRealVNC>echo %errorlevel%
1

C:iSTEPVMTicketingRealVNC>echo 1.1.1.1 | findstr /b /e /R "bd{1,3}.d{1,3}.d{1,3}.d{1,3}b"

C:iSTEPVMTicketingRealVNC>echo %errorlevel%
1

C:iSTEPVMTicketingRealVNC>echo 1.1.1.1 | findstr /b /e /R "d{1,3}.d{1,3}.d{1,3}.d{1,3}"

C:iSTEPVMTicketingRealVNC>echo %errorlevel%
1

C:iSTEPVMTicketingRealVNC>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:iSTEPVMTicketingRealVNC>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:iSTEPVMTicketingRealVNC>echo 1.1.1.1 | findstr /b /e /R "d{1,3}.d{1,3}.d{1,3}.d{1,3}*"

C:iSTEPVMTicketingRealVNC>echo %errorlevel%
1

C:iSTEPVMTicketingRealVNC>echo 1.1.1.1 | findstr /b /e /R "d{1,3}.d{1,3}.d{1,3}.d{1,3}>"

C:iSTEPVMTicketingRealVNC>echo %errorlevel%
1

enter image description here

Source: Windows Questions

LEAVE A COMMENT