Visual Studio Find/Replace Regular Expression Usage






3.04/5 (11 votes)
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