我能够拼凑下面的代码,以便它执行多个搜索和替换功能作为文本过滤器。该代码适用于 EditPlus 文本编辑器程序。
我想执行相同的想法,除了正则表达式搜索和替换。
在文本过滤器中为多个正则表达式搜索和替换编码的正确方法是什么?
enter code here
Option Explicit
Dim oWS, oFS
Set oWS = WScript.CreateObject("WScript.Shell")
Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
'----------
' Script Setup
'----------
Dim oInStream, oOutStream, sInFile, sOutFile, nTotalLines, sArg
'----------
' Process File(s)
'----------
If Wscript.Arguments.Count = 0 Then
If InStr(LCase(WScript.FullName), "cscript.exe") <> 0 Then
sInFile = "StdIn"
sOutFile = "StdOut"
Set oInStream = WScript.StdIn
Set oOutStream = WScript.StdOut
Call ProcessFile()
Else
Call HelpMsg()
End If
Else
For sArg = 0 To Wscript.Arguments.Count -1
sInFile = Wscript.Arguments(sArg)
If IsFile(sInFile) Then
sOutFile = Left(sInFile, InStrRev(sInFile, ".") - 1) & ".lst"
Set oOutStream = oFS.OpenTextFile(sOutFile, 2, True, 0)
Set oInStream = oFS.OpenTextFile(sInFile, 1)
Call ProcessFile()
oWS.Run "NotePad.exe " & sOutFile
Else
Wscript.Echo "File Not Found: " & sInFile, , "Error"
End If
Next
End If
Call CleanUp(0)
'---------------------
' Subroutines
' ********************
'---------------------
Sub CleanUp(exitCode)
Set oInStream = Nothing
Set oOutStream = Nothing
Set oWS = Nothing
Set oWS = Nothing
WScript.Quit(exitCode)
End Sub
'---------------------
Sub ProcessFile()
'oOutStream.WriteLine "<DIV class='mesa'>"
nTotalLines = SeaRep()
'oOutStream.WriteLine "</DIV>"
End Sub
'---------------------
'---------------------
' Functions
' ********************
'---------------------
Function SeaRep()
Dim nLine, sLine, nCount, outPut1, outPut2, outPut3
nCount = 0
Do Until oInStream.AtEndOfStream
nLine = oInStream.Line
sLine = oInStream.ReadLine
outPut1 = Replace(sLine,"1a","foo")
outPut2 = Replace(outPut1,"2b","foobar")
outPut3 = Replace(outPut2,"3c","foosod")
oOutStream.WriteLine (Replace(outPut3,"4d","fooyard"))
nCount = nCount + 1
Loop
AddLineNum = nCount
End Function
'---------------------
'---------------------
Function IsFile (fName)
If oFS.FileExists(fName) Then IsFile = True Else IsFile = False
End Function
' ********************
' End code