lua-users home
lua-l archive

Re: how to translate lua pattern "(.*)and(.*)" with lpeg re ?

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


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.

AltStyle によって変換されたページ (->オリジナル) /