lua-users home
lua-l archive

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




On 2018-02-16 06:17 PM, Dirk Laurie wrote:
2018-02-16 19:28 GMT+02:00 Albert Chan <albertmcchan@yahoo.com>:

But ... what if z = re.compile " 'and' %s+ ('possibly' / 'likely' / 'definitely') %s+ " ?
or, possibly even more complicated ?

Above lpeg re pattern can handle it
LPeg without `re` handles it too.

   C, P = lpeg.C, lpeg.P
   kw = P'possibly' + 'likely' + 'definitely'
   (C((1-kw)^0) * kw * (P(-1)/''+C(P(1)^0)))

your lpeg pattern first capture is non-greedy.
it stop on the first kw match (also, P(-1) is unnecessary)
So, if kw = 'and', your lpeg is same as lua pattern "^(.-)and(.*)$"
OK, so the assignment becomes "find the last occurrence of a keyword
and return the strings before and after it." I'm not too proud to
admit that I would do that with a mixture of Lua and LPeg. Too late at
night in my timezone to work out the details.


Now I imagine libraries for lpeg.

Yes, you heard that right.

Lpeg is a VM, why don't we stick a require function on it? ;)

Just things like "pat = split_last(pat)" and stuff. :)

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.