Click here to Skip to main content
15,884,388 members
Articles / Operating Systems / Windows

Visual Studio Find/Replace Regular Expression Usage

Rate me:
Please Sign up or sign in to vote.
3.04/5 (11 votes)
22 Mar 2007Public Domain3 min read 93.4K   14   4
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


Written By
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

Comments and Discussions

 
Questionwrapping literals with _T() Pin
vjedlicka26-Sep-13 5:54
vjedlicka26-Sep-13 5:54 
Questiongreat Pin
Member 1002974018-May-13 2:48
Member 1002974018-May-13 2:48 
QuestionA more complicated example Pin
Mike Lang7-Dec-11 6:01
Mike Lang7-Dec-11 6:01 
GeneralThank you for sharing Pin
Southmountain19-May-11 14:31
Southmountain19-May-11 14:31 

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.