$string = 'WXYYZAABCCCCADWWXYYYZ'; while( $string =~ m/((\w)\2([^\2])([^\2\3]){4}\1[^\2\3\4])/g ) { print $1, "\n"; }