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

Hi all,

At the moment I've been able to retrieve a paragraph of text from a variable by specifying a start and an end point as so:

if(Dumper($page) =~ /===Comments===(.*?)=Microarray Data=/s) { $lit_comments = $1; }

By doing this I've stored all the text under the ===Comments=== header. E.g.

===Comments===

Comments made here will be stored in $lit_comments.

=Microarray Data=

Now I have a problem where I need to do the same again, except this time I need to extract the text after the second occurrence of the ===Comments=== tag. Any ideas anyone?

Replies are listed 'Best First'.
Re: Search for Second Occurence of Substing and get containing text
by BrowserUk (Patriarch) on May 06, 2010 at 15:32 UTC

    m//g in a while loop will continue from where it left off last time:

    $s = "head\nstuff\nstuff\ntail\nthis\nthat\nhead\nother stuff\nmore\n +tail\nand more";; while( $s =~ m[head\n(.+?)tail\n]smg ) { print ">$1<"; };; >stuff stuff < >other stuff more <
Re: Search for Second Occurence of Substing and get containing text
by kennethk (Abbot) on May 06, 2010 at 15:23 UTC
    There are some very clever ways to do this, but simple is probably easiest: why not just include the string ===Comments=== in your regular expression twice?

    if(Dumper($page) =~ /===Comments===(.*?)=Microarray Data=/s.*?===Comme +nts===(.*)) { $lit_comments = $1; $second_comments = $2; }

    See perlretut for some reference material.

Re: Search for Second Occurence of Substing and get containing text
by graff (Chancellor) on May 06, 2010 at 21:56 UTC
    The previous replies probably give enough to go on, but the OP question wasn't clear. To expand a bit, one of the following two methods would be useful, depending on what the question really meant: The basic point is that a regex match will return its capture(s) as a list, which you can then pick from via an array slice (or assign to an array or list).
Re: Search for Second Occurence of Substing and get containing text
by k_manimuthu (Monk) on May 07, 2010 at 06:03 UTC

    For your code

    if(Dumper($page) =~ /===Comments===(.*?)=Microarray Data=/s) { $lit_comments = $1; }

    Search the match one time only

    You want to do the stuff's repeated. You will use the while loop and use the 'g' (global modifier) in your code.

    while(Dumper($page) =~ /===Comments===(.*?)=Microarray Data=/gs) { $lit_comments = $1; }

    Otherwise. Try this

    Dumper($page) =~ s/===Comments===(.*?)=Microarray Data=/&my_process($& +)/ges; sub my_process { my ($content)=@_; ## Do You stuff's Here return $content; }