string.match
Example
local toMatch = "this is a sample text"
print( string.match( toMatch, "sample" ) )
-- patterns work
print( string.match( toMatch, "^[a-z]" ) )
print( string.match( toMatch, "^this" ) )
print( string.match( toMatch, "^..is" ) )
print( string.match( toMatch, "text$" ) )
-- entire string
print( string.match( toMatch, "^.*$" ) )
-- multiple return values
print( string.match( toMatch, "(this) is a (%w+)" ) )
-- nil
print( string.match( toMatch, "this$" ) )
print( string.match( toMatch, "nil" ) )
Output:
sample
t
this
this
text
this is a sample text
this sample
nil
nil