lua-lpeg表达式在两个delimeters之间不sub



如果字符串不在某个起始和结束分隔符之间,我想了解如何使用lpeg来替换字符串。下面是一个示例,我想使用SKIPstartSKIPstop来表示不应该替换文本的位置。

rep
rep
SKIPstart
rep
rep
SKIPstop
rep
rep

new
new
SKIPstart
rep
rep
SKIPstop
new
new

下面是另一个具有多个分隔符的示例:

rep
rep
SKIPstart
rep
rep
SKIPstop
rep
rep
SKIPstart
rep
rep
SKIPstop

new
new
SKIPstart
rep
rep
SKIPstop
new
new
SKIPstart
rep
rep
SKIPstop

和嵌套

rep
rep
SKIPstart
rep
SKIPstart
rep
SKIPstop
rep
SKIPstop
rep
rep

new
new
SKIPstart
rep
SKIPstart
rep
SKIPstop
rep
SKIPstop
new
new

对不起,我不知道lpeg,但您的任务很容易用通常的Lua模式解决
IMO、lpeg或其他外部regex库在大多数情况下都是过度使用的,Lua模式非常好。

local s = [[
rep
rep
SKIPstart
rep
rep
SKIPstop
rep
rep
SKIPstart
rep
SKIPstart
rep
SKIPstop
rep
SKIPstop
rep
rep
]]
s = s:gsub("SKIPstart", "1%0")
:gsub("SKIPstop", "%02")
:gsub("%b12", "%0")
:gsub("(%Z*)%z?(%Z*)%z?",
function(a, b) return a:gsub("rep", "new")..b:gsub("[12]", "") end)
print(s)

输出:

new
new
SKIPstart
rep
rep
SKIPstop
new
new
SKIPstart
rep
SKIPstart
rep
SKIPstop
rep
SKIPstop
new
new

Egor Skcriptunoff的答案是用标准的lua模式来实现目标的一种很好的方法。我同意,如果一个简单的方法可以工作,我不会建议使用LPeg或其他外部库。

当你问到LPeg时,我将向你展示如何使用LPeg。

local re = require('lpeg.re')
local defs = {
do_rep = function(p)
return p:gsub('rep', 'new')
end
}
local pat = re.compile([=[--lpeg
all <- {~ ( (!delimited . [^S]*)+ -> do_rep / delimited )* ~}
delimited <- s (!s !e . / delimited)* e
s <- 'SKIPstart'
e <- 'SKIPstop'
]=], defs)
local s = [[
rep
rep
SKIPstart
rep
rep
SKIPstop
rep
rep
SKIPstart
rep
SKIPstart
rep
SKIPstop
rep
SKIPstop
rep
rep
]]
s = pat:match(s)
print(s)

最新更新