#! perl -slw use strict; my $s = 'XXQQGGYYNNQGYNNNNQNGGNGGNGGGQQQNNN'; print $s; print ' ' x( pos( $s ) - length( $1) ), $1 while $s =~ m[ ( ## Capture to $1 (?: ## A group ([QGYN]) ## of these characters (?!\2{2}) ## repeated no more than twice in succession ){3,6} ## 3 to 6 characters in length ? ## Remove for greedy matching. ) ]xg; ## Condensed and greedy print $s; print ' ' x( pos( $s ) - length( $1) ), $1 while $s =~ m[( (?: ([QGYN]) (?!\2{2}) ){3,6} ) ]xg; __END__ [20:37:58.32] P:\test>temp XXQQGGYYNNQGYNNNNQNGGNGGNGGGQQQNNN QQG GYY NNQ NNQ NGG NGG XXQQGGYYNNQGYNNNNQNGGNGGNGGGQQQNNN QQGGYY NNQGY NNQNGG NGGN