跳转到内容

模块:Regex:修订间差异

来自DJMAX中文资料库
Raxter留言 | 贡献
无编辑摘要
Raxter留言 | 贡献
无编辑摘要
 
第7行: 第7行:


function p.match(frame)
function p.match(frame)
     local args   = getArgs(frame)
     local args     = getArgs(frame)
     local text  = args[1] or ''
     local str      = args[1] or ''
     local pat    = args[2] or ''
     local pattern  = args[2] or ''
     local okTpl  = args[3] or '$1'   -- 匹配后返回的模板,默认第一个捕获
     local fmt      = args[3] or '$1'
     local failTip= args[4]           -- 未匹配时返回什么(nil 就返回错误标签)
     local noMatch  = args[4] or ''


     if text == '' or pat == '' then
     if str == '' or pattern == '' then
         return failTip or errMsg('缺少文本或正则')
         return noMatch or errMsg('缺少文本或正则')
     end
     end


     local m = {text:match(pat)}     -- 放入表,可能捕获多项
     local caps = {str:match(pattern)}
     if #m == 0 then                 -- 完全没有匹配
     if #caps == 0 then
         return failTip or errMsg('正则未匹配到任何内容')
         return noMatch or errMsg('正则未匹配到任何内容')
     end
     end


     if not okTpl:find('$%d') then
     if not fmt:find('$%d') then
         return okTpl
         return fmt
     end
     end


     local out = okTpl
     local out = fmt
     for i = 1, #m do
     for i = 1, #caps do
         out = out:gsub('$' .. i, m[i])
         out = out:gsub('$' .. i, caps[i])
     end
     end
     return out
     return out
第34行: 第34行:


function p.replace(frame)
function p.replace(frame)
     local args = getArgs(frame)
     local args   = getArgs(frame)
     local text = args[1] or ''
     local str    = args[1] or ''
     local pat  = args[2] or ''
     local pattern = args[2] or ''
     local repl = args[3] or ''
     local replace = args[3] or ''
     if text == '' or pat == '' or repl == '' then
     if str == '' or pattern == '' or replace == '' then
         return errMsg('缺少文本、正则或替换内容')
         return errMsg('缺少文本、正则或替换内容')
     end
     end
     local ok, res = pcall(function()
     local ok, res = pcall(function()
         return (text:gsub(pat, repl))
         return (str:gsub(pattern, replace))
     end)
     end)
     if not ok then return errMsg(res) end
     if not ok then return errMsg(res) end

2025年10月13日 (一) 23:55的最新版本

此模块的文档可以在模块:Regex/doc创建

local p = {}
local getArgs = require('Module:Arguments').getArgs

local function errMsg(s)
    return '<span class="error">' .. tostring(s) .. '</span>'
end

function p.match(frame)
    local args      = getArgs(frame)
    local str       = args[1] or ''
    local pattern   = args[2] or ''
    local fmt       = args[3] or '$1'
    local noMatch   = args[4] or ''

    if str == '' or pattern == '' then
        return noMatch or errMsg('缺少文本或正则')
    end

    local caps = {str:match(pattern)}
    if #caps == 0 then
        return noMatch or errMsg('正则未匹配到任何内容')
    end

    if not fmt:find('$%d') then
        return fmt
    end

    local out = fmt
    for i = 1, #caps do
        out = out:gsub('$' .. i, caps[i])
    end
    return out
end

function p.replace(frame)
    local args    = getArgs(frame)
    local str     = args[1] or ''
    local pattern = args[2] or ''
    local replace = args[3] or ''
    if str == '' or pattern == '' or replace == '' then
        return errMsg('缺少文本、正则或替换内容')
    end
    local ok, res = pcall(function()
        return (str:gsub(pattern, replace))
    end)
    if not ok then return errMsg(res) end
    return res
end

return p