1

as expected gsub starts replacing string occurrences from left to right, but I assumed I could easily reverse this behavior by using a negative 1 as forth argument in case I wanted to only replace only the very last _L, for example like this:

string.gsub("Bone_L_L", "_L", "_R", -1)

But turns out it replaces all the occurrences instead, which is not useful at all from my POV, since that's already the default behavior...

I guess I could somehow get what I want by reversing the involved strings in my function with the help of string.reverse(), but I wanted to be sure I was not missing a... handier way?

Rai
  • 314
  • 1
  • 2
  • 9
  • Hi and thanks for the link. I finally had to resort to the "string.reverse()" method as I was afraid, since I really didn't find another way and the other proposed method there based on ".*" didn't work to me (it made "string.gsub()" returned just the pattern instead of the entire modified string I don't know why). Well, not the elegant solution I wanted, but at least it works! – Rai Aug 06 '22 at 00:57
  • `string.gsub("Bone_L_L", "^(.*)_L(.*)", "%1_R%2")` – Egor Skriptunoff Aug 06 '22 at 08:24
  • Thank you @EgorSkriptunoff, it totally seems to work! I still don't understand the logic behind all the involved symbols, but it's definitely closer to what I was looking for than the reversing strings solution I had to implement. I find that your comment adds value to the existing question, so although stack overflow gives me the option to delete this one, I think it doesn't hurt not deleting just in case someone else finds it useful. Well, thanks again! – Rai Aug 07 '22 at 01:26

0 Answers0