Home Explore Blog CI



neovim

65th chunk of `runtime/doc/luaref.txt`
6eb1c1800ec2cd1cb360d9a40b495284ae9ca473072a531e0000000100000fa8
 occurrences of a character in the class;
   - `%n`, for `n` between 1 and 9; such item matches a substring equal to the
      `n` -th captured string (see below);
   - `%bxy`, where `x` and `y` are two distinct characters; such item matches
      strings that start with `x`, end with `y`, and where the `x` and `y`
      are balanced. This means that, if one reads the string from left to
      right, counting `+1` for an `x` and `-1` for a `y`, the ending `y` is the first
      `y` where the count reaches 0. For instance, the item `%b()` matches
      expressions with balanced parentheses.

PATTERN

A pattern is a sequence of pattern items. A `^` at the beginning of a pattern
anchors the match at the beginning of the subject string. A `$` at the end of
a pattern anchors the match at the end of the subject string. At other
positions, `^` and `$` have no special meaning and represent themselves.

CAPTURES                                                        *lua-capture*

A pattern may contain sub-patterns enclosed in parentheses; they describe
captures. When a match succeeds, the substrings of the subject string that
match captures are stored (captured) for future use. Captures are numbered
according to their left parentheses. For instance, in the pattern
`"(a*(.)%w(%s*))"`, the part of the string matching `"a*(.)%w(%s*)"` is stored
as the first capture (and therefore has number 1); the character matching `.`
is captured with number 2, and the part matching `%s*` has number 3.

As a special case, the empty capture `()` captures the current string position
(a number). For instance, if we apply the pattern `"()aa()"` on the
string `"flaaap"`, there will be two captures: 3 and 5.

A pattern cannot contain embedded zeros.  Use `%z` instead.

==============================================================================
5.5  Table Manipulation                                        *lua-lib-table*

This library provides generic functions for table manipulation. It provides
all its functions inside the table `table`.

Most functions in the table library assume that the table represents an array
or a list. For those functions, when we talk about the "length" of a table we
mean the result of the length operator.

table.concat({table} [, {sep} [, {i} [, {j}]]])                 *table.concat()*
        Given an array where all elements are strings or numbers, returns
        `table[i]..sep..table[i+1] ... sep..table[j]`. The default value for
        {sep} is the empty string, the default for {i} is 1, and the default
        for {j} is the length of the table. If {i} is greater than {j},
        returns the empty string.

table.foreach({table}, {f})                                    *table.foreach()*
        Executes the given {f} over all elements of {table}. For each element,
        {f} is called with the index and respective value as arguments. If {f}
        returns a non-`nil` value, then the loop is broken, and this value is
        returned as the final value of `table.foreach`.

        See |next()| for extra information about table traversals.

table.foreachi({table}, {f})                                  *table.foreachi()*
        Executes the given {f} over the numerical indices of {table}. For each
        index, {f} is called with the index and respective value as arguments.
        Indices are visited in sequential order, from 1 to `n`, where `n` is
        the length of the table. If {f} returns a non-`nil` value, then the
        loop is broken and this value is returned as the result of
        `table.foreachi`.

table.insert({table}, [{pos},] {value})                         *table.insert()*
        Inserts element {value} at position {pos} in {table}, shifting up
        other elements to open space, if necessary. The default value for
        {pos} is `n+1`, where `n` is the length of the table (see
        |lua-length|), so that a call `table.insert(t,x)` inserts `x`
        at the end of table `t`.

table.maxn({table})

Title: Lua Patterns: Captures and Table Manipulation Library
Summary
This section describes captures in Lua patterns, where sub-patterns in parentheses are stored for later use, numbered by their left parentheses. It also covers a special case of empty captures `()`. Furthermore, it introduces the `table` library, focusing on functions for table manipulation, like `table.concat`, `table.foreach`, `table.foreachi`, and `table.insert`.