跳转到内容

模块:Regex:修订间差异

来自DJMAX中文资料库
Raxter留言 | 贡献
创建页面,内容为“local p = {} local getArgs = require('Module:Arguments').getArgs local function showError(err) return '<span class="error">' .. tostring(err) .. '</span>' end local function performMatch(text, pattern) local result = text:match(pattern) if result == nil then error("No match found") end return result end local function performReplace(text, pattern, use) local result = text:gsub(pattern, use) if result == text then e…”
 
Raxter留言 | 贡献
无编辑摘要
第2行: 第2行:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs


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


local function performMatch(text, pattern)
function p.match(frame)
     local result = text:match(pattern)
     local args  = getArgs(frame)
     if result == nil then
    local text  = args[1] or ''
         error("No match found")
    local pat    = args[2] or ''
    local okTpl  = args[3] or '$1'  -- 匹配后返回的模板,默认第一个捕获
    local failTip= args[4]          -- 未匹配时返回什么(nil 就返回错误标签)
 
     if text == '' or pat == '' then
         return failTip or errMsg('缺少文本或正则')
     end
     end
    return result
end


local function performReplace(text, pattern, use)
     local m = {text:match(pat)}      -- 放入表,可能捕获多项
     local result = text:gsub(pattern, use)
     if #m == 0 then                 -- 完全没有匹配
     if result == text then
         return failTip or errMsg('正则未匹配到任何内容')
         error("No use occurred")
     end
     end
    return result
end
function p.match(frame)
    local args = getArgs(frame)
    local text = args[1] or ''
    local pattern = args[2] or ''


     if not text or not pattern then
     if not okTpl:find('$%d') then
         return showError("警告:文字或正则表达式缺失。")
         return okTpl
     end
     end


     local result
     local out = okTpl
     local status, err = pcall(function()
     for i = 1, #m do
         result = performMatch(text, pattern)
         out = out:gsub('$' .. i, m[i])
    end)
 
    if status then
        return result
    else
        return showError(err)
     end
     end
    return out
end
end


第46行: 第36行:
     local args = getArgs(frame)
     local args = getArgs(frame)
     local text = args[1] or ''
     local text = args[1] or ''
     local pattern = args[2] or ''
     local pat  = args[2] or ''
     local use = args[3] or ''
     local repl = args[3] or ''
 
     if text == '' or pat == '' or repl == '' then
     if not text or not pattern or not use then
         return errMsg('缺少文本、正则或替换内容')
         return showError("文字、正则表达式或者使用函数错误。")
     end
     end
 
     local ok, res = pcall(function()
    local result
         return (text:gsub(pat, repl))
     local status, err = pcall(function()
         result = performReplace(text, pattern, use)
     end)
     end)
 
     if not ok then return errMsg(res) end
     if status then
     return res
        return result
    else
        return showError(err)
     end
end
end


return p
return p

2025年10月12日 (日) 00:43的版本

此模块的文档可以在模块: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 text   = args[1] or ''
    local pat    = args[2] or ''
    local okTpl  = args[3] or '$1'   -- 匹配后返回的模板,默认第一个捕获
    local failTip= args[4]           -- 未匹配时返回什么(nil 就返回错误标签)

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

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

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

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

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

return p