Skatz1990
Skatz1990

Reputation: 212

Regex replace special comments

So few months ago one of my colleagues left. He used to comment all his code this way:

//----------------------------
// COMMENT
//----------------------------

private void func()...

So each comment, instead of using 1 line at most, uses 4 lines (including break line), which drives me crazy. I'm trying to create a Regex which I can remove this comment safely and replace it. The above code should like this way:

// COMMENT
private void func()...

I thought of just removing each one of the '//----------------------------' but it leaves me with many empty lines as well as break line between the comment and the actual line which to be described. Any help will be well appreciated.

EDIT

Note one: Our project is written in Visual Studio

Note two: Some comments may contain more than 1 line of comment, example:

//----------------------------
// LINE 1 COMMENT
// LINE 2 COMMENT
//----------------------------

Upvotes: 3

Views: 117

Answers (2)

Elad Nava
Elad Nava

Reputation: 7906

Here's a Regular Expression that you can use to strip out the excess (decorative) comment lines and convert these bulky comments into one-liners.

It also supports indentation and multi-line comments using this style:

//----------------------------
// LINE 1 COMMENT
// LINE 2 COMMENT
//----------------------------

private void func()...

Find:

(( |\t)*?\r\n)?( |\t)*?//-+(\r\n( |\t)*?// .+)+\r\n( |\t)*?//-+\r\n

Replace With:

\4

(Replace \4 with $4 if the replace failed)

Good luck!

Upvotes: 1

AlexBay
AlexBay

Reputation: 1313

This expression matches your case and any 3 lines of comments where the first and the last ones have trailing -:

((\s|\t)*\/{2,})(.*[-]+)(\r?\n)((\1(.*)\4?)+)\4\1\3\4?

Try it here

And then you can replace it with:

\5 (or $5)

EDIT: for multi-line comments.

Upvotes: 3

Related Questions