使用string.gsub替换字符串,但仅替换整个单词 [英] Use string.gsub to replace strings, but only whole words

查看:786
本文介绍了使用string.gsub替换字符串,但仅替换整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索替换脚本,该脚本可以替换字符串.它已经可以选择不区分大小写的搜索和转义"的匹配项(例如,允许搜索%(等).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆