String:match()

The match method on a string allows you to match the first occurrence of a pattern you specify and returns the matched pattern. This function uses Lua's built in Pattern Matching to search strings.

local String = "Take control of your data" String:match("control") -- returns "control"

The Colon Operator shorthand passes the String object (the “self“) as the 1st argument.

Optionally, you can use a 3rd argument to set the search start position. This index can be negative to start the search from the end of the string.

local String = "hello Lua user, are you enjoying Lua today?" String:match('Lua%s(%a+)') -- returns "user" String:match('Lua%s(%a+)',20) -- returns "today" String:match('Lua%s(%a+)',-10) -- returns "today"

Test out the match method with different Lua patterns in the Developing in the Translator!

Â