locked
VS2010 Visual Studio (IDE) - Regular Expressions (Not VS2012) RRS feed

  • General discussion


  • The regular expressions in Visual Studio 2003/2005/2008/2010 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:

    <fieldset><legend style="color:#008080;">Visual Studio Search & Replace Dialog</legend>
    Find What: return data\[{["A-Z_]*}\]
    Replace with: return ProcessDataString(\1)
    </fieldset>
    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.
    <fieldset><legend style="color:#008080;">Visual Studio Search & Replace Dialog</legend>
    Find: copy-cp{:z}
    Replace with: copy\1-cp
    </fieldset>

    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


    • Edited by OmegaManModerator Thursday, September 13, 2012 7:15 PM VS2012 doesn't apply
    Wednesday, January 31, 2007 10:59 PM
    Moderator

All replies

  •  

    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.

    Thursday, June 5, 2008 2:00 PM
  • It is most useful control to validate the regular expression fields. 
    Pandi
    Wednesday, December 17, 2008 6:41 AM
  • Anything that is a bug should be fixed. Post the exact issue to Microsoft Connect Visual Studio and .Net Framework . Keep us informed of your progress.

    William Wegerson (www.OmegaCoder.Com)
    Monday, August 2, 2010 7:16 PM
    Moderator
  • Hi guys.

    I am really love the MS VS IDE fo its regular expressions and im a frequent user of that capability. Im using VS IDE always when i need to process text files i need - VS IDE is my No1 instrument in such tasks.

    But despite all beauty of this capability in IDE there are many bugs in its' functioning. I changed 3 version of MSVS (2005, 2008, 2010) and many of these bugs still there.

    Does MS programmers correcting bugs in IDE regular expressions at all?

    I cant write particular bugs descriptions here at moment of writing this message (cuz im oriented to perform my tasks first than search the bugs in VS), but if you try to process files with VS IDE regular expressions for long period - you should meet all these surprises well. And of course, now i will post about each bug i encountered and remembered here in this thread.


    This is what I'm looking for, Nice writing, Now I understand more about it, Thanks for your explanation!
    Sunday, September 5, 2010 3:58 AM