#!/usr/str/perl -w use strict; use warnings; my $toTranslate ="333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333233333333322333333333233333333333333333333333333333333323333333322333333332333333333"; my $tmpFound; my $count = 0; while($toTranslate =~ /(3{4,})+/g) { $tmpFound = $1; $tmpFound = length($tmpFound); $toTranslate =~ s/$1/[$tmpFound]/; } print $toTranslate,"\n";