Reputation: 13
How can I implement vba-like symbol *
in Lua?
I want to compare pattern like "aabb*"
in strings "aaabbb"
(false) or "aabbds"
(true).
Upvotes: 1
Views: 96
Reputation: 974
function string.like(text, pattern)
pattern = "^"..pattern:gsub("*", "\0"):gsub("%p", "%%%0"):gsub("%z", ".-").."$"
return text:find(pattern) and true or false
end
Usage is local bool_result = str:like(pattern)
local pattern = "aabb*"
print(("aaabbb"):like(pattern)) --> false
print(("aabbds"):like(pattern)) --> true
Upvotes: 2