Lua 不完全支持正则表达式。
但是您可以使用中间字符串逐步完成任务。
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]=] ])