in reply to Regex - remove characters (pattern not terminated)

Just guessing, but maybe something like this?

my $temp_seq = "fooXXXXXXbar"; my $tag_seq = "foo"; $temp_seq =~ s/^($tag_seq)\w{6}(.*)/$1$2/; print $temp_seq; # "foobar"

(removes the six characters following the search pattern "foo", which must occur at the beginning of $temp_seq)

See s/// under Regexp-Quote-Like-Operators.

Another way to achieve the same would be

if ($temp_seq =~ /^$tag_seq/) { substr($temp_seq, $+[0], 6) = ''; }

where $+[0] is the position after the matched pattern.

Replies are listed 'Best First'.
Re^2: Regex - remove characters (pattern not terminated)
by AnomalousMonk (Archbishop) on Jun 28, 2010 at 20:32 UTC

    Or:  s/^($tag_seq)\w{6}/$1/
    Or (requires 5.10 due to use of \K):  s{ \A $tag_seq \K \w{6} }{}xms
    Or (requires 5.10 due to use of \K):  s{ \A \Q$tag_seq\E \K \w{6} }{}xms
    Or...

Re^2: Regex - remove characters (pattern not terminated)
by twaddlac (Novice) on Jun 29, 2010 at 18:47 UTC
    Thanks for the help! However, I would like to do a little bit more than just remove 6 characters following the pattern; I would like to remove the pattern as well as the 6 preceding/following characters.

    The following did remove the 6 following characters but I would like to integrate the pattern into the regex so that it will also be removed.:
    $temp_seq =~ s/^($tag_seq)\w{6}(.*)/$1$2/;
    Any suggestions?
      I would like to remove the pattern as well as the 6 preceding/following characters.

      Actually, that's even easier; see my other reply.

        Ahh thank you!! I don't know how I overlooked before, but forgive me! I'm going to try and implement the reverse pattern and I'm sure I'll run into more problems! I'll keep you update. Thanks again!!