如果字符串不在某个起始和结束分隔符之间,我想了解如何使用lpeg
来替换字符串。下面是一个示例,我想使用SKIPstart
和SKIPstop
来表示不应该替换文本的位置。
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", "