Replace with regular expression using Batch (Windows)

允我心安 提交于 2020-02-29 06:24:06

问题


is it possible to use regular expression for search and replace for a specific text file? I have a text file located C:\content.txt

inside text there is google link

https://www.google.com/

i need a batch (.bat) script which can replace <https://www.google.com/> to [abc]https://www.google.com/[/abc]

here is the notepad++ regex replace

Find What: .+(.google\.com).+ Replace with: [abc]$0[/abc]


回答1:


You should use Regex in vbscript with a batch file like this code : You can see Regex Demo here

@echo off
Mode 85,20 & color 0A
Title Replace String using Regex with vbscript
Set "InputFile=C:\Test\content.txt"
Set "OutPutFile=%~dp0NewContent.txt"
echo(
:: To show Results on screen of console
Call :Search_Replace "%InputFile%" CON
:: To write Result in new file
Call :Search_Replace "%InputFile%" "%OutPutFile%"
echo(
echo Press any key to show the results in a new file :
echo "%OutPutFile%"
pause>nul
Start "" "%OutPutFile%" 
echo(
echo Did you want to update and replace all in your original file "%InputFile%" ?
Pause>nul
Move /Y "%OutPutFile%" "%InputFile%">nul
Start "" "%InputFile%" & Exit
::-----------------------------------------------------------------------------------
:Search_Replace <InputFile> <OutPutFile>
(
    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 strPattern = "(\x22<|<)([\s\S]*?)(/>\x22|>| />\x22| />| \x22>)"
    echo strReplace = "[abc]$2[/abc]"
    echo Set oRegExp = New RegExp
    echo oRegExp.Global = True 
    echo oRegExp.IgnoreCase = True 
    echo oRegExp.Pattern = strPattern
    echo strResult = oRegExp.Replace(Data,strReplace^)
    echo Search_Replace = strResult
    echo End Function
)>"%tmp%\%~n0.vbs"
cscript //nologo "%tmp%\%~n0.vbs" < "%~1" > "%~2"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------

For example if the content of your file C:\Content.txt looks something like that :

"<https://www.google.com />"
<https://www.google.com>
"<https://www.developpez.net/>"
<https://www.developpez.net />
"<https://www.bing.com/>"
"<https://www.bing.com />"
"<https://stackoverflow.com/>
<https://stackoverflow.com/>"
<https://stackoverflow.com />
"<https://stackoverflow.com />"

After Replace with Regex should be like that :

[abc]https://www.google.com[/abc]
[abc]https://www.google.com[/abc]
[abc]https://www.developpez.net[/abc]
[abc]https://www.developpez.net[/abc]
[abc]https://www.bing.com[/abc]
[abc]https://www.bing.com[/abc]
[abc]https://stackoverflow.com/[/abc]
[abc]https://stackoverflow.com[/abc]
[abc]https://stackoverflow.com[/abc]
[abc]https://stackoverflow.com[/abc]



回答2:


You can use pseudo-regex with command FINDSTR.

In the below example, for each line in file "content.txt" we check if it contains regex and replace the link. Then you can redirect output to a new file.

@ECHO OFF
SET "REGEX=.*.google\.com..*"
FOR /F %%I IN (content.txt) DO CALL :REPLACE "%%I"
EXIT /B 0

:REPLACE
SET "LINK=%~1"
ECHO "%LINK%" | FINDSTR /R "%REGEX%" > nul && SET "LINK=[abc]%LINK%[/abc]"
ECHO "%LINK%"
GOTO :EOF

Output :

"[abc]<https://www.google.com/>[/abc]"
"<https://www.bing.com/>"

Redirect output and replace file :

test.bat > output.txt
move output.txt content.txt


来源:https://stackoverflow.com/questions/58336784/replace-with-regular-expression-using-batch-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!