Codectionary / Developer documentation / Luau

Luau pattern-based extraction

Luau string.match supports Lua-style patterns and captures. Patterns are not full regular expressions; explicit anchors make them useful for small structured identifiers.

Syntax

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

Examples

Luau pattern-based extraction

Run as a standalone current Luau script, or as a Script in Roblox Studio ServerScriptService. No Roblox engine APIs are required; type-annotated syntax is not standard Lua.

local function readCode(text: string): number?
    local prefix, digits = text:match("^(%a+)%-(%d+)$")
    if prefix ~= "ITEM" then return nil end
    return tonumber(digits)
end
print(readCode("ITEM-42"))
print(readCode("BAD-42") == nil)
  1. Output: 42, then true. Both the structure and the captured prefix must be accepted.

Best practices

  • Use % escapes and avoid assuming regex alternation or lookaround exists. Check nil captures before numeric conversion.

Runtime & versions

Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Luau documentation

Specifications & further reading