读取文本文件并在结果字符串上使用正则表达式应用

发布于 2024-12-01 08:27:49 字数 1022 浏览 0 评论 0原文

我想读取一次文本文件的内容,并对结果字符串应用正则表达式。我无法弄清楚如何读取文本文件一次。

我一直在尝试一种替代方案,但它无法正常工作

Set oFS = oFSO.OpenTextFile(sFilename)
num = 15
mynum = 1
Do Until oFS.AtEndOfStream
    'Cells(num, mynum).Value = oFS.ReadLine
     MyString = oFS.ReadLine
     If Not oFS.AtEndOfStream Then
            oFS.SkipLine

     strStrings = Split(MyString, " ")
     For intInd = LBound(strStrings) To UBound(strStrings)

         Cells(num, mynum).Value = strStrings(intInd)

         mynum = mynum + 1
         If mynum Mod 4 = 0 Then
             num = num + 1
             mynum = 1
         End If
    Next

    End If
Loop
oFS.Close
Set oFSO = Nothing

,我注意到问题来自两个来源......其中一个是,一旦要分割的字符串长度可变,我的函数就会严重失败。我该如何修补这个问题?其次,我的文件内容包含一些非字符,我怀疑这些非字符也可能导致问题。 Myfile 在第三行之后有不同的空间。我能够得到前两行

DNI ROLL TEST ON 896_271209

Directional Data Test Started

=> KS 32223.63 火焰

=> SIP -7.25 度

=>右 90.57 度

=>方位角 105.46 度

=>左 73.92 度

=>偏移 -1.15 度

I would to read the content of text file once and apply regex on the resultant string. I have not been able to figure out how to read text file once.

There is an alternative I have been experimenting with but it not working correctly

Set oFS = oFSO.OpenTextFile(sFilename)
num = 15
mynum = 1
Do Until oFS.AtEndOfStream
    'Cells(num, mynum).Value = oFS.ReadLine
     MyString = oFS.ReadLine
     If Not oFS.AtEndOfStream Then
            oFS.SkipLine

     strStrings = Split(MyString, " ")
     For intInd = LBound(strStrings) To UBound(strStrings)

         Cells(num, mynum).Value = strStrings(intInd)

         mynum = mynum + 1
         If mynum Mod 4 = 0 Then
             num = num + 1
             mynum = 1
         End If
    Next

    End If
Loop
oFS.Close
Set oFSO = Nothing

I noticed that the problem is from two sources ...One my function fails woefully once the string to be split is of variable length. How could I patch this up?. Secondly, my file content some non characters which I am suspecting may be causing the issue too. Myfile has varying space after the third line. I am able to get the first two lines

DNI ROLL TEST ON 896_271209

Directional Data Test Started

=> KS 32223.63 Flammas

=> SIP -7.25 Deg

=> RIGHT 90.57 Deg

=> AZIMUTH 105.46 Deg

=> LEFT 73.92 Deg

=> OFFSET -1.15 Deg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯角 2024-12-08 08:27:49

这是我之前使用过的一个非常有用的代码片段,它将整个文本文件存储在一个字符串中,然后您可以对其进行处理(就像使用正则表达式)。

Dim strFile As String
Dim intFile As Long

intFile = FreeFile

Open "C:\File.txt" For Input As #intFile
   strFile = Input$(LOF(intFile), #intFile)  '  LOF returns Length of File
Close #intFile

'Do what you want with strFile

更新
实际上,这是我认为更安全的方法。我什至展示了如何使用打开的对话框获取文件名,这非常方便:

Sub test()

Dim fileString As String
Dim fileName As String

' You can use GetOpenFilename() if you like
fileName = Application.GetOpenFilename("Text Files (*.txt,*.txt")
fileString = Space(FileLen(fileName))

Open fileName For Binary As #1
    Get #1, , fileString
Close #1

' Do what you wish with fileString
MsgBox Len(fileString)
End Sub

Here is a very useful code sniplet I've used before that will store an entire text file in a string that you can then work on (like using regex).

Dim strFile As String
Dim intFile As Long

intFile = FreeFile

Open "C:\File.txt" For Input As #intFile
   strFile = Input$(LOF(intFile), #intFile)  '  LOF returns Length of File
Close #intFile

'Do what you want with strFile

UPDATE:
Actually, here's a method I find more safe. I have even shown how to get the file name using the open dialogue box, which is very convenient:

Sub test()

Dim fileString As String
Dim fileName As String

' You can use GetOpenFilename() if you like
fileName = Application.GetOpenFilename("Text Files (*.txt,*.txt")
fileString = Space(FileLen(fileName))

Open fileName For Binary As #1
    Get #1, , fileString
Close #1

' Do what you wish with fileString
MsgBox Len(fileString)
End Sub
萌无敌 2024-12-08 08:27:49

如果将此行:更改

Cells(num, mynum).Value = strStrings(intInd)

为:,

Cells(num, mynum).Value = "'" & strStrings(intInd)

它将强制 Excel 将每一行视为文本。我不认为它喜欢“=>”中的等号,因为等号在Excel中一般用来表示公式。

这是我在进行更改后运行它时得到的输出:

在此处输入图像描述

这对我来说似乎不太有用,但我不确定这是否给出了您正在寻找的输出——您没有提供有关输出需要的详细信息。不过,这应该有助于消除“应用程序定义或对象定义的错误”。

如果您希望在文本上运行正则表达式,请解释您希望程序执行什么操作,我相信我们中的一个人可以帮助您编写正则表达式。

If you change this line:

Cells(num, mynum).Value = strStrings(intInd)

to:

Cells(num, mynum).Value = "'" & strStrings(intInd)

it will force Excel to see each line as text. I don't think it likes the equals sign in "=>", because an equal sign is generally used to denote a formula in Excel.

Here's the output I get when I run it with that change:

enter image description here

This doesn't seem very useful to me, but I'm not sure if this gives the output you are looking for-- you didn't supply a lot of detail on what the your output needs to look like. This should help to get rid of your "Application-defined or Object-defined error", though.

If you're looking to run a RegEx on the text, please explain what you are looking for your program to do, and I'm sure one of us could help you write up a RegEx expression.

烂人 2024-12-08 08:27:49

我后来让上面的代码工作了..

Set oFS = oFSO.OpenTextFile(sFilename)
num = 15
mynum = 1
Do Until oFS.AtEndOfStream
    'Cells(num, mynum).Value = oFS.ReadLine
     MyString = oFS.ReadLine

     Do While InStr(MyString, "  ")
     MyString = Replace(MyString, "  ", " ")
    Loop


     strStrings = Split(MyString, " ")
     If UBound(strStrings) > 0 Then
     For intInd = LBound(strStrings) To UBound(strStrings)
         If Not strStrings(intInd) = "=>" Then
         Cells(num, mynum).Value = strStrings(intInd)

         mynum = mynum + 1
         If mynum Mod 5 = 0 Then
             num = num + 1
             mynum = 1
         End If
         End If
    Next
    End If

I later got the above code working ..

Set oFS = oFSO.OpenTextFile(sFilename)
num = 15
mynum = 1
Do Until oFS.AtEndOfStream
    'Cells(num, mynum).Value = oFS.ReadLine
     MyString = oFS.ReadLine

     Do While InStr(MyString, "  ")
     MyString = Replace(MyString, "  ", " ")
    Loop


     strStrings = Split(MyString, " ")
     If UBound(strStrings) > 0 Then
     For intInd = LBound(strStrings) To UBound(strStrings)
         If Not strStrings(intInd) = "=>" Then
         Cells(num, mynum).Value = strStrings(intInd)

         mynum = mynum + 1
         If mynum Mod 5 = 0 Then
             num = num + 1
             mynum = 1
         End If
         End If
    Next
    End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文