模式匹配Lua中的字符串

我使用Lua将以下字符串拆分为表:
(数据彼此对齐.我没有找到如何在此网站上编写格式的数据)

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

结果应该放在这样的表中:

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”}

我尝试过:

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

那是我最好的尝试.

解决方法

如果我理解你的用例,以下应该可以解决问题.可能需要稍微调整一下.

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

相关文章

1.github代码实践源代码是lua脚本语言,下载th之后运行thmai...
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/...
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能...
localfunctiongenerate_action(params)localscale_action=cc...
2022年1月11日13:57:45 官方:https://opm.openresty.org/官...
在Lua中的table(表),就像c#中的HashMap(哈希表),key和...