More actions
dustloop>TarkusLee Created page with "local p = {} --p stands for package --- 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 = {} for s in string.gmatch( str, '[^' .. delimiter .. ']+' ) do..." |
dustloop>Alistair3149 Escape regex pattern |
||
| Line 1: | Line 1: | ||
local p = {} --p stands for package | 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 | --- Split string by delimiter and return the table of string parts | ||
| Line 11: | Line 19: | ||
end | end | ||
local t = {} | local t = {} | ||
local pattern = '[^' .. escapePattern( delimiter ) .. ']+' | |||
for s in string.gmatch( str, pattern ) do | |||
table.insert( t, s ) | table.insert( t, s ) | ||
end | end | ||
Revision as of 05:56, 23 May 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