[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Modifier ? in lua pattern matching
- From: Ico Doornekamp <lua@...>
- Date: Mon, 26 May 2008 21:35:21 +0200
* On 2008-05-26 Bertrand Mansion <golgote@mamasam.com> wrote :
> Hi,
>
> About pattern matching, the PiL book says:
>
> "Unlike some other systems, in Lua a modifier can only be applied to a
> character class; there is no way to group patterns under a modifier. For
> instance, there is no pattern that matches an optional word (unless the
> word has only one letter). Usually you can circumvent this limitation
> using some of the advanced techniques that we will see later."
>
> My problem is that I didn't find any examples for that "later" and I
> didn't manage to figure out how to do it myself.
> 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
--
:wq
^X^Cy^K^X^C^C^C^C