lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On Fri, Nov 14, 2008 at 11:36 AM, Pete Kay <petedao@gmail.com> wrote:
Hi,
 
I am struggling with trying to match regex agains string.  Let's say I have a reg that looks like 4|44|22|445, basically a bunch of arbitrary numbers.  Is that any Lua function that can tell me if a string match exactly one of those numbers?
 
Any suggestion will be great.
 
Thanks,
Pete

I'd do it like this:

function numcheck(test, list)
    test = tostring(test)
    for n in list:gmatch("%d+") do
        if (n == test) then  return true;  end
    end
    return false
end

...unless (a) the lists are particularly long, (b) the same list might get used many times, and (c) performance is potentially an issue, in which case I'd construct and memoize a table instead of using string match functions.

-Duncan