6

文字列を置換する検索置換スクリプトがあります。大文字と小文字を区別しない検索と「エスケープされた」一致を行うオプションが既にあります(たとえば、検索で % ( などを検索できます。

単語全体のみを一致させるように求められましたが、両端に %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
4

2 に答える 2

6

私が今やりたいことをする方法がありますが、それはエレガントではありません。より良い方法はありますか?

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-19T16:12:58.930 に答える
1

nocase() foo を渡すと、[fF][oO][oO] の代わりに [fooFOO] が欲しいということですか? もしそうなら、あなたはこれを試すことができますか?

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

単語を分割したら、テーブル内の単語を反復処理して、各単語に対して完全な比較を行うのは非常に簡単です。

于 2012-04-19T15:16:42.703 に答える