my $s = 'Hello [2][1]'; my @a = ('x','y','z'); my $array_ref = \@a; $s =~ s/\[(\d{1,2})\]/$array_ref->[$1]/g; print $s;