string.gmatch

Iterate over a string

Prototype

it = string.gmatch (str, pattern)

Description

Returns an iterator function for returning the next capture from a pattern over a string. If there is no capture, the whole match is produced.

See string.find for an explanation of regular expressions.

for w in string.gmatch ("nick takes a stroll", "%a+") do
  print (w)
end -- for

--> 

nick
takes
a
stroll

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

Lua functions

Topics