Remove Parenthesis From A String (Quick Question)

I am trying to remove the parenthesis from a string but can’t find a way how to. I’ve tried using this line of code but it doesn’t seem to work.

The line of code:

i = "<Instance> (HelloWorld)"

local getValue= string.gsub(i, "(<Instance>)", "")
print(getValue)

What it returns:

(HelloWorld)

Thank you if you could help :slight_smile:

1 Like
i = "<Instance> (HelloWorld)"

local getValue = string.gsub(i, "%(", "")
getValue = string.gsub(getValue, "%)", "")
print(getValue) -- <Instance> HelloWorld

It’s easiest to do this with two operations conceptually. Though I suppose you could do it with another method as well.

Your problem is you are matching against a capture pattern of <Instance> and then replacing that with "" or in otherwords, no charater, which doesn’t affect parenthesis at all.

Sorry for necro bump but was looking for a similar thing and wanted a way to do this in a singular operation, so I came up with this:

local before = "Hello (world)!"
local after = string.gsub(before, "(%(?)(%)?)", "")
print(after) -- Hello world!
2 Likes

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