Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

string.gsub parentheses error "Invalid pattern capture?" (Code provided)

Asked by 4 years ago
Edited 4 years ago
var = "(I do not enjoy pineapple pizza.)"
var = string.gsub(var, "(", "")
var = string.gsub(var, ")", "")
print(var)

I want to know how to remove parentheses from a string. The code provided gives error "Invalid pattern capture."

1 answer

Log in to vote
0
Answered by
Elixcore 1337 Moderation Voter
4 years ago
Edited 4 years ago

Hey Shaddy, what you're finding here is lua recognizing that as a magic character.

() are part of the magic characters and have special meaning when used in a pattern, more information can be found here.

In order for you to just get the ( part of the string you want to use % which allows you to escape it from being recognized as a pattern, and just taken as a simple string.

Example:

local var = "(I do not enjoy pineapple pizza.)"
var = string.gsub(var, "%(", "")
var = string.gsub(var, "%)", "")
print(var) --> I do not enjoy pineapple pizza.

Ad

Answer this question