0

私はこの種のコメントを持っています(いくつかの例):

  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
    // (..)
    

このスタイルのコメントに置き換えたい:

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

Description:

    some text some text
    some text some text some text

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

したがって、これには正規表現が必要です。私はなんとかこの正規表現を作ることができました:

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

グループ内のコメントと一致しますが、1行のみです(例1)。多くの行のコメント(例2のように)でそれを機能させる方法は?

手伝ってくれてありがとう

4

4 に答える 4

1

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

コメント付きバージョン:

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
于 2012-08-23T14:23:50.573 に答える
0

コメントで説明されている、必要なことを行う短い perl スクリプト:

#!/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.
}
于 2012-08-23T13:10:28.623 に答える
0

正規表現がまだ必要な場合は、より良い解決策があるかどうかわかりません。これが私が思いついたものです:

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

関心のあるすべてのテキストを取得する必要があります。

于 2012-08-23T13:45:33.477 に答える
0

この正規表現はコメント全体に一致します

(\/\/=+)(\s*\/\/ .+?$)+
于 2012-08-23T13:05:14.930 に答える