模块:Utils/String:修订间差异
来自「荏苒之境」
无编辑摘要 |
无编辑摘要 |
||
第33行: | 第33行: | ||
end | end | ||
utils.unlines = function(str) | utils.unlines = function(str, alternative) | ||
local regex = alternative | |||
and "([^"..alternative.."]*)(["..alternative.."]?)" | |||
or "([^\n]*)(\n?)" | |||
local t = {} | local t = {} | ||
for line, newline in string.gmatch(str, | for line, newline in string.gmatch(str, regex) do | ||
if line == "" and newline == "" then | if line == "" and newline == "" then | ||
break | break |
2025年8月21日 (四) 19:10的最新版本
此模块的文档可以在模块:Utils/String/doc创建
local utils = {}
utils.escape_regex = function(str)
return str:gsub("[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%1")
end
utils.replace = function(str, this, that, count)
return string.gsub(str, utils.escape_regex(this), that:gsub("%%", "%%%%"), count)
end
utils.trim = function(str)
local res = string.gsub(str, "^%s*(.-)%s*$", "%1")
return res
end
utils.split = function(str, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(str, "([^"..sep.."]+)") do
t[#t+1] = str
end
return t
end
utils.unword = function(str)
local t = {}
for str in string.gmatch(str, "([^%s]+)") do
t[#t+1] = str
end
return t
end
utils.unlines = function(str, alternative)
local regex = alternative
and "([^"..alternative.."]*)(["..alternative.."]?)"
or "([^\n]*)(\n?)"
local t = {}
for line, newline in string.gmatch(str, regex) do
if line == "" and newline == "" then
break
elseif string.sub(line, #line) == "\r" then
line = string.sub(line, 1, #line - 1)
end
t[#t+1] = line
end
return t
end
utils.match_xml_noattr = function(str, tag)
local res = string.match(str, "<"..tag..">(.-)</"..tag..">")
return res
end
utils.sub_xml_noattr = function(str, tag, rep)
local res = string.gsub(str, "<"..tag..">(.-)</"..tag..">", rep)
return res
end
utils.expand_xml_noattr = function(str, tag)
return utils.sub_xml_noattr(str, tag, "%1")
end
return utils