lua-users home
lua-l archive

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

I need to match a fixed number of repetitions of a pattern, so I tried
the multiply_pattern() function listed in However, for me it never
matches. I also tried the function listed in the referenced blog post,
with the same result. Has LPeG changed significantly since this was
written, or am I using it wrong? Here is the code I'm using:

    function multiply_pattern(item, count)
      return lpeg.Cmt(lpeg.P(true), function(s, i)
        local set, offset = {}, i
        for j = 1, count do
          set[j], offset = lpeg.match(item * lpeg.Cp(), s, offset)
          if not offset then
            return false
        return offset, set

    print(lpeg.match(lpeg.C(multiply_pattern('a', 2)), 'aa'))    --> nil