Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:SplitStringToTable: Difference between revisions

From EmissaryFGC
dustloop>Alistair3149
Escape regex pattern
m 3 revisions imported: Credit to the Dustloop wiki (dustloop.com).
 
(One intermediate revision by one other user not shown)
Line 6: Line 6:
--- @return string
--- @return string
local function escapePattern(s)
local function escapePattern(s)
return s:gsub("([^%w])", "%%%1")
return s:gsub("%W", "%%%1")
end
end



Latest revision as of 21:52, 7 October 2024

Documentation for this module may be created at Module:SplitStringToTable/doc

local p = {} --p stands for package

--- Escape pattern for regex

--- @param s string string to escape
--- @return string
local function escapePattern(s)
	return s:gsub("%W", "%%%1")
end

--- Split string by delimiter and return the table of string parts
---
--- @param str string String to split
--- @param delimiter string Delimiter used to split the string, default to %s
--- @return table
function p.splitStringIntoTable( str, delimiter )
        if delimiter == nil then
                delimiter = "%s"
        end
        local t = {}
        local pattern = '[^' .. escapePattern( delimiter ) .. ']+'
        for s in string.gmatch( str, pattern ) do
                table.insert( t, s )
        end
        return t
end

return p
MediaWiki Appliance - Powered by TurnKey Linux