I’m new to XOJO (and new to programming of any kind!)… I’m struggling with a bit of code that will check to see if a text-file exists and then if it does, read a MAC address from that file into a new variable (or display it in a TextArea).
I can check for the existence of the file and read it into a string… that bit seems to work ok for me.
I’m then trying to use RegEx to search though the entire file and find the first occurence of the string I’m after “(\w{2}-){5}(\w{2})”. When finished, I intend to run this code on multiple computers, so obviously the MAC address will be different every time… hence me using RegEx (to make things worse, the MAC address won’t always be located in the same position in the text file).
The following code will return the information I want, but I can only get to to search on a specific line of the original textfile instead of the whole file.
e.g “text = imported(3)” reads the fourth line
Can anyone help me to get this working please?
Cheers
Neil
Dim t As TextInputStream
Dim f As FolderItem
f= Volume(0).Child(“Folder1”).Child(“Folder2”).Child(“File.txt”)
If f.exists Then
t = TextInputStream.Open(f)
Dim imported() As String
While Not t.EOF
imported.Append t.ReadLine
Wend
dim text as string
text = imported(3)
dim matchLines() as string
dim rx as new RegEx
rx.SearchPattern = “NAME”
dim match as RegExMatch = rx.Search( text )
while match <> nil
matchLines.Append match.SubExpressionString( 0 )
match = rx.Search
wend
TextArea1.Text = join( matchLines, EndOfLine )
end if
The text immediately before the MAC address is usually “HOSTID=00-11-22-33-44-55”. But this could appear at different points in the imported text. i.e. The HOSTID= field isn’t always at the start of a line.
Neil, the key is how you’re reading the file, not in using a RegEx. You are reading it line by line into an array, then applying the pattern to a single line, exactly as you described. The solution is to read the entire file into a single variable and apply the RegEx against that variable.
Axel shows you how to read it into one variable above (t.ReadAll). I didn’t check the pattern, but since it works on one line, I’m sure it’s fine.
I’ve just tried the .ReadALL instruction but seem to only get the first line of the text file… I must be doing something wrong (just not sure what it is).