Lua:具有修饰符的%b [英] Lua: %b with modifiers
本文介绍了Lua:具有修饰符的%b的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试匹配包含Lua代码的字符串.
I'm trying to match a string that contains Lua code.
a = [[workspace.Object["Child"]:remove()]]
为此,我试图创建一个选项,其中.x
或['x']
都将被匹配,而不管它们按什么顺序或有多少.
to do this, I'm trying to create an option where either .x
or ['x']
would be matched regardless of what order they are in or how many of them there are.
我遇到了几个问题:
- 如何在方括号之间匹配多个组合字符/图案?
[abc]
匹配a
或b
或c
,但不匹配abc
. - 如何为
%b[]
添加修饰符?前任.%b[]+
匹配['x']['x']['x']
- 如果我可以匹配
%[.-%] *
之类的东西,那将同样起作用.
- How do I match more than one combined character/pattern between brackets?
[abc]
matchesa
orb
orc
, but notabc
. - How do I add a modifier to
%b[]
? ex.%b[]+
to match['x']['x']['x']
- If I could match something like
%[.-%] *
, that would work the same.
推荐答案
Lua不完全支持正则表达式.
但是,您可以使用中间字符串逐步完成任务.
Lua does not fully support regexps.
But you can do your task step-by-step, using intermediate strings.
local str0 = [[workspace.Object["Child"]['xx'][5].xxx:remove()]]
local str = str0
:gsub('%b[]',
function(s)
return s:gsub('^%[%s*([\'"]?)(.*)%1%s*%]$','{%2}')
end
)
:gsub('[%.:]%s*([%w_]+)','{%1}')
print(str0)
print(str)
print()
for w in str:gmatch'{(.-)}' do
print(w)
end
---------------------------
-- output
---------------------------
workspace.Object["Child"]['xx'][5].xxx:remove()
workspace{Object}{Child}{xx}{5}{xxx}{remove}()
Object
Child
xx
5
xxx
remove
local str0 = [[workspace.Object["Child"]['xx'][5][ [=[xxx]=] ]:remove()]]
local str = str0
:gsub('%b[]',
function(s)
return s:gsub('^%[%s*([\'"]?).*%1%s*%]$','{%0}')
end
)
:gsub('%.%s*[%w_]+','{%0}')
:gsub(':%s*[%w_]+%s*([\'"]).-%1','{%0}')
:gsub(':%s*[%w_]+%s*%b()','{%0}')
:gsub('{(:%s*remove%s*%(%s*%))}','%1')
:gsub('}%s*{', '')
:gsub('([%w_]+)%s*(%b{})%s*:%s*remove%s*%(%s*%)',
function(s1, s2)
return 'removefilter('..s1..s2:match'^{(.*)}$'..')'
end
)
:gsub('([%w_]+)%s*:%s*remove%s*%(%s*%)','removefilter(%1)')
:gsub('[{}]', '')
print(str0)
print(str)
---------------------------
-- output
---------------------------
workspace.Object["Child"]['xx'][5][ [=[xxx]=] ]:remove()
removefilter(workspace.Object["Child"]['xx'][5][ [=[xxx]=] ])
这篇关于Lua:具有修饰符的%b的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文