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

.NET Regular Expressions Find and Replace Add-In for Visual Studio 2008

By , 12 Oct 2009
 
replace_in_files.PNG

Introduction

This add-in is for anyone who wants to use Regular Expressions to find and replace in Visual Studio, but doesn't want to use the strange Regular Expression syntax of the built-in dialog. This add-in provides a new dialog that uses .NET's Regular Expressions to find and replace.

Background

I worked on this a couple of years ago, so I don't remember everything that motivated me to do it. I was tired of the crippled version of Regular Expressions available within the Visual Studio IDE. I found this article about a Visual Studio add-in that sounded good enough, but it didn't work with Visual Studio 2005. My add-in is partly based on that add-in, with several bug fixes and additional features.

The project has now been updated for use in Visual Studio 2008.

Using the Add-In

If you just want to use the add-in, download the add-in files and copy them to your "C:\<user folder>\Visual Studio 2008\AddIns" directory. If you want to mess around with the source (or fix any glaring bugs), download the source and run the project.

To debug the project, move "RegexFindAndReplace - For Testing.AddIn" to "C:\<user folder>\Visual Studio 2008\Addins".

You will also need to update the section that looks like this in RegexFindAndReplace.csproj:

<Content Include="C:\<user folder>
	\Visual Studio 2008\Addins\RegexFindAndReplace - For Testing.AddIn">
<Link>RegexFindAndReplace - For Testing.AddIn</Link>
</Content>

Within "RegexFindAndReplace - For Testing.AddIn", you need to update this section to point to the location of the project:

<Assembly><project directory>\RegexFindAndReplace\bin\RegexFindAndReplace.dll</Assembly>

The following Debug project settings also need to be updated:

  • Start external program: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe
  • Command line arguments: /resetaddin RegexFindAndReplace.Connect
  • Working directory: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\

Once everything is installed, you will find a new entry in the Edit|Find and Replace menu:

menu.png

There is also a keyboard shortcut of Ctrl+Shift+X, for those of you who avoid the mouse when you can.

Points of Interest

This add-in provides most of the functionality as the built-in Find and Replace dialog, with a few additional options. In the Find in Files mode, the results are printed to the Output window:

output.PNG

To the right of the Find or Replace text boxes, there is a drop-down menu that has several common constructs used in Regular Expressions or replacement strings. The captured sub-expressions menu lists all of the back references available, and when one is selected, the corresponding expression is highlighted in blue in the text box:

highlighted_subexpressions.PNG

I have had a lot of fun working on this; please contact me about feature requests or bugs, and I will do my best to make sure this is as useful as possible.

Limitations

Each file that is searched for matches is read into memory; this means that files too large to fit into memory will not be able to be searched with this dialog. The reason for this is that regular expressions can span more than one line. In the future, I may add a check for newline characters in the regular expression to see if the file can be processed one line at a time.

History

  • 13 Jun 2008
    • Original article posted
  • 28 July 2008
    • Improved algorithm for processing files in Find All and Replace All modes
    • Added "Stop" button to enable cancelling a Find All or Replace All operation
  • 5 March 2009
    • Updated the article and project for Visual Studio 2008
    • Fixed the "Object reference not set to an instance of an object." error message with certain types of Visual Studio projects
    • Fixed the way the dialog window is displayed
  • 8 October 2009
    • Added better support for web projects with code-behind files
    • Fixed issue with file encoding with Replace All
  • 12 October 2009 
    • Added use of the CollapsibleGroupBox class described in this article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

jhillman
Software Developer
United States United States
Member
I am a software developer currently working in Salt Lake City, Utah. I work primarily with C# for my job, but I mess around with C, Perl, and Windows PowerShell for fun.

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberVJ Reddy7 May '12 - 7:13 
GeneralMy vote of 5memberProEnggSoft7 Mar '12 - 18:46 
QuestionThanks! Very helpful toolmemberThomas.Mueller629 Nov '11 - 1:25 
GeneralRegex bugsmembernvila14 Sep '10 - 9:34 
GeneralRe: Regex bugsmemberjhillman15 Sep '10 - 5:29 
GeneralRe: Regex bugsmemberkhuongdp26 Jan '11 - 9:37 
GeneralRe: Regex bugsmemberjhillman26 Jan '11 - 10:37 
GeneralMy vote of 5membermsmits19 Aug '10 - 3:06 
QuestionVS2010?memberdigger6916 Apr '10 - 10:54 
AnswerRe: VS2010?memberjhillman16 Apr '10 - 11:21 
GeneralRe: VS2010?memberdigger6916 Apr '10 - 11:28 
GeneralRe: VS2010?memberjhillman16 Apr '10 - 12:12 
GeneralRe: VS2010?memberdigger6916 Apr '10 - 12:15 
GeneralRe: VS2010? [modified]memberdigger699 Nov '10 - 13:36 
GeneralRe: VS2010?memberjhillman10 Nov '10 - 3:46 
Generaldifference between "Replace" & "Replace All"memberVivek Athalye9 Oct '09 - 2:08 
GeneralRe: difference between "Replace" & "Replace All"memberjhillman9 Oct '09 - 6:33 
GeneralRe: difference between "Replace" & "Replace All"memberVivek Athalye11 Oct '09 - 21:04 
GeneralRe: difference between "Replace" & "Replace All"memberjhillman13 Oct '09 - 6:59 
Generalproblem in searching text in .cs (code behind) filesmemberMember 5245386 Oct '09 - 4:39 
GeneralRe: problem in searching text in .cs (code behind) filesmemberjhillman6 Oct '09 - 5:59 
GeneralRe: problem in searching text in .cs (code behind) filesmemberVivek Athalye9 Oct '09 - 0:55 
GeneralRe: problem in searching text in .cs (code behind) filesmemberjhillman9 Oct '09 - 5:01 
GeneralRe: problem in searching text in .cs (code behind) filesmemberVivek Athalye21 Oct '09 - 22:44 
GeneralRe: problem in searching text in .cs (code behind) filesmemberjhillman26 Oct '09 - 10:10 
GeneralFixed an error when searching in Entire Solution in VS2008memberMember 347019017 Sep '09 - 22:37 
GeneralRe: Fixed an error when searching in Entire Solution in VS2008memberjhillman19 Sep '09 - 1:45 
GeneralRe: Fixed an error when searching in Entire Solution in VS2008memberjhillman9 Oct '09 - 6:33 
GeneralUpdate Notificationmemberrbrock038130 Aug '09 - 6:57 
GeneralIt is probably me but...memberhere2play27 Jul '09 - 12:42 
Generalquery regarding multiline regular eexpression.memberSidharthaShenoy10 Mar '09 - 2:25 
GeneralRe: query regarding multiline regular eexpression.memberjhillman10 Mar '09 - 4:14 
GeneralRe: query regarding multiline regular eexpression.memberSidharthaShenoy12 Mar '09 - 6:35 
GeneralRe: query regarding multiline regular eexpression.memberjhillman12 Mar '09 - 10:16 
GeneralRe: query regarding multiline regular eexpression.memberSidharthaShenoy13 Mar '09 - 2:14 
GeneralRe: query regarding multiline regular eexpression.memberjhillman13 Mar '09 - 5:36 
GeneralRe: query regarding multiline regular eexpression.memberSidharthaShenoy20 Mar '09 - 2:34 
GeneralRe: query regarding multiline regular eexpression.memberStone Free4 Aug '09 - 7:44 
GeneralRe: query regarding multiline regular eexpression.memberSidharthaShenoy4 Aug '09 - 10:13 
GeneralRe: query regarding multiline regular eexpression.memberSidharthaShenoy4 Aug '09 - 10:15 
GeneralSweet app, but....memberDiscoJimmy5 Jan '09 - 8:22 
GeneralBe careful with spécial caractersmemberyocec30 Dec '08 - 1:52 
GeneralGreat JobmemberViktar Karpach21 Oct '08 - 10:32 
GeneralFind in Entire Solution is broken and annoyingmemberRob Siklos18 Jul '08 - 5:45 
GeneralRe: Find in Entire Solution is broken and annoyingmemberjhillman19 Jul '08 - 0:53 
GeneralRe: Find in Entire Solution is broken and annoyingmemberjhillman22 Jul '08 - 18:54 
GeneralRe: Find in Entire Solution is broken and annoyingmemberjhillman26 Jul '08 - 2:52 
Generalgood jobmemberaruan11 Jul '08 - 13:59 
GeneralVS 2005 closes when searchingmembermikebk3 Jul '08 - 9:18 
GeneralRe: VS 2005 closes when searchingmemberjhillman3 Jul '08 - 16:50 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 12 Oct 2009
Article Copyright 2008 by jhillman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid