用模式替换 Lua 中的子字符串 [英] Replace a substring in Lua with a pattern

查看:74
本文介绍了用模式替换 Lua 中的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的字符串

I have a string like this

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

我希望模式匹配 ["password"] = 并让它仅替换之间的字符串";'在这种情况下为'"krghfkghkfghf" .

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

推荐答案

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.

这篇关于用模式替换 Lua 中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆