【发布时间】:2015-07-17 19:47:00
【问题描述】:
function msgcontains(msg, what)
msg = msg:lower()
-- Should be replaced by a more complete parser
if type(what) == "string" and string.find(what, "|", 1, true) ~= nil then
what = what:explode("|")
end
-- Check recursively if what is a table
if type(what) == "table" then
for _, v in ipairs(what) do
if msgcontains(msg, v) then
return true
end
end
return false
end
what = string.gsub(what, "[%%%^%$%(%)%.%[%]%*%+%-%?]", function(s) return "%" .. s end)
return string.match(msg, what) ~= nil
end
这个功能是在RPG服务器上使用的,基本上我是在尝试匹配玩家所说的
例如; 如果 msgcontains(msg, "hi") 那么
msg = 玩家发送的消息
但是,它匹配“yesimstupidhi”之类的任何东西,它真的不应该匹配它,因为“hi”不是一个词,我能做什么? T_T
【问题讨论】:
-
试试
return string.match(' '..msg..' ', '%W'..what..'%W') ~= nil
标签: lua lua-patterns