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

i had a string basically the contents of a whole file ,i want to extract substrings whenever a pattern occurs as below....

$string=" hi hello @begin i want this to extract yes i want @end asas x x x x x zxzxax hi hello hi hellooo @begin i want this to extract yes i want @end" .....
this is my string and i want to extract the string between @begin....@end store the multipe occurances to another string

Replies are listed 'Best First'.
Re: help regarding a regular expression in perl
by AnomalousMonk (Archbishop) on Nov 19, 2014 at 10:17 UTC

    You can also grab all matches at once to an array:

    c:\@Work\Perl>perl -wMstrict -le "my $s = qq{xxx \@B grab \n this \@E yy zzzz \@B and \n this \n too \@ +E qq}; print qq{[[$s]]}; ;; my @captures = $s =~ m{ \@B .*? \@E }xmsg; print qq{<<$_>>} for @captures; " [[xxx @B grab this @E yy zzzz @B and this too @E qq]] <<@B grab this @E>> <<@B and this too @E>>

    Update 1:

    $string=" ... @begin ... @end ..." .....
    Note that arrays like  @begin @end interpolate into interpolating or "double-quote" strings like  " ... " and  qq/ ... / along with  $scalar scalars. Neither interpolate into non-interpolating 'single-quote' strings. Please see Quote and Quote-like Operators and Quote-Like Operators in perlop.

    Update 2: Or if you mean you want to grab only the sub-string between the delimiting patterns, add a coupla parens:

    c:\@Work\Perl>perl -wMstrict -le "my $s = qq{xxx \@B grab \n this \@E yy zzzz}; print qq{[[$s]]}; ;; my @captures = $s =~ m{ \@B (.*?) \@E }xmsg; print qq{<<$_>>} for @captures; " [[xxx @B grab this @E yy zzzz]] << grab this >>
    (You could also do this without a capture group using look-arounds, but let's let this stand for now.)

Re: help regarding a regular expression in perl
by pme (Monsignor) on Nov 19, 2014 at 09:38 UTC
    Hi praveenchappa,

    You may filter all the matching strings one by one as follows:

    my $string = ' hi hello @begin i want this to extract yes i want @end asas x x x x x zxzxax hi hello hi hellooo @begin i want this to extract yes i want @end ...'; while ($string =~ m/\@begin([\w\W]*?)\@end/g) { print "match: $1\n"; }
Re: help regarding a regular expression in perl
by Sathishkumar (Scribe) on Nov 19, 2014 at 09:15 UTC
    use this regex \@begin([\w\W]*?)\@end
      how could i do that i want to capture the matching occurances to another string.....
Re: help regarding a regular expression in perl
by praveenchappa (Acolyte) on Nov 20, 2014 at 07:15 UTC

    thank u all for ur response and it helped me a lot.....Now i had another issue in the same problem insted of capturing i need to replace with another string ....as below

    $string=" hi hello @begin i want this to extract yes i want @end asas x x x x x zxzxax hi hello hi hellooo @begin i want this to extract yes i want @end" ..... i had @replace=["replacestring1","replacestring2"....] i need to have my string like. $string=" hi hello @begin replacestrinjg1 @end asas x x x x x zxzxax hi hello hi hellooo @begin replacestring2 @end" .....