Executing in a command prompt window findstr /?
outputs the help for this command.
The search regular expression means:
^
... find a string at begin of a line
[0-9]
... which has at least 1 digit
[0-9]*
... and can have 0 or more digits
$
... and ends at end of the line.
In other words the line output by ECHO must have 1 or more digits and no other character for a positive match with exit code 0
assigned to environment variable ERRORLEVEL
. Any other string on output line results in no match and exit code 1
.
The first [0-9]
is needed to make sure the user input consists of at least 1 digit. Otherwise an empty line would result also in a positive match. FINDSTR does not support multiplier +
which in other applications with regular expression support means 1 or more of previous character or character class or expression.
Well, a not defined userinput
would result in output ECHO is OFF
or ECHO is ON
to process by FINDSTR if userinput
was not defined before set /P "userinput=prompt text: "
and the user just hits RETURN or ENTER. For that reason ^[0-9]*$
would work here also by chance because it does not occur here that FINDSTR has to process an empty line.
But FINDSTR has some unexpected matching behavior. For example [0-9]
matches also ¹
and ²
and ³
depending on code page like Windows-1252, see What are the undocumented features and limitations of the Windows FINDSTR command? Therefore it would be better to use as validation regular expression:
^[0123456789][0123456789]*$
That expression looks not as compact as ^[0-9][0-9]*$
but is better for user input validation.
Please note that even when the user input passes this test, the entered number string could be invalid for further processing in an arithmetic expression, for example if the user enters 45829413953053
. This number is too large for a 32-bit signed integer supporting only numbers in the range -2147483648
to 2147483647
.