返回列表 发帖
本帖最后由 WHY 于 2020-12-12 10:06 编辑
Rem On Error Resume Next
Dim srcFile, dstFile, fso, objFile
srcFile = "a.txt"    '输入文件
dstFile = "b.txt"    '输出文件
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFile = fso.OpenTextFile(srcFile, 1)
Dim reg, strLine, n, out, match
Set reg = New RegExp
reg.Pattern = "^\s*[((]\s*(?=([^))\s]+))\1\s+(?=([^))]+))\2[))]\s*$"
n = 0
ReDim out(n)
while Not objFile.AtEndOfStream
    ReDim Preserve out(n)
    strLine = objFile.ReadLine
    If reg.Test(strLine) Then
        Set match = reg.Execute(strLine)(0)
        out(n) = match.SubMatches(0) + ";" + Replace(match.SubMatches(1), "-", ".")
    Else
        out(n) = strLine
    End If
    n = n + 1
wend
objFile.Close
fso.OpenTextFile(dstFile, 2, True).Write Join(out, vbCrLf)
MsgBox "Done"COPY
1

评分人数

TOP

回复 6# loveforjg


    已改。

TOP

回复 9# loveforjg


    请给一个真实的数据样本文件便于测试,如果文件过大,可以放到网盘。
另外,请确认是以256“字节”还是256“字符”为单位分割行?文本中是否包含宽字符(中文字符)?

TOP

试试
Rem On Error Resume Next
Dim srcFile, dstFile, fso, objFile
srcFile = "in.txt"         '输入文件
dstFile = "out.txt"        '输出文件
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFile = fso.OpenTextFile(srcFile, 1)
Dim reg1, strLine, n, out(), match, reg2, s
Set reg1 = New RegExp
Set reg2 = New RegExp
reg1.Pattern = "^\s*\(\s*(?=([^)\s]+))\1\s+(?=([^)]+))\2\)\s*$"
reg2.Pattern = "(?:\S+\s+){9}(?=(\S+))\1(?!\s*$)"
reg2.Global = True
n = 0
while Not objFile.AtEndOfStream
    ReDim Preserve out(n)
    strLine = objFile.ReadLine
    If reg1.Test(strLine) Then
        Set match = reg1.Execute(strLine)(0)
        s = Replace(match.SubMatches(1), "-", ".")
        s = reg2.Replace(s, "$&," + vbCrLf + "    ")
        out(n) = match.SubMatches(0) + ";" + s
    Else
        out(n) = strLine
    End If
    n = n + 1
wend
objFile.Close
fso.OpenTextFile(dstFile, 2, True).Write Join(out, vbCrLf)
MsgBox "Done"COPY
1

评分人数

    • loveforjg: 可以了 太牛了,您这个正则式太巧妙了,求解 ...技术 + 1

TOP

返回列表