回复 7# loveforjg - Option Explicit
- ' On Error Resume Next
- Const conRead = 1
- Const conWrite = 2
- Const conAppend = 8
- Dim fso,sr,sw,str,src,dst,Match,arrLines,strtmp
- src = "a.txt"
- dst = "b.txt"
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set sr = fso.OpenTextFile(src,conRead,False,vbUseDefault)
- Do Until sr.AtEndOfStream
- str = sr.ReadAll
- Loop
- sr.Close
- Set sw = fso.OpenTextFile(dst,conWrite,True,vbUseDefault)
- With New RegExp
- .Global = True
- .Ignorecase = True
- .Multiline = False
- .Pattern = "\[[^\[\]]*\]"
- For Each Match In .Execute(str)
- arrLines = Split(Match,vbNewLine,-1,vbBinaryCompare)
- If UBound(arrLines) > 2 Then ' 含至少2个元素才swap前2个
- strtmp = arrLines(1)
- arrLines(1) = arrLines(2)
- arrLines(2) = strtmp
- End If
- sw.WriteLine Join(arrLines," ")
- Next
- End With
- sw.Close
- Set sr = Nothing
- Set sw = Nothing
- Set fso = Nothing
复制代码
|