lpeg.Cg
Lua function

lpeg.Cg

Summary

Creates a group capture

Prototype

lpeg.Cg (patt [, name])



Description

It groups all values returned by patt into a single capture. The group may be anonymous (if no name is given) or named with the given name.

An anonymous group serves to join values from several captures into a single capture. A named group has a different behavior. In most situations, a named group returns no values at all. Its values are only relevant for a following back capture or when used inside a table capture.

This example parses a list of name-value pairs and returns a table with those pairs:

lpeg.locale(lpeg)

local space = lpeg.space^0
local name = lpeg.C(lpeg.alpha^1) * space
local sep = lpeg.S(",;") * space
local pair = lpeg.Cg(name * "=" * space * name) * sep^-1
local list = lpeg.Cf(lpeg.Ct("") * pair^0, rawset)
t = list:match("a=b, c = hi; next = pi") --> { a = "b", c = "hi", next = "pi" }

Each pair has the format name = name followed by an optional separator (a comma or a semicolon). The pair pattern encloses the pair in a group pattern, so that the names become the values of a single capture. The list pattern then folds these captures. It starts with an empty table, created by a table capture matching an empty string; then for each capture (a pair of names) it applies rawset over the accumulator (the table) and the capture values (the pair of names). rawset returns the table itself, so the accumulator is always the table.



See Also ...

Topics

DOC_lua_bc Lua bc (big number) functions
DOC_lua_bit Lua bit manipulation functions
DOC_lua_lpeg Lua LPEG library
DOC_lua_package Lua package functions
DOC_lua_rex Lua PCRE regular expression functions
DOC_lua Lua script extensions
DOC_lua_string Lua string functions
DOC_lua_tables Lua table functions
DOC_lua_utils Lua utilities
DOC_regexp Regular Expressions
DOC_scripting Scripting

Lua functions

LUA_lpeg.C lpeg.C (Creates a simple capture)
LUA_lpeg.Carg lpeg.Carg (Creates an argument capture)
LUA_lpeg.Cb lpeg.Cb (Creates a back capture)
LUA_lpeg.Cc lpeg.Cc (Creates a constant capture)
LUA_lpeg.Cf lpeg.Cf (Creates a fold capture)
LUA_lpeg.Cmt lpeg.Cmt (Creates a match-time capture)
LUA_lpeg.Cp lpeg.Cp (Creates a position capture)
LUA_lpeg.Cs lpeg.Cs (Creates a substitution capture)
LUA_lpeg.Ct lpeg.Ct (Creates a table capture)
LUA_lpeg.locale lpeg.locale (Returns a table of patterns matching the current locale)
LUA_lpeg.match lpeg.match (Matches a pattern against a string)
LUA_lpeg.P lpeg.P (Converts a value into a pattern)
LUA_lpeg.print lpeg.print (Outputs debugging information to stdout)
LUA_lpeg.R lpeg.R (Returns a pattern that matches a range of characters)
LUA_lpeg.S lpeg.S (Returns a pattern that matches a set of characters)
LUA_lpeg.type lpeg.type (Tests if a value is a pattern)
LUA_lpeg.V lpeg.V (Creates a non-terminal variable for a grammar)
LUA_lpeg.version lpeg.version (Returns the LPeg version)

(Help topic: lua=lpeg.Cg)

DOC_contents Documentation contents page