my @a = ( '0 11 11 / 11 11', '0 11 11 / 111 111', '0123 / 111 11 11', '011 / 1 111 1111', '012 3 / 11 11 1 ', ); sub formatieren { my $num = shift; $num =~ s/\s//g; $num = reverse join(' ', unpack('(A2)*', reverse $num)); return $num; } for my $tel (@a) { $tel =~ s#^([\d ]+)/([\d ]+)$#formatieren($1).' / '.formatieren($2)#e; say $tel; }