1

I have a example.c file with some codes and comment lines, In that file, I have a comment line for ex:

static Bool Start = FALSE;/*  $n=Start$ $t=static Bool$ $i=FALSE$
                              $d=initialize start to false$ */

I am using perl to remove comment lines, i tried Regular Expressions to remove the comment lines.

I have tried the following code , but it is not working,

`open(F,"<","example.c") or die "Could not open the .c file ";
 {
 $MultilineComment0 = 0;
 while(my $matchLine = <F>)
{
if (($matchLine =~ s/^.*\/\*.*/) && ($matchLine !~ m/\*\/$/)) # matches Code         + Multi Comment line Ex: Variable Declaration statements
{
 $MultilineComment0 = 1;
 next;                  #removes comment part from excutable lines
}
if ($MultilineComment0 == 1)
{
   if ($matchLine =~ m/\*\/$/)
{
$MultilineComment0 = 0;
}
next;
}
push(@executable_code_only, $matchLine);
}
}
close(F);
open(F, ">",example.c");
 print F ("@executable_code_only");
close(F);`

I will be helpful if someone provide me a regular expression to remove these comment lines.

AnFi
  • 10,493
  • 3
  • 23
  • 47
  • 2
    It's not as easy as you think it is. http://perldoc.perl.org/perlfaq6.html#How-do-I-use-a-regular-expression-to-strip-C-style-comments-from-a-file%3f – Matt Jacob Jan 30 '16 at 06:23
  • @MattJacob - Posting a FAQ link and synopsis is an answer to this question IMO. – Sobrique Jan 30 '16 at 12:16
  • 1
    @Sobrique brian d foy [already did](http://stackoverflow.com/a/911583/176646). This is a duplicate many times over: http://stackoverflow.com/q/1714530, http://stackoverflow.com/q/877470, http://stackoverflow.com/q/27682363, http://stackoverflow.com/q/13244677, http://stackoverflow.com/q/11775924. Actually, the first one in my list might(?) be a better canonical than the one I voted as a dupe target. Since you're a gold badger, if you felt so inclined you could close all of these as dupes of the best one. – ThisSuitIsBlackNot Jan 30 '16 at 14:06
  • Keep forgetting I can do that :) – Sobrique Jan 30 '16 at 19:26

0 Answers0