.NET Framework Bookmark and Share   
 index > Regular Expressions > Visual Studio 200X (IDE) - Regular Expressions
 

Visual Studio 200X (IDE) - Regular Expressions


The regular expressions in Visual Studio 2003/2005/2008 are not the same as .Net Regular Expressions found in code. For that resources check out Regular Expression Resource.



If you are reading this you no doubt have questions about Visual Studio's version of Regular Expressions which is totally different from .Net Regular Expressions or any other flavor.

The differences between .Net and Studio's version are best summed up here by Jeff Atwood's Coding Horror: Visual Studio IDE and Regular Expression.

Of course to do any search or replacing, one will need to use the syntax. That is provided here Regular Expressions Visual Studio.

Here are two examples and notes to do a search and replace that I have in my notes which I will share with you.



I ran into a situation where I had to refactor text within a few documents. I had never used the Regular expressions in studio during search and replace and decided to learn something new. I had in the text files text that had a pattern like this:

return data["COVERAGE_ELECT"]

and needed to change each similar occurance to this:

return ProcessDataString("COVERAGE_ELECT");

Here is the search text I created which specified that the quoted string would be saved into a Visual Studio set (as seen in the {} ) which would create a match that would later be accessed in the replacement text by the Visual Studio replace \1:

Visual Studio Search & Replace Dialog
Find What: return data\[{["A-Z_]*}\]
Replace with: return ProcessDataString(\1)

At that point I could search the file and replace at will.

Next Replace:

Change p-nikko-copy-cp2 to p-nikko-copy2-cp but also allow for other numbers. This was basically moving the digit at the end to the previous section.

Here is the replacement.
Visual Studio Search & Replace Dialog
Find: copy-cp{:z}
Replace with: copy\1-cp


That found over 18 occurances and moved a number into the previous sections location.



Notes
  • Some regular expression syntax used by studio is not the same as the standard Regular Expression, such as usage of :a instead of \w or :z for \d.
  • \1 through \9 matches can be parsed or use of \0 for all matches.
  • Must check Use: Regular Expressions in the dialog.
Happy Hunting


Links

OmegaMan

I'd like to suggest my recently released tool Regent. It automatically creates search and replace regular expression from text example and has explicit support for Visual Studio's version of regular expressions.

Sergey Vlasov
It is most useful control to validate the regular expression fields.
Pandi
Karunagara

You can use google to search for other answers

Custom Search

More Threads

• how to add target to link?
• Reading specific values from string:
• Last part of URL
• Returning an empty group ()
• Exclude more than one dots in Numbers
• How can allow numbers and # symbol
• A question about $
• Matching using multiple question marks
• regex to fnid a sentence. help!!!
• Pad currency amount with with zero after decimal point