use strict; use warnings; my $str = '  >'; my $c = 0; my @arr; while ($str =~ /<[^<>]+>/) { $str =~ s/<([^<>]+)>/`$c`/; $arr[$c++] = $1; } s/ /"/ig for @arr; while ($str =~ /`\d+`/) { $str =~ s/`(\d+)`/<$arr[$1]>/g; } print $str;