Lua 使用string.gsub替换字符串,但仅替换整个单词
我有一个搜索替换脚本,用于替换字符串。它已经可以选择进行不区分大小写的搜索和“转义”匹配(例如允许在搜索中搜索%)等 尽管现在有人要求我只匹配整个单词,但我已尝试在每一端添加%s,但这与字符串末尾的单词不匹配,因此我无法确定如何捕获在替换过程中发现的空白项,使其保持完整 我是否需要使用string.find重做脚本,并添加用于单词检查的逻辑,或者这可能与模式有关 我用于不区分大小写和转义项的两个函数如下所示,它们都返回要搜索的模式Lua 使用string.gsub替换字符串,但仅替换整个单词,lua,Lua,我有一个搜索替换脚本,用于替换字符串。它已经可以选择进行不区分大小写的搜索和“转义”匹配(例如允许在搜索中搜索%)等 尽管现在有人要求我只匹配整个单词,但我已尝试在每一端添加%s,但这与字符串末尾的单词不匹配,因此我无法确定如何捕获在替换过程中发现的空白项,使其保持完整 我是否需要使用string.find重做脚本,并添加用于单词检查的逻辑,或者这可能与模式有关 我用于不区分大小写和转义项的两个函数如下所示,它们都返回要搜索的模式 -- Build Pattern from Stri
-- 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
你的意思是如果你通过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
一旦你把单词分割了,就可以很容易地在表中的单词上进行迭代,并对每个单词进行完整的比较
我现在有办法做我想做的事,但这不雅观。有更好的办法吗
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
Lua的模式匹配库中有一个未记录的特性,称为,它可以让您编写如下内容:
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
不,我需要做的是使用我现在处理过的搜索字符串或一个变体,让它只匹配整个单词,所以我搜索“street”,它只匹配“street”,而不匹配“broadstreet”。您如何使用从nocase()和strPlainText()返回的值?这更相关。。。