How can I remove multiline C comments from a file using Perl?

Can someone get me with regex to separate multi-line comments and single comments in a file?

eg:

                  " 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.   /*.........
       .........
       .........
       ......... */

I really appreciate you if you need it .... thanks in advance.

+5
source share
6 answers

Including tests:

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');
-2
source

From perlfaq6 "How to use regex to erase style style comments from a file?":


Although this can really be done, it is much more complicated than you think. For example, this one line

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

, . , C, , , . , , .

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

, , /x, . , .

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;

++, , :

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

Perl, CPAN: Regexp:: Common:: Comment . , , , , Nickle, , , PHP (// ).

, , . , regexp-parser - print "/*";.

+11

FAQ:

perldoc -q comment

perlfaq6:

C ?

, , .    , ...

+6

: stripcmt:

StripCmt - , C, C, ++, Java. Unix , FIFO ( - ) .

+1

/* */ ( )

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

, , , ,

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

, .

0
source

All Articles