0Pricing
Lua Academy · Lesson

string.gmatch for Tokenization

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

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

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