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

Hello guys, Im trying to come up a regex for getting some text out of that:
// useless comment Some Code //********************************************************** // //I need that text // And that also // //********************************************************** Some Code // useless comment
I've come up with the solution that is better to read the file into a string and proceed as a whole. I've tried /\/\/*+(\w.+)\/\/*+/s and some others but it doesn't even come close to what I need. Any suggestions or ideas are more than welcome! Thank you.

Replies are listed 'Best First'.
Re: Regex needed :<
by Athanasius (Archbishop) on Jan 22, 2015 at 15:29 UTC

    Here’s variation on trippledubssolution using the 3-dot form of the flip-flop operator:

    #! perl use strict; use warnings; my $re = qr{^//\*+$}; while (<DATA>) { if (my $seq = /$re/ ... /$re/) { if ($seq > 1 && $seq !~ /E0$/) { print if m{^//\s*\S+}; } } } __DATA__ // useless comment Some Code //********************************************************** // //I need that text // And that also // //********************************************************** Some Code // useless comment //********************************************************** // More text //********************************************************** More code

    Update: Changed print unless m{^//$}; to print if m{^//\s*\S+}; to filter-out non-comment lines.

    Output:

    1:25 >perl 1128_SoPW.pl //I need that text // And that also // More text 1:25 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      ++ Athanasius to address me to the 3 dot form: never realized it exists!!
      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Regex needed :<
by trippledubs (Deacon) on Jan 22, 2015 at 15:26 UTC
    #!/usr/bin/env perl use strict; use warnings; my $flag; while (<DATA>) { if (/\/\*/) { $flag=!$flag; #turns on and off next; } if ( ($flag) && /\/{2,2}(.*)/) { print "$1\n"; } } __DATA__ // useless comment Some Code //********************************************************** // //I need that text // And that also // //********************************************************** Some Code // useless comment
Re: Regex needed :<
by kennethk (Abbot) on Jan 22, 2015 at 16:08 UTC
    Flip-flop in either configuration is probably the more natural choice, but a regular expression was the spec, so I give you:
    #!/usr/local/bin/perl use strict; use warnings; $_ = <<'EOT'; // useless comment Some Code //********************************************************** // //I need that text // And that also // //********************************************************** Some Code //********************************************************** // //I need that text // And that also // //********************************************************** Some Code // useless comment Some Code //********************************************************** // //I need that text // And that also // And some more // //********************************************************** Some Code // useless comment EOT my $i = 0; while (m|^//\*+\s*^.+?(\w[^*]+\w).+?^//\*+\s*$|smg) { printf "%d:\n%s\n\n", ++$i, $1; } __END__ 1: I need that text // And that also 2: I need that text // And that also 3: I need that text // And that also // And some more
    There's no way in a single autonomous expression to grab a variable number of fields, so I had to grab the whole chunk. If you know each one is 2 lines, you could use
    ^//\*+\s+^.+?^//\s*(\w.+?)\s*^//\s*(\w.+?$).+?^//\*+\s+$

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.