lua 使用正则表达式分割字符串

function string_split(str, delimiter)
 if str == nil or str == '' or delimiter == nil then
  return nil
 end
 
    local result = {}
    for match in (str..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match)
    end
    return result
end

local tbl = string_split("aaaaacdddddzzzz", "c")

for k, v in pairs(tbl) do
 print(k ,v )
end

输出结果:

> dofile "123.lua"
1       aaaaa
2       dddddzzzz
>

原文地址:https://www.cnblogs.com/HemJohn/p/5815990.html