Need help in string patterns

I want to make a script that can replace Repeated arithmetic operations to only one operation

example :

local s1 = "1---2+++++3-4**5///6^^7"

local s2 = s1:gsub(...)

print(s2) --outputs : 1-2+3-4*5/6^7

I asked ChatGPT but it gives me invalid script

this is it :

local s1 = "1++++----2"
local s2 = s1:gsub("([%+%-%*/])%1+", "%1")
print(s1) -- outputs : 1++----2

if you have any suggestions or solutions, please share them.

Here’s a regex that could fit in gsub that catches repeated non-numeral characters:

(?<=-)%1-|(?<=\+)%1\+|(?<=\*)%1\*|(?<=\/)%1\/|(?<=\^)%1\^
How this works
  • A regular expression is a list of things to match. However, different languages have different RegEx flavors. ChatGPT uses the GoLang flavor which is the most limited, while Roblox uses a C++ version which, while not the best flavor, does support a few crucial features we need.
  • Every vertical bar character ( | ) defines a different target to match. So every arithmetic character gets its own target.
  • Next, we need to find every instances of an arithmatic character. This is done with what I call a matching group, or literally a pair of parenthesis( () ).
  • Now that we are matching characters, we need to find a way to match based on characters already present. Thankfully, C++ and C# both support a match group operator called “lookbehind”. It makes the parser validate characters behind the matching group instead of ahead. This is crucial, since we want to make sure to leave one arithmetic symbol, instead of matching the entire group.
  • Using the knowledge above, we pick an arithmetic symbol, and write a lookbehind matching group to find a matching arithmatic symbol behind it (done with (?<=...).) If a given character has a matching arithmatic symbol already present behind it, match the duplicate symbol.
  • Finally, we can go back to gsub, and tell it to delete the capture group with a simple replacement.

Your valid code:

local s1 = "1++++++2--+^^^3///++4^^5"
local s2 = s1:gsub("(?<=-)%1-|(?<=\+)%1\+|(?<=\*)%1\*|(?<=\/)%1\/|(?<=\^)%1\^", "%1")
print(s2) -- outputs : 1+2-+^3/+4^5

but it didnt work :melting_face:
maybe there is maybe you messed up something.
this is the output :

1++++++2–+^^^3///++4^^5

Lua’s handling of gsub must be very special, then :melting_face:

The example works in a regex lab with any library that supports lookbehind:

In case you don’t figure out a regex solution:

local function removeDuplicateSymbols(s: string, sym: string): string
	local result = ""
	for i, part in pairs(s:split(sym)) do
		if part == "" then continue end 
		if i > 1 then result ..= sym end 
		result ..= part 
	end
	return result
end

local function removeDuplicateOperations(s: string): string
	local operations = {"+", "-", "*", "/", "^"}
	local result = s
	for _, operation in pairs(operations) do
		result = removeDuplicateSymbols(result, operation)
	end
	return result 
end

--Should print "1-2+3-4*5/6^7"
print(removeDuplicateOperations("1---2+++++3-4**5///6^^7"))

Well, i find out through my intelligent that you can use gsub multiple times so i created this simple function :

function Remove_D_Operations(TheString)

	TheString = string.gsub(TheString,"%++","+")
	TheString = string.gsub(TheString,"%-+","-")
	TheString = string.gsub(TheString,"%*+","*")
	TheString = string.gsub(TheString,"%/+","/")
	TheString = string.gsub(TheString,"%^+","^")
	
	return TheString
end

print(Remove_D_Operations(1---2+++++3-4**5///6^^7"))
 --//OutPut : 1-2+3-4*5/6^7

Sometimes you have to think outside the box. :sparkles:

this would work too but gsub is lighter for processing

And gsub uses Lua patterns not ReGex so you cant use that.

maybe Roblox should add ReGex library with a function.

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