使用string.gsub替换字符串,但仅替换整个单词

编程入门 行业动态 更新时间:2024-10-28 20:21:44
本文介绍了使用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替换字符串,但仅替换整个单词

本文发布于:2023-11-06 09:30:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563316.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   单词   string   gsub

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!