Reputation: 59
__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?
Upvotes: 1
Views: 62
Reputation: 35208
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");
}
Upvotes: 0
Reputation: 21676
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?
Upvotes: 2