Using one pattern to replace multiple ;
to single ;
, and another to remove the last ;
is like this
local str = "one;two;;three;four;;five;;six;seven;"
local result = str:gsub(';+', ';'):gsub(';*$', '')
One possible way to use a single pattern is like this:
local result = (str .. ";;;"):gsub(";+", function(s)
if s == ";" or s == ";;" then
return ";"
else
return ""
end
end)
What I'm trying to do is to append three ;
to the end of the string, so any one or two ;
should be replaced with one ;
. And more ;
(could be three or four and it must be at the end) is replaced to empty string.
I would prefer the solution with two patterns as it's more clear.