Replace a substring in Lua with a pattern

喜夏-厌秋 提交于 2021-01-28 00:34:05

问题


I have a string like this

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

I would like the pattern match ["password"] = and have it replace only the string in between the ";' that would be '"krghfkghkfghf" in this instance.


回答1:


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

That won't work if your password contains a double quote inside.




回答2:


i have same question as well, how about the following password replace ?

"password" : "krghfkghkfghf"


来源:https://stackoverflow.com/questions/16638792/replace-a-substring-in-lua-with-a-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!