in reply to text matching

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"); }

Replies are listed 'Best First'.
Re^2: text matching
by prassi (Acolyte) on Jun 16, 2012 at 06:17 UTC
    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?

        I am running the code in cygwin and here is my complete code.

        #!/bin/perl5.8.6 use warnings; use diagnostics; use File::Basename; @files = <../tryremove/*>; #This is the destination path where the comment removed files are crea +ted. $path1 = "C:/Projects/tryremove/c"; foreach my $file (@files) { my $filename =basename($file); open(file1,"$file") or die "The file cannot be opened the $file:$!\n +"; open(file2,">$path1/$filename") or die "The file cannot be opened:$! +\n"; while(!eof(file1)){ $/ = undef; $_ = <file1>; s# .* report [s]? .* ; .* ##sx; print file2 ($_); } close(file1); close(file2); }