use strict; use warnings; my @strings = qw( 12-12 12-12-12 12-13-12-13 12-12-13-13 12-13-13-14 ); for (@strings) { my %hash; s/(\-?)(\d\d)/ $hash{$2}++ ? '' : $1.$2 /ge; print "$_\n"; }