[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Greedy matching with LPEG
- From: Roberto Ierusalimschy <roberto@...>
- Date: Mon, 16 Feb 2009 22:03:58 -0300
> I have a long list of tokens ("foo", "foobar", "fooj"), and I wish to
> construct a pattern that will match the longest of these.
> I can this:
> p = P("foo") + P("foobar") + P("fooj")
> However, if I match "foobars" against this, I get "foo" --- as I'd
> expect, given that + is described as being ordered in the docs.
> How can I do a greedy match rather than an ordered one? That is, I don't
> care about the order; I just want the longest applicable alternative to
> (Yes, I'm aware I can simply change the order of my definition, but it's
> actually quite long and is generated automatically, so I'd rather not do
Exactly because it is generated automatically, shouldn't it be easy to
put the list in proper order? All you have to do is to order the entries by
decreasing token length.