使用 Perl 正则表达式删除多行 C 风格 /* 注释 */

dou*_*sso 3 regex perl comments

如何删除多行 C 风格注释,例如:

/* comments
   comments
   comments
   comments */
Run Code Online (Sandbox Code Playgroud)

我可以删除一行中的注释,就像/* comments */使用其他问题中提供的多个代码一样。

s#/\*[\s\S]*?\*/##sg;
s#/\*(.*?)\*/##sg;
s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse
Run Code Online (Sandbox Code Playgroud)

上述所有三个正则表达式不适用于多行注释。如何处理?

Avi*_*Raj 5

我愿意,

perl -0777pe 's/\/\*(?:(?!\*\/).)*\*\/\n?//sg' file
Run Code Online (Sandbox Code Playgroud)

例子:

$ cat fi
/* comments
   comments
   comments
   comments */
bar
$ perl -0777pe 's/\/\*(?:(?!\*\/).)*\*\/\n?//sg' fi
bar
Run Code Online (Sandbox Code Playgroud)