删除多行C样式/ *注释* /使用Perl regex [英] Remove multi-line C style /* comments */ using Perl regex
本文介绍了删除多行C样式/ *注释* /使用Perl regex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何删除多行C样式的注释:
How do I remove multi-line C style comments like:
/* comments
comments
comments
comments */
我可以删除一行中的注释,如<$
I am able to remove comments in one line like /* comments */
by using several codes provided in other questions.
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?
推荐答案
我会这样做,
perl -0777pe 's/\/\*(?:(?!\*\/).)*\*\/\n?//sg' file
示例:
$ cat fi
/* comments
comments
comments
comments */
bar
$ perl -0777pe 's/\/\*(?:(?!\*\/).)*\*\/\n?//sg' fi
bar
这篇关于删除多行C样式/ *注释* /使用Perl regex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文