Search code examples
c#regexvisual-studio-2010replacecapturing-group

Visual Studio regex replace repeated capturing group


I'm working on a fairly large project (c#) and from place to place I have snippets that looks like this:

((int)foo).ToString() + "," + ((int)bar).ToString() + "," + ((int)zig).ToString() + ...

The comma delimited string has arbitrary length (average range from 1 to 5, but I'm not sure whether there's more). so far I'm able to find all the occurrences I want using:

\({.+}\).ToString\(\)( *\+ *"," *\+ *\({.+}\).ToString\(\))*

now for each of the occurrence I want to replace it with something like this:

{ (int)foo, (int)bar, (int)zig , ...}

replacing each occurrence manually is not feasible, is there a replace regex that does the job?

Thanks


Solution

  • Yes you can do a find and replace in Visual Studio using regular expressions.

    In a few words you need to use curly braces for tagged expressions and reuse them using \1, \2, etc.