string.match roblox
local inputString = "Hello, Lua!"
local pattern = "(%a+), (%a+)!"
local match1, match2 = string.match(inputString, pattern)
print(match1) -- Output: Hello
print(match2) -- Output: Lua
Explanation:
local inputString = "Hello, Lua!"
: Defines a string variableinputString
containing the text "Hello, Lua!".local pattern = "(%a+), (%a+)!"
: Defines a pattern to match two words separated by a comma and space, followed by an exclamation mark.%a+
matches one or more alphabetical characters.local match1, match2 = string.match(inputString, pattern)
: Usesstring.match
function to find substrings ininputString
that match the defined pattern. It captures two matches using parentheses()
and assigns them tomatch1
andmatch2
.print(match1)
: Outputs the value ofmatch1
, which is the first captured substring matching the pattern. In this case, it prints "Hello".print(match2)
: Outputs the value ofmatch2
, which is the second captured substring matching the pattern. In this case, it prints "Lua".