Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Comments with Perl

Tags:

regex

perl

Is there any regular expression that is able to remove all comments of the // and /**/ variety from a given C program say in perl?

Given the multi-line program, it should use the regular expression to remove the comments and return the non-commented out portion.

Thanks.

like image 219
Tim Haggard Avatar asked Jan 27 '26 15:01

Tim Haggard


1 Answers

Try with Regexp::Common::comment:

  use Regexp::Common qw /comment/;
  while (<>) {
        s/($RE{comment}{C++})//;
  }
like image 98
Tudor Constantin Avatar answered Jan 31 '26 07:01

Tudor Constantin