How would I go about doing a multiple pattern search in Lua? (I have Lpeg set up).
For example, say I'm receiving strings in a row, I'm processing one at a time, captalizing them and calling them msg
. Now I want to get msg
and check if it has any of the following patterns: MUFFIN MOOPHIN MUPHEN M0FF1N
for a start. How can I check if msg
has any of those (doesn't matter if it's more than one) whithout having to write a huge if(or or or or)
?
One thing you could do is make a table of words you want to look for, then use gmatch
to iterate each word in the string and check if it's in that table.
#!/usr/bin/env lua
function matchAny(str, pats)
for w in str:gmatch('%S+') do
if pats[w] then
return true
end
end
return false
end
pats = {
['MUFFIN'] = true,
['MOOPHIN'] = true,
['MUPHEN'] = true,
['M0FF1N'] = true,
}
print(matchAny("I want a MUFFIN", pats)) -- true
print(matchAny("I want more MUFFINs", pats)) -- false