Help understanding Luau string patterns

I’m trying to learn how to use Luau string patterns in Roblox studio.
I’ve been looking on the Roblox documentation about it, but it’s really confusing. I understand the simple “%d+” and etc, but I don’t understand further down the documentation where they start using “?,-,()”.
I’ve looked a little on the developer forum, but all solutions basically tell you to go back to the Roblox documentation.

If somebody has a larger explanation/understanding of this, I really appreciate the help.

1 Like

theres probably a better way to explain it but,
lets say you want to get all numbers in a string

local str = "1+1=11?"

for number in str:gmatch("%d+") do
   print(number) -- 1, 1, 11

for number in str:gmatch("%d") do
   print(number) -- 1, 1, 1, 1 (get individual numbers)

print( str:gsub("%d+") ) -- +=? (removes the numbers)

gmatch is a function that returns an iterator function which then you can get all the matches. and gsub removes all the matches in a string.

%d means, hey get all the numbers in this string!, and the + which is the quantifier, determines do i get more than 1 or just 1?

for the ? quantifier, it means get 0 or more characters, for example string.match("This is a RANDom string", "%a?") returns “RAND”

more information here: Strings | Documentation - Roblox Creator Hub

Thank you for responding so quick!

But the problem is that I don’t really understand these types of patterns: “(%a+)%s?=%s?(%d+)”.

ok lets break it down,

first, we have 2 capturing groups, which is what is returned,

the first one is %a+, which means “An uppercase or lowercase letter”. and the quantifier is +, which means get 1 OR more characters

which is followed by %s?, which is “A space or whitespace character”. which has the ? quantifier, which means get 1 OR less characters

the second capturing group is %d+, which means “Any digit (number)” and followed by the + quantifier

the capturing groups, is what the function returns, in this case it returns the %a+ and the %d+

when we run that pattern in a match function

local str, num = string.match("HelloWorld = 5", "(%a+)%s?=%s?(%d+)")

print(str, num) -- HelloWorld 5

this also matches with:

  • HelloWorld = 5
  • HelloWorld=5
  • HelloWorld= 5
  • HelloWorld=59
  • etc…


Ohhh, I kinda understand now. But what does the %s?=%s? do?

sorry, i mistaken the ? quantifier with *, ? means 1 OR less characters, while * means 0 OR more characters. the %s?=%s? means, is there one or no whitespace between = ?

so these would not match %s?=%s?

"  = " -- two spaces, invalid!
"=   " -- three spaces, invalid!

string patterns seems hard at first, but once you get the hang of it, its gonna make your scripting much easier

OH, Thank you so much! I’ll start using string patterns more!

My question is unrelated, but what is the usage of the - modifier? Because I played around with it a little and it seems to be useless.

1 Like

I’m not sure about that, I’ve never practically used it (as a quantifier), but it really seems to be useless, because it will get no matches, but you can try doing [A-Z] (matches all characters from A to Z), which uses -, but not in a “quantifier” way

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.