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

时间:2016-08-28 22:21:08   收藏:0   阅读:363

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
>

 

原文:http://www.cnblogs.com/HemJohn/p/5815990.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!