#!/usr/bin/perl -w use strict; print doFunc( "AABBCC",0 ); print doFunc( "AAAABBBBCCCC",0 ); sub doFunc { my( $theParam,$depth) = @_; print STDERR "dofunc($theParam)\n"; my $re; $re = ${['(.+)\1']}[$depth]||'(.+)(\1)'; print STDERR join"\n", $theParam =~ /$re/g,''; $theParam =~ s/$re/&doFunc($1,$depth+1)/ge; ++$theParam; print STDERR "=$theParam\n"; return $theParam; }