string.gmatch()

Type Function
Library string.*
Return value pattern-finding iterator
Revision Release 2024.3703
Keywords string, match, patterns, gmatch
See also string.find()
string.gsub()
string.match()

Overview

Returns a pattern-finding iterator. See Lua String Manipulation for more information.

Syntax

string.gmatch( s, pattern )

s:gmatch( pattern )
s (required)

String. The string to be searched.

pattern (required)

String. A string specifying the pattern to match. See Lua String Manipulation for more information.

Example

-- Collect all key-value pairs 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