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

时间:2014-12-29 01:29:05

标签: regex perl comments

如何删除多行C样式的评论,如:

/* comments
   comments
   comments
   comments */

我可以使用其他问题中提供的几个代码删除一行中的评论,例如/* comments */

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

上述所有三个正则表达式都不适用于多行注释。怎么办?

1 个答案:

答案 0 :(得分:4)

我愿意,

perl -0777pe 's/\/\*(?:(?!\*\/).)*\*\/\n?//sg' file

示例:

$ cat fi
/* comments
   comments
   comments
   comments */
bar
$ perl -0777pe 's/\/\*(?:(?!\*\/).)*\*\/\n?//sg' fi
bar
相关问题