Difference between revisions of "String.gmatch"
From OpenEUO
(Created page with " string.gmatch (s, pattern) Returns an iterator function that, each time it is called, returns the next captures from pattern over string s. If pattern specifies no captures, the...") |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 18: | Line 18: | ||
For this function, a '^' at the start of a pattern does not work as an anchor, as this would prevent the iteration. | For this function, a '^' at the start of a pattern does not work as an anchor, as this would prevent the iteration. | ||
+ | |||
+ | [[String.find]] | ||
+ | |||
+ | [[String.match]] | ||
+ | |||
+ | [[String.sub]] | ||
+ | |||
+ | [http://www.lua.org/manual/5.1/manual.html#5.4.1 Lua Manual - String Patterns] |
Latest revision as of 12:57, 9 October 2010
string.gmatch (s, pattern)
Returns an iterator function that, each time it is called, returns the next captures from pattern over string s. If pattern specifies no captures, then the whole match is produced in each call.
As an example, the following loop
s = "hello world from Lua" for w in string.gmatch(s, "%a+") do print(w) end
will iterate over all the words from string s, printing one per line. 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 end
For this function, a '^' at the start of a pattern does not work as an anchor, as this would prevent the iteration.