简体   繁体   English

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

[英]Remove multi-line C style /* comments */ using Perl regex

How do I remove multi-line C style comments like: 如何删除多行C样式注释,如:

/* comments
   comments
   comments
   comments */

I am able to remove comments in one line like /* comments */ by using several codes provided in other questions. 我可以使用其他问题中提供的几个代码删除一行中的注释,例如/* comments */

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

All three regexes above do not work with multi-line comments. 上面的所有三个正则表达式都不适用于多行注释。 How can they be handled? 他们怎么办?

I would do like, 我愿意,

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

Example: 例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM