VBScript正则表达式:替换内容

VBScript正则表达式:替换内容,vbscript,Vbscript,我想从原始源更新未绑定的\u DNS配置文件,但无法获得所需的结果 我想格式化每个条目(每行): 到 以下是我所做的(感谢hackoofr): 选项显式 Dim URL、Save2File、ws 如果不存在WScript.Arguments.Named.Exists(“提升”),则 CreateObject(“Shell.Application”).ShellExecute WScript.FullName_ ,“”,&WScript.ScriptFullName&“/elevate”,“”,,

我想从原始源更新未绑定的\u DNS配置文件,但无法获得所需的结果

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

以下是我所做的(感谢hackoofr):

选项显式
Dim URL、Save2File、ws
如果不存在WScript.Arguments.Named.Exists(“提升”),则
CreateObject(“Shell.Application”).ShellExecute WScript.FullName_
,“”,&WScript.ScriptFullName&“/elevate”,“”,,“runas”,1
WScript.Quit
如果结束
URL=”https://raw.githubusercontent.com/notracking/hosts-blocklists/master/domains.txt"
设置ws=CreateObject(“wscript.Shell”)
Save2File=ws.ExpandEnvironmentStrings(“%Windir%\Temp\test”)
呼叫下载(URL,Save2File)
'**********************************************************************************************
子下载(URL,Save2File)
Dim文件、行、BS、ws、RegExp
出错时继续下一步
Set File=CreateObject(“MSXML2.XMLHTTP”)
文件。打开“获取”,URL,False
文件。发送
如果错误号为0,则
Line=Line&vbcrlf&“获取文件时出错”
Line=Line&vbcrlf&“Error”&errr.number&“(0x”&hex(err.number)&“”&vbcrlf&_
错误描述
Line=Line&vbcrlf&“Source”&err.Source
MsgBox行,vbCritical,“获取文件时出错”
清楚
wscript.quit
如果结束
如果File.Status=200,则
'**********************************************************************************************
'将内容替换为软未绑定\u DNS的文件service.conf以供使用
'
'address=/abc.com/0.0.0到本地区域:“abc.com”重定向
'本地数据:“0.0.0.0中的abc.com 3600”
'**********************************************************************************************
设置RegExp=CreateObject(“VBScript.RegExp”)
RegExp.IgnoreCase=True
RegExp.Global=True
RegExp.Pattern=“地址=/(.*)/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})”
File.ResponseBody=RegExp.Replace(File.ResponseBody,“本地区域:\”“$1\”“重定向$1”&ret&>“>本地数据:\”“$1 3600在$2\”)
Set RegExp=Nothing
'**********************************************************************************************
"写内容",
'**********************************************************************************************
设置BS=CreateObject(“ADODB.Stream”)
设置ws=CreateObject(“wscript.Shell”)
BS.type=1
英国公开赛
BS.Write File.ResponseBody
BS.SaveToFile Save2File,2
'**********************************************************************************************
'清除缓存DNS
'**********************************************************************************************
运行(“cmd/c psexec\\-s ipconfig/flushdns>>&hostName,TRUE”)
ElseIf File.Status=404然后
MsgBox“UpdateHostname.vbs:找不到文件:”&File.Status,vbCritical,“UpdateHostname.vbs:找不到错误文件”
其他的
MsgBox“UpdateHostname.vbs:未知错误:”&File.Status,vbCritical,“UpdateHostname.vbs:获取文件时出错”
如果结束
端接头
'**********************************************************************************************
提前感谢您的帮助

编辑1:
内容不变。File.ResponseBody正确返回内容,但不受regexp的修改

更换以下代码:

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
为此:

Dim objReg, strTest, objMatches, objMatch
Set objReg = New RegExp
strTest = File.ResponseBody                    'address=/abc.com/0.0.0.0
objReg.Global = True
objReg.Pattern = "address=/(.*?)/(\d{1,3}(?:\.\d{1,3}){3})"    'abc.com gets stored in Group 1 and the IP address gets stored in Group 2
Set objMatches = objReg.Execute(strTest)
For Each objMatch In objMatches
    strTest = "local zone: """ & objMatch.Submatches.Item(0) & """ redirect" & vbCrLf &_
              "local data: """ & objMatch.Submatches.Item(0) & " 86400 in A " & objMatch.Submatches.Item(1)&""""
Next
File.ResponseBody = strTest
set objReg = Nothing
(在演示中,
/
\
转义)

正则表达式解释:

地址=/(.*)/(\d{1,3}(?:\.\d{1,3}){3})

  • 地址=/
    -按字面意思匹配
    地址=/
  • (.*)
    -匹配任何字符的0+次出现次数(换行除外),尽可能少。括号用于将此匹配捕获为组1
  • /
    -按字面意思匹配
    /
  • (\d{1,3}(?:\.\d{1,3}){3})
    -匹配模式的字符串
    12.222.212.33
    ,并将其捕获到组2中
更新:

这是我的最终解决方案。根据我对代码的理解,首先从服务器获取响应主体,修改并将更新后的响应存储在temp文件夹中名为
test
的文件中。下面是我为做同样的事情而编写的代码。我已经在我的系统上对其进行了测试,存储在
C:\Windows\Temp\test.txt
文件中的最终输出看起来正确,如所附屏幕截图所示。现在,这可能不是你想要的,但你可以从中得到一个想法。将此代码存储在新的vbs文件中,并按原样直接运行

注意:由于来自服务器的响应文本很长,所以执行起来需要一点时间。如果您只想查看它是否工作,请取消对for循环中的代码的注释。您将能够看到,您正在为前几个URL获得所需的结果

输出:


以下是根据@Gurman的回复和@Ansgar Wiechers的评论对代码进行的更新,效果非常好。谢谢你的帮助

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

感谢您更正了regexp,但是对内容的修改仍然不起作用。我刚测试过!可以到底是什么问题?是否有错误?没有错误,文件保存为原始文件。(无修改)@StackOverflowCom放置一行,如
msgbox strTest
,然后查看在该行前面显示的内容
File.ResponseBody=strTest
msgbox strTest=未显示任何内容(编辑:就在File.ResponseBody=strTest行之前)
File.ResponseBody
是不可更改的。如果要修改某个变量,请将该属性的值指定给该变量。请不要更改
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
Dim objReg, strTest, objMatches, objMatch
Set objReg = New RegExp
strTest = File.ResponseBody                    'address=/abc.com/0.0.0.0
objReg.Global = True
objReg.Pattern = "address=/(.*?)/(\d{1,3}(?:\.\d{1,3}){3})"    'abc.com gets stored in Group 1 and the IP address gets stored in Group 2
Set objMatches = objReg.Execute(strTest)
For Each objMatch In objMatches
    strTest = "local zone: """ & objMatch.Submatches.Item(0) & """ redirect" & vbCrLf &_
              "local data: """ & objMatch.Submatches.Item(0) & " 86400 in A " & objMatch.Submatches.Item(1)&""""
Next
File.ResponseBody = strTest
set objReg = Nothing
Option Explicit
Dim File, objReg, strTest, 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   
    Set objReg = New RegExp
    strTest = File.responseText  'address=/abc.com/0.0.0.0
    objReg.Global = True
    objReg.Pattern = "address=/(.*?)/(\d{1,3}(?:\.\d{1,3}){3})"    'abc.com gets stored in Group 1 and the IP address gets stored in Group 2
    Set objMatches = objReg.Execute(strTest)
    For Each objMatch In objMatches
        strReplace = "local zone: """ & objMatch.Submatches.Item(0) & """ redirect" & vbCrLf &_
                     "local data: """ & objMatch.Submatches.Item(0) & " 86400 in A " & objMatch.Submatches.Item(1)&"""" & vbCrLf

        strTest = Replace(strTest,objMatch.Value,strReplace)

        'Uncomment the following code to see the result for the 1st 5 URLs, if the whole thing is taking too long to get executed
        'i=i+1
        'If(i>5) Then
        '   Exit for
        'End If
    Next
    set objReg = Nothing

    '**********************************************************************************************
    ' Write content 
    '**********************************************************************************************
    Set fso = CreateObject("scripting.filesystemobject")
    Set outFile = fso.OpenTextFile(saveToFile,2,True)
    outFile.Write strTest
    outFile.Close
End If
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