用模式替换Lua中的子字符串

时间:2013-05-19 19:52:03

标签: lua string-substitution

我有一个像这样的字符串

     str = '["username"] = "user";
     ["deepscan"] = "true";
     ["token"] = true;
     ["password"] = "krghfkghkfghf";
     ["uploadMethod"] = "JSON";
     ["serviceIsRunning"] = {};
     ["host"] = "sample.com";
     ["instance_ID"] = 405454058;'

我希望模式匹配["password"] =并让它只替换此实例中";' that would be '"krghfkghkfghf"之间的字符串。

2 个答案:

答案 0 :(得分:1)

local function replacePass(configstr, newpass)
    return configstr:gsub("(%[\"password\"%]%s*=%s*)%b\"\"", "%1\"" .. newpass .. "\"")
end

如果你的密码里面包含双引号,那将无法运作。

答案 1 :(得分:0)

我也有同样的问题, 如何替换以下密码?

"password" : "krghfkghkfghf"