Home Explore Blog CI



neovim

44th chunk of `runtime/doc/lua.txt`
b2e1d01cca83549c61248a7144d995ab13f30e6373b9571f0000000100000fb7

(https://www.inf.puc-rio.br/~roberto/lpeg/re.html).



Pattern:match({subject}, {init}, {...})                      *Pattern:match()*
    Matches the given `pattern` against the `subject` string. If the match
    succeeds, returns the index in the subject of the first character after
    the match, or the captured values (if the pattern captured any value). An
    optional numeric argument `init` makes the match start at that position in
    the subject string. As usual in Lua libraries, a negative value counts
    from the end. Unlike typical pattern-matching functions, `match` works
    only in anchored mode; that is, it tries to match the pattern with a
    prefix of the given subject string (at position `init`), not with an
    arbitrary substring of the subject. So, if we want to find a pattern
    anywhere in a string, we must either write a loop in Lua or write a
    pattern that matches anywhere.

    Example: >lua
        local pattern = lpeg.R('az') ^ 1 * -1
        assert(pattern:match('hello') == 6)
        assert(lpeg.match(pattern, 'hello') == 6)
        assert(pattern:match('1 hello') == nil)
<

    Parameters: ~
      • {subject}  (`string`)
      • {init}     (`integer?`)
      • {...}      (`any`)

    Return: ~
        (`any`) ...

vim.lpeg.B({pattern})                                           *vim.lpeg.B()*
    Returns a pattern that matches only if the input string at the current
    position is preceded by `patt`. Pattern `patt` must match only strings
    with some fixed length, and it cannot contain captures. Like the `and`
    predicate, this pattern never consumes any input, independently of success
    or failure.

    Parameters: ~
      • {pattern}  (`vim.lpeg.Pattern|string|integer|boolean|table`)

    Return: ~
        (`vim.lpeg.Pattern`)

vim.lpeg.C({patt})                                              *vim.lpeg.C()*
    Creates a simple capture, which captures the substring of the subject that
    matches `patt`. The captured value is a string. If `patt` has other
    captures, their values are returned after this one.

    Example: >lua
        local function split (s, sep)
          sep = lpeg.P(sep)
          local elem = lpeg.C((1 - sep) ^ 0)
          local p = elem * (sep * elem) ^ 0
          return lpeg.match(p, s)
        end
        local a, b, c = split('a,b,c', ',')
        assert(a == 'a')
        assert(b == 'b')
        assert(c == 'c')
<

    Parameters: ~
      • {patt}  (`vim.lpeg.Pattern|string|integer|boolean|table|function`)

    Return: ~
        (`vim.lpeg.Capture`)

vim.lpeg.Carg({n})                                           *vim.lpeg.Carg()*
    Creates an argument capture. This pattern matches the empty string and
    produces the value given as the nth extra argument given in the call to
    `lpeg.match`.

    Parameters: ~
      • {n}  (`integer`)

    Return: ~
        (`vim.lpeg.Capture`)

vim.lpeg.Cb({name})                                            *vim.lpeg.Cb()*
    Creates a back capture. This pattern matches the empty string and produces
    the values produced by the most recent group capture named `name` (where
    `name` can be any Lua value). 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. In
    the same way that LPeg does not specify when it evaluates captures, it
    does not specify whether it reuses values previously produced by the group
    or re-evaluates them.

    Parameters: ~
      • {name}  (`any`)

    Return: ~
        (`vim.lpeg.Capture`)

vim.lpeg.Cc({...})                                             *vim.lpeg.Cc()*
    Creates a constant capture. This pattern matches the empty string and
    produces all given values as its captured values.

    Parameters: ~
      • {...}  (`any`)

    Return: ~
        (`vim.lpeg.Capture`)

Title: Lua API: More vim.lpeg Functions
Summary
This section details several functions within the `vim.lpeg` Lua library for pattern matching. It explains the usage of `Pattern:match()`, including its parameters and return values. It then describes functions for creating specific LPeg patterns, such as `vim.lpeg.B()` (lookbehind), `vim.lpeg.C()` (simple capture), `vim.lpeg.Carg()` (argument capture), `vim.lpeg.Cb()` (back capture), and `vim.lpeg.Cc()` (constant capture), providing their parameters and return values.