Tricky regular expression
Hi Claus. I found that your split procedure works fine when the input is in a string variable. But for some reason there's a bug when I read the input string from a file. Here's the code:
Sub FileReadTestForError()
Dim sPath As String
Dim sLine As String
Dim sFile As Object
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
' data file contains "a123 A. BC #123"
sPath = "C:\data.txt"
Set sFile = fso.OpenTextFile(sPath, 1)
sLine = sFile.ReadLine ' The bug occurs here.
sLine = Application.Substitute(sLine, " ", "*", 1)
varTmp = Split(sLine, "*")
str1 = varTmp(0)
str2 = Trim(varTmp(1))
MsgBox str2 ' Is supposed to return "A. BC #123"
' But returns "BC #123"
sFile.Close
End Sub
The code actually works if you replace that sFile.Readline code with the
following:
sLine = "a123 A. BC #123"
So, that tells me the Readline function is producing a tainted string somehow. Do you know what's going on?
Thanks!
|