my $s = "ZBBBCZZ"; my @x; push @x, $1 while $s =~ /((.)\2*)/g; #### s = "ZBBBCZZ" x = [] s.scan(/((.)\2*)/){x.push [$~[0]]} #### import itertools s = "ZBBBCCZZ" x = [''.join(g) for k, g in itertools.groupby(s)] #### group "ZBBBCCZZ"