function solution(input, cmd) -- parse all ="" (key/value pairs) local i, values, pos = 0, {}, {} local j, k, v while true do j, i, k, v = string.find(input, '([a-zA-Z]+)="([^"]*)"', i+1) if not j then break end values[k] = v pos[k] = {j+#k+2, i} end -- cmd = "copy to " local key1, key2 = cmd:match("copy ([%S]+) to ([%S]+)") if not key1 then error("Could not parse command: "..cmd) end -- look up key1 local value = values[key1] if not value then error("attribute "..key1.." not found") end -- paste after key2 in input (removing the old value) if not pos[key2] then error("attribute "..key2.." not found") end local i, j = unpack(pos[key2]) input = input:sub(1, i-1)..value..input:sub(j) return input end