0

テキスト フィルターとして複数の検索および置換機能を実行するように、以下のコードを石造りにすることができました。このコードは、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
4

2 に答える 2

1

正規表現での置換は、「通常の」置換とそれほど違いはありません。正規表現を準備する追加の手順があります。

Set re = New RegExp
re.Pattern    = "1a"
re.IgnoreCase = True
re.Global     = True

実際の置換は次のように行われます(その情報は正規表現に含まれているため、検索テキストはありません)。

outPut1 = re.Replace(sLine, "foo")

いくつかの正規表現が必要になる場合があるため、それらの作成を関数にカプセル化するのがおそらく最善です。

Function CreateRegExp(str)
  Set re = New RegExp
  re.Pattern    = str
  re.IgnoreCase = True
  re.Global     = True

  Set CreateRegExp = re
End Function

その場合、コードは次のようになります。

Set re1 = CreateRegExp("1a")
Set re2 = CreateRegExp("2b")
Set re3 = CreateRegExp("3c")
Set re4 = CreateRegExp("4d")

'...

Function SeaRep()
  Dim nLine, sLine, nCount, outPut1, outPut2, outPut3
  nCount = 0
  Do Until oInStream.AtEndOfStream
    nLine = oInStream.Line
    sLine = oInStream.ReadLine

    outPut1 = re1.Replace(sLine, "foo")
    outPut2 = re2.Replace(outPut1, "foobar")
    outPut3 = re3.Replace(outPut2, "foosod")
    oOutStream.WriteLine (re4.Replace(outPut3, "fooyard"))

    nCount = nCount + 1
  Loop
  AddLineNum = nCount
End Function
于 2013-03-15T23:54:18.133 に答える