lua-users home
lua-l archive

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


On Thu, Oct 24, 2013 at 2:21 AM, Patrick Donnelly <batrick@batbytes.com> wrote:
> Cb matches the empty string, not the literal value of the capture (from your
> original code). You need to use matchtime captures to solve your problem.
> See the Lua longstring example in the lpeg docs.

Patrick was right. Of course this is no different than the example in
the manual. Again, I took out the unique field names, but here is a
solution that I WOULD show my dear old mum:

```

lpeg = require'lpeg'

P, Cg, Cb, C,  Ct, Cmt = lpeg.P, lpeg.Cg, lpeg.Cb, lpeg.C, lpeg.Ct, lpeg.Cmt

test_pat = "!fs/f!sd/sd!/sd/!sdsf!sd/f"

delims = P(P"!" + P"/")
open_delim = Cg(C(delims), "delim_cap")
repeat_delim = Cmt(Cb("delim_cap")* C(delims),
    function(s,i,a,b)
        return a == b
    end
)

field = C((P(1) - repeat_delim)^0 )
fields = open_delim * field * ( field* delims)^0 * (field + -1)

fields_t = Ct(fields)


for i, v in pairs(fields_t:match(test_pat)) do
print(i,"=",v)
end

```

The trick is that you need to actually CAPTURE the delimiters . Do it
within Cmt, because then "delim_cap" will have been evaluated and you
can return "true", instead of the captured delimiters.

Hopefully this helps. I love LPEG puzzles and I hope I didn't wear you
out with my emails.

-Andrew