Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
.NET Zone is brought to you in partnership with:

I've been a game developer since 2005. After working 3 years in the game industry I've decided to go indie and create a little development studio called Different Pixel. We've debuted our first game Vizati on June 2010 David is a DZone MVB and is not an employee of DZone and has posted 17 posts at DZone. You can read more from them at their website. View Full User Profile

Find/Replace in Visual Studio using Regular Expressions

02.09.2012
Email
Views: 2004
  • submit to reddit
The .NET Zone is presented by JNBridge to keep you updated on all the latest news, tips, and tools in the .NET community.  Check out today's top .NET content and read about JNBridge's innovative tools for .NET and Java interoperability.  

Usually Find/Replace gets the job done for what I need, although sometimes using “Replace All” can break more stuff than it fixes.

But today I had this function I wanted to get rid of and simply change it with a public variable.

So I had something like this.

object->setLayer(/*BLABAL BLA CODE, */);


I want to replace it with something more simple

object->Z = /*BLABAL BLA CODE, */;


So using Visual Studio Find/Replace in regular expressions mode I used this as a search string

setLayer\({.+}\);


And this as a Replace with string

Z=\1;

 


Now, notice the first one, it’s actually simple, you have “setLayer”, followed by “\(” to tell the expression we really need to be that character, after that we have “.+”, which means match any single character except a line break and at least one occurrence. Followed by “\);”

Now if you notice we have curly brackets around the inside of what the function receives as parameter, that’s because we are tagging that string to use it later.

This way on the replace we simply need “Z=” followed by the first tagged string we have “\1″. if we have more curly brackets it would be “\2″, “\3″ etc.

Check the full list for more options: http://msdn.microsoft.com/en-us/library/2k3te2cs%28v=vs.80%29.aspx



Source: http://www.david-amador.com/2012/01/findreplace-in-visual-studio-using-regular-expressions/

Published at DZone with permission of David Amador, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

This content was brought to you in partnership with JNBridge.  JNBridge specializes in .NET and Java interoperability.  Be sure to view JNBridge's brief video series on accessing Java from .NET.