Type Function
Library string.*
Return value String
Revision Current Public Release (2016.2830)
Keywords string, match, find
See also string.gmatch()


Extract substrings by matching a pattern in a string. If a match is found, the captures from the pattern; otherwise it returns nil. If pattern specifies no captures, then the whole match is returned.


string.match( s, pattern [, init] ) 

s:match( pattern [, init] )
s (required)

String. Any string.

pattern (required)

String. Specifies the pattern to match. See Lua String Manipulation.

init (optional)

Number. Number specifying where in s to start the search. The default is 1; can be negative.


print( string.match( "I have 2 questions for you.", "%d+ %a+" ) )  --> 2 questions

print( string.format( "%d, %q", string.match( "I have 2 questions for you.", "(%d+) (%a+)" ) ) )  --> 2, "questions"