lua-users home
lua-l archive

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


Am 23.03.2013 00:55 schröbte dan young:
Hello all,

Hi!


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


If you add a capture to your pattern, you can see exactly which parts of the strings are matched:

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

disease_show/881/////
disease_show/266/
disease_show/191/
nil
disease_show/881/

So it appears in this case you need something that works like '$' for regexes (anchor the pattern at the end of the string), and thats P(-1) or -P(1) in LPeg[1][2]:

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

22
nil
nil
nil
18

  [1]: http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html#op-p
  [2]: http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html#op-unm


Regards,

Dano


HTH,
Philipp