prassi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

I need to do a particular text matching with the text "report" here is the example of the C code

#include <stdio.h> #include "report.h" void main() { #ifdef CHECK_REPORT report("This is good"); reports("This is also good") #endif #if defined (REPORT_ENABLE) report("This is not good"); #endif printf("The execution is completed\n"); }

In the above code I need to match all the report & reports statement and remove it the out put has to be like this

#include <stdio.h> #include "report.h" void main() { #ifdef CHECK_REPORT #endif #if defined (REPORT_ENABLE) #endif printf("The execution is completed\n"); }

but my perl code is not able to retain with the words starting with report_, when the line REPORT_ENABLE comes it blindly removes rest of the code. please suggest me the correction required in the regex in which I can retain even REPORT_ENABLE

s#\breport[s]?.*?\)\;|("[^"].*?")#defined $1 ? $1 :""#gsie;

regards,

-Prasad

Replies are listed 'Best First'.
Re: text matching
by frozenwithjoy (Priest) on Jun 16, 2012 at 04:45 UTC

    Can you give the context you are using this snippet in? When I use it, everything looks as you expected except that #include "report.h" is retained.

    That being said, there do seem to be some issues with the regex. Based on your description of what you want to accomplish, why not just use something like this:

    while(my $line = <$in>) { $line =~ s/ .* report [s]? .* ; .* //sx; print $line; }

    It gives the following output:

    #include <stdio.h> #include "report.h" void main() { #ifdef CHECK_REPORT #endif #if defined (REPORT_ENABLE) #endif printf("The execution is completed\n"); }
      Hi frozen,

      This code dint work for me, It deleted all the code from the C program.

      Regards,

      -Prasad

        Hmmm. Can you show how you are running it? This is how my complete code looked:

        #!/usr/bin/env perl use strict; use warnings; open my $in, "<", shift; while( my $line = <$in> ) { $line =~ s/ .* report s? .* ; .* //sx; print $line; }

        Be sure to put the name of the report file as an argument on the CL. Also, which operating system are you using?