Lua字符串模式 - 更短的代码

3

这是可能的吗:

a=[[do end workspace.Part["Child 1"].Object.child2["thing"]remove() do end]]
a=a:gsub("%.%a+","{F}%0{F}")  
a=a:gsub('(%[%s*([\'"]?).*%2%s*%]):remove%(%)','{F}%1{F}:remove()')
a=a:gsub('{F}%s*{F}','')
a=a:gsub('{F}.-{F}','filterremove(%0)')

Output: do end filterremove(Workspace.Part["Child 1"].Object.child2["thing"]) do end

如何只使用一个gsub函数来得到相同的结果,而不是用两个函数?无论是x.y、x[y]、[x][y]的组合方式都适用。


2
提供示例输入和输出字符串? - hjpotter92
输入:workspace.Part["Child"]:remove(),输出:filterremove(workspace.Part["Child"])。 - Waffle
a=a:gsub("(.*)%:remove%(%)", "filterremove(%1)") <- 运行正常。 - hjpotter92
那将尝试将整个程序直到“remove”的部分放入一个函数中。 - Waffle
1
相关。你不能从这里到那里。 - Eric
显示剩余4条评论
2个回答

1

a:gsub("(%S*%b[]):remove%(%)", "filterremove(%1)")

a:gsub("(%S*%b[]):remove%(%)", "filterremove(%1)")


我知道,但是你需要指定一个你想要看到工作的示例。或者多个示例。一旦有人提出满足您需求的实现,您就会得到所需的内容。 - Paul Kulchenko

1
你至少可以将它串联并换行:

a = [[do end workspace.Part["Child 1"]:remove() do end]]
a = a:gsub("%.%a+","{F}%0{F}")  
     :gsub('(%[%s*([\'"]?).*%2%s*%]):remove%(%)','{F}%1{F}:remove()')
     :gsub('{F}%s*{F}','')
     :gsub('{F}.-{F}','filterremove(%0)')

但实际上,这永远不会奏效。那么问题来了:

workspace.remove(x)
workspace["remove"](x)
getfenv()["work" .. "space"]["re".."move"](x)

1
而关于空格:workspace . Part["..."] :remove();操作者需要做出至少一些假设... - Paul Kulchenko
@Paul:我认为原帖作者不能做出任何假设,因为他们几乎肯定是在尝试沙盒化未知代码。 - Eric
我不知道那个,但如果是这样的话,这些尝试是徒劳无功的。 - Paul Kulchenko
空格不是问题。现在%s*,忘掉它吧。连接和变量,我无法确定是否使用了“remove”,这才是问题所在。 - Waffle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接