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. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.
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.
Learn Lua with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 40
- Lessons
- 159
Frequently Asked Questions
Is the “Lua Pattern Syntax” lesson free?
Yes — any 3 lessons of the Lua Academy learning path, including “Lua Pattern Syntax”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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.
All lessons in this course
- Lua Pattern Syntax
- Captures and string.match
- string.gmatch for Tokenization
- string.gsub for Transformations