1
__DATA__
/*This is the file which is used for the
 developing the code for multiple IO operation
 Author : Hello Wolrd remove */

void main();

/*Hello World */
/* hello 
   world
   good
   morning
  comment /*
/* Hello Good Morning 
  !!!!!!!!!!!!!!!!!*/
void main()
{
    printf("Hello World");
}

The above code is my c file. Here I have to replace "Hello" by "Hi".. if I do simple parse the file and replace ... it will replace in all the places, For both comment and non-comment part of the code. But I have to replace it only in non-comment part. Is it possible to replace ?

After I reading and rewriting the same file I should have the following output

__DATA__
/*This is the file which is used for the
 developing the code for multiple IO operation
 Author : Hello Wolrd remove */
/*Hello World */
/* hello 
   world
   good
   morning
  comment /*
/* Hello Good Morning 
  !!!!!!!!!!!!!!!!!*/
void main()
{
    printf("Hi World");
}

How to decide the string is a comment or non-comment code for c files? Is it possible to do replace only non-comment part of the code?

jiandingzhe
  • 1,881
  • 15
  • 35
user3016320
  • 49
  • 1
  • 3
  • 6
  • 1
    I'd be very careful about this. Trying to use a regex to parse a non-regular language is not a good idea (see [this question](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) for a related example). I would recommend using [ctags](http://en.wikipedia.org/wiki/Ctags) as one relatively easy approach. – Rook May 15 '14 at 08:39
  • Use a proper parser ([Parse::RecDescent](http://p3rl.org/Parse::RecDescent), [Marpa::R2](http://p3rl.org/Marpa::R2)) with a [grammar](http://www.lysator.liu.se/c/ANSI-C-grammar-y.html). – choroba May 15 '14 at 08:53

2 Answers2

2

You may use Regexp::Common::comment

Sample:

  while (<>) {
        s/($RE{comment}{C})//;
  }

Also check perlfaq: How do I use a regular expression to strip C-style comments from a file?

Miller
  • 34,962
  • 4
  • 39
  • 60
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
0

Expanding on the recommendation to use Regexp::Common, the following uses the comment and quoted patterns to perform this substitution:

use strict;
use warnings;

use Regexp::Common;

my $data = do {local $/; <DATA>};

$data =~ s{$RE{comment}{C}\K|($RE{quoted})}{
    my $string = $1 // '';
    $string =~ s/Hello/Hi/g;
    $string;
}eg;

print $data;

__DATA__
/*This is the file which is used for the
 developing the code for multiple IO operation
 Author : Hello Wolrd remove */

void main();

/*Hello World */
/* hello 
   world
   good
   morning
  comment /*
/* Hello Good Morning 
  !!!!!!!!!!!!!!!!!*/
void main()
{
    printf("Hello World");
}

Outputs:

/*This is the file which is used for the
 developing the code for multiple IO operation
 Author : Hello Wolrd remove */

void main();

/*Hello World */
/* hello
   world
   good
   morning
  comment /*
/* Hello Good Morning
  !!!!!!!!!!!!!!!!!*/
void main()
{
    printf("Hi World");
}
Miller
  • 34,962
  • 4
  • 39
  • 60