Correcting myself:

Le 02/12/2010 20:59, Paul Isambert a écrit :
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

The solution doesn't even work, because if the suffix (i.e. s1) contains a magic character, things might go wrong, since it is used as a pattern afterward. For instance the following returns an error about an "invalid pattern capture" (the right parenthesis):

isub("abc)abc", "b", "B", 1)

So I'd be very glad if anybody had a solution that works, without escaping all magic characters beforehand, and preferably without LPeg for the moment.

Best,
Paul

Reply via email to