MartinDotNet
MartinDotNet

Reputation: 2505

Regex to match all of a particular repeated word

I have a string that would be something like this:

SELECT * FROM blah WHERE ID IN ('<replaced>', '<replaced>', '<replaced>')

Where the "<replaced>" part is a string and I want to match on everything between ( and ) so I can replace it with "<replacedlist>".

The resultant string would be

SELECT * FROM blah WHERE ID IN (<replacedList>)

I don't want to just do everything inside brackets as this will match other strings, only when the brackets contain "<replaced>" and it's repeated any number of times.

If it's relevant, I'll be using C# to parse it, but I'm not tagging it as this should be general regex.

Upvotes: 3

Views: 180

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149020

You could use this pattern:

(?<=\([^)]*)'<replaced>'(, '<replaced>')*(?=[^)]*\))

As in

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>)

Or alternatively, you can use this pattern:

(\([^)]*?)(<replaced>(?:, <replaced>)*)([^)]*\))

As in:

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>)

Upvotes: 2

Related Questions