0Pricing
Lua Academy · Lesson

string.gmatch for Tokenization

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

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

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.

Frequently asked questions

Is the “string.gmatch for Tokenization” lesson free?

Yes — the full text of “string.gmatch for Tokenization” 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 “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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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