Help us shape the future of Go! Take the 2026 Go Developer Survey

regexp

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2025 License: BSD-3-Clause Imports: 2 Imported by: 9

README

regexp GoDoc

Usage

local regexp = require("regexp")
local inspect = require("inspect")

-- regexp.match(regexp, data)
local result, err = regexp.match("hello", "hello world")
if err then error(err) end
if not(result==true) then error("regexp.match()") end

-- regexp.find_all_string_submatch(regexp, data)
local result, err = regexp.find_all_string_submatch("string: '(.*)\\s+(.*)'$", "my string: 'hello world'")
if err then error(err) end
if not(result[1][2] == "hello") then error("not found: "..tostring(result[1][2])) end
if not(result[1][3] == "world") then error("not found: "..tostring(result[1][3])) end

-- regexp:match()
local reg, err = regexp.compile("hello")
if err then error(err) end
local result = reg:match("string: 'hello world'")
if not(result==true) then error("regexp:match()") end

-- regexp:find_all_string_submatch()
local reg, err = regexp.compile("string: '(.*)\\s+(.*)'$")
if err then error(err) end
local result = reg:find_all_string_submatch("string: 'hello world'")
local result = inspect(result, {newline="", indent=""})
if not(result == [[{ { "string: 'hello world'", "hello", "world" } }]]) then error("regexp:find_all_string_submatch()") end

-- regexp.find_all_string_submatch(regexp, data)
local result, err = regexp.find_all_string_submatch("string: '(.*)\\s+(.*)'$", "my string: 'hello world'")
if err then error(err) end
if not(result[1][2] == "hello") then error("not found: "..tostring(result[1][2])) end
if not(result[1][3] == "world") then error("not found: "..tostring(result[1][3])) end

Documentation

Overview

Package regexp implements golang package regexp functionality for lua.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compile

func Compile(L *lua.LState) int

regexp.compile(string) returns (regexp_ud, error)

func FindAllStringSubmatch

func FindAllStringSubmatch(L *lua.LState) int

regexp_ud:find_all_string_submatch(string) returns table of table of strings

Example

regexp_ud:find_all_string_submatch(string)

state := lua.NewState()
Preload(state)
inspect.Preload(state)
source := `
    local regexp = require("regexp")
    local inspect = require("inspect")
    local reg, err = regexp.compile("string: '(.*)\\s+(.*)'$")
    if err then error(err) end
    local result = reg:find_all_string_submatch("string: 'hello world'")
    print(inspect(result, {newline="", indent=""}))
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:
{ { "string: 'hello world'", "hello", "world" } }

func Loader

func Loader(L *lua.LState) int

Loader is the module loader function.

func Match

func Match(L *lua.LState) int

regexp_ud:match(string) returns bool

Example

regexp_ud:match(string)

state := lua.NewState()
Preload(state)
source := `
    local regexp = require("regexp")
    local reg, err = regexp.compile("hello")
    if err then error(err) end
    local result = reg:match("string: 'hello world'")
    print(result)
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:
true

func Preload

func Preload(L *lua.LState)

Preload adds regexp to the given Lua state's package.preload table. After it has been preloaded, it can be loaded using require:

local regexp = require("regexp")

func SimpleFindAllStringSubmatch

func SimpleFindAllStringSubmatch(L *lua.LState) int

regexp.find_all_string_submatch(regular expression string, string) returns (table of table strings, error)

func SimpleMatch

func SimpleMatch(L *lua.LState) int

regexp.match(regular expression string, string) returns (bool, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL