Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: string match using with an N in any position

by roboticus (Chancellor)
on Nov 18, 2011 at 02:55 UTC ( #938728=note: print w/ replies, xml ) Need Help??


in reply to string match using with an N in any position

biobee07:

Update: My solution just wasn't as good as the others in this thread. It looks like BrowserUk has the best solution.

Write a bit of code to create the regular expression for you:

$ cat 938725.pl #!/usr/bin/perl use strict; use warnings; my @search = ('GCGAT','CACGT'); # build regexes my @newsearch; for my $str (@search) { my @tmp; for my $char_idx (0 .. length($str)-1) { my $t = $str; substr($t,$char_idx,1,"[N".substr($t,$char_idx,1)."]"); push @tmp, $t; } push @newsearch, "^(".join("|",@tmp).")"; } while (<DATA>) { print "$.: $_"; for my $t (@newsearch) { print "MATCH for /$t/\n" if /$t/; } } __DATA__ GNGATNNNNNNNNNNNNNNN GCGANBBBBBBBBBBBBBBB CNCGTNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNNNN $ perl 938725.pl 1: GNGATNNNNNNNNNNNNNNN MATCH for /^([NG]CGAT|G[NC]GAT|GC[NG]AT|GCG[NA]T|GCGA[NT])/ 2: GCGANBBBBBBBBBBBBBBB MATCH for /^([NG]CGAT|G[NC]GAT|GC[NG]AT|GCG[NA]T|GCGA[NT])/ 3: CNCGTNNNNNNNNNNNNNNN MATCH for /^([NC]ACGT|C[NA]CGT|CA[NC]GT|CAC[NG]T|CACG[NT])/ 4: NNNNNNNNNNNNNNNNNNNN

Update: D'oh! Of course jwkrahn is right, so I corrected & replaced the program. Original code (less output) is:

$ cat 938725.pl #!/usr/bin/perl use strict; use warnings; my @search = ('GCGAT','CACGT'); # build regexes my @newsearch; push @newsearch, "^".join("", map { "[N$_]" } split //,$_) for @search +; while (<DATA>) { print "$.: $_"; for my $t (@newsearch) { print "MATCH for /$t/\n" if /$t/; } } __DATA__ GNGATNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNN GCGANBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB CNCGTNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNN

...roboticus

When your only tool is a hammer, all problems look like your thumb.


Comment on Re: string match using with an N in any position
Select or Download Code
Re^2: string match using with an N in any position
by jwkrahn (Monsignor) on Nov 18, 2011 at 03:03 UTC

    /^[NG][NC][NG][NA][NT]/ will also match the string 'NNNNN'

Re^2: string match using with an N in any position
by biobee07 (Novice) on Nov 18, 2011 at 05:22 UTC
    Thanks for replying to my thread -:)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://938728]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2015-07-02 04:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (27 votes), past polls