Visual Studio 2012 Find/Replace With Tagged Expressions

If you ever wanted to do some heavy duty find/replace options in VS 2012, you’ll find yourself needing to use the Regular Expressions option.

One of the most useful things you can do with it is called Tagged Expressions, or backreferences.

Unfortunately, nearly all the online documentation on this is completely wrong.

So I’ll just give a little example.  I’m doing some refactoring, and I have lots of blocks of code that look like this:

Using com As New SqlCommand("SomeCommandNameHere", conn)

and of course each line like this has a different command name.  I want to change the code above to look like this:

Using com As System.Data.IDbCommand = dbProvider.CreateDataCommand()
com.CommandText = "SomeCommandNameHere"

So to achieve this, I use the find/replace below.  You can see that I have to escape a number of characters in the FIND and I’m matching the command name with (.*).  The  () wrap each tagged expression, not { } like all the documentation says.  In the bottom, I just reference the result by saying $1 for the first tagged expression (not \1 like msdn says).

image

Advertisement

4 thoughts on “Visual Studio 2012 Find/Replace With Tagged Expressions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s