BhariD has asked for the wisdom of the Perl Monks concerning the following question:

I have the following example string:
ACGTTGGCTATTGGGCCCGCT

I am searching for the pattern 'GTT' in the above string. As you will notice, pattern 'GTT' occurs in the start of the string. Once the pattern is matched, I need to remove any number of characters before (here, AC) and 3 characters (GGC) after the pattern ('GTT').

so the output will look something like this:

TATTGGGCCCGCT

Any pointers how I can do this? Thanks!

Replies are listed 'Best First'.
Re: how to remove characters before and after a search pattern in a string?
by ikegami (Patriarch) on Mar 03, 2010 at 17:25 UTC

      I don't see the difference bewteen
      Last GTT:

      s/^.*GTT.{3}//s
      and
      Every GTT:
      s/^.*GTT.{3}//s
      . Shouldn't it have been
      s/^.*GTT.{3}$//s
      ?

      Also, won't the last one remove everything preceding the last occurrence of GTT (assuming it's followed by 3 somethings), and not just every occurrence of 'GTT' within the string?

        There isn't any difference. Deleting everything before every instance and deleting everything before the last instances gives the same result.

        No, adding $ makes it so it will only match if GTT is three characters from the end of the string (or 4 if the last character is \n).

Re: how to remove characters before and after a search pattern in a string?
by thunders (Priest) on Mar 03, 2010 at 18:49 UTC
    since your pattern is a simple string, you can use index and substr.
    my $str = "ACGTTGGCTATTGGGCCCGCT"; my $pattern = "GTT"; my $offset = index($str,$pattern) + length($pattern) + 3; $str = substr($str,$offset); print $str;

      Probably better as:

      my $str = "ACGTTGGCTATTGGGCCCGCT"; my $pattern = "GTT"; if ( ( my $offset = index $str, $pattern ) >= 0 ) { + substr $str, 0, $offset + length( $pattern ) + 3, ""; } print $str;