in reply to how to remove characters before and after a search pattern in a string?

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;
  • Comment on Re: how to remove characters before and after a search pattern in a string?
  • Download Code

Replies are listed 'Best First'.
Re^2: how to remove characters before and after a search pattern in a string?
by jwkrahn (Abbot) on Mar 03, 2010 at 19:58 UTC

    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;