使用批处理替换为正则表达式(Windows) [英] Replace with regular expression using Batch (Windows)
问题描述
是否可以使用正则表达式进行搜索并替换特定的文本文件?我有一个位于C:\ content.txt
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
文本内有Google链接
inside text there is google link
我需要一个可以将<https://www.google.com/>
替换为[abc]https://www.google.com/[/abc]
i need a batch (.bat) script which can replace <https://www.google.com/>
to [abc]https://www.google.com/[/abc]
这是notepad ++ regex替换
here is the notepad++ regex replace
Find What: .+(.google\.com).+
Replace with: [abc]$0[/abc]
Find What: .+(.google\.com).+
Replace with: [abc]$0[/abc]
推荐答案
您应在vbscript中将Regex与如下代码的批处理文件一起使用: 您可以在此处看到 Regex演示
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
::----------------------------------------------------------------------------------
例如,如果文件C:\Content.txt
的内容看起来像这样:
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 />"
用Regex替换后应该是这样的:
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]
这篇关于使用批处理替换为正则表达式(Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!