模块:字符串处理
跳转到导航
跳转到搜索
此模块的文档可以在 手动:字符串处理/doc 创建
require('strict')
local frame = mw.getCurrentFrame()
local p = { }
local function replace(s, pattern, repl, n)
local str = s:gsub(pattern, repl, n)
return str
end
local function explode(s, delimiter)
local result = {}
local str = s:gsub(delimiter, '|')
for match in str:gmatch('[^|]+') do
table.insert(result, match)
end
return result
end
function p.replace(frame)
if string.len(frame.args[4])>0 then
return replace(frame.args[1], frame.args[2], frame.args[3], tonumber(frame.args[4]))
else
return replace(frame.args[1], frame.args[2], frame.args[3])
end
end
function p.dateprocess(frame)
local output = ''
for i, value in ipairs(explode(frame.args[1], '<br>')) do
local date = {value:match('(%d*)(%d%d%d%d%-[%dx]+)')}
if date[1] == '' and ({value:match('(%d*)(%d%d%d%d%-%d+%-[%dx]+)')})[2] then
date = value:gsub('(%d%d%d%d)%-(%d+)%-([%dx]+)', '%1 年 %2 月 %3 日')
elseif date[1] == '' and date[2] then
date = value:gsub('(%d%d%d%d)%-(%d+)', '%1 年 %2 月')
elseif value:match('^%d%d%d%d') then
date = value:gsub('(%d%d%d%d)', '%1 年')
else
date = value
end
output = output..date..'<br>'
end
output = output:gsub('<br>$','')
return output
end
function p.preprocess(frame)
for i, value in ipairs(explode(frame.args[1], '<br>')) do
local date = {value:match('(%d*)(%d%d%d%d%-%d+)')}
if date[1] == '' and date[2] then
return 1
elseif value:match('^%d%d%d%d') then
return 1
end
end
return 0
end
return p