in reply to PERL STRING QUESTION

#!/usr/bin/perl -l my $seq = "atgtagat"; my $find = "gta"; my ($before, $after) = $seq =~ /(..)$find(..)/; print "before: $before"; # "at" print "after: $after"; # "ga"

Replies are listed 'Best First'.
Re^2: PERL STRING QUESTION
by AnomalousMonk (Archbishop) on Apr 01, 2010 at 08:20 UTC
    my ($before, $after) = $seq =~ /(..)$find(..)/;

    ... will find only one instance of a sub-sequence in a sequence. The following will find multiple instances of sub-sequences, either overlapping or non-overlapping:

    >perl -wMstrict -le "my $seq = 'atggggtaggggat'; my $find = 'ggg'; print 'non-overlapping:'; while ($seq =~ m{ (..) $find (..) }xmsg) { print qq{($1)$find($2)}; } print 'overlapping:'; while ($seq =~ m{ (?= (..) $find (..)) }xmsg) { print qq{($1)$find($2)}; } " non-overlapping: (at)ggg(gt) (ag)ggg(at) overlapping: (at)ggg(gt) (tg)ggg(ta) (ta)ggg(ga) (ag)ggg(at)
A reply falls below the community's threshold of quality. You may see it by logging in.