Tim Haggard
Tim Haggard

Reputation: 105

Removing Comments with 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.

Upvotes: 3

Views: 3422

Answers (2)

Tudor Constantin
Tudor Constantin

Reputation: 26861

Try with Regexp::Common::comment:

  use Regexp::Common qw /comment/;
  while (<>) {
        s/($RE{comment}{C++})//;
  }

Upvotes: 7

Related Questions