您可以使用这个模式:
(?<=\([^)]*)'<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>)
\(\.\*\)
模式可以吗?即只匹配括号内的所有内容,并将其替换为<replacedList>
。 - Adam Stelmaszczyk()
组合,这些组合不应该被替换。 - Martin