如何从BATCH中的字符串中提取数字

时间:2011-05-25 06:52:14

标签: string parsing batch-file

我想提取第一个数字,在路径字符串中找到。

一些例子

c:\dir\release1\temp  should extract: 1
c:\dir\release11\temp  should extract: 11
c:\dir\release1\temp\rel2  should extract: 1
c:\dir\release15a\temp  should extract: 15

我当前的代码,循环文件夹名称,并测试文件夹名称是否为数字(我需要在此处进行一些更改):

setlocal enableextensions enabledelayedexpansion
set line=one\two\three\4\pet\0\sest\rel6\a
rem set line=%cd%
:processToken
for /f "tokens=1* delims=\" %%a in ("%line%") do (
echo Token: %%a
set line=%%b
rem need fix here: need to extract number from string
echo %%a|findstr /r /c:"^[0-9][0-9]*$" >nul
if errorlevel 1 (echo not a number) else (echo number)
)
if not "%line%" == "" goto :processToken
endlocal

谢谢!

编辑: 我想从该路径字符串中解析数字。 好吧,我找到了只检查字符串的最后3个字符的解决方案。现在好了。

::test last 3 characters
set relno=!token:~-3,3!
echo !token:~-3,3!|findstr /r /c:"^[0-9]*$" >nul
if errorlevel 1 (echo not number) else (echo number)

::test last 2 characters
set relno=!token:~-2,2!
echo !token:~-2,2!|findstr /r /c:"^[0-9]*$" >nul
if errorlevel 1 (echo not number) else (echo number)

::test last character
set relno=!token:~-1,1!
echo !token:~-1,1!|findstr /r /c:"^[0-9]*$" >nul
if errorlevel 1 (echo not number) else (echo number)

1 个答案:

答案 0 :(得分:5)

好的,这是一个批量版本。它实现了isdigit,遍历输入以查找第一个数字,并在它到达结尾或非数字之间停止并打印字符。

这很慢 - 输入越长,它就越慢。

@setlocal
@echo off
rem extractfirstnumber.bat
rem Given a string possibly containing a number, print the first integer.
rem 123test456 -> 123
rem Note that special characters may not be properly handled.  (e.g. , ;)
rem http://stackoverflow.com/questions/6120623/how-to-extract-number-from-string-in-batch
set input=%1
if ("%input%") == ("") goto :eof
call :firstnum input output
if not ("%output%") == ("") echo %output%&&goto end_success
goto end_fail

:end_success
endlocal
@exit /b 0

:end_fail
endlocal
@exit /b 1

:firstnum
SETLOCAL ENABLEDELAYEDEXPANSION
call set "string=%%%~1%%"
set /a index = 0
set return_number=
goto firstnum_loop

:firstnum_loop
if ("!string:~%index%,1!") == ("") goto firstnum_end
set test_char=!string:~%index%,1!
call :isdigit test_char is_digit
rem Found a digit?  Add it to the return.
if ("%is_digit%") == ("true") set return_number=%return_number%%test_char%
rem Found a not-digit?  If we found a digit before, end.
if ("%is_digit%") == ("false") if not ("%return_number%") == ("") goto firstnum_end
set /a index = %index% + 1
goto firstnum_loop

:firstnum_end
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" SET "%~2=%return_number%"
)
goto :eof

:isdigit
SETLOCAL ENABLEDELAYEDEXPANSION
set NUMBERS=1234567890
set found_number=false
call set "string=%%%~1%%"
REM If the passed string does not have a single character, return immediately with false.
if ("%string:~0,1%") == ("") goto isdigit_end
if not ("%string:~1,1%") == ("") goto isdigit_end
set /a index=0
goto isdigit_loop

:isdigit_loop
if ("!NUMBERS:~%index%,1!") == ("") goto isdigit_end
set test_char=!NUMBERS:~%index%,1!
if ("%test_char%") == ("%string%") set found_number=true&&goto isdigit_end
set /a index = %index% + 1
goto isdigit_loop

:isdigit_end
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" SET "%~2=%found_number%"
)
goto :eof

示例输出:

C:\>extractfirstnumber c:\dir\release1\temp
1
C:\>extractfirstnumber c:\dir\release11\temp
11
C:\>extractfirstnumber c:\dir\release1\temp\rel2
1
C:\>extractfirstnumber c:\dir\release15a\temp
15