VBScript 中的正则表达式 [英] Regular Expressions in VBScript

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

问题描述

如何用替换文本保存所有文本.
现在只保存替换文本

How to save all text with replaced text.
At now save only replaced text

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

I want to change only 9752951c-0392-71e1-01a3- ac10016b0000 in txt 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

推荐答案

就我个人而言,我会改用 XML 解析器对您的文件进行更改:

Personally, I would make changes to your file using an XML parser instead:

' 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"

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

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