Lua提取 XXXX="YYYY" 赋值

local s = [[
aaa = "xyz"
bbb = "good"
ccc = "1234"
]];


-- 获取 XXX = "YYY" 格式的数据(YYYY中无引号)
function get_eq(s)
 local m = {};
 local r = [[%w+%s*=%s*".-"]];
 for w in string.gmatch(s, r) do
  local pos1,pos2=string.find(w, "%s*=%s*");
  local str_k = string.sub(w, 1, pos1-1);
  local str_v = string.sub(w, pos2+2, -2);
  m[str_k] = str_v;
 end
 return m;
end


-- 显示获取的结果
local m = get_eq(s);
for k,v in pairs(m) do
 print("[" .. k .. "]=(" .. v .. ") " .. type(v));
end

你可能感兴趣的:(Lua)