#$str =~ s/(\d+?)%var%(.*)/$1REPLACED$2/; As pointed out by Eimi Metamorphoumai $str =~ s/(\d*)%var%(.*)/$1REPLACED$2/;