Lua Academy · Lesson

string.gmatch for Tokenization

Iterate over all matches in a string with string.gmatch.

Lesson 3 of 413 steps

string.gmatch for Tokenization is a free Lua Academy lesson on CoddyKit. This is lesson 3 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.

What Is string.gmatch?

string.gmatch(s, pattern) returns an iterator that successively returns all matches of pattern in string s.

Iterating Over Words

Tokenize a string into words by matching sequences of non-space characters.

local s = "the quick brown fox"
for word in s:gmatch("%S+") do
  print(word)
end

Collecting Matches into a Table

Gather all matches into a table for further processing.

local function split(s, pat)
  local parts = {}
  for token in s:gmatch(pat) do
    parts[#parts+1] = token
  end
  return parts
end
local words = split("one two three", "%S+")
print(#words)  -- 3

Extracting Numbers

Extract all integers from a string with a %d+ pattern.

local s = "x=10, y=20, z=30"
for n in s:gmatch("%d+") do
  print(tonumber(n))
end

Using Captures in gmatch

When the pattern has captures, each iteration returns the captured groups instead of the whole match.

local s = "from=Alice, to=Bob, cc=Carol"
for k, v in s:gmatch("(%w+)=(%w+)") do
  print(k, "->", v)
end

CSV Tokenizer

Split a CSV line by capturing everything between commas.

local csv = "apple,banana,cherry,date"
for field in (csv .. ","):gmatch("([^,]*),") do
  print(field)
end

Key-Value Pair Parsing

Parse an HTTP-style header string into a table of key-value pairs.

local headers = "Content-Type: text/html; Content-Length: 1234"
local parsed = {}
for k, v in headers:gmatch("([%w%-]+): ([^;]+)") do
  parsed[k] = v:match("^%s*(.-)%s*$")
end

Counting Pattern Occurrences

Count how many times a pattern appears in a string.

local function count(s, pat)
  local n = 0
  for _ in s:gmatch(pat) do n = n + 1 end
  return n
end
print(count("banana", "an"))  -- 2

Sentence Tokenizer

Split text into sentences by matching up to punctuation marks.

local text = "Hello. How are you? Fine!"
for sent in text:gmatch("[^.!?]+[.!?]?") do
  print(sent:match("^%s*(.-)%s*$"))
end

gmatch vs string.find in a Loop

gmatch is the preferred way to iterate over all matches. Using string.find in a while loop is more verbose and error-prone.

Unicode Caveat

Lua patterns operate on bytes. Multi-byte UTF-8 characters may not match single-character patterns correctly. For full Unicode support, use a UTF-8 library.

gmatch Question

What does for w in s:gmatch("%S+") do iterate over?

Recap: string.gmatch

string.gmatch is the standard Lua tokenizer. Use it to split strings, extract all numbers, parse key-value pairs, and count pattern occurrences in a clean iterator loop.

Free to start

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 “string.gmatch for Tokenization” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “string.gmatch for Tokenization”, 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 “string.gmatch for Tokenization”?

Iterate over all matches in a string with string.gmatch. 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 3 of 4.

How long does the “string.gmatch for Tokenization” 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