Lua Pattern Syntax
Learn character classes %d, %a, %s, anchors, and quantifiers in Lua patterns.
Lua Pattern Syntax is a free Lua Academy lesson on CoddyKit — lesson 1 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.
Lua Patterns vs Regex
Lua uses its own pattern language, not full POSIX regex. It is simpler and faster, but lacks alternation (|) and lookaheads.
Character Classes
%d digit, %a letter, %l lowercase, %u uppercase, %s whitespace, %p punctuation, %w alphanumeric. Uppercase negates: %D = non-digit.
Quantifiers
* zero or more (greedy), + one or more, ? zero or one, - zero or more (lazy/shortest match).
local s = "hello world"
print(s:match("%a+")) -- hello (first word)
print(s:match(".-o")) -- hell (lazy stops early)Anchors
^ anchors to start of string, $ anchors to end.
print(("hello"):match("^h")) -- h
print(("hello"):match("o$")) -- o
print(("hello"):match("^world")) -- nilCharacter Sets with []
Custom character classes: [aeiou] matches any vowel, [^aeiou] matches any non-vowel, [a-z] matches lowercase letters.
local s = "Hello World 123"
print(s:match("[A-Z]")) -- H
print(s:match("[0-9]+")) -- 123Escaping Magic Characters
Magic characters ( ) . % + - * ? [ ^ $ must be escaped with %.
local s = "price: 3.14"
print(s:match("%d+%.%d+")) -- 3.14 (. escaped to match literal dot)The . Wildcard
. matches any single character. Combine with quantifiers: .- is the lazy match-all.
print(("abc123"):match("(.-)%d")) -- abc (lazy, stops at first digit)Frontier Pattern %f
%f[set] matches an empty position between a character not in set and one in set — useful for word boundaries.
local s = "hello world foo"
for w in s:gmatch("%f[%a]%a+") do
print(w) -- hello, world, foo
endstring.find vs string.match
string.find returns start/end indices (+ captures). string.match returns the matched text (or captures) directly.
local s = "date: 2025-01-15"
local i, j = s:find("%d%d%d%d%-%d%d%-%d%d")
print(i, j) -- 7 16
print(s:match("(%d+)-(%d+)-(%d+)")) -- 2025 01 15Balanced Match %b
%bxy matches a balanced pair of characters x and y. For example %b() matches balanced parentheses including nested ones.
local s = "func(a, (b+c), d)"
print(s:match("%b()")) -- (a, (b+c), d)Pattern Limitations
Lua patterns do not support: alternation (a|b), backreferences, lookahead/lookbehind, or named groups. For those, use LPEG or an external regex library.
Pattern Syntax Question
Which pattern matches one or more digits?
Recap: Lua Pattern Syntax
Lua patterns use %d %a %s classes, * + ? - quantifiers, ^ $ anchors, [] sets, and %b for balanced matching. They are efficient but lack regex alternation.
Frequently asked questions
Is the “Lua Pattern Syntax” lesson free?
Yes — the full text of “Lua Pattern Syntax” 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 “Lua Pattern Syntax”?
Learn character classes %d, %a, %s, anchors, and quantifiers in Lua patterns. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Lua Pattern Syntax” 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.