Search code examples
lualua-patterns

Trying to check if a string contains a given word


function msgcontains(msg, what)
    msg = msg:lower()

    -- Should be replaced by a more complete parser
    if type(what) == "string" and string.find(what, "|", 1, true) ~= nil then
        what = what:explode("|")
    end

    -- Check recursively if what is a table
    if type(what) == "table" then
        for _, v in ipairs(what) do
            if msgcontains(msg, v) then
                return true
            end
        end
        return false
    end

    what = string.gsub(what, "[%%%^%$%(%)%.%[%]%*%+%-%?]", function(s) return "%" .. s end)
    return string.match(msg, what) ~= nil
end

This function is used on a RPG server, basically I'm trying to match what the player says

e.g; if msgcontains(msg, "hi") then

msg = the message the player sent

However, it's matching anything like "yesimstupidhi", it really shouldn't match it because "hi" isn't a single word, any ideas what can I do? T_T


Solution

  • You can use a trick mentioned by Egor in his comment, namely: add some non-word characters to the input string, and then enclose the regex with non-letter %A (or non-alphanumeric with %W if you want to disallow digits, too).

    So, use

    return string.match(' '..msg..' ', '%A'..what..'%A') ~= nil
    

    or

    return string.match(' '..msg..' ', '%W'..what..'%W') ~= nil
    

    This code:

    --This will print "yes im stupid hi" since "yes" is a whole word
    msg = "yes im stupid hi"
    if msgcontains(msg, "yes") then
        print(msg)
    end
    --This will not print anything
    msg = "yesim stupid hi"
    if msgcontains(msg, "yes") then
        print(msg)
    end
    

    Here is a CodingGround demo