views:

38

answers:

5

I've inherited some C# code that contains about a thousand lines of source that I need to modify, transforming it from this:

newDataRow["to_dir"] = comboBox108.Text;

To this:

assetAttributes.Add("to_dir", comboBox108.Text);

The lines occur in various places throughout the application in groups of 40 or 50. Modifying each line by hand in Visual Studio 2008 can be done but it's labor intensive and prone to errors.

Is there a Windows utility out there that will let me cut and paste groups of code into it and then run some sort of reg-ex expression to transform the individual lines one-by-one? I'd also be willing to use some sort of VS 2008 add-in that performed the same set of reg-ex operations against a selection of code.

Thanks in advance.

+1  A: 

In Visual Studio, Find and Replace allows you to replace using regular expressions. In the Find and Replace dialog, under Find options, there is a 'Use' checkbox with Regular expressions in it. Use the arrows next to 'Find what' and 'Replace with' to learn about Visual Studio's slightly different syntax.

Bryan
Did not realize the VS allowed find/replace with regular expressions. Would have gone this way if VS allowed operations on selections and used standard regex.
billmaya
+2  A: 

While not a great experience, you can use the VS2008 built-in search & replace to do this.

Note that it has its own regex syntax. To do this, go to Edit | Find & Replace | Replace in files. Then expand "Find Options" and select use regular expressions.

Try this - test and fix... For your search term, use
newDataRow\[{"[."]*"}\] = {[^;]*} and replace with assetAttributes.Add\("\1", \2\);

The \1 and \2 identifiers are the first and second groups (surrounded with {}) that were found. hit the arrows next to the search terms for a menu, select the botom option to go to help.

Philip Rieck
A: 

Windows Grep is great, and should do exactly what you want, and more:

http://www.wingrep.com/

Mike
+1  A: 

Using the regular expression search and replace, you can search for:

newDataRow\["([a-zA-Z_]+)"\] = comboBox([0-9]+).Text;

and replace with

assetAttributes.Add("\1", comboBox\2.Text);

which should do the trick :o)

Chief17
+1  A: 

If you simply want a text editor that can do this, you might want to try PSPad - the find/replace allows for regular expression useage.

Cam
Ended up using Programmer's Notepad 2 with three different sets of fine/replace regex expressions.
billmaya
Although I like the +15 rep from the accepted answer, FYI you're able to answer your own question and accept it if you found the answer yourself. That being said if one of the answers _led_ you to a solution, you could accept that one and simply edit your description with a note about your solution (or leave a comment as you did here).
Cam