use strict; my $string = '|xx-xxx-xxxxx-xxx x/xx|xx-xxxx-xxx-xx-xxxx-xx-xx|'; print "$string\n"; $string =~ s/\|//; # gets rid of the first pipe $string =~ s/\|/,/g; # replaces all other pipes by commas print "$string\n"; my @chunks = split '-', $string; $string = join( '-', @chunks[ 0 .. 3 ] ) . ',' . join( ',', @chunks[ 4 .. $#chunks ] ); print "$string\n"; #### |xx-xxx-xxxxx-xxx x/xx|xx-xxxx-xxx-xx-xxxx-xx-xx| xx-xxx-xxxxx-xxx x/xx,xx-xxxx-xxx-xx-xxxx-xx-xx, xx-xxx-xxxxx-xxx x/xx,xx,xxxx,xxx,xx,xxxx,xx,xx,