[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: LPeg back captures
- From: Andrew Starks <andrew.starks@...>
- Date: Thu, 24 Oct 2013 10:11:09 -0500
On Thu, Oct 24, 2013 at 12:04 AM, Sean Conner <firstname.lastname@example.org> wrote:
> But where delim is defined as the first character of the string (instead of
> the hardcoded pattern I have here). This is harder than it looks.
Just so that I can move on with my life, here is a wicked ugly version
that I would not show my mother:
lpeg = require'lpeg'
P, Cg, Cb, C, R, Ct, Cmt, V = lpeg.P, lpeg.Cg, lpeg.Cb, lpeg.C,
lpeg.R, lpeg.Ct, lpeg.Cmt, lpeg.V
test_pat = "!f/sd!/sdsf!sd/f"
local d = P("#")
delim_char = Cmt((P(P"!" + P"/")) , function(c, p, a, b) print("cmt:
",c,p,a,b );d = P(a); return p end)
delim = Cmt( P(1), function(c,p,a,b) return d:match(a) and true or false end)
field = C((P(1) - delim)^0)
regexp = delim_char * field * (delim * field)^0 * -1
regexp_t = Ct(regexp)
for i, v in pairs(regexp_t:match(test_pat)) do
I took out the field names, only for brevity. I don't think it would
change the results, though.