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:

  1. local inputString = "Hello, Lua!": Defines a string variable inputString containing the text "Hello, Lua!".
  2. 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.
  3. local match1, match2 = string.match(inputString, pattern): Uses string.match function to find substrings in inputString that match the defined pattern. It captures two matches using parentheses () and assigns them to match1 and match2.
  4. print(match1): Outputs the value of match1, which is the first captured substring matching the pattern. In this case, it prints "Hello".
  5. print(match2): Outputs the value of match2, which is the second captured substring matching the pattern. In this case, it prints "Lua".