使用批处理多个文本 (Windows) 替换为正则表达式

Replace with regular expression using Batch multiple text (Windows)

我有三个文本文件。

C:\content1.txt C:\content2.txt C:\content3.txt

文本文件包含随机链接

https://example2.com/file/casdqwe/test1.rar https://example2.com/file/casdqwe/test2.rar https://example5.com/file/casdqwe/test3.rar

我想删除域名并只保存最后一部分。输出

test1.rar test2.rar test3.rar

并保存在原文件中,无需任何确认。

notepad++ 正则表达式替换工作,但我不确定如何在 使用 Powershell/VBS 任何脚本都可以正常工作。

Find What: https://example\.com[^\s\[\]<'\"]+/([^\s\[\]<'\"]+) Replace with:

和 powershell 我有类似的东西

(Get-Content -path C:\content1.txt -Raw) -replace "https://example2.com/file/asdcb6af26/test.rar" -replace "https://example2.com/file/(.*)\/(.*)", ''

我猜也许,

\bhttps?:\/\/.*\/

简单地替换为空字符串可能就可以了。


如果您希望 simplify/modify/explore 表达式,regex101.com. If you'd like, you can also watch in this link 的右上面板已对其进行说明,它将如何匹配一些样本输入。


参考你的,你可以试试这个在vbscript中使用正则表达式的批处理文件。

@echo off
Mode 85,35 & color 0A
Title Replace Multi String using Regex with vbscript into Folder with text files
Set "Source_Folder=C:\test"
Set "Backup_Folder=%userprofile%\Backup_Contents\"
Rem :: Just make a backup of your folder and its contents if something went wrong!
If Not Exist "%Backup_Folder%" XCopy "%Source_Folder%" "%Backup_Folder%" /D /Y /E /F >%~dp0BackupLogFile.txt
Set "VBSFILE=%tmp%\%~n0.vbs" & Call :CreateVBS
Set "TmpFile=%Temp%\%~n0.tmp"

for /R "%Source_Folder%" %%f in (*.txt) do (
    echo( ------------------------------------------
    echo  Replacing Contents of "%%f"
    echo( ------------------------------------------
    Call :Search_Replace "%%f" "%TmpFile%"
    Move /Y "%TmpFile%" "%%f">nul
)
If Exist "%VBSFILE%" Del "%VBSFILE%"
Timeout /T 3 /NoBreak>nul & Exit
::-----------------------------------------------------------------------------
:CreateVBS
(
    echo WScript.StdOut.WriteLine Search_Replace(Data^)
    echo Function Search_Replace(Data^)
    echo Dim strPattern, strReplace, strResult,oRegExp
    echo Data = "%~1" 
    echo Data = WScript.StdIn.ReadAll
    echo strPattern1 = "(\x22<|<)([\s\S]*?)(/>\x22|>| />\x22| />| \x22>)"
    echo strReplace1 = "[abc][/abc]"
    echo strPattern2 = "(http:\/\/|https:\/\/)(.+)[^\s\[\]<]\/([^\s\[\]<].+)"
    echo strReplace2 = ""
    echo Set oRegExp = New RegExp
    echo oRegExp.Global = True 
    echo oRegExp.IgnoreCase = True 
    echo oRegExp.Pattern = strPattern1
    echo strResult1 = oRegExp.Replace(Data,strReplace1^)
    echo oRegExp.Pattern = strPattern2
    echo strResult2 = oRegExp.Replace(strResult1,strReplace2^)
    echo Search_Replace = strResult2
    echo End Function
)>"%VBSFILE%"
Exit /b 
::----------------------------------------------------------------------------
:Search_Replace <InputFile> <OutPutFile>
Cscript /nologo "%VBSFILE%" < "%~1" > "%~2"
Exit /B
::----------------------------------------------------------------------------

使用这些简单的 powershell 命令:

(Get-Content C:\content1.txt) -creplace '(?s)^.*/', '' | Set-Content C:\content1.txt
(Get-Content C:\content2.txt) -creplace '(?s)^.*/', '' | Set-Content C:\content2.txt
(Get-Content C:\content3.txt) -creplace '(?s)^.*/', '' | Set-Content C:\content3.txt

如果您有任何问题,请告诉我。