使用 string.gsub 替换字符串,但只能替换整个单词 [英] Use string.gsub to replace strings, but only whole words
问题描述
我有一个用于替换字符串的搜索替换脚本.它已经有选项可以进行不区分大小写的搜索和转义"匹配(例如,允许在搜索中搜索 % ( 等.
I have a search replace script which works to replace strings. It already has options to do case insensitive searches and "escaped" matches (eg allows searching for % ( etc in the search.
我现在如何被要求只匹配整个单词,我曾尝试在每一端添加 %s,但这与字符串末尾的单词不匹配,然后我无法弄清楚如何捕获在替换过程中发现的空白项使它们完好无损.
How ever I have now been asked to match whole words only, I have tried adding %s to each end, but that does not match words at the end of a string and I can't then work out how to trap for the white-space items found to leave them intact during the replace.
我是否需要使用 string.find 重做脚本并添加用于单词检查的逻辑或可能使用模式.
Do I need to redo the script using string.find and add logic for the word checking or this possible with patterns.
我用于不区分大小写和转义项的两个函数如下均返回要搜索的模式.
The two functions I use for case insensitive and escaped items are as follows both return the pattern to search for.
-- 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
我现在有办法做我想做的事,但它不雅.有没有更好的办法?
I have a way of doing what I want now, but it's inelegant. Is there a better way?
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
推荐答案
有办法做我现在想做的事,但不雅.有没有更好的办法?
have a way of doing what I want now, but it's inelegant. Is there a better way?
Lua 的模式匹配库有一个未公开的特性,称为 Frontier Pattern,它可以让你写的东西是这样的:
There is an undocumented feature of Lua's pattern matching library called the Frontier Pattern, which will let you write something like this:
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
这篇关于使用 string.gsub 替换字符串,但只能替换整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!