1

我一直在尝试编写一个程序,该程序打开一个文本文件并C_PIN.

我觉得我已经差不多完成了,但是我遇到了运行时错误 5 和 438。

正在阅读的示例文本:


COMP C48 66250110810 cap sc_cap.0603_H9 43.3959 74.3331 1 0
C_PROP (PB_FREE,"Y") (VALUE,"10nF") (TOLER,"10%") (PART_NAME,"06035C103K4T2A")
C_PIN C48-1 43.3959 075.095 sp.0603_H9.1 /N$1567
C_PIN C48-2 43.3959 73.5711 1 1 0 sp.0603_H9.2 GN


之后需要一个额外的空间1 1 0

这就是我认为我的代码中的问题所在:

x = " "
Do While Not EOF(infilenum%)
 Line Input #infilenum%, a$
  If Left$(a$, 5) = "C_PIN" Then
   For Each x In InStr
   'If InStr(strText, " ") Then
     w = w + 1
    'w = strText.Split
     For w = 5 To w = 7
      My.Computer.FileSystem.WriteAllText (infilename$)
      strText = My.Computer.FileSystem.ReadAllText(infilename$).Replace(w, x + w)
      vb.newline

任何帮助深表感谢!

4

2 回答 2

3
For Each x In InStr

无论如何都无效!!!

您可以使用拆分和加入:

If Left(a, 5) = "C_PIN" Then
    va = Split(a, " ")
    va(4) = va(4) & " " 'Add extra space
    va(5) = va(4) & " "
    va(6) = va(4) & " "
    a =  Join(va, " ") 'Join with extra spaces added
End If

现在你可以写字符串了。

于 2013-07-04T10:04:36.387 回答
1

我玩了一个使用正则进行单次替换的替代版本

虽然它看起来有点复杂,但除了单次替换之外,它的优点是它只会改变那些

  • 从...开始C_PIN
  • 有(至少)另外 6 个字

此示例获取您的初始文件,并使用填充间距保存第二个版本。

更新了额外的要求,使用两个单独的正则表达式替换

Sub ReDo()
Dim objFso As Object
Dim objFil As Object
Dim objFil2 As Object
Dim objRegex As Object
Dim strFile As String
Dim strAll As String

strFil = "c:\temp\REnglish.txt"
strFil2 = "c:\temp\REnglish2.txt"

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "(\nC_PIN\s)((\b[^\s]+\b\s){3})(\b[^\s]+\b\s)(\b[^\s]+\b\s)(\b\d\b\s)"
.Global = True
Set objFil = objFso.OpenTextFile(strFil)
strAll = objFil.ReadAll
Set objFil2 = objFso.createtextfile(strFil2)
strAll = .Replace(strAll, "$1$2$3$4 $5 $6 ")
.Pattern = "(\nC_PIN\s)((\b[^\s]+\b\s){3})(\b[^\s]+\b\s)(\b[^\s]+\b\s)(\b\d{2,}\b\s)"
objFil2.write .Replace(strAll, "$1$2$3$4 $5 $6")
End With
objFil.Close
objFil2.Close
End Sub
于 2013-07-04T12:57:40.910 回答