This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
hugeblank's profile picture

string.gsub() giving invalid pattern capture error

Started by hugeblank, 16 October 2016 - 01:22 AM
hugeblank #1
Posted 16 October 2016 - 03:22 AM
So I am working on a little project that requires the use of a find and replace sort of feature, and I figure that using string.gsub() is the proper way to go about what I want to do. However, when I use this:
 --Read the file
local filer = fs.open('.gphkit/temp', "r")
local str = filer.readAll()
filer.close()
--variable calling
local fx = tostring(.. oox .. '--x-offset')
local fxr = tostring(.. ox .. '--x-offset')
local fy = tostring(.. ooy .. '--y-offset')
local fyr = tostring(.. oy .. '--y-offset')
--find and replace where the offsets are stored in the fid API
local str = string.gsub(str, fx, fxr)
local str = string.gsub(str, fy, fyr)
--embed into fid API
local filew = fs.open('.gphkit/temp', "w")
filew.write(str)
filew.close()
it gives an error saying: 'invalid pattern capture'
I figured that the variables oox, ox, ooy, and oy are the issue, but…
oox, ooy, oy, and ox are numbers that are getting converted into strings, and I am 100% sure strings are one of the ways gsub can be used to capture patterns, so what is wrong here?
Anavrins #2
Posted 16 October 2016 - 04:50 AM
Since - is a control character, you'll have to escape them with %.
Example, "%-%-x%-offset"
hugeblank #3
Posted 16 October 2016 - 06:07 AM
Diddily darn, you're right. Thanks for the help!
On top of that, for some dumb reason, at the very beginning of the tostring() for each variable, I added a .., that has since been removed :P/> (because it was unnecessary and prone to error causing, b/c that's not how lua works XP)
Edited on 16 October 2016 - 04:08 AM