regex - Windows cmd脚本从jar文件中提取版本号

标签 regex cmd window

我有一个名为

的 jar 文件
MyApplication-1.9.1-SNAPSHOT-windows.jar

如何将版本号(即 1.9.1-SNAPSHOT)提取到 Windows cmd 脚本中的变量中

-SNAPSHOT 是可选的,因此 MyApplication-1.9.1-windows.jar 必须生成 1.9.1

我已经尝试过了

echo %JAR_FILE% | findstr /i /r "[0-9]\.[0-9]\.[0-9]\(-SNAPSHOT\)?"

但不工作

非常感谢

约翰

最佳答案

有很多方法可以通过纯批处理来实现此目的。

:: Q:\Test\2018\11\06\SO_53170183.cmd
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion

Set "JAR_FILE=MyApplication-1.9.1-SNAPSHOT-windows.jar"

:: Variant 1 for /f
For /f "tokens=2 delims=-" %%V in ("%JAR_FILE%") Do Set "Version=%%V"
Echo Version=%Version%

:: Variant 2 self modifying code
Set i=0&Set "Split=-%JAR_FILE%"
Set "Split=%Split:-="&Set /a i+=1&Set "Split[!i!]=%"
Set Split

:: Variant 3 shuffle
Set "Strip=%JAR_FILE:*-=%"
Set "Rest=%Strip:*-=%"
Set "Version=!Strip:-%Rest%=!"
Echo Version3=%Version%

:: wrapped powershell
For /f "usebackq" %%V in (`
  Powershell -Nop -C "[regex]::match($ENV:JAR_FILE,'(\d+\.\d+\.\d+)').Groups[1].Value
`) Do set "Version4=%%V"
Echo Version4=%Version%

示例输出:

>  Q:\Test\2018\11\06\SO_53170183.cmd
Version=1.9.1
Split[1]=MyApplication
Split[2]=1.9.1
Split[3]=SNAPSHOT
Split[4]=windows.jar
Version3=1.9.1
Version4=1.9.1

关于regex - Windows cmd脚本从jar文件中提取版本号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53170183/

相关文章:

python - 斜杠在python字符串和正则表达式中的使用

php - 将单词分为两部分 : letters and numbers

python - 接受 1 个位置参数(给定 2 个)

mvvm - 如何使用中介模式在 MVVM 中打开/显示新窗口?

r - 通过 strsplit 使用非贪婪正则表达式拆分字符串

python - 直到注释或行尾的表达式

windows - 如何通过批处理文件在 Windows 防火墙上打开端口

windows - 如何从批处理文件中调用 VBA 函数

c - SDL指定窗口大小和位置

html - 当窗口缩小时,CSS 使可用空间首先减少