下面的模式应该适合你,前提是:
- IP 地址是十进制点分表示法。
- MAC 地址是用冒号分隔的十六进制。
注意:您问题中提供的 MAC 地址有一个不是十六进制数字的“G”。
编辑:在详细考虑了您的问题之后,我扩展了我的答案以显示如何将多个实例捕获到一个表中。
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