Import text file into variable and then search using RegEx

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

can you post an example of the text you want to import, maybe you can use nthfield

Can’t really post an exaple of the text I’m importing onto a public board (sorry).

The data is in the form of license files for all the CAD workstations that I’m looking-after.

there is text before the MAC address, which is always the same?

for example if you have text like this

you could use

mystring = left(nthfield(myimportedtext, "Ethernet NIC: ", 2), 17)

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.

[code]Dim t As TextInputStream
Dim imported As String
Dim f As FolderItem
f= Volume(0).Child(“Folder1”).Child(“Folder2”).Child(“File.txt”)

If f <> nil and f.exists Then
t = TextInputStream.Open(f)
imported = t.ReadAll

TextArea1.Text = left(nthfield(imported, “HOSTID=”, 2), 17)
End If[/code]

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.

Thanks Axel and Kem!

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).

Perhaps post your new code?

I’ll do that when I get back home later on… cheers =)

The .ReadAll code is now working as I want it to… I think the lesson to be learnt here is not to try and write code when you’re tired/half asleep!

Thank you for helping Axel and Kem