这样不是吧 bingo 这个单词破坏了吗?
我看你的意思也就是 简单搜索下,那这种在lua中就更容易了。---
-- function maskMsg takes string, table, string returns string
--
-- @text 预处理的文本
-- @set 预搜索的子串集合
--
-- @repl 替换的文本
-- 不指定则替换成 与字符串长度相等的 "*"
--
-- @return 处理后的文本
local function maskMsg ( text, set, repl )
if #text <= 0 then
return text
end
local function maskor ( str )
return repl or string.rep( "*", #str )
end
for k, str in pairs(set) do
text = string.gsub( text, str, maskor(str) )
end
return text
end
-- 对给定文本中出现的 给定集合中的子串进行替换
text = "lua is my style! hello boy ,let`s go,我们的主题曲! Bingo!"
wordLib = {"lua","my","hello","go","主题","你好"}
local function main ()
local msg = maskMsg ( text, wordLib, "**")
print(msg)
end
main()
--- 输出结果
-- ** is ** style! ** boy ,let`s **,我们的**曲! Bin**!
【注:】
【1】上面这个demo 是效率很低很低的方法。
【2】附件:简单屏蔽指定集合中的单词(含中文)- flameleo.lua--- 输出结果
-- ** is ** style! ** boy ,let`s **,我们的**曲! Bingo!
是一个分别处理模式串和非模式串的方法,需要改进中文分词算法