我有一个名为的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\)?"
但不起作用
非常感谢
Johan
答案 0 :(得分:1)
有很多方法可以通过纯批处理实现。
:: 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
答案 1 :(得分:0)
您将无法使用findstr
实现此目的,它将不允许您提取匹配的数据。我建议您改用PowerShell:
echo $Env:JAR_FILE | Select-String -Pattern '[0-9]+(\.[0-9]+){0,3}(-\w+){0,2}' | % { $_.Matches } | % { $_.Value }