Java コメント正規表現: egrep "(/\*\* | /* | \*/ | \*\*/)" text.txt

Java コメント正規表現: egrep "(/\*\* | /* | \*/ | \*\*/)" text.txt

Java コメントの開始または終了行を抽出しようとしています:

私が持っているものは次のとおりです:

egrep "(/** | /* | */ | **/)" テキスト.txt

これは、以下の行を除くすべての行(/*コメント*/など)で機能することがわかりました。のみ/*、/**、**/、または */ で、その前後に何もない。

どうしてこれなの?

答え1

パターンegrep "(/** | /* | */ | **/)" text.txtに明示的なスペースが含まれています。スペースなしで試してください。egrep "(/**|/*|*/|**/)" text.txt

答え2

パターン内にスペースが含まれており、 で始まるコメント行を忘れています//

と:

egrep "(/\*\*|/\*|\*/|\*\*/|//)" text.txt

トークンのみを含む行も含め、コメントの開始または終了となるすべての行が表示されます。たとえば...

テキスト.txt:

this should not be there
// this should be there
/* and this too */
/** even this
should be there too **/
/* or
that
also */
not this
/*
*/
/**
**/

出力:

// this should be there
/* and this too */
/** even this
should be there too **/
/* or
also */
/*
*/
/**
**/

関連情報