while(){ s/\]([ATCG]*)\[/my $a = $1; $a =~ s![^C]+!'N' x length($1)!e; $a/e; print $_; } __DATA__ TG[CCC]CC[TTT] Respective Output is: TG[CCCCCTTT]