Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex replace special comments

Tags:

regex

replace

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
//----------------------------
like image 977
Skatz1990 Avatar asked Apr 18 '26 08:04

Skatz1990


2 Answers

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.

like image 154
AlexBay Avatar answered Apr 19 '26 20:04

AlexBay


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!

like image 41
Elad Nava Avatar answered Apr 19 '26 22:04

Elad Nava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!