如何在BATCH字符串中提取数 [英] How to extract number from string in BATCH

查看:160
本文介绍了如何在BATCH字符串中提取数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想先抽取号码,在路径字符串中。

一些例子

  C:\\目录\\ release1 \\ TEMP应提取:1
C:\\目录\\ release11 \\ TEMP应提取:11
C:\\目录\\ release1 \\ TEMP \\ REL2应提取:1
C:\\目录\\ release15a \\ TEMP应提取:15

我目前的code,即循环槽文件夹名称,并测试文件夹的名字是一个数字(我在这里需要一些变化):

  SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion
集线等于一\\ 2 \\ 3 \\ 4 \\宠物\\ 0 \\ SEST \\ REL6 \\一
REM SET行=%CD%
:processToken
FOR / F令牌= 1 * delims = \\%%一中(%线%)做(
回声令牌:%%一
集线= %% b
REM修复需要的位置:需要从字符串中提取数
呼应%%一个| FINDSTR / R / C:^ [0-9] [0-9] * $> NUL
如果错误级别1(回声非数字)其他(回声号)

如果不是%线%==GOTO:processToken
ENDLOCAL

谢谢!

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

  ::测试最后3个字符
设置RELNO =令牌:〜-3,3!
回声令牌:〜-3,3 | FINDSTR / R / C:!^ [0-9] * $> NUL
如果错误级别1(不呼应号)其他(回声号)::测试最后2个字符
设置RELNO =令牌:〜-2,2!
回声令牌:〜-2,2 | FINDSTR / R / C:!^ [0-9] * $> NUL
如果错误级别1(不呼应号)其他(回声号)::测试最后一个字符
设置RELNO =令牌:〜!1,1!
回声令牌:〜-1,1 | FINDSTR / R / C:!^ [0-9] * $> NUL
如果错误级别1(不呼应号)其他(回声号)


解决方案

好吧,这里有一批唯一版本。它实现ISDIGIT,走输入寻找第一位数字,并停止并打印时它击中端或一个非数字的字符。

这是缓慢的 - 越长的输入,速度较慢是

  @setlocal
关闭@echo
REM extractfirstnumber.bat
REM鉴于可能包含数字的字符串,打印的第一个整数。
REM 123test456 - > 123
REM注意,特殊字符可能无法妥善处理。 (例如,)
REM http://stackoverflow.com/questions/6120623/how-to-extract-number-from-string-in-batch
组输入=%1
如果(%输入%)==()转到:EOF
拨打:firstnum输入输出
如果不是(%输出%)==()呼应%输出%和放大器;&安培;转到END_SUCCESS
转到end_fail:END_SUCCESS
ENDLOCAL
@exit / B 0:end_fail
ENDLOCAL
@exit / B 1:firstnum
SETLOCAL ENABLEDELAYEDEXPANSION
呼叫建立串= %%%〜1 %%
集/ A指数= 0
设置return_number =
转到firstnum_loop:firstnum_loop
如果(字符串:!〜%索引%,1)==()转到firstnum_end
设置test_char =字符串:!〜%索引%,1!
拨打:ISDIGIT test_char is_digit
REM发现一个数字?将它添加到回报。
如果(%is_digit%)==(真)设置return_number =%return_number %% test_char%
REM发现了一个没有数字?如果我们发现之前,结束一个数字。
如果(%is_digit%)==(假)如果不是(%return_number%)==()转到firstnum_end
集/ A指数=%索引%+ 1
转到firstnum_loop:firstnum_end
(ENDLOCAL&安培; REM返回值
    IF%〜2NEQSET%〜2 =%return_number%

GOTO:EOF:ISDIGIT
SETLOCAL ENABLEDELAYEDEXPANSION
组号码= 1234567890
设置found_number = FALSE
呼叫建立串= %%%〜1 %%
REM如果传递的字符串没有一个单个字符,以虚假立即返回。
如果(%字符串:〜0.1%)==()转到isdigit_end
如果不是(%字符串:〜1,1%)==()转到isdigit_end
集/ A指数= 0
转到isdigit_loop:isdigit_loop
如果(号码:!〜%索引%,1)==()转到isdigit_end
设置test_char =号码:!〜%索引%,1!
如果(%test_char%)==(%字符串%),设置found_number =真放;&安培;转到isdigit_end
集/ A指数=%索引%+ 1
转到isdigit_loop:isdigit_end
(ENDLOCAL&安培; REM返回值
    IF%〜2NEQSET%〜2 =%found_number%

GOTO:EOF

示例输出:

  C:\\> extractfirstnumber C:\\目录\\ release1 \\ TEMP
1
C:\\> extractfirstnumber C:\\目录\\ release11 \\ TEMP
11
C:\\> extractfirstnumber C:\\目录\\ release1 \\ TEMP \\ REL2
1
C:\\> extractfirstnumber C:\\目录\\ release15a \\ TEMP
15

I would like to extract first number, found in a path string.

Some examples

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

My current code, that loops trough folder names, and tests if folder name is a number (I need some changes here):

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

Thanks!

EDIT: I wanted to parse number from that path string. Well I've found solution that checks only last 3 characters of string. It's ok for now.

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

解决方案

Okay, here's a batch only version. It implements isdigit, walks the input looking for the first digit, and stops and prints the characters between when it hits the end or a non-digit.

This is slow - the longer the input, the slower it is.

@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

Sample output:

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

这篇关于如何在BATCH字符串中提取数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆