[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 WHY 于 2020-12-12 10:06 编辑
  1. Rem On Error Resume Next
  2. Dim srcFile, dstFile, fso, objFile
  3. srcFile = "a.txt"    '输入文件
  4. dstFile = "b.txt"    '输出文件
  5. Set fso = CreateObject("Scripting.FileSystemObject")
  6. Set objFile = fso.OpenTextFile(srcFile, 1)
  7. Dim reg, strLine, n, out, match
  8. Set reg = New RegExp
  9. reg.Pattern = "^\s*[((]\s*(?=([^))\s]+))\1\s+(?=([^))]+))\2[))]\s*$"
  10. n = 0
  11. ReDim out(n)
  12. while Not objFile.AtEndOfStream
  13.     ReDim Preserve out(n)
  14.     strLine = objFile.ReadLine
  15.     If reg.Test(strLine) Then
  16.         Set match = reg.Execute(strLine)(0)
  17.         out(n) = match.SubMatches(0) + ";" + Replace(match.SubMatches(1), "-", ".")
  18.     Else
  19.         out(n) = strLine
  20.     End If
  21.     n = n + 1
  22. wend
  23. objFile.Close
  24. fso.OpenTextFile(dstFile, 2, True).Write Join(out, vbCrLf)
  25. MsgBox "Done"
复制代码
1

评分人数

TOP

回复 6# loveforjg


    已改。

TOP

回复 9# loveforjg


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

TOP

试试
  1. Rem On Error Resume Next
  2. Dim srcFile, dstFile, fso, objFile
  3. srcFile = "in.txt"         '输入文件
  4. dstFile = "out.txt"        '输出文件
  5. Set fso = CreateObject("Scripting.FileSystemObject")
  6. Set objFile = fso.OpenTextFile(srcFile, 1)
  7. Dim reg1, strLine, n, out(), match, reg2, s
  8. Set reg1 = New RegExp
  9. Set reg2 = New RegExp
  10. reg1.Pattern = "^\s*\(\s*(?=([^)\s]+))\1\s+(?=([^)]+))\2\)\s*$"
  11. reg2.Pattern = "(?:\S+\s+){9}(?=(\S+))\1(?!\s*$)"
  12. reg2.Global = True
  13. n = 0
  14. while Not objFile.AtEndOfStream
  15.     ReDim Preserve out(n)
  16.     strLine = objFile.ReadLine
  17.     If reg1.Test(strLine) Then
  18.         Set match = reg1.Execute(strLine)(0)
  19.         s = Replace(match.SubMatches(1), "-", ".")
  20.         s = reg2.Replace(s, "$&," + vbCrLf + "    ")
  21.         out(n) = match.SubMatches(0) + ";" + s
  22.     Else
  23.         out(n) = strLine
  24.     End If
  25.     n = n + 1
  26. wend
  27. objFile.Close
  28. fso.OpenTextFile(dstFile, 2, True).Write Join(out, vbCrLf)
  29. MsgBox "Done"
复制代码
1

评分人数

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

TOP

返回列表