使用string.gsub替换字符串,但只替换完整的单词。
2012-4-19 17:16:15
收藏:0
阅读:130
评论:2
我有一个搜索替换脚本,可以用来替换字符串。它已经有了可以大小写不敏感搜索和“转义”匹配(例如允许在搜索中使用%(等符号)的选项。
然而,现在我被要求仅匹配整个单词,我尝试在每个端点添加%s,但这不会匹配字符串末尾的单词,我无法找到如何捕获找到的空格项以保留它们在替换期间保持不变。
我是否需要重做脚本,并使用string.find添加单词检查逻辑,或者这可以使用模式来实现。
我用于大小写不敏感和转义项的两个功能如下,两者都返回要搜索的模式。
-- Build Pattern from String for case insensitive search
function nocase (s)
s = string.gsub(s, "%a", function (c)
return string.format("[%s%s]", string.lower(c),
string.upper(c))
end)
return s
end
function strPlainText(strText)
-- Prefix every non-alphanumeric character (%W) with a % escape character, where %% is the % escape, and %1 is original character
return strText:gsub("(%W)","%%%1")
end
我现在有一种做我想做的事情的方法,但它不太优雅。有没有更好的方法?
local strToString = ''
local strSearchFor = strSearchi
local strReplaceWith = strReplace
bSkip = false
if fhGetDataClass(ptr) == 'longtext' then
strBoxType = 'm'
end
if pWhole == 1 then
strSearchFor = '(%s+)('..strSearchi..')(%s+)'
strReplaceWith = '%1'..strReplace..'%3'
end
local strToString = string.gsub(strFromString,strSearchFor,strReplaceWith)
if pWhole == 1 then
-- Special Case search for last word and first word
local strSearchFor3 = '(%s+)('..strSearchi..')$'
local strReplaceWith3 = '%1'..strReplace
strToString = string.gsub(strToString,strSearchFor3,strReplaceWith3)
local strSearchFor3 = '^('..strSearchi..')(%s+)'
local strReplaceWith3 = strReplace..'%2'
strToString = string.gsub(strToString,strSearchFor3,strReplaceWith3)
end
原文链接 https://stackoverflow.com/questions/10226150
点赞
stackoverflow用户501459
现在有一种方法可以做我想要做的事情,但它不够优雅。有更好的方法吗?
Lua 的模式匹配库有一种未记录的功能叫做 Frontier Pattern,它可以让你编写这样的代码:
function replacetext(source, find, replace, wholeword)
if wholeword then
find = '%f[%a]'..find..'%f[%A]'
end
return (source:gsub(find,replace))
end
local source = 'test testing this test of testicular footest testimation test'
local find = 'test'
local replace = 'XXX'
print(replacetext(source, find, replace, false)) --> XXX XXXing this XXX of XXXicular fooXXX XXXimation XXX
print(replacetext(source, find, replace, true )) --> XXX testing this XXX of testicular footest testimation XXX
2012-04-19 16:12:58
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
你的意思是如果你传递nocase() foo,你想要[fooFOO]而不是[fF][oO][oO]吗?如果是这样,你可以尝试这个?
function nocase (s) s = string.gsub(s, "(%a+)", function (c) return string.format("[%s%s]", string.lower(c), string.upper(c)) end) return s end
如果你想要一个将句子分成单词的简单方法,可以使用这个:
function split(strText) local words = {} string.gsub(strText, "(%a+)", function(w) table.insert(words, w) end) return words end
一旦你把单词分开,就很容易在表中遍历单词并对每个单词进行全面比较。