0

I have this kind of comments (a few examples):

  1. //========================================================================
    // some text some text some text some text some text some text some text 
    
  2. //========================================================================
    // some text some text some text some text some text some text some text some text
    // some text some text
    // (..)
    

I want to replace it with comment of this style:

/*****************************************************************************\

Description:

    some text some text
    some text some text some text

\*****************************************************************************/

So I need regular expression for this. I managed to make this regex:

//=+\r//(.+)+

It matches the comment in group, but only one line(example 1). How to make it work with many lines comments(like example 2)?

Thanks for help

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
bourne
  • 11
  • 1
  • May be helpful: http://stackoverflow.com/questions/152708/how-can-i-search-for-a-multiline-pattern-in-a-file-use-pcregrep – Brad Christie Aug 23 '12 at 12:56
  • Possible duplicate: http://stackoverflow.com/questions/1507684/convert-single-line-comments-to-block-comments – Mr. Black Aug 23 '12 at 13:09

4 Answers4

1

Using sed:

sed -n '
  \_^//==*_!p;
  \_^//==*_{
    s_//_/*_; s_=_\*_g; s_\*$_\*\\_;
    h; p; i\
Desctiption:
    : l; n; \_//[^=]_{s_//_\t_;p;};t l;
    x;s_^/_\\_;s_\\$_/_;p;x;p;
  }
  ' input_file

Commented version:

sed -n '
  # just print non comment lines
  \_^//==*_!p;
  # for old-style block comments:
  \_^//==*_{
    # generate header line
    s_//_/*_; s_=_\*_g; s_\*$_\*\\_;
    # remember header, add description
    h; p; i\
Desctiption:
    # while comment continues, replace // with tab
    : l; n; \_//[^=]_{s_//_\t_;p;};t l;
    # modify the header as footer and print
    x;s_^/_\\_;s_\\$_/_;p
    # also print the non-comment line
    x;p;
  }
  ' input_file
perreal
  • 94,503
  • 21
  • 155
  • 181
0

This regex matches the whole comment

(\/\/=+)(\s*\/\/ .+?$)+
Gabber
  • 5,152
  • 6
  • 35
  • 49
0

A short perl script that should do what you need, explained in comments:

#!/usr/bin/perl -p

$ast = '*' x 75;                  # Number of asterisks.
if (m{//=+}) {                    # Beginning of a comment.
    $inside = 1;
    s{.*}{/$ast\\\nDescription:};
    next;
}
if ($inside) {
    unless (m{^//}) {             # End of a comment.
        undef $inside;
        print '\\', $ast, "/\n" ;
    }
    s{^//}{};                     # Remove the comment sign for internal lines.
}
choroba
  • 231,213
  • 25
  • 204
  • 289
0

If a regex is still wanted, dont know if there is a better solution or not this is what I came up with:

(?<=\/{2}\s)[\w()\.\s]+

Should get all the text that is of interest.

sQVe
  • 1,977
  • 12
  • 16