views:

166

answers:

3

I have the following string to split into a table using lua: (the data is aligned with each other. I did not find out how to write format it like that on this site)

IP: 192.168.128.12
MAC: AF:3G:9F:c9:32:2E
Expires: Fri Aug 13 20:04:53 2010
Time Left: 11040 seconds

the result should be put into a table like this:

t = {"IP" : "192.168.128.12", "MAC" : "AF:3G:9F:c9:32:2E", "Expires" : "Fri Aug 13 20:04:53 2010", "Time Left" : "11040 seconds"}

I tried with:

for k,v in string.gmath(data, "([%w]+):([%w%p%s]+\n") do t[k] = v end

That was my best attempt. I have been sitting with this for quite some time now, but it just never becomes right. If there is anyone who can help me I will be very appreciative.

Thanks

+2  A: 

If I understood your use case the following should do the trick. It might require a bit of tweaking though.

local s = "IP: 192.168.128.12 MAC: AF:3G:9F:c9:32:2E Expires: Fri Aug 13 20:04:53 2010 Time Left: 11040 seconds"
local result = {}
result["IP"] = s:match("IP: (%d+.%d+.%d+.%d+)")
result["MAC"] = s:match("MAC: (%w+:%w+:%w+:%w+:%w+:%w+)")
result["Expires"] = s:match("Expires: (%w+ %w+ %d+ %d+:%d+:%d+ %d+)")
result["Time Left"] = s:match("Time Left: (%d+ %w+)")
ponzao
A: 

The pattern below should work for you, provided that:

  1. The IP address is decimal dotted notation.
  2. The MAC address is hexadecimal separated by colons.

Note: The MAC address provided in your question has a 'G' which is not a hexadecimal digit.

Edit: After thinking about your question in detail, I have expanded my answer to show how multiple instances can be captured into a table.

sString = [[
IP: 192.168.128.16
MAC: AF:3F:9F:c9:32:2E
Expires: Fri Aug 1 20:04:53 2010
Time Left: 11040 seconds

IP: 192.168.128.124
MAC: 1F:3F:9F:c9:32:2E
Expires: Fri Aug 3 02:04:53 2010
Time Left: 1140 seconds

IP: 192.168.128.12
MAC: 6F:3F:9F:c9:32:2E
Expires: Fri Aug 15 18:04:53 2010
Time Left: 110 seconds
]]

local tMatches = {}

for sIP, sMac, sDate, sSec in sString:gmatch("IP:%s([%d+\.]+)%sMAC:%s([%x+:]+)%sExpires:%s(.-)%sTime%sLeft:%s(%d+)%s%w+") do
    if sIP and sMac and sDate and sSec then
        print("Matched!\n"
                .."IP: "..sIP.."\n"
                .."MAC: "..sMac.."\n"
                .."Date: "..sDate.."\n"
                .."Time: "..sSec.."\n")

        table.insert(tMatches, { ["IP"]=sIP, ["MAC"]=sMac, ["Date"]=sDate, ["Expires"]=sSec })
    end
end

print("Total Matches: "..table.maxn(tMatches))

for k,v in ipairs(tMatches) do
    print("IP Address: "..v["IP"])
end
Adam
+1  A: 

Assuming "data aligned with each other" means something like the following:

IP:          192.168.128.12
MAC:         AF:3G:9F:c9:32:2E
Expires:     Fri Aug 13 20:04:53 2010
Time Left:   11040 seconds

The <pre> tag can be used to maintain alignment.

Minimizing changes to your existing code:

for k,v in string.gmatch(data, "(%w[%w ]*):%s*([%w%p ]+)\n") do t[k] = v end
  • changed first capture to (%w[%w ]*), to avoid leading spaces and to get space in Time Left
  • added %s* after :, to avoid leading spaces in captured values
  • changed %s to space in second capture, to avoid capturing \n
  • fixed typos gmath to gmatch and added ) for capture
gwell