Capturing named pattern inside another pattern

80 views Asked by At

Consider the following lpeg pattern

pat = lpeg.C((lpeg.P " ")^0 * lpeg.C(lpeg.R "az") * lpeg.P ".")
lpeg.match(lpeg.Ct(pat), "    a.")

I would like to capture both the full substring and the letter only as two different named patterns, but I have some difficulties figuring out how this can be done. A named capture doesn't seem to survive an outer capture, e.g.

pat = lpeg.C((lpeg.P " ")^0 * lpeg.Cg(lpeg.C(lpeg.R "az"), "letter") * lpeg.P ".")
lpeg.match(lpeg.Ct(pat), "    a.")

only produces {" a."}, without letter. At the same time I see no obvious way to unpack the two captured items into two separate groups without jumping though some very weird hoops (like capturing them into a named table and then using back captures to extract the individual items...

0

There are 0 answers