Here's another approach. This needs Perl version 5.10+ for \K (if you have < 5.10, let me know; I can supply adjusted code).
c:\@Work\Perl>perl -wMstrict -le "use 5.010; ;; my $s = qq{xxx \@B grab \n this \@E yy zzzz \@B and \n this \n too \@ +E qq}; print qq{[[$s]]}; ;; my $start = qr{ \@B }xms; my $stop = qr{ \@E }xms; ;; my $replace = [ 'ZOT', 'ZONK' ]; ;; my $i = 0; $s =~ s{ $start \K .*? (?= $stop) } { $replace->[ $i++ % @$replace ] }xmsg; print qq{<<$s>>}; " [[xxx @B grab this @E yy zzzz @B and this too @E qq]] <<xxx @B ZOT @E yy zzzz @B ZONK @E qq>>
Pay particular attention to Loops's and Corion's remarks here and here about what should happen when the $replace array is exhausted (I just wrap around and start re-using replacement strings), and also here about using a real templating engine rather than trying to roll your own.
Update: Removed a needless capture group: .*? vice (.*?)
In reply to Re: need a regular exdpression in perl
by AnomalousMonk
in thread need a regular exdpression in perl
by praveenchappa
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |