in reply to Re^6: reg expression question
in thread reg expression question

Can you please confirm based on your comments I made the following changes
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

Replies are listed 'Best First'.
Re^8: reg expression question
by Athanasius (Archbishop) on Jan 29, 2015 at 16:53 UTC

    Almost; except that PUN VALEY B still isn’t handled correctly. That’s because it doesn’t match the regex, so the if clause isn’t entered and the substitutions are not applied. But if you move the two substitutions from within the if clause to after the else clause — that is, to immediately before the return line — then all the tests pass.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      excellent, all is working now. Thanks very much for your help. Best Regards