|
p1 = X'(.*)and%s+' * (X'possibly' + 'likely' + 'definitely') * X'%s+(.*)' p1_match = p1:compile() It will fail with text = "x and possibly y and z" It was plain luck that it worked at all. Once you breakup the problem into pieces, string.match loses its ability to backtrack correctly (xpattern examples clearly showed) Mixing lpeg and lua string library could introduce the same type of bug. Plain lpeg re is much better for the job (below without my '>' patch). z = re.compile[[ 'and' %s+ ('possibly' / 'likely' / 'definitely') %s+ ]] p2 = re.compile("{(. (g <- &%z / .g))*} %z {.*}", {z=z}) For any %z match, it set a backtrack mark just before the match, and keep trying until it failed, THEN backtrack back to last match. --> greedy match up to %z (without consuming %z) |