lua-users home
lua-l archive

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


>>>>> "albertmcchan" == albertmcchan  <albertmcchan@yahoo.com> writes:

 albertmcchan> looks like it is not easy to convert lua pattern
 albertmcchan> "(.*)and(.*)" after all my last attempt, re.compile
 albertmcchan> "{(!'and' .)*} 'and' {.*}" was close, but still wrong

 albertmcchan> the re pattern correspond to lua pattern "(.-)and(.*)",
 albertmcchan> not the greedy version.

These work, but I have no idea which is best (I'm still new to lpeg):

{ ((!'and' .) / 'and' &( (!'and' .)* 'and' ))* } 'and' {.*}

or

{ ((!'and' .)+ / 'and' !( (!'and' .)* !. ))* } 'and' {.*}

or

-- any nonempty string not containing 'and'
not_and = (1 - P('and'))^1
-- match 'and' only as long as there's a following 'and' somewhere
and_and_and = P('and') * -(not_and^-1 * -1)
pattern = C((not_and + and_and_and)^0) * P('and') * C(P(1)^0)

-- 
Andrew.