lua-users home
lua-l archive

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



Le 26 mai 08 à 21:35, Ico Doornekamp a écrit :


* On 2008-05-26 Bertrand Mansion <golgote@mamasam.com> wrote  :

Basically, I have a string that can take these three forms for example:

DATETIME
VARCHAR(10)
FLOAT(10,2)

With perl regex, I could use the ? modifier to have an all in one pattern
like this for example:
'^([A-Z]+)(\(([0-9]+)(,([0-9]+))?\))?$'

With Lua, do I have to write three different patterns or is there a
better solution?

Would this be of any help ?

as = {
  "DATETIME",
  "VARCHAR(10)",
  "FLOAT(10,2)",
}

for _, a in ipairs(as) do
  word, arg1, arg2 = a:match("(%w+)%(?(%d*),?(%d*)%)?")
  print(word, arg1, arg2)
end

This will work for me because my input does not vary much.
Thank you very much for taking the time to post the snippet :)

--
Bertrand Mansion
Mamasam
Work : http://www.mamasam.com
Blog : http://golgote.freeflux.net