$ cat "

Search & Replace with Visual Studio Regular Expressions


Regular expressions are an awesome tool, but unfortunately every language/environment has its own dialect, including Visual Studio.

Visual Studio uses {} to indicate a group to capture. A captured group can later be referenced by using backslash and the match number (one based).

As an example, let's say you want to do the following replacement:

// Original
var person = new Person("Frank");

// Expected result
var person = new Person { Name = "Frank" };

To achieve this you can use the following search and replace patterns in Visual Studio:

Search pattern: var person = new Person\({:q}\);
Replace pattern: var person = new Person { Name = \1 };

:q is a special Visual Studio regex symbol for matching a quoted string. Another custom VS symbol is :z which matches an integer.

Written by Erik Öjebo 2010-06-24 10:27