Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Windows shell脚本中的字符串中提取数字_Windows_For Loop_Batch File_Cmd - Fatal编程技术网

从Windows shell脚本中的字符串中提取数字

从Windows shell脚本中的字符串中提取数字,windows,for-loop,batch-file,cmd,Windows,For Loop,Batch File,Cmd,我有这样一个字符串,脚本文件hello-1234-something。我需要得到1234使用批处理文件。但是数字1234不一样,它一直在变化,我需要在字符串中找到数字,然后只取出数字。我不熟悉批处理文件编程。我想成批地做这件事试试这个: @ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION SET "teststring=abcDEFG1234ABSdefh" FOR %%a IN ( a b c d e f g h i j k l m n o

我有这样一个字符串,脚本文件hello-1234-something。我需要得到1234使用批处理文件。但是数字1234不一样,它一直在变化,我需要在字符串中找到数字,然后只取出数字。我不熟悉批处理文件编程。我想成批地做这件事

试试这个:

@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "teststring=abcDEFG1234ABSdefh"

FOR %%a IN (
    a b c d e f g h i j k l m n o p q r s t u v w x y z
        ) DO (
    SET "teststring=!teststring:%%a=!"
)
ECHO %teststring%

注意:这不适用于特殊字符,例如:
&|^

如果您的字符串始终具有字符串连字符编号连字符字符串的形式(例如,
foo-23-bar
some-205-or-other
,…),您可以执行以下操作:

@echo off

setlocal

set "string=foo-23-bar"

for /f "tokens=2 delims=-" %%n in ("%string%") do set "num=%%n"

echo %num%