Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how should I combine these regex?

Tags:

regex

perl

 $syb =~ s/(at{3,6})/\U$1/gi;

 $syb =~ s/(aat{2,5})/\U$1/gi;

 $syb =~ s/(aaat{1,4})/\U$1/gi;

 $syb =~ s/(aaaat{0,3})/\U$1/gi;

 $syb =~ s/(aaaaat{0,2})/\U$1/gi;

 $syb =~ s/(a{4,7})/\U$1/gi;

 $syb =~ s/(aaaaaat)/\U$1/gi;

 $syb =~ s/(t{4,7})/\U$1/gi;

Is there any way I could get all these regexps into one? Is it bad practice to use this many regexps on the same string? the end result should if $syb is aaatcgacgatcgatcaatttcgaaaaaggattttttatgcacgcacggggattaaaa the regexp should make it AAATcgacgatcgatcAATTTcgAAAAAggATTTTTTatgcacgcacggggattAAAA

one problem with my regexps is that they match aaaatttt as two separate matches and output AAAATTTT. i need to fix this as well.

i have a string of A's C's T's and G's stored in $syb. i want to capitalize any part of the string that has a set of A's followed by T's, just A's or just T's (T's followed by A's should not) and the capitalized section may be no shorter than 4 and no longer than 7

like image 871
Orion Avatar asked Jul 11 '11 15:07

Orion


1 Answers

This is a tough one. I think this might work:

s/((?<!a)a|(?<!a|t)t)((?<!t)\1|t){3,6}(?!\2|t)/\U$&/gi

Essentially, what I'm doing is:

  1. Get an a not preceded by an a. Or a t not preceded by an a or t.
    • ((?<!a)a|(?<!a|t)t)
  2. Get 3-6 more of the first match, or t's not preceded by a t
    • ((?<!t)\1|t){3,6}
  3. Make sure it is not followed by the last item in the sequence or a t.
    • (?!\2|t)/

And the perl code:

$syb = "aaatcgacgatcgatcaatttcgaaaaaggattttttatgcacgcacggggattaaaaactgaaaattttactgaaaaaaaasttttttts";
$syb =~ s/((?<!a)a|(?<!a|t)t)((?<!t)\1|t){3,6}(?!\2|t)/\U$&/gi;
print $syb;

Edit taking a queue from qtax I've removed capturing groups from mine and chars from his:

s/(?:(?<!a)a|(?<!a|t)t)(?:(?<!t)a|t){3,6}(?!(?<=a)a|t)/\U$&/gi

Edit: reducing the regex by 5 chars.

s/(?<!a|t(?=t))(?:a|t(?!a)){3,6}(?:a(?!a)|t)(?!t)/\U$&/gi

with commments

s/
# Look behind for a char not an 'a' nor a 't' followed by a 't'
(?<!a|t(?=t))
# Capture 3-6 'a's or 't's not followed by 'a's
(?:a|t(?!a)){3,6}
# Capture an 'a' not followed by an 'a', or a 't'
(?:a(?!a)|t)
#make sure none of this is followed by a 't'.
(?!t)
/\U$&/gix;
like image 103
Jacob Eggers Avatar answered Sep 19 '22 08:09

Jacob Eggers