如何使用 Windows CMD 根据模式提取字符串的一部分?

How can I extract part of a string based on a pattern using Windows CMD?

我有一个输入让我们说:

DummyString-v1.0.0

我想提取 v 之后的所有内容以获得:1.0.0

如何在 CMD 中实现这一点?

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

其中应用 :*substring=replacementstring 语法意味着“从变量 (string) 开始到子字符串 (v) 的每个字符都被替换字符串 ( 没有).

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