0Pricing
Lua Academy · Lesson

Captures and string.match

Extract captured groups from strings using string.match and () captures.

Captures and string.match is a free Lua Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Captures?

Captures are parenthesized sub-patterns in Lua patterns. They extract sub-strings from the matched text.

string.match with Captures

When a pattern contains captures, string.match returns the captured substrings instead of the whole match.

local date = "2025-01-15"
local y, m, d = date:match("(%d+)-(%d+)-(%d+)")
print(y, m, d)  -- 2025  01  15

Single Capture

A single capture returns one string value.

local s = "name: Alice"
local name = s:match("name: (%a+)")
print(name)  -- Alice

Multiple Captures

Multiple captures are returned as multiple values.

local pair = "key = value"
local k, v = pair:match("(%w+)%s*=%s*(%w+)")
print(k, v)  -- key  value

Positional Captures ()

An empty capture () captures the current string position as a number.

local s = "hello world"
local pos = s:match("()world")
print(pos)  -- 7 (position where world starts)

Optional Captures

Capture an optional portion by using ? after the captured group.

local s = "http://example.com"
local protocol, host = s:match("(https?)://([^/]+)")
print(protocol, host)  -- http  example.com

string.find with Captures

string.find with captures returns start, end, then captured substrings.

local s = "phone: 555-1234"
local i, j, num = s:find("phone: (%d+-%d+)")
print(i, j, num)  -- 1  16  555-1234

Nested Captures

Captures can be nested. They are numbered left to right by opening parenthesis.

local s = "2025-01-15"
local full, year = s:match("((%d%d%d%d)-%d%d-%d%d)")
print(full, year)  -- 2025-01-15  2025

Capture Best Practices

Use named-convention variables for clarity. Don't over-nest captures. Match failures return nil — always check before using captures.

local result = s:match("(%d+)")
if result then print("found:", result)
else print("no match") end

Matching Email Addresses

A simple (not RFC-compliant) email pattern with captures.

local email = "user@example.com"
local user, domain = email:match("([^@]+)@(.+)")
print(user, domain)  -- user  example.com

Table of All Matches

To collect all matches into a table use string.gmatch (covered in the next lesson).

Captures Question

What does "2025-01-15":match("(%d+)-(%d+)-(%d+)") return?

Recap: Captures and string.match

Captures use () in patterns to extract sub-strings. string.match returns captured values directly. Empty () captures positions. Always nil-check match results.

Frequently asked questions

Is the “Captures and string.match” lesson free?

Yes — the full text of “Captures and string.match” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “Captures and string.match”?

Extract captured groups from strings using string.match and () captures. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Captures and string.match” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Lua Pattern Syntax
  2. Captures and string.match
  3. string.gmatch for Tokenization
  4. string.gsub for Transformations
← Back to Lua Academy