Captures and string.match
Extract captured groups from strings using string.match and () captures.
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 15All lessons in this course
- Lua Pattern Syntax
- Captures and string.match
- string.gmatch for Tokenization
- string.gsub for Transformations