使用一种模式替换多个 ;
单一 ;
, ,另一个要删除最后一个 ;
就是这样
local str = "one;two;;three;four;;five;;six;seven;"
local result = str:gsub(';+', ';'):gsub(';*$', '')
使用单个模式的一种可能方法是这样:
local result = (str .. ";;;"):gsub(";+", function(s)
if s == ";" or s == ";;" then
return ";"
else
return ""
end
end)
我要做的是附加三个 ;
到弦的末端,所以任何一个或两个 ;
应该用一个 ;
. 。和更多 ;
(可能是三,四个,必须在末尾)替换为空字符串。
我更喜欢使用两种模式的解决方案,因为更清楚。