Click here to Skip to main content
14,693,599 members
Articles » Platforms, Frameworks & Libraries » .NET Framework » General
Posted 2 Feb 2006


19 bookmarked

Replace text using tagged expressions

Rate me:
Please Sign up or sign in to vote.
3.91/5 (5 votes)
2 Feb 2006
An article on how to replace text using regular expressions, in the VS.NET IDE.


Visual Studio has a very powerful search and replace feature, which is the "tagged expression". You can search and replace using regular expressions, keeping groups of content intact. This can be useful in stuff like refactoring or simply reformatting code.

The basics

A tagged expression is used to identify a piece of text so it can be used in the replacement. You identify a tagged expression with "{" and "}". You can use as many of those as you want.

If you enter the following in the replace dialog:

Find what:

{ content }

Replace with:


You are replacing the group containing "content" with the same.

The following entry will replace the selection with the same:

Find what:


Replace with:


Just to be clear, a picture of the dialog:

Image 1

So what?

OK, I admit, so far it doesn't look very useful. However, from this starting point, you can use any expression to split up text in parts and reassemble them.

Here's a hopefully more interesting example of what you can do:

Given below is an SQL connection string property value after dragging a data source in the designer:

workstation id=LEBOWSKI;packet size=4096;
   user id=sa;data source=LEBOWSKI;
   persist security info=True;
   initial catalog=TheInternet;password=protected

If I replace:

Find what:


Replace with:

options.Add("\1", "\2");\n

You'll get the following:

options.Add("workstation id", "LEBOWSKI");
options.Add("packet size", "4096");
options.Add("user id", "sa");
options.Add("data source", "LEBOWSKI");
options.Add("persist security info", "True");
options.Add("initial catalog", "TheInternet");
options.Add("password", "protected");

Note: "[^=]" means any character except "=", and "*" means as many as there are.

Final words

This may not be a sample you will use yourself, but I hope to have shown you the power of what you can do. And hopefully you'll start using it yourself.

For an overview of the expression syntax, click on the Help button in the Replace dialog. Thanks for reading!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United States United States
No Biography provided

Comments and Discussions

Generalhi Pin
Mubi | www.mrmubi.com24-Feb-06 3:01
professionalMubi | www.mrmubi.com24-Feb-06 3:01 
GeneralRe: hi Pin
xyzzer3-Mar-06 0:13
Memberxyzzer3-Mar-06 0:13 
GeneralThanks and Regex comment Pin
larbs15-Feb-06 6:20
Memberlarbs15-Feb-06 6:20 
Thanks for pointing this out. I am pretty familiar Regex but did not realize you could do this with vstudio. I would take this over Macros any day of the week.... It is odd how few folks use regex condering how powerful it is for BS day to day reformatting etc.

Also, not that this was the point of your article at all, but this regex
(ie minimal matching)
is probably a lot less expensive than the [^=]*.... version
Not important in a quick find and replace, but could make a big difference in an app.
GeneralRe: Thanks and Regex comment Pin
msmits19-Feb-06 23:47
Membermsmits19-Feb-06 23:47 
GeneralI prefer macros Pin
xyzzer9-Feb-06 1:18
Memberxyzzer9-Feb-06 1:18 
GeneralRe: I prefer macros Pin
msmits9-Feb-06 2:56
Membermsmits9-Feb-06 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.