文字列を置換する検索置換スクリプトがあります。大文字と小文字を区別しない検索と「エスケープされた」一致を行うオプションが既にあります(たとえば、検索で % ( などを検索できます。
単語全体のみを一致させるように求められましたが、両端に %s を追加しようとしましたが、文字列の末尾の単語と一致せず、白をトラップする方法がわかりません-スペースアイテムは、交換中に元のままになっていることがわかりました。
string.find を使用してスクリプトをやり直し、単語チェック用のロジックを追加する必要がありますか、それともパターンで可能ですか。
大文字と小文字を区別しないアイテムとエスケープされたアイテムに使用する 2 つの関数は次のとおりです。どちらも検索するパターンを返します。
-- 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