The very good working solution posted by LotPings is:
Echo:%newvalue%|findstr "^[0-9][0-9]\.[0-9]$" >Nul 2>&1 &&(echo matched pattern)||(echo didn't match pattern)
I suggest a little bit different single line solution:
echo:%newvalue%|%SystemRoot%\System32\findstr.exe /R "^[0123456789][0123456789]\.[0123456789]$" >nul && (echo matched pattern) || (echo didn't match pattern)
Or easier readable and working for really any string value assigned to environment variable newvalue
:
setlocal EnableExtensions EnableDelayedExpansion
echo:!newvalue!| %SystemRoot%\System32\findstr.exe /R "^[0123456789][0123456789]\.[0123456789]$" >nul
if errorlevel 1 (
echo Value does not match the regular expression pattern.
) else (
echo Value matches the regular expression pattern.
)
endlocal
A colon is used between command echo
and the string assigned to environment variable newvalue
instead of a space to avoid that command ECHO outputs the current status of command echoing in case of newvalue
is not defined at all.
The last solution uses delayed expansion to avoid that the command line with ECHO and FINDSTR does not work or does something completely different than it is written for if the string assigned to variable newvalue
contains operators like &<>|
.
It is important that there is no space left to redirection operator |
which pipes output of command ECHO to the command FINDSTR as input because of this space character would be also output by command ECHO and the regular expression find would never be positive. A space right to |
does not matter as last example demonstrates.
FINDSTR does not support \d
as representation for any digit. It is necessary to specify the characters to match in a self-defined character class in square brackets. FINDSTR matches with [0-9]
the characters 0123456789¹²³
. It is necessary to use [0123456789]
to match just the characters 0123456789
without ¹²³
.
.
means any character, except the dot is escaped with a backslash in which case it is interpreted as literal character.
A search string in "..."
is interpreted by FINDSTR by default as regular expression string. But I think, it is always good to make use of /L
or /R
to make it 100% clear for FINDSTR and for every reader how the search string should be interpreted, as literal or as regular expression string.
FINDSTR exits with value 1
on no positive match on searched input character stream and 0
on at least one positive match. The exit code of FINDSTR can be evaluated as shown above and described in detail in single line with multiple commands using Windows batch file.
The output of FINDSTR on a positive match is of no interest and therefore redirected to device NUL to suppress it.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
findstr /?
if /?
setlocal /?
See also the Microsoft article about Using command redirection operators and DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ for the reason why using :
after echo
is often better than a space on output of a string read from a file or entered by a user.