my previous lpeg re pattern for greedy search may fail if the pattern is repeated
(I learn this from http://www.inf.puc-rio.br/~roberto/docs/peg.pdf, page 11)
-- greedy search for 'xuxu', my old way will not work
= re.match( 'xuxuxui', "(g <- 'xuxu' / .g)+")
5
-- with known pattern, we can change the search:
= re.match('xuxuxui', "(g <- 'xu'^+2 / .g)+ ")
7
However, it maybe hard to recognize a repeated pattern, say %a%u
And, how to change the search ? Is %a%u+ correct ?
This is Roberto's solution to repeat sub-patterns problem
(the published pattern is flawed, it had * instead of +, which return 1 for no match)
= re.match( 'xuxuxui', "(g <- &'xuxu' . / .g)+")
4
returned position is 1 pass last occurence, but is correct
final position = 4 - 1 + fixedlen('xuxu') = 3 + 4 = 7
I have an improvement to his pattern, without the position arithmetic
(note: the loop never check first position, but it is ok)
= re.match( 'xuxuxui', "(. (g <- &'xuxu' / .g))* 'xuxu' ")
7