Lua gsub expression to rename variables in a Lua script

Viewed 111

I would like a gsub pattern to rename variables in a Lua script. Say I wanted to rename x to y.

scr = scr:gsub(...,'y')

Where ... would find instances of x that are not beside any underscore, letter, or number.

EDIT: Here's an attempt

-- should be replaced?
--  yes   yes   no      no      yes   yes  yes    yes   no    no     no     yes
s = 'unc unc function FUNCTION :unc: .unc. -unc- =unc=  unc1 unc_ \' unc\' unc'
print(s:gsub('([%A?%-?])(unc)([%W?%-?])', '%1XXX%3'))
-- need to fix the start or beginning of string
--    could easily pad the string with a space then remove
-- also missing underscores and strings (ie dont replace between quotes) though..
-- tried ^_? (not 0 or 1 underscore) but it didn't work

> unc XXX function FUNCTION :XXX: .XXX. -XXX- =XXX= unc1 XXX_ ' XXX' unc

0 Answers
Related