Lua Patterns
Lua doesn't include full regular expressions (they would be larger than the whole interpreter). Instead it has patterns: a smaller, simpler syntax that covers most text-processing jobs. Patterns are used by string.find, string.match, string.gmatch and string.gsub.
local log = "2026-03-14 ERROR disk full on /dev/sda1"
print(log:match("%d+-%d+-%d+")) -- a date
print(log:match("%u+")) -- first run of uppercase letters
print(log:find("disk")) -- start and end position2026-03-14
ERROR
18 21
Character classes
| Class | Matches |
|---|---|
. |
any character |
%a |
letters |
%d |
digits |
%l / %u |
lowercase / uppercase letters |
%s |
whitespace (space, tab, newline…) |
%w |
letters and digits |
%p |
punctuation |
%x |
hexadecimal digits |
%c |
control characters |
%A, %D, %S, … |
the complement (uppercase letter = "not") |
[abc], [a-z], [%w_] |
a set; [^…] is the complement |
%., %%, %- |
a literal ., %, - (% escapes magic characters) |
The magic characters are ^ $ ( ) % . [ ] * + - ?. Lua uses % to escape them, not the backslash.
Repetition
| Quantifier | Meaning |
|---|---|
* |
0 or more, as many as possible (greedy) |
+ |
1 or more, greedy |
- |
0 or more, as few as possible (lazy) |
? |
optional (0 or 1) |
Anchors: ^ at the start of the pattern matches the start of the string, $ at the end matches the end.
local html = "<b>bold</b> and <i>italic</i>"
print(html:match("<.*>")) -- greedy: as much as possible
print(html:match("<.->")) -- lazy: as little as possible
print((" padded "):match("^%s*(.-)%s*quot;))
print(("v1.2"):match("^v%d+%.%d+quot;), ("v12"):match("^v%d+%.%d+quot;))
print(("colour color"):gsub("colou?r", "hue"))<b>bold</b> and <i>italic</i>
<b>
padded
v1.2 nil
hue hue 2
Captures
Parentheses capture parts of the match. match returns the captures (instead of the whole match):
local date = "2026-03-14"
local y, m, d = date:match("(%d+)-(%d+)-(%d+)")
print(y, m, d, math.type(tonumber(m)))
local key, value = ("timeout = 30"):match("^(%w+)%s*=%s*(%w+)quot;)
print(key, value)
-- empty capture () gives a position
print(("hello world"):match("()o"))2026 03 14 integer
timeout 30
5
Captures are always strings (or positions); convert with tonumber as needed.
gmatch: all matches
gmatch returns an iterator over every match. With captures, each step yields the captures:
local query = "name=Ana&age=31&city=Porto"
local params = {}
for k, v in query:gmatch("(%w+)=(%w+)") do
params[k] = v
print(k, v)
end
print(params.city)
local count = 0
for _ in ("one two three four"):gmatch("%S+") do count = count + 1 end
print("words:", count)name Ana
age 31
city Porto
Porto
words: 4
gsub: replacing
The replacement can be a string (with %1, %2 … for captures and %0 for the whole match), a table (the match is the key) or a function:
-- string with captures: swap first and last name
print(("Lovelace, Ada"):gsub("(%w+), (%w+)", "%2 %1"))
-- table: look up each $word
local vars = { user = "kim", home = "/home/kim" }
print(("$user lives in $home ($shell)"):gsub("%$(%w+)", vars))
-- function: double every number
print(("3 apples and 12 pears"):gsub("%d+", function(n) return n * 2 end))Ada Lovelace 1
kim lives in /home/kim ($shell) 3
6 apples and 24 pears 2
Missing table keys (like shell) leave the match unchanged, but it still counts as a match: the count is 3.
Special items
| Item | Meaning |
|---|---|
%b() |
a balanced pair: ( … ) with nested pairs inside |
%f[set] |
frontier: an empty match where the previous character is not in set and the next one is |
%1 (in a pattern) |
the text matched by capture 1 again |
print(("f(a, g(b, c)) + 1"):match("%b()"))
print(("THE (quick) fox"):gsub("%f[%a]%a+", function(w) return w:lower() end))
print(("say 'hi' or \"bye\""):match("([\"'])(.-)%1"))(a, g(b, c))
the (quick) fox 3
' hi
Patterns vs regular expressions
Lua patterns have no alternation (a|b), no quantifiers on groups ((ab)+) and no {n,m} counts. When you need those, split the job into several patterns or use a library such as LPeg. For most parsing of logs, configuration and simple formats, patterns are enough.
Test yourself
How do you match a literal dot in a Lua pattern?
\.%.[.]only..
What does ("<a><b>"):match("<.->") return?
<a><b><a>anil
What does %d+ match?
- Exactly one digit
- One or more digits
- Any number including decimals
- The letter d