my $x = "<0>"; for (qw {<1> <2> <3>}) { if ($x =~ /<\d+>/gc) { $x =~ s/\G(<\d+>)/$1$_/; } } say $x;