use strict; use warnings; # I have no idea how unicode might affect this my $wrongway = 'This is a string with numbers 12345 that must be reversed without reversing the numbers.'; my @tmp; my @raw = split('',$wrongway); my @numeric; my $was_numeric = 0; for ( @raw ) { if ( /\d/ ) { push( @numeric, $_ ); $was_numeric = 1; next; } elsif ( $was_numeric ) { unshift( @tmp, @numeric ); $was_numeric = 0; @numeric = (); } unshift( @tmp, $_ ); } my $right_way =join('', @tmp); print "$wrongway\n"; print $right_way; print "\n\n";