Answers:
根据debham的评论进行编辑以修复正则表达式。事实证明,在回声之后在管道之前添加一个空格会在管道字符串中添加一个空格,这破坏了之前的行匹配的开始/结束。通过在开头和结尾处丢弃空格可以进一步改善正则表达式。
有findstr命令。它可以搜索带有正则表达式的文件,就像grep在Linux中一样。它还可以搜索管道输入。
@echo off
set param=%1
echo %param%| findstr /r "^[1-9][0-9]*$">nul
if %errorlevel% equ 0 (
echo Valid number
)
使用的参数设置在param变量中。但是,没有什么可以阻止您直接使用参数(%1对于第一个参数)。
findstr用于搜索带有/r标志的管道输入的正则表达式。
模式:
^ 表示行首。
[0-9]表示一个数字。在*前面的手段重复零次或更多次。因此[0-9][0-9]*表示一位数字,再加上零个或多个数字。换句话说,至少一个数字。在+一次或多次,似乎并没有被支持findstr。请注意,[1-9]第一个数字用于禁止前导零-请参见注释。
$ 表示行尾。
现在,一个for循环分批执行了x次...如果x不是有效数字,则该循环实际上根本不会运行-只会跳过下一行。因此,无需检查输入的数字是否有效!
@echo off
set param=%1
for /l %%a in (1,1,%param%) do (
echo %%a
)
使用来完成循环for /l,其中的(x,y,z)意思是从处开始x,递增y直到z达到。并设置%%a为当前编号/迭代次数。
注意:如果有前导零,则此操作实际上会失败,从而导致命令处理器将其视为八进制数。请参阅dbenham的答案以获得更好的解决方案。
"this 1 fails"FINDSTR之类的参数进行精确匹配。它不应该单词匹配。
这将检测第一个参数是否为有效的自然数(非负整数)。
@echo off
echo %1|findstr /xr "[1-9][0-9]* 0" >nul && (
echo %1 is a valid number
) || (
echo %1 is NOT a valid number
)
如果您想在数字前后加上引号,则
@echo off
echo "%~1"|findstr /xr /c:\"[1-9][0-9]*\" /c:\"0\" >nul && (
echo %~1 is a valid number
) || (
echo %~1 is NOT a valid number
)
注意-不允许使用前导零,因为批处理会将其视为八进制,因此值like 09无效,并且010值为8。
受到鲍勃出色回答的启发,并增加了以下内容
:::::::::::::::
::CountTo.bat
:::::::::::::::
@echo off
::This is the number to test
if "%1"=="" goto :Usage
set param=%1
set matchPattern="^[1-9][0-9]*$"
::test if param matches matchPattern, quietly
:: (redirect stdout to nul, and stderr to stdout)
echo %param%|findstr /r %matchPattern%>nul 2>&1
:: check for errorlevel 1 or higher. errorlevel 0 is handled as
:: an unchecked fall-through
if errorlevel 1 goto :MyHandleErrorCode
::Success (errorlevel ! >= 1) so proceed.
echo %param% is a valid number
echo (matches findstr /r %param% %matchPattern%)
echo findstr returned errorlevel 0
::any other code that the batch file needs to do goes here
echo.
echo Iterating from 1 to %param%
echo.
for /l %%i in (1,1,%param%) do call :DoWork %%i
::anything else,
:: .
:: .
::cleanup
:: .
:: .
::exit the batch file here, skipping embedded subroutines
goto :eof
:::::::::::::::::::::::::
:: Main work subroutine
:::::::::::::::::::::::::
:DoWork
set /a offset = %1 - 1
set /a square = %1 * %1
echo item %1
echo offset: %offset%
echo square: %square%
echo.
goto :eof
:::::::::::::::::::::::
:: Error handler code
:::::::::::::::::::::::
:MyHandleErrorCode
echo.
echo CountTo %param%
echo %param% is not a valid number
echo (does not match findstr /r %param% %matchPattern%)
echo findstr returned errorlevel ^>= 1
:: error code doesn't have a goto :eof, we want to drop through to :Usage
::::::::
:Usage
::::::::
echo.
echo Usage:
echo. CountTo ^<someNumber^>
以下问题是它始终返回“有效数字”,因为“ if errorlevel 0”始终为true,这是因为它是一个“> = 0”比较,而不是“ == 0”比较。
@echo off
set param=%1
echo %param%| findstr /r "^[1-9][0-9]*$">nul
if errorlevel 0 (
echo Valid number
)
我的$ .02
您可以验证任何变量,如果其编号为:
SET "var="&for /f "delims=0123456789" %i in ("%a") do set var=%i
if defined var (echo."NIC">nul) else (echo."number")
我不知道为什么,但是在我的系统上该findstr命令不起作用。匹配或不匹配时未更改错误级别。
我确实想出了另一种方法
:: Look for all digits. Parse the string and use all the digits as
:: delimiters. If the entire string is a digit then we will get an empty
:: parse value.
SET ALL_DIGITS=0
FOR /F "tokens=* delims=0123456789" %%a IN ("%VALUE%") DO (
IF "[%%a]" EQU "[]" SET ALL_DIGITS=1
)
IF %ALL_DIGITS% EQU 0 (
ECHO ERROR: %VALUE% is not all numbers
)
:main
set /p input=text
if %input% equ 0 goto valid
set /a inputval="%input%"*1
if %inputval% equ 0 goto invalid
goto valid
:invalid
echo Input is not an integer.
:valid
echo Input is an integer.
这是使用此功能的游戏。
@echo off
:main
set /a guessmin=1
set /a guessmax=100
set /a guessrand=%random% %%100 +1
echo.
:check
if %guessmin% equ %guessmax% goto fail
set /p input=- Pick a number %guessmin% - %guessmax%:
set /a inputval="%input%"*1
if %inputval% equ 0 goto invalid
if %inputval% gtr %guessmax% goto invalid
if %inputval% lss %guessmin% goto invalid
if %inputval% gtr %guessrand% goto high
if %inputval% lss %guessrand% goto low
if %inputval% equ %guessrand% goto mid
:invalid
echo Please enter a valid number.
echo.
goto check
:high
echo Your guess was too high.
echo.
set /a guessmax=%inputval%-1
goto check
:low
echo Your guess was too low.
echo.
set /a guessmin=%inputval%+1
goto check
:mid
echo Your guess was correct. The game will now reset.
set /p input=- Press enter to play again.
cls
goto main
:fail
echo You actually managed to lose because there is only
echo one number remaining. That number is %guessrand%.
set /p input=- Press enter to lose again.
cls
goto main
set /a NO_LINES=%~1.; 2if %NO_LINES% NEQ %~1 goto ABORT。当两者相等时-变量或参数为数字。