0

Well given a string like this: local filename = "Help1_Index of Contents_Comments_EN.png", I can get the three occurrences ( are they even called occurrences? Anyway...) by means of this: local first, second, third = filename:match("_(.-)_(.-)_(.-)"), so far so good...

BUT, turns out it only works as long as there are indeed three occurrences in the string, so in a string like this: "Help1_Index of Contents_Comments.png" no one is returned even there are two of them... I've been told using "?" should make it work no matter the number of the occurrences present in the string, e.g. this ways: local first, second, third = filename:match("_(.-)?(.-)?(.-)?") or local first, second, third = filename:match("_(.-)?_(.-)?_(.-)?"), but no matter how I try to implement such advice, the function simply stops returning anything.

Well, I wanted to use string.match instead of string.gmatch for simplify, but it's clear I'm already beating around the bush on this... So at this point any input would be welcomed. Thanks.

Rai
  • 314
  • 1
  • 2
  • 9
  • Don't you just want to [split a string](https://stackoverflow.com/questions/1426954/split-string-in-lua) by `_`? – Luatic Mar 28 '23 at 06:13
  • Basically same concept, yes... But I started to try to solve it with string.match this time and, since it seemed technically possible for the info I had, it seemed to me it could be the way to go for this simple job. Finally, it has not resulted so easy... so I well may had take the gmach way, but at least I've now a workable alternative to it accordingly to shingo answer. But, thanks also for the suggestion, of course. – Rai Mar 28 '23 at 18:38

1 Answers1

1

Try this pattern:

_([^_]*)_?([^_]*)_?([^_]*)$
shingo
  • 18,436
  • 5
  • 23
  • 42
  • It seems to do the job! And after all that trial and error I couldn't be more grateful... I may have to make some little adjustments to manage special cases and so, but at least I have what seems a quite solid base to work with now. I'll try to study and understand what made the difference and see if I'm able to retain it (the most difficult part for me when it comes to patters ). Well, again, thank you very much for show me the way. – Rai Mar 28 '23 at 18:26