Bernardo Meurer
Bernardo Meurer

Reputation: 2355

Lua word search

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)?

Upvotes: 2

Views: 568

Answers (2)

wqw
wqw

Reputation: 11991

A late answer but you can construct a pattern to match all words case-insensitively (only if not followed by an alphanum), capture match position in subject and word index that is being matched with something like this:

local lpeg = require("lpeg")

local function find_words(subj, words)
    local patt
    for idx, word in ipairs(words) do
        word = lpeg.P(word:upper()) * lpeg.Cc(idx)
        patt = patt and (patt + word) or word
    end
    local locale = lpeg.locale()
    patt = lpeg.P{ lpeg.Cp() * patt * (1 - locale.alnum) + 1 * lpeg.V(1) }
    return patt:match(subj:upper())
end

local words = { "MUFFIN", "MOOPHIN", "MUPHEN", "M0FF1N" }
local pos, idx = find_words("aaaaa  bbb ccc muPHEN ddd", words)

-- output: 16, 3

Upvotes: 0

Rena
Rena

Reputation: 646

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

Upvotes: 3

Related Questions