use strict; use warnings; my $string=' hi hello @begintobe replace @end asas x x x x x zxzxax hi hello hi hellooo @beginthis has tobe replaced @end ...'; my @replace= ("replacestring1","replacestring2"); $string =~ s|\@begin.*?\@end |shift @replace // '[null]' |sexg; print $string;