Click here to Skip to main content
Click here to Skip to main content

Visual Studio Find/Replace Regular Expression Usage

, 22 Mar 2007
Rate this:
Please Sign up or sign in to vote.
Find/Replace text in Visual Studio using regular expressions

Introduction

How do you replace all instances of something like "Id" with "ID" in code files, but only where Id is not part of a larger word such as "Identity" or "Provider"? In a small code base, you may just find each separately and hit replace only on the ones that match your criteria. However, what if there are ~5000 instances in code that have "Id", where ~2000 should not be replaced.

Regular Expressions to the rescue. The Visual Studio 2003/2005 "find in files" dialog lets you find text using regular expressions, and then also replace the matched values with another regular expression.

How Do I Create a New Expression?

In the Visual Studio Find dialog, select the "Use" checkbox, and in the list box to the right select "Regular Expressions". To the right of the "Find what" textbox, there is a button with a flyout menu with many commonly used match symbols. Use this with a combination of a regex cheatsheet such as on regexlib.com to formulate your expression. The same applies to the "replace with" textbox.

When you want to keep a portion of what is matched in your expression in the replacement expression use the "{} tag expression" item with the expression in question highlighted. It will then place the {} braces around that expression in the "Find what" textbox. You can also just type the braces manually. The expression button to the right of the "Replace with" textbox has a corresponding "Tagged Expression x" where X is 1 through 9. When selected, it will place a "\x" (where x is 1-9) in the "Replace with" textbox where the cursor is located.

The order of appearance of {} pairs will match \1, \2, and \3 respectively. The order in which the expressions were tagged does not matter. All the tag expression menu command does is place the {} around the selected expression. There is no hidden meaning applied to each {} pair.

Example Expression

Find:

Id{([^a-zA-Z]|$}

Replace:

ID\1

What Does this Expression Do?

The example above looks for the literal value "Id" with any single character following except upper or lower case a-z. The pipe character '|' means that an alternate value may match instead of the "not alpha" expression. The $ matches the end of a line. Even though a newline character is not an alpha character, it does not match the 'not alpha' portion of the expression. The matched character after the "Id" is marked with tag 1. The replace expression will output the literal "ID" and the value from the tagged expression that was found.

This particular expression should be used only in certain types of code. For instance, don't use it on code that uses an external code that uses the Id casing; especially when that code is evaluated at run-time, such as JavaScript code "getElementById (...)". If it is compiled code, at least the inappropriate ID would be caught at compile time and is easily fixed.

Sample Matches

Find Replace
Id. ID.
Id> ID>
Id< ID<
Id= ID=
Id, ID,
Id) ID)
Id<space> ID<space>
Id<newline> ID<newline>

Non-Matches

Identity, Provider, id.

Resources

  • Find a common expression here [^]
  • Create your own expression with this cheatsheet [^]
  • Besides using the Visual Studio dialog, you can test a regular expression here [^]
  • Here are some other tools you can download to help create and dissect expressions [^]

History

  • 03/22/2007 - Created

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

About the Author

Mike Lang
Web Developer Nexul Software LLC
United States United States
Mike has worked in the .Net platform since the beta 2 release of version 1.0. Before that he worked on VB6 windows forms applications automating other applications such as AutoCAD and "Intent".
 
Mike has released a number of open source applications in javascript and C#.Net. Most of them can be found on github.
github/michael-lang
 
You can find older .Net open source projects on sourceforge at:
http://sourceforge.net/users/versat1474/
 
Mike is currently blogging at candordeveloper.com
Follow on   Twitter

Comments and Discussions

 
Questionwrapping literals with _T() Pinmembervjedlicka26-Sep-13 5:54 
Questiongreat PinmemberMember 1002974018-May-13 2:48 
QuestionA more complicated example PinmemberMike Lang7-Dec-11 6:01 
GeneralThank you for sharing PinmemberSouthmountain19-May-11 14:31 
keep going please...
usability & truth

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 22 Mar 2007
Article Copyright 2007 by Mike Lang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid