Invalid pattern capture - Gsub

Hey! I’m trying to gsub the brackets away but for some reason it’s not working and it gets me the error in the title.

function reverseInParentheses(inputString)

  --Remove the brackets
  local firstGsub = inputString:gsub("(", "")
  local secondGsub = firstGsub:gsub(")", "")

The inputString is “(bar”)

Edit: Maybe it’s because brackets are used for character matching?
But what should I do then? It needs to remove the ()s and the ()s are not only at the beginning and at the end, sometimes they are in the middle etc

1 Like


You need to escape it. This is easy! Just add a % to it to escape the symbol:

inputString:gsub("%(", ""):gsub("%)", "")

Sorry for nitpicking! But you can as well just do

inputString:gsub("[()]", "") --this is a character set, it removes anything that's either a ( or ), note that you don't need to escape them here

and as well this is possible but don’t really do it

print(inputString:gsub(".", {["("] = "", [")"] = ""})) --"." captures every character in the string, and looks in the dictionary and replaces it with what's corresponding.
1 Like