The following should work as long as the variable does not contain any "poison" characters like &
, |
, etc.
echo(%var%|findstr /rx "D[0123456789]*" >nul && (
echo Valid
) || (
echo Invalid
)
If you want to protect against poison characters, then
setlocal enableDelayedExpansion
echo(!var!|findstr /rx "D[0123456789]*" >nul && (
echo Valid
) || (
echo Invalid
)
If the last command that you execute in the "success" ('&&') section might raise an error, then the ERRORLEVEL must be cleared, otherwise the "failure" ('||') section might fire when you don't want. The simplest and fastest way is to use (call )
setlocal enableDelayedExpansion
echo(!var!|findstr /rx "D[0123456789]*" >nul && (
echo Valid
someCommandThatCouldRaiseAnError
(call )
) || (
echo Invalid
)