Alan C. wrote:
> Hello,
>
> I figured out how to substitute.  But I need howto help as I didn't
> know how to print out my desired matched items.  Thanks!
>
> Instead of the substitution, I want to match and keep.  In other
> words, I want to extract and keep/print <comment>up to the blank line
>
> so that the output will be:
>
> <comment>semester of 2002.
> involvement began in the Spring of 2001.
> requesting and obtaining a regarding my
>
> ---begin source file---
> with a matter that consumed or occupied a
> during the fall <comment>semester of 2002.
> involvement began in the Spring of 2001.
> requesting and obtaining a regarding my
>
> There were several reports to review.
> to and continued during the fall semester
> ----end source file---
>
> #!/perl/bin/perl -w
> undef $/;    # Enter "file slurp" mode.
> $text = <>;  # This file/selection slurped into the scalar
> for ($text =~ s/<comment>.*\n\n/<<here>>\n\n\n\n/) {
> print $text;
> }

Hi Alan.

You can do it this way if you want, by capturing sequences
in the regex.

    $text =~ s/(<comment>.+?)\n\n/;
    print $1;

but this is exactly the sort of thing the scalar range operator
was meant for:

    @ARGV = 'source.txt';
    while (<>)
    {
        next unless (my $seq = (/<comment>/ .. /^$/));
        if ($seq == 1) { s/^.+(?=<comment>)// }
        print;
    }

Cheers,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to