读取文本文件并在结果字符串上使用正则表达式应用
我想读取一次文本文件的内容,并对结果字符串应用正则表达式。我无法弄清楚如何读取文本文件一次。
我一直在尝试一种替代方案,但它无法正常工作
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我之前使用过的一个非常有用的代码片段,它将整个文本文件存储在一个字符串中,然后您可以对其进行处理(就像使用正则表达式)。
更新:
实际上,这是我认为更安全的方法。我什至展示了如何使用打开的对话框获取文件名,这非常方便:
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).
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:
如果将此行:更改
为:,
它将强制 Excel 将每一行视为文本。我不认为它喜欢“=>”中的等号,因为等号在Excel中一般用来表示公式。
这是我在进行更改后运行它时得到的输出:
这对我来说似乎不太有用,但我不确定这是否给出了您正在寻找的输出——您没有提供有关输出需要的详细信息。不过,这应该有助于消除“应用程序定义或对象定义的错误”。
如果您希望在文本上运行正则表达式,请解释您希望程序执行什么操作,我相信我们中的一个人可以帮助您编写正则表达式。
If you change this line:
to:
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:
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.
我后来让上面的代码工作了..
I later got the above code working ..