如何在Lua中实现类似vba的符号*
?
我想在字符串"aabb*"
(false)或"aaabbb"
(true)中比较"aabbds"
之类的模式。
答案 0 :(得分:2)
function string.like(text, pattern)
pattern = "^"..pattern:gsub("*", "\0"):gsub("%p", "%%%0"):gsub("%z", ".-").."$"
return text:find(pattern) and true or false
end
用法为local bool_result = str:like(pattern)
local pattern = "aabb*"
print(("aaabbb"):like(pattern)) --> false
print(("aabbds"):like(pattern)) --> true