Click here to Skip to main content
Licence Public Domain
First Posted 22 Mar 2007
Views 34,395
Bookmarked 12 times

Visual Studio Find/Replace Regular Expression Usage

By Mike Lang | 22 Mar 2007
Find/Replace text in Visual Studio using regular expressions
2 votes, 25.0%
1
2 votes, 25.0%
2

3
1 vote, 12.5%
4
3 votes, 37.5%
5
2.74/5 - 8 votes
μ 2.74, σa 3.16 [?]

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

United States United States

Member
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 based on .net. You can find each of these on sourceforge. The most notable being a provider pattern implementation for .net 1.1 (xqs-provider), a provider based logging component (xqs-logging), and a provider based database neutral data access component (xqs-data). You can find them all on sourceforge at:
http://sourceforge.net/users/versat1474/[^]
 
Mike is currently working on a new social network, http://nexul.com/.[^]

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionA more complicated example PinmemberMike Lang7:01 7 Dec '11  
GeneralThank you for sharing PinmemberSouthmountain15:31 19 May '11  

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.

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