Batch file 如何在批处理中检查字符串不是以数字开头的?
如何检查字符串的第一个字符是字母,而不是数字,或者更确切地说是密码?此字符串中没有空格或特殊字符Batch file 如何在批处理中检查字符串不是以数字开头的?,batch-file,Batch File,如何检查字符串的第一个字符是字母,而不是数字,或者更确切地说是密码?此字符串中没有空格或特殊字符 @echo off setlocal set "the_string=a23something" for /l %%a in (%the_string% ; 1 ; %the_string%) do set "cl_string=%%~a" if %the_string:~0,1% neq 0 if "%cl_string%" equ "0" ( echo does not sta
@echo off
setlocal
set "the_string=a23something"
for /l %%a in (%the_string% ; 1 ; %the_string%) do set "cl_string=%%~a"
if %the_string:~0,1% neq 0 if "%cl_string%" equ "0" (
echo does not start with number
) else (
echo starts with number
)
endlocal
另一种方法是使用FINDSTR
,它最终会变慢,因为它是cmd.exe
命令的外部命令
@echo off
set "the_string=something"
echo %the_string%|findstr /b /r "[0-9]" >nul 2>&1 && (
echo starts with number
) || (
echo does not start with number
)
如果var内容以数字开头,则for
命令中的token/delim管理将删除它
编辑只是为了包括通常的(包括以前的代码)和一些不太常用的选项,以防有人感兴趣
@echo off
setlocal enableextensions disabledelayedexpansion
set "var=1hello"
echo(%var%
rem Option 1 - Use the for command to tokenize the string
rem A dot is added to handle empty vars
for /f "tokens=* delims=0123456789" %%a in ("%var%.") do (
if not "%%a"=="%var%." (
echo var starts with a number
) else (
echo var does not start with a number
)
)
rem Option 2 - Use set arithmetic and detect errors
rem This will fail if the string starts with + or -
set "%var%_=0"
set /a "test=%var%_" 2>nul
if not errorlevel 1 (
echo var does not start with a number
) else (
echo var starts with a number
)
rem Option 3 - Use substring operations and logic operators
set "test=%var%."
if "%test:~0,1%" GEQ "0" if "%test:~0,1%" LEQ "9" set "test="
if defined test (
echo var does not start with a number
) else (
echo var starts with a number
)
rem Option 4 - Use findstr
rem This is SLOW as findstr needs to be executed
echo(%var%|findstr /b /r /c:"[0-9]" >nul && (
echo var starts with a number
) || (
echo var does not start with a number
)
将
findstr
与regexp一起使用:
@echo off
set "$string=2toto"
echo %$string:~0,1%|findstr /i "^-*0*x*[0-9][0-9]*$">nul && echo is NUM || echo Is not NUM
代替echo is NUM
或echo is not NUM
您可以使用goto
以您想要的方式重定向脚本
@echo off
set "$string=2toto"
echo %$string:~0,1%|findstr /i "^-*0*x*[0-9][0-9]*$">nul && goto:isnum || goto:isnotnum
:isnum
echo is NUM
exit/b
:isnotnum
echo is not NUM
必须将字符串设置为变量;通过这种方式,您可以从主字符串中提取子字符串。以下是一个例子:
@echo off
set EXAMPLESTRING=12345abcde
set FIRSTCHARACTERSTRING=%EXAMPLESTRING:~0,1%
在这种情况下,此短脚本的结果应该是1
。
然后,可以设置一系列条件来验证第一个字符是否为数字:
如果%FIRSTCHARACTERSTRING%==0转到号码如果%FIRSTCHARACTERSTRING%==1转到号码
如果%FIRSTCHARACTERSTRING%==2个转到号码
如果%FIRSTCHARACTERSTRING%==3转到号码
如果%FIRSTCHARACTERSTRING%==4转到号码
如果%FIRSTCHARACTERSTRING%==5转到号码
如果%FIRSTCHARACTERSTRING%==6转到号码
如果%FIRSTCHARACTERSTRING%==7转到号码
如果%FIRSTCHARACTERSTRING%==8转到号码
如果%FIRSTCHARACTERSTRING%==9转到号码
去信 :编号
回显第一个字符是一个数字
转到EOF
:字母
回显第一个字符是一个字母
转到EOF 也许这不是最有效的解决方案,但它运行良好,更容易理解。
@ECHO OFF
SETLOCAL
设置/a数量=5678
调用:initnum
设置“num=hello”
调用:initnum
设置“num=4ello”
调用:initnum
设置“num=hell0”
调用:initnum
设置“num=he8lo”
调用:initnum
设置“num=”
调用:initnum
回音(==============
设置/a nam=7654
设置“nem=hello”
设置“nim=4ello”
设置“nom=hell0”
设置“num=he8lo”
设置“nzm=”
电话:initnum2 nam
呼叫:initnum2 nem
调用:initnum2 nim
调用:initnum2 nom
调用:initnum2 num
调用:initnum2 nzm
后藤:EOF
:initnum
如果未定义num ECHO num为空,则它不会以数字&GOTO:EOF开头
对于(0,1,9)中的/l%%a,如果%num:~0,1%==%%则执行,回显%num%以数字开头(&GOTO:EOF)
回显%num%不是以数字开头
后藤:eof
:initnum2
如果未定义,则%1回显%1为空,因此它不会以数字&GOTO:EOF开头
调用集“$1=%%%1%%”
对于(0,1,9)中的/l%%a,如果%$1:~0,1%==%%,则执行回显%1(%%$1%)以数字开头(&GOTO:EOF)
回显%1(%$1%)不是以数字开头
后藤:eof
您应该能够从本演示中获得所需内容。这将在您的情况下起作用:
echo %variable%|findstr "^[a-zA-Z]" >nul && echo it starts with an alpha character
我认为这是最简单的方法:
@echo off
setlocal EnableDelayedExpansion
set digits=0123456789
set var=1something
if "!digits:%var:~0,1%=!" neq "%digits%" (
echo First char is digit
) else (
echo First char is not digit
)
尝试从
数字
字符串中删除var的第一个字符。如果这样的字符是数字,则数字
字符串将更改;否则,数字
字符串将保持不变。相关:您的搜索字符串将是与FINDSTR^[0-9]行类似的内容
+1.但是为什么要禁用延迟扩展?它真的有必要吗?@npocmaka,在声明的“…变量中没有空格或特殊字符…”
不,没有必要。但是从前面的答案来看,现在我有了定义环境行为的习惯,以防…@npocmaka No,^
将其锚定到行的开头。
@echo off
setlocal EnableDelayedExpansion
set digits=0123456789
set var=1something
if "!digits:%var:~0,1%=!" neq "%digits%" (
echo First char is digit
) else (
echo First char is not digit
)