lua-users home
lua-l archive

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


Hey Sean, 

Thanx again for the lpeg lesson....to build on this a bit more, what I really have is a tab delimited line where I want to check the given line to see if it matches my given pattern.  For example, the line  below would pass because of the disease_show/388////

9444f850ff0c10a862b4a6a9c4ab0a74 disease_show disease_show/388//// 4.5

but this line would not

9444f850ff0c10a862b4a6a9c4ab0a74 disease_show disease_show/388/description/// 4.5


What would you recommend the best way to go about this would be? it seems that I need to consume all the text/numbers, and tabs prior to the disease_show/388//// element.  If the given line does pass the test, I do some additional processing of the line, otherwise I skip it and move onto the next.....

Regards,

Dano





On Fri, Mar 22, 2013 at 7:52 PM, Sean Conner <sean@conman.org> wrote:
It was thus said that the Great dan young once stated:
> Hello all,
>
> FIrst of all Lua is awesome!  I'm a fairly newbie @ Lua and a total newbie
> @ LPeg.
>
> I'm trying to figure out how to do the following. FOr example, I have the
> following strings, and I only want to match strings a and e.
>
> a = 'disease_show/881/////'
> b = 'disease_show/266/tests///'
> c = 'disease_show/191/description////'
> d = 'disease_show//description////'
> e = 'disease_show/881/'
>
>
> I don't know how to exclude anything that might have letters after the
> digits and the slash (i.e... b and c)
>
> disease_match = (P'disease_show/'*R'09'^1*P'/'^0)
>
> > = disease_match:match(a)
> 22
> > return  disease_match:match(b)
> 18
> > return  disease_match:match(c)
> 18
> > return  disease_match:match(d)
> nil
> > return  disease_match:match(e)
> 18

  Lines a, b, c, and e match the pattern given.  What LPeg is returning is
the first position that doesn't match in the string (this is so the rest can
be passed on to other patterns).  What I think you want is:

        disease_match = (P'disease_show/'*R'09'^1*P'/'^0 * -P(1))

That final "-P(1)" matches the end of the string (i.e. there is no more
input to match).

  -spc (Hope this helps)