【发布时间】:2019-11-01 09:34:30
【问题描述】:
我需要像这样拆分一个字符串;并尝试获取号码
“记录数/CC_NUMBER = 123”。
我尝试了很多方法,但都失败了。
数字可以更大(4位或更多)或更小
已经感谢您的帮助。
【问题讨论】:
标签: batch-file
我需要像这样拆分一个字符串;并尝试获取号码
“记录数/CC_NUMBER = 123”。
我尝试了很多方法,但都失败了。
数字可以更大(4位或更多)或更小
已经感谢您的帮助。
【问题讨论】:
标签: batch-file
一种可能的方法是使用标准的for loop:
set "STR=Record Count/CC_NUMBER = 123"
for %%I in (%STR%) do set "VAL=%%I"
echo/%VAL%
每个SPACE 以及=-sign 都被视为标记分隔符,每个标记都由for 循环处理,最后一次迭代将数值赋给变量.
当字符串包含以下任何字符时,这可能会失败:*、?、<、>、"、^、|、&、( , ).
【讨论】:
如果行长可以包含任意数量的单词,然后用=分割并去掉空格,它也不关心=前后是否有空格:
@echo off
set "line=this can be any length Record Count/CC_NUMBER = 123"
for /f "tokens=2 delims==" %%i in ("%line%") do set "var=%%i"
echo %var: =%
另外,正如Stephan 所建议的,与上述类似但更短的方法:
@echo off
for /f "tokens=2 delims==" %%i in ("%line: =%") do echo %%i
如果该行带有空格并且数字是第三个标记,如您的示例所示:
@echo off
set "line=Record Count/CC_NUMBER = 123"
for /f "tokens=3" %%i in ("%line%") do echo %%i
如果该行不包含我们用= 分隔的空格:
@echo off
set "line=Record Count/CC_NUMBER=123"
for /f "tokens=2 delims==" %%i in ("%line%") do echo %%i
【讨论】:
for /f "tokens=2 delims==" %%i in ("%line: =%") do echo %%i
一种选择是按空格分割输入,然后打印/回显第三个标记:
for /f "tokens=3" %%G IN ("Record Count/CC_NUMBER = 123") DO echo %%G
更多信息请参见How to split a string by spaces in a Windows batch file?。
【讨论】:
如果所需的标记是严格的数字(在INT32 的范围内),您可以使用set /a,它会忽略任何空格:
@echo off
set "line=this can be any length Record Count/CC_NUMBER = 123"
for /f "tokens=2 delims==" %%i in ("%line%") do set /a var=%%i
echo "%var%"
【讨论】:
这是另一种在 powershell 中使用正则表达式从TestFile.txt 的字符串内容中提取数字的解决方案
@echo off & color 0A
Title Extract a Digit Number from String using Regex in Powershell
Set "InputFile=TestFile.txt"
Set psCmd="&{$content=Get-Content -Path '%InputFile%';[regex]$regex='\d+';$regex.Matches($content).value}"
Call :RunPS %psCmd% Number
Echo The Number extracted is N=%Number%
pause & Exit
::----------------------------------------------------------------------
:RunPS <PassPSCMD> <RetValue>
for /F "usebackq tokens=*" %%i in (`Powershell %1`) do set "%2=%%i"
Goto:eof
:: End of :RunPS function
::----------------------------------------------------------------------
【讨论】:
这可以通过使用 echo 命令解决,结合 cmd /u 使用 Find + FindSTR +regex 删除字符串中的非数字> 循环For。
cmd /u echo your string中它们之间的空格。FindSTR和 RegEx 在此输出过程中仅获取 数字...
使用 For 逐一循环以过滤 U n i c o d e echo 输出中的字符串并删除Find / v " " 的空格,然后使用 FindSTR 应用另一个过滤器,通过添加 regex 仅获得 数字 > 在 FindSTR 中并将递增的循环输出保存到变量中。
Loop output variable !_str! in unicode filter space and get numbers:
@echo off && setlocal EnableDelayedExpansion
set "_str= Record Count/CC_NUMBER = 01234 "
for /f %%n in ('cmd /u /c echo="!_str!"^|find /v " "^|findstr [0-9]
')do echo=%%n
:: echo=%%n == one by one nunber character in loop output:
:: -----------------------------------------------------------------
:: echo=%%n == output !_str! reults == 0
:: echo=%%n == output !_str! reults == 1
:: echo=%%n == output !_str! reults == 2
:: echo=%%n == output !_str! reults == 3
:: echo=%%n == output !_str! reults == 4
:: -----------------------------------------------------------------
观察:1)
- 因此,一个一个操作输出,可以检查输出的字符是否为数字,否则处理中的当前字符,也不会显示,这哪里有可能检查/获取任何 数字 发生在 variable/string 中的任何位置,循环,因此,无需 担心/工作带分隔符。
观察:2)
- 这个机械工作是通过在这个 cmd/bat 进程中使用
EnableDelayedExpansion完成的。
Loop output variable !_str! in Unicode incremented variable with numbers %%n:
@echo off && setlocal EnableDelayedExpansion
(set "_str=0R/ecord\-# ',? 1>|<2Count>/3$4]C{^5_6&7}NUMBER = *|8!9"
for /f %%n in ('cmd /u /c echo="!_str!"^|find /v " "^|findstr [0-9]
')do set "_l=!_l!%%~n") && echo=Numbers in this variable _str: !_l!
:: -----------------------------------------------------------------------------
:: loop output !_str! reults == 0 && set "_var=!_var!%%~n results == 0
:: loop output !_str! reults == 1 && set "_var=!_var!%%~n results == 01
:: loop output !_str! reults == 2 && set "_var=!_var!%%~n results == 012
:: loop output !_str! reults == 3 && set "_var=!_var!%%~n results == 0123
:: loop output !_str! reults == 4 && set "_var=!_var!%%~n results == 01234
:: loop output !_str! reults == 5 && set "_var=!_var!%%~n results == 012345
:: loop output !_str! reults == 6 && set "_var=!_var!%%~n results == 0123456
:: loop output !_str! reults == 7 && set "_var=!_var!%%~n results == 01234567
:: loop output !_str! reults == 8 && set "_var=!_var!%%~n results == 012345678
:: loop output !_str! reults == 9 && set "_var=!_var!%%~n results == 0123456789
:: -----------------------------------------------------------------------------
get all numbers 01234 from string independent of the delimiter
@echo off && setlocal EnableDelayedExpansion
(set "_str= Record Count/CC_NUMBER = 01234 "
for /f %%n in ('cmd /u /c echo="!_str!"^|find /v " "^|findstr [0-9]
')do set "_l=!_l!%%~n") && echo=Numbers in this variable _str: !_l!
rem :: Code Results: Numbers in this variable _str: 01234
To get all numbers from string in any position independent of the delimiter
- 字符串:
0R/ecord\-# ',? 1>|<2Count>/3$4]C{^5_6&7}NUMBER = *|8!9- 输出:
Numbers in this variable _str: 0123456789
@echo off && setlocal EnableDelayedExpansion
(set "_str=0R/ecord\-# ',? 1>|<2Count>/3$4]C{^5_6&7}NUMBER = *|8!9"
for /f %%n in ('cmd /u /c echo="!_str!"^|find /v " "^|findstr [0-9]
')do set "_l=!_l!%%~n") && echo=Numbers in this variable _str: !_l!
【讨论】: