Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^3: Pattern searching allowing for mis-matches...

by bv (Friar)
on Dec 14, 2009 at 00:08 UTC ( #812627=note: print w/replies, xml ) Need Help??

in reply to Re^2: Pattern searching allowing for mis-matches...
in thread Pattern searching allowing for mis-matches...

Your second question is much easier: Yes. Regular expressions can be built from any string, including those supplied by users. Generally, you should use quotemeta or the \Q and \E markers to make sure the string is free from regular expression meta characters like *, ., and more evil eval-type expressions. In your case, you could also check that the string is a valid nt sequence:

my $string = quotemeta shift; die "Not a valid nucleotide sequence" if $string =~ /[^AGTC]/;

As for the first question, one way would be to build a regex for each possibility. An example:

my $string = "TGAT"; my @nts = map { my $tmp = $string; substr $tmp, $_, 1, '.'; $tmp; } (0 .. length ($string) -1); my $groupings = join '|', @nts; my $sample = "TGATTGGAATGTTAGAT"; while ( $sample =~ /($groupings)/go ) { print "Matched $1 ending at position ", pos $sample, "\n"; }

@_=qw; Just another Perl hacker,; ;$_=q=print "@_"= and eval;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2020-02-28 16:21 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (124 votes). Check out past polls.