Revision as of 04:40, 24 February 2021 by MoKaLux (talk | contribs) (remove language stuff)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Available since: Gideros 2011.6
Class: string


Returns an iterator function that, each time it is called, returns the next captures from pattern pat over string s. If pattern specifies no captures, then the whole match is produced in each call.

(function) = string.gmatch(s,pat)


s: (string) string where to look for patterns
pat: (string) pattern to look for

Return values

Returns (function) iterator function


The following loop will iterate over all the words from string s, printing one per line

s = "hello world from Lua"
for w in string.gmatch(s, "%a+") do

The next example collects all pairs key=value from the given string into a table

t = {}
s = "from=world, to=Lua"
for k, v in string.gmatch(s, "(%w+)=(%w+)") do
   t[k] = v

For this function, a '^' at the start of a pattern does not work as an anchor, as this would prevent the iteration.