#!/usr/bin/perl use strict; use File::Find; use warnings; my @file=("hh","yy","zz","aa","bb","hh","yy","zz","aa","bb","hh","yy","zz","aa","bb"); my $i=0; my $string='hi hello @begintobe replace @end asas x x x x x zxzxax hi hello hi hellooo @beginthis has tobe replaced @end'; my $x=$file[$i]; while ($string =~ m/\@begin([\w\W]*?)\@end/g) { $i++; $string =~ s/\@begin([\w\W]*?)\@end/$x/s; $x=$file[$i]; print $i."\n"; } print $string;