[luatex] String manipulation in Lua.

Paul Isambert zappathustra at free.fr
Thu Dec 2 20:59:51 CET 2010


Hello all,

My question is about Lua only, but I think here's the right place to ask 
anyway.

I want a version of string.gsub that performs replacement in the string 
but only from a certain index. For instance, in "abcabc", I would like 
to replace "b" by "B", but only in the second part.

I have the following solution:


local sub, gsub = string.sub, string.gsub
function isub (str, pattern, replace, index, num)
   -- Extract the suffix starting at given index
   local s1 = sub(str, index)
   -- Make the replacement on the suffix
   local s2 = gsub(s1, pattern, replace, num)
   -- Replace the suffix in the string with it modified version
   return gsub(str, s1 .. "$", s2)
end


It works, but I find the solution an overkill for what seems to be a 
basic operation. So, as I like to ask: have I missed something? (Yes, 
probably LPeg, but I don't want to go into that for the moment.)

Best,
Paul


More information about the luatex mailing list