我正在尝试为WoW编程一个插件(在lua中).这是一个基于特定单词的聊天过滤器.我无法弄清楚如何使这些单词的数组不区分大小写,以便该单词的任何大写/小写组合与数组匹配.任何想法将不胜感激.谢谢!
- local function wordFilter(self,event,msg)
- local keyWords = {"word","test","blah","here","code","woot"}
- local matchCount = 0;
- for _,word in ipairs(keyWords) do
- if (string.match(msg,word,)) then
- matchCount = matchCount + 1;
- end
- end
- if (matchCount > 1) then
- return false;
- else
- return true;
- end
- end
解决方法
>在函数之外定义关键字.否则你就是在重新创造
桌子每次都只是为了把它抛到一边,浪费时间
在创作和GC上.
>将keyWords转换为匹配的模式
大写和小写字母.
>您不需要捕获的数据
来自string,所以使用string.find获取速度.
>根据你的意见
逻辑,如果你有多个比赛你发出’假’的信号.以来
你只需要一场比赛,你不需要计算它们.回来吧
你一打它就会假的.节省您检查所有时间的时间
剩下的话也是.如果以后你决定要多个
匹配,你还是最好在循环中检查它并尽快返回
你达到了预期的数量.
>不要使用ipairs.从1到数组长度的循环比简单慢,并且在Lua 5.2中不推荐使用ipairs.
桌子每次都只是为了把它抛到一边,浪费时间
在创作和GC上.
>将keyWords转换为匹配的模式
大写和小写字母.
>您不需要捕获的数据
来自string,所以使用string.find获取速度.
>根据你的意见
逻辑,如果你有多个比赛你发出’假’的信号.以来
你只需要一场比赛,你不需要计算它们.回来吧
你一打它就会假的.节省您检查所有时间的时间
剩下的话也是.如果以后你决定要多个
匹配,你还是最好在循环中检查它并尽快返回
你达到了预期的数量.
>不要使用ipairs.从1到数组长度的循环比简单慢,并且在Lua 5.2中不推荐使用ipairs.
- local keyWords = {"word","woot"}
- local caselessKeyWordsPatterns = {}
- local function letter_to_pattern(c)
- return string.format("[%s%s]",string.lower(c),string.upper(c))
- end
- for idx = 1,#keyWords do
- caselessKeyWordsPatterns[idx] = string.gsub(keyWords[idx],"%a",letter_to_pattern)
- end
- local function wordFilter(self,msg)
- for idx = 1,#caselessKeyWordsPatterns do
- if (string.find(msg,caselessKeyWordsPatterns[idx])) then
- return false
- end
- end
- return true
- end
- local _
- print(wordFilter(_,_,'omg wtf lol'))
- print(wordFilter(_,'word man'))
- print(wordFilter(_,'this is a tEsT'))
- print(wordFilter(_,'BlAh bLAH Blah'))
- print(wordFilter(_,'let me go'))
结果是:
- true
- false
- false
- false
- true