如何检查字符串不是以批处理中的数字开头的?

时间:2014-08-14 11:25:06

标签: batch-file

如何检查字符串的第一个字符是否为字母,以便它不是数字,或者更确切地说是密码?此字符串中没有空格或特殊字符。

7 个答案:

答案 0 :(得分:2)

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "var=1hello"

    for /f "tokens=* delims=0123456789" %%a in ("%var%") do (
        if not "%%a"=="%var%" echo var starts with a number
    )

如果var内容以数字开头,for命令中的令牌/分隔管理将删除它。

已修改只是为了包含通常(包含前面的代码)和一些较少使用的选项,以防有人感兴趣

@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
    )

答案 1 :(得分:2)

@ECHO OFF
SETLOCAL
SET /a num=5678
CALL :initnum
SET "num=hello"
CALL :initnum
SET "num=4ello"
CALL :initnum
SET "num=hell0"
CALL :initnum
SET "num=he8lo"
CALL :initnum
SET "num="
CALL :initnum
ECHO(==============
SET /a nam=7654
SET "nem=hello"
SET "nim=4ello"
SET "nom=hell0"
SET "num=he8lo"
SET "nzm="
CALL :initnum2 nam
CALL :initnum2 nem
CALL :initnum2 nim
CALL :initnum2 nom
CALL :initnum2 num
CALL :initnum2 nzm

GOTO :EOF

:initnum
IF NOT DEFINED num ECHO NUM is empty, so it doesn't begin with a numeric&GOTO :EOF 
FOR /l %%a IN (0,1,9) DO IF %num:~0,1%==%%a ECHO %num% Begins with numeric&GOTO :EOF 
ECHO %num% Does NOT begin with a numeric
GOTO :eof

:initnum2
IF NOT DEFINED %1 ECHO %1 is empty, so it doesn't begin with a numeric&GOTO :EOF 
CALL SET "$1=%%%1%%"
FOR /l %%a IN (0,1,9) DO IF %$1:~0,1%==%%a ECHO %1 (%$1%) Begins with numeric&GOTO :EOF 
ECHO %1 (%$1%) Does NOT begin with a numeric
GOTO :eof

你应该能够从这个演示中得到你想要的东西。

答案 2 :(得分:1)

@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
)

答案 3 :(得分:1)

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 NUMecho 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

答案 4 :(得分:1)

这适用于您的情况:

echo %variable%|findstr "^[a-zA-Z]" >nul && echo it starts with an alpha character

答案 5 :(得分:1)

我认为这是最简单的方法:

@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
)

尝试从digits字符串中删除var的第一个字符。如果这样的字符是数字,digits字符串更改;否则,digits字符串保持不变。

答案 6 :(得分:0)

您必须将字符串设置为变量;通过这种方式,您可以从主字符串中提取子字符串。这是一个例子:

@echo off
set EXAMPLESTRING=12345abcde
set FIRSTCHARACTERSTRING=%EXAMPLESTRING:~0,1%

在这种情况下,此短脚本的结果应为1。 然后,您可以设置一系列条件来验证第一个字符是否为数字:

  

如果%FIRSTCHARACTERSTRING%== 0转到NUMBER
  if%FIRSTCHARACTERSTRING%== 1 goto NUMBER
  if%FIRSTCHARACTERSTRING%== 2 goto NUMBER
  if%FIRSTCHARACTERSTRING%== 3 goto NUMBER
  if%FIRSTCHARACTERSTRING%== 4 goto NUMBER
  if%FIRSTCHARACTERSTRING%== 5 goto NUMBER
  如果%FIRSTCHARACTERSTRING%== 6转到NUMBER
  if%FIRSTCHARACTERSTRING%== 7 goto NUMBER
  if%FIRSTCHARACTERSTRING%== 8 goto NUMBER
  if%FIRSTCHARACTERSTRING%== 9 goto NUMBER
  转到信中

     

:NUMBER
  echo第一个字符是数字!
  转到EOF
  :字母
  echo第一个字符是一个字母!
  转到EOF

也许这不是最有效的解决方案,但它工作正常,更容易理解。