VBScript 正则表达式:替换内容 [英] VBScript RegEx : Replace Content

查看:31
本文介绍了VBScript 正则表达式:替换内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从原始源更新 Unbound_DNS 配置文件,但无法获得所需的结果.

I want to update the Unbound_DNS configuration file from a raw source but I can not get the desired result.

我想格式化每个条目(每行):

I would like to format each entry (each line):

address=/abc.com/0.0.0.0

local-zone: "abc.com" redirect
local-data: "abc.com 86400 IN A 0.0.0.0"

这是我所做的(感谢 hackoofr):

Here is what I did (thanks to hackoofr):

Option Explicit
Dim URL,Save2File,ws
If Not WScript.Arguments.Named.Exists("elevate") Then
    CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
    WScript.Quit
End If
URL = "https://raw.githubusercontent.com/notracking/hosts-blocklists/master/domains.txt"
Set ws = CreateObject("wscript.Shell")
Save2File = ws.ExpandEnvironmentStrings("%Windir%\Temp\test")
Call Download(URL,Save2File)
'**********************************************************************************************
Sub Download(URL,Save2File)
    Dim File,Line,BS,ws,RegExp
    On Error Resume Next
    Set File = CreateObject("MSXML2.XMLHTTP")
    File.Open "GET",URL, False
    File.Send
    If err.number <> 0 then
        Line  = Line &  vbcrlf & "Error Getting File"
        Line  = Line &  vbcrlf & "Error " & err.number & "(0x" & hex(err.number) & ") " &  vbcrlf &_
        err.description
        Line  = Line &  vbcrlf & "Source " & err.source 
        MsgBox Line,vbCritical,"Error getting file"
        Err.clear
        wscript.quit
    End If
    If File.Status = 200 Then   
        '**********************************************************************************************
        ' Replace content for use with the file service.conf of soft Unbound_DNS
        '
        ' address=/abc.com/0.0.0.0      to      local-zone: "abc.com" redirect
        '                                       local-data: "abc.com 3600 IN A 0.0.0.0"
        '**********************************************************************************************
        Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.IgnoreCase = True
        RegExp.Global = True
        RegExp.Pattern = "address=/(.*)/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})"       
        File.ResponseBody = RegExp.Replace(File.ResponseBody, "local-zone: \""$1\"" redirect $1" & ret & ">local-data: \""$1 3600 IN A $2\""")  
        Set RegExp = Nothing

        '**********************************************************************************************
        ' Write content 
        '**********************************************************************************************
        Set BS = CreateObject("ADODB.Stream")
        Set ws = CreateObject("wscript.Shell")
        BS.type = 1
        BS.open
        BS.Write File.ResponseBody
        BS.SaveToFile Save2File, 2

        '**********************************************************************************************
        ' Clean cache DNS
        '**********************************************************************************************
        wshShell.run("cmd /c psexec \\ -s ipconfig /flushdns >> & hostName,TRUE")
    ElseIf File.Status = 404 Then
        MsgBox "UpdateHostname.vbs : File Not Found : " & File.Status,vbCritical,"UpdateHostname.vbs : Error File Not Found"
    Else
        MsgBox "UpdateHostname.vbs : Unknown Error : " & File.Status,vbCritical,"UpdateHostname.vbs : Error getting file"
    End If
End Sub
'**********************************************************************************************

预先感谢您的帮助.

编辑 1:内容不变.File.ResponseBody 正确返回内容,但没有被正则表达式修改!

Edit 1: The content does not change. File.ResponseBody returns the content correctly, but no modification by the regexp!

推荐答案

这里是基于@Gurman 的响应和@Ansgar Wiechers 的评论,对运行良好的代码进行了更新.感谢您的帮助

Here is the update of the code that works very well based on the response of @Gurman and the comment of @Ansgar Wiechers. Thank you for your help

Option Explicit
Dim File, objReg, strTest, RegExp, objMatches, objMatch, saveToFile, fso, outFile, strReplace, objShell, i
Set objShell = CreateObject("wscript.shell")
saveToFile = objShell.ExpandEnvironmentStrings("%windir%\Temp\test.txt")

Set File = CreateObject("MSXML2.XMLHTTP")
File.Open "GET","https://raw.githubusercontent.com/notracking/hosts-blocklists/master/domains.txt", False
File.send

If File.Status = 200 Then   

    '**********************************************************************************************
    ' Replace content for use with the file service.conf of soft Unbound_DNS
    '
    ' address=/abc.com/0.0.0.0      to      local-zone: "abc.com" redirect
    '                                       local-data: "abc.com 86400 IN A 0.0.0.0"
    '**********************************************************************************************
    strTest = File.responseText

    Set RegExp = CreateObject("VBScript.RegExp")
    RegExp.IgnoreCase = True
    RegExp.Global = True
    RegExp.Pattern = "address=/(.*?)/(\d{1,3}(?:\.\d{1,3}){3})" 
    strReplace = "local-zone: ""$1"" redirect" & vbCrLf & "local-data: ""$1 86400 IN A $2"""

    strTest = RegExp.Replace(strTest, strReplace)  
    Set RegExp = Nothing

    '**********************************************************************************************
    ' Write content 
    '**********************************************************************************************
    Set fso = CreateObject("scripting.filesystemobject")
    Set outFile = fso.OpenTextFile(saveToFile,2,True)
    outFile.Write strTest
    outFile.Close
End If

这篇关于VBScript 正则表达式:替换内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆