InPut : $str = "abcdefghiaabccdjklm"; OutPut : $str="efghijklm"; #### Original String : my $str = "abcdeffghijkllmnoppqestqaserb"; 1st iteration : $str =~ s/(\w)(.*)\1/$2/g; Output : bcdeffghijkllmnoppqestqserb 2nd Iteration : $str =~ s/(\w)(.*)\1/$2/g; OutPut : cdeffghijkllmnoppqestqser 3rd Iteration : $str =~ s/(\w)(.*)\1/$2/g; OutPut : cdffghijkllmnoppqestqsr 4th Iteration : Gets rid of consecutive elements $str =~ s/(\w)(.*)\1/$2/g; OutPut: cdghijkmnoestsr