tags:

views:

100

answers:

4

Are there any regex experts who can help me clean up the following source code? I'm going through some existing code and I see several instances similar to the following:

public enum Numbers
{
    /// <summary>
    /// One = 1, 
    /// </summary>
    One = 1,

    /// <summary>
    /// Two = 2, 
    /// </summary>
    Two = 2,

    /// <summary>
    /// Three = 3, 
    /// </summary>
    Three = 3,

    /// <summary>
    /// Four = 4 but don't use this because it will break everything, 
    /// </summary>
    Four = 4,
}

Unless someone can tell me that the comments for 1-3 are necessary, I'd like to do a find/replace (remove) on all of the comments that don't add any value to the code. From browsing the code I think it's safe to assume that any line that resembles "/// word = number," can be replaced. Cleaned up, I think it should look like:

public enum Numbers
{
    One = 1,
    Two = 2,
    Three = 3,

    /// <summary>
    /// Four = 4 but don't use this because it will break everything, 
    /// </summary>
    Four = 4,
}

Your help is greatly appreciated! And by helping me, you are really helping yourself. Because who knows, someday you might be maintaining this very code!

+2  A: 

Here is a perl script which will remove such comments:

my $text = join "", <>;
$text =~ s{///\s+<summary>\s+///\s+\w+\s+=\s+\d+,\s+///\s+</summary>}{}g;
print $text;
tster
`\s` already contains `\n`, so `[\s\n]+` can be written as `\s+`.
Bart Kiers
thanks Bart, modified.
tster
No problemo tster!
Bart Kiers
A: 

Are you working on a unix-like os or have cygwin?

I've never done multiline matching with regex, but there's a link on using sed to do it...

http://www.ilfilosofo.com/blog/2008/04/26/sed-multi-line-search-and-replace/

I'll read and edit this answer later to apply it to your problem. Or maybe someone else will.

EMPraptor
Actually I'm using Visual Studio and I was going to try a Find/Replace using regex and replacing with an empty string.
Jonathan S.
+2  A: 

With VS2008 FindAndReplace I tried this and worked;

Find what:

/// \<summary\>.*\n.*\=:b:d[:b,]*\n.*\<\/summary\>\n

Replace with:

(empty)

Use:

Regular expressions

David Elizondo
Thanks, you just saved me thousands of unnecessary keystrokes and mouse highlights!
Jonathan S.
A: 

You can use PilotEdit to replace multiline strings with Regular expression. www.pilotedit.com Try to replace the following regular expression to a empty string:

/// <summary>[]*/// * = [0-9]+,[]*/// </summary>

The source code will be changed to: *

public enum Numbers
{

    One = 1,

    Two = 2,

    Three = 3,
    /// <summary>
    /// Four = 4 but don't use this because it will break everything, 
    /// </summary>
    Four = 4,
}

*

Dracoder