'Lua gsub replace value after a word in a string
I am with a little problem, here is a simple version of it I am trying to substitute a value after word. The pattern is keyword value\n
This is how far I am getting string.match is finding the value but gmatch isnt
str = [[
hey lala
foo lala
we 4568$%AW
bobo 5555
nono -5%aw!
la34 444
]]
key = 'foo'
new_value = 'bar'
print(string.match(str,key..' '..'(.-)\n')) -- Finds lala
print(string.gsub(str,key..' '..'(.-)\n',new_value..'\n')) --dont find lala
Solution 1:[1]
function ChangeChunkVal2(str, key, new_value)
local val = string.match(str,key..' '..'(.-)\n') -- Finds value
return string.gsub(str,key..' '..val,new_value) --change it
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cacilda |
