Find and replace ")"

The string “)” isn’t allowed to be .gsub-ed, so I was wondering how to do that. I would like to replace all occurrences of the string “)” in a given string, but it keeps giving me “invalid pattern capture”.

C

I think you just have to escape it by putting a % in front.  ) is a special character, so to use it literally, write %) instead.  See the info here: http://www.lua.org/pil/20.2.html

Hope this helps.

  • Andrew

I’m not getting the error any more, but now it’s not doing anything in a string that has “)” in it.

Any other ideas?

C

Can you post the code you’re using?

  • Andrew

[lua]

local swap={

    ["%)"]=" "

}

local function formatStringWithSwap(str)

    for k, v in pairs(swap) do

        str:gsub(k, v)

    end

    return str

end

print(formatStringWithSwap(“ABCDE)”))

[/lua]

Ok, I think I’ve got it - this works:

[lua]

local swap={

    ["%)"]=" "

}

local function formatStringWithSwap(str)

    for k, v in pairs(swap) do

        str=str:gsub(k, v)

    end

    return str

end

print(formatStringWithSwap(“ABCDE)”))

[/lua]

Glad you’ve got it working.  :-)

Thanks for the tip on escaping!

C

I think you just have to escape it by putting a % in front.  ) is a special character, so to use it literally, write %) instead.  See the info here: http://www.lua.org/pil/20.2.html

Hope this helps.

  • Andrew

I’m not getting the error any more, but now it’s not doing anything in a string that has “)” in it.

Any other ideas?

C

Can you post the code you’re using?

  • Andrew

[lua]

local swap={

    ["%)"]=" "

}

local function formatStringWithSwap(str)

    for k, v in pairs(swap) do

        str:gsub(k, v)

    end

    return str

end

print(formatStringWithSwap(“ABCDE)”))

[/lua]

Ok, I think I’ve got it - this works:

[lua]

local swap={

    ["%)"]=" "

}

local function formatStringWithSwap(str)

    for k, v in pairs(swap) do

        str=str:gsub(k, v)

    end

    return str

end

print(formatStringWithSwap(“ABCDE)”))

[/lua]

Glad you’ve got it working.  :-)

Thanks for the tip on escaping!

C