Jump to navigation
Jump to search
Documentation for this module may be created at Module:Explode/Documentation
--[[
This module is used to explode a passed string into parts. First parameter is the divider string to divide the string at. The second parameter is the string.
]]--
local p = {}
function p.explode(frame)
--[[
By Notmyhandle
args:
1:string
2:delimiter character (divider character)
]]--
if (frame.args[1]=='') then return false end
local rightside,s,e = 0
--capture the starting and ending index
s,e = string.find(frame.args[1],frame.args[2])
print(s)
print(e)
return rightside
end
function p.explode2(frame)
--[[
args:
1:delimiter character (divider character)
2:string
]]--
if (frame.args[1]=='') then return false end
local pos,arr = 0,{}
-- for each divider found
for st,sp in function() return string.find(frame.args[2],frame.args[1],pos,true) end do
table.insert(arr,string.sub(frame.args[2],pos,st-1)) -- Attach chars left of current divider
pos = sp + 1 -- Jump past current divider
end
table.insert(arr,string.sub(frame.args[2],pos)) -- Attach chars right of last divider
return arr
end
function p.find(frame)
--[[
args:
1:string
2:delimiter character (divider character)
3:starting position (far left if not set)
4:set to 'true' if you want plain matching enabled.
See http://lua-users.org/wiki/StringLibraryTutorial
]]--
return string.find(frame.args[1],frame.args[2],frame.args[3],frame.args[4])
end
function p.split(frame)
--[[
args:
1:string
2:delimiter character (divider character)
]]--
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. frame.args[2]
local last_end = 1
local s, e, cap = frame.args[1]:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = frame.args[1]:find(fpat, last_end)
end
if last_end <= #frame.args[1] then
cap = frame.args[1]:sub(last_end)
table.insert(t, cap)
end
return t
end
return p