Codectionary / Developer documentation / Lua

Lua pattern captures

string.match extracts the first match of a Lua pattern. Parentheses capture portions, while anchors require a complete match. Lua patterns do not provide regular-expression alternation.

Syntax

local name, digits = text:match("^(%a+):(%d+)$")

Examples

Lua pattern captures

Run with standalone Lua 5.4: lua example.lua. These scripts target Lua 5.4 rather than Roblox Luau.

local input = "level:12"
local label, digits = input:match("^(%a+):(%d+)$")
if label then print(label .. "=" .. (tonumber(digits) + 1)) end
  1. Output: level=13. The captures return label and digit strings; malformed input returns nil.

Best practices

  • Anchor validation patterns and convert captured numbers explicitly. Lua uses % escapes rather than regex backslash escapes.

Runtime & versions

Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Lua reference manuals

Specifications & further reading