[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: how to translate lua pattern "(.*)and(.*)" with lpeg re ?
- From: Albert Chan <albertmcchan@...>
- Date: Fri, 16 Feb 2018 12:28:22 -0500
> On Feb 16, 2018, at 9:50 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>
> 2018-02-16 16:09 GMT+02:00 albertmcchan <albertmcchan@yahoo.com>:
>>
>>> On Feb 16, 2018, at 8:04 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>>>
>>> 2018-02-16 14:13 GMT+02:00 albertmcchan <albertmcchan@yahoo.com>:
>>>> I post this about a month ago. I've found a simple lpeg re pattern for above
>>>>
>>>> It can handle "repeated" separactor too, say, "(.*)andand(.*) too.
>>>>
>>>> pat = re.compile( "{ (.(g <- &%z / .g))* } %z {.*}", { z = 'and' } )
>>>>
>>>> note: the loop never match the first position, but it is ok
>>>> the second %z take care of of text with %z up front.
>>>
>>> Just remind me ... since `lpeg` and `re` are packages you need
>>> to load into Lua, and standard Lua comes with a string library,
>>> why on earth would you want to do things in `re` that Lua can
>>> already do very well without it?
>>
>> you are right.
>>
>> If all I wanted were captures of "(.*)and(.*)", I would use string.match
>> I had picked the simple pattern to learn about lpeg matching.
>>
>> 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(.*)$"