首页
随机
登录
设置
关于DJMAX中文资料库
免责声明
DJMAX中文资料库
搜索
查看“︁模块:Regex”︁的源代码
←
模块:Regex
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
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
该页面使用的模板:
模块:Regex/doc
(
查看源代码
)
返回
模块:Regex
。