Jump to content




string.gsub() giving invalid pattern capture error


  • You cannot reply to this topic
2 replies to this topic

#1 hugeblank

  • Members
  • 122 posts
  • LocationA grocery store near you!

Posted 16 October 2016 - 02: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?

#2 Anavrins

  • Members
  • 775 posts

Posted 16 October 2016 - 03:50 AM

Since - is a control character, you'll have to escape them with %.
Example, "%-%-x%-offset"

#3 hugeblank

  • Members
  • 122 posts
  • LocationA grocery store near you!

Posted 16 October 2016 - 05: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 by hugeblank, 16 October 2016 - 05:08 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users