gpt4 book ai didi

lua - 尝试检查字符串是否包含给定单词

转载 作者:行者123 更新时间:2023-12-02 04:03:04 26 4
gpt4 key购买 nike

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

最佳答案

边界非常适合处理模式的边界(请参阅 Lua frontier pattern match (whole word search) ),并且您不必修改字符串:

return msg:match('%f[%a]'..what..'%f[%A]') ~= nil

仅当前一个字符不在“%a”中而下一个字符在“%a”中时,边界'%f[%a]'才匹配。边界模式从 5.1 开始可用,从 5.2 开始正式使用。

关于lua - 尝试检查字符串是否包含给定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31483613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com