lpeg.Cb
Creates a back capture
Prototype
lpeg.Cb (name)
Description
This pattern matches the empty string and produces the values produced by the most recent group capture named name.
Most recent means the last complete outermost group capture with the given name. A Complete capture means that the entire pattern corresponding to the capture has matched. An Outermost capture means that the capture is not inside another complete capture.
For example:
open = "[" * lpeg.Cg(lpeg.P"="^0, "init") * "[" * lpeg.P"\n"^-1
close = "]" * lpeg.C(lpeg.P"="^0) * "]"
closeeq = lpeg.Cmt(close * lpeg.Cb("init"), function (s, i, a, b) return a == b end)
string = open * m.C((lpeg.P(1) - closeeq)^0) * close /
function (o, s) return s end
The open pattern matches [=*[, capturing the repetitions of equal signs in a group named init; it also discharges an optional newline, if present. The close pattern matches ]=*]. The closeeq pattern first matches close; then it uses a back capture to recover the capture made by the previous open, which is named init; finally it uses a match-time capture to check whether both captures are equal. The string pattern starts with an open, then it goes as far as possible until matching closeeq, and then matches the final close. The final function capture simply consumes the captures made by open and close and returns only the middle capture, which is the string contents.
Most recent means the last complete outermost group capture with the given name. A Complete capture means that the entire pattern corresponding to the capture has matched. An Outermost capture means that the capture is not inside another complete capture.
For example:
open = "[" * lpeg.Cg(lpeg.P"="^0, "init") * "[" * lpeg.P"\n"^-1
close = "]" * lpeg.C(lpeg.P"="^0) * "]"
closeeq = lpeg.Cmt(close * lpeg.Cb("init"), function (s, i, a, b) return a == b end)
string = open * m.C((lpeg.P(1) - closeeq)^0) * close /
function (o, s) return s end
The open pattern matches [=*[, capturing the repetitions of equal signs in a group named init; it also discharges an optional newline, if present. The close pattern matches ]=*]. The closeeq pattern first matches close; then it uses a back capture to recover the capture made by the previous open, which is named init; finally it uses a match-time capture to check whether both captures are equal. The string pattern starts with an open, then it goes as far as possible until matching closeeq, and then matches the final close. The final function capture simply consumes the captures made by open and close and returns only the middle capture, which is the string contents.
Lua functions
- lpeg.B - Matches patt n characters behind the current position, consuming no input
- lpeg.C - Creates a simple capture
- lpeg.Carg - Creates an argument capture
- lpeg.Cc - Creates a constant capture
- lpeg.Cf - Creates a fold capture
- lpeg.Cg - Creates a group capture
- lpeg.Cmt - Creates a match-time capture
- lpeg.Cp - Creates a position capture
- lpeg.Cs - Creates a substitution capture
- lpeg.Ct - Creates a table capture
- lpeg.locale - Returns a table of patterns matching the current locale
- lpeg.match - Matches a pattern against a string
- lpeg.P - Converts a value into a pattern
- lpeg.print - Outputs debugging information to stdout
- lpeg.R - Returns a pattern that matches a range of characters
- lpeg.S - Returns a pattern that matches a set of characters
- lpeg.setmaxstack - Sets the maximum size for the backtrack stack
- lpeg.type - Tests if a value is a pattern
- lpeg.V - Creates a non-terminal variable for a grammar
- lpeg.version - Returns the LPeg version