lua-users home
lua-l archive

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


On Jan 20, 2018, at 9:51 PM, Nick Gammon <nick@gammon.com.au> wrote:

> 
> 
> On 21/01/18 13:33, Sean Conner wrote:
>> I'm thinking the input is something like:
>>    this and that and the other
> 
> Yes, I can reproduce the issue with "foo and bar and whatever" as I get a match of:
> 
> 1 = foo
> 2 =bar and whatever
> 
> I can't for the life of me work out how to match up the *last* and (which would be the greedy match).
> 
> A pattern like:  { (!'and' .)*} 'and' {.*}
> 
> ... consumes up to the first 'and'. I need to somehow say "keep consuming until 'and' doesn't appear any more".
> 
> - Nick
> 

i am close to solve the problem, by backtrack from the end of string

pat = re.compile "g <- . g / 'and'{}"     -- position capture last and
i = pat:match(s)                                 -- assumed i not nil

pieces (before and after 'and') = s:sub(1, i-4), s:sub(i)