0

How to remove a trailing blank space in a regex substitution?

Here the Data

              __Data__
              Test - results 
              dkdkdkdkdkdkdkdkdkkkdkd
              slsldldldldldldldldldll
              Information
              ddkdkdkeieieieieieieiei
              eieieieieieieieieieieiei
              Test - summary
              dkdkdkdkdkdkdkdkkdkdkdk
              dkdkdkdkdkdkdkdkdkdkdkk

What I would like to remove these lines shown above:

              Information
              ddkdkdkeieieieieieieiei
              eieieieieieieieieieieiei

My attempt using regex expression

           $/ = "__Data__";

           $_ =~ s/^(Test.*[^\n]+)\n(Information.*[^\n]+)\n(Test.*Summary[^\n]+)/$1$3/ms;
              print $_

The input of the data is the same as the output. In other words, nothing changes.

4

2 回答 2

1

Why not this:

while (<DATA>) {
    if ( m/^Information/..m/^Test/ ) {
        next unless m/^Test/;
    }
    s{\s+$}{};
    print "$_\n";
}
于 2012-05-16T18:40:29.363 回答
0

if you want to remove the information section use

$s =~ s/Information.*Test/Test/s;

if $s contains the data you gave then the following is returned

Test - results 
dkdkdkdkdkdkdkdkdkkkdkd
slsldldldldldldldldldll
Test - summary
dkdkdkdkdkdkdkdkkdkdkdk
dkdkdkdkdkdkdkdkdkdkdkk

If you want the information only then use

$s =~ s/(.*?)(Information.*?)(Test.*)/$2/s;

In both cases notice the the "s" at the end of the substitution, that flag allows it to process multiple lines.

于 2012-05-16T20:03:43.850 回答