in reply to Re^2: Regex - remove characters (pattern not terminated)
in thread Regex - remove characters (pattern not terminated)

I would probably write that as:
my $pattern = '...'; $temp =~ s/${pattern}.{6}//s || $temp =~ s/.{6}${pattern}//s;
Not sure you need the /i, as your example uses numbers.
Instead of creating a whole new variable to match the 321, is there a simpler way to reverse the $pattern variable explicitly in a regex?
But what if the pattern is more complicated? And why do you consider creating a "whole new variable" such a big deal? I guess if your pattern is just a simple string without characters special to the regexp engine, you could write
$temp =~ s/${\scalar reverse $pattern}.{6}//s || $temp =~ s/.{6}${\scalar reverse $pattern}//s;
but I wouldn't. The following is, IMO, much simpler:
my $rpattern = reverse $pattern; $temp =~ s/$rpattern.{6}//s || $temp =~ s/.{6}$rpattern//s;

Replies are listed 'Best First'.
Re^4: Regex - remove characters (pattern not terminated)
by twaddlac (Novice) on Jun 29, 2010 at 19:03 UTC
    Thanks for the help but unfortunately I received the following error when implementing: $temp =~ s/${pattern}.{6}//s || $temp =~ s/.{6}${pattern}//s; The error message that I received was: "Use of uninitialized value in concatenation (.) or string".

    In regards to the reversing the pattern, the program that I am writing is going already VERY tedious since the data are so numerous and I'm trying to cut down on as much unnecessary memory usage as possible. It would be simpler to incorporate the 6 preceding/following characters into the pattern but unfortunately they are random sequences of "ACTG" 6 characters long. The possible situations that I would encounter while using a regex would be:
    1. Pattern found at the beginning of string
    2. Inverted pattern found at the beginning of string
    3. Pattern found at end of string
    4. Inverted Pattern found at the end of the string

    All of which would need the adjacent 6 characters removed as well as the pattern. Let me know if you can help me out! Thanks again!!