你可以使用这种模式:
(?<=\([^)]*)'<replaced>'(, '<replaced>')*(?=[^)]*\))
如在
var input = "SELECT * FROM blah WHERE ID IN ('<replaced>', '<replaced>', '<replaced>')";
var pattern = @"(?<=\([^)]*)'<replaced>'(, '<replaced>')*(?=[^)]*\))";
var output = Regex.Replace(input, pattern, "<replacedList>");
Console.WriteLine(output); // SELECT * FROM blah WHERE ID IN (<replacedList>)
或者,您可以使用此模式:
(\([^)]*?)(<replaced>(?:, <replaced>)*)([^)]*\))
如:
var input = "SELECT * FROM blah WHERE ID IN ('<replaced>', '<replaced>', '<replaced>')";
var pattern = @"(\([^)]*?)('<replaced>'(?:, '<replaced>')*)([^)]*\))";
var output = Regex.Replace(input, pattern, "$1<replacedList>$3");
Console.WriteLine(output); // SELECT * FROM blah WHERE ID IN (<replacedList>)