in reply to Quantified Regex Replacement

I'd suggest s/\]([ATCG]+)\[/"N" x length($1)/e, which replaces the ]...[ part with one "N" for each letter in between the closing and opening bracket.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Quantified Regex Replacement
by Samy_rio (Vicar) on Feb 23, 2006 at 05:54 UTC

    Hi, Try this,

    while(<DATA>){ s/\]([ATCG]*)\[/'N' x length($1)/e; print $_; } __DATA__ TG[CCC]CC[TTT] TG[CCC][TTT] TG[CCAAATTT] Respective Output is: TG[CCCNNTTT] TG[CCCTTT] TG[CCAAATTT]

    Updated: monkfan, Try below code for your second question.

    while(<DATA>){ 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]

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re^2: Quantified Regex Replacement
by monkfan (Curate) on Feb 23, 2006 at 05:53 UTC
    Thanks so much for your reply.
    I am just wondering how can I just take away the two consecutive reversed brackets -- ][. Like this:
    my $str = "TGC[CCC]CC[TTT]"; # Into my $rep = "TG[CCCCCTTT]";
    Still my regex below doesn't do it right, it also removes two Cs in between:
    $str =~ s/\]([ATCG]+)\[//;

    Regards,
    Edward
      That's because your substitution matches the ]...[ part and replaces it with nothing! You'll have to replace it with $1, which in your regex, contains the letters in between the closing and opening brackets. s/\]([ACTG]+)\[/$1/

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart