views:

733

answers:

5

I'm looking for a decent tool that can do search and replace over multiple files, with regular expression syntax I'm use to in C#. Normally I would do this in Visual Studio, except it has the strangest regex syntax (and this is meant to be faster than just replacing the text in the files manually).

So far I've tried windows grep but it didn't like the regex below. The regex in question is

<see cref="(?<class>.+)">(.+)</see>

To replace with

<see cref="${class}"/>

Alternatively converting this to Visual Studio's syntax would be fine!

Jeff has a whole post on this on his blog.

+1  A: 

notepad++ is quite popular and lightweight and has this functionality, have you tried that?:

http://notepad-plus.sourceforge.net/uk/site.htm

EDIT: It would appear my link is outdated and this usage is only quite recent:

http://www.opensourcereleasefeed.com/release/show/notepad-v5-2-released-replace-in-files-feature-added

REA_ANDREW
Does that work over multiple files?
AnthonyWJones
In my edit, it would appear yes. Your comment triggered me to double check.
REA_ANDREW
@AnthonyWJones : I have not used it since older versions but it is very useful
REA_ANDREW
+3  A: 

In Visual Studio you can do this with the following Reg Ex:

\<see cref="{.+}"\>.+\</see\>

Replace:

<see cref="\1" />

Remember to select Use/Regular Expressions in the Find and Replace dialog.

I've long been thinking it sucks that Visual Studio does not support the same Regular Expression syntax as the .Net framework.

Martin Brown
This works just as well as the answer but I couldn't give two answers
Chris S
No worries. I gave this answer a +1, because it answered my specific problem.
awe
+3  A: 

In Visual Studio, find:

\<see cref="{:i}"\>.*\</see\>

and replace with:

<see cref="\1"/>

{} is the VS grouping operator. It is not named. In replacements \n is used for the value of the n-th group in the find expression.

You can tweak the :i and .* parts if you need to account for new lines or other whitespace.

Franci Penov
+2  A: 

There are a few choices:

  • If it's something you do regularly, I would go with PowerGrep as it is probably the most complete and user-friendly tool for this.

  • Its sibling, RegexBuddy also has a Grep functionality, albeit not as comprehensive.

  • If you're not afraid of Perl, you can also use its command line as a search and replace tool.

  • Otherwise, there is still the venerable sed that works on Windows.

Renaud Bompuis
Since the poster asked about C# compatibility, RegexBuddy has the option to emulate the C# syntax (.NET flavor). PowerGREP's regex flavor is a superset of the .NET flavor.
Jan Goyvaerts
+1  A: 

You can use the .NET Regular Expression AddIn to allow you to use .NET regexes with Visual Studio. It's on CodeProject at http://www.codeproject.com/KB/macros/VS2005RegexAddIn.aspx.

Mike