in reply to Removing Flanking "N"s in a DNA String

A pair of substitutions with a regular expression anchored to the start and end of the string will help.
$dna = "NNNNATCGNNNTCGANNN"; $dna =~ s/^[N]+//; $dna =~ s/[N]+$//; print "$dna\n" __END__ ATCGNNNTCGA

Replies are listed 'Best First'.
Re^2: Removing Flanking "N"s in a DNA String
by ikegami (Patriarch) on Nov 07, 2005 at 13:52 UTC
    No need for the square brackets:
    $dna = "NNNNATCGNNNTCGANNN"; $dna =~ s/^N+//; $dna =~ s/N+$//; print "$dna\n" __END__ ATCGNNNTCGA
Re^2: Removing Flanking "N"s in a DNA String
by davidrw (Prior) on Nov 07, 2005 at 15:17 UTC
    As metioned already, no need for the character class.. Also, (although i tend to prefer the 2 separate statements as well), this can be written as one:
    $dna =~ s/(^N+|N+$)//g;
      Also,
      s/(?:^N+|N+$)//g;
      since we don't really need capturing. Which makes me think that ideally one may want it, instead, to see the text that has been substituted. But then he would only get the N's at ^ (if any) as $1 or . Which in turn make me wonder why there's this asymmetry between s/// and m//, since the latter, in list context and with /g does return all captures while the former just returns a true (or false) value in any context, precisely the numer of matches (or the empty string)...
Re^2: Removing Flanking "N"s in a DNA String
by blazar (Canon) on Nov 07, 2005 at 14:49 UTC
    Why do you need those charachter classes?
    s/^N+//, s/N+$// for $dna;