Your question is a bit cofusing, but to replace ,')
you don't need escapes at all. Simply use
def value = "('cat','rat',',')";
println value.replace(",')", ")"); // ('cat','rat',')
However, I think you rather want this result ('cat','rat')
. Right?
If so, you can use the following code, using Pattern:
import java.util.regex.Pattern
def value = "('cat','rat',',')";
def pattern = Pattern.compile(",'\\)");
def matcher = pattern.matcher(value);
while (matcher.find()) {
value = matcher.replaceAll(")");
matcher = pattern.matcher(value);
}
println value; // ('cat','rat')
Explanation:
You are creating the second replaceable text with your regex, it's not there when you try to replace it, but get's created as a result of the first replacement. So we create a new matcher in the loop and let it find the string again...