[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua match regex
- From: Rob Kendrick <lua-l@...>
- Date: Fri, 14 Nov 2008 11:45:13 +0000
On Fri, 14 Nov 2008 19:36:23 +0800
"Pete Kay" <petedao@gmail.com> wrote:
> 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.
You have a number represented as a string, and you want to check if
it's in a list of numbers? Try this:
valid = { [4] = true, [44] = true, [22] = true, [445] = true }
if valid[tonumber(mystring)] then
-- number is in the valid list
end
One can of course write a little helper function to make building the
valid table easier. Perhaps something like this;
function build_valid(...)
local r = { }
for _, v in ipairs { ... } do
r[v] = true
end
return r
end
valid = build_valid(4, 44, 22, 445)
B.