use strict; use warnings; my $xml = join '', ; while ($xml =~ s/(<\/?.*?>)\s+(<\/?.*?>)/$1$2/sg) {} print $xml; __DATA__ 1 These spaces are to be preserved.