【问题标题】:Regular Expressions in VBScriptVBScript 中的正则表达式
【发布时间】:2011-10-31 15:38:58
【问题描述】:

如何使用替换文本保存所有文本。
目前只保存替换的文本

我只想更改 txt 中的 9752951c-0392-71e1-01a3-ac10016b0000 1.txt

  text ...
        <URL>http://bs.com/opr-console/rest/9.10/event_list/9752951c-0392-71e1-01a3- ac10016b0000</URL>        
           <method>PUT</method>
            <auth-methods>DIGEST</auth-methods>
            <auth-preemptive>true</auth-preemptive>
            <auth-username>admin</auth-username> 
            <auth-password>rO0ABXQABWFkbWlu</auth-
          .....bla-bla-la..

vbs 脚本:

Dim objExec, objShell, objWshScriptExec, objStdOut, objArgs, ReplaceWith  
Const ForReading = 1  
Const ForWriting = 2  
Set objArgs = WScript.Arguments  
strID = Trim(objArgs(0))  
Set objRegEx = CreateObject("VBScript.RegExp")  
objRegEx.Pattern = "^<URL>http://bsmgw.bms.consulting.com/opr-console/rest/9.10/event_list/"

Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set objFile = objFSO.OpenTextFile("C:\test\1\1.txt", ForReading)

Do Until objFile.AtEndOfStream
    strSearchString = objFile.ReadLine
    Set colMatches = objRegEx.Execute(strSearchString)  
    If colMatches.Count > 0 Then
        For Each strMatch In colMatches   
            Wscript.Echo strSearchString

            st = Mid(strSearchString, 71, 36)
            WScript.Echo st 
            strNewFileName = Replace(strSearchString, st, strID)
        Wscript.Echo strNewFileName
        objFile.Write
        Next
    End If 
Loop 
objFile.Close  
objFile.Close 
objFile.Close

【问题讨论】:

    标签: vbscript replace pattern-matching


    【解决方案1】:

    就我个人而言,我会改为使用 XML 解析器来更改您的文件:

    ' load the document into an object
    Dim xmldoc: set xmldoc = CreateObject("MSXML2.DomDocument")
    xmldoc.async = false
    xmldoc.setProperty "SelectionLanguage", "XPath"
    xmldoc.load "C:\test\1\1.txt"
    
    ' get the URL node (look up XPath - i'm assuming that there is a single node called URL)
    dim urlnode: set urlnode = xmldoc.selectSingleNode("//URL")
    ' replace the innerText of the URL node with your replacement text
    urlnode.text = strID
    
    'save your document
    xmldoc.Save "C:\test\1\2.txt"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多