#!/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; $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 =~ s/\@begin([\w\W]*?)\@end/$x/ge) { $i++; $x=$file[$i]; print $i."\n"; } print $string; #### ##output hi hello hh asas x x x x x zxzxax hi hello hi hellooo hh