在找到“-1”或任何其他数字“-[0-9]”后批量删除部分字符串

Batch remove part of string after “-1” is found or any other number “-[0-9]”

我得到一个文件,每行包含一个字符串,如下所示:

fruit-apple-1.5.6
vegtable-sla-mc5-6.5-16515
extra-huh-9.5-511-515
extra-3.2

我正在遍历它并希望它在找到“-”+任何数字“-0”,“-1”,“-2”,“-9”之后删除右边的字符串部分",...

所以输出应该是

fruit-apple
vegtable-sla-mc5
extra-huh
extra

这是我的代码,但它只适用于“-”,我不能将它组合起来,所以它需要“-”+任何数字,如“-1”、“-5”、“-2”、.. .

for /f "delims=|" %%A in ("!fileNameCheck:-=|!") do (
echo stripped string = %%A
)

我认为不需要完整的代码,但如果您需要下面的代码

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set "RawPath=%~dp0"


FOR /F "USEBACKQ TOKENS=*" %%M IN ("%RawPath%/mods") DO (
REM for %%f in (*.jar) do (
    Set "fileNameCheck=%%M"
    for /f "delims=|" %%A in ("!fileNameCheck:-=|!") do (
        Echo [46m%%A[0m
        if exist "%~dp0%%A*.jar" (
            REM echo [32mFound %%A "%~dp0%%A*.jar"[0m
            if exist "%~dp0%%M" (
                REM echo [42mUp to Date[0m [32m%%A "%~dp0%%M"[0m
            ) else (
                for %%j in (*.jar) do (
                    echo %%j |find "%%A" >nul
                    if not errorlevel 1 (
                        echo [41mDifferent Version[0m [31m%%j [0m[90mNewer version[0m [32m%%M[0m 
                    )
                    
                )
                
            )
        ) else (
            REM echo [31mMissing %%A[0m
        )
    )
)
pause

一种方法是将 - 替换为 \,然后使用变量替换从字符串中删除 %%~ni 部分。我们只需要从字符串中删除第一次和最后一次出现。 findstr会判断字符串结尾需要-num.*

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('dir /b /s *.jar ^| findstr /RC:"-[0-9].*$"') do (
        set "line=%%~nxA"
        for %%i in ("!line:-=\!") do set "final=%%~pi"
        set "final=!final:~0,-1!-"
        echo !final! | findstr /RC:"\[0-9].*$" >nul 2>&1
        if !errorlevel! equ 0 (
            for %%s in ("!final!") do set "final=%%~ps"
            set final=!final!
        )
        set "final=!final:~0,-1!"
        set "final=!final:~1!"
        echo !final:\=-!
)

根据您的评论示例 name-subname-10.5-55.5

,编辑后的代码应处理额外的 -

这是一个未经测试的想法,它可能不是特别快,特别是如果您的 mods 文件中有大量字符串:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F %%G In ('Echo Prompt $E ^| %SystemRoot%\System32\cmd.exe /D'
) Do Set "$E=%%G"
For /F Delims^=^ EOL^=^ UseBackQ %%G In ("%~dp0mods") Do (Set "Line=%%G"
    SetLocal EnableDelayedExpansion
    For /L %%H In (0 1 9) Do For /F Delims^=^ EOL^= %%I In (
        '(Echo(^) ^| Set /P "=!Line:-%%H="^&:"!" 0^<NUL') Do Set "Line=%%I" 
    Echo(%$E%[46m!Line!%$E%[0m
    EndLocal
)
Pause

鉴于字符串不包含任何字符 ?*<>,以下脚本应该可以解决问题:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_ROOT=%~dp0."
set "_FILE=mods.txt"

rem // Read file line by line:
for /F usebackq^ delims^=^ eol^= %%L in ("%_FILE%") do (
    rem // Store current line, initialise variables:
    set "LINE=%%L" & set "PREV=-" & set "COLL=-"
    setlocal EnableDelayedExpansion
    rem /* Split line string at every `-` and iterate through items;
    rem    (`?`, `*`, `<`, `>` must not occur in the string!): */
    for %%I in ("!LINE:-=" "!") do (
        endlocal & set "ITEM=%%~I"
        rem // Check whether item only consists of numerals and `.`:
        (
            rem // This loop executes when there are other characters:
            for /F "delims=0123456789. eol=." %%J in ("%%~I") do (
                setlocal EnableDelayedExpansion
                rem /* Rebuilt string with currently found items; if there
                rem    are other items following numeric ones, keep them: */
                for /F "delims=" %%K in ("!COLL!!PREV:~1!-!ITEM!") do (
                    endlocal & set "COLL=%%K" & set "PREV=-"
                )
            )
        ) || (
            rem // This section runs when there are numerals and `.`:
            setlocal EnableDelayedExpansion
            for /F "delims=" %%K in ("!PREV!-!ITEM!") do (
                rem /* Store numeric items in a separate buffer; if there
                rem    are no other items following, this is dismissed: */
                endlocal & set "PREV=%%K"
            )
        )
        setlocal EnableDelayedExpansion
    )
    rem // Return rebuilt line string:
    echo(!COLL:~2!
    endlocal
)

endlocal
exit /B

示例输入文件mods.txt:

fruit-apple-1.5.6
vegtable-sla-mc5-6.5
extra-huh-9.5
extra-3.2
test-9.15.5
keep-forge
name-subname-10.5-55.5
globalxp-1.16.5
mix-1.2-string-10.5-55.5-more
mix-1.2-string-10.5-55.5-more-3.4
mix-1.2-string-10.5-55.5-more-3.4-5.6-7
8.9
1.2.3-lead
1.2.3-4.5.6-7.8.9-lead-mult

导致以下输出:

fruit-apple
vegtable-sla-mc5
extra-huh
extra
test
keep-forge
name-subname
globalxp
mix-1.2-string-10.5-55.5-more
mix-1.2-string-10.5-55.5-more
mix-1.2-string-10.5-55.5-more

1.2.3-lead
1.2.3-4.5.6-7.8.9-lead-mult