2

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.

2
  • str = str:gsub('(%["password"%]%s*=%s*").-"','%1'..new_pwd:gsub('%%','%%%%')..'"') Commented May 19, 2013 at 20:05
  • 2
    @EgorSkriptunoff - why do you always post answers as comments :)? Commented May 19, 2013 at 20:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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

"password" : "krghfkghkfghf"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.