7,763
社区成员
发帖
与我相关
我的任务
分享
Private Sub Command1_Click()
'
Const ForReading = 1
Const ForWriting = 2
Dim strTxt As String '用于查找替换的文本
Dim expTxt As String '查找串(可用正则表达式)
Dim repTxt As String '替换串
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\temp.txt", ForReading)
strTxt = objFile.ReadAll
objFile.Close
strTxt = StrReplace(strTxt, "\\l\(([^\)]+)\)", "$1")
strTxt = StrReplace(strTxt, "\\d\\fo\d+\\li\(([^\)]*)\)", "[CD#3]$1")
strTxt = StrReplace(strTxt, "\\d\\fo\d+\(([^\)]*)\)", "[KG2]$1")
'……
Set objFile = objFSO.OpenTextFile("c:\temp.txt", ForWriting)
objFile.WriteLine strTxt
objFile.Close
Set objFSO = Nothing
Set objFile = Nothing
MsgBox "OK"
End Sub
Function StrReplace(strTxt As String, expTxt As String, repTxt As String) As String
'正则替换的函数
Dim RegEx As New VBScript_RegExp_55.RegExp '定义一个新的正则表达式对象
RegEx.Global = True '全程查找
RegEx.IgnoreCase = False ' 区分大小写
RegEx.Pattern = expTxt
StrReplace = RegEx.Replace(strTxt, repTxt)
End Function