Mode matching strings in Lua

I used Lua to split the following strings into tables:
(The data are aligned with each other. I did not find how to write the formatted data on this website)

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

The results should be placed in 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:

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

That was my best attempt.

< /div>

If I understand your use case, the following should solve the problem. It may need to be adjusted slightly.

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+)")

I used Lua to split the following strings into tables:
(The data are aligned with each other. I did not find how to write the formatted data on this website)

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

The results should be placed in 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:

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

That was my best attempt.

< /p>

If I understand your use case, the following should solve the problem. It may need to be adjusted slightly.

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+)")

Leave a Comment

Your email address will not be published.