【问题标题】:Replace a substring in Lua with a pattern用模式替换 Lua 中的子字符串
【发布时间】:2013-05-19 19:52:03
【问题描述】:

我有一个这样的字符串

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

我想要模式匹配 ["password"] = 并让它只替换 ";' that would be '"krghfkghkfghf" 在这种情况下之间的字符串。

【问题讨论】:

  • str = str:gsub('(%["password"%]%s*=%s*").-"','%1'..new_pwd:gsub('%%','%%%%')..'"')
  • @EgorSkriptunoff - 为什么你总是将答案发布为 cmets :)?

标签: lua string-substitution


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

如果您的密码中包含双引号,这将不起作用。

【讨论】:

    【解决方案2】:

    我也有同样的问题, 下面的密码替换怎么样?

    "password" : "krghfkghkfghf"
    

    【讨论】:

      猜你喜欢
      • 2019-04-23
      • 2015-05-26
      • 1970-01-01
      • 2019-03-08
      • 2011-05-16
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      相关资源
      最近更新 更多