如何使用Windows CMD基于模式提取部分字符串?

时间:2015-01-23 15:48:37

标签: batch-file cmd

我有一个输入让我们说:

DummyString-v1.0.0

我希望在v之后提取所有内容:1.0.0

如何在CMD中实现这一目标?

2 个答案:

答案 0 :(得分:7)

@ECHO OFF
SETLOCAL
SET "string=DummyString-v1.0.0"
SET "string=%string:*v=%"
ECHO %string%
GOTO :EOF

其中应用:*substring=replacementstring语法意味着"每个字符构成变量的开头(string)直到子字符串(v)被替换字符串替换( mothing )"

答案 1 :(得分:1)

@echo off

call :split "DummyString-v1.0.0" "v"
echo %last_part%

exit /b 0

:split

set "string=%~1"
set "splitter=%~2"

setlocal EnableDelayedExpansion
set LF=^


rem ** Two empty lines are required
echo off
for %%L in ("!LF!") DO (
    for /f "delims=" %%R in ("!splitter!") do ( 
        set "var=!string:%%R=%%L!"      
    )
)
for /f "delims=" %%P in (""!var!"") DO set "last_part=%%~P"
endlocal &(
  set last_part=%last_part%
  exit /b 0
)