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.
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
end
for number in str:gmatch("%d") do
print(number) -- 1, 1, 1, 1 (get individual numbers)
end
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”
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
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