如何使用Perl从文件中删除多行C注释?

时间:2009-05-18 12:22:01

标签: c perl comments

任何人都可以使用正则表达式来删除文件中的多行注释和单行注释吗?

例如:

                  " WHOLE "/*...*/" HAS TO BE STRIPED OFF....."

1.   /* comment */
2.   /* comment1 */  code   /* comment2 */ #both /*comment1*/ and /*comment2*/ 
                                             #has to striped off and rest should 
                                                 #remain.
3.   /*.........
       .........
       .........
       ......... */

我真的很感谢你,如果你有这个需要......提前谢谢。

6 个答案:

答案 0 :(得分:16)

From perlfaq6“如何使用正则表达式从文件中删除C样式注释?”:


虽然这实际上可以做到,但它比你想象的要困难得多。例如,这个单行

perl -0777 -pe 's{/\*.*?\*/}{}gs' foo.c

可以在很多但不是所有情况下都有效。你看,对于某些类型的C程序来说,它太简单了,特别是那些带有引号字符串注释的程序。为此,你需要这样的东西,由Jeffrey Friedl创建,后来由Fred Curtis修改。

$/ = undef;
$_ = <>;
s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse;
print;

当然,这可以用/ x修饰符更清晰地编写,添加空格和注释。在这里,它扩大了,由Fred Curtis提供。

s{
   /\*         ##  Start of /* ... */ comment
   [^*]*\*+    ##  Non-* followed by 1-or-more *'s
   (
     [^/*][^*]*\*+
   )*          ##  0-or-more things which don't start with /
               ##    but do end with '*'
   /           ##  End of /* ... */ comment

 |         ##     OR  various things which aren't comments:

   (
     "           ##  Start of " ... " string
     (
       \\.           ##  Escaped char
     |               ##    OR
       [^"\\]        ##  Non "\
     )*
     "           ##  End of " ... " string

   |         ##     OR

     '           ##  Start of ' ... ' string
     (
       \\.           ##  Escaped char
     |               ##    OR
       [^'\\]        ##  Non '\
     )*
     '           ##  End of ' ... ' string

   |         ##     OR

     .           ##  Anything other char
     [^/"'\\]*   ##  Chars which doesn't start a comment, string or escape
   )
 }{defined $2 ? $2 : ""}gxse;

稍作修改也会删除C ++注释,可能使用延续字符跨越多行:

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

答案 1 :(得分:11)

通常在Perl中,您可以获得CPAN:Regexp::Common::Comment应该可以帮助您。我发现使用你所描述的评论的一种语言是Nickle,但也许PHP评论可以(//也可以开始单行注释)。

请注意,在任何情况下,使用regexp删除注释都很危险,语言的完整解析器风险要小得多。例如,regexp-parser可能会被print "/*";等内容弄糊涂。

答案 2 :(得分:6)

这是一个FAQ:

perldoc -q comment

perlfaq6中找到:

  

如何使用正则表达式从文件中删除C样式注释?

     

虽然这实际上可以做到,但它比你想象的要困难得多。对于      例如,这个单线......

答案 3 :(得分:1)

还有一个非perl答案:使用程序stripcmt

  

StripCmt是一个简单的实用程序   在C中删除C,C ++中的注释,   和Java源文件。在盛大   Unix文本处理的传统   程序,它可以作为一个   FIFO(先进先出)滤波器或   接受命令行上的参数。

答案 4 :(得分:0)

删除/ * * /评论(包括多行)

s/\/\*.*?\*\///gs

我发布这个因为它很简单,但我相信它会绊倒嵌入式评论,如

/* sdafsdfsdf /*sda asd*/ asdsdf */

但由于它们相当不常见,我更喜欢简单的正则表达式。

答案 5 :(得分:-2)

包括测试:

use strict;
use warnings;
use Test::More qw(no_plan);
sub strip_comments {
  my $string=shift;
  $string =~ s#/\*.*?\*/##sg; #strip multiline C comments
  return $string;
}
is(strip_comments('a/* comment1 */  code   /* comment2 */b'),'a  code   b');
is(strip_comments('a/* comment1 /* comment2 */b'),'ab');
is(strip_comments("a/* comment1\n\ncomment */ code /* comment2 */b"),'a code b');
相关问题