sub rewrite { my ($string) = @_; my ($left, $right) = split /\s+/, $string, 2; if (my @m = $right =~ /^\d{2}(\d{2})(.*)/) { $right = $m[0] . ($m[1] =~ /^-/ ? '' : '-') . $m[1]; // here are the new additions $right =~ s/\s+//g; $right =~ s/-$//; } else { $right = '-' . $right unless $right =~ /^-/; } return $left . $right; } Please confirm that this is what you meant. Thanks